Commit ac921d46 authored by Dmitry Frank's avatar Dmitry Frank Committed by Cesanta Bot

Improve includes in the amalgamation script

Now it also tries to find the included file relatively to the "parent"
file. This is required for amalgamation of the frozen into mjs, since
`frozen.c` includes `frozen.h` instead of `frozen/frozen.h`.

PUBLISHED_FROM=8f670a1509a30cfdfb22e454b7851f4cf28fb98d
parent f046434f
...@@ -61,10 +61,11 @@ parser.add_argument('-I', default=".", dest='include_path', help='include path') ...@@ -61,10 +61,11 @@ parser.add_argument('-I', default=".", dest='include_path', help='include path')
parser.add_argument('sources', nargs='*', help='sources') parser.add_argument('sources', nargs='*', help='sources')
class File(object): class File(object):
def __init__(self, name): def __init__(self, name, parent_name):
self.name = name self.name = name
self.parent_name = parent_name
self.buf = StringIO() self.buf = StringIO()
emit_file(self.buf, self.name) emit_file(self.buf, self.name, self.parent_name)
def emit(self): def emit(self):
print self.buf.getvalue(), print self.buf.getvalue(),
...@@ -79,14 +80,22 @@ already_included = set() ...@@ -79,14 +80,22 @@ already_included = set()
ignore_files = [i.strip() for i in args.ignore.split(',')] ignore_files = [i.strip() for i in args.ignore.split(',')]
def should_ignore(name): def should_ignore(name, parent_name):
return (name in already_included return (name in already_included
or not (args.strict or os.path.exists(resolve(name))) or not (args.strict or os.path.exists(resolve(name, parent_name)))
or name in ignore_files) or name in ignore_files)
def resolve(path): def resolve(path, parent_name):
path_from_parent = None
if parent_name != None and not os.path.isabs(path):
# calculate the path relative to the "parent_name" file, i.e. to the file
# which includes the current one.
path_from_parent = os.path.join(os.path.dirname(parent_name), path)
if os.path.isabs(path) or os.path.exists(path): if os.path.isabs(path) or os.path.exists(path):
p = path p = path
elif path_from_parent != None and os.path.exists(path_from_parent):
p = path_from_parent
else: else:
p = os.path.join(args.include_path, path) p = os.path.join(args.include_path, path)
if os.path.exists(p) and not args.norel: if os.path.exists(p) and not args.norel:
...@@ -94,40 +103,40 @@ def resolve(path): ...@@ -94,40 +103,40 @@ def resolve(path):
# print >>sys.stderr, '%s -> %s (cwd %s)' % (path, p, os.getcwd()) # print >>sys.stderr, '%s -> %s (cwd %s)' % (path, p, os.getcwd())
return p return p
def emit_line_directive(out, name): def emit_line_directive(out, name, parent_name):
print >>out, '''#ifdef %(prefix)s_MODULE_LINES print >>out, '''#ifdef %(prefix)s_MODULE_LINES
#line 1 "%(name)s" #line 1 "%(name)s"
#endif''' % dict( #endif''' % dict(
prefix = args.prefix, prefix = args.prefix,
name = resolve(name), name = resolve(name, parent_name),
) )
def emit_body(out, name): def emit_body(out, name, parent_name):
path = resolve(name) resolved_name = resolve(name, parent_name)
if not args.strict and not os.path.exists(path): if not args.strict and not os.path.exists(resolved_name):
print >>out, '#include "%s"' % (name,) print >>out, '#include "%s"' % (name,)
return return
with open(resolve(name)) as f: with open(resolved_name) as f:
for l in f: for l in f:
match = re.match('( *#include "(.*)")', l) match = re.match('( *#include "(.*)")', l)
if match: if match:
all, path = match.groups() all, path_to_include = match.groups()
if args.autoinc: if args.autoinc:
if not should_ignore(path): if not should_ignore(path_to_include, parent_name):
already_included.add(path) already_included.add(path_to_include)
includes.append(File(path)) includes.append(File(path_to_include, resolved_name))
print >>out, '/* Amalgamated: %s */' % (all,) print >>out, '/* Amalgamated: %s */' % (all,)
else: else:
print >>out, l, print >>out, l,
def emit_file(out, name): def emit_file(out, name, parent_name):
emit_line_directive(out, name) emit_line_directive(out, name, parent_name)
emit_body(out, name) emit_body(out, name, parent_name)
for i in args.sources: for i in args.sources:
sources.append(File(i)) sources.append(File(i, None))
if args.first: if args.first:
for inc in reversed(args.first.split(',')): for inc in reversed(args.first.split(',')):
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment