source: mainline/tools/autotool.py@ 4b9a410

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

avoid unwanted bias towards "larger" prefixes/suffixes

  • Property mode set to 100755
File size: 18.3 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.5.1 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, strc, conc, value) \\
57 asm volatile ( \\
58 "AUTOTOOL_DECLARE\\t" category "\\t" subcategory "\\t" tag "\\t" name "\\t" strc "\\t" conc "\\t%[val]\\n" \\
59 : \\
60 : [val] "n" (value) \\
61 )
62
63#define DECLARE_INTSIZE(tag, type, strc, conc) \\
64 AUTOTOOL_DECLARE("intsize", "unsigned", tag, #type, strc, conc, sizeof(unsigned type)); \\
65 AUTOTOOL_DECLARE("intsize", "signed", tag, #type, strc, conc, 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 = open(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 = open(PROBE_SOURCE, 'w')
194 outf.write(PROBE_HEAD)
195
196 for typedef in sizes:
197 outf.write("\tDECLARE_INTSIZE(\"%s\", %s, %s, %s);\n" % (typedef['tag'], typedef['type'], typedef['strc'], typedef['conc']))
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 = open(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 unsigned_strcs = {}
234 signed_strcs = {}
235
236 unsigned_concs = {}
237 signed_concs = {}
238
239 for j in range(len(lines)):
240 tokens = lines[j].strip().split("\t")
241
242 if (len(tokens) > 0):
243 if (tokens[0] == "AUTOTOOL_DECLARE"):
244 if (len(tokens) < 7):
245 print_error(["Malformed declaration in \"%s\" on line %s." % (PROBE_OUTPUT, j), COMPILER_FAIL])
246
247 category = tokens[1]
248 subcategory = tokens[2]
249 tag = tokens[3]
250 name = tokens[4]
251 strc = tokens[5]
252 conc = tokens[6]
253 value = tokens[7]
254
255 if (category == "intsize"):
256 base = 10
257
258 if ((value.startswith('$')) or (value.startswith('#'))):
259 value = value[1:]
260
261 if (value.startswith('0x')):
262 value = value[2:]
263 base = 16
264
265 try:
266 value_int = int(value, base)
267 except:
268 print_error(["Integer value expected in \"%s\" on line %s." % (PROBE_OUTPUT, j), COMPILER_FAIL])
269
270 if (subcategory == "unsigned"):
271 unsigned_sizes[name] = value_int
272 unsigned_tags[tag] = value_int
273 unsigned_strcs[strc] = value_int
274 unsigned_concs[conc] = value_int
275 elif (subcategory == "signed"):
276 signed_sizes[name] = value_int
277 signed_tags[tag] = value_int
278 signed_strcs[strc] = value_int
279 signed_concs[conc] = value_int
280 else:
281 print_error(["Unexpected keyword \"%s\" in \"%s\" on line %s." % (subcategory, PROBE_OUTPUT, j), COMPILER_FAIL])
282
283 return {'unsigned_sizes': unsigned_sizes, 'signed_sizes': signed_sizes, 'unsigned_tags': unsigned_tags, 'signed_tags': signed_tags, 'unsigned_strcs': unsigned_strcs, 'signed_strcs': signed_strcs, 'unsigned_concs': unsigned_concs, 'signed_concs': signed_concs}
284
285def detect_uints(probe, bytes):
286 "Detect correct types for fixed-size integer types"
287
288 macros = []
289 typedefs = []
290
291 for b in bytes:
292 fnd = False
293 for name, value in probe['unsigned_sizes'].items():
294 if (value == b):
295 typedefs.append({'oldtype': "unsigned %s" % name, 'newtype': "uint%u_t" % (b * 8)})
296 fnd = True
297 break
298
299 if (not fnd):
300 print_error(['Unable to find appropriate unsigned integer type for %u bytes' % b,
301 COMPILER_FAIL])
302
303
304 fnd = False
305 for name, value in probe['signed_sizes'].items():
306 if (value == b):
307 typedefs.append({'oldtype': "signed %s" % name, 'newtype': "int%u_t" % (b * 8)})
308 fnd = True
309 break
310
311 if (not fnd):
312 print_error(['Unable to find appropriate signed integer type for %u bytes' % b,
313 COMPILER_FAIL])
314
315
316 fnd = False
317 for name, value in probe['unsigned_strcs'].items():
318 if (value == b):
319 macros.append({'oldmacro': "\"%so\"" % name, 'newmacro': "PRIo%u" % (b * 8)})
320 macros.append({'oldmacro': "\"%su\"" % name, 'newmacro': "PRIu%u" % (b * 8)})
321 macros.append({'oldmacro': "\"%sx\"" % name, 'newmacro': "PRIx%u" % (b * 8)})
322 macros.append({'oldmacro': "\"%sX\"" % name, 'newmacro': "PRIX%u" % (b * 8)})
323 fnd = True
324 break
325
326 if (not fnd):
327 print_error(['Unable to find appropriate unsigned printf formatter for %u bytes' % b,
328 COMPILER_FAIL])
329
330
331 fnd = False
332 for name, value in probe['signed_strcs'].items():
333 if (value == b):
334 macros.append({'oldmacro': "\"%sd\"" % name, 'newmacro': "PRId%u" % (b * 8)})
335 fnd = True
336 break
337
338 if (not fnd):
339 print_error(['Unable to find appropriate signed printf formatter for %u bytes' % b,
340 COMPILER_FAIL])
341
342
343 fnd = False
344 for name, value in probe['unsigned_concs'].items():
345 if (value == b):
346 if ((name.startswith('@')) or (name == "")):
347 macros.append({'oldmacro': "c ## U", 'newmacro': "UINT%u_C(c)" % (b * 8)})
348 else:
349 macros.append({'oldmacro': "c ## U%s" % name, 'newmacro': "UINT%u_C(c)" % (b * 8)})
350 fnd = True
351 break
352
353 if (not fnd):
354 print_error(['Unable to find appropriate unsigned literal macro for %u bytes' % b,
355 COMPILER_FAIL])
356
357
358 fnd = False
359 for name, value in probe['signed_concs'].items():
360 if (value == b):
361 if ((name.startswith('@')) or (name == "")):
362 macros.append({'oldmacro': "c", 'newmacro': "INT%u_C(c)" % (b * 8)})
363 else:
364 macros.append({'oldmacro': "c ## %s" % name, 'newmacro': "INT%u_C(c)" % (b * 8)})
365 fnd = True
366 break
367
368 if (not fnd):
369 print_error(['Unable to find appropriate unsigned literal macro for %u bytes' % b,
370 COMPILER_FAIL])
371
372 for tag in ['CHAR', 'SHORT', 'INT', 'LONG', 'LLONG']:
373 fnd = False;
374 newmacro = "U%s" % tag
375
376 for name, value in probe['unsigned_tags'].items():
377 if (name == tag):
378 oldmacro = "UINT%s" % (value * 8)
379 macros.append({'oldmacro': "%s_MIN" % oldmacro, 'newmacro': "%s_MIN" % newmacro})
380 macros.append({'oldmacro': "%s_MAX" % oldmacro, 'newmacro': "%s_MAX" % newmacro})
381 fnd = True
382 break
383
384 if (not fnd):
385 print_error(['Unable to find appropriate size macro for %s' % newmacro,
386 COMPILER_FAIL])
387
388 fnd = False;
389 newmacro = tag
390
391 for name, value in probe['signed_tags'].items():
392 if (name == tag):
393 oldmacro = "INT%s" % (value * 8)
394 macros.append({'oldmacro': "%s_MIN" % oldmacro, 'newmacro': "%s_MIN" % newmacro})
395 macros.append({'oldmacro': "%s_MAX" % oldmacro, 'newmacro': "%s_MAX" % newmacro})
396 fnd = True
397 break
398
399 if (not fnd):
400 print_error(['Unable to find appropriate size macro for %s' % newmacro,
401 COMPILER_FAIL])
402
403 return {'macros': macros, 'typedefs': typedefs}
404
405def create_makefile(mkname, common):
406 "Create makefile output"
407
408 outmk = open(mkname, 'w')
409
410 outmk.write('#########################################\n')
411 outmk.write('## AUTO-GENERATED FILE, DO NOT EDIT!!! ##\n')
412 outmk.write('#########################################\n\n')
413
414 for key, value in common.items():
415 outmk.write('%s = %s\n' % (key, value))
416
417 outmk.close()
418
419def create_header(hdname, maps):
420 "Create header output"
421
422 outhd = open(hdname, 'w')
423
424 outhd.write('/***************************************\n')
425 outhd.write(' * AUTO-GENERATED FILE, DO NOT EDIT!!! *\n')
426 outhd.write(' ***************************************/\n\n')
427
428 outhd.write('#ifndef %s\n' % GUARD)
429 outhd.write('#define %s\n\n' % GUARD)
430
431 for macro in maps['macros']:
432 outhd.write('#define %s %s\n' % (macro['newmacro'], macro['oldmacro']))
433
434 outhd.write('\n')
435
436 for typedef in maps['typedefs']:
437 outhd.write('typedef %s %s;\n' % (typedef['oldtype'], typedef['newtype']))
438
439 outhd.write('\n#endif\n')
440 outhd.close()
441
442def main():
443 config = {}
444 common = {}
445
446 # Read and check configuration
447 if os.path.exists(CONFIG):
448 read_config(CONFIG, config)
449 else:
450 print_error(["Configuration file %s not found! Make sure that the" % CONFIG,
451 "configuration phase of HelenOS build went OK. Try running",
452 "\"make config\" again."])
453
454 check_config(config, "PLATFORM")
455 check_config(config, "COMPILER")
456 check_config(config, "BARCH")
457
458 # Cross-compiler prefix
459 if ('CROSS_PREFIX' in os.environ):
460 cross_prefix = os.environ['CROSS_PREFIX']
461 else:
462 cross_prefix = "/usr/local"
463
464 # Prefix binutils tools on Solaris
465 if (os.uname()[0] == "SunOS"):
466 binutils_prefix = "g"
467 else:
468 binutils_prefix = ""
469
470 owd = sandbox_enter()
471
472 try:
473 # Common utilities
474 check_app(["ln", "--version"], "Symlink utility", "usually part of coreutils")
475 check_app(["rm", "--version"], "File remove utility", "usually part of coreutils")
476 check_app(["mkdir", "--version"], "Directory creation utility", "usually part of coreutils")
477 check_app(["cp", "--version"], "Copy utility", "usually part of coreutils")
478 check_app(["find", "--version"], "Find utility", "usually part of findutils")
479 check_app(["diff", "--version"], "Diff utility", "usually part of diffutils")
480 check_app(["make", "--version"], "Make utility", "preferably GNU Make")
481 check_app(["makedepend", "-f", "-"], "Makedepend utility", "usually part of imake or xutils")
482
483 # Compiler
484 if (config['COMPILER'] == "gcc_cross"):
485 if (config['PLATFORM'] == "abs32le"):
486 check_config(config, "CROSS_TARGET")
487 target = config['CROSS_TARGET']
488
489 if (config['CROSS_TARGET'] == "arm32"):
490 gnu_target = "arm-linux-gnu"
491
492 if (config['CROSS_TARGET'] == "ia32"):
493 gnu_target = "i686-pc-linux-gnu"
494
495 if (config['CROSS_TARGET'] == "mips32"):
496 gnu_target = "mipsel-linux-gnu"
497
498 if (config['PLATFORM'] == "amd64"):
499 target = config['PLATFORM']
500 gnu_target = "amd64-linux-gnu"
501
502 if (config['PLATFORM'] == "arm32"):
503 target = config['PLATFORM']
504 gnu_target = "arm-linux-gnu"
505
506 if (config['PLATFORM'] == "ia32"):
507 target = config['PLATFORM']
508 gnu_target = "i686-pc-linux-gnu"
509
510 if (config['PLATFORM'] == "ia64"):
511 target = config['PLATFORM']
512 gnu_target = "ia64-pc-linux-gnu"
513
514 if (config['PLATFORM'] == "mips32"):
515 check_config(config, "MACHINE")
516
517 if ((config['MACHINE'] == "lgxemul") or (config['MACHINE'] == "msim")):
518 target = config['PLATFORM']
519 gnu_target = "mipsel-linux-gnu"
520
521 if (config['MACHINE'] == "bgxemul"):
522 target = "mips32eb"
523 gnu_target = "mips-linux-gnu"
524
525 if (config['PLATFORM'] == "ppc32"):
526 target = config['PLATFORM']
527 gnu_target = "ppc-linux-gnu"
528
529 if (config['PLATFORM'] == "sparc64"):
530 target = config['PLATFORM']
531 gnu_target = "sparc64-linux-gnu"
532
533 path = "%s/%s/bin" % (cross_prefix, target)
534 prefix = "%s-" % gnu_target
535
536 check_gcc(path, prefix, common, PACKAGE_CROSS)
537 check_binutils(path, prefix, common, PACKAGE_CROSS)
538
539 check_common(common, "GCC")
540 common['CC'] = common['GCC']
541
542 if (config['COMPILER'] == "gcc_native"):
543 check_gcc(None, "", common, PACKAGE_GCC)
544 check_binutils(None, binutils_prefix, common, PACKAGE_BINUTILS)
545
546 check_common(common, "GCC")
547 common['CC'] = common['GCC']
548
549 if (config['COMPILER'] == "icc"):
550 common['CC'] = "icc"
551 check_app([common['CC'], "-V"], "Intel C++ Compiler", "support is experimental")
552 check_gcc(None, "", common, PACKAGE_GCC)
553 check_binutils(None, binutils_prefix, common, PACKAGE_BINUTILS)
554
555 if (config['COMPILER'] == "suncc"):
556 common['CC'] = "suncc"
557 check_app([common['CC'], "-V"], "Sun Studio Compiler", "support is experimental")
558 check_gcc(None, "", common, PACKAGE_GCC)
559 check_binutils(None, binutils_prefix, common, PACKAGE_BINUTILS)
560
561 if (config['COMPILER'] == "clang"):
562 common['CC'] = "clang"
563 check_app([common['CC'], "--version"], "Clang compiler", "preferably version 1.0 or newer")
564 check_gcc(None, "", common, PACKAGE_GCC)
565 check_binutils(None, binutils_prefix, common, PACKAGE_BINUTILS)
566
567 # Platform-specific utilities
568 if ((config['BARCH'] == "amd64") or (config['BARCH'] == "ia32") or (config['BARCH'] == "ppc32") or (config['BARCH'] == "sparc64")):
569 check_app(["mkisofs", "--version"], "ISO 9660 creation utility", "usually part of genisoimage")
570
571 probe = probe_compiler(common,
572 [
573 {'type': 'char', 'tag': 'CHAR', 'strc': '"hh"', 'conc': '"@@"'},
574 {'type': 'short int', 'tag': 'SHORT', 'strc': '"h"', 'conc': '"@"'},
575 {'type': 'int', 'tag': 'INT', 'strc': '""', 'conc': '""'},
576 {'type': 'long int', 'tag': 'LONG', 'strc': '"l"', 'conc': '"L"'},
577 {'type': 'long long int', 'tag': 'LLONG', 'strc': '"ll"', 'conc': '"LL"'}
578 ]
579 )
580
581 maps = detect_uints(probe, [1, 2, 4, 8])
582
583 finally:
584 sandbox_leave(owd)
585
586 create_makefile(MAKEFILE, common)
587 create_header(HEADER, maps)
588
589 return 0
590
591if __name__ == '__main__':
592 sys.exit(main())
Note: See TracBrowser for help on using the repository browser.