-
Notifications
You must be signed in to change notification settings - Fork 58
/
configbuilder.py
187 lines (162 loc) · 6.21 KB
/
configbuilder.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
182
183
184
185
186
# Copyright 2012 David Malcolm <[email protected]>
# Copyright 2012 Red Hat, Inc.
#
# This is free software: you can redistribute it and/or modify it
# under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful, but
# WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
# General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program. If not, see
# <http://www.gnu.org/licenses/>.
import os
import shutil
from subprocess import Popen, PIPE, check_output
import sys
def indent(prefix, text):
text = str(text)
return '\n'.join([prefix + line
for line in text.splitlines()])
class ConfigurationFailure(Exception):
pass
class CheckFor:
"""
Context manager for wrapping a feature test
The feature test should raise ConfigurationFailure to signify a failure
"""
def __init__(self, initmsg, mandatory, okmsg=None, failmsg='failed'):
self.initmsg = initmsg
self.mandatory = mandatory
self.okmsg = okmsg
self.failmsg = failmsg
self.result = None
# context manager hooks:
def __enter__(self):
sys.stdout.write('%s... ' % self.initmsg) # no newline
return self
def __exit__(self, exc_type, exc_val, exc_tb):
if exc_type:
# exception occurred:
self.result = False
# is it one of ours, signifiying the failure of a test?
if isinstance(exc_val, ConfigurationFailure):
# Write the failure message:
sys.stdout.write('%s\n' % self.failmsg)
if self.mandatory:
# Print diagnostic information:
print(exc_val)
# terminate the build
sys.exit(1)
else:
return True # swallow the exception
else:
# some kind of unexpected error; propagate it:
return False
else:
# Assume success:
self.result = True
# Write the success message:
if self.okmsg:
sys.stdout.write('%s\n' % self.okmsg)
def succeeded(self):
# Did this test succeed?
return self.result
class Result:
pass
class OptionFlag(Result):
# the outcome of a feature test
def __init__(self, description, flag, defn):
self.description = description
self.flag = flag
self.defn = defn
def write_to(self, f):
f.write('/* %s */\n' % self.description)
if self.flag:
f.write('#define %s\n\n' % self.defn)
else:
f.write('#undef %s\n\n' % self.defn)
class ConfigBuilder:
def __init__(self, argv):
import argparse
parser = argparse.ArgumentParser()
parser.add_argument('-o', '--output-file')
args = parser.parse_args(argv[1:])
#print(args)
self.filename = args.output_file
self.dirname = 'config-tests'
if os.path.exists(self.dirname):
shutil.rmtree(self.dirname)
os.mkdir(self.dirname)
self.testid = 0
self.results = []
def make_test_dir(self, test):
self.testid += 1
dirname = ('%05i-' % self.testid) + test.initmsg
dirname = '-'.join(dirname.split())
dirpath = os.path.join(self.dirname, dirname)
os.mkdir(dirpath)
return dirpath
def write_outcome(self):
sys.stdout.write('writing %s\n' % self.filename)
with open(self.filename, 'w') as f:
f.write('/* autogenerated header file */\n\n')
for r in self.results:
r.write_to(f)
def compile(self, test, src, extraargs):
dirpath = self.make_test_dir(test)
srcpath = os.path.join(dirpath, 'feature-test.c')
with open(srcpath, 'w') as f:
f.write(src)
outpath = os.path.join(dirpath, 'feature-test.o')
args= [os.environ.get('CC', 'gcc'),
'-c', # don't run the linker (no main)
'-o', outpath]
args += extraargs
args += [srcpath]
p = Popen(args, stdout=PIPE, stderr=PIPE)
stdout, stderr = p.communicate()
c = p.wait()
if c != 0:
class CompilationError(ConfigurationFailure):
def __str__(self):
result = 'Test compilation failed with exit code %i\n' % c
result += ' The command was:\n'
result += ' %s\n' % ' '.join(args)
result += ' The source was: (in %s)\n' % srcpath
result += indent(' ', src) + '\n'
result += ' The stderr was:\n'
result += indent(' ', stderr) + '\n'
return result
raise CompilationError()
def capture_shell_output(self, initmsg, cmd):
with CheckFor(initmsg,
mandatory=True) as test:
out = check_output(cmd,
shell=True) # input must be trusted
out = str(out.decode())
sys.stdout.write('%s\n' % out.strip())
return out
def test_for_mandatory_c_header(self, header, extraargs):
with CheckFor('checking for %s' % header,
okmsg='found',
failmsg='not found',
mandatory=True) as test:
self.compile(test,
src='#include <%s>' % header,
extraargs=extraargs)
def test_c_compilation(self, initmsg, src, extraargs, description, defn):
with CheckFor(initmsg,
okmsg='yes',
failmsg='no',
mandatory=False) as test:
self.compile(test,
src,
extraargs)
self.results.append(OptionFlag(description,
test.succeeded(),
defn))