Skip to content

Commit

Permalink
Merge pull request #39 from rust-cross/cargo-config2
Browse files Browse the repository at this point in the history
Parse `rustflags` config using `cargo-config2`
  • Loading branch information
messense authored Jan 9, 2023
2 parents d66a9c5 + ccd9ca7 commit 3664e87
Show file tree
Hide file tree
Showing 3 changed files with 94 additions and 60 deletions.
88 changes: 86 additions & 2 deletions Cargo.lock

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

3 changes: 1 addition & 2 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -13,14 +13,13 @@ rust-version = "1.62"

[dependencies]
anyhow = "1.0.53"
cargo-config2 = "0.1.0"
cargo-options = "0.5.2"
clap = { version = "4.0.0", features = ["derive", "env", "wrap_help"] }
dirs = "4.0.0"
fs-err = "2.7.0"
indicatif = "0.17.2"
path-slash = "0.2.0"
serde = { version = "1.0.152", features = ["derive"] }
serde_json = "1.0.91"
which = "4.2.4"
xwin = { version = "0.2.9", default-features = false }

Expand Down
63 changes: 7 additions & 56 deletions src/common.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
use std::collections::{HashMap, HashSet};
use std::collections::HashSet;
use std::convert::TryInto;
use std::env;
use std::path::{Path, PathBuf};
Expand All @@ -12,7 +12,6 @@ use clap::{
use fs_err as fs;
use indicatif::{MultiProgress, ProgressBar, ProgressStyle};
use path_slash::PathExt;
use serde::Deserialize;
use which::{which, which_in};
use xwin::util::ProgressTarget;

Expand Down Expand Up @@ -543,67 +542,19 @@ fn default_build_target_from_config(workdir: &Path) -> Result<Option<String>> {
Ok(Some(target.to_string()))
}

#[derive(Debug, Clone, Deserialize)]
#[serde(untagged)]
enum Rustflags {
String(String),
Array(Vec<String>),
}

impl From<Rustflags> for String {
fn from(rustflags: Rustflags) -> String {
match rustflags {
Rustflags::String(flags) => flags,
Rustflags::Array(flags) => flags.join(" "),
}
}
}

#[derive(Debug, Deserialize)]
struct CargoConfigRustflags {
rustflags: Option<Rustflags>,
}

#[derive(Debug, Deserialize)]
struct CargoConfig {
build: Option<CargoConfigRustflags>,
#[serde(default)]
target: HashMap<String, CargoConfigRustflags>,
}

/// Get RUSTFLAGS in the following order:
///
/// 1. `RUSTFLAGS` environment variable.
/// 2. `target.<triple>.rustflags` config value, no support for `target.<cfg>.rustflags` yet
/// 3. `build.rustflags` config value
/// 2. `rustflags` cargo configuration
fn get_rustflags(workdir: &Path, target: &str) -> Result<Option<String>> {
match env::var("RUSTFLAGS") {
Ok(flags) => Ok(Some(flags)),
Err(_) => {
let output = Command::new("cargo")
.current_dir(workdir)
.args([
"config",
"get",
"-Z",
"unstable-options",
"--format",
"json",
])
.env("__CARGO_TEST_CHANNEL_OVERRIDE_DO_NOT_USE_THIS", "nightly")
.output()?;
if !output.status.success() {
return Ok(None);
}
let cargo_config: CargoConfig = serde_json::from_slice(&output.stdout)?;
match cargo_config.target.get(target) {
Some(CargoConfigRustflags { rustflags }) if rustflags.is_some() => {
Ok(rustflags.clone().map(|flags| flags.into()))
}
_ => Ok(cargo_config
.build
.and_then(|c| c.rustflags)
.map(|flags| flags.into())),
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),
}
}
}
Expand Down

0 comments on commit 3664e87

Please sign in to comment.