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