Skip to content

Commit

Permalink
Build cargo rustc command and env vars with cargo-options crate
Browse files Browse the repository at this point in the history
  • Loading branch information
messense committed Jun 18, 2022
1 parent 13ced58 commit 86b79aa
Show file tree
Hide file tree
Showing 4 changed files with 61 additions and 40 deletions.
1 change: 1 addition & 0 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 @@ -24,6 +24,7 @@ base64 = "0.13.0"
bytesize = "1.0.1"
glob = "0.3.0"
cargo_metadata = "0.14.0"
cargo-options = "0.2.0"
cargo-zigbuild = "0.10.1"
cargo-xwin = { version = "0.9.1", default-features = false }
cbindgen = { version = "0.24.2", default-features = false }
Expand Down
34 changes: 34 additions & 0 deletions src/build_options.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1174,6 +1174,40 @@ fn extract_cargo_metadata_args(cargo_options: &CargoOptions) -> Result<Vec<Strin
Ok(cargo_metadata_extra_args)
}

impl From<CargoOptions> for cargo_options::Rustc {
fn from(cargo: CargoOptions) -> Self {
cargo_options::Rustc {
common: cargo_options::CommonOptions {
quiet: cargo.quiet,
jobs: cargo.jobs,
profile: cargo.profile,
features: cargo.features,
all_features: cargo.all_features,
no_default_features: cargo.no_default_features,
target: match cargo.target {
Some(target) => vec![target],
None => Vec::new(),
},
target_dir: cargo.target_dir,
manifest_path: cargo.manifest_path,
ignore_rust_version: cargo.ignore_rust_version,
verbose: cargo.verbose,
color: cargo.color,
frozen: cargo.frozen,
locked: cargo.locked,
offline: cargo.offline,
config: cargo.config,
unstable_flags: cargo.unstable_flags,
timings: cargo.timings,
..Default::default()
},
future_incompat_report: cargo.future_incompat_report,
args: cargo.args,
..Default::default()
}
}
}

#[cfg(test)]
mod test {
use std::path::Path;
Expand Down
65 changes: 25 additions & 40 deletions src/compile.rs
Original file line number Diff line number Diff line change
Expand Up @@ -140,24 +140,23 @@ fn compile_target(
) -> Result<HashMap<String, PathBuf>> {
let target = &context.target;

let mut shared_args = Vec::new();
let mut rustc_args: Vec<&str> = context
.cargo_options
.args
.iter()
.map(String::as_str)
.collect();
let mut cargo_rustc: cargo_options::Rustc = context.cargo_options.clone().into();
cargo_rustc.message_format = vec!["json".to_string()];

// --release and --profile are conflicting options
if context.release && cargo_rustc.profile.is_none() {
cargo_rustc.release = true;
}

let mut rust_flags = env::var_os("RUSTFLAGS");

// We need to pass --bin / --lib to set the rustc extra args later
// We need to pass --bin / --lib
match bindings_crate {
BridgeModel::Bin(..) => {
shared_args.push("--bin");
shared_args.push(binding_target.name.as_str());
cargo_rustc.bin.push(binding_target.name.clone());
}
BridgeModel::Cffi | BridgeModel::Bindings(..) | BridgeModel::BindingsAbi3(..) => {
shared_args.push("--lib");
cargo_rustc.lib = true;
// 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
Expand Down Expand Up @@ -185,39 +184,32 @@ fn compile_target(
// https://github.com/PyO3/pyo3/issues/88#issuecomment-337744403
if target.is_macos() {
if let BridgeModel::Bindings(..) | BridgeModel::BindingsAbi3(..) = bindings_crate {
let mac_args = &[
"-C",
"link-arg=-undefined",
"-C",
"link-arg=dynamic_lookup",
"-C",
&macos_dylib_install_name,
let mac_args = [
"-C".to_string(),
"link-arg=-undefined".to_string(),
"-C".to_string(),
"link-arg=dynamic_lookup".to_string(),
"-C".to_string(),
macos_dylib_install_name,
];
rustc_args.extend(mac_args);
cargo_rustc.args.extend(mac_args);
}
}

if context.strip {
rustc_args.extend(&["-C", "link-arg=-s"]);
cargo_rustc
.args
.extend(["-C".to_string(), "link-arg=-s".to_string()]);
}

let cargo_args = vec!["--message-format", "json"];

let build_args: Vec<_> = cargo_args
.iter()
.chain(&shared_args)
.chain(&["--"])
.chain(&rustc_args)
.collect();

let target_triple = target.target_triple();
let mut build_command = if target.is_msvc() && target.cross_compiling() {
let mut build = cargo_xwin::Rustc::new(Some(context.manifest_path.clone()));
let mut build = cargo_xwin::Rustc::from(cargo_rustc);

build.target = vec![target_triple.to_string()];
build.build_command()?
} else {
let mut build = cargo_zigbuild::Rustc::new(Some(context.manifest_path.clone()));
let mut build = cargo_zigbuild::Rustc::from(cargo_rustc);
if !context.zig {
build.disable_zig_linker = true;
if target.user_specified {
Expand Down Expand Up @@ -252,7 +244,6 @@ fn compile_target(
}

build_command
.args(&build_args)
// We need to capture the json messages
.stdout(Stdio::piped())
// We can't get colored human and json messages from rustc as they are mutually exclusive,
Expand Down Expand Up @@ -375,16 +366,10 @@ fn compile_target(
.expect("Failed to wait on cargo child process");

if !status.success() {
let command_str = build_args
.iter()
.map(ToString::to_string)
.collect::<Vec<String>>()
.join(" ");
bail!(
r#"Cargo build finished with "{}": `cargo rustc --manifest-path {} {}`"#,
r#"Cargo build finished with "{}": `{:?}`"#,
status,
context.manifest_path.display(),
command_str
build_command,
)
}

Expand Down

0 comments on commit 86b79aa

Please sign in to comment.