-
Notifications
You must be signed in to change notification settings - Fork 26
/
configure.py
executable file
·181 lines (154 loc) · 6.8 KB
/
configure.py
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
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
#!/usr/bin/env python3
import os
import shlex
import shutil
import sys
import unittest
def main():
root_dir = os.path.dirname(os.path.realpath(__file__))
src_dir = os.path.join(root_dir, 'src')
m = MakefileGen(root_dir, src_dir, 'build', 'cpp', 'g++')
m.generate()
return 0
class MakefileGen:
def __init__(self, root_dir = '.', src_dir='src', build_dir='build',
ext='cpp', compiler='g++'):
# Save arguments
self.root_dir = root_dir
self.build_dir = build_dir
self.source_dir = src_dir
self.extension = ext
self.compiler = compiler
# Init member variables
self.files = []
self.tests = []
self.makefile_header = []
self.makefile_targets = []
self.makefile_footer = []
def generate(self):
self.clean_build_directory()
self.scan_source_directory()
self.create_makefile_header()
self.create_makefile_targets()
self.create_makefile_footer()
self.write_makefile()
print("Done.")
def clean_build_directory(self):
if os.path.isdir(self.build_dir):
shutil.rmtree(self.build_dir)
print("Existing build directory removed.")
def scan_source_directory(self):
self.files = [f for f in sorted(os.listdir(self.source_dir)) if
os.path.splitext(f)[1] == '.' + self.extension]
self.tests = [x[:-(len(self.extension)+1)] for x in self.files]
print("Source directory scanned for test files.")
def create_makefile_header(self):
self.makefile_header = []
# Get configuration options
if 'CXX' in os.environ:
CXX = os.environ['CXX']
else:
CXX = self.compiler
if 'CPPFLAGS' in os.environ:
CPPFLAGS = os.environ['CPPFLAGS']
else:
CPPFLAGS = ''
if 'CXXFLAGS' in os.environ:
CXXFLAGS = os.environ['CXXFLAGS']
else:
CXXFLAGS = ''
# Add command that can be used to recreate the same Makefile
self.makefile_header.append("# Command to regenerate this Makefile:")
self.makefile_header.append(
"# CXX={CXX} CPPFLAGS={CPPFLAGS} CXXFLAGS={CXXFLAGS} ".format(
CXX=shlex.quote(CXX),
CPPFLAGS=shlex.quote(CPPFLAGS),
CXXFLAGS=shlex.quote(CXXFLAGS))
+ sys.argv[0])
self.makefile_header.append("")
# Add directories and executables
self.makefile_header.append("ROOT := {}".format(self.root_dir))
self.makefile_header.append("SRC := {}".format(self.source_dir))
self.makefile_header.append("BLD := {}".format(self.build_dir))
self.makefile_header.append("RT := $(ROOT)/runtest.py")
# Add compiler executable if specified
self.makefile_header.append("CXX := {}".format(CXX))
# Add preprocessor flags if specified
self.makefile_header.append("CPPFLAGS := {}".format(CPPFLAGS))
# Add compiler flags if specified
self.makefile_header.append("CXXFLAGS := {}".format(CXXFLAGS))
# Enable colors by default
self.makefile_header.append("export COLORIZED ?= 1")
print("Makefile header generated.")
def create_makefile_targets(self):
# Add default target
self.makefile_targets.append("")
self.makefile_targets.append("# Default target")
self.makefile_targets.append("all: tests")
self.makefile_targets.append("")
self.makefile_targets.append("# Create build directory")
self.makefile_targets.append("$(BLD):")
self.makefile_targets.append("\t@mkdir -p $(BLD)")
self.makefile_targets.append("")
self.makefile_targets.append("# Run all tests")
self.makefile_targets.append("tests: {}".format(' '.join(self.tests)))
self.makefile_targets.append("")
self.makefile_targets.append("# Force rebuilding all tests")
self.makefile_targets.append("force: clean all")
# Add test targets
for test, filename in zip(self.tests, self.files):
self.makefile_targets.append("")
self.makefile_targets.append("# Build rules for {}".format(test))
self.makefile_targets.append(
"$(BLD)/{t}.o: $(SRC)/{t}.cpp | $(BLD)".format(t=test))
self.makefile_targets.append(
"\t@$(RT) {t} $(BLD)/{t}.log \\".format(t=test))
self.makefile_targets.append(
"\t\t$(CXX) -c $(CPPFLAGS) $(CXXFLAGS) -o \\")
self.makefile_targets.append(
"\t\t$(BLD)/{t}.o $(SRC)/{t}.{e}".format(
t=test, e=self.extension))
self.makefile_targets.append("")
self.makefile_targets.append("{t}: $(BLD)/{t}.o".format(t=test))
# Add additional targets
self.makefile_targets.append("")
self.makefile_targets.append("# Phony targets")
self.makefile_targets.append(
".PHONY: all clean distclean help")
self.makefile_targets.append("")
self.makefile_targets.append("# Clean up test files")
self.makefile_targets.append("clean:")
self.makefile_targets.append("\t@rm -f $(BLD)/*.o $(BLD)/*.log")
self.makefile_targets.append("")
self.makefile_targets.append("# Clean up everything")
self.makefile_targets.append("distclean: clean")
self.makefile_targets.append("\t@rm -f Makefile")
self.makefile_targets.append("\t@rm -f $(SRC)/runtest.pyc")
self.makefile_targets.append("")
self.makefile_targets.append("# Show targets that can be built")
self.makefile_targets.append("help:")
self.makefile_targets.append(
"\t@echo 'The following targets can be built:'")
self.makefile_targets.append("\t@echo ' all (default)'")
self.makefile_targets.append("\t@echo ' tests'")
self.makefile_targets.append("\t@echo ' force'")
self.makefile_targets.append("\t@echo ' clean'")
self.makefile_targets.append("\t@echo ' distclean'")
self.makefile_targets.append("\t@echo ' help'")
for t in self.tests:
self.makefile_targets.append("\t@echo ' {}'".format(t))
print("Makefile targets generated.")
def create_makefile_footer(self):
print("Makefile footer generated.")
def write_makefile(self):
self.makefile = 'Makefile'
with open(self.makefile, 'w') as f:
f.write('\n'.join(self.makefile_header))
f.write('\n')
f.write('\n'.join(self.makefile_targets))
f.write('\n')
f.write('\n'.join(self.makefile_footer))
f.write('\n')
print("Makefile written to disk.")
if __name__ == '__main__':
sys.exit(main())