| 1 | #!/usr/bin/env python2
 | 
|---|
| 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 | """
 | 
|---|
| 31 | Detect important prerequisites and parameters for building HelenOS
 | 
|---|
| 32 | """
 | 
|---|
| 33 | 
 | 
|---|
| 34 | import sys
 | 
|---|
| 35 | import os
 | 
|---|
| 36 | import shutil
 | 
|---|
| 37 | import re
 | 
|---|
| 38 | import time
 | 
|---|
| 39 | import subprocess
 | 
|---|
| 40 | 
 | 
|---|
| 41 | SANDBOX = 'autotool'
 | 
|---|
| 42 | CONFIG = 'Makefile.config'
 | 
|---|
| 43 | MAKEFILE = 'Makefile.common'
 | 
|---|
| 44 | HEADER = 'common.h.new'
 | 
|---|
| 45 | GUARD = '_AUTOTOOL_COMMON_H_'
 | 
|---|
| 46 | 
 | 
|---|
| 47 | PROBE_SOURCE = 'probe.c'
 | 
|---|
| 48 | PROBE_OUTPUT = 'probe.s'
 | 
|---|
| 49 | 
 | 
|---|
| 50 | PACKAGE_BINUTILS = "usually part of binutils"
 | 
|---|
| 51 | PACKAGE_GCC = "preferably version 4.7.0 or newer"
 | 
|---|
| 52 | PACKAGE_CROSS = "use tools/toolchain.sh to build the cross-compiler toolchain"
 | 
|---|
| 53 | PACKAGE_CLANG = "reasonably recent version of clang needs to be installed"
 | 
|---|
| 54 | 
 | 
|---|
| 55 | TOOLCHAIN_FAIL = [
 | 
|---|
| 56 |         "Compiler toolchain for target is not installed, or CROSS_PREFIX",
 | 
|---|
| 57 |         "environment variable is not set correctly. Use tools/toolchain.sh",
 | 
|---|
| 58 |         "to (re)build the cross-compiler toolchain."]
 | 
|---|
| 59 | COMPILER_FAIL = "The compiler is probably not capable to compile HelenOS."
 | 
|---|
| 60 | COMPILER_WARNING = "The compilation of HelenOS might fail."
 | 
|---|
| 61 | 
 | 
|---|
| 62 | PROBE_HEAD = """#define AUTOTOOL_DECLARE(category, tag, name, signedness, base, size, compatible) \\
 | 
|---|
| 63 |         asm volatile ( \\
 | 
|---|
| 64 |                 "AUTOTOOL_DECLARE\\t" category "\\t" tag "\\t" name "\\t" signedness "\\t" base "\\t%[size_val]\\t%[cmp_val]\\n" \\
 | 
|---|
| 65 |                 : \\
 | 
|---|
| 66 |                 : [size_val] "n" (size), [cmp_val] "n" (compatible) \\
 | 
|---|
| 67 |         )
 | 
|---|
| 68 | 
 | 
|---|
| 69 | #define STRING(arg)      STRING_ARG(arg)
 | 
|---|
| 70 | #define STRING_ARG(arg)  #arg
 | 
|---|
| 71 | 
 | 
|---|
| 72 | #define DECLARE_BUILTIN_TYPE(tag, type) \\
 | 
|---|
| 73 |         AUTOTOOL_DECLARE("unsigned long long int", tag, STRING(type), "unsigned", "long long", sizeof(type), __builtin_types_compatible_p(type, unsigned long long int)); \\
 | 
|---|
| 74 |         AUTOTOOL_DECLARE("unsigned long int", tag, STRING(type), "unsigned", "long", sizeof(type), __builtin_types_compatible_p(type, unsigned long int)); \\
 | 
|---|
| 75 |         AUTOTOOL_DECLARE("unsigned int", tag, STRING(type), "unsigned", "int", sizeof(type), __builtin_types_compatible_p(type, unsigned int)); \\
 | 
|---|
| 76 |         AUTOTOOL_DECLARE("unsigned short int", tag, STRING(type), "unsigned", "short", sizeof(type), __builtin_types_compatible_p(type, unsigned short int)); \\
 | 
|---|
| 77 |         AUTOTOOL_DECLARE("unsigned char", tag, STRING(type), "unsigned", "char", sizeof(type), __builtin_types_compatible_p(type, unsigned char)); \\
 | 
|---|
| 78 |         AUTOTOOL_DECLARE("signed long long int", tag, STRING(type), "signed", "long long", sizeof(type), __builtin_types_compatible_p(type, signed long long int)); \\
 | 
|---|
| 79 |         AUTOTOOL_DECLARE("signed long int", tag, STRING(type), "signed", "long", sizeof(type), __builtin_types_compatible_p(type, signed long int)); \\
 | 
|---|
| 80 |         AUTOTOOL_DECLARE("signed int", tag, STRING(type), "signed", "int", sizeof(type), __builtin_types_compatible_p(type, signed int)); \\
 | 
|---|
| 81 |         AUTOTOOL_DECLARE("signed short int", tag, STRING(type), "signed", "short", sizeof(type), __builtin_types_compatible_p(type, signed short int)); \\
 | 
|---|
| 82 |         AUTOTOOL_DECLARE("signed char", tag, STRING(type), "signed", "char", sizeof(type), __builtin_types_compatible_p(type, signed char)); \\
 | 
|---|
| 83 |         AUTOTOOL_DECLARE("pointer", tag, STRING(type), "N/A", "pointer", sizeof(type), __builtin_types_compatible_p(type, void*)); \\
 | 
|---|
| 84 |         AUTOTOOL_DECLARE("long double", tag, STRING(type), "signed", "long double", sizeof(type), __builtin_types_compatible_p(type, long double)); \\
 | 
|---|
| 85 |         AUTOTOOL_DECLARE("double", tag, STRING(type), "signed", "double", sizeof(type), __builtin_types_compatible_p(type, double)); \\
 | 
|---|
| 86 |         AUTOTOOL_DECLARE("float", tag, STRING(type), "signed", "float", sizeof(type), __builtin_types_compatible_p(type, float));
 | 
|---|
| 87 | 
 | 
|---|
| 88 | extern int main(int, char *[]);
 | 
|---|
| 89 | 
 | 
|---|
| 90 | int main(int argc, char *argv[])
 | 
|---|
| 91 | {
 | 
|---|
| 92 | """
 | 
