Bug 1709253 - Fix building rust programs. r=firefox-build-system-reviewers,mhentges

Differential Revision: https://phabricator.services.mozilla.com/D114212
This commit is contained in:
Mike Hommey 2021-05-04 20:45:43 +00:00
parent df45b126d1
commit 506594be8f
3 changed files with 63 additions and 1 deletions

23
build/win32/dummy_libs.py Normal file
View File

@ -0,0 +1,23 @@
# This Source Code Form is subject to the terms of the Mozilla Public
# License, v. 2.0. If a copy of the MPL was not distributed with this
# file, You can obtain one at http://mozilla.org/MPL/2.0/.
import os
import subprocess
from buildconfig import substs
def main(output, *other_libs):
output.close()
# ar doesn't like it when the file exists beforehand.
os.unlink(output.name)
libs = [output.name]
parent = os.path.dirname(output.name)
libs.extend(os.path.join(parent, l) for l in other_libs)
for lib in libs:
result = subprocess.run(
[substs["AR"]] + [f.replace("$@", lib) for f in substs["AR_FLAGS"]]
)
if result.returncode != 0:
return result.returncode
return 0

View File

@ -15,6 +15,17 @@ if CONFIG["ENABLE_TESTS"]:
NO_PGO = True
# See comment about *-windows-gnu targets in config/makefiles/rust.mk
if CONFIG["CC_TYPE"] == "clang":
GeneratedFile(
"libgcc.a",
"libgcc_eh.a",
"libpthread.a",
script="dummy_libs.py",
flags=["libgcc_eh.a", "libpthread.a"],
)
if CONFIG["WIN32_REDIST_DIR"] and CONFIG["COMPILE_ENVIRONMENT"]:
for f in ["MSVC_C_RUNTIME_DLL", "MSVC_CXX_RUNTIME_DLL"]:
FINAL_TARGET_FILES += ["%%%s/%s" % (CONFIG["WIN32_REDIST_DIR"], CONFIG[f])]

View File

@ -330,6 +330,34 @@ ifndef NATIVE_TSAN
$(TARGET_RECIPES): MOZ_CARGO_WRAP_LDFLAGS:=$(filter-out -fsanitize=cfi% -framework Cocoa -lobjc AudioToolbox ExceptionHandling -fprofile-%,$(LDFLAGS))
endif # NATIVE_TSAN
# When building programs with sanitizer, rustc links its own runtime, which
# conflicts with the one that passing -fsanitize=* to the linker would add.
ifneq (,$(filter -Zsanitizer=%,$(RUSTFLAGS)))
force-cargo-program-build: MOZ_CARGO_WRAP_LDFLAGS:=$(filter-out -fsanitize=%,$(MOZ_CARGO_WRAP_LDFLAGS))
endif
# Rustc assumes that *-windows-gnu targets build with mingw-gcc and manually
# add runtime libraries that don't exist with mingw-clang. We created dummy
# libraries in $(topobjdir)/build/win32, but that's not enough, because some
# of the wanted symbols that come from these libraries are available in a
# different library, that we add manually. We also need to avoid rustc
# passing -nodefaultlibs to clang so that it adds clang_rt.
ifeq (WINNT_clang,$(OS_ARCH)_$(CC_TYPE))
force-cargo-program-build: MOZ_CARGO_WRAP_LDFLAGS+=-L$(topobjdir)/build/win32 -lunwind
force-cargo-program-build: CARGO_RUSTCFLAGS += -C default-linker-libraries=yes
endif
# Rustc passes -nodefaultlibs to the linker (clang) on mac, which prevents
# clang from adding the necessary sanitizer runtimes when building with
# C/C++ sanitizer but without rust sanitizer.
ifeq (Darwin,$(OS_ARCH))
ifeq (,$(filter -Zsanitizer=%,$(RUSTFLAGS)))
ifneq (,$(filter -fsanitize=%,$(LDFLAGS)))
force-cargo-program-build: CARGO_RUSTCFLAGS += -C default-linker-libraries=yes
endif
endif
endif
$(HOST_RECIPES): MOZ_CARGO_WRAP_LDFLAGS:=$(HOST_LDFLAGS) $(WRAP_HOST_LINKER_LIBPATHS)
$(TARGET_RECIPES) $(HOST_RECIPES): MOZ_CARGO_WRAP_HOST_LDFLAGS:=$(HOST_LDFLAGS) $(WRAP_HOST_LINKER_LIBPATHS)
@ -426,7 +454,7 @@ ifdef RUST_PROGRAMS
force-cargo-program-build: $(call resfile,module)
$(REPORT_BUILD)
$(call CARGO_BUILD) $(addprefix --bin ,$(RUST_CARGO_PROGRAMS)) $(cargo_target_flag) -- $(addprefix -C link-arg=$(CURDIR)/,$(call resfile,module))
$(call CARGO_BUILD) $(addprefix --bin ,$(RUST_CARGO_PROGRAMS)) $(cargo_target_flag) -- $(addprefix -C link-arg=$(CURDIR)/,$(call resfile,module)) $(CARGO_RUSTCFLAGS)
$(RUST_PROGRAMS): force-cargo-program-build ;