Source code

Revision control

Other Tools

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
# Enable color output if original stdout was a TTY and color settings
36
# aren't already present. This essentially restores the default behavior
37
# of cargo when running via `mach`.
38
ifdef MACH_STDOUT_ISATTY
39
ifeq (,$(findstring --color,$(cargo_build_flags)))
40
cargo_build_flags += --color=always
41
endif
42
endif
43
44
# These flags are passed via `cargo rustc` and only apply to the final rustc
45
# invocation (i.e., only the top-level crate, not its dependencies).
46
cargo_rustc_flags = $(CARGO_RUSTCFLAGS)
47
ifndef DEVELOPER_OPTIONS
48
ifndef MOZ_DEBUG_RUST
49
# Enable link-time optimization for release builds, but not when linking
50
# gkrust_gtest.
51
ifeq (,$(findstring gkrust_gtest,$(RUST_LIBRARY_FILE)))
52
cargo_rustc_flags += -Clto
53
endif
54
endif
55
endif
56
57
ifdef CARGO_INCREMENTAL
58
export CARGO_INCREMENTAL
59
endif
60
61
rustflags_neon =
62
ifeq (neon,$(MOZ_FPU))
63
# Enable neon and disable restriction to 16 FPU registers
64
# (CPUs with neon have 32 FPU registers available)
65
rustflags_neon += -C target_feature=+neon,-d16
66
endif
67
68
rustflags_override = $(MOZ_RUST_DEFAULT_FLAGS) $(rustflags_neon)
69
70
ifdef MOZ_USING_SCCACHE
71
export RUSTC_WRAPPER=$(CCACHE)
72
endif
73
74
ifdef MOZ_CODE_COVERAGE
75
ifeq (gcc,$(CC_TYPE))
76
CODE_COVERAGE_GCC=1
77
endif
78
endif
79
80
# We start with host variables because the rust host and the rust target might be the same,
81
# in which case we want the latter to take priority.
82
83
# We're passing these for consumption by the `cc` crate, which doesn't use the same
84
# convention as cargo itself:
86
rust_host_cc_env_name := $(subst -,_,$(RUST_HOST_TARGET))
87
88
export CC_$(rust_host_cc_env_name)=$(HOST_CC)
89
export CXX_$(rust_host_cc_env_name)=$(HOST_CXX)
90
# We don't have a HOST_AR. If rust needs one, assume it's going to pick an appropriate one.
91
92
rust_cc_env_name := $(subst -,_,$(RUST_TARGET))
93
94
export CC_$(rust_cc_env_name)=$(CC)
95
export CXX_$(rust_cc_env_name)=$(CXX)
96
export AR_$(rust_cc_env_name)=$(AR)
97
ifeq (,$(MOZ_ASAN)$(MOZ_TSAN)$(MOZ_UBSAN)$(CODE_COVERAGE_GCC)$(FUZZING_INTERFACES))
98
# -DMOZILLA_CONFIG_H is added to prevent mozilla-config.h from injecting anything
99
# in C/C++ compiles from rust. That's not needed in the other branch because the
100
# base flags don't force-include mozilla-config.h.
101
export CFLAGS_$(rust_host_cc_env_name)=$(COMPUTED_HOST_CFLAGS) -DMOZILLA_CONFIG_H
102
export CXXFLAGS_$(rust_host_cc_env_name)=$(COMPUTED_HOST_CXXFLAGS) -DMOZILLA_CONFIG_H
103
export CFLAGS_$(rust_cc_env_name)=$(COMPUTED_CFLAGS) -DMOZILLA_CONFIG_H
104
export CXXFLAGS_$(rust_cc_env_name)=$(COMPUTED_CXXFLAGS) -DMOZILLA_CONFIG_H
105
else
106
# Because cargo doesn't allow to distinguish builds happening for build
107
# scripts/procedural macros vs. those happening for the rust target,
108
# we can't blindly pass all our flags down for cc-rs to use them, because of the
109
# side effects they can have on what otherwise should be host builds.
110
# So for sanitizer, fuzzing and coverage builds, we only pass the base compiler
111
# flags.
112
# This means C code built by rust is not going to be covered by sanitizer,
113
# fuzzing and coverage. But at least we control what compiler is being used,
114
# rather than relying on cc-rs guesses, which, sometimes fail us.
115
export CFLAGS_$(rust_host_cc_env_name)=$(HOST_CC_BASE_FLAGS)
116
export CXXFLAGS_$(rust_host_cc_env_name)=$(HOST_CXX_BASE_FLAGS)
117
export CFLAGS_$(rust_cc_env_name)=$(CC_BASE_FLAGS)
118
export CXXFLAGS_$(rust_cc_env_name)=$(CXX_BASE_FLAGS)
119
endif
120
121
export CARGO_TARGET_DIR
122
export RUSTFLAGS
123
export RUSTC
124
export RUSTDOC
125
export RUSTFMT
126
export MOZ_SRC=$(topsrcdir)
127
export MOZ_DIST=$(ABS_DIST)
128
export LIBCLANG_PATH=$(MOZ_LIBCLANG_PATH)
129
export CLANG_PATH=$(MOZ_CLANG_PATH)
130
export PKG_CONFIG
131
export PKG_CONFIG_ALLOW_CROSS=1
132
export RUST_BACKTRACE=full
133
export MOZ_TOPOBJDIR=$(topobjdir)
134
135
target_rust_ltoable := force-cargo-library-build
136
target_rust_nonltoable := force-cargo-test-run force-cargo-library-check $(foreach b,build check,force-cargo-program-$(b))
137
138
ifdef MOZ_PGO_RUST
139
rust_pgo_flags := $(if $(MOZ_PROFILE_GENERATE),-C profile-generate=$(topobjdir)) $(if $(MOZ_PROFILE_USE),-C profile-use=$(PGO_PROFILE_PATH))
140
endif
141
142
$(target_rust_ltoable): RUSTFLAGS:=$(rustflags_override) $(RUSTFLAGS) $(if $(MOZ_LTO_RUST),-Clinker-plugin-lto) $(rust_pgo_flags)
143
$(target_rust_nonltoable): RUSTFLAGS:=$(rustflags_override) $(RUSTFLAGS)
144
145
TARGET_RECIPES := $(target_rust_ltoable) $(target_rust_nonltoable)
146
147
HOST_RECIPES := \
148
$(foreach a,library program,$(foreach b,build check,force-cargo-host-$(a)-$(b)))
149
150
$(HOST_RECIPES): RUSTFLAGS:=$(rustflags_override)
151
152
cargo_env = $(subst -,_,$(subst a,A,$(subst b,B,$(subst c,C,$(subst d,D,$(subst e,E,$(subst f,F,$(subst g,G,$(subst h,H,$(subst i,I,$(subst j,J,$(subst k,K,$(subst l,L,$(subst m,M,$(subst n,N,$(subst o,O,$(subst p,P,$(subst q,Q,$(subst r,R,$(subst s,S,$(subst t,T,$(subst u,U,$(subst v,V,$(subst w,W,$(subst x,X,$(subst y,Y,$(subst z,Z,$1)))))))))))))))))))))))))))
153
154
# We use the + prefix to pass down the jobserver fds to cargo, but we
155
# don't use the prefix when make -n is used, so that cargo doesn't run
156
# in that case)
157
define RUN_CARGO
158
$(if $(findstring n,$(filter-out --%, $(MAKEFLAGS))),,+)$(CARGO) $(1) $(cargo_build_flags)
159
endef
160
161
# This function is intended to be called by:
162
#
163
# $(call CARGO_BUILD,EXTRA_ENV_VAR1=X EXTRA_ENV_VAR2=Y ...)
164
#
165
# but, given the idiosyncracies of make, can also be called without arguments:
166
#
167
# $(call CARGO_BUILD)
168
define CARGO_BUILD
169
$(call RUN_CARGO,rustc)
170
endef
171
172
define CARGO_CHECK
173
$(call RUN_CARGO,check)
174
endef
175
176
cargo_host_linker_env_var := CARGO_TARGET_$(call cargo_env,$(RUST_HOST_TARGET))_LINKER
177
cargo_linker_env_var := CARGO_TARGET_$(call cargo_env,$(RUST_TARGET))_LINKER
178
179
# Defining all of this for ASan/TSan builds results in crashes while running
180
# some crates's build scripts (!), so disable it for now.
181
ifndef MOZ_ASAN
182
ifndef MOZ_TSAN
183
ifndef MOZ_UBSAN
184
ifndef FUZZING_INTERFACES
185
# Cargo needs the same linker flags as the C/C++ compiler,
186
# but not the final libraries. Filter those out because they
187
# cause problems on macOS 10.7; see bug 1365993 for details.
188
# Also, we don't want to pass PGO flags until cargo supports them.
189
export MOZ_CARGO_WRAP_LDFLAGS
190
export MOZ_CARGO_WRAP_LD
191
export MOZ_CARGO_WRAP_HOST_LDFLAGS
192
export MOZ_CARGO_WRAP_HOST_LD
193
# Exporting from make always exports a value. Setting a value per-recipe
194
# would export an empty value for the host recipes. When not doing a
195
# cross-compile, the --target for those is the same, and cargo will use
196
# CARGO_TARGET_*_LINKER for its linker, so we always pass the
197
# cargo-linker wrapper, and fill MOZ_CARGO_WRAP_{HOST_,}LD* more or less
198
# appropriately for all recipes.
199
ifeq (WINNT,$(HOST_OS_ARCH))
200
# Use .bat wrapping on Windows hosts, and shell wrapping on other hosts.
201
# Like for CC/C*FLAGS, we want the target values to trump the host values when
202
# both variables are the same.
203
export $(cargo_host_linker_env_var):=$(topsrcdir)/build/cargo-host-linker.bat
204
export $(cargo_linker_env_var):=$(topsrcdir)/build/cargo-linker.bat
205
WRAP_HOST_LINKER_LIBPATHS:=$(HOST_LINKER_LIBPATHS_BAT)
206
else
207
export $(cargo_host_linker_env_var):=$(topsrcdir)/build/cargo-host-linker
208
export $(cargo_linker_env_var):=$(topsrcdir)/build/cargo-linker
209
WRAP_HOST_LINKER_LIBPATHS:=$(HOST_LINKER_LIBPATHS)
210
endif
211
$(TARGET_RECIPES): MOZ_CARGO_WRAP_LDFLAGS:=$(filter-out -fsanitize=cfi% -framework Cocoa -lobjc AudioToolbox ExceptionHandling -fprofile-%,$(LDFLAGS))
212
213
$(HOST_RECIPES): MOZ_CARGO_WRAP_LDFLAGS:=$(HOST_LDFLAGS) $(WRAP_HOST_LINKER_LIBPATHS)
214
$(TARGET_RECIPES) $(HOST_RECIPES): MOZ_CARGO_WRAP_HOST_LDFLAGS:=$(HOST_LDFLAGS) $(WRAP_HOST_LINKER_LIBPATHS)
215
216
ifeq (,$(filter clang-cl,$(CC_TYPE)))
217
$(TARGET_RECIPES): MOZ_CARGO_WRAP_LD:=$(CC)
218
else
219
$(TARGET_RECIPES): MOZ_CARGO_WRAP_LD:=$(LINKER)
220
endif
221
222
ifeq (,$(filter clang-cl,$(HOST_CC_TYPE)))
223
$(HOST_RECIPES): MOZ_CARGO_WRAP_LD:=$(HOST_CC)
224
$(TARGET_RECIPES) $(HOST_RECIPES): MOZ_CARGO_WRAP_HOST_LD:=$(HOST_CC)
225
else
226
$(HOST_RECIPES): MOZ_CARGO_WRAP_LD:=$(HOST_LINKER)
227
$(TARGET_RECIPES) $(HOST_RECIPES): MOZ_CARGO_WRAP_HOST_LD:=$(HOST_LINKER)
228
endif
229
230
endif # FUZZING_INTERFACES
231
endif # MOZ_UBSAN
232
endif # MOZ_TSAN
233
endif # MOZ_ASAN
234
235
ifdef RUST_LIBRARY_FILE
236
237
ifdef RUST_LIBRARY_FEATURES
238
rust_features_flag := --features '$(RUST_LIBRARY_FEATURES)'
239
endif
240
241
# Assume any system libraries rustc links against are already in the target's LIBS.
242
#
243
# We need to run cargo unconditionally, because cargo is the only thing that
244
# has full visibility into how changes in Rust sources might affect the final
245
# build.
246
force-cargo-library-build:
247
$(REPORT_BUILD)
248
$(call CARGO_BUILD) --lib $(cargo_target_flag) $(rust_features_flag) -- $(cargo_rustc_flags)
249
250
$(RUST_LIBRARY_FILE): force-cargo-library-build
251
# When we are building in --enable-release mode; we add an additional check to confirm
252
# that we are not importing any networking-related functions in rust code. This reduces
253
# the chance of proxy bypasses originating from rust code.
254
# The check only works when rust code is built with -Clto.
255
ifndef MOZ_PROFILE_GENERATE
256
ifeq ($(OS_ARCH), Linux)
257
ifneq (,$(filter -Clto,$(cargo_rustc_flags)))
258
$(call py_action,check_binary,--target --networking $@)
259
endif
260
endif
261
endif
262
263
force-cargo-library-check:
264
$(call CARGO_CHECK) --lib $(cargo_target_flag) $(rust_features_flag)
265
else
266
force-cargo-library-check:
267
@true
268
endif # RUST_LIBRARY_FILE
269
270
ifdef RUST_TESTS
271
272
rust_test_options := $(foreach test,$(RUST_TESTS),-p $(test))
273
274
ifdef RUST_TEST_FEATURES
275
rust_test_features_flag := --features '$(RUST_TEST_FEATURES)'
276
endif
277
278
# Don't stop at the first failure. We want to list all failures together.
279
rust_test_flag := --no-fail-fast
280
281
force-cargo-test-run:
282
$(call RUN_CARGO,test $(cargo_target_flag) $(rust_test_flag) $(rust_test_options) $(rust_test_features_flag))
283
284
endif
285
286
ifdef HOST_RUST_LIBRARY_FILE
287
288
ifdef HOST_RUST_LIBRARY_FEATURES
289
host_rust_features_flag := --features '$(HOST_RUST_LIBRARY_FEATURES)'
290
endif
291
292
force-cargo-host-library-build:
293
$(REPORT_BUILD)
294
$(call CARGO_BUILD) --lib $(cargo_host_flag) $(host_rust_features_flag)
295
296
$(HOST_RUST_LIBRARY_FILE): force-cargo-host-library-build
297
298
force-cargo-host-library-check:
299
$(call CARGO_CHECK) --lib $(cargo_host_flag) $(host_rust_features_flag)
300
else
301
force-cargo-host-library-check:
302
@true
303
endif # HOST_RUST_LIBRARY_FILE
304
305
ifdef RUST_PROGRAMS
306
307
GARBAGE_DIRS += $(RUST_TARGET)
308
309
force-cargo-program-build: $(RESFILE)
310
$(REPORT_BUILD)
311
$(call CARGO_BUILD) $(addprefix --bin ,$(RUST_CARGO_PROGRAMS)) $(cargo_target_flag) -- $(if $(RESFILE),-C link-arg=$(CURDIR)/$(RESFILE))
312
313
$(RUST_PROGRAMS): force-cargo-program-build
314
315
force-cargo-program-check:
316
$(call CARGO_CHECK) $(addprefix --bin ,$(RUST_CARGO_PROGRAMS)) $(cargo_target_flag)
317
else
318
force-cargo-program-check:
319
@true
320
endif # RUST_PROGRAMS
321
ifdef HOST_RUST_PROGRAMS
322
323
GARBAGE_DIRS += $(RUST_HOST_TARGET)
324
325
force-cargo-host-program-build:
326
$(REPORT_BUILD)
327
$(call CARGO_BUILD) $(addprefix --bin ,$(HOST_RUST_CARGO_PROGRAMS)) $(cargo_host_flag)
328
329
$(HOST_RUST_PROGRAMS): force-cargo-host-program-build
330
331
force-cargo-host-program-check:
332
$(REPORT_BUILD)
333
$(call CARGO_CHECK) $(addprefix --bin ,$(HOST_RUST_CARGO_PROGRAMS)) $(cargo_host_flag)
334
else
335
force-cargo-host-program-check:
336
@true
337
endif # HOST_RUST_PROGRAMS