|---|
| 93 | 
 | 
|---|
| 94 | PROBE_TAIL = """}
 | 
|---|
| 95 | """
 | 
|---|
| 96 | 
 | 
|---|
| 97 | def read_config(fname, config):
 | 
|---|
| 98 |         "Read HelenOS build configuration"
 | 
|---|
| 99 | 
 | 
|---|
| 100 |         inf = open(fname, 'r')
 | 
|---|
| 101 | 
 | 
|---|
| 102 |         for line in inf:
 | 
|---|
| 103 |                 res = re.match(r'^(?:#!# )?([^#]\w*)\s*=\s*(.*?)\s*$', line)
 | 
|---|
| 104 |                 if (res):
 | 
|---|
| 105 |                         config[res.group(1)] = res.group(2)
 | 
|---|
| 106 | 
 | 
|---|
| 107 |         inf.close()
 | 
|---|
| 108 | 
 | 
|---|
| 109 | def print_error(msg):
 | 
|---|
| 110 |         "Print a bold error message"
 | 
|---|
| 111 | 
 | 
|---|
| 112 |         sys.stderr.write("\n")
 | 
|---|
| 113 |         sys.stderr.write("######################################################################\n")
 | 
|---|
| 114 |         sys.stderr.write("HelenOS build sanity check error:\n")
 | 
|---|
| 115 |         sys.stderr.write("\n")
 | 
|---|
| 116 |         sys.stderr.write("%s\n" % "\n".join(msg))
 | 
|---|
| 117 |         sys.stderr.write("######################################################################\n")
 | 
|---|
| 118 |         sys.stderr.write("\n")
 | 
|---|
| 119 | 
 | 
|---|
| 120 |         sys.exit(1)
 | 
|---|
| 121 | 
 | 
|---|
| 122 | def print_warning(msg):
 | 
|---|
| 123 |         "Print a bold error message"
 | 
|---|
| 124 | 
 | 
|---|
| 125 |         sys.stderr.write("\n")
 | 
|---|
| 126 |         sys.stderr.write("######################################################################\n")
 | 
|---|
| 127 |         sys.stderr.write("HelenOS build sanity check warning:\n")
 | 
|---|
| 128 |         sys.stderr.write("\n")
 | 
|---|
| 129 |         sys.stderr.write("%s\n" % "\n".join(msg))
 | 
|---|
| 130 |         sys.stderr.write("######################################################################\n")
 | 
|---|
| 131 |         sys.stderr.write("\n")
 | 
|---|
| 132 | 
 | 
|---|
| 133 |         time.sleep(5)
 | 
|---|
| 134 | 
 | 
|---|
| 135 | def sandbox_enter():
 | 
|---|
| 136 |         "Create a temporal sandbox directory for running tests"
 | 
|---|
| 137 | 
 | 
|---|
| 138 |         if (os.path.exists(SANDBOX)):
 | 
|---|
| 139 |                 if (os.path.isdir(SANDBOX)):
 | 
|---|
| 140 |                         try:
 | 
|---|
| 141 |                                 shutil.rmtree(SANDBOX)
 | 
|---|
| 142 |                         except:
 | 
|---|
| 143 |                                 print_error(["Unable to cleanup the directory \"%s\"." % SANDBOX])
 | 
|---|
| 144 |                 else:
 | 
|---|
| 145 |                         print_error(["Please inspect and remove unexpected directory,",
 | 
|---|
| 146 |                                      "entry \"%s\"." % SANDBOX])
 | 
|---|
| 147 | 
 | 
|---|
| 148 |         try:
 | 
|---|
| 149 |                 os.mkdir(SANDBOX)
 | 
|---|
| 150 |         except:
 | 
|---|
| 151 |                 print_error(["Unable to create sandbox directory \"%s\"." % SANDBOX])
 | 
|---|
| 152 | 
 | 
|---|
| 153 |         owd = os.getcwd()
 | 
|---|
| 154 |         os.chdir(SANDBOX)
 | 
|---|
| 155 | 
 | 
|---|
| 156 |         return owd
 | 
|---|
| 157 | 
 | 
|---|
| 158 | def sandbox_leave(owd):
 | 
|---|
| 159 |         "Leave the temporal sandbox directory"
 | 
|---|
| 160 | 
 | 
|---|
| 161 |         os.chdir(owd)
 | 
|---|
| 162 | 
 | 
|---|
| 163 | def check_config(config, key):
 | 
|---|
| 164 |         "Check whether the configuration key exists"
 | 
|---|
| 165 | 
 | 
|---|
| 166 |         if (not key in config):
 | 
|---|
| 167 |                 print_error(["Build configuration of HelenOS does not contain %s." % key,
 | 
|---|
| 168 |                              "Try running \"make config\" again.",
 | 
|---|
| 169 |                              "If the problem persists, please contact the developers of HelenOS."])
 | 
|---|
| 170 | 
 | 
|---|
| 171 | def check_common(common, key):
 | 
|---|
| 172 |         "Check whether the common key exists"
 | 
|---|
| 173 | 
 | 
|---|
| 174 |         if (not key in common):
 | 
