3
# USAGE: gen-build.py TYPE
5
# where TYPE is one of: make, dsp, vcproj
7
# It reads build.conf from the current directory, and produces its output
8
# into the current directory.
22
# legal platforms: aix, beos, netware, os2, os390, unix, win32
23
# 'make' users: aix, beos, os2, os390, unix
25
PLATFORMS = [ 'aix', 'beos', 'netware', 'os2', 'os390', 'unix', 'win32' ]
33
# note: MAKE_PLATFORMS is an ordered set. we want to generate unix symbols
34
# first, so that the later platforms can reference them.
38
parser = ConfigParser.ConfigParser()
39
parser.read('build.conf')
41
headers = get_files(parser.get('options', 'headers'))
43
# compute the relevant headers, along with the implied includes
46
legal_deps[os.path.basename(fname)] = fname
50
h_deps[os.path.basename(fname)] = extract_deps(fname, legal_deps)
53
f = open('build-outputs.mk', 'w')
54
f.write('# DO NOT EDIT. AUTOMATICALLY GENERATED.\n\n')
56
# write out the platform-independent files
57
files = get_files(parser.get('options', 'paths'))
58
objects, dirs = write_objects(f, legal_deps, h_deps, files)
59
f.write('\nOBJECTS_all = %s\n\n' % string.join(objects))
61
# for each platform and each subdirectory holding platform-specific files,
62
# write out their compilation rules, and an OBJECT_<subdir>_<plat> symbol.
63
for platform, parent in MAKE_PLATFORMS:
65
# record the object symbols to build for each platform
66
group = [ '$(OBJECTS_all)' ]
68
for subdir in string.split(parser.get('options', 'platform_dirs')):
69
path = '%s/%s' % (subdir, platform)
70
if not os.path.exists(path):
71
# this subdir doesn't have a subdir for this platform, so we'll
72
# use the parent-platform's set of symbols
74
group.append('$(OBJECTS_%s_%s)' % (subdir, parent))
77
# remember that this directory has files/objects
80
# write out the compilation lines for this subdir
81
files = get_files(path + '/*.c')
82
objects, _unused = write_objects(f, legal_deps, h_deps, files)
84
symname = 'OBJECTS_%s_%s' % (subdir, platform)
86
# and write the symbol for the whole group
87
f.write('\n%s = %s\n\n' % (symname, string.join(objects)))
89
# and include that symbol in the group
90
group.append('$(%s)' % symname)
92
# write out a symbol which contains the necessary files
93
f.write('OBJECTS_%s = %s\n\n' % (platform, string.join(group)))
95
f.write('HEADERS = $(top_srcdir)/%s\n\n' % string.join(headers, ' $(top_srcdir)/'))
96
f.write('SOURCE_DIRS = %s $(EXTRA_SOURCE_DIRS)\n\n' % string.join(dirs.keys()))
98
# Build a list of all necessary directories in build tree
100
for dir in dirs.keys():
104
d = os.path.dirname(d)
106
# Sort so 'foo' is before 'foo/bar'
107
keys = alldirs.keys()
109
f.write('BUILD_DIRS = %s\n\n' % string.join(keys))
111
f.write('.make.dirs: $(srcdir)/build-outputs.mk\n' \
112
'\t@for d in $(BUILD_DIRS); do test -d $$d || mkdir $$d; done\n' \
113
'\t@echo timestamp > $@\n')
116
def write_objects(f, legal_deps, h_deps, files):
121
assert file[-2:] == '.c'
122
obj = file[:-2] + '.lo'
125
dirs[os.path.dirname(file)] = None
127
# what headers does this file include, along with the implied headers
128
deps = extract_deps(file, legal_deps)
129
for hdr in deps.keys():
130
deps.update(h_deps.get(hdr, {}))
132
f.write('%s: %s .make.dirs %s\n' % (obj, file, string.join(deps.values())))
137
def extract_deps(fname, legal_deps):
138
"Extract the headers this file includes."
140
for line in open(fname).readlines():
141
if line[:8] != '#include':
143
inc = _re_include.match(line).group(1)
144
if inc in legal_deps.keys():
145
deps[inc] = legal_deps[inc]
147
_re_include = re.compile('#include *["<](.*)[">]')
150
def resolve_deps(header_deps):
151
"Alter the provided dictionary to flatten includes-of-includes."
155
for hdr, deps in header_deps.items():
158
for dep in deps.keys():
159
deps.update(header_deps.get(dep, {}))
160
if len(deps) != start:
164
def get_files(patterns):
166
for pat in string.split(patterns):
167
files.extend(glob.glob(pat))
171
if __name__ == '__main__':