source: mainline/tools/autotool.py@ dc0b964

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