|---|
| 175 |                 print_error(["Failed to determine the value %s." % key,
 | 
|---|
| 176 |                              "Please contact the developers of HelenOS."])
 | 
|---|
| 177 | 
 | 
|---|
| 178 | def get_target(config):
 | 
|---|
| 179 |         platform = None
 | 
|---|
| 180 |         target = None
 | 
|---|
| 181 | 
 | 
|---|
| 182 |         if (config['PLATFORM'] == "abs32le"):
 | 
|---|
| 183 |                 check_config(config, "CROSS_TARGET")
 | 
|---|
| 184 |                 platform = config['CROSS_TARGET']
 | 
|---|
| 185 | 
 | 
|---|
| 186 |                 if (config['CROSS_TARGET'] == "arm32"):
 | 
|---|
| 187 |                         target = "arm-helenos"
 | 
|---|
| 188 | 
 | 
|---|
| 189 |                 if (config['CROSS_TARGET'] == "ia32"):
 | 
|---|
| 190 |                         target = "i686-helenos"
 | 
|---|
| 191 | 
 | 
|---|
| 192 |                 if (config['CROSS_TARGET'] == "mips32"):
 | 
|---|
| 193 |                         target = "mipsel-helenos"
 | 
|---|
| 194 | 
 | 
|---|
| 195 |         if (config['PLATFORM'] == "amd64"):
 | 
|---|
| 196 |                 platform = config['PLATFORM']
 | 
|---|
| 197 |                 target = "amd64-helenos"
 | 
|---|
| 198 | 
 | 
|---|
| 199 |         if (config['PLATFORM'] == "arm32"):
 | 
|---|
| 200 |                 platform = config['PLATFORM']
 | 
|---|
| 201 |                 target = "arm-helenos"
 | 
|---|
| 202 | 
 | 
|---|
| 203 |         if (config['PLATFORM'] == "ia32"):
 | 
|---|
| 204 |                 platform = config['PLATFORM']
 | 
|---|
| 205 |                 target = "i686-helenos"
 | 
|---|
| 206 | 
 | 
|---|
| 207 |         if (config['PLATFORM'] == "ia64"):
 | 
|---|
| 208 |                 platform = config['PLATFORM']
 | 
|---|
| 209 |                 target = "ia64-helenos"
 | 
|---|
| 210 | 
 | 
|---|
| 211 |         if (config['PLATFORM'] == "mips32"):
 | 
|---|
| 212 |                 check_config(config, "MACHINE")
 | 
|---|
| 213 | 
 | 
|---|
| 214 |                 if ((config['MACHINE'] == "msim") or (config['MACHINE'] == "lmalta")):
 | 
|---|
| 215 |                         platform = config['PLATFORM']
 | 
|---|
| 216 |                         target = "mipsel-helenos"
 | 
|---|
| 217 | 
 | 
|---|
| 218 |                 if ((config['MACHINE'] == "bmalta")):
 | 
|---|
| 219 |                         platform = "mips32eb"
 | 
|---|
| 220 |                         target = "mips-helenos"
 | 
|---|
| 221 | 
 | 
|---|
| 222 |         if (config['PLATFORM'] == "mips64"):
 | 
|---|
| 223 |                 check_config(config, "MACHINE")
 | 
|---|
| 224 | 
 | 
|---|
| 225 |                 if (config['MACHINE'] == "msim"):
 | 
|---|
| 226 |                         platform = config['PLATFORM']
 | 
|---|
| 227 |                         target = "mips64el-helenos"
 | 
|---|
| 228 | 
 | 
|---|
| 229 |         if (config['PLATFORM'] == "ppc32"):
 | 
|---|
| 230 |                 platform = config['PLATFORM']
 | 
|---|
| 231 |                 target = "ppc-helenos"
 | 
|---|
| 232 | 
 | 
|---|
| 233 |         if (config['PLATFORM'] == "riscv64"):
 | 
|---|
| 234 |                 platform = config['PLATFORM']
 | 
|---|
| 235 |                 target = "riscv64-helenos"
 | 
|---|
| 236 | 
 | 
|---|
| 237 |         if (config['PLATFORM'] == "sparc64"):
 | 
|---|
| 238 |                 platform = config['PLATFORM']
 | 
|---|
| 239 |                 target = "sparc64-helenos"
 | 
|---|
| 240 | 
 | 
|---|
| 241 |         return (platform, target)
 | 
|---|
| 242 | 
 | 
|---|
| 243 | def check_app(args, name, details):
 | 
|---|
| 244 |         "Check whether an application can be executed"
 | 
|---|
| 245 | 
 | 
|---|
| 246 |         try:
 | 
|---|
| 247 |                 sys.stderr.write("Checking for %s ... " % args[0])
 | 
|---|
| 248 |                 subprocess.Popen(args, stdout = subprocess.PIPE, stderr = subprocess.PIPE).wait()
 | 
|---|
| 249 |         except:
 | 
|---|
| 250 |                 sys.stderr.write("failed\n")
 | 
|---|
| 251 |                 print_error(["%s is missing." % name,
 | 
|---|
| 252 |                              "",
 | 
|---|
| 253 |                              "Execution of \"%s\" has failed. Please make sure that it" % " ".join(args),
 | 
|---|
| 254 |                              "is installed in your system (%s)." % details])
 | 
|---|
| 255 | 
 | 
|---|
| 256 |         sys.stderr.write("ok\n")
 | 
|---|
| 257 | 
 | 
|---|
| 258 | def check_path_gcc(target):
 | 
|---|
| 259 |         "Check whether GCC for a given target is present in $PATH."
 | 
|---|
| 260 | 
 | 
|---|
| 261 |         try:
 | 
