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