source: mainline/tools/autotool.py@ 7dfd339

lfn serial ticket/834-toolchain-update topic/msim-upgrade topic/simplify-dev-export
Last change on this file since 7dfd339 was 9539be6, checked in by Martin Decky <martin@…>, 15 years ago

autotool now also detects values of UINT_MAX and friends

  • Property mode set to 100755
File size: 15.9 KB
Line 
1#!/usr/bin/env python
2#
3# Copyright (c) 2010 Martin Decky
4# All rights reserved.
5#
6# Redistribution and use in source and binary forms, with or without
7# modification, are permitted provided that the following conditions
8# are met:
9#
10# - Redistributions of source code must retain the above copyright
11# notice, this list of conditions and the following disclaimer.
12# - Redistributions in binary form must reproduce the above copyright
13# notice, this list of conditions and the following disclaimer in the
14# documentation and/or other materials provided with the distribution.
15# - The name of the author may not be used to endorse or promote products
16# derived from this software without specific prior written permission.
17#
18# THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
19# IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
20# OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
21# IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
22# INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
23# NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
24# DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
25# THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
26# (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
27# THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
28#
29
30"""
31Detect important prerequisites and parameters for building HelenOS
32"""
33
34import sys
35import os
36import shutil
37import re
38import time
39import subprocess
40
41SANDBOX = 'autotool'
42CONFIG = 'Makefile.config'
43MAKEFILE = 'Makefile.common'
44HEADER = 'common.h'
45GUARD = 'AUTOTOOL_COMMON_H_'
46
47PROBE_SOURCE = 'probe.c'
48PROBE_OUTPUT = 'probe.s'
49
50PACKAGE_BINUTILS = "usually part of binutils"
51PACKAGE_GCC = "preferably version 4.4.3 or newer"
52PACKAGE_CROSS = "use tools/toolchain.sh to build the cross-compiler toolchain"
53
54COMPILER_FAIL = "The compiler is probably not capable to compile HelenOS."
55
56PROBE_HEAD = """#define AUTOTOOL_DECLARE(category, subcategory, tag, name, value) \\
57 asm volatile ( \\
58 "AUTOTOOL_DECLARE\\t" category "\\t" subcategory "\\t" tag "\\t" name "\\t%[val]\\n" \\
59 : \\
60 : [val] "n" (value) \\
61 )
62
63#define DECLARE_INTSIZE(tag, type) \\
64 AUTOTOOL_DECLARE("intsize", "unsigned", tag, #type, sizeof(unsigned type)); \\
65 AUTOTOOL_DECLARE("intsize", "signed", tag, #type, sizeof(signed type))
66
67int main(int argc, char *argv[])
68{
69"""
70
71PROBE_TAIL = """}
72"""
73
74def read_config(fname, config):
75 "Read HelenOS build configuration"
76
77 inf = file(fname, 'r')
78
79 for line in inf:
80 res = re.match(r'^(?:#!# )?([^#]\w*)\s*=\s*(.*?)\s*$', line)
81 if (res):
82 config[res.group(1)] = res.group(2)
83
84 inf.close()
85
86def print_error(msg):
87 "Print a bold error message"
88
89 sys.stderr.write("\n")
90 sys.stderr.write("######################################################################\n")
91 sys.stderr.write("HelenOS build sanity check error:\n")
92 sys.stderr.write("\n")
93 sys.stderr.write("%s\n" % "\n".join(msg))
94 sys.stderr.write("######################################################################\n")
95 sys.stderr.write("\n")
96
97 sys.exit(1)
98
99def sandbox_enter():
100 "Create a temporal sandbox directory for running tests"
101
102 if (os.path.exists(SANDBOX)):
103 if (os.path.isdir(SANDBOX)):
104 try:
105 shutil.rmtree(SANDBOX)
106 except:
107 print_error(["Unable to cleanup the directory \"%s\"." % SANDBOX])
108 else:
109 print_error(["Please inspect and remove unexpected directory,",
110 "entry \"%s\"." % SANDBOX])
111
112 try:
113 os.mkdir(SANDBOX)
114 except:
115 print_error(["Unable to create sandbox directory \"%s\"." % SANDBOX])
116
117 owd = os.getcwd()
118 os.chdir(SANDBOX)
119
120 return owd
121
122def sandbox_leave(owd):
123 "Leave the temporal sandbox directory"
124
125 os.chdir(owd)
126
127def check_config(config, key):
128 "Check whether the configuration key exists"
129
130 if (not key in config):
131 print_error(["Build configuration of HelenOS does not contain %s." % key,
132 "Try running \"make config\" again.",
133 "If the problem persists, please contact the developers of HelenOS."])
134
135def check_common(common, key):
136 "Check whether the common key exists"
137
138 if (not key in common):
139 print_error(["Failed to determine the value %s." % key,
140 "Please contact the developers of HelenOS."])
141
142def check_app(args, name, details):
143 "Check whether an application can be executed"
144
145 try:
146 sys.stderr.write("Checking for %s ... " % args[0])
147 subprocess.Popen(args, stdout = subprocess.PIPE, stderr = subprocess.PIPE).wait()
148 except:
149 sys.stderr.write("failed\n")
150 print_error(["%s is missing." % name,
151 "",
152 "Execution of \"%s\" has failed. Please make sure that it" % " ".join(args),
153 "is installed in your system (%s)." % details])
154
155 sys.stderr.write("ok\n")
156
157def check_gcc(path, prefix, common, details):
158 "Check for GCC"
159
160 common['GCC'] = "%sgcc" % prefix
161
162 if (not path is None):
163 common['GCC'] = "%s/%s" % (path, common['GCC'])
164
165 check_app([common['GCC'], "--version"], "GNU GCC", details)
166
167def check_binutils(path, prefix, common, details):
168 "Check for binutils toolchain"
169
170 common['AS'] = "%sas" % prefix
171 common['LD'] = "%sld" % prefix
172 common['AR'] = "%sar" % prefix
173 common['OBJCOPY'] = "%sobjcopy" % prefix
174 common['OBJDUMP'] = "%sobjdump" % prefix
175 common['STRIP'] = "%sstrip" % prefix
176
177 if (not path is None):
178 for key in ["AS", "LD", "AR", "OBJCOPY", "OBJDUMP", "STRIP"]:
179 common[key] = "%s/%s" % (path, common[key])
180
181 check_app([common['AS'], "--version"], "GNU Assembler", details)
182 check_app([common['LD'], "--version"], "GNU Linker", details)
183 check_app([common['AR'], "--version"], "GNU Archiver", details)
184 check_app([common['OBJCOPY'], "--version"], "GNU Objcopy utility", details)
185 check_app([common['OBJDUMP'], "--version"], "GNU Objdump utility", details)
186 check_app([common['STRIP'], "--version"], "GNU strip", details)
187
188def probe_compiler(common, sizes):
189 "Generate, compile and parse probing source"
190
191 check_common(common, "CC")
192
193 outf = file(PROBE_SOURCE, 'w')
194 outf.write(PROBE_HEAD)
195
196 for typedef in sizes:
197 outf.write("\tDECLARE_INTSIZE(\"%s\", %s);\n" % (typedef['tag'], typedef['type']))
198
199 outf.write(PROBE_TAIL)
200 outf.close()
201
202 args = [common['CC'], "-S", "-o", PROBE_OUTPUT, PROBE_SOURCE]
203
204 try:
205 sys.stderr.write("Checking compiler properties ... ")
206 output = subprocess.Popen(args, stdout = subprocess.PIPE, stderr = subprocess.PIPE).communicate()
207 except:
208 sys.stderr.write("failed\n")
209 print_error(["Error executing \"%s\"." % " ".join(args),
210 "Make sure that the compiler works properly."])
211
212 if (not os.path.isfile(PROBE_OUTPUT)):
213 sys.stderr.write("failed\n")
214 print output[1]
215 print_error(["Error executing \"%s\"." % " ".join(args),
216 "The compiler did not produce the output file \"%s\"." % PROBE_OUTPUT,
217 "",
218 output[0],
219 output[1]])
220
221 sys.stderr.write("ok\n")
222
223 inf = file(PROBE_OUTPUT, 'r')
224 lines = inf.readlines()
225 inf.close()
226
227 unsigned_sizes = {}
228 signed_sizes = {}
229
230 unsigned_tags = {}
231 signed_tags = {}
232
233 for j in range(len(lines)):
234 tokens = lines[j].strip().split("\t")
235
236 if (len(tokens) > 0):
237 if (tokens[0] == "AUTOTOOL_DECLARE"):
238 if (len(tokens) < 5):
239 print_error(["Malformed declaration in \"%s\" on line %s." % (PROBE_OUTPUT, j), COMPILER_FAIL])
240
241 category = tokens[1]
242 subcategory = tokens[2]
243 tag = tokens[3]
244 name = tokens[4]
245 value = tokens[5]
246
247 if (category == "intsize"):
248 base = 10
249
250 if ((value.startswith('$')) or (value.startswith('#'))):
251 value = value[1:]
252
253 if (value.startswith('0x')):
254 value = value[2:]
255 base = 16
256
257 try:
258 value_int = int(value, base)
259 except:
260 print_error(["Integer value expected in \"%s\" on line %s." % (PROBE_OUTPUT, j), COMPILER_FAIL])
261
262 if (subcategory == "unsigned"):
263 unsigned_sizes[name] = value_int
264 unsigned_tags[tag] = value_int
265 elif (subcategory == "signed"):
266 signed_sizes[name] = value_int
267 signed_tags[tag] = value_int
268 else:
269 print_error(["Unexpected keyword \"%s\" in \"%s\" on line %s." % (subcategory, PROBE_OUTPUT, j), COMPILER_FAIL])
270
271 return {'unsigned_sizes' : unsigned_sizes, 'signed_sizes' : signed_sizes, 'unsigned_tags': unsigned_tags, 'signed_tags': signed_tags}
272
273def detect_uints(probe, bytes):
274 "Detect correct types for fixed-size integer types"
275
276 macros = []
277 typedefs = []
278
279 for b in bytes:
280 fnd = False
281 newtype = "uint%s_t" % (b * 8)
282
283 for name, value in probe['unsigned_sizes'].items():
284 if (value == b):
285 oldtype = "unsigned %s" % name
286 typedefs.append({'oldtype' : oldtype, 'newtype' : newtype})
287 fnd = True
288 break
289
290 if (not fnd):
291 print_error(['Unable to find appropriate integer type for %s' % newtype,
292 COMPILER_FAIL])
293
294
295 fnd = False
296 newtype = "int%s_t" % (b * 8)
297
298 for name, value in probe['signed_sizes'].items():
299 if (value == b):
300 oldtype = "signed %s" % name
301 typedefs.append({'oldtype' : oldtype, 'newtype' : newtype})
302 fnd = True
303 break
304
305 if (not fnd):
306 print_error(['Unable to find appropriate integer type for %s' % newtype,
307 COMPILER_FAIL])
308
309 for tag in ['CHAR', 'SHORT', 'INT', 'LONG', 'LLONG']:
310 fnd = False;
311 newmacro = "U%s" % tag
312
313 for name, value in probe['unsigned_tags'].items():
314 if (name == tag):
315 oldmacro = "UINT%s" % (value * 8)
316 macros.append({'oldmacro': "%s_MIN" % oldmacro, 'newmacro': "%s_MIN" % newmacro})
317 macros.append({'oldmacro': "%s_MAX" % oldmacro, 'newmacro': "%s_MAX" % newmacro})
318 fnd = True
319 break
320
321 if (not fnd):
322 print_error(['Unable to find appropriate size macro for %s' % newmacro,
323 COMPILER_FAIL])
324
325 fnd = False;
326 newmacro = tag
327
328 for name, value in probe['signed_tags'].items():
329 if (name == tag):
330 oldmacro = "INT%s" % (value * 8)
331 macros.append({'oldmacro': "%s_MIN" % oldmacro, 'newmacro': "%s_MIN" % newmacro})
332 macros.append({'oldmacro': "%s_MAX" % oldmacro, 'newmacro': "%s_MAX" % newmacro})
333 fnd = True
334 break
335
336 if (not fnd):
337 print_error(['Unable to find appropriate size macro for %s' % newmacro,
338 COMPILER_FAIL])
339
340 return {'macros': macros, 'typedefs': typedefs}
341
342def create_makefile(mkname, common):
343 "Create makefile output"
344
345 outmk = file(mkname, 'w')
346
347 outmk.write('#########################################\n')
348 outmk.write('## AUTO-GENERATED FILE, DO NOT EDIT!!! ##\n')
349 outmk.write('#########################################\n\n')
350
351 for key, value in common.items():
352 outmk.write('%s = %s\n' % (key, value))
353
354 outmk.close()
355
356def create_header(hdname, maps):
357 "Create header output"
358
359 outhd = file(hdname, 'w')
360
361 outhd.write('/***************************************\n')
362 outhd.write(' * AUTO-GENERATED FILE, DO NOT EDIT!!! *\n')
363 outhd.write(' ***************************************/\n\n')
364
365 outhd.write('#ifndef %s\n' % GUARD)
366 outhd.write('#define %s\n\n' % GUARD)
367
368 for macro in maps['macros']:
369 outhd.write('#define %s %s\n' % (macro['newmacro'], macro['oldmacro']))
370
371 outhd.write('\n')
372
373 for typedef in maps['typedefs']:
374 outhd.write('typedef %s %s;\n' % (typedef['oldtype'], typedef['newtype']))
375
376 outhd.write('\n#endif\n')
377 outhd.close()
378
379def main():
380 config = {}
381 common = {}
382
383 # Read and check configuration
384 if os.path.exists(CONFIG):
385 read_config(CONFIG, config)
386 else:
387 print_error(["Configuration file %s not found! Make sure that the" % CONFIG,
388 "configuration phase of HelenOS build went OK. Try running",
389 "\"make config\" again."])
390
391 check_config(config, "PLATFORM")
392 check_config(config, "COMPILER")
393 check_config(config, "BARCH")
394
395 # Cross-compiler prefix
396 if ('CROSS_PREFIX' in os.environ):
397 cross_prefix = os.environ['CROSS_PREFIX']
398 else:
399 cross_prefix = "/usr/local"
400
401 # Prefix binutils tools on Solaris
402 if (os.uname()[0] == "SunOS"):
403 binutils_prefix = "g"
404 else:
405 binutils_prefix = ""
406
407 owd = sandbox_enter()
408
409 try:
410 # Common utilities
411 check_app(["ln", "--version"], "Symlink utility", "usually part of coreutils")
412 check_app(["rm", "--version"], "File remove utility", "usually part of coreutils")
413 check_app(["mkdir", "--version"], "Directory creation utility", "usually part of coreutils")
414 check_app(["cp", "--version"], "Copy utility", "usually part of coreutils")
415 check_app(["find", "--version"], "Find utility", "usually part of findutils")
416 check_app(["diff", "--version"], "Diff utility", "usually part of diffutils")
417 check_app(["make", "--version"], "Make utility", "preferably GNU Make")
418 check_app(["makedepend", "-f", "-"], "Makedepend utility", "usually part of imake or xutils")
419
420 # Compiler
421 if (config['COMPILER'] == "gcc_cross"):
422 if (config['PLATFORM'] == "abs32le"):
423 check_config(config, "CROSS_TARGET")
424 target = config['CROSS_TARGET']
425
426 if (config['CROSS_TARGET'] == "arm32"):
427 gnu_target = "arm-linux-gnu"
428
429 if (config['CROSS_TARGET'] == "ia32"):
430 gnu_target = "i686-pc-linux-gnu"
431
432 if (config['CROSS_TARGET'] == "mips32"):
433 gnu_target = "mipsel-linux-gnu"
434
435 if (config['PLATFORM'] == "amd64"):
436 target = config['PLATFORM']
437 gnu_target = "amd64-linux-gnu"
438
439 if (config['PLATFORM'] == "arm32"):
440 target = config['PLATFORM']
441 gnu_target = "arm-linux-gnu"
442
443 if (config['PLATFORM'] == "ia32"):
444 target = config['PLATFORM']
445 gnu_target = "i686-pc-linux-gnu"
446
447 if (config['PLATFORM'] == "ia64"):
448 target = config['PLATFORM']
449 gnu_target = "ia64-pc-linux-gnu"
450
451 if (config['PLATFORM'] == "mips32"):
452 check_config(config, "MACHINE")
453
454 if ((config['MACHINE'] == "lgxemul") or (config['MACHINE'] == "msim")):
455 target = config['PLATFORM']
456 gnu_target = "mipsel-linux-gnu"
457
458 if (config['MACHINE'] == "bgxemul"):
459 target = "mips32eb"
460 gnu_target = "mips-linux-gnu"
461
462 if (config['PLATFORM'] == "ppc32"):
463 target = config['PLATFORM']
464 gnu_target = "ppc-linux-gnu"
465
466 if (config['PLATFORM'] == "sparc64"):
467 target = config['PLATFORM']
468 gnu_target = "sparc64-linux-gnu"
469
470 path = "%s/%s/bin" % (cross_prefix, target)
471 prefix = "%s-" % gnu_target
472
473 check_gcc(path, prefix, common, PACKAGE_CROSS)
474 check_binutils(path, prefix, common, PACKAGE_CROSS)
475
476 check_common(common, "GCC")
477 common['CC'] = common['GCC']
478
479 if (config['COMPILER'] == "gcc_native"):
480 check_gcc(None, "", common, PACKAGE_GCC)
481 check_binutils(None, binutils_prefix, common, PACKAGE_BINUTILS)
482
483 check_common(common, "GCC")
484 common['CC'] = common['GCC']
485
486 if (config['COMPILER'] == "icc"):
487 common['CC'] = "icc"
488 check_app([common['CC'], "-V"], "Intel C++ Compiler", "support is experimental")
489 check_gcc(None, "", common, PACKAGE_GCC)
490 check_binutils(None, binutils_prefix, common, PACKAGE_BINUTILS)
491
492 if (config['COMPILER'] == "suncc"):
493 common['CC'] = "suncc"
494 check_app([common['CC'], "-V"], "Sun Studio Compiler", "support is experimental")
495 check_gcc(None, "", common, PACKAGE_GCC)
496 check_binutils(None, binutils_prefix, common, PACKAGE_BINUTILS)
497
498 if (config['COMPILER'] == "clang"):
499 common['CC'] = "clang"
500 check_app([common['CC'], "--version"], "Clang compiler", "preferably version 1.0 or newer")
501 check_gcc(None, "", common, PACKAGE_GCC)
502 check_binutils(None, binutils_prefix, common, PACKAGE_BINUTILS)
503
504 # Platform-specific utilities
505 if ((config['BARCH'] == "amd64") or (config['BARCH'] == "ia32") or (config['BARCH'] == "ppc32") or (config['BARCH'] == "sparc64")):
506 check_app(["mkisofs", "--version"], "ISO 9660 creation utility", "usually part of genisoimage")
507
508 probe = probe_compiler(common,
509 [
510 {'type': 'char', 'tag': 'CHAR'},
511 {'type': 'short int', 'tag': 'SHORT'},
512 {'type': 'int', 'tag': 'INT'},
513 {'type': 'long int', 'tag': 'LONG'},
514 {'type': 'long long int', 'tag': 'LLONG'}
515 ]
516 )
517
518 maps = detect_uints(probe, [1, 2, 4, 8])
519
520 finally:
521 sandbox_leave(owd)
522
523 create_makefile(MAKEFILE, common)
524 create_header(HEADER, maps)
525
526 return 0
527
528if __name__ == '__main__':
529 sys.exit(main())
Note: See TracBrowser for help on using the repository browser.