|---|
| 262 |                 subprocess.Popen([ "%s-gcc" % target, "--version" ], stdout = subprocess.PIPE, stderr = subprocess.PIPE).wait()
 | 
|---|
| 263 |                 return True
 | 
|---|
| 264 |         except:
 | 
|---|
| 265 |                 return False
 | 
|---|
| 266 | 
 | 
|---|
| 267 | def check_app_alternatives(alts, args, name, details):
 | 
|---|
| 268 |         "Check whether an application can be executed (use several alternatives)"
 | 
|---|
| 269 | 
 | 
|---|
| 270 |         tried = []
 | 
|---|
| 271 |         found = None
 | 
|---|
| 272 | 
 | 
|---|
| 273 |         for alt in alts:
 | 
|---|
| 274 |                 working = True
 | 
|---|
| 275 |                 cmdline = [alt] + args
 | 
|---|
| 276 |                 tried.append(" ".join(cmdline))
 | 
|---|
| 277 | 
 | 
|---|
| 278 |                 try:
 | 
|---|
| 279 |                         sys.stderr.write("Checking for %s ... " % alt)
 | 
|---|
| 280 |                         subprocess.Popen(cmdline, stdout = subprocess.PIPE, stderr = subprocess.PIPE).wait()
 | 
|---|
| 281 |                 except:
 | 
|---|
| 282 |                         sys.stderr.write("failed\n")
 | 
|---|
| 283 |                         working = False
 | 
|---|
| 284 | 
 | 
|---|
| 285 |                 if (working):
 | 
|---|
| 286 |                         sys.stderr.write("ok\n")
 | 
|---|
| 287 |                         found = alt
 | 
|---|
| 288 |                         break
 | 
|---|
| 289 | 
 | 
|---|
| 290 |         if (found is None):
 | 
|---|
| 291 |                 print_error(["%s is missing." % name,
 | 
|---|
| 292 |                              "",
 | 
|---|
| 293 |                              "Please make sure that it is installed in your",
 | 
|---|
| 294 |                              "system (%s)." % details,
 | 
|---|
| 295 |                              "",
 | 
|---|
| 296 |                              "The following alternatives were tried:"] + tried)
 | 
|---|
| 297 | 
 | 
|---|
| 298 |         return found
 | 
|---|
| 299 | 
 | 
|---|
| 300 | def check_clang(path, prefix, common, details):
 | 
|---|
| 301 |         "Check for clang"
 | 
|---|
| 302 | 
 | 
|---|
| 303 |         common['CLANG'] = "%sclang" % prefix
 | 
|---|
| 304 | 
 | 
|---|
| 305 |         if (not path is None):
 | 
|---|
| 306 |                 common['CLANG'] = "%s/%s" % (path, common['CLANG'])
 | 
|---|
| 307 | 
 | 
|---|
| 308 |         check_app([common['CLANG'], "--version"], "clang", details)
 | 
|---|
| 309 | 
 | 
|---|
| 310 | def check_gcc(path, prefix, common, details):
 | 
|---|
| 311 |         "Check for GCC"
 | 
|---|
| 312 | 
 | 
|---|
| 313 |         common['GCC'] = "%sgcc" % prefix
 | 
|---|
| 314 |         common['GXX'] = "%sg++" % prefix
 | 
|---|
| 315 | 
 | 
|---|
| 316 |         if (not path is None):
 | 
|---|
| 317 |                 common['GCC'] = "%s/%s" % (path, common['GCC'])
 | 
|---|
| 318 |                 common['GXX'] = "%s/%s" % (path, common['GXX'])
 | 
|---|
| 319 | 
 | 
|---|
| 320 |         check_app([common['GCC'], "--version"], "GNU GCC", details)
 | 
|---|
| 321 | 
 | 
|---|
| 322 | def check_libgcc(common):
 | 
|---|
| 323 |         sys.stderr.write("Checking for libgcc.a ... ")
 | 
|---|
| 324 |         libgcc_path = None
 | 
|---|
| 325 |         proc = subprocess.Popen([ common['GCC'], "-print-search-dirs" ], stdout=subprocess.PIPE, stderr=subprocess.STDOUT)
 | 
|---|
| 326 |         for line in proc.stdout:
 | 
|---|
| 327 |                 line = line.decode('utf-8').strip('\n')
 | 
|---|
| 328 |                 parts = line.split()
 | 
|---|
| 329 |                 if parts[0] == "install:":
 | 
|---|
| 330 |                         p = parts[1] + "libgcc.a"
 | 
|---|
| 331 |                         if os.path.isfile(p):
 | 
|---|
| 332 |                                 libgcc_path = p
 | 
|---|
| 333 |         proc.wait()
 | 
|---|
| 334 | 
 | 
|---|
| 335 |         if libgcc_path is None:
 | 
|---|
| 336 |                 sys.stderr.write("failed\n")
 | 
|---|
| 337 |                 print_error(["Unable to find gcc library (libgcc.a).",
 | 
|---|
| 338 |                                         "",
 | 
|---|
| 339 |                                         "Please ensure that you have installed the",
 | 
|---|
| 340 |                                         "toolchain properly."])
 | 
|---|
| 341 | 
 | 
|---|
| 342 |         sys.stderr.write("ok\n")
 | 
|---|
| 343 |         common['LIBGCC_PATH'] = libgcc_path
 | 
|---|
| 344 | 
 | 
|---|
| 345 | 
 | 
|---|
| 346 | def check_binutils(path, prefix, common, details):
 | 
|---|
| 347 |         "Check for binutils toolchain"
 | 
|---|
| 348 | 
 | 
|---|
| 349 |         common['AS'] = "%sas" % prefix
 | 
|---|
| 350 |         common['LD'] = "%sld" % prefix
 | 
|---|
| 351 |         common['AR'] = "%sar" % prefix
 | 
