fork/rust
git clone https://git.julia.blue/fork/rust
f3e1a954d2e
1.41.1
1#!/usr/bin/env python 2 3# ignore-tidy-linelength 4 5from __future__ import absolute_import, division, print_function 6import sys 7import os 8rust_dir = os.path.dirname(os.path.abspath(__file__)) 9rust_dir = os.path.dirname(rust_dir) 10rust_dir = os.path.dirname(rust_dir) 11sys.path.append(os.path.join(rust_dir, "src", "bootstrap")) 12import bootstrap 13 14 15class Option(object): 16 def __init__(self, name, rustbuild, desc, value): 17 self.name = name 18 self.rustbuild = rustbuild 19 self.desc = desc 20 self.value = value 21 22 23options = [] 24 25 26def o(*args): 27 options.append(Option(*args, value=False)) 28 29 30def v(*args): 31 options.append(Option(*args, value=True)) 32 33 34o("debug", "rust.debug", "enables debugging environment; does not affect optimization of bootstrapped code (use `--disable-optimize` for that)") 35o("docs", "build.docs", "build standard library documentation") 36o("compiler-docs", "build.compiler-docs", "build compiler documentation") 37o("optimize-tests", "rust.optimize-tests", "build tests with optimizations") 38o("parallel-compiler", "rust.parallel-compiler", "build a multi-threaded rustc") 39o("verbose-tests", "rust.verbose-tests", "enable verbose output when running tests") 40o("ccache", "llvm.ccache", "invoke gcc/clang via ccache to reuse object files between builds") 41o("sccache", None, "invoke gcc/clang via sccache to reuse object files between builds") 42o("local-rust", None, "use an installed rustc rather than downloading a snapshot") 43v("local-rust-root", None, "set prefix for local rust binary") 44o("local-rebuild", "build.local-rebuild", "assume local-rust matches the current version, for rebuilds; implies local-rust, and is implied if local-rust already matches the current version") 45o("llvm-static-stdcpp", "llvm.static-libstdcpp", "statically link to libstdc++ for LLVM") 46o("llvm-link-shared", "llvm.link-shared", "prefer shared linking to LLVM (llvm-config --link-shared)") 47o("rpath", "rust.rpath", "build rpaths into rustc itself") 48o("llvm-version-check", "llvm.version-check", "check if the LLVM version is supported, build anyway") 49o("codegen-tests", "rust.codegen-tests", "run the src/test/codegen tests") 50o("option-checking", None, "complain about unrecognized options in this configure script") 51o("ninja", "llvm.ninja", "build LLVM using the Ninja generator (for MSVC, requires building in the correct environment)") 52o("locked-deps", "build.locked-deps", "force Cargo.lock to be up to date") 53o("vendor", "build.vendor", "enable usage of vendored Rust crates") 54o("sanitizers", "build.sanitizers", "build the sanitizer runtimes (asan, lsan, msan, tsan)") 55o("dist-src", "rust.dist-src", "when building tarballs enables building a source tarball") 56o("cargo-native-static", "build.cargo-native-static", "static native libraries in cargo") 57o("profiler", "build.profiler", "build the profiler runtime") 58o("full-tools", None, "enable all tools") 59o("lld", "rust.lld", "build lld") 60o("lldb", "rust.lldb", "build lldb") 61o("missing-tools", "dist.missing-tools", "allow failures when building tools") 62o("use-libcxx", "llvm.use_libcxx", "build LLVM with libc++") 63 64o("cflags", "llvm.cflags", "build LLVM with these extra compiler flags") 65o("cxxflags", "llvm.cxxflags", "build LLVM with these extra compiler flags") 66o("ldflags", "llvm.ldflags", "build LLVM with these extra linker flags") 67 68o("llvm-libunwind", "rust.llvm_libunwind", "use LLVM libunwind") 69 70# Optimization and debugging options. These may be overridden by the release 71# channel, etc. 72o("optimize", "rust.optimize", "build optimized rust code") 73o("optimize-llvm", "llvm.optimize", "build optimized LLVM") 74o("llvm-assertions", "llvm.assertions", "build LLVM with assertions") 75o("debug-assertions", "rust.debug-assertions", "build with debugging assertions") 76o("llvm-release-debuginfo", "llvm.release-debuginfo", "build LLVM with debugger metadata") 77v("debuginfo-level", "rust.debuginfo-level", "debuginfo level for Rust code") 78v("debuginfo-level-rustc", "rust.debuginfo-level-rustc", "debuginfo level for the compiler") 79v("debuginfo-level-std", "rust.debuginfo-level-std", "debuginfo level for the standard library") 80v("debuginfo-level-tools", "rust.debuginfo-level-tools", "debuginfo level for the tools") 81v("debuginfo-level-tests", "rust.debuginfo-level-tests", "debuginfo level for the test suites run with compiletest") 82v("save-toolstates", "rust.save-toolstates", "save build and test status of external tools into this file") 83 84v("prefix", "install.prefix", "set installation prefix") 85v("localstatedir", "install.localstatedir", "local state directory") 86v("datadir", "install.datadir", "install data") 87v("sysconfdir", "install.sysconfdir", "install system configuration files") 88v("infodir", "install.infodir", "install additional info") 89v("libdir", "install.libdir", "install libraries") 90v("mandir", "install.mandir", "install man pages in PATH") 91v("docdir", "install.docdir", "install documentation in PATH") 92v("bindir", "install.bindir", "install binaries") 93 94v("llvm-root", None, "set LLVM root") 95v("llvm-config", None, "set path to llvm-config") 96v("llvm-filecheck", None, "set path to LLVM's FileCheck utility") 97v("python", "build.python", "set path to python") 98v("android-cross-path", "target.arm-linux-androideabi.android-ndk", 99 "Android NDK standalone path (deprecated)") 100v("i686-linux-android-ndk", "target.i686-linux-android.android-ndk", 101 "i686-linux-android NDK standalone path") 102v("arm-linux-androideabi-ndk", "target.arm-linux-androideabi.android-ndk", 103 "arm-linux-androideabi NDK standalone path") 104v("armv7-linux-androideabi-ndk", "target.armv7-linux-androideabi.android-ndk", 105 "armv7-linux-androideabi NDK standalone path") 106v("thumbv7neon-linux-androideabi-ndk", "target.thumbv7neon-linux-androideabi.android-ndk", 107 "thumbv7neon-linux-androideabi NDK standalone path") 108v("aarch64-linux-android-ndk", "target.aarch64-linux-android.android-ndk", 109 "aarch64-linux-android NDK standalone path") 110v("x86_64-linux-android-ndk", "target.x86_64-linux-android.android-ndk", 111 "x86_64-linux-android NDK standalone path") 112v("musl-root", "target.x86_64-unknown-linux-musl.musl-root", 113 "MUSL root installation directory (deprecated)") 114v("musl-root-x86_64", "target.x86_64-unknown-linux-musl.musl-root", 115 "x86_64-unknown-linux-musl install directory") 116v("musl-root-i586", "target.i586-unknown-linux-musl.musl-root", 117 "i586-unknown-linux-musl install directory") 118v("musl-root-i686", "target.i686-unknown-linux-musl.musl-root", 119 "i686-unknown-linux-musl install directory") 120v("musl-root-arm", "target.arm-unknown-linux-musleabi.musl-root", 121 "arm-unknown-linux-musleabi install directory") 122v("musl-root-armhf", "target.arm-unknown-linux-musleabihf.musl-root", 123 "arm-unknown-linux-musleabihf install directory") 124v("musl-root-armv5te", "target.armv5te-unknown-linux-musleabi.musl-root", 125 "armv5te-unknown-linux-musleabi install directory") 126v("musl-root-armv7", "target.armv7-unknown-linux-musleabi.musl-root", 127 "armv7-unknown-linux-musleabi install directory") 128v("musl-root-armv7hf", "target.armv7-unknown-linux-musleabihf.musl-root", 129 "armv7-unknown-linux-musleabihf install directory") 130v("musl-root-aarch64", "target.aarch64-unknown-linux-musl.musl-root", 131 "aarch64-unknown-linux-musl install directory") 132v("musl-root-mips", "target.mips-unknown-linux-musl.musl-root", 133 "mips-unknown-linux-musl install directory") 134v("musl-root-mipsel", "target.mipsel-unknown-linux-musl.musl-root", 135 "mipsel-unknown-linux-musl install directory") 136v("musl-root-mips64", "target.mips64-unknown-linux-muslabi64.musl-root", 137 "mips64-unknown-linux-muslabi64 install directory") 138v("musl-root-mips64el", "target.mips64el-unknown-linux-muslabi64.musl-root", 139 "mips64el-unknown-linux-muslabi64 install directory") 140v("qemu-armhf-rootfs", "target.arm-unknown-linux-gnueabihf.qemu-rootfs", 141 "rootfs in qemu testing, you probably don't want to use this") 142v("qemu-aarch64-rootfs", "target.aarch64-unknown-linux-gnu.qemu-rootfs", 143 "rootfs in qemu testing, you probably don't want to use this") 144v("experimental-targets", "llvm.experimental-targets", 145 "experimental LLVM targets to build") 146v("release-channel", "rust.channel", "the name of the release channel to build") 147 148# Used on systems where "cc" is unavailable 149v("default-linker", "rust.default-linker", "the default linker") 150 151# Many of these are saved below during the "writing configuration" step 152# (others are conditionally saved). 153o("manage-submodules", "build.submodules", "let the build manage the git submodules") 154o("full-bootstrap", "build.full-bootstrap", "build three compilers instead of two") 155o("extended", "build.extended", "build an extended rust tool set") 156 157v("tools", None, "List of extended tools will be installed") 158v("build", "build.build", "GNUs ./configure syntax LLVM build triple") 159v("host", None, "GNUs ./configure syntax LLVM host triples") 160v("target", None, "GNUs ./configure syntax LLVM target triples") 161 162v("set", None, "set arbitrary key/value pairs in TOML configuration") 163 164 165def p(msg): 166 print("configure: " + msg) 167 168 169def err(msg): 170 print("configure: error: " + msg) 171 sys.exit(1) 172 173 174if '--help' in sys.argv or '-h' in sys.argv: 175 print('Usage: ./configure [options]') 176 print('') 177 print('Options') 178 for option in options: 179 if 'android' in option.name: 180 # no one needs to know about these obscure options 181 continue 182 if option.value: 183 print('\t{:30} {}'.format('--{}=VAL'.format(option.name), option.desc)) 184 else: 185 print('\t{:30} {}'.format('--enable-{}'.format(option.name), option.desc)) 186 print('') 187 print('This configure script is a thin configuration shim over the true') 188 print('configuration system, `config.toml`. You can explore the comments') 189 print('in `config.toml.example` next to this configure script to see') 190 print('more information about what each option is. Additionally you can') 191 print('pass `--set` as an argument to set arbitrary key/value pairs') 192 print('in the TOML configuration if desired') 193 print('') 194 print('Also note that all options which take `--enable` can similarly') 195 print('be passed with `--disable-foo` to forcibly disable the option') 196 sys.exit(0) 197 198# Parse all command line arguments into one of these three lists, handling 199# boolean and value-based options separately 200unknown_args = [] 201need_value_args = [] 202known_args = {} 203 204p("processing command line") 205i = 1 206while i < len(sys.argv): 207 arg = sys.argv[i] 208 i += 1 209 if not arg.startswith('--'): 210 unknown_args.append(arg) 211 continue 212 213 found = False 214 for option in options: 215 value = None 216 if option.value: 217 keyval = arg[2:].split('=', 1) 218 key = keyval[0] 219 if option.name != key: 220 continue 221 222 if len(keyval) > 1: 223 value = keyval[1] 224 elif i < len(sys.argv): 225 value = sys.argv[i] 226 i += 1 227 else: 228 need_value_args.append(arg) 229 continue 230 else: 231 if arg[2:] == 'enable-' + option.name: 232 value = True 233 elif arg[2:] == 'disable-' + option.name: 234 value = False 235 else: 236 continue 237 238 found = True 239 if option.name not in known_args: 240 known_args[option.name] = [] 241 known_args[option.name].append((option, value)) 242 break 243 244 if not found: 245 unknown_args.append(arg) 246p("") 247 248# Note: here and a few other places, we use [-1] to apply the *last* value 249# passed. But if option-checking is enabled, then the known_args loop will 250# also assert that options are only passed once. 251option_checking = ('option-checking' not in known_args 252 or known_args['option-checking'][-1][1]) 253if option_checking: 254 if len(unknown_args) > 0: 255 err("Option '" + unknown_args[0] + "' is not recognized") 256 if len(need_value_args) > 0: 257 err("Option '{0}' needs a value ({0}=val)".format(need_value_args[0])) 258 259# Parse all known arguments into a configuration structure that reflects the 260# TOML we're going to write out 261config = {} 262 263 264def build(): 265 if 'build' in known_args: 266 return known_args['build'][-1][1] 267 return bootstrap.default_build_triple() 268 269 270def set(key, value): 271 s = "{:20} := {}".format(key, value) 272 if len(s) < 70: 273 p(s) 274 else: 275 p(s[:70] + " ...") 276 277 arr = config 278 parts = key.split('.') 279 for i, part in enumerate(parts): 280 if i == len(parts) - 1: 281 arr[part] = value 282 else: 283 if part not in arr: 284 arr[part] = {} 285 arr = arr[part] 286 287 288for key in known_args: 289 # The `set` option is special and can be passed a bunch of times 290 if key == 'set': 291 for option, value in known_args[key]: 292 keyval = value.split('=', 1) 293 if len(keyval) == 1 or keyval[1] == "true": 294 value = True 295 elif keyval[1] == "false": 296 value = False 297 else: 298 value = keyval[1] 299 set(keyval[0], value) 300 continue 301 302 # Ensure each option is only passed once 303 arr = known_args[key] 304 if option_checking and len(arr) > 1: 305 err("Option '{}' provided more than once".format(key)) 306 option, value = arr[-1] 307 308 # If we have a clear avenue to set our value in rustbuild, do so 309 if option.rustbuild is not None: 310 set(option.rustbuild, value) 311 continue 312 313 # Otherwise we're a "special" option and need some extra handling, so do 314 # that here. 315 if option.name == 'sccache': 316 set('llvm.ccache', 'sccache') 317 elif option.name == 'local-rust': 318 for path in os.environ['PATH'].split(os.pathsep): 319 if os.path.exists(path + '/rustc'): 320 set('build.rustc', path + '/rustc') 321 break 322 for path in os.environ['PATH'].split(os.pathsep): 323 if os.path.exists(path + '/cargo'): 324 set('build.cargo', path + '/cargo') 325 break 326 elif option.name == 'local-rust-root': 327 set('build.rustc', value + '/bin/rustc') 328 set('build.cargo', value + '/bin/cargo') 329 elif option.name == 'llvm-root': 330 set('target.{}.llvm-config'.format(build()), value + '/bin/llvm-config') 331 elif option.name == 'llvm-config': 332 set('target.{}.llvm-config'.format(build()), value) 333 elif option.name == 'llvm-filecheck': 334 set('target.{}.llvm-filecheck'.format(build()), value) 335 elif option.name == 'tools': 336 set('build.tools', value.split(',')) 337 elif option.name == 'host': 338 set('build.host', value.split(',')) 339 elif option.name == 'target': 340 set('build.target', value.split(',')) 341 elif option.name == 'full-tools': 342 set('rust.codegen-backends', ['llvm']) 343 set('rust.lld', True) 344 set('rust.llvm-tools', True) 345 set('build.extended', True) 346 elif option.name == 'option-checking': 347 # this was handled above 348 pass 349 else: 350 raise RuntimeError("unhandled option {}".format(option.name)) 351 352set('build.configure-args', sys.argv[1:]) 353 354# "Parse" the `config.toml.example` file into the various sections, and we'll 355# use this as a template of a `config.toml` to write out which preserves 356# all the various comments and whatnot. 357# 358# Note that the `target` section is handled separately as we'll duplicate it 359# per configured target, so there's a bit of special handling for that here. 360sections = {} 361cur_section = None 362sections[None] = [] 363section_order = [None] 364targets = {} 365 366for line in open(rust_dir + '/config.toml.example').read().split("\n"): 367 if line.startswith('['): 368 cur_section = line[1:-1] 369 if cur_section.startswith('target'): 370 cur_section = 'target' 371 elif '.' in cur_section: 372 raise RuntimeError("don't know how to deal with section: {}".format(cur_section)) 373 sections[cur_section] = [line] 374 section_order.append(cur_section) 375 else: 376 sections[cur_section].append(line) 377 378# Fill out the `targets` array by giving all configured targets a copy of the 379# `target` section we just loaded from the example config 380configured_targets = [build()] 381if 'build' in config: 382 if 'host' in config['build']: 383 configured_targets += config['build']['host'] 384 if 'target' in config['build']: 385 configured_targets += config['build']['target'] 386if 'target' in config: 387 for target in config['target']: 388 configured_targets.append(target) 389for target in configured_targets: 390 targets[target] = sections['target'][:] 391 targets[target][0] = targets[target][0].replace("x86_64-unknown-linux-gnu", target) 392 393 394def is_number(value): 395 try: 396 float(value) 397 return True 398 except ValueError: 399 return False 400 401# Here we walk through the constructed configuration we have from the parsed 402# command line arguments. We then apply each piece of configuration by 403# basically just doing a `sed` to change the various configuration line to what 404# we've got configure. 405def to_toml(value): 406 if isinstance(value, bool): 407 if value: 408 return "true" 409 else: 410 return "false" 411 elif isinstance(value, list): 412 return '[' + ', '.join(map(to_toml, value)) + ']' 413 elif isinstance(value, str): 414 # Don't put quotes around numeric values 415 if is_number(value): 416 return value 417 else: 418 return "'" + value + "'" 419 else: 420 raise RuntimeError('no toml') 421 422 423def configure_section(lines, config): 424 for key in config: 425 value = config[key] 426 found = False 427 for i, line in enumerate(lines): 428 if not line.startswith('#' + key + ' = '): 429 continue 430 found = True 431 lines[i] = "{} = {}".format(key, to_toml(value)) 432 break 433 if not found: 434 raise RuntimeError("failed to find config line for {}".format(key)) 435 436 437for section_key in config: 438 section_config = config[section_key] 439 if section_key not in sections: 440 raise RuntimeError("config key {} not in sections".format(section_key)) 441 442 if section_key == 'target': 443 for target in section_config: 444 configure_section(targets[target], section_config[target]) 445 else: 446 configure_section(sections[section_key], section_config) 447 448# Now that we've built up our `config.toml`, write it all out in the same 449# order that we read it in. 450p("") 451p("writing `config.toml` in current directory") 452with bootstrap.output('config.toml') as f: 453 for section in section_order: 454 if section == 'target': 455 for target in targets: 456 for line in targets[target]: 457 f.write(line + "\n") 458 else: 459 for line in sections[section]: 460 f.write(line + "\n") 461 462with bootstrap.output('Makefile') as f: 463 contents = os.path.join(rust_dir, 'src', 'bootstrap', 'mk', 'Makefile.in') 464 contents = open(contents).read() 465 contents = contents.replace("$(CFG_SRC_DIR)", rust_dir + '/') 466 contents = contents.replace("$(CFG_PYTHON)", sys.executable) 467 f.write(contents) 468 469p("") 470p("run `python {}/x.py --help`".format(rust_dir)) 471p("")