1 # -*- Mode: python; indent-tabs-mode: nil; tab-width: 40 -*-
2 # vim: set filetype=python:
3 # This Source Code Form is subject to the terms of the Mozilla Public
4 # License, v. 2.0. If a copy of the MPL was not distributed with this
5 # file, You can obtain one at http://mozilla.org/MPL/2.0/.
8 # Rust is required by `rust_compiler` below. We allow_missing here
9 # to propagate failures to the better error message there.
10 option(env="RUSTC", nargs=1, help="Path to the rust compiler")
11 option(env="CARGO", nargs=1, help="Path to the Cargo package manager")
17 paths=rust_search_path,
25 paths=rust_search_path,
32 def unwrap_rustup(prog, name):
33 # rustc and cargo can either be rustup wrappers, or they can be the actual,
34 # plain executables. For cargo, on OSX, rustup sets DYLD_LIBRARY_PATH (at
35 # least until https://github.com/rust-lang/rustup.rs/pull/1752 is merged
36 # and shipped) and that can wreak havoc (see bug 1536486). Similarly, for
37 # rustc, rustup silently honors toolchain overrides set by vendored crates
40 # In either case, we need to find the plain executables.
42 # To achieve that, try to run `PROG +stable`. When the rustup wrapper is in
43 # use, it either prints PROG's help and exits with status 0, or prints
44 # an error message (error: toolchain 'stable' is not installed) and exits
45 # with status 1. In the cargo case, when plain cargo is in use, it exits
46 # with a different error message (e.g. "error: no such subcommand:
47 # `+stable`"), and exits with status 101.
49 # Unfortunately, in the rustc case, when plain rustc is in use,
50 # `rustc +stable` will exit with status 1, complaining about a missing
51 # "+stable" file. We'll examine the error output to try and distinguish
52 # between failing rustup and failing rustc.
53 @depends(prog, dependable(name))
54 @imports(_from="__builtin__", _import="open")
56 def unwrap(prog, name):
60 def from_rustup_which():
61 out = check_cmd_output("rustup", "which", name, executable=prog).rstrip()
62 # If for some reason the above failed to return something, keep the
63 # PROG we found originally.
65 log.info("Actually using '%s'", out)
68 log.info("No `rustup which` output, using '%s'", prog)
71 (retcode, stdout, stderr) = get_cmd_output(prog, "+stable")
73 if name == "cargo" and retcode != 101:
74 prog = from_rustup_which()
77 prog = from_rustup_which()
78 elif "+stable" in stderr:
79 # PROG looks like plain `rustc`.
82 # Assume PROG looks like `rustup`. This case is a little weird,
83 # insofar as the user doesn't have the "stable" toolchain
84 # installed, but go ahead and unwrap anyway: the user might
85 # have only certain versions, beta, or nightly installed, and
86 # we'll catch invalid versions later.
87 prog = from_rustup_which()
94 rustc = unwrap_rustup(rustc, "rustc")
95 cargo = unwrap_rustup(cargo, "cargo")
98 set_config("CARGO", cargo)
99 set_config("RUSTC", rustc)
103 @checking("rustc version", lambda info: info.version)
104 def rustc_info(rustc):
107 out = check_cmd_output(rustc, "--version", "--verbose").splitlines()
108 info = dict((s.strip() for s in line.split(":", 1)) for line in out[1:])
110 version=Version(info.get("release", "0")),
111 commit=info.get("commit-hash", "unknown"),
113 llvm_version=Version(info.get("LLVM version", "0")),
119 depends(rustc_info)(lambda info: str(info.version) if info else None),
124 @checking("cargo version", lambda info: info.version)
126 def cargo_info(cargo):
129 out = check_cmd_output(cargo, "--version", "--verbose").splitlines()
130 info = dict((s.strip() for s in line.split(":", 1)) for line in out[1:])
131 version = info.get("release")
132 # Older versions of cargo didn't support --verbose, in which case, they
133 # only output a not-really-pleasant-to-parse output. Fortunately, they
134 # don't error out, so we can just try some regexp matching on the output
137 VERSION_FORMAT = r"^cargo (\d\.\d+\.\d+).*"
139 m = re.search(VERSION_FORMAT, out[0])
140 # Fail fast if cargo changes its output on us.
142 die("Could not determine cargo version from output: %s", out)
146 version=Version(version),
150 @depends(rustc_info, cargo_info)
151 @imports(_from="mozboot.util", _import="MINIMUM_RUST_VERSION")
152 @imports(_from="textwrap", _import="dedent")
153 def rust_compiler(rustc_info, cargo_info):
158 Rust compiler not found.
159 To compile rust language sources, you must have 'rustc' in your path.
160 See https://www.rust-lang.org/ for more information.
162 You can install rust by running './mach bootstrap'
163 or by directly running the installer from https://rustup.rs/
167 rustc_min_version = Version(MINIMUM_RUST_VERSION)
168 cargo_min_version = rustc_min_version
170 version = rustc_info.version
171 is_nightly = "nightly" in version.version
172 is_version_number_match = (
173 version.major == rustc_min_version.major
174 and version.minor == rustc_min_version.minor
175 and version.patch == rustc_min_version.patch
178 if version < rustc_min_version or (is_version_number_match and is_nightly):
182 Rust compiler {} is too old.
184 To compile Rust language sources please install at least
185 version {} of the 'rustc' toolchain (or, if using nightly,
186 at least one version newer than {}) and make sure it is
189 You can verify this by typing 'rustc --version'.
191 If you have the 'rustup' tool installed you can upgrade
192 to the latest release by typing 'rustup update'. The
193 installer is available from https://rustup.rs/
195 version, rustc_min_version, rustc_min_version
204 Cargo package manager not found.
205 To compile Rust language sources, you must have 'cargo' in your path.
206 See https://www.rust-lang.org/ for more information.
208 You can install cargo by running './mach bootstrap'
209 or by directly running the installer from https://rustup.rs/
214 version = cargo_info.version
215 if version < cargo_min_version:
219 Cargo package manager {} is too old.
221 To compile Rust language sources please install at least
222 version {} of 'cargo' and make sure it is first in your path.
224 You can verify this by typing 'cargo --version'.
226 ).format(version, cargo_min_version)
232 @depends(rustc, when=rust_compiler)
233 @imports(_from="__builtin__", _import="ValueError")
234 def rust_supported_targets(rustc):
235 out = check_cmd_output(rustc, "--print", "target-list").splitlines()
239 info = split_triplet(t, allow_wasi=True)
241 if t.startswith("thumb"):
242 cpu, rest = t.split("-", 1)
243 retry = "-".join(("arm", rest))
244 elif t.endswith("-windows-msvc"):
245 retry = t[: -len("windows-msvc")] + "mingw32"
246 elif t.endswith("-windows-gnu"):
247 retry = t[: -len("windows-gnu")] + "mingw32"
251 info = split_triplet(retry, allow_wasi=True)
254 key = (info.cpu, info.endianness, info.os)
255 data.setdefault(key, []).append(namespace(rust_target=t, target=info))
259 def detect_rustc_target(
260 host_or_target, compiler_info, arm_target, rust_supported_targets
262 # Rust's --target options are similar to, but not exactly the same
263 # as, the autoconf-derived targets we use. An example would be that
264 # Rust uses distinct target triples for targetting the GNU C++ ABI
265 # and the MSVC C++ ABI on Win32, whereas autoconf has a single
266 # triple and relies on the user to ensure that everything is
267 # compiled for the appropriate ABI. We need to perform appropriate
268 # munging to get the correct option to rustc.
269 # We correlate the autoconf-derived targets with the list of targets
270 # rustc gives us with --print target-list.
271 candidates = rust_supported_targets.get(
272 (host_or_target.cpu, host_or_target.endianness, host_or_target.os), []
275 def find_candidate(candidates):
276 if len(candidates) == 1:
277 return candidates[0].rust_target
281 # We have multiple candidates. There are two cases where we can try to
282 # narrow further down using extra information from the build system.
283 # - For windows targets, correlate with the C compiler type
284 if host_or_target.kernel == "WINNT":
285 if compiler_info.type in ("gcc", "clang"):
286 suffix = "windows-gnu"
288 suffix = "windows-msvc"
290 c for c in candidates if c.rust_target.endswith("-{}".format(suffix))
292 if len(narrowed) == 1:
293 return narrowed[0].rust_target
295 candidates = narrowed
297 vendor_aliases = {"pc": ("w64", "windows")}
301 if host_or_target.vendor in vendor_aliases.get(c.target.vendor, ())
304 if len(narrowed) == 1:
305 return narrowed[0].rust_target
307 # - For arm targets, correlate with arm_target
308 # we could be more thorough with the supported rust targets, but they
309 # don't support OSes that are supported to build Gecko anyways.
310 # Also, sadly, the only interface to check the rust target cpu features
311 # is --print target-spec-json, and it's unstable, so we have to rely on
312 # our own knowledge of what each arm target means.
313 if host_or_target.cpu == "arm" and host_or_target.endianness == "little":
315 if arm_target.arm_arch >= 7:
316 if arm_target.thumb2 and arm_target.fpu == "neon":
317 prefixes.append("thumbv7neon")
318 if arm_target.thumb2:
319 prefixes.append("thumbv7a")
320 prefixes.append("armv7")
321 if arm_target.arm_arch >= 6:
322 prefixes.append("armv6")
323 if host_or_target.os != "Android":
324 # arm-* rust targets are armv6... except arm-linux-androideabi
325 prefixes.append("arm")
326 if arm_target.arm_arch >= 5:
327 prefixes.append("armv5te")
328 if host_or_target.os == "Android":
329 # arm-* rust targets are armv6... except arm-linux-androideabi
330 prefixes.append("arm")
331 if arm_target.arm_arch >= 4:
332 prefixes.append("armv4t")
333 # rust freebsd targets are the only ones that don't have a 'hf' suffix
334 # for hard-float. Technically, that means if the float abi ever is not
335 # hard-float, this will pick a wrong target, but since rust only
336 # supports hard-float, let's assume that means freebsd only support
338 if arm_target.float_abi == "hard" and host_or_target.os != "FreeBSD":
344 if c.rust_target.startswith(
346 ) and c.rust_target.endswith(suffix):
349 # See if we can narrow down on the exact alias
350 narrowed = [c for c in candidates if c.target.alias == host_or_target.alias]
351 if len(narrowed) == 1:
352 return narrowed[0].rust_target
354 candidates = narrowed
356 # See if we can narrow down with the raw OS
357 narrowed = [c for c in candidates if c.target.raw_os == host_or_target.raw_os]
358 if len(narrowed) == 1:
359 return narrowed[0].rust_target
361 candidates = narrowed
363 # See if we can narrow down with the raw OS and raw CPU
367 if c.target.raw_os == host_or_target.raw_os
368 and c.target.raw_cpu == host_or_target.raw_cpu
370 if len(narrowed) == 1:
371 return narrowed[0].rust_target
373 # Finally, see if the vendor can be used to disambiguate.
374 narrowed = [c for c in candidates if c.target.vendor == host_or_target.vendor]
375 if len(narrowed) == 1:
376 return narrowed[0].rust_target
380 rustc_target = find_candidate(candidates)
382 if rustc_target is None:
383 die("Don't know how to translate {} for rustc".format(host_or_target.alias))
389 @imports(_from="six", _import="ensure_binary")
390 @imports(_from="tempfile", _import="mkstemp")
391 @imports(_from="textwrap", _import="dedent")
392 @imports(_from="mozbuild.configure.util", _import="LineIO")
393 def assert_rust_compile(host_or_target, rustc_target, rustc):
394 # Check to see whether our rustc has a reasonably functional stdlib
395 # for our chosen target.
396 target_arg = "--target=" + rustc_target
397 in_fd, in_path = mkstemp(prefix="conftest", suffix=".rs", text=True)
398 out_fd, out_path = mkstemp(prefix="conftest", suffix=".rlib")
401 source = 'pub extern fn hello() { println!("Hello world"); }'
402 log.debug("Creating `%s` with content:", in_path)
403 with LineIO(lambda l: log.debug("| %s", l)) as out:
406 os.write(in_fd, ensure_binary(source))
423 Cannot compile for {} with {}
424 The target may be unsupported, or you may not have
425 a rust std library for that target installed. Try:
429 host_or_target.alias, rustc, rustc_target
434 check_cmd_output(*cmd, onerror=failed)
435 if not os.path.exists(out_path) or os.path.getsize(out_path) == 0:
447 rust_supported_targets,
451 @checking("for rust host triplet")
452 @imports(_from="textwrap", _import="dedent")
453 def rust_host_triple(
454 rustc, host, compiler_info, rustc_host, rust_supported_targets, arm_target
456 rustc_target = detect_rustc_target(
457 host, compiler_info, arm_target, rust_supported_targets
459 if rustc_target != rustc_host:
460 if host.alias == rustc_target:
461 configure_host = host.alias
463 configure_host = "{}/{}".format(host.alias, rustc_target)
467 The rust compiler host ({rustc}) is not suitable for the configure host ({configure}).
469 You can solve this by:
470 * Set your configure host to match the rust compiler host by editing your
471 mozconfig and adding "ac_add_options --host={rustc}".
472 * Or, install the rust toolchain for {configure}, if supported, by running
473 "rustup default stable-{rustc_target}"
476 configure=configure_host,
477 rustc_target=rustc_target,
481 assert_rust_compile(host, rustc_target, rustc)
486 rustc, target, c_compiler, rust_supported_targets, arm_target, when=rust_compiler
488 @checking("for rust target triplet")
489 def rust_target_triple(
490 rustc, target, compiler_info, rust_supported_targets, arm_target
492 rustc_target = detect_rustc_target(
493 target, compiler_info, arm_target, rust_supported_targets
495 assert_rust_compile(target, rustc_target, rustc)
499 set_config("RUST_TARGET", rust_target_triple)
500 set_config("RUST_HOST_TARGET", rust_host_triple)
503 # This is used for putting source info into symbol files.
504 set_config("RUSTC_COMMIT", depends(rustc_info)(lambda i: i.commit))
506 # Rustdoc is required by Rust tests below.
507 option(env="RUSTDOC", nargs=1, help="Path to the rustdoc program")
509 rustdoc = check_prog(
512 paths=rust_search_path,
517 # This option is separate from --enable-tests because Rust tests are particularly
518 # expensive in terms of compile time (especially for code in libxul).
520 "--enable-rust-tests",
521 help="Enable building and running of Rust tests during `make check`",
525 @depends("--enable-rust-tests", rustdoc)
526 def rust_tests(enable_rust_tests, rustdoc):
527 if enable_rust_tests and not rustdoc:
528 die("--enable-rust-tests requires rustdoc")
529 return bool(enable_rust_tests)
532 set_config("MOZ_RUST_TESTS", rust_tests)
535 @depends(target, c_compiler, rustc)
537 def rustc_natvis_ldflags(target, compiler_info, rustc):
538 if target.kernel == "WINNT" and compiler_info.type == "clang-cl":
539 sysroot = check_cmd_output(rustc, "--print", "sysroot").strip()
540 etc = os.path.join(sysroot, "lib/rustlib/etc")
542 if os.path.isdir(etc):
543 for f in os.listdir(etc):
544 if f.endswith(".natvis"):
545 ldflags.append("-NATVIS:" + normsep(os.path.join(etc, f)))
549 set_config("RUSTC_NATVIS_LDFLAGS", rustc_natvis_ldflags)
553 "--enable-rust-debug",
554 default=depends(when="--enable-debug")(lambda: True),
555 help="{Build|Do not build} Rust code with debug assertions turned " "on.",
559 @depends(when="--enable-rust-debug")
564 set_config("MOZ_DEBUG_RUST", debug_rust)
565 set_define("MOZ_DEBUG_RUST", debug_rust)
567 # ==============================================================
569 option(env="RUSTFLAGS", nargs=1, help="Rust compiler flags")
570 set_config("RUSTFLAGS", depends("RUSTFLAGS")(lambda flags: flags))
573 # Rust compiler flags
574 # ==============================================================
577 @depends(moz_optimize)
578 def rustc_opt_level_default(moz_optimize):
579 return "2" if moz_optimize.optimize else "0"
583 env="RUSTC_OPT_LEVEL",
584 default=rustc_opt_level_default,
586 help="Rust compiler optimization level (-C opt-level=%s)",
590 @depends("RUSTC_OPT_LEVEL")
591 def rustc_opt_level(opt_level_option):
592 return opt_level_option[0]
595 set_config("CARGO_PROFILE_RELEASE_OPT_LEVEL", rustc_opt_level)
596 set_config("CARGO_PROFILE_DEV_OPT_LEVEL", rustc_opt_level)
603 "--enable-debug-symbols",
604 "--enable-frame-pointers",
606 def rust_compile_flags(opt_level, debug_rust, target, debug_symbols, frame_pointers):
607 # Cargo currently supports only two interesting profiles for building:
608 # development and release. Those map (roughly) to --enable-debug and
609 # --disable-debug in Gecko, respectively.
611 # But we'd also like to support an additional axis of control for
612 # optimization level. Since Cargo only supports 2 profiles, we're in
615 # Code here derives various compiler options given other configure options.
616 # The options defined here effectively override defaults specified in
619 debug_assertions = None
622 # opt-level=0 implies -C debug-assertions, which may not be desired
623 # unless Rust debugging is enabled.
624 if opt_level == "0" and not debug_rust:
625 debug_assertions = False
632 if debug_assertions is not None:
633 opts.append("debug-assertions=%s" % ("yes" if debug_assertions else "no"))
634 if debug_info is not None:
635 opts.append("debuginfo=%s" % debug_info)
637 opts.append("force-frame-pointers=yes")
638 # CFG for arm64 is crashy, see `def security_hardening_cflags`.
639 if target.kernel == "WINNT" and target.cpu != "aarch64":
640 opts.append("control-flow-guard=yes")
644 flags.extend(["-C", opt])
649 # Rust incremental compilation
650 # ==============================================================
653 option("--disable-cargo-incremental", help="Disable incremental rust compilation.")
661 "--disable-cargo-incremental",
666 def cargo_incremental(
675 """Return a value for the CARGO_INCREMENTAL environment variable."""
680 # We never want to use incremental compilation in automation. sccache
681 # handles our automation use case much better than incremental compilation
686 # Coverage instrumentation doesn't play well with incremental compilation
687 # https://github.com/rust-lang/rust/issues/50203.
691 # Incremental compilation doesn't work as well as it should, and if we're
692 # using sccache, it's better to use sccache than incremental compilation.
693 if not using_sccache and rustc_wrapper:
694 rustc_wrapper = os.path.basename(rustc_wrapper[0])
695 if os.path.splitext(rustc_wrapper)[0].lower() == "sccache":
700 # Incremental compilation is automatically turned on for debug builds, so
701 # we don't need to do anything special here.
705 # --enable-release automatically sets -O2 for Rust code, and people can
706 # set RUSTC_OPT_LEVEL to 2 or even 3 if they want to profile Rust code.
707 # Let's assume that if Rust code is using -O2 or higher, we shouldn't
708 # be using incremental compilation, because we'd be imposing a
709 # significant runtime cost.
710 if opt_level not in ("0", "1"):
713 # We're clear to use incremental compilation!
717 set_config("CARGO_INCREMENTAL", cargo_incremental)
720 @depends(rust_compile_flags, rust_warning_flags)
721 def rust_flags(compile_flags, warning_flags):
722 return compile_flags + warning_flags
725 set_config("MOZ_RUST_DEFAULT_FLAGS", rust_flags)