|---|
| 352 |         common['OBJCOPY'] = "%sobjcopy" % prefix
 | 
|---|
| 353 |         common['OBJDUMP'] = "%sobjdump" % prefix
 | 
|---|
| 354 |         common['STRIP'] = "%sstrip" % prefix
 | 
|---|
| 355 | 
 | 
|---|
| 356 |         if (not path is None):
 | 
|---|
| 357 |                 for key in ["AS", "LD", "AR", "OBJCOPY", "OBJDUMP", "STRIP"]:
 | 
|---|
| 358 |                         common[key] = "%s/%s" % (path, common[key])
 | 
|---|
| 359 | 
 | 
|---|
| 360 |         check_app([common['AS'], "--version"], "GNU Assembler", details)
 | 
|---|
| 361 |         check_app([common['LD'], "--version"], "GNU Linker", details)
 | 
|---|
| 362 |         check_app([common['AR'], "--version"], "GNU Archiver", details)
 | 
|---|
| 363 |         check_app([common['OBJCOPY'], "--version"], "GNU Objcopy utility", details)
 | 
|---|
| 364 |         check_app([common['OBJDUMP'], "--version"], "GNU Objdump utility", details)
 | 
|---|
| 365 |         check_app([common['STRIP'], "--version"], "GNU strip", details)
 | 
|---|
| 366 | 
 | 
|---|
| 367 | def decode_value(value):
 | 
|---|
| 368 |         "Decode integer value"
 | 
|---|
| 369 | 
 | 
|---|
| 370 |         base = 10
 | 
|---|
| 371 | 
 | 
|---|
| 372 |         if ((value.startswith('$')) or (value.startswith('#'))):
 | 
|---|
| 373 |                 value = value[1:]
 | 
|---|
| 374 | 
 | 
|---|
| 375 |         if (value.startswith('0x')):
 | 
|---|
| 376 |                 value = value[2:]
 | 
|---|
| 377 |                 base = 16
 | 
|---|
| 378 | 
 | 
|---|
| 379 |         return int(value, base)
 | 
|---|
| 380 | 
 | 
|---|
| 381 | def probe_compiler(cc, common, typesizes):
 | 
|---|
| 382 |         "Generate, compile and parse probing source"
 | 
|---|
| 383 | 
 | 
|---|
| 384 |         check_common(common, "CC")
 | 
|---|
| 385 | 
 | 
|---|
| 386 |         outf = open(PROBE_SOURCE, 'w')
 | 
|---|
| 387 |         outf.write(PROBE_HEAD)
 | 
|---|
| 388 | 
 | 
|---|
| 389 |         for typedef in typesizes:
 | 
|---|
| 390 |                 if 'def' in typedef:
 | 
|---|
| 391 |                         outf.write("#ifdef %s\n" % typedef['def'])
 | 
|---|
| 392 |                 outf.write("\tDECLARE_BUILTIN_TYPE(\"%s\", %s);\n" % (typedef['tag'], typedef['type']))
 | 
|---|
| 393 |                 if 'def' in typedef:
 | 
|---|
| 394 |                         outf.write("#endif\n")
 | 
|---|
| 395 | 
 | 
|---|
| 396 |         outf.write(PROBE_TAIL)
 | 
|---|
| 397 |         outf.close()
 | 
|---|
| 398 | 
 | 
|---|
| 399 |         args = cc.split(' ')
 | 
|---|
| 400 |         args.extend(["-S", "-o", PROBE_OUTPUT, PROBE_SOURCE])
 | 
|---|
| 401 | 
 | 
|---|
| 402 |         try:
 | 
|---|
| 403 |                 sys.stderr.write("Checking compiler properties ... ")
 | 
|---|
| 404 |                 output = subprocess.Popen(args, stdout = subprocess.PIPE, stderr = subprocess.PIPE).communicate()
 | 
|---|
| 405 |         except:
 | 
|---|
| 406 |                 sys.stderr.write("failed\n")
 | 
|---|
| 407 |                 print_error(["Error executing \"%s\"." % " ".join(args),
 | 
|---|
| 408 |                              "Make sure that the compiler works properly."])
 | 
|---|
| 409 | 
 | 
|---|
| 410 |         if (not os.path.isfile(PROBE_OUTPUT)):
 | 
|---|
| 411 |                 sys.stderr.write("failed\n")
 | 
|---|
| 412 |                 print(output[1])
 | 
|---|
| 413 |                 print_error(["Error executing \"%s\"." % " ".join(args),
 | 
|---|
| 414 |                              "The compiler did not produce the output file \"%s\"." % PROBE_OUTPUT,
 | 
|---|
| 415 |                              "",
 | 
|---|
| 416 |                              output[0],
 | 
|---|
| 417 |                              output[1]])
 | 
|---|
| 418 | 
 | 
|---|
| 419 |         sys.stderr.write("ok\n")
 | 
|---|
| 420 | 
 | 
|---|
| 421 |         inf = open(PROBE_OUTPUT, 'r')
 | 
|---|
| 422 |         lines = inf.readlines()
 | 
|---|
| 423 |         inf.close()
 | 
|---|
| 424 | 
 | 
|---|
| 425 |         builtins = {}
 | 
|---|
| 426 | 
 | 
|---|
| 427 |         for j in range(len(lines)):
 | 
|---|
| 428 |                 tokens = lines[j].strip().split("\t")
 | 
|---|
| 429 | 
 | 
|---|
| 430 |                 if (len(tokens) > 0):
 | 
|---|
| 431 |                         if (tokens[0] == "AUTOTOOL_DECLARE"):
 | 
|---|
| 432 |                                 if (len(tokens) < 8):
 | 
