From b7cc53c1acd88138886097502bdebe2b644c5ee0 Mon Sep 17 00:00:00 2001 From: Jonathan Schwender Date: Tue, 12 Apr 2022 12:14:58 +0200 Subject: [PATCH] Fix linking Rust targets on Macos 11 For Rust targets, Rust will invoke the linker and add (among others) the -lSystem flag. This works fine for the default linker, but if we use a custom linker (e.g. g++, or even /usr/bin/ld), then we must provide that path to the System library. Adding -L to RUSTFLAGS works fine for regular libraries, but fails for build-scripts (See https://github.com/rust-lang/cargo/issues/4423 ) because there is no way to set RUSTFLAGS for build scripts when cross-compiling. This is mostly an issue because we always specify the target, even when building for host, but that is already sufficient for the RUSTFLAGS to not apply to the build-script-build. Target specific RUSTFLAGS (i.e CARGO_TARGET__RUSTFLAGS also don't seem to affect buildscripts. Setting LIBRARY_PATH= applies to normal targets and build-scripts too, and seems to fix the issue. My previous fix probably only fixed the case where a rust library gets linked into a foreign library or executable. --- cmake/Corrosion.cmake | 10 ++++++++++ 1 file changed, 10 insertions(+) diff --git a/cmake/Corrosion.cmake b/cmake/Corrosion.cmake index 7c0497a6..da74f542 100644 --- a/cmake/Corrosion.cmake +++ b/cmake/Corrosion.cmake @@ -226,6 +226,15 @@ function(_add_cargo_build) set(cargo_target_option "$") set(target_artifact_dir "$") + # Rust will add `-lSystem` as a flag for the linker on macOS. Adding the -L flag via RUSTFLAGS only fixes the + # problem partially - buildscripts still break, since they won't receive the RUSTFLAGS. This seems to only be a + # problem if we specify the linker ourselves (which we do, since this is necessary for e.g. linking C++ code). + # We can however set `LIBRARY_PATH`, which is propagated to the build-script-build properly. + if(NOT CMAKE_CROSSCOMPILING AND CMAKE_SYSTEM_NAME STREQUAL "Darwin") + set(cargo_library_path "LIBRARY_PATH=/Library/Developer/CommandLineTools/SDKs/MacOSX.sdk/usr/lib") + elseif(CMAKE_CROSSCOMPILING AND CMAKE_HOST_SYSTEM_NAME STREQUAL "Darwin") + set(cargo_library_path "$") + endif() if(cargo_profile_name) set(cargo_profile "--profile=${cargo_profile_name}") @@ -321,6 +330,7 @@ function(_add_cargo_build) ${rustflags_genex_test} ${cargo_target_linker} ${corrosion_cc_rs_flags} + ${cargo_library_path} CORROSION_BUILD_DIR=${CMAKE_CURRENT_BINARY_DIR} CARGO_BUILD_RUSTC="${_CORROSION_RUSTC}" "${_CORROSION_CARGO}"