Skip to content

Commit

Permalink
Merge pull request #1 from dtolnay/envflags
Browse files Browse the repository at this point in the history
Propagate encoded flags from cargo environment
  • Loading branch information
dtolnay committed Sep 27, 2023
2 parents 743b29c + 7b303d2 commit ac49ae4
Showing 1 changed file with 28 additions and 3 deletions.
31 changes: 28 additions & 3 deletions src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -139,26 +139,46 @@ fn do_main() -> Result<()> {
cargo_rustdoc.arg(default_target);
}

let mut rustflags = metadata.rustc_args.clone();
if let Some(encoded_rustflags) = env::var_os("CARGO_ENCODED_RUSTFLAGS") {
if let Some(encoded_rustflags) = encoded_rustflags.to_str() {
rustflags.splice(0..0, encoded_rustflags.split('\x1f').map(str::to_owned));
}
} else if let Some(env_rustflags) = env::var_os("RUSTFLAGS") {
if let Some(env_rustflags) = env_rustflags.to_str() {
rustflags.splice(0..0, env_rustflags.split_whitespace().map(str::to_owned));
}
}

cargo_rustdoc.arg("--config");
cargo_rustdoc.arg(format!(
"build.rustflags={}",
toml::Value::try_from(&metadata.rustc_args).unwrap(),
toml::Value::try_from(&rustflags).unwrap(),
));

cargo_rustdoc.arg("--config");
cargo_rustdoc.arg(format!(
"host.rustflags={}",
toml::Value::try_from(&metadata.rustc_args).unwrap(),
toml::Value::try_from(&rustflags).unwrap(),
));

let mut rustdocflags = metadata.rustdoc_args.clone();
rustdocflags.insert(0, "-Zunstable-options".to_owned());
if let Some(encoded_rustdocflags) = env::var_os("CARGO_ENCODED_RUSTDOCFLAGS") {
if let Some(encoded_rustdocflags) = encoded_rustdocflags.to_str() {
rustdocflags.splice(1..1, encoded_rustdocflags.split('\x1f').map(str::to_owned));
}
} else if let Some(env_rustdocflags) = env::var_os("RUSTDOCFLAGS") {
if let Some(env_rustdocflags) = env_rustdocflags.to_str() {
rustdocflags.splice(1..1, env_rustdocflags.split_whitespace().map(str::to_owned));
}
}
rustdocflags.push("--extern-html-root-takes-precedence".to_owned());

cargo_rustdoc.arg("--config");
cargo_rustdoc.arg(format!(
"build.rustdocflags={}",
toml::Value::try_from(rustdocflags).unwrap(),
toml::Value::try_from(&rustdocflags).unwrap(),
));

cargo_rustdoc.arg("--config");
Expand All @@ -184,6 +204,11 @@ fn do_main() -> Result<()> {
cargo_rustdoc.arg("--verbose");
}

cargo_rustdoc.env_remove("RUSTFLAGS");
cargo_rustdoc.env_remove("RUSTDOCFLAGS");
cargo_rustdoc.env_remove("CARGO_ENCODED_RUSTFLAGS");
cargo_rustdoc.env_remove("CARGO_ENCODED_RUSTDOCFLAGS");

let status = cargo_rustdoc.status()?;
if !status.success() {
process::exit(status.code().unwrap_or(1));
Expand Down

0 comments on commit ac49ae4

Please sign in to comment.