|---|
| 433 |                                         print_error(["Malformed declaration in \"%s\" on line %s." % (PROBE_OUTPUT, j), COMPILER_FAIL])
 | 
|---|
| 434 | 
 | 
|---|
| 435 |                                 category = tokens[1]
 | 
|---|
| 436 |                                 tag = tokens[2]
 | 
|---|
| 437 |                                 name = tokens[3]
 | 
|---|
| 438 |                                 signedness = tokens[4]
 | 
|---|
| 439 |                                 base = tokens[5]
 | 
|---|
| 440 |                                 size = tokens[6]
 | 
|---|
| 441 |                                 compatible = tokens[7]
 | 
|---|
| 442 | 
 | 
|---|
| 443 |                                 try:
 | 
|---|
| 444 |                                         compatible_int = decode_value(compatible)
 | 
|---|
| 445 |                                         size_int = decode_value(size)
 | 
|---|
| 446 |                                 except:
 | 
|---|
| 447 |                                         print_error(["Integer value expected in \"%s\" on line %s." % (PROBE_OUTPUT, j), COMPILER_FAIL])
 | 
|---|
| 448 | 
 | 
|---|
| 449 |                                 if (compatible_int == 1):
 | 
|---|
| 450 |                                         builtins[tag] = {
 | 
|---|
| 451 |                                                 'tag': tag,
 | 
|---|
| 452 |                                                 'name': name,
 | 
|---|
| 453 |                                                 'sign': signedness,
 | 
|---|
| 454 |                                                 'base': base,
 | 
|---|
| 455 |                                                 'size': size_int,
 | 
|---|
| 456 |                                         }
 | 
|---|
| 457 | 
 | 
|---|
| 458 |         for typedef in typesizes:
 | 
|---|
| 459 |                 if not typedef['tag'] in builtins:
 | 
|---|
| 460 |                         print_error(['Unable to determine the properties of type %s.' % typedef['tag'],
 | 
|---|
| 461 |                              COMPILER_FAIL])
 | 
|---|
| 462 |                 if 'sname' in typedef:
 | 
|---|
| 463 |                         builtins[typedef['tag']]['sname'] = typedef['sname']
 | 
|---|
| 464 | 
 | 
|---|
| 465 |         return builtins
 | 
|---|
| 466 | 
 | 
|---|
| 467 | def get_suffix(type):
 | 
|---|
| 468 |         if type['sign'] == 'unsigned':
 | 
|---|
| 469 |                 return {
 | 
|---|
| 470 |                         "char": "",
 | 
|---|
| 471 |                         "short": "",
 | 
|---|
| 472 |                         "int": "U",
 | 
|---|
| 473 |                         "long": "UL",
 | 
|---|
| 474 |                         "long long": "ULL",
 | 
|---|
| 475 |                 }[type['base']]
 | 
|---|
| 476 |         else:
 | 
|---|
| 477 |                 return {
 | 
|---|
| 478 |                         "char": "",
 | 
|---|
| 479 |                         "short": "",
 | 
|---|
| 480 |                         "int": "",
 | 
|---|
| 481 |                         "long": "L",
 | 
|---|
| 482 |                         "long long": "LL",
 | 
|---|
| 483 |                 }[type['base']]
 | 
|---|
| 484 | 
 | 
|---|
| 485 | def get_max(type):
 | 
|---|
| 486 |         val = (1 << (type['size']*8 - 1))
 | 
|---|
| 487 |         if type['sign'] == 'unsigned':
 | 
|---|
| 488 |                 val *= 2
 | 
|---|
| 489 |         return val - 1
 | 
|---|
| 490 | 
 | 
|---|
| 491 | def detect_sizes(probe):
 | 
|---|
| 492 |         "Detect properties of builtin types"
 | 
|---|
| 493 | 
 | 
|---|
| 494 |         macros = {}
 | 
|---|
| 495 | 
 | 
|---|
| 496 |         for type in probe.values():
 | 
|---|
| 497 |                 macros['__SIZEOF_%s__' % type['tag']] = type['size']
 | 
|---|
| 498 | 
 | 
|---|
| 499 |                 if ('sname' in type):
 | 
|---|
| 500 |                         macros['__%s_TYPE__'  % type['sname']] = type['name']
 | 
|---|
| 501 |                         macros['__%s_WIDTH__' % type['sname']] = type['size']*8
 | 
|---|
| 502 |                         macros['__%s_%s__' % (type['sname'], type['sign'].upper())] = "1"
 | 
|---|
| 503 |                         macros['__%s_C_SUFFIX__' % type['sname']] = get_suffix(type)
 | 
|---|
| 504 |                         macros['__%s_MAX__' % type['sname']] = "%d%s" % (get_max(type), get_suffix(type))
 | 
|---|
| 505 | 
 | 
|---|
| 506 |         if (probe['SIZE_T']['sign'] != 'unsigned'):
 | 
|---|
| 507 |                 print_error(['The type size_t is not unsigned.', COMPILER_FAIL])
 | 
|---|
| 508 | 
 | 
|---|
| 509 |         return macros
 | 
|---|
| 510 | 
 | 
|---|
| 511 | def create_makefile(mkname, common):
 | 
|---|
| 512 |         "Create makefile output"
 | 
|---|
| 513 | 
 | 
|---|
| 514 |         outmk = open(mkname, 'w')
 | 
|---|
| 515 | 
 | 
|---|
| 516 |         outmk.write('#########################################\n')
 | 
|---|
| 517 |         outmk.write('## AUTO-GENERATED FILE, DO NOT EDIT!!! ##\n')
 | 
|---|
| 518 |         outmk.write('## Generated by: tools/autotool.py     ##\n')
 | 
|---|
| 519 |         outmk.write('#########################################\n\n')
 | 
|---|
| 520 | 
 | 
