diff --git a/Cargo.lock b/Cargo.lock index ebb0e81..7000503 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -86,6 +86,20 @@ version = "1.1.2" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "c77df041dc383319cc661b428b6961a005db4d6808d5e12536931b1ca9556055" +[[package]] +name = "cargo-config2" +version = "0.1.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "44657e1d99d098d66a39f9d1519722c34461e5ecf84d5b9a818e4bbebeed977f" +dependencies = [ + "cfg-expr", + "home", + "once_cell", + "serde", + "shell-escape", + "toml_edit", +] + [[package]] name = "cargo-options" version = "0.5.3" @@ -100,14 +114,13 @@ name = "cargo-xwin" version = "0.13.5" dependencies = [ "anyhow", + "cargo-config2", "cargo-options", "clap", "dirs", "fs-err", "indicatif", "path-slash", - "serde", - "serde_json", "which", "xwin", ] @@ -129,6 +142,15 @@ dependencies = [ "uuid", ] +[[package]] +name = "cfg-expr" +version = "0.13.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "a327683d7499ecc47369531a679fe38acdd300e09bf8c852d08b1e10558622bd" +dependencies = [ + "smallvec", +] + [[package]] name = "cfg-if" version = "1.0.0" @@ -486,6 +508,12 @@ dependencies = [ "wasi", ] +[[package]] +name = "hashbrown" +version = "0.12.3" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "8a9ee70c43aaf417c914396645a0fa852624801b24ebb7ae78fe8272889ac888" + [[package]] name = "heck" version = "0.4.0" @@ -501,6 +529,15 @@ dependencies = [ "libc", ] +[[package]] +name = "home" +version = "0.5.4" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "747309b4b440c06d57b0b25f2aee03ee9b5e5397d288c60e21fc709bb98a7408" +dependencies = [ + "winapi", +] + [[package]] name = "idna" version = "0.3.0" @@ -511,6 +548,16 @@ dependencies = [ "unicode-normalization", ] +[[package]] +name = "indexmap" +version = "1.9.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "1885e79c1fc4b10f0e172c475f458b7f7b93061064d98c3293e98c5ba0c8b399" +dependencies = [ + "autocfg", + "hashbrown", +] + [[package]] name = "indicatif" version = "0.17.2" @@ -700,6 +747,15 @@ dependencies = [ "minimal-lexical", ] +[[package]] +name = "nom8" +version = "0.2.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "ae01545c9c7fc4486ab7debaf2aad7003ac19431791868fb2e8066df97fad2f8" +dependencies = [ + "memchr", +] + [[package]] name = "num_cpus" version = "1.15.0" @@ -1138,6 +1194,12 @@ dependencies = [ "lazy_static", ] +[[package]] +name = "shell-escape" +version = "0.1.5" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "45bb67a18fa91266cc7807181f62f9178a6873bfad7dc788c42e6430db40184f" + [[package]] name = "smallvec" version = "1.10.0" @@ -1266,6 +1328,28 @@ version = "0.1.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "cda74da7e1a664f795bb1f8a87ec406fb89a02522cf6e50620d016add6dbbf5c" +[[package]] +name = "toml_datetime" +version = "0.5.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "808b51e57d0ef8f71115d8f3a01e7d3750d01c79cac4b3eda910f4389fdf92fd" +dependencies = [ + "serde", +] + +[[package]] +name = "toml_edit" +version = "0.17.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "a34cc558345efd7e88b9eda9626df2138b80bb46a7606f695e751c892bc7dac6" +dependencies = [ + "indexmap", + "itertools", + "nom8", + "serde", + "toml_datetime", +] + [[package]] name = "tracing" version = "0.1.37" diff --git a/Cargo.toml b/Cargo.toml index 2500b31..67c1be5 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -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 } diff --git a/src/common.rs b/src/common.rs index affe88c..7d4d9c4 100644 --- a/src/common.rs +++ b/src/common.rs @@ -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}; @@ -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; @@ -543,67 +542,19 @@ fn default_build_target_from_config(workdir: &Path) -> Result> { Ok(Some(target.to_string())) } -#[derive(Debug, Clone, Deserialize)] -#[serde(untagged)] -enum Rustflags { - String(String), - Array(Vec), -} - -impl From 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, -} - -#[derive(Debug, Deserialize)] -struct CargoConfig { - build: Option, - #[serde(default)] - target: HashMap, -} - /// Get RUSTFLAGS in the following order: /// /// 1. `RUSTFLAGS` environment variable. -/// 2. `target..rustflags` config value, no support for `target..rustflags` yet -/// 3. `build.rustflags` config value +/// 2. `rustflags` cargo configuration fn get_rustflags(workdir: &Path, target: &str) -> Result> { 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), } } }