Skip to content

Commit

Permalink
Respect rustflags settings in cargo configuration file
Browse files Browse the repository at this point in the history
  • Loading branch information
messense committed Jan 9, 2023
1 parent 8f6630a commit 1409676
Show file tree
Hide file tree
Showing 3 changed files with 72 additions and 16 deletions.
54 changes: 46 additions & 8 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -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 }
Expand Down
33 changes: 25 additions & 8 deletions src/compile.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand All @@ -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");
}
}
}
}
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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")]
{
Expand Down Expand Up @@ -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<Option<String>> {
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_<module name>` and warns
/// if it's missing.
///
Expand Down

0 comments on commit 1409676

Please sign in to comment.