|---|
| 521 |         for key, value in common.items():
 | 
|---|
| 522 |                 if (type(value) is list):
 | 
|---|
| 523 |                         outmk.write('%s = %s\n' % (key, " ".join(value)))
 | 
|---|
| 524 |                 else:
 | 
|---|
| 525 |                         outmk.write('%s = %s\n' % (key, value))
 | 
|---|
| 526 | 
 | 
|---|
| 527 |         outmk.close()
 | 
|---|
| 528 | 
 | 
|---|
| 529 | def create_header(hdname, macros):
 | 
|---|
| 530 |         "Create header output"
 | 
|---|
| 531 | 
 | 
|---|
| 532 |         outhd = open(hdname, 'w')
 | 
|---|
| 533 | 
 | 
|---|
| 534 |         outhd.write('/***************************************\n')
 | 
|---|
| 535 |         outhd.write(' * AUTO-GENERATED FILE, DO NOT EDIT!!! *\n')
 | 
|---|
| 536 |         outhd.write(' * Generated by: tools/autotool.py     *\n')
 | 
|---|
| 537 |         outhd.write(' ***************************************/\n\n')
 | 
|---|
| 538 | 
 | 
|---|
| 539 |         outhd.write('#ifndef %s\n' % GUARD)
 | 
|---|
| 540 |         outhd.write('#define %s\n\n' % GUARD)
 | 
|---|
| 541 | 
 | 
|---|
| 542 |         for macro in sorted(macros):
 | 
|---|
| 543 |                 outhd.write('#ifndef %s\n' % macro)
 | 
|---|
| 544 |                 outhd.write('#define %s  %s\n' % (macro, macros[macro]))
 | 
|---|
| 545 |                 outhd.write('#endif\n\n')
 | 
|---|
| 546 | 
 | 
|---|
| 547 |         outhd.write('\n#endif\n')
 | 
|---|
| 548 |         outhd.close()
 | 
|---|
| 549 | 
 | 
|---|
| 550 | def main():
 | 
|---|
| 551 |         config = {}
 | 
|---|
| 552 |         common = {}
 | 
|---|
| 553 | 
 | 
|---|
| 554 |         # Read and check configuration
 | 
|---|
| 555 |         if os.path.exists(CONFIG):
 | 
|---|
| 556 |                 read_config(CONFIG, config)
 | 
|---|
| 557 |         else:
 | 
|---|
| 558 |                 print_error(["Configuration file %s not found! Make sure that the" % CONFIG,
 | 
|---|
| 559 |                              "configuration phase of HelenOS build went OK. Try running",
 | 
|---|
| 560 |                              "\"make config\" again."])
 | 
|---|
| 561 | 
 | 
|---|
| 562 |         check_config(config, "PLATFORM")
 | 
|---|
| 563 |         check_config(config, "COMPILER")
 | 
|---|
| 564 |         check_config(config, "BARCH")
 | 
|---|
| 565 | 
 | 
|---|
| 566 |         # Cross-compiler prefix
 | 
|---|
| 567 |         if ('CROSS_PREFIX' in os.environ):
 | 
|---|
| 568 |                 cross_prefix = os.environ['CROSS_PREFIX']
 | 
|---|
| 569 |         else:
 | 
|---|
| 570 |                 cross_prefix = "/usr/local/cross"
 | 
|---|
| 571 | 
 | 
|---|
| 572 |         owd = sandbox_enter()
 | 
|---|
| 573 | 
 | 
|---|
| 574 |         try:
 | 
|---|
| 575 |                 # Common utilities
 | 
|---|
| 576 |                 check_app(["ln", "--version"], "Symlink utility", "usually part of coreutils")
 | 
|---|
| 577 |                 check_app(["rm", "--version"], "File remove utility", "usually part of coreutils")
 | 
|---|
| 578 |                 check_app(["mkdir", "--version"], "Directory creation utility", "usually part of coreutils")
 | 
|---|
| 579 |                 check_app(["cp", "--version"], "Copy utility", "usually part of coreutils")
 | 
|---|
| 580 |                 check_app(["find", "--version"], "Find utility", "usually part of findutils")
 | 
|---|
| 581 |                 check_app(["diff", "--version"], "Diff utility", "usually part of diffutils")
 | 
|---|
| 582 |                 check_app(["make", "--version"], "Make utility", "preferably GNU Make")
 | 
|---|
| 583 |                 check_app(["unzip"], "unzip utility", "usually part of zip/unzip utilities")
 | 
|---|
| 584 |                 check_app(["tar", "--version"], "tar utility", "usually part of tar")
 | 
|---|
| 585 | 
 | 
|---|
| 586 |                 platform, target = get_target(config)
 | 
|---|
| 587 | 
 | 
|---|
| 588 |                 if (platform is None) or (target is None):
 | 
|---|
| 589 |                         print_error(["Unsupported compiler target.",
 | 
|---|
| 590 |                                      "Please contact the developers of HelenOS."])
 | 
|---|
| 591 | 
 | 
|---|
| 592 |                 path = None
 | 
|---|
| 593 | 
 | 
|---|
| 594 |                 if not check_path_gcc(target):
 | 
|---|
| 595 |                         path = "%s/bin" % cross_prefix
 | 
|---|
| 596 | 
 | 
|---|
| 597 |                 common['TARGET'] = target
 | 
|---|
| 598 |                 prefix = "%s-" % target
 | 
|---|
| 599 | 
 | 
|---|
| 600 |                 cc_autogen = None
 | 
|---|
| 601 | 
 | 
|---|
| 602 |                 # We always need to check for GCC as we
 | 
|---|
| 603 |                 # need libgcc
 | 
|---|
| 604 |                 check_gcc(path, prefix, common, PACKAGE_CROSS)
 | 
|---|
| 605 | 
 | 
|---|
| 606 |                 # Compiler
 | 
|---|
| 607 |                 if (config['COMPILER'] == "gcc_cross"):
 | 
|---|
| 608 |                         check_binutils(path, prefix, common, PACKAGE_CROSS)
 | 
|---|
| 609 | 
 | 
|---|
| 610 |                         check_common(common, "GCC")
 | 
|---|
| 611 |                         common['CC'] = common['GCC']
 | 
|---|
| 612 |                         cc_autogen = common['CC']
 | 
|---|
| 613 | 
 | 
|---|
| 614 |                         check_common(common, "GXX")
 | 
|---|
| 615 |                         common['CXX'] = common['GXX']
 | 
|---|
| 616 | 
 | 
|---|
| 617 |                 if (config['COMPILER'] == "clang"):
 | 
|---|
| 618 |                         check_binutils(path, prefix, common, PACKAGE_CROSS)
 | 
|---|
| 619 |                         check_clang(path, prefix, common, PACKAGE_CLANG)
 | 
|---|
| 620 | 
 | 
|---|
| 621 |                         check_common(common, "CLANG")
 | 
|---|
| 622 |                         common['CC'] = common['CLANG']
 | 
|---|
| 623 |                         cc_autogen = common['CC'] + " -no-integrated-as"
 | 
|---|
| 624 | 
 | 
|---|
| 625 |                         if (config['INTEGRATED_AS'] == "yes"):
 | 
|---|
| 626 |                                 common['CC'] += " -integrated-as"
 | 
|---|
| 627 | 
 | 
|---|
| 628 |                         if (config['INTEGRATED_AS'] == "no"):
 | 
|---|
| 629 |                                 common['CC'] += " -no-integrated-as"
 | 
|---|
| 630 | 
 | 
|---|
| 631 |                 # Find full path to libgcc
 | 
|---|
| 632 |                 check_libgcc(common)
 | 
|---|
| 633 | 
 | 
|---|
| 634 |                 # Platform-specific utilities
 | 
|---|
| 635 |                 if ((config['BARCH'] == "amd64") or (config['BARCH'] == "ia32") or (config['BARCH'] == "ppc32") or (config['BARCH'] == "sparc64")):
 | 
|---|
| 636 |                         common['GENISOIMAGE'] = check_app_alternatives(["genisoimage", "mkisofs", "xorriso"], ["--version"], "ISO 9660 creation utility", "usually part of genisoimage")
 | 
|---|
| 637 |                         if common['GENISOIMAGE'] == 'xorriso':
 | 
|---|
| 638 |                                 common['GENISOIMAGE'] += ' -as genisoimage'
 | 
|---|
| 639 | 
 | 
|---|
| 640 |                 probe = probe_compiler(cc_autogen, common,
 | 
|---|
| 641 |                         [
 | 
|---|
| 642 |                                 {'type': 'long long int', 'tag': 'LONG_LONG', 'sname': 'LLONG' },
 | 
|---|
| 643 |                                 {'type': 'long int', 'tag': 'LONG', 'sname': 'LONG' },
 | 
|---|
| 644 |                                 {'type': 'int', 'tag': 'INT', 'sname': 'INT' },
 | 
|---|
| 645 |                                 {'type': 'short int', 'tag': 'SHORT', 'sname': 'SHRT'},
 | 
|---|
| 646 |                                 {'type': 'void*', 'tag': 'POINTER'},
 | 
|---|
| 647 |                                 {'type': 'long double', 'tag': 'LONG_DOUBLE'},
 | 
|---|
| 648 |                                 {'type': 'double', 'tag': 'DOUBLE'},
 | 
|---|
| 649 |                                 {'type': 'float', 'tag': 'FLOAT'},
 | 
|---|
| 650 |                                 {'type': '__SIZE_TYPE__', 'tag': 'SIZE_T', 'def': '__SIZE_TYPE__', 'sname': 'SIZE' },
 | 
|---|
| 651 |                                 {'type': '__PTRDIFF_TYPE__', 'tag': 'PTRDIFF_T', 'def': '__PTRDIFF_TYPE__', 'sname': 'PTRDIFF' },
 | 
|---|
| 652 |                                 {'type': '__WINT_TYPE__', 'tag': 'WINT_T', 'def': '__WINT_TYPE__', 'sname': 'WINT' },
 | 
|---|
| 653 |                                 {'type': '__WCHAR_TYPE__', 'tag': 'WCHAR_T', 'def': '__WCHAR_TYPE__', 'sname': 'WCHAR' },
 | 
|---|
| 654 |                                 {'type': '__INTMAX_TYPE__', 'tag': 'INTMAX_T', 'def': '__INTMAX_TYPE__', 'sname': 'INTMAX' },
 | 
|---|
| 655 |                                 {'type': 'unsigned __INTMAX_TYPE__', 'tag': 'UINTMAX_T', 'def': '__INTMAX_TYPE__', 'sname': 'UINTMAX' },
 | 
|---|
| 656 |                         ]
 | 
|---|
| 657 |                 )
 | 
|---|
| 658 | 
 | 
|---|
| 659 |                 macros = detect_sizes(probe)
 | 
|---|
| 660 | 
 | 
|---|
| 661 |         finally:
 | 
|---|
| 662 |                 sandbox_leave(owd)
 | 
|---|
| 663 | 
 | 
|---|
| 664 |         create_makefile(MAKEFILE, common)
 | 
|---|
| 665 |         create_header(HEADER, macros)
 | 
|---|
| 666 | 
 | 
|---|
| 667 |         return 0
 | 
|---|
| 668 | 
 | 
|---|
| 669 | if __name__ == '__main__':
 | 
|---|
| 670 |         sys.exit(main())
 | 
|---|