Revision ac921d46

View differences:

tools/amalgam.py
61 61
parser.add_argument('sources', nargs='*', help='sources')
62 62

  
63 63
class File(object):
64
    def __init__(self, name):
64
    def __init__(self, name, parent_name):
65 65
        self.name = name
66
        self.parent_name = parent_name
66 67
        self.buf = StringIO()
67
        emit_file(self.buf, self.name)
68
        emit_file(self.buf, self.name, self.parent_name)
68 69

  
69 70
    def emit(self):
70 71
        print self.buf.getvalue(),
......
79 80

  
80 81
ignore_files = [i.strip() for i in args.ignore.split(',')]
81 82

  
82
def should_ignore(name):
83
def should_ignore(name, parent_name):
83 84
    return (name in already_included
84
            or not (args.strict or os.path.exists(resolve(name)))
85
            or not (args.strict or os.path.exists(resolve(name, parent_name)))
85 86
            or name in ignore_files)
86 87

  
87
def resolve(path):
88
def resolve(path, parent_name):
89
    path_from_parent = None
90
    if parent_name != None and not os.path.isabs(path):
91
        # calculate the path relative to the "parent_name" file, i.e. to the file
92
        # which includes the current one.
93
        path_from_parent = os.path.join(os.path.dirname(parent_name), path)
94

  
88 95
    if os.path.isabs(path) or os.path.exists(path):
89 96
        p = path
97
    elif path_from_parent != None and os.path.exists(path_from_parent):
98
        p = path_from_parent
90 99
    else:
91 100
        p = os.path.join(args.include_path, path)
92 101
    if os.path.exists(p) and not args.norel:
......
94 103
    # print >>sys.stderr, '%s -> %s (cwd %s)' % (path, p, os.getcwd())
95 104
    return p
96 105

  
97
def emit_line_directive(out, name):
106
def emit_line_directive(out, name, parent_name):
98 107
    print >>out, '''#ifdef %(prefix)s_MODULE_LINES
99 108
#line 1 "%(name)s"
100 109
#endif''' % dict(
101 110
    prefix = args.prefix,
102
    name = resolve(name),
111
    name = resolve(name, parent_name),
103 112
)
104 113

  
105
def emit_body(out, name):
106
    path = resolve(name)
107
    if not args.strict and not os.path.exists(path):
114
def emit_body(out, name, parent_name):
115
    resolved_name = resolve(name, parent_name)
116
    if not args.strict and not os.path.exists(resolved_name):
108 117
        print >>out, '#include "%s"' % (name,)
109 118
        return
110 119

  
111
    with open(resolve(name)) as f:
120
    with open(resolved_name) as f:
112 121
        for l in f:
113 122
            match = re.match('( *#include "(.*)")', l)
114 123
            if match:
115
                all, path = match.groups()
124
                all, path_to_include = match.groups()
116 125
                if args.autoinc:
117
                    if not should_ignore(path):
118
                        already_included.add(path)
119
                        includes.append(File(path))
126
                    if not should_ignore(path_to_include, parent_name):
127
                        already_included.add(path_to_include)
128
                        includes.append(File(path_to_include, resolved_name))
120 129
                print >>out, '/* Amalgamated: %s */' % (all,)
121 130
            else:
122 131
                print >>out, l,
123 132

  
124 133

  
125
def emit_file(out, name):
126
    emit_line_directive(out, name)
127
    emit_body(out, name)
134
def emit_file(out, name, parent_name):
135
    emit_line_directive(out, name, parent_name)
136
    emit_body(out, name, parent_name)
128 137

  
129 138
for i in args.sources:
130
    sources.append(File(i))
139
    sources.append(File(i, None))
131 140

  
132 141
if args.first:
133 142
    for inc in reversed(args.first.split(',')):

Also available in: Unified diff