Bug 1855360 - Fix the skip-if syntax. a=bustage-fix
[gecko.git] / config / makefiles / rust.mk
blob9e3d5256b91ac7c075d9442489768d0ab4137dd1
1 # This Source Code Form is subject to the terms of the Mozilla Public
2 # License, v. 2.0. If a copy of the MPL was not distributed with this file,
3 # You can obtain one at http://mozilla.org/MPL/2.0/.
5 # /!\ In this file, we export multiple variables globally via make rather than
6 # in recipes via the `env` command to avoid round-trips to msys on Windows, which
7 # tend to break environment variable values in interesting ways.
9 # /!\ Avoid the use of double-quotes in this file, so that the cargo
10 # commands can be executed directly by make, without doing a round-trip
11 # through a shell.
13 cargo_host_flag := --target=$(RUST_HOST_TARGET)
14 cargo_target_flag := --target=$(RUST_TARGET)
16 # Permit users to pass flags to cargo from their mozconfigs (e.g. --color=always).
17 cargo_build_flags = $(CARGOFLAGS)
18 ifndef MOZ_DEBUG_RUST
19 cargo_build_flags += --release
20 endif
22 # The Spidermonkey library can be built from a package tarball outside the
23 # tree, so we want to let Cargo create lock files in this case. When built
24 # within a tree, the Rust dependencies have been vendored in so Cargo won't
25 # touch the lock file.
26 ifndef JS_STANDALONE
27 cargo_build_flags += --frozen
28 endif
30 cargo_build_flags += --manifest-path $(CARGO_FILE)
31 ifdef BUILD_VERBOSE_LOG
32 cargo_build_flags += -vv
33 endif
35 ifneq (,$(USE_CARGO_JSON_MESSAGE_FORMAT))
36 cargo_build_flags += --message-format=json
37 endif
39 # Enable color output if original stdout was a TTY and color settings
40 # aren't already present. This essentially restores the default behavior
41 # of cargo when running via `mach`.
42 ifdef MACH_STDOUT_ISATTY
43 ifeq (,$(findstring --color,$(cargo_build_flags)))
44 ifdef NO_ANSI
45 cargo_build_flags += --color=never
46 else
47 cargo_build_flags += --color=always
48 endif
49 endif
50 endif
52 # Without -j > 1, make will not pass jobserver info down to cargo. Force
53 # one job when requested as a special case.
54 cargo_build_flags += $(filter -j1,$(MAKEFLAGS))
56 # We also need to rebuild the rust stdlib so that it's instrumented. Because
57 # build-std is still pretty experimental, we need to explicitly request
58 # the panic_abort crate for `panic = "abort"` support.
59 ifdef MOZ_TSAN
60 cargo_build_flags += -Zbuild-std=std,panic_abort
61 RUSTFLAGS += -Zsanitizer=thread
62 endif
64 rustflags_sancov =
65 ifdef LIBFUZZER
66 ifndef MOZ_TSAN
67 ifndef FUZZING_JS_FUZZILLI
68 # These options should match what is implicitly enabled for `clang -fsanitize=fuzzer`
69 # here: https://github.com/llvm/llvm-project/blob/release/13.x/clang/lib/Driver/SanitizerArgs.cpp#L422
71 # -sanitizer-coverage-inline-8bit-counters Increments 8-bit counter for every edge.
72 # -sanitizer-coverage-level=4 Enable coverage for all blocks, critical edges, and indirect calls.
73 # -sanitizer-coverage-trace-compares Tracing of CMP and similar instructions.
74 # -sanitizer-coverage-pc-table Create a static PC table.
76 # In TSan builds, we must not pass any of these, because sanitizer coverage is incompatible with TSan.
77 rustflags_sancov += -Cpasses=sancov-module -Cllvm-args=-sanitizer-coverage-inline-8bit-counters -Cllvm-args=-sanitizer-coverage-level=4 -Cllvm-args=-sanitizer-coverage-trace-compares -Cllvm-args=-sanitizer-coverage-pc-table
78 endif
79 endif
80 endif
82 # These flags are passed via `cargo rustc` and only apply to the final rustc
83 # invocation (i.e., only the top-level crate, not its dependencies).
84 cargo_rustc_flags = $(CARGO_RUSTCFLAGS)
85 ifndef DEVELOPER_OPTIONS
86 ifndef MOZ_DEBUG_RUST
87 # Enable link-time optimization for release builds, but not when linking
88 # gkrust_gtest. And not when doing cross-language LTO.
89 ifndef MOZ_LTO_RUST_CROSS
90 # Never enable when sancov is enabled to work around https://github.com/rust-lang/rust/issues/90300.
91 ifndef rustflags_sancov
92 # Never enable when coverage is enabled to work around https://github.com/rust-lang/rust/issues/90045.
93 ifndef MOZ_CODE_COVERAGE
94 ifeq (,$(findstring gkrust_gtest,$(RUST_LIBRARY_FILE)))
95 cargo_rustc_flags += -Clto$(if $(filter full,$(MOZ_LTO_RUST_CROSS)),=fat)
96 endif
97 # We need -Cembed-bitcode=yes for all crates when using -Clto.
98 RUSTFLAGS += -Cembed-bitcode=yes
99 endif
100 endif
101 endif
102 endif
103 endif
105 ifdef CARGO_INCREMENTAL
106 export CARGO_INCREMENTAL
107 endif
109 rustflags_neon =
110 ifeq (neon,$(MOZ_FPU))
111 ifneq (,$(filter thumbv7neon-,$(RUST_TARGET)))
112 # Enable neon and disable restriction to 16 FPU registers when neon is enabled
113 # but we're not using a thumbv7neon target, where it's already the default.
114 # (CPUs with neon have 32 FPU registers available)
115 rustflags_neon += -C target_feature=+neon,-d16
116 endif
117 endif
119 rustflags_override = $(MOZ_RUST_DEFAULT_FLAGS) $(rustflags_neon)
121 ifdef DEVELOPER_OPTIONS
122 # By default the Rust compiler will perform a limited kind of ThinLTO on each
123 # crate. For local builds this additional optimization is not worth the
124 # increase in compile time so we opt out of it.
125 rustflags_override += -Clto=off
126 endif
128 ifdef MOZ_USING_SCCACHE
129 export RUSTC_WRAPPER=$(CCACHE)
130 endif
132 ifndef CROSS_COMPILE
133 ifdef MOZ_TSAN
134 PASS_ONLY_BASE_CFLAGS_TO_RUST=1
135 else
136 ifneq (,$(MOZ_ASAN)$(MOZ_UBSAN))
137 ifneq ($(OS_ARCH), Linux)
138 PASS_ONLY_BASE_CFLAGS_TO_RUST=1
139 endif # !Linux
140 endif # MOZ_ASAN || MOZ_UBSAN
141 endif # MOZ_TSAN
142 endif # !CROSS_COMPILE
144 ifeq (WINNT,$(HOST_OS_ARCH))
145 ifdef MOZ_CODE_COVERAGE
146 PASS_ONLY_BASE_CFLAGS_TO_RUST=1
147 endif # MOZ_CODE_COVERAGE
148 endif # WINNT
150 ifeq (WINNT,$(HOST_OS_ARCH))
151 normalize_sep = $(subst \,/,$(1))
152 else
153 normalize_sep = $(1)
154 endif
156 # We start with host variables because the rust host and the rust target might be the same,
157 # in which case we want the latter to take priority.
159 # We're passing these for consumption by the `cc` crate, which doesn't use the same
160 # convention as cargo itself:
161 # https://github.com/alexcrichton/cc-rs/blob/baa71c0e298d9ad7ac30f0ad78f20b4b3b3a8fb2/src/lib.rs#L1715
162 rust_host_cc_env_name := $(subst -,_,$(RUST_HOST_TARGET))
164 # HOST_CC/HOST_CXX/CC/CXX usually contain base flags for e.g. the build target.
165 # We want to pass those through CFLAGS_*/CXXFLAGS_* instead, so that they end up
166 # after whatever cc-rs adds to the compiler command line, so that they win.
167 # Ideally, we'd use CRATE_CC_NO_DEFAULTS=1, but that causes other problems at the
168 # moment.
169 export CC_$(rust_host_cc_env_name)=$(filter-out $(HOST_CC_BASE_FLAGS),$(HOST_CC))
170 export CXX_$(rust_host_cc_env_name)=$(filter-out $(HOST_CXX_BASE_FLAGS),$(HOST_CXX))
171 export AR_$(rust_host_cc_env_name)=$(HOST_AR)
173 rust_cc_env_name := $(subst -,_,$(RUST_TARGET))
175 export CC_$(rust_cc_env_name)=$(filter-out $(CC_BASE_FLAGS),$(CC))
176 export CXX_$(rust_cc_env_name)=$(filter-out $(CXX_BASE_FLAGS),$(CXX))
177 export AR_$(rust_cc_env_name)=$(AR)
179 ifeq (WINNT,$(HOST_OS_ARCH))
180 HOST_CC_BASE_FLAGS += -DUNICODE
181 HOST_CXX_BASE_FLAGS += -DUNICODE
182 endif
183 ifeq (WINNT,$(OS_ARCH))
184 CC_BASE_FLAGS += -DUNICODE
185 CXX_BASE_FLAGS += -DUNICODE
186 endif
188 ifneq (1,$(PASS_ONLY_BASE_CFLAGS_TO_RUST))
189 # -DMOZILLA_CONFIG_H is added to prevent mozilla-config.h from injecting anything
190 # in C/C++ compiles from rust. That's not needed in the other branch because the
191 # base flags don't force-include mozilla-config.h.
192 export CFLAGS_$(rust_host_cc_env_name)=$(HOST_CC_BASE_FLAGS) $(COMPUTED_HOST_CFLAGS) -DMOZILLA_CONFIG_H
193 export CXXFLAGS_$(rust_host_cc_env_name)=$(HOST_CXX_BASE_FLAGS) $(COMPUTED_HOST_CXXFLAGS) -DMOZILLA_CONFIG_H
194 export CFLAGS_$(rust_cc_env_name)=$(CC_BASE_FLAGS) $(COMPUTED_CFLAGS) -DMOZILLA_CONFIG_H
195 export CXXFLAGS_$(rust_cc_env_name)=$(CXX_BASE_FLAGS) $(COMPUTED_CXXFLAGS) -DMOZILLA_CONFIG_H
196 else
197 # Because cargo doesn't allow to distinguish builds happening for build
198 # scripts/procedural macros vs. those happening for the rust target,
199 # we can't blindly pass all our flags down for cc-rs to use them, because of the
200 # side effects they can have on what otherwise should be host builds.
201 # So for sanitizer and coverage builds, we only pass the base compiler flags.
202 # This means C code built by rust is not going to be covered by sanitizers
203 # and coverage. But at least we control what compiler is being used,
204 # rather than relying on cc-rs guesses, which, sometimes fail us.
205 export CFLAGS_$(rust_host_cc_env_name)=$(HOST_CC_BASE_FLAGS)
206 export CXXFLAGS_$(rust_host_cc_env_name)=$(HOST_CXX_BASE_FLAGS)
207 export CFLAGS_$(rust_cc_env_name)=$(CC_BASE_FLAGS)
208 export CXXFLAGS_$(rust_cc_env_name)=$(CXX_BASE_FLAGS)
209 endif
211 # When host == target, cargo will compile build scripts with sanitizers enabled
212 # if sanitizers are enabled, which may randomly fail when they execute
213 # because of https://github.com/google/sanitizers/issues/1322.
214 # Work around by disabling __tls_get_addr interception (bug 1635327).
215 ifeq ($(RUST_TARGET),$(RUST_HOST_TARGET))
216 define sanitizer_options
217 ifdef MOZ_$1
218 export $1_OPTIONS:=$$($1_OPTIONS:%=%:)intercept_tls_get_addr=0
219 endif
220 endef
221 $(foreach san,ASAN TSAN UBSAN,$(eval $(call sanitizer_options,$(san))))
222 endif
224 # Force the target down to all bindgen callers, even those that may not
225 # read BINDGEN_SYSTEM_FLAGS some way or another.
226 export BINDGEN_EXTRA_CLANG_ARGS:=$(filter --target=%,$(BINDGEN_SYSTEM_FLAGS))
227 export CARGO_TARGET_DIR
228 export RUSTFLAGS
229 export RUSTC
230 export RUSTDOC
231 export RUSTFMT
232 export LIBCLANG_PATH=$(MOZ_LIBCLANG_PATH)
233 export CLANG_PATH=$(MOZ_CLANG_PATH)
234 export PKG_CONFIG
235 export PKG_CONFIG_ALLOW_CROSS=1
236 export PKG_CONFIG_PATH
237 ifneq (,$(PKG_CONFIG_SYSROOT_DIR))
238 export PKG_CONFIG_SYSROOT_DIR
239 endif
240 ifneq (,$(PKG_CONFIG_LIBDIR))
241 export PKG_CONFIG_LIBDIR
242 endif
243 export RUST_BACKTRACE=full
244 export MOZ_TOPOBJDIR=$(topobjdir)
245 export MOZ_FOLD_LIBS
246 export PYTHON3
247 export CARGO_PROFILE_RELEASE_OPT_LEVEL
248 export CARGO_PROFILE_DEV_OPT_LEVEL
250 # Set COREAUDIO_SDK_PATH for third_party/rust/coreaudio-sys/build.rs
251 ifeq ($(OS_ARCH), Darwin)
252 ifdef MACOS_SDK_DIR
253 export COREAUDIO_SDK_PATH=$(MACOS_SDK_DIR)
254 endif
255 endif
257 ifndef RUSTC_BOOTSTRAP
258 RUSTC_BOOTSTRAP := mozglue_static,qcms
259 ifdef MOZ_RUST_SIMD
260 RUSTC_BOOTSTRAP := $(RUSTC_BOOTSTRAP),encoding_rs,packed_simd
261 endif
262 export RUSTC_BOOTSTRAP
263 endif
265 target_rust_ltoable := force-cargo-library-build $(ADD_RUST_LTOABLE)
266 target_rust_nonltoable := force-cargo-test-run force-cargo-program-build
268 ifdef MOZ_PGO_RUST
269 ifdef MOZ_PROFILE_GENERATE
270 rust_pgo_flags := -C profile-generate=$(topobjdir)
271 ifeq (1,$(words $(filter 5.% 6.% 7.% 8.% 9.% 10.% 11.%,$(CC_VERSION) $(RUSTC_LLVM_VERSION))))
272 # Disable value profiling when:
273 # (RUSTC_LLVM_VERSION < 12 and CC_VERSION >= 12) or (RUSTC_LLVM_VERSION >= 12 and CC_VERSION < 12)
274 rust_pgo_flags += -C llvm-args=--disable-vp=true
275 endif
276 # The C compiler may be passed extra llvm flags for PGO that we also want to pass to rust as well.
277 # In PROFILE_GEN_CFLAGS, they look like "-mllvm foo", and we want "-C llvm-args=foo", so first turn
278 # "-mllvm foo" into "-mllvm:foo" so that it becomes a unique argument, that we can then filter for,
279 # excluding other flags, and then turn into the right string.
280 rust_pgo_flags += $(patsubst -mllvm:%,-C llvm-args=%,$(filter -mllvm:%,$(subst -mllvm ,-mllvm:,$(PROFILE_GEN_CFLAGS))))
281 else # MOZ_PROFILE_USE
282 rust_pgo_flags := -C profile-use=$(PGO_PROFILE_PATH)
283 endif
284 endif
286 # Work around https://github.com/rust-lang/rust/issues/112480
287 ifdef MOZ_DEBUG_RUST
288 ifneq (,$(filter i686-pc-windows-%,$(RUST_TARGET)))
289 RUSTFLAGS += -Zmir-enable-passes=-CheckAlignment
290 RUSTC_BOOTSTRAP := 1
291 endif
292 endif
294 $(target_rust_ltoable): RUSTFLAGS:=$(rustflags_override) $(rustflags_sancov) $(RUSTFLAGS) $(rust_pgo_flags) \
295 $(if $(MOZ_LTO_RUST_CROSS),\
296 -Clinker-plugin-lto \
298 $(target_rust_nonltoable): RUSTFLAGS:=$(rustflags_override) $(rustflags_sancov) $(RUSTFLAGS)
300 TARGET_RECIPES := $(target_rust_ltoable) $(target_rust_nonltoable)
302 HOST_RECIPES := \
303 $(foreach a,library program,$(foreach b,build check udeps clippy,force-cargo-host-$(a)-$(b)))
305 $(HOST_RECIPES): RUSTFLAGS:=$(rustflags_override)
307 # If this is a release build we want rustc to generate one codegen unit per
308 # crate. This results in better optimization and less code duplication at the
309 # cost of longer compile times.
310 ifndef DEVELOPER_OPTIONS
311 $(TARGET_RECIPES) $(HOST_RECIPES): RUSTFLAGS += -C codegen-units=1
312 endif
314 # We use the + prefix to pass down the jobserver fds to cargo, but we
315 # don't use the prefix when make -n is used, so that cargo doesn't run
316 # in that case)
317 define RUN_CARGO_INNER
318 $(if $(findstring n,$(filter-out --%, $(MAKEFLAGS))),,+)$(CARGO) $(1) $(cargo_build_flags) $(CARGO_EXTRA_FLAGS) $(cargo_extra_cli_flags)
319 endef
321 ifdef CARGO_CONTINUE_ON_ERROR
322 define RUN_CARGO
323 -$(RUN_CARGO_INNER)
324 endef
325 else
326 define RUN_CARGO
327 $(RUN_CARGO_INNER)
328 endef
329 endif
331 # This function is intended to be called by:
333 # $(call CARGO_BUILD,EXTRA_ENV_VAR1=X EXTRA_ENV_VAR2=Y ...)
335 # but, given the idiosyncracies of make, can also be called without arguments:
337 # $(call CARGO_BUILD)
338 define CARGO_BUILD
339 $(call RUN_CARGO,rustc$(if $(BUILDSTATUS), --timings))
340 endef
342 cargo_host_linker_env_var := CARGO_TARGET_$(call varize,$(RUST_HOST_TARGET))_LINKER
343 cargo_linker_env_var := CARGO_TARGET_$(call varize,$(RUST_TARGET))_LINKER
345 export MOZ_CLANG_NEWER_THAN_RUSTC_LLVM
346 export MOZ_CARGO_WRAP_LDFLAGS
347 export MOZ_CARGO_WRAP_LD
348 export MOZ_CARGO_WRAP_LD_CXX
349 export MOZ_CARGO_WRAP_HOST_LDFLAGS
350 export MOZ_CARGO_WRAP_HOST_LD
351 export MOZ_CARGO_WRAP_HOST_LD_CXX
352 # Exporting from make always exports a value. Setting a value per-recipe
353 # would export an empty value for the host recipes. When not doing a
354 # cross-compile, the --target for those is the same, and cargo will use
355 # CARGO_TARGET_*_LINKER for its linker, so we always pass the
356 # cargo-linker wrapper, and fill MOZ_CARGO_WRAP_{HOST_,}LD* more or less
357 # appropriately for all recipes.
358 ifeq (WINNT,$(HOST_OS_ARCH))
359 # Use .bat wrapping on Windows hosts, and shell wrapping on other hosts.
360 # Like for CC/C*FLAGS, we want the target values to trump the host values when
361 # both variables are the same.
362 export $(cargo_host_linker_env_var):=$(topsrcdir)/build/cargo-host-linker.bat
363 export $(cargo_linker_env_var):=$(topsrcdir)/build/cargo-linker.bat
364 WRAP_HOST_LINKER_LIBPATHS:=$(HOST_LINKER_LIBPATHS_BAT)
365 else
366 export $(cargo_host_linker_env_var):=$(topsrcdir)/build/cargo-host-linker
367 export $(cargo_linker_env_var):=$(topsrcdir)/build/cargo-linker
368 WRAP_HOST_LINKER_LIBPATHS:=$(HOST_LINKER_LIBPATHS)
369 endif
371 # Cargo needs the same linker flags as the C/C++ compiler,
372 # but not the final libraries. Filter those out because they
373 # cause problems on macOS 10.7; see bug 1365993 for details.
374 # Also, we don't want to pass PGO flags until cargo supports them.
375 # Finally, we also remove the -Wl,--build-id=uuid flag when it's in
376 # the LDFLAGS. The flag was chosen over the default (build-id=sha1)
377 # in developer builds, because for libxul, it's faster. But it's also
378 # non-deterministic. So when the rust compiler produces procedural
379 # macros as libraries, they're not reproducible. Those procedural
380 # macros then end up as dependencies of other crates, and their
381 # non-reproducibility leads to sccache transitively having cache
382 # misses.
383 $(TARGET_RECIPES): MOZ_CARGO_WRAP_LDFLAGS:=$(filter-out -fsanitize=cfi% -framework Cocoa -lobjc AudioToolbox ExceptionHandling -fprofile-% -Wl$(COMMA)--build-id=uuid,$(LDFLAGS))
385 # When building with sanitizer, rustc links its own runtime, which conflicts
386 # with the one that passing -fsanitize=* to the linker would add.
387 # Ideally, we'd always do this filtering, but because the flags may also apply
388 # to build scripts because cargo doesn't allow the distinction, we only filter
389 # when building programs, except when using thread sanitizer where we filter
390 # everywhere.
391 ifneq (,$(filter -Zsanitizer=%,$(RUSTFLAGS)))
392 $(if $(filter -Zsanitizer=thread,$(RUSTFLAGS)),$(TARGET_RECIPES),force-cargo-program-build): MOZ_CARGO_WRAP_LDFLAGS:=$(filter-out -fsanitize=%,$(MOZ_CARGO_WRAP_LDFLAGS))
393 endif
395 # Rustc assumes that *-windows-gnu targets build with mingw-gcc and manually
396 # add runtime libraries that don't exist with mingw-clang. We created dummy
397 # libraries in $(topobjdir)/build/win32, but that's not enough, because some
398 # of the wanted symbols that come from these libraries are available in a
399 # different library, that we add manually. We also need to avoid rustc
400 # passing -nodefaultlibs to clang so that it adds clang_rt.
401 ifeq (WINNT_clang,$(OS_ARCH)_$(CC_TYPE))
402 force-cargo-program-build: MOZ_CARGO_WRAP_LDFLAGS+=-L$(topobjdir)/build/win32 -lunwind
403 force-cargo-program-build: CARGO_RUSTCFLAGS += -C default-linker-libraries=yes
404 endif
406 # Rustc passes -nodefaultlibs to the linker (clang) on mac, which prevents
407 # clang from adding the necessary sanitizer runtimes when building with
408 # C/C++ sanitizer but without rust sanitizer.
409 ifeq (Darwin,$(OS_ARCH))
410 ifeq (,$(filter -Zsanitizer=%,$(RUSTFLAGS)))
411 ifneq (,$(filter -fsanitize=%,$(LDFLAGS)))
412 force-cargo-program-build: CARGO_RUSTCFLAGS += -C default-linker-libraries=yes
413 endif
414 endif
415 endif
417 $(HOST_RECIPES): MOZ_CARGO_WRAP_LDFLAGS:=$(HOST_LDFLAGS) $(WRAP_HOST_LINKER_LIBPATHS)
418 $(TARGET_RECIPES) $(HOST_RECIPES): MOZ_CARGO_WRAP_HOST_LDFLAGS:=$(HOST_LDFLAGS) $(WRAP_HOST_LINKER_LIBPATHS)
420 ifeq (,$(filter clang-cl,$(CC_TYPE)))
421 $(TARGET_RECIPES): MOZ_CARGO_WRAP_LD:=$(CC)
422 $(TARGET_RECIPES): MOZ_CARGO_WRAP_LD_CXX:=$(CXX)
423 else
424 $(TARGET_RECIPES): MOZ_CARGO_WRAP_LD:=$(LINKER)
425 $(TARGET_RECIPES): MOZ_CARGO_WRAP_LD_CXX:=$(LINKER)
426 endif
428 ifeq (,$(filter clang-cl,$(HOST_CC_TYPE)))
429 $(HOST_RECIPES): MOZ_CARGO_WRAP_LD:=$(HOST_CC)
430 $(HOST_RECIPES): MOZ_CARGO_WRAP_LD_CXX:=$(HOST_CXX)
431 $(TARGET_RECIPES) $(HOST_RECIPES): MOZ_CARGO_WRAP_HOST_LD:=$(HOST_CC)
432 $(TARGET_RECIPES) $(HOST_RECIPES): MOZ_CARGO_WRAP_HOST_LD_CXX:=$(HOST_CXX)
433 else
434 $(HOST_RECIPES): MOZ_CARGO_WRAP_LD:=$(HOST_LINKER)
435 $(HOST_RECIPES): MOZ_CARGO_WRAP_LD_CXX:=$(HOST_LINKER)
436 $(TARGET_RECIPES) $(HOST_RECIPES): MOZ_CARGO_WRAP_HOST_LD:=$(HOST_LINKER)
437 $(TARGET_RECIPES) $(HOST_RECIPES): MOZ_CARGO_WRAP_HOST_LD_CXX:=$(HOST_LINKER)
438 endif
440 define make_default_rule
441 $(1):
443 endef
445 # make_cargo_rule(target, real-target [, extra-deps])
446 # Generates a rule suitable to rebuild $(target) only if its dependencies are
447 # obsolete.
448 # It relies on the fact that upon build, cargo generates a dependency file named
449 # `$(target).d'. Unfortunately the lhs of the rule has an absolute path,
450 # so we extract it under the name $(target)_deps below.
452 # If the dependencies are empty, the file was not created so we force a rebuild.
453 # Otherwise we add it to the dependency list.
455 # The actual rule is a bit tricky. The `+' prefix allow for recursive parallel
456 # make, and it's skipped (`:') if we already triggered a rebuild as part of the
457 # dependency chain.
459 # Another tricky thing: some dependencies may contain escaped spaces, and they
460 # need to be preserved, but $(foreach) splits on spaces, so we replace escaped
461 # spaces with some unlikely string for the foreach, and replace them back in the
462 # loop itself.
463 define make_cargo_rule
464 $(notdir $(1))_deps := $$(wordlist 2, 10000000, $$(if $$(wildcard $(basename $(1)).d),$$(shell cat $(basename $(1)).d)))
465 $(1): $(CARGO_FILE) $(3) $(topsrcdir)/Cargo.lock $$(if $$($(notdir $(1))_deps),$$($(notdir $(1))_deps),$(2))
466 $$(REPORT_BUILD)
467 $$(if $$($(notdir $(1))_deps),+$(MAKE) $(2),:)
469 $$(foreach dep, $$(call normalize_sep,$$(subst \ ,_^_^_^_,$$($(notdir $(1))_deps))),$$(eval $$(call make_default_rule,$$(subst _^_^_^_,\ ,$$(dep)))))
470 endef
472 ifdef RUST_LIBRARY_FILE
474 rust_features_flag := --features '$(if $(RUST_LIBRARY_FEATURES),$(RUST_LIBRARY_FEATURES) )mozilla-central-workspace-hack'
476 ifeq (WASI,$(OS_ARCH))
477 # The rust wasi target defaults to statically link the wasi crt, but when we
478 # build static libraries from rust and link them with C/C++ code, we also link
479 # a wasi crt, which may conflict with rust's.
480 force-cargo-library-build: CARGO_RUSTCFLAGS += -C target-feature=-crt-static
481 endif
483 # Assume any system libraries rustc links against are already in the target's LIBS.
485 # We need to run cargo unconditionally, because cargo is the only thing that
486 # has full visibility into how changes in Rust sources might affect the final
487 # build.
488 force-cargo-library-build:
489 $(call BUILDSTATUS,START_Rust $(notdir $(RUST_LIBRARY_FILE)))
490 $(call CARGO_BUILD) --lib $(cargo_target_flag) $(rust_features_flag) -- $(cargo_rustc_flags)
491 $(call BUILDSTATUS,END_Rust $(notdir $(RUST_LIBRARY_FILE)))
492 # When we are building in --enable-release mode; we add an additional check to confirm
493 # that we are not importing any networking-related functions in rust code. This reduces
494 # the chance of proxy bypasses originating from rust code.
495 # The check only works when rust code is built with -Clto but without MOZ_LTO_RUST_CROSS.
496 # Sanitizers and sancov also fail because compiler-rt hooks network functions.
497 ifndef MOZ_PROFILE_GENERATE
498 ifeq ($(OS_ARCH), Linux)
499 ifeq (,$(rustflags_sancov)$(MOZ_ASAN)$(MOZ_TSAN)$(MOZ_UBSAN))
500 ifndef MOZ_LTO_RUST_CROSS
501 ifneq (,$(filter -Clto,$(cargo_rustc_flags)))
502 $(call py_action,check_binary $(@F),--networking $(RUST_LIBRARY_FILE))
503 endif
504 endif
505 endif
506 endif
507 endif
509 $(eval $(call make_cargo_rule,$(RUST_LIBRARY_FILE),force-cargo-library-build))
511 SUGGEST_INSTALL_ON_FAILURE = (ret=$$?; if [ $$ret = 101 ]; then echo If $1 is not installed, install it using: cargo install $1; fi; exit $$ret)
513 ifndef CARGO_NO_AUTO_ARG
514 force-cargo-library-%:
515 $(call RUN_CARGO,$*) --lib $(cargo_target_flag) $(rust_features_flag) || $(call SUGGEST_INSTALL_ON_FAILURE,cargo-$*)
516 else
517 force-cargo-library-%:
518 $(call RUN_CARGO,$*) || $(call SUGGEST_INSTALL_ON_FAILURE,cargo-$*)
519 endif
521 else
522 force-cargo-library-%:
523 @true
525 endif # RUST_LIBRARY_FILE
527 ifdef RUST_TESTS
529 rust_test_options := $(foreach test,$(RUST_TESTS),-p $(test))
531 rust_test_features_flag := --features '$(if $(RUST_TEST_FEATURES),$(RUST_TEST_FEATURES) )mozilla-central-workspace-hack'
533 # Don't stop at the first failure. We want to list all failures together.
534 rust_test_flag := --no-fail-fast
536 force-cargo-test-run:
537 $(call RUN_CARGO,test $(cargo_target_flag) $(rust_test_flag) $(rust_test_options) $(rust_test_features_flag))
539 endif # RUST_TESTS
541 ifdef HOST_RUST_LIBRARY_FILE
543 host_rust_features_flag := --features '$(if $(HOST_RUST_LIBRARY_FEATURES),$(HOST_RUST_LIBRARY_FEATURES) )mozilla-central-workspace-hack'
545 force-cargo-host-library-build:
546 $(call BUILDSTATUS,START_Rust $(notdir $(HOST_RUST_LIBRARY_FILE)))
547 $(call CARGO_BUILD) --lib $(cargo_host_flag) $(host_rust_features_flag)
548 $(call BUILDSTATUS,END_Rust $(notdir $(HOST_RUST_LIBRARY_FILE)))
550 $(eval $(call make_cargo_rule,$(HOST_RUST_LIBRARY_FILE),force-cargo-host-library-build))
552 ifndef CARGO_NO_AUTO_ARG
553 force-cargo-host-library-%:
554 $(call RUN_CARGO,$*) --lib $(cargo_host_flag) $(host_rust_features_flag)
555 else
556 force-cargo-host-library-%:
557 $(call RUN_CARGO,$*) --lib $(filter-out --release $(cargo_host_flag)) $(host_rust_features_flag)
558 endif
560 else
561 force-cargo-host-library-%:
562 @true
563 endif # HOST_RUST_LIBRARY_FILE
565 ifdef RUST_PROGRAMS
567 program_features_flag := --features mozilla-central-workspace-hack
569 force-cargo-program-build: $(call resfile,module)
570 $(call BUILDSTATUS,START_Rust $(RUST_CARGO_PROGRAMS))
571 $(call CARGO_BUILD) $(addprefix --bin ,$(RUST_CARGO_PROGRAMS)) $(cargo_target_flag) $(program_features_flag) -- $(addprefix -C link-arg=$(CURDIR)/,$(call resfile,module)) $(CARGO_RUSTCFLAGS)
572 $(call BUILDSTATUS,END_Rust $(RUST_CARGO_PROGRAMS))
574 $(foreach RUST_PROGRAM,$(RUST_PROGRAMS), $(eval $(call make_cargo_rule,$(RUST_PROGRAM),force-cargo-program-build,$(call resfile,module))))
576 ifndef CARGO_NO_AUTO_ARG
577 force-cargo-program-%:
578 $(call RUN_CARGO,$*) $(addprefix --bin ,$(RUST_CARGO_PROGRAMS)) $(cargo_target_flag) $(program_features_flag)
579 else
580 force-cargo-program-%:
581 $(call RUN_CARGO,$*)
582 endif
584 else
585 force-cargo-program-%:
586 @true
587 endif # RUST_PROGRAMS
588 ifdef HOST_RUST_PROGRAMS
590 host_program_features_flag := --features mozilla-central-workspace-hack
592 force-cargo-host-program-build:
593 $(call BUILDSTATUS,START_Rust $(HOST_RUST_CARGO_PROGRAMS))
594 $(call CARGO_BUILD) $(addprefix --bin ,$(HOST_RUST_CARGO_PROGRAMS)) $(cargo_host_flag) $(host_program_features_flag)
595 $(call BUILDSTATUS,END_Rust $(HOST_RUST_CARGO_PROGRAMS))
597 $(foreach HOST_RUST_PROGRAM,$(HOST_RUST_PROGRAMS), $(eval $(call make_cargo_rule,$(HOST_RUST_PROGRAM),force-cargo-host-program-build)))
599 ifndef CARGO_NO_AUTO_ARG
600 force-cargo-host-program-%:
601 $(call BUILDSTATUS,START_Rust $(HOST_RUST_CARGO_PROGRAMS))
602 $(call RUN_CARGO,$*) $(addprefix --bin ,$(HOST_RUST_CARGO_PROGRAMS)) $(cargo_host_flag) $(host_program_features_flag)
603 $(call BUILDSTATUS,END_Rust $(HOST_RUST_CARGO_PROGRAMS))
604 else
605 force-cargo-host-program-%:
606 $(call RUN_CARGO,$*) $(addprefix --bin ,$(HOST_RUST_CARGO_PROGRAMS)) $(filter-out --release $(cargo_target_flag))
607 endif
609 else
610 force-cargo-host-program-%:
611 @true
613 endif # HOST_RUST_PROGRAMS