Debian Codegraph @

config/makefiles/rust.mk

raw
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/.
4
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.
8
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.
12
13 cargo_host_flag := --target=$(RUST_HOST_TARGET)
14 cargo_target_flag := --target=$(RUST_TARGET)
15
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
21
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
29
30 cargo_build_flags += --manifest-path $(CARGO_FILE)
31 ifdef BUILD_VERBOSE_LOG
32 cargo_build_flags += -vv
33 endif
34
35 ifneq (,$(USE_CARGO_JSON_MESSAGE_FORMAT))
36 cargo_build_flags += --message-format=json
37 endif
38
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
51
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))
55
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
63
64 rustflags_sancov =
65 ifndef MOZ_TSAN
66 ifndef FUZZING_JS_FUZZILLI
67 ifdef LIBFUZZER
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
70 #
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.
75 #
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 else
79 ifdef AFLFUZZ
80 # Use the same flags as afl-cc, specified here:
81 # https://github.com/AFLplusplus/AFLplusplus/blob/4eaacfb095ac164afaaf9c10b8112f98d8ad7c2a/src/afl-cc.c#L2101
82 # -sanitizer-coverage-level=3 Enable coverage for all blocks, critical edges. Implied by clang as default.
83 # -sanitizer-coverage-pc-table Create a static PC table.
84 # -sanitizer-coverage-trace-pc-guard Adds guard_variable (uint32_t) to every edge
85 rustflags_sancov += -Cpasses=sancov-module -Cllvm-args=-sanitizer-coverage-level=3 -Cllvm-args=-sanitizer-coverage-pc-table -Cllvm-args=-sanitizer-coverage-trace-pc-guard
86 endif
87 endif
88 endif
89 endif
90
91 # These flags are passed via `cargo rustc` and only apply to the final rustc
92 # invocation (i.e., only the top-level crate, not its dependencies).
93 cargo_rustc_flags = $(CARGO_RUSTCFLAGS)
94 ifndef DEVELOPER_OPTIONS
95 ifndef MOZ_DEBUG_RUST
96 # Enable link-time optimization for release builds, but not when linking
97 # gkrust_gtest. And not when doing cross-language LTO.
98 ifndef MOZ_LTO_RUST_CROSS
99 # Never enable when sancov is enabled to work around https://github.com/rust-lang/rust/issues/90300.
100 ifndef rustflags_sancov
101 # Never enable when coverage is enabled to work around https://github.com/rust-lang/rust/issues/90045.
102 ifndef MOZ_CODE_COVERAGE
103 ifeq (,$(findstring gkrust_gtest,$(RUST_LIBRARY_FILE)))
104 cargo_rustc_flags += -Clto$(if $(filter full,$(MOZ_LTO_RUST_CROSS)),=fat)
105 endif
106 # We need -Cembed-bitcode=yes for all crates when using -Clto.
107 RUSTFLAGS += -Cembed-bitcode=yes
108 endif
109 endif
110 endif
111 endif
112 endif
113
114 ifdef CARGO_INCREMENTAL
115 export CARGO_INCREMENTAL
116 endif
117
118 rustflags_neon =
119 ifeq (neon,$(MOZ_FPU))
120 ifneq (,$(filter thumbv7neon-,$(RUST_TARGET)))
121 # Enable neon and disable restriction to 16 FPU registers when neon is enabled
122 # but we're not using a thumbv7neon target, where it's already the default.
123 # (CPUs with neon have 32 FPU registers available)
124 rustflags_neon += -C target_feature=+neon,-d16
125 endif
126 endif
127
128 rustflags_override = $(MOZ_RUST_DEFAULT_FLAGS) $(rustflags_neon)
129
130 ifdef DEVELOPER_OPTIONS
131 # By default the Rust compiler will perform a limited kind of ThinLTO on each
132 # crate. For local builds this additional optimization is not worth the
133 # increase in compile time so we opt out of it.
134 rustflags_override += -Clto=off
135 endif
136
137 ifdef MOZ_USING_SCCACHE
138 export RUSTC_WRAPPER=$(CCACHE)
139 endif
140
141 ifndef CROSS_COMPILE
142 ifdef MOZ_TSAN
143 PASS_ONLY_BASE_CFLAGS_TO_RUST=1
144 else
145 ifneq (,$(MOZ_ASAN)$(MOZ_UBSAN))
146 ifneq ($(OS_ARCH), Linux)
147 PASS_ONLY_BASE_CFLAGS_TO_RUST=1
148 endif # !Linux
149 endif # MOZ_ASAN || MOZ_UBSAN
150 endif # MOZ_TSAN
151 endif # !CROSS_COMPILE
152
153 ifeq (WINNT,$(HOST_OS_ARCH))
154 ifdef MOZ_CODE_COVERAGE
155 PASS_ONLY_BASE_CFLAGS_TO_RUST=1
156 endif # MOZ_CODE_COVERAGE
157 endif # WINNT
158
159 ifeq (WINNT,$(HOST_OS_ARCH))
160 # //?/ is the long path prefix which seems to confuse make, so we remove it
161 # (things should work without it).
162 normalize_sep = $(patsubst //?/%,%,$(subst \,/,$(1)))
163 else
164 normalize_sep = $(1)
165 endif
166
167 # We start with host variables because the rust host and the rust target might be the same,
168 # in which case we want the latter to take priority.
169
170 # We're passing these for consumption by the `cc` crate, which doesn't use the same
171 # convention as cargo itself:
172 # https://github.com/alexcrichton/cc-rs/blob/baa71c0e298d9ad7ac30f0ad78f20b4b3b3a8fb2/src/lib.rs#L1715
173 rust_host_cc_env_name := $(subst -,_,$(RUST_HOST_TARGET))
174
175 # HOST_CC/HOST_CXX/CC/CXX usually contain base flags for e.g. the build target.
176 # We want to pass those through CFLAGS_*/CXXFLAGS_* instead, so that they end up
177 # after whatever cc-rs adds to the compiler command line, so that they win.
178 # Ideally, we'd use CRATE_CC_NO_DEFAULTS=1, but that causes other problems at the
179 # moment.
180 export CC_$(rust_host_cc_env_name)=$(filter-out $(HOST_CC_BASE_FLAGS),$(HOST_CC))
181 export CXX_$(rust_host_cc_env_name)=$(filter-out $(HOST_CXX_BASE_FLAGS),$(HOST_CXX))
182 export AR_$(rust_host_cc_env_name)=$(HOST_AR)
183
184 rust_cc_env_name := $(subst -,_,$(RUST_TARGET))
185
186 export CC_$(rust_cc_env_name)=$(filter-out $(CC_BASE_FLAGS),$(CC))
187 export CXX_$(rust_cc_env_name)=$(filter-out $(CXX_BASE_FLAGS),$(CXX))
188 export AR_$(rust_cc_env_name)=$(AR)
189
190 ifeq (WINNT,$(HOST_OS_ARCH))
191 HOST_CC_BASE_FLAGS += -DUNICODE
192 HOST_CXX_BASE_FLAGS += -DUNICODE
193 endif
194 ifeq (WINNT,$(OS_ARCH))
195 CC_BASE_FLAGS += -DUNICODE
196 CXX_BASE_FLAGS += -DUNICODE
197 endif
198
199 ifneq (1,$(PASS_ONLY_BASE_CFLAGS_TO_RUST))
200 # -DMOZILLA_CONFIG_H is added to prevent mozilla-config.h from injecting anything
201 # in C/C++ compiles from rust. That's not needed in the other branch because the
202 # base flags don't force-include mozilla-config.h.
203 export CFLAGS_$(rust_host_cc_env_name)=$(HOST_CC_BASE_FLAGS) $(COMPUTED_HOST_CFLAGS) -DMOZILLA_CONFIG_H
204 export CXXFLAGS_$(rust_host_cc_env_name)=$(HOST_CXX_BASE_FLAGS) $(COMPUTED_HOST_CXXFLAGS) -DMOZILLA_CONFIG_H
205 # We exclude -fprofile-generate from the PGO flags because on non-cross compiles,
206 # that affects build scripts, and they fail to link because the linker flags are
207 # not adequate, and also, we don't want to run instrumented build scripts.
208 # The cc crate will fill in for those flags anyways, but we do need the PGO and
209 # LTO flags to fill in for what the cc crate doesn't handle
210 # (e.g. -pgo-temporal-instrumentation)
211 # We can't use LTO flags with GCC, though: https://github.com/rust-lang/rust/issues/138681
212 ifneq (,$(filter clang%,$(CC_TYPE)))
213 RUST_LTO_CFLAGS=$(MOZ_LTO_CFLAGS)
214 endif
215 export CFLAGS_$(rust_cc_env_name)=$(CC_BASE_FLAGS) $(RUST_LTO_CFLAGS) $(COMPUTED_CFLAGS) $(filter-out -fprofile-generate%,$(PGO_CFLAGS)) -DMOZILLA_CONFIG_H
216 export CXXFLAGS_$(rust_cc_env_name)=$(CXX_BASE_FLAGS) $(RUST_LTO_CFLAGS) $(COMPUTED_CXXFLAGS) $(filter-out -fprofile-generate%,$(PGO_CFLAGS)) -DMOZILLA_CONFIG_H
217 else
218 # Because cargo doesn't allow to distinguish builds happening for build
219 # scripts/procedural macros vs. those happening for the rust target,
220 # we can't blindly pass all our flags down for cc-rs to use them, because of the
221 # side effects they can have on what otherwise should be host builds.
222 # So for sanitizer and coverage builds, we only pass the base compiler flags.
223 # This means C code built by rust is not going to be covered by sanitizers
224 # and coverage. But at least we control what compiler is being used,
225 # rather than relying on cc-rs guesses, which, sometimes fail us.
226 # -fno-sized-deallocation is important, though, as -fsized-deallocation may be the
227 # compiler default and we don't want it to be used
228 # (see build/moz.configure/flags.configure). Likewise with -fno-aligned-new.
229 export CFLAGS_$(rust_host_cc_env_name)=$(HOST_CC_BASE_FLAGS)
230 export CXXFLAGS_$(rust_host_cc_env_name)=$(HOST_CXX_BASE_FLAGS)
231 export CFLAGS_$(rust_cc_env_name)=$(CC_BASE_FLAGS)
232 export CXXFLAGS_$(rust_cc_env_name)=$(CXX_BASE_FLAGS) $(filter -fno-aligned-new -fno-sized-deallocation,$(COMPUTED_CXXFLAGS))
233 endif
234
235 # When host == target, cargo will compile build scripts with sanitizers enabled
236 # if sanitizers are enabled, which may randomly fail when they execute
237 # because of https://github.com/google/sanitizers/issues/1322.
238 # Work around by disabling __tls_get_addr interception (bug 1635327).
239 ifeq ($(RUST_TARGET),$(RUST_HOST_TARGET))
240 define sanitizer_options
241 ifdef MOZ_$1
242 export $1_OPTIONS:=$$($1_OPTIONS:%=%:)intercept_tls_get_addr=0
243 endif
244 endef
245 $(foreach san,ASAN TSAN UBSAN,$(eval $(call sanitizer_options,$(san))))
246 endif
247
248 # Force the target down to all bindgen callers, even those that may not
249 # read BINDGEN_SYSTEM_FLAGS some way or another.
250 export BINDGEN_EXTRA_CLANG_ARGS:=$(filter --target=%,$(BINDGEN_SYSTEM_FLAGS))
251 export CARGO_TARGET_DIR
252 export RUSTFLAGS
253 export RUSTC
254 export RUSTDOC
255 export RUSTDOCFLAGS
256 export RUSTFMT
257 export LIBCLANG_PATH=$(MOZ_LIBCLANG_PATH)
258 export CLANG_PATH=$(MOZ_CLANG_PATH)
259 export PKG_CONFIG
260 export PKG_CONFIG_ALLOW_CROSS=1
261 export PKG_CONFIG_PATH
262 ifneq (,$(PKG_CONFIG_SYSROOT_DIR))
263 export PKG_CONFIG_SYSROOT_DIR
264 endif
265 ifneq (,$(PKG_CONFIG_LIBDIR))
266 export PKG_CONFIG_LIBDIR
267 endif
268 export RUST_BACKTRACE=full
269 export MOZ_TOPOBJDIR=$(topobjdir)
270 export MOZ_FOLD_LIBS
271 export PYTHON3
272 export CARGO_PROFILE_RELEASE_OPT_LEVEL
273 export CARGO_PROFILE_DEV_OPT_LEVEL
274
275 # Set COREAUDIO_SDK_PATH for third_party/rust/coreaudio-sys/build.rs
276 ifeq ($(OS_ARCH), Darwin)
277 ifdef MACOS_SDK_DIR
278 export COREAUDIO_SDK_PATH=$(MACOS_SDK_DIR)
279 endif
280 ifdef IPHONEOS_SDK_DIR
281 export COREAUDIO_SDK_PATH=$(IPHONEOS_SDK_DIR)
282 # export for build/macosx/xcrun
283 export IPHONEOS_SDK_DIR
284 PATH := $(topsrcdir)/build/macosx:$(PATH)
285 endif
286 endif
287 # Use the same prefix as set through modules/zlib/src/mozzconf.h
288 # for libz-rs-sys, since we still use the headers from there.
289 export LIBZ_RS_SYS_PREFIX=MOZ_Z_
290
291 ifndef RUSTC_BOOTSTRAP
292 RUSTC_BOOTSTRAP := mozglue_static,qcms
293 ifdef MOZ_RUST_SIMD
294 RUSTC_BOOTSTRAP := $(RUSTC_BOOTSTRAP),encoding_rs,any_all_workaround
295 endif
296 export RUSTC_BOOTSTRAP
297 endif
298
299 target_rust_ltoable := force-cargo-library-build $(ADD_RUST_LTOABLE)
300 target_rust_nonltoable := force-cargo-test-run force-cargo-program-build
301
302 ifdef MOZ_PGO_RUST
303 ifdef MOZ_PROFILE_GENERATE
304 rust_pgo_flags := -C profile-generate=$(topobjdir)
305 ifeq (1,$(words $(filter 5.% 6.% 7.% 8.% 9.% 10.% 11.%,$(CC_VERSION) $(RUSTC_LLVM_VERSION))))
306 # Disable value profiling when:
307 # (RUSTC_LLVM_VERSION < 12 and CC_VERSION >= 12) or (RUSTC_LLVM_VERSION >= 12 and CC_VERSION < 12)
308 rust_pgo_flags += -C llvm-args=--disable-vp=true
309 endif
310 # The C compiler may be passed extra llvm flags for PGO that we also want to pass to rust as well.
311 # In PROFILE_GEN_CFLAGS, they look like "-mllvm foo", and we want "-C llvm-args=foo", so first turn
312 # "-mllvm foo" into "-mllvm:foo" so that it becomes a unique argument, that we can then filter for,
313 # excluding other flags, and then turn into the right string.
314 rust_pgo_flags += $(patsubst -mllvm:%,-C llvm-args=%,$(filter -mllvm:%,$(subst -mllvm ,-mllvm:,$(PROFILE_GEN_CFLAGS))))
315 else # MOZ_PROFILE_USE
316 rust_pgo_flags := -C profile-use=$(PGO_PROFILE_PATH)
317 endif
318 endif
319
320 # Work around https://github.com/rust-lang/rust/issues/112480
321 ifdef MOZ_DEBUG_RUST
322 ifneq (,$(filter i686-pc-windows-%,$(RUST_TARGET)))
323 RUSTFLAGS += -Zmir-enable-passes=-CheckAlignment
324 RUSTC_BOOTSTRAP := 1
325 endif
326 endif
327
328 ifeq (WINNT_clang,$(OS_ARCH)_$(CC_TYPE))
329 RUSTFLAGS += -C dlltool=$(LLVM_DLLTOOL)
330 endif
331
332 $(target_rust_ltoable): RUSTFLAGS:=$(rustflags_override) $(rustflags_sancov) $(RUSTFLAGS) $(rust_pgo_flags) \
333 $(if $(MOZ_LTO_RUST_CROSS),\
334 -Clinker-plugin-lto \
335 ,)
336 $(target_rust_nonltoable): RUSTFLAGS:=$(rustflags_override) $(rustflags_sancov) $(RUSTFLAGS)
337
338 TARGET_RECIPES := $(target_rust_ltoable) $(target_rust_nonltoable)
339
340 HOST_RECIPES := \
341 $(foreach a,library program,$(foreach b,build check udeps clippy,force-cargo-host-$(a)-$(b)))
342
343 $(HOST_RECIPES): RUSTFLAGS:=$(rustflags_override)
344
345 # If this is a release build we want rustc to generate one codegen unit per
346 # crate. This results in better optimization and less code duplication at the
347 # cost of longer compile times.
348 ifndef DEVELOPER_OPTIONS
349 $(TARGET_RECIPES) $(HOST_RECIPES): RUSTFLAGS += -C codegen-units=1
350 endif
351
352 # We use the + prefix to pass down the jobserver fds to cargo, but we
353 # don't use the prefix when make -n is used, so that cargo doesn't run
354 # in that case)
355 define RUN_CARGO_INNER
356 $(if $(findstring n,$(filter-out --%, $(MAKEFLAGS))),,+)$(CARGO) $(1) $(cargo_build_flags) $(CARGO_EXTRA_FLAGS) $(cargo_extra_cli_flags)
357 endef
358
359 ifdef CARGO_CONTINUE_ON_ERROR
360 define RUN_CARGO
361 -$(RUN_CARGO_INNER)
362 endef
363 else
364 define RUN_CARGO
365 $(RUN_CARGO_INNER)
366 endef
367 endif
368
369 # This function is intended to be called by:
370 #
371 # $(call CARGO_BUILD,EXTRA_ENV_VAR1=X EXTRA_ENV_VAR2=Y ...)
372 #
373 # but, given the idiosyncracies of make, can also be called without arguments:
374 #
375 # $(call CARGO_BUILD)
376 define CARGO_BUILD
377 $(call RUN_CARGO,rustc$(if $(BUILDSTATUS), --timings)$(if $(findstring k,$(filter-out --%, $(MAKEFLAGS))), --keep-going))
378 endef
379
380 cargo_host_linker_env_var := CARGO_TARGET_$(call varize,$(RUST_HOST_TARGET))_LINKER
381 cargo_linker_env_var := CARGO_TARGET_$(call varize,$(RUST_TARGET))_LINKER
382
383 export MOZ_CLANG_NEWER_THAN_RUSTC_LLVM
384 export MOZ_CARGO_WRAP_LDFLAGS
385 export MOZ_CARGO_WRAP_LD
386 export MOZ_CARGO_WRAP_LD_CXX
387 export MOZ_CARGO_WRAP_HOST_LDFLAGS
388 export MOZ_CARGO_WRAP_HOST_LD
389 export MOZ_CARGO_WRAP_HOST_LD_CXX
390 # Exporting from make always exports a value. Setting a value per-recipe
391 # would export an empty value for the host recipes. When not doing a
392 # cross-compile, the --target for those is the same, and cargo will use
393 # CARGO_TARGET_*_LINKER for its linker, so we always pass the
394 # cargo-linker wrapper, and fill MOZ_CARGO_WRAP_{HOST_,}LD* more or less
395 # appropriately for all recipes.
396 ifeq (WINNT,$(HOST_OS_ARCH))
397 # Use .bat wrapping on Windows hosts, and shell wrapping on other hosts.
398 # Like for CC/C*FLAGS, we want the target values to trump the host values when
399 # both variables are the same.
400 export $(cargo_host_linker_env_var):=$(topsrcdir)/build/cargo-host-linker.bat
401 export $(cargo_linker_env_var):=$(topsrcdir)/build/cargo-linker.bat
402 WRAP_HOST_LINKER_LIBPATHS:=$(HOST_LINKER_LIBPATHS_BAT)
403 else
404 export $(cargo_host_linker_env_var):=$(topsrcdir)/build/cargo-host-linker
405 export $(cargo_linker_env_var):=$(topsrcdir)/build/cargo-linker
406 WRAP_HOST_LINKER_LIBPATHS:=$(HOST_LINKER_LIBPATHS)
407 endif
408
409 # Cargo needs the same linker flags as the C/C++ compiler,
410 # but not the final libraries. Filter those out because they
411 # cause problems on macOS 10.7; see bug 1365993 for details.
412 # Also, we don't want to pass PGO flags until cargo supports them.
413 # Finally, we also remove the -Wl,--build-id=uuid flag when it's in
414 # the LDFLAGS. The flag was chosen over the default (build-id=sha1)
415 # in developer builds, because for libxul, it's faster. But it's also
416 # non-deterministic. So when the rust compiler produces procedural
417 # macros as libraries, they're not reproducible. Those procedural
418 # macros then end up as dependencies of other crates, and their
419 # non-reproducibility leads to sccache transitively having cache
420 # misses.
421 $(TARGET_RECIPES): MOZ_CARGO_WRAP_LDFLAGS:=$(filter-out -fsanitize=cfi% -framework Cocoa -lobjc AudioToolbox ExceptionHandling -fprofile-% -Wl$(COMMA)--build-id=uuid,$(LDFLAGS))
422
423 # When building with sanitizer, rustc links its own runtime, which conflicts
424 # with the one that passing -fsanitize=* to the linker would add.
425 # Ideally, we'd always do this filtering, but because the flags may also apply
426 # to build scripts because cargo doesn't allow the distinction, we only filter
427 # when building programs, except when using thread sanitizer where we filter
428 # everywhere.
429 ifneq (,$(filter -Zsanitizer=%,$(RUSTFLAGS)))
430 $(if $(filter -Zsanitizer=thread,$(RUSTFLAGS)),$(TARGET_RECIPES),force-cargo-program-build): MOZ_CARGO_WRAP_LDFLAGS:=$(filter-out -fsanitize=%,$(MOZ_CARGO_WRAP_LDFLAGS))
431 endif
432
433 # Rustc assumes that *-windows-gnu targets build with mingw-gcc and manually
434 # add runtime libraries that don't exist with mingw-clang. We created dummy
435 # libraries in $(topobjdir)/build/win32, but that's not enough, because some
436 # of the wanted symbols that come from these libraries are available in a
437 # different library, that we add manually. We also need to avoid rustc
438 # passing -nodefaultlibs to clang so that it adds clang_rt.
439 ifeq (WINNT_clang,$(OS_ARCH)_$(CC_TYPE))
440 force-cargo-program-build: MOZ_CARGO_WRAP_LDFLAGS+=-L$(topobjdir)/build/win32 -lunwind
441 force-cargo-program-build: CARGO_RUSTCFLAGS += -C default-linker-libraries=yes
442 endif
443
444 # Rustc passes -nodefaultlibs to the linker (clang) on mac, which prevents
445 # clang from adding the necessary sanitizer runtimes when building with
446 # C/C++ sanitizer but without rust sanitizer.
447 ifeq (Darwin,$(OS_ARCH))
448 ifeq (,$(filter -Zsanitizer=%,$(RUSTFLAGS)))
449 ifneq (,$(filter -fsanitize=%,$(LDFLAGS)))
450 $(TARGET_RECIPES): RUSTFLAGS += -C default-linker-libraries=yes
451 endif
452 endif
453 endif
454
455 $(HOST_RECIPES): MOZ_CARGO_WRAP_LDFLAGS:=$(HOST_LDFLAGS) $(WRAP_HOST_LINKER_LIBPATHS)
456 $(TARGET_RECIPES) $(HOST_RECIPES): MOZ_CARGO_WRAP_HOST_LDFLAGS:=$(HOST_LDFLAGS) $(WRAP_HOST_LINKER_LIBPATHS)
457
458 ifeq (,$(filter clang-cl,$(CC_TYPE)))
459 $(TARGET_RECIPES): MOZ_CARGO_WRAP_LD:=$(CC)
460 $(TARGET_RECIPES): MOZ_CARGO_WRAP_LD_CXX:=$(CXX)
461 else
462 $(TARGET_RECIPES): MOZ_CARGO_WRAP_LD:=$(LINKER)
463 $(TARGET_RECIPES): MOZ_CARGO_WRAP_LD_CXX:=$(LINKER)
464 endif
465
466 ifeq (,$(filter clang-cl,$(HOST_CC_TYPE)))
467 $(HOST_RECIPES): MOZ_CARGO_WRAP_LD:=$(HOST_CC)
468 $(HOST_RECIPES): MOZ_CARGO_WRAP_LD_CXX:=$(HOST_CXX)
469 $(TARGET_RECIPES) $(HOST_RECIPES): MOZ_CARGO_WRAP_HOST_LD:=$(HOST_CC)
470 $(TARGET_RECIPES) $(HOST_RECIPES): MOZ_CARGO_WRAP_HOST_LD_CXX:=$(HOST_CXX)
471 else
472 $(HOST_RECIPES): MOZ_CARGO_WRAP_LD:=$(HOST_LINKER)
473 $(HOST_RECIPES): MOZ_CARGO_WRAP_LD_CXX:=$(HOST_LINKER)
474 $(TARGET_RECIPES) $(HOST_RECIPES): MOZ_CARGO_WRAP_HOST_LD:=$(HOST_LINKER)
475 $(TARGET_RECIPES) $(HOST_RECIPES): MOZ_CARGO_WRAP_HOST_LD_CXX:=$(HOST_LINKER)
476 endif
477
478 define make_default_rule
479 $(1):
480
481 endef
482
483 # make_cargo_rule(target, real-target [, extra-deps])
484 # Generates a rule suitable to rebuild $(target) only if its dependencies are
485 # obsolete.
486 # It relies on the fact that upon build, cargo generates a dependency file named
487 # `$(target).d'. Unfortunately the lhs of the rule has an absolute path,
488 # so we extract it under the name $(target)_deps below.
489 #
490 # If the dependencies are empty, the file was not created so we force a rebuild.
491 # Otherwise we add it to the dependency list.
492 #
493 # The actual rule is a bit tricky. The `+' prefix allow for recursive parallel
494 # make, and it's skipped (`:') if we already triggered a rebuild as part of the
495 # dependency chain.
496 #
497 # Another tricky thing: some dependencies may contain escaped spaces, and they
498 # need to be preserved, but $(foreach) splits on spaces, so we replace escaped
499 # spaces with some unlikely string for the foreach, and replace them back in the
500 # loop itself.
501 define make_cargo_rule
502 $(notdir $(1))_deps := $$(call normalize_sep,$$(wordlist 2, 10000000, $$(if $$(wildcard $(basename $(1)).d),$$(shell cat $(basename $(1)).d))))
503 $(1): $(CARGO_FILE) $(3) $(topsrcdir)/Cargo.lock $$(if $$($(notdir $(1))_deps),$$($(notdir $(1))_deps),$(2))
504 $$(REPORT_BUILD)
505 $$(if $$($(notdir $(1))_deps),+$(MAKE) $(2),:)
506
507 $$(foreach dep, $$(subst \ ,_^_^_^_,$$($(notdir $(1))_deps)),$$(eval $$(call make_default_rule,$$(subst _^_^_^_,\ ,$$(dep)))))
508 endef
509
510 ifdef RUST_LIBRARY_FILE
511
512 rust_features_flag := --features '$(if $(RUST_LIBRARY_FEATURES),$(RUST_LIBRARY_FEATURES) )mozilla-central-workspace-hack'
513
514 ifeq (WASI,$(OS_ARCH))
515 # The rust wasi target defaults to statically link the wasi crt, but when we
516 # build static libraries from rust and link them with C/C++ code, we also link
517 # a wasi crt, which may conflict with rust's.
518 force-cargo-library-build: CARGO_RUSTCFLAGS += -C target-feature=-crt-static
519 endif
520
521 # Assume any system libraries rustc links against are already in the target's LIBS.
522 #
523 # We need to run cargo unconditionally, because cargo is the only thing that
524 # has full visibility into how changes in Rust sources might affect the final
525 # build.
526 force-cargo-library-build:
527 $(call BUILDSTATUS,START_Rust $(notdir $(RUST_LIBRARY_FILE)))
528 $(call CARGO_BUILD) --lib $(cargo_target_flag) $(rust_features_flag) -- $(cargo_rustc_flags)
529 $(call BUILDSTATUS,END_Rust $(notdir $(RUST_LIBRARY_FILE)))
530 # When we are building in --enable-release mode; we add an additional check to confirm
531 # that we are not importing any networking-related functions in rust code. This reduces
532 # the chance of proxy bypasses originating from rust code.
533 # The check only works when rust code is built with -Clto but without MOZ_LTO_RUST_CROSS.
534 # Sanitizers and sancov also fail because compiler-rt hooks network functions.
535 ifndef MOZ_PROFILE_GENERATE
536 ifeq ($(OS_ARCH), Linux)
537 ifeq (,$(rustflags_sancov)$(MOZ_ASAN)$(MOZ_TSAN)$(MOZ_UBSAN))
538 ifndef MOZ_LTO_RUST_CROSS
539 ifneq (,$(filter -Clto,$(cargo_rustc_flags)))
540 $(call py_action,check_binary $(@F),--networking $(RUST_LIBRARY_FILE))
541 endif
542 endif
543 endif
544 endif
545 endif
546
547 $(eval $(call make_cargo_rule,$(RUST_LIBRARY_FILE),force-cargo-library-build))
548
549 SUGGEST_INSTALL_ON_FAILURE = (ret=$$?; if [ $$ret = 101 ]; then echo If $1 is not installed, install it using: cargo install $1; fi; exit $$ret)
550
551 ifndef CARGO_NO_AUTO_ARG
552 force-cargo-library-%:
553 $(call RUN_CARGO,$*) --lib $(cargo_target_flag) $(rust_features_flag) || $(call SUGGEST_INSTALL_ON_FAILURE,cargo-$*)
554 else
555 force-cargo-library-%:
556 $(call RUN_CARGO,$*) || $(call SUGGEST_INSTALL_ON_FAILURE,cargo-$*)
557 endif
558
559 else
560 force-cargo-library-%:
561 @true
562
563 endif # RUST_LIBRARY_FILE
564
565 ifdef RUST_TESTS
566
567 rust_test_options := $(foreach test,$(RUST_TESTS),-p $(test))
568
569 rust_test_features_flag := --features '$(if $(RUST_TEST_FEATURES),$(RUST_TEST_FEATURES) )mozilla-central-workspace-hack'
570
571 # Don't stop at the first failure. We want to list all failures together.
572 rust_test_flag := --no-fail-fast
573
574 force-cargo-test-run:
575 $(call RUN_CARGO,test $(cargo_target_flag) $(rust_test_flag) $(rust_test_options) $(rust_test_features_flag))
576
577 endif # RUST_TESTS
578
579 ifdef HOST_RUST_LIBRARY_FILE
580
581 host_rust_features_flag := --features '$(if $(HOST_RUST_LIBRARY_FEATURES),$(HOST_RUST_LIBRARY_FEATURES) )mozilla-central-workspace-hack'
582
583 force-cargo-host-library-build:
584 $(call BUILDSTATUS,START_Rust $(notdir $(HOST_RUST_LIBRARY_FILE)))
585 $(call CARGO_BUILD) --lib $(cargo_host_flag) $(host_rust_features_flag)
586 $(call BUILDSTATUS,END_Rust $(notdir $(HOST_RUST_LIBRARY_FILE)))
587
588 $(eval $(call make_cargo_rule,$(HOST_RUST_LIBRARY_FILE),force-cargo-host-library-build))
589
590 ifndef CARGO_NO_AUTO_ARG
591 force-cargo-host-library-%:
592 $(call RUN_CARGO,$*) --lib $(cargo_host_flag) $(host_rust_features_flag)
593 else
594 force-cargo-host-library-%:
595 $(call RUN_CARGO,$*) --lib $(filter-out --release $(cargo_host_flag)) $(host_rust_features_flag)
596 endif
597
598 else
599 force-cargo-host-library-%:
600 @true
601 endif # HOST_RUST_LIBRARY_FILE
602
603 ifdef RUST_PROGRAMS
604
605 program_features_flag := --features mozilla-central-workspace-hack
606
607 force-cargo-program-build: $(call resfile,module)
608 $(call BUILDSTATUS,START_Rust $(RUST_CARGO_PROGRAMS))
609 $(call CARGO_BUILD) $(addprefix --bin ,$(RUST_CARGO_PROGRAMS)) $(cargo_target_flag) $(program_features_flag) -- $(addprefix -C link-arg=$(CURDIR)/,$(call resfile,module)) $(CARGO_RUSTCFLAGS)
610 $(call BUILDSTATUS,END_Rust $(RUST_CARGO_PROGRAMS))
611
612 $(foreach RUST_PROGRAM,$(RUST_PROGRAMS), $(eval $(call make_cargo_rule,$(RUST_PROGRAM),force-cargo-program-build,$(call resfile,module))))
613
614 ifndef CARGO_NO_AUTO_ARG
615 force-cargo-program-%:
616 $(call RUN_CARGO,$*) $(addprefix --bin ,$(RUST_CARGO_PROGRAMS)) $(cargo_target_flag) $(program_features_flag)
617 else
618 force-cargo-program-%:
619 $(call RUN_CARGO,$*)
620 endif
621
622 else
623 force-cargo-program-%:
624 @true
625 endif # RUST_PROGRAMS
626 ifdef HOST_RUST_PROGRAMS
627
628 host_program_features_flag := --features mozilla-central-workspace-hack
629
630 force-cargo-host-program-build:
631 $(call BUILDSTATUS,START_Rust $(HOST_RUST_CARGO_PROGRAMS))
632 $(call CARGO_BUILD) $(addprefix --bin ,$(HOST_RUST_CARGO_PROGRAMS)) $(cargo_host_flag) $(host_program_features_flag)
633 $(call BUILDSTATUS,END_Rust $(HOST_RUST_CARGO_PROGRAMS))
634
635 $(foreach HOST_RUST_PROGRAM,$(HOST_RUST_PROGRAMS), $(eval $(call make_cargo_rule,$(HOST_RUST_PROGRAM),force-cargo-host-program-build)))
636
637 ifndef CARGO_NO_AUTO_ARG
638 force-cargo-host-program-%:
639 $(call BUILDSTATUS,START_Rust $(HOST_RUST_CARGO_PROGRAMS))
640 $(call RUN_CARGO,$*) $(addprefix --bin ,$(HOST_RUST_CARGO_PROGRAMS)) $(cargo_host_flag) $(host_program_features_flag)
641 $(call BUILDSTATUS,END_Rust $(HOST_RUST_CARGO_PROGRAMS))
642 else
643 force-cargo-host-program-%:
644 $(call RUN_CARGO,$*) $(addprefix --bin ,$(HOST_RUST_CARGO_PROGRAMS)) $(filter-out --release $(cargo_target_flag))
645 endif
646
647 else
648 force-cargo-host-program-%:
649 @true
650
651 endif # HOST_RUST_PROGRAMS