From 14096761697f5c1a0e8e8d1436fb461e086057ea Mon Sep 17 00:00:00 2001 From: messense Date: Mon, 9 Jan 2023 22:37:36 +0800 Subject: [PATCH] Respect `rustflags` settings in cargo configuration file --- Cargo.lock | 54 ++++++++++++++++++++++++++++++++++++++++++-------- Cargo.toml | 1 + src/compile.rs | 33 ++++++++++++++++++++++-------- 3 files changed, 72 insertions(+), 16 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index a116c570a..4d98b8d11 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -213,6 +213,20 @@ dependencies = [ "serde", ] +[[package]] +name = "cargo-config2" +version = "0.1.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "59a3dbc2202646fb5a83a0f0f45259be618f006862edc0d20938f247f2678706" +dependencies = [ + "cfg-expr", + "home", + "once_cell", + "serde", + "shell-escape", + "toml_edit", +] + [[package]] name = "cargo-options" version = "0.5.3" @@ -233,19 +247,18 @@ dependencies = [ [[package]] name = "cargo-xwin" -version = "0.13.5" +version = "0.13.6" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "570ef9c57470aa820aae02ec06456a9e2148149598c9927943ae8afba56ba322" +checksum = "ce9653c9f9630bbdb6c3566f9e77ad568e38eeb4c4849b0913a65aa5c43c12df" dependencies = [ "anyhow", + "cargo-config2", "cargo-options", "clap", "dirs", "fs-err", "indicatif", "path-slash", - "serde", - "serde_json", "which", "xwin", ] @@ -320,6 +333,15 @@ dependencies = [ "uuid", ] +[[package]] +name = "cfg-expr" +version = "0.13.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "a327683d7499ecc47369531a679fe38acdd300e09bf8c852d08b1e10558622bd" +dependencies = [ + "smallvec", +] + [[package]] name = "cfg-if" version = "1.0.0" @@ -1107,6 +1129,15 @@ dependencies = [ "digest 0.9.0", ] +[[package]] +name = "home" +version = "0.5.4" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "747309b4b440c06d57b0b25f2aee03ee9b5e5397d288c60e21fc709bb98a7408" +dependencies = [ + "winapi", +] + [[package]] name = "humantime" version = "2.1.0" @@ -1327,6 +1358,7 @@ dependencies = [ "anyhow", "base64", "bytesize", + "cargo-config2", "cargo-options", "cargo-xwin", "cargo-zigbuild", @@ -1823,9 +1855,9 @@ checksum = "b4596b6d070b27117e987119b4dac604f3c58cfb0b191112e24771b2faeac1a6" [[package]] name = "platform-info" -version = "1.0.1" +version = "1.0.2" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "4278b2b54a23c9a91d5bae9b09e21d566f8b23890491951160b05d64f29d1d8b" +checksum = "4e7c23cfae725ae06d9e43010153fa77bdfa8c827bf08fe4beeb2a3514e6be12" dependencies = [ "libc", "winapi", @@ -2042,9 +2074,9 @@ dependencies = [ [[package]] name = "regex" -version = "1.7.0" +version = "1.7.1" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "e076559ef8e241f2ae3479e36f97bd5741c0330689e217ad51ce2c76808b868a" +checksum = "48aaa5748ba571fb95cd2c85c09f629215d3a6ece942baa100950af03a34f733" dependencies = [ "aho-corasick", "memchr", @@ -2338,6 +2370,12 @@ dependencies = [ "lazy_static", ] +[[package]] +name = "shell-escape" +version = "0.1.5" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "45bb67a18fa91266cc7807181f62f9178a6873bfad7dc788c42e6430db40184f" + [[package]] name = "shlex" version = "1.1.0" diff --git a/Cargo.toml b/Cargo.toml index d0bc3c69b..a9d4f10a3 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -23,6 +23,7 @@ name = "maturin" anyhow = "1.0.63" base64 = "0.13.0" glob = "0.3.0" +cargo-config2 = "0.1.1" cargo_metadata = "0.15.2" cargo-options = "0.5.2" cbindgen = { version = "0.24.2", default-features = false } diff --git a/src/compile.rs b/src/compile.rs index 05d43151c..0cf593156 100644 --- a/src/compile.rs +++ b/src/compile.rs @@ -173,7 +173,10 @@ fn compile_target( } } - let mut rust_flags = env::var_os("RUSTFLAGS"); + let target_triple = target.target_triple(); + + let manifest_dir = context.manifest_path.parent().unwrap(); + let mut rust_flags = get_rustflags(manifest_dir, target_triple)?; // We need to pass --bin / --lib match bridge_model { @@ -189,10 +192,11 @@ fn compile_target( // We must only do this for libraries as it breaks binaries // For some reason this value is ignored when passed as rustc argument if context.target.is_musl_target() { - debug!("Setting `-C target-features=-crt-static` for musl dylib"); - rust_flags - .get_or_insert_with(Default::default) - .push(" -C target-feature=-crt-static"); + let flags = rust_flags.get_or_insert_with(Default::default); + if !flags.contains("target-feature=-crt-static") { + debug!("Setting `-C target-features=-crt-static` for musl dylib"); + flags.push_str(" -C target-feature=-crt-static"); + } } } } @@ -226,8 +230,9 @@ fn compile_target( } else if target.is_emscripten() { let flags = rust_flags.get_or_insert_with(Default::default); // Allow user to override these default flags - if !flags.to_string_lossy().contains("link-native-libraries") { - flags.push(" -Z link-native-libraries=no"); + if !flags.contains("link-native-libraries") { + debug!("Setting `-Z link-native-libraries=no` for Emscripten"); + flags.push_str(" -Z link-native-libraries=no"); } let mut emscripten_args = Vec::new(); // Allow user to override these default settings @@ -260,7 +265,6 @@ fn compile_target( .extend(["-C".to_string(), "link-arg=-s".to_string()]); } - let target_triple = target.target_triple(); let mut build_command = if target.is_msvc() && target.cross_compiling() { #[cfg(feature = "xwin")] { @@ -505,6 +509,19 @@ fn compile_target( Ok(artifacts) } +/// Get `RUSTFLAGS` in the following order: +/// +/// 1. `RUSTFLAGS` environment variable +/// 2. `rustflags` cargo configuration +fn get_rustflags(workdir: &Path, target: &str) -> Result> { + let cargo_config = cargo_config2::Config::load_with_cwd(workdir)?; + let rustflags = cargo_config.rustflags(target)?; + match rustflags { + Some(rustflags) => Ok(Some(rustflags.encode_space_separated()?)), + None => Ok(None), + } +} + /// Checks that the native library contains a function called `PyInit_` and warns /// if it's missing. ///