-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmakemake
More file actions
executable file
·154 lines (126 loc) · 4.67 KB
/
Copy pathmakemake
File metadata and controls
executable file
·154 lines (126 loc) · 4.67 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
#!/usr/bin/python
# Copyright (c) 2011 by Chris Westin. See LICENSE.txt.
# phoenix4c - https://github.com/cwestin/phoenix4c
#
# This script generates a makefile for the library. It assumes a particular
# set of directories that are part of the library, but addresses them
# relatively (or uses os.getcwd()) so that the library directory can be
# put anywhere.
#
# To use, cd to the containing directory, and issue "./makemake", followed by
# "make"
import os
import os.path
import re
# keep around some compiled regular expressions we use repeatedly
filebasere = re.compile('(?P<filebase>.*)\\.c$')
includere = re.compile('^#\\s*include\\s*"(?P<incfile>.*)"')
def writeobjdeps(makefilefd, filebase):
srcname = 'src/' + filebase + '.c'
srcfd = open(srcname)
if (srcfd is None):
print 'cannot open %s' % srcname
return
# the object file depends on the source file
objname = 'obj/' + filebase + '.o'
makefilefd.write('%s:\t%s' % (objname, srcname))
for srcline in srcfd:
mo = includere.search(srcline)
if (mo is None):
continue
incfile = mo.group('incfile')
if (incfile is None):
continue
# the object also depends on this include file
makefilefd.write(' include/%s' % incfile)
# end the dependency list
makefilefd.write('\n')
srcfd.close()
# write the rule
makefilefd.write('\t$(CC) $(CFLAGS) -c -o %s %s\n' % (objname, srcname))
makefilefd.write('\t$(AR) $(ARFLAGS) lib/$(LIBRARY) %s\n' % objname)
makefilefd.write('\n')
# the test file depends on the test source file and library
testsrc = 'testsrc/test%s.c' % filebase
if (os.path.isfile(testsrc)):
makefilefd.write('test/test%s:\ttestsrc/test%s.c obj/%s.o lib/$(LIBRARY)\n' %
(filebase, filebase, filebase))
makefilefd.write('\t$(CC) $(CFLAGS) -o $@ testsrc/test%s.c -Wl,-l:lib/$(LIBRARY)\n' % filebase)
makefilefd.write('\n')
def visitsrcdir(makefilefd):
names = os.listdir('src')
if (names is None) or (len(names) == 0):
return
filebases = []
makefilefd.write('OBJECTS =')
for srcfilename in names:
mo = filebasere.search(srcfilename)
# if we found a file that isn't a source file, alert the user
if (mo is None):
# don't report on emacs backup files
if (srcfilename[-1] == '~'):
continue
print '%s is not a source file name' % srcfilename
return
filebase = mo.group('filebase')
if (filebase is None):
print '%s is not a source file name' % srcfilename
return
filebases.append(filebase)
makefilefd.write(' obj/%s.o' % filebase)
# end the OBJECTS list
makefilefd.write('\n')
makefilefd.write('\n')
# the rule to build the library
makefilefd.write('lib/$(LIBRARY):\t$(OBJECTS)\n')
makefilefd.write('\n')
# clean
makefilefd.write('.PHONY:\tclean\n')
makefilefd.write('\n')
makefilefd.write('clean:\n')
makefilefd.write('\trm *~ src/*~ testsrc/*~ obj/*.o lib/*.a test/*\n')
makefilefd.write('\n')
# add the individual files' dependencies
makefilefd.write('\n')
for srcbase in filebases:
writeobjdeps(makefilefd, srcbase)
# tests
testcounter = 0
makefilefd.write('TESTS =')
for srcbase in filebases:
if (os.path.isfile('testsrc/test%s.c' % srcbase)):
makefilefd.write(' test/test%s' % srcbase)
testcounter = testcounter + 1
makefilefd.write('\n')
makefilefd.write('\n')
if (testcounter > 0):
makefilefd.write('.PHONY:\ttests\n')
makefilefd.write('\n')
makefilefd.write('tests:\t$(TESTS)\n')
makefilefd.write('\n')
makefilefd.write('.PHONY:\ttestall\n')
makefilefd.write('\n')
makefilefd.write('testall:\ttests\n')
for srcbase in filebases:
if (not os.path.isfile('testsrc/test%s.c' % srcbase)):
continue
makefilefd.write('\ttest/test%s\n' % srcbase)
makefilefd.write('\n')
if __name__ == '__main__':
# create the makefile
makefilefd = open('Makefile', 'w')
cwd = os.getcwd()
# standard preamble
makefilefd.write('# Makefile generated by makemake\n')
makefilefd.write('\n')
makefilefd.write('CC = g++\n')
makefilefd.write('INCLUDE = %s/include/\n' % cwd)
makefilefd.write('CFLAGS = -Wall -I$(INCLUDE) -ggdb\n')
makefilefd.write('\n')
makefilefd.write('AR = ar\n')
makefilefd.write('ARFLAGS = -rsc\n')
makefilefd.write('LIBRARY = phoenix4c.a')
makefilefd.write('\n')
visitsrcdir(makefilefd)
makefilefd.close()
pass