Skip to content

Commit

Permalink
Merge #1405
Browse files Browse the repository at this point in the history
1405: Respect `rustflags` settings in cargo configuration file r=messense a=messense

https://github.com/taiki-e/cargo-config2

`CARGO_ENCODED_RUSTFLAGS` requires Rust 1.55+ so technically it's a breaking change even though our MSRV is 1.62 we can actually build packages with older versions of Rust.

Co-authored-by: messense <messense@icloud.com>
  • Loading branch information
bors[bot] and messense authored Jan 11, 2023
2 parents 5a2ea22 + bc631f1 commit cd02ecf
Show file tree
Hide file tree
Showing 4 changed files with 64 additions and 15 deletions.
46 changes: 42 additions & 4 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
1 change: 1 addition & 0 deletions Changelog.md
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
* **Breaking Change**: Remove deprecated `python-source` option in `Cargo.toml` in [#1335](https://github.com/PyO3/maturin/pull/1335)
* **Breaking Change**: Turn `patchelf` version warning into a hard error in [#1335](https://github.com/PyO3/maturin/pull/1335)
* **Breaking Change**: [`uniffi_bindgen` CLI](https://mozilla.github.io/uniffi-rs/tutorial/Prerequisites.html#the-uniffi-bindgen-cli-tool) is required for building `uniffi` bindings wheels in [#1352](https://github.com/PyO3/maturin/pull/1352)
* Respect `rustflags` settings in cargo configuration file in [#1405](https://github.com/PyO3/maturin/pull/1405)

## [0.14.9] - 2023-01-10

Expand Down
31 changes: 20 additions & 11 deletions src/compile.rs
Original file line number Diff line number Diff line change
Expand Up @@ -173,7 +173,12 @@ 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 rustflags = cargo_config2::Config::load_with_cwd(manifest_dir)?
.rustflags(target_triple)?
.unwrap_or_default();

// We need to pass --bin / --lib
match bridge_model {
Expand All @@ -188,11 +193,15 @@ fn compile_target(
// https://github.com/rust-lang/rust/issues/59302#issue-422994250
// 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() {
if context.target.is_musl_target()
&& !rustflags
.flags
.iter()
.any(|f| f == "target-feature=-crt-static")
{
debug!("Setting `-C target-features=-crt-static` for musl dylib");
rust_flags
.get_or_insert_with(Default::default)
.push(" -C target-feature=-crt-static");
rustflags.push("-C");
rustflags.push("target-feature=-crt-static");
}
}
}
Expand Down Expand Up @@ -224,10 +233,11 @@ fn compile_target(
cargo_rustc.args.extend(mac_args);
}
} 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 !rustflags.flags.iter().any(|f| f == "link-native-libraries") {
debug!("Setting `-Z link-native-libraries=no` for Emscripten");
rustflags.push("-Z");
rustflags.push("link-native-libraries=no");
}
let mut emscripten_args = Vec::new();
// Allow user to override these default settings
Expand Down Expand Up @@ -260,7 +270,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 @@ -332,8 +341,8 @@ fn compile_target(
// but forwarding stderr is still useful in case there some non-json error
.stderr(Stdio::inherit());

if let Some(flags) = rust_flags {
build_command.env("RUSTFLAGS", flags);
if !rustflags.flags.is_empty() {
build_command.env("CARGO_ENCODED_RUSTFLAGS", rustflags.encode()?);
}

if let BridgeModel::BindingsAbi3(_, _) = bridge_model {
Expand Down

0 comments on commit cd02ecf

Please sign in to comment.