Debian Codegraph @

build/moz.configure/rust.configure

raw ◀ ▶
2 Debian patches touch this file
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/.
6
7
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")
12
13 rustc = check_prog(
14 "_RUSTC",
15 ["rustc"],
16 what="rustc",
17 paths=rust_search_path,
18 input="RUSTC",
19 allow_missing=True,
20 )
21 cargo = check_prog(
22 "_CARGO",
23 ["cargo"],
24 what="cargo",
25 paths=rust_search_path,
26 input="CARGO",
27 allow_missing=True,
28 )
29
30
31 @template
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
38 # (see bug 1547196).
39 #
40 # In either case, we need to find the plain executables.
41 #
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.
48 #
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")
55 @imports("os")
56 def unwrap(prog, name):
57 if not prog:
58 return
59
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.
64 if out:
65 log.info("Actually using '%s'", out)
66 return out
67
68 log.info("No `rustup which` output, using '%s'", prog)
69 return prog
70
71 (retcode, stdout, stderr) = get_cmd_output(prog, "+stable")
72
73 if name == "cargo" and retcode != 101:
74 prog = from_rustup_which()
75 elif name == "rustc":
76 if retcode == 0:
77 prog = from_rustup_which()
78 elif "+stable" in stderr:
79 # PROG looks like plain `rustc`.
80 pass
81 else:
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()
88
89 return normalize_path(prog)
90
91 return unwrap
92
93
94 rustc = unwrap_rustup(rustc, "rustc")
95 cargo = unwrap_rustup(cargo, "cargo")
96
97
98 set_config("CARGO", cargo)
99 set_config("RUSTC", rustc)
100
101
102 @depends_if(rustc)
103 @checking("rustc version", lambda info: info.version)
104 def rustc_info(rustc):
105 if not rustc:
106 return
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:])
109 return namespace(
110 version=Version(info.get("release", "0")),
111 commit=info.get("commit-hash", "unknown"),
112 host=info["host"],
113 llvm_version=Version(info.get("LLVM version", "0")),
114 )
115
116
117 set_config(
118 "RUSTC_VERSION",
119 depends(rustc_info)(lambda info: str(info.version) if info else None),
120 )
121
122
123 set_config(
124 "RUSTC_LLVM_VERSION",
125 depends(rustc_info)(lambda info: str(info.llvm_version) if info else None),
126 )
127
128 set_config(
129 "MOZ_CLANG_NEWER_THAN_RUSTC_LLVM",
130 depends(c_compiler, rustc_info)(
131 lambda c_compiler, rustc_info: rustc_info
132 and c_compiler.type == "clang"
133 and c_compiler.version.major > rustc_info.llvm_version.major
134 ),
135 )
136
137
138 @depends_if(cargo)
139 @checking("cargo version", lambda info: info.version)
140 @imports("re")
141 def cargo_info(cargo):
142 if not cargo:
143 return
144 out = check_cmd_output(cargo, "--version", "--verbose").splitlines()
145 info = dict((s.strip() for s in line.split(":", 1)) for line in out[1:])
146 version = info.get("release")
147 # Older versions of cargo didn't support --verbose, in which case, they
148 # only output a not-really-pleasant-to-parse output. Fortunately, they
149 # don't error out, so we can just try some regexp matching on the output
150 # we already got.
151 if version is None:
152 VERSION_FORMAT = r"^cargo (\d\.\d+\.\d+).*"
153
154 m = re.search(VERSION_FORMAT, out[0])
155 # Fail fast if cargo changes its output on us.
156 if not m:
157 die("Could not determine cargo version from output: %s", out)
158 version = m.group(1)
159
160 return namespace(
161 version=Version(version),
162 )
163
164
165 @depends(rustc_info, cargo_info, target)
166 @imports(_from="mozboot.util", _import="MINIMUM_RUST_VERSION")
167 @imports(_from="textwrap", _import="dedent")
168 def rust_compiler(rustc_info, cargo_info, target):
169 if not rustc_info:
170 die(
171 dedent(
172 """\
173 Rust compiler not found.
174 To compile rust language sources, you must have 'rustc' in your path.
175 See https://www.rust-lang.org/ for more information.
176
177 You can install rust by running './mach bootstrap'
178 or by directly running the installer from https://rustup.rs/
179 """
180 )
181 )
182 rustc_min_version = Version(MINIMUM_RUST_VERSION)
•183 cargo_min_version = Version("1.56.0")
184
185 version = rustc_info.version
186 is_nightly = "nightly" in version.version
187 is_version_number_match = (
188 version.major == rustc_min_version.major
189 and version.minor == rustc_min_version.minor
190 and version.patch == rustc_min_version.patch
191 )
192
193 if version < rustc_min_version or (is_version_number_match and is_nightly):
194 die(
195 dedent(
196 """\
197 Rust compiler {} is too old.
198
199 To compile Rust language sources please install at least
200 version {} of the 'rustc' toolchain (or, if using nightly,
201 at least one version newer than {}) and make sure it is
202 first in your path.
203
204 You can verify this by typing 'rustc --version'.
205
206 If you have the 'rustup' tool installed you can upgrade
207 to the latest release by typing 'rustup update'. The
208 installer is available from https://rustup.rs/
209 """.format(
210 version, rustc_min_version, rustc_min_version
211 )
212 )
213 )
214
215 if target.kernel == "WINNT" and (version.major, version.minor) == (1, 56):
216 die(
217 dedent(
218 """\
219 Rust compiler 1.56.* is not supported for Windows builds.
220
221 Use a newer or an older version.
222
223 See https://github.com/rust-lang/rust/issues/88576.
224 """
225 )
226 )
227
228 if not cargo_info:
229 die(
230 dedent(
231 """\
232 Cargo package manager not found.
233 To compile Rust language sources, you must have 'cargo' in your path.
234 See https://www.rust-lang.org/ for more information.
235
236 You can install cargo by running './mach bootstrap'
237 or by directly running the installer from https://rustup.rs/
238 """
239 )
240 )
241
242 version = cargo_info.version
243 if version < cargo_min_version:
244 die(
245 dedent(
246 """\
247 Cargo package manager {} is too old.
248
249 To compile Rust language sources please install at least
250 version {} of 'cargo' and make sure it is first in your path.
251
252 You can verify this by typing 'cargo --version'.
253 """
254 ).format(version, cargo_min_version)
255 )
256
257 return True
258
259
260 @depends(rustc, when=rust_compiler)
261 @imports(_from="__builtin__", _import="ValueError")
262 def rust_supported_targets(rustc):
263 out = check_cmd_output(rustc, "--print", "target-list").splitlines()
264 data = {}
265 for t in out:
266 try:
267 info = split_triplet(t, allow_wasi=True)
268 except ValueError:
269 if t.startswith("thumb"):
270 cpu, rest = t.split("-", 1)
271 retry = "-".join(("arm", rest))
272 else:
273 continue
274 try:
275 info = split_triplet(retry, allow_wasi=True)
276 except ValueError:
277 continue
278 key = (info.cpu, info.endianness, info.os)
279 data.setdefault(key, []).append(namespace(rust_target=t, target=info))
280 return data
281
•282 option(env="RUST_TARGET", nargs=1, help="Rust compiler target triple override")
283
284 @depends("RUST_TARGET")
285 def rust_target_env(value):
286 return value[0] if value else None
287
288 def detect_rustc_target(
•289 host_or_target, compiler_info, arm_target, rust_supported_targets, rust_target_env=None
290 ):
291 # Rust's --target options are similar to, but not exactly the same
292 # as, the autoconf-derived targets we use. An example would be that
293 # Rust uses distinct target triples for targetting the GNU C++ ABI
294 # and the MSVC C++ ABI on Win32, whereas autoconf has a single
295 # triple and relies on the user to ensure that everything is
296 # compiled for the appropriate ABI. We need to perform appropriate
297 # munging to get the correct option to rustc.
298 # We correlate the autoconf-derived targets with the list of targets
299 # rustc gives us with --print target-list.
300 candidates = rust_supported_targets.get(
301 (host_or_target.cpu, host_or_target.endianness, host_or_target.os), []
302 )
303
304 def find_candidate(candidates):
305 if len(candidates) == 1:
306 return candidates[0].rust_target
307 elif not candidates:
308 return None
309
•310 # riscv64 has multiple Rust targets (e.g. riscv64gc, riscv64a23).
311 # If RUST_TARGET is set and exactly matches one of the candidates,
312 # use it to disambiguate.
313 if host_or_target.cpu == "riscv64" and rust_target_env:
314 for c in candidates:
315 if c.rust_target == rust_target_env:
316 return c.rust_target
317
318 # We have multiple candidates. There are two cases where we can try to
319 # narrow further down using extra information from the build system.
320 # - For windows targets, correlate with the C compiler type
321 if host_or_target.kernel == "WINNT":
322 if host_or_target.abi:
323 if host_or_target.abi == "msvc":
324 suffix = "windows-msvc"
325 elif host_or_target.abi == "mingw":
326 suffix = "windows-gnu"
327 elif compiler_info.type in ("gcc", "clang"):
328 suffix = "windows-gnu"
329 else:
330 suffix = "windows-msvc"
331 narrowed = [
332 c for c in candidates if c.rust_target.endswith("-{}".format(suffix))
333 ]
334 if len(narrowed) == 1:
335 return narrowed[0].rust_target
336 elif narrowed:
337 candidates = narrowed
338
339 vendor_aliases = {"pc": ("w64", "windows")}
340 narrowed = [
341 c
342 for c in candidates
343 if host_or_target.vendor in vendor_aliases.get(c.target.vendor, ())
344 ]
345
346 if len(narrowed) == 1:
347 return narrowed[0].rust_target
348
349 # - For arm targets, correlate with arm_target
350 # we could be more thorough with the supported rust targets, but they
351 # don't support OSes that are supported to build Gecko anyways.
352 # Also, sadly, the only interface to check the rust target cpu features
353 # is --print target-spec-json, and it's unstable, so we have to rely on
354 # our own knowledge of what each arm target means.
355 if host_or_target.cpu == "arm" and host_or_target.endianness == "little":
356 prefixes = []
357 if arm_target.arm_arch >= 7:
358 if arm_target.thumb2 and arm_target.fpu == "neon":
359 prefixes.append("thumbv7neon")
360 if arm_target.thumb2:
361 prefixes.append("thumbv7a")
362 prefixes.append("armv7")
363 if arm_target.arm_arch >= 6:
364 prefixes.append("armv6")
365 if host_or_target.os != "Android":
366 # arm-* rust targets are armv6... except arm-linux-androideabi
367 prefixes.append("arm")
368 if arm_target.arm_arch >= 5:
369 prefixes.append("armv5te")
370 if host_or_target.os == "Android":
371 # arm-* rust targets are armv6... except arm-linux-androideabi
372 prefixes.append("arm")
373 if arm_target.arm_arch >= 4:
374 prefixes.append("armv4t")
375 # rust freebsd targets are the only ones that don't have a 'hf' suffix
376 # for hard-float. Technically, that means if the float abi ever is not
377 # hard-float, this will pick a wrong target, but since rust only
378 # supports hard-float, let's assume that means freebsd only support
379 # hard-float.
380 if arm_target.float_abi == "hard" and host_or_target.os != "FreeBSD":
381 suffix = "hf"
382 else:
383 suffix = ""
384 for p in prefixes:
385 for c in candidates:
386 if c.rust_target.startswith(
387 "{}-".format(p)
388 ) and c.rust_target.endswith(suffix):
389 return c.rust_target
390
391 # See if we can narrow down on the exact alias.
392 # We use the sub_configure_alias to keep support mingw32 triplets as input.
393 narrowed = [
394 c
395 for c in candidates
396 if c.target.sub_configure_alias == host_or_target.sub_configure_alias
397 ]
398 if len(narrowed) == 1:
399 return narrowed[0].rust_target
400 elif narrowed:
401 candidates = narrowed
402
403 # See if we can narrow down with the raw OS
404 narrowed = [c for c in candidates if c.target.raw_os == host_or_target.raw_os]
405 if len(narrowed) == 1:
406 return narrowed[0].rust_target
407 elif narrowed:
408 candidates = narrowed
409
410 # The wasm32-wasi target was renamed to wasm32-wasip1
411 if host_or_target.raw_os == "wasi":
412 narrowed = [c for c in candidates if c.target.raw_os == "wasip1"]
413 if len(narrowed) == 1:
414 return narrowed[0].rust_target
415 elif narrowed:
416 candidates = narrowed
417
418 # See if we can narrow down with the raw OS and raw CPU
419 narrowed = [
420 c
421 for c in candidates
422 if c.target.raw_os == host_or_target.raw_os
423 and c.target.raw_cpu == host_or_target.raw_cpu
424 ]
425 if len(narrowed) == 1:
426 return narrowed[0].rust_target
427
428 # Finally, see if the vendor can be used to disambiguate.
429 narrowed = [c for c in candidates if c.target.vendor == host_or_target.vendor]
430 if len(narrowed) == 1:
431 return narrowed[0].rust_target
432
433 return None
434
435 rustc_target = find_candidate(candidates)
436
437 if rustc_target is None:
438 die("Don't know how to translate {} for rustc".format(host_or_target.alias))
439
440 return rustc_target
441
442
443 @imports("os")
444 @imports(_from="textwrap", _import="dedent")
445 @imports(_from="mozbuild.configure.util", _import="LineIO")
446 @imports(_from="__builtin__", _import="open")
447 def assert_rust_compile(host_or_target, rustc_target, rustc):
448 # Check to see whether our rustc has a reasonably functional stdlib
449 # for our chosen target.
450 target_arg = "--target=" + rustc_target
451 with create_temporary_file(suffix=".rs") as in_path, create_temporary_file(
452 suffix=".rlib"
453 ) as out_path:
454 with open(in_path, "w") as fd:
455 source = b'pub extern "C" fn hello() { println!("Hello world"); }'
456 log.debug("Creating `%s` with content:", in_path)
457 with LineIO(lambda l: log.debug("| %s", l)) as out:
458 out.write(source)
459
460 fd.write(source.decode())
461
462 cmd = [
463 rustc,
464 "--crate-type",
465 "staticlib",
466 target_arg,
467 "-o",
468 out_path,
469 in_path,
470 ]
471
472 def failed():
473 die(
474 dedent(
475 """\
476 Cannot compile for {} with {}
477 The target may be unsupported, or you may not have
478 a rust std library for that target installed. Try:
479
480 rustup target add {}
481 """.format(
482 host_or_target.alias, rustc, rustc_target
483 )
484 )
485 )
486
487 check_cmd_output(*cmd, onerror=failed)
488 if not os.path.exists(out_path) or os.path.getsize(out_path) == 0:
489 failed()
490
491
492 @depends(
493 rustc,
494 host,
495 host_c_compiler,
496 rustc_info.host,
497 rust_supported_targets,
498 arm_target,
•499 rust_target_env,
500 when=rust_compiler,
501 )
502 @checking("for rust host triplet")
503 @imports(_from="textwrap", _import="dedent")
504 def rust_host_triple(
•505 rustc, host, compiler_info, rustc_host, rust_supported_targets, arm_target, rust_target_env
506 ):
507 rustc_target = detect_rustc_target(
•508 host, compiler_info, arm_target, rust_supported_targets, rust_target_env=rust_target_env
509 )
510 if rustc_target != rustc_host:
511 if host.alias == rustc_target:
512 configure_host = host.alias
513 else:
514 configure_host = "{}/{}".format(host.alias, rustc_target)
515 die(
516 dedent(
517 """\
518 The rust compiler host ({rustc}) is not suitable for the configure host ({configure}).
519
520 You can solve this by:
521 * Set your configure host to match the rust compiler host by editing your
522 mozconfig and adding "ac_add_options --host={rustc}".
523 * Or, install the rust toolchain for {configure}, if supported, by running
524 "rustup default stable-{rustc_target}"
525 """.format(
526 rustc=rustc_host,
527 configure=configure_host,
528 rustc_target=rustc_target,
529 )
530 )
531 )
532 assert_rust_compile(host, rustc_target, rustc)
533 return rustc_target
534
535
536 @depends(
•537 rustc, target, c_compiler, rust_supported_targets, arm_target, rust_target_env, when=rust_compiler
538 )
539 @checking("for rust target triplet")
540 def rust_target_triple(
•541 rustc, target, compiler_info, rust_supported_targets, arm_target, rust_target_env
542 ):
543 rustc_target = detect_rustc_target(
•544 target, compiler_info, arm_target, rust_supported_targets, rust_target_env=rust_target_env
545 )
546 assert_rust_compile(target, rustc_target, rustc)
547 return rustc_target
548
549
550 set_config("RUST_TARGET", rust_target_triple)
551 set_config("RUST_HOST_TARGET", rust_host_triple)
552
553
554 # This is used for putting source info into symbol files.
555 set_config("RUSTC_COMMIT", depends(rustc_info)(lambda i: i.commit))
556
557 # Rustdoc is required by Rust tests below.
558 option(env="RUSTDOC", nargs=1, help="Path to the rustdoc program")
559
560 rustdoc = check_prog(
561 "RUSTDOC",
562 ["rustdoc"],
563 paths=rust_search_path,
564 input="RUSTDOC",
565 allow_missing=True,
566 )
567
568 option(
569 env="RUSTDOCFLAGS",
570 nargs=1,
571 help="Extra options for the rustdoc program",
572 )
573 set_config("RUSTDOCFLAGS", depends_if("RUSTDOCFLAGS")(lambda flags: flags[0]))
574
575 # This option is separate from --enable-tests because Rust tests are particularly
576 # expensive in terms of compile time (especially for code in libxul).
577 option(
578 "--enable-rust-tests",
579 help="Enable building and running of Rust tests during `make check`",
580 )
581
582
583 @depends("--enable-rust-tests", rustdoc)
584 def rust_tests(enable_rust_tests, rustdoc):
585 if enable_rust_tests and not rustdoc:
586 die("--enable-rust-tests requires rustdoc")
587 return bool(enable_rust_tests)
588
589
590 set_config("MOZ_RUST_TESTS", rust_tests)
591
592
593 @depends(target, c_compiler, rustc)
594 @imports("os")
595 def rustc_natvis_ldflags(target, compiler_info, rustc):
596 if target.kernel == "WINNT" and compiler_info.type == "clang-cl":
597 sysroot = check_cmd_output(rustc, "--print", "sysroot").strip()
598 etc = os.path.join(sysroot, "lib/rustlib/etc")
599 ldflags = []
600 if os.path.isdir(etc):
601 for f in os.listdir(etc):
602 if f.endswith(".natvis"):
603 ldflags.append("-NATVIS:" + normsep(os.path.join(etc, f)))
604 return ldflags
605
606
607 set_config("RUSTC_NATVIS_LDFLAGS", rustc_natvis_ldflags)
608
609
610 option(
611 "--enable-rust-debug",
612 default=depends(when="--enable-debug")(lambda: True),
613 help="{Build|Do not build} Rust code with debug assertions turned on",
614 )
615
616
617 @depends(when="--enable-rust-debug")
618 def debug_rust():
619 return True
620
621
622 set_config("MOZ_DEBUG_RUST", debug_rust)
623 set_define("MOZ_DEBUG_RUST", debug_rust)
624
625 # ==============================================================
626
627 option(env="RUSTFLAGS", nargs=1, help="Rust compiler flags")
628 set_config("RUSTFLAGS", depends("RUSTFLAGS")(lambda flags: flags))
629
630
631 # Rust compiler flags
632 # ==============================================================
633
634
635 @depends(moz_optimize)
636 def rustc_opt_level_default(moz_optimize):
637 return "2" if moz_optimize else "0"
638
639
640 option(
641 env="RUSTC_OPT_LEVEL",
642 default=rustc_opt_level_default,
643 nargs=1,
644 help="Rust compiler optimization level (-C opt-level=%s)",
645 )
646
647
648 @depends("RUSTC_OPT_LEVEL")
649 def rustc_opt_level(opt_level_option):
650 return opt_level_option[0]
651
652
653 set_config("CARGO_PROFILE_RELEASE_OPT_LEVEL", rustc_opt_level)
654 set_config("CARGO_PROFILE_DEV_OPT_LEVEL", rustc_opt_level)
655
656
657 @depends(
658 rustc_opt_level,
659 debug_rust,
660 target,
661 "--enable-debug-symbols",
662 "--enable-frame-pointers",
663 path_remapping,
664 path_remappings,
665 )
666 def rust_compile_flags(
667 opt_level,
668 debug_rust,
669 target,
670 debug_symbols,
671 frame_pointers,
672 path_remapping,
673 path_remappings,
674 ):
675 # Cargo currently supports only two interesting profiles for building:
676 # development and release. Those map (roughly) to --enable-debug and
677 # --disable-debug in Gecko, respectively.
678 #
679 # But we'd also like to support an additional axis of control for
680 # optimization level. Since Cargo only supports 2 profiles, we're in
681 # a bit of a bind.
682 #
683 # Code here derives various compiler options given other configure options.
684 # The options defined here effectively override defaults specified in
685 # Cargo.toml files.
686
687 debug_assertions = None
688 debug_info = None
689
690 # opt-level=0 implies -C debug-assertions, which may not be desired
691 # unless Rust debugging is enabled.
692 if opt_level == "0" and not debug_rust:
693 debug_assertions = False
694
695 if debug_symbols:
696 debug_info = "2"
697
698 opts = []
699
700 if debug_assertions is not None:
701 opts.append("debug-assertions=%s" % ("yes" if debug_assertions else "no"))
702 if debug_info is not None:
703 opts.append("debuginfo=%s" % debug_info)
704 if frame_pointers:
705 opts.append("force-frame-pointers=yes")
706 # CFG for arm64 is crashy, see `def security_hardening_cflags`.
707 if target.kernel == "WINNT" and target.cpu != "aarch64":
708 opts.append("control-flow-guard=yes")
709
710 flags = []
711 for opt in opts:
712 flags.extend(["-C", opt])
713
714 if "rust" in path_remapping:
715 # rustc has supported --remap-path-prefix since version 1.26, well
716 # before our required minimum Rust version, so there's no need to
717 # feature-detect or gate on versions.
718 for old, new in path_remappings:
719 flags.append(f"--remap-path-prefix={old}={new}")
720
721 return flags
722
723
724 # Rust incremental compilation
725 # ==============================================================
726
727
728 option("--disable-cargo-incremental", help="Disable incremental rust compilation")
729
730
731 @depends(
732 developer_options,
733 debug_rust,
734 moz_automation,
735 code_coverage,
736 "--disable-cargo-incremental",
737 using_sccache,
738 "RUSTC_WRAPPER",
739 )
740 @imports("os")
741 def cargo_incremental(
742 developer_options,
743 debug_rust,
744 automation,
745 code_coverage,
746 enabled,
747 using_sccache,
748 rustc_wrapper,
749 ):
750 """Return a value for the CARGO_INCREMENTAL environment variable."""
751
752 if not enabled:
753 return "0"
754 elif enabled.origin != "default":
755 return "1"
756
757 # We never want to use incremental compilation in automation. sccache
758 # handles our automation use case much better than incremental compilation
759 # would.
760 if automation:
761 return "0"
762
763 # Coverage instrumentation doesn't play well with incremental compilation
764 # https://github.com/rust-lang/rust/issues/50203.
765 if code_coverage:
766 return "0"
767
768 # Incremental compilation doesn't work as well as it should, and if we're
769 # using sccache, it's better to use sccache than incremental compilation.
770 if not using_sccache and rustc_wrapper:
771 rustc_wrapper = os.path.basename(rustc_wrapper[0])
772 if os.path.splitext(rustc_wrapper)[0].lower() == "sccache":
773 using_sccache = True
774 if using_sccache:
775 return "0"
776
777 # Incremental compilation is automatically turned on for debug builds, so
778 # we don't need to do anything special here.
779 if debug_rust:
780 return
781
782 # Don't enable on --enable-release builds, because of the runtime
783 # performance cost.
784 if not developer_options:
785 return
786
787 # We're clear to use incremental compilation!
788 return "1"
789
790
791 set_config("CARGO_INCREMENTAL", cargo_incremental)
792
793
794 @depends(rust_compile_flags, "--enable-warnings-as-errors")
795 def rust_flags(compile_flags, warnings_as_errors):
796 warning_flags = []
797
798 # Note that cargo passes --cap-lints warn to rustc for third-party code, so
799 # we don't need a very complicated setup.
800 if warnings_as_errors:
801 warning_flags.append("-Dwarnings")
802 else:
803 warning_flags.extend(("--cap-lints", "warn"))
804
805 return compile_flags + warning_flags
806
807
808 set_config("MOZ_RUST_DEFAULT_FLAGS", rust_flags)