From 01e3c3a9d0cc466e4d0057722247e2b1bcb93ecb Mon Sep 17 00:00:00 2001 From: messense Date: Sun, 8 Jan 2023 20:49:57 +0800 Subject: [PATCH] Add support for reading `rustflags` from Cargo config file --- Cargo.lock | 97 +++++++++++++++++++-------------------------------- Cargo.toml | 2 ++ src/common.rs | 66 ++++++++++++++++++++++++++++++----- 3 files changed, 95 insertions(+), 70 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index 76c6678..4ab2ae9 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -82,9 +82,9 @@ dependencies = [ [[package]] name = "camino" -version = "1.1.1" +version = "1.1.2" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "88ad0e1e3e88dd237a156ab9f571021b8a158caa0ae44b1968a241efb5144c1e" +checksum = "c77df041dc383319cc661b428b6961a005db4d6808d5e12536931b1ca9556055" [[package]] name = "cargo-options" @@ -106,6 +106,8 @@ dependencies = [ "fs-err", "indicatif", "path-slash", + "serde", + "serde_json", "which", "xwin", ] @@ -133,17 +135,11 @@ version = "1.0.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "baf1de4339761588bc0619e3cbc0120ee582ebb74b53b4efbf79117bd2da40fd" -[[package]] -name = "chunked_transfer" -version = "1.4.0" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "fff857943da45f546682664a79488be82e69e43c1a7a2307679ab9afb3a66d2e" - [[package]] name = "clap" -version = "4.0.29" +version = "4.0.32" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "4d63b9e9c07271b9957ad22c173bae2a4d9a81127680962039296abcd2f8251d" +checksum = "a7db700bc935f9e43e88d00b0850dae18a63773cfbec6d8e070fccf7fef89a39" dependencies = [ "bitflags", "clap_derive", @@ -152,7 +148,7 @@ dependencies = [ "once_cell", "strsim", "termcolor", - "terminal_size 0.2.3", + "terminal_size", ] [[package]] @@ -189,16 +185,15 @@ dependencies = [ [[package]] name = "console" -version = "0.15.2" +version = "0.15.4" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "c050367d967ced717c04b65d8c619d863ef9292ce0c5760028655a2fb298718c" +checksum = "c9b6515d269224923b26b5febea2ed42b2d5f2ce37284a4dd670fedd6cb8347a" dependencies = [ "encode_unicode", "lazy_static", "libc", - "terminal_size 0.1.17", "unicode-width", - "winapi", + "windows-sys 0.42.0", ] [[package]] @@ -497,15 +492,6 @@ version = "0.4.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "2540771e65fc8cb83cd6e8a237f70c319bd5c29f78ed1084ba5d50eeac86f7f9" -[[package]] -name = "hermit-abi" -version = "0.1.19" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "62b467343b94ba476dcb2500d242dadbb39557df889310ac77c5d99100aaac33" -dependencies = [ - "libc", -] - [[package]] name = "hermit-abi" version = "0.2.6" @@ -558,11 +544,11 @@ dependencies = [ [[package]] name = "is-terminal" -version = "0.4.1" +version = "0.4.2" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "927609f78c2913a6f6ac3c27a4fe87f43e2a35367c0c4b0f8265e8f49a104330" +checksum = "28dfb6c8100ccc63462345b67d1bbc3679177c75ee4bf59bf29c8b1d110b8189" dependencies = [ - "hermit-abi 0.2.6", + "hermit-abi", "io-lifetimes", "rustix", "windows-sys 0.42.0", @@ -600,9 +586,9 @@ checksum = "e2abad23fbc42b3700f2f279844dc832adb2b2eb069b2df918f455c4e18cc646" [[package]] name = "libc" -version = "0.2.138" +version = "0.2.139" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "db6d7e329c562c5dfab7a46a2afabc8b987ab9a4834c9d1ca04dc54c1546cef8" +checksum = "201de327520df007757c1f0adce6e827fe8562fbc28bfd9c15571c66ca1f5f79" [[package]] name = "linux-raw-sys" @@ -706,9 +692,9 @@ dependencies = [ [[package]] name = "nom" -version = "7.1.1" +version = "7.1.2" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "a8903e5a29a317527874d0402f867152a3d21c908bb0b933e416c65e301d4c36" +checksum = "e5507769c4919c998e69e49c839d9dc6e693ede4cc4290d6ad8b41d4f09c548c" dependencies = [ "memchr", "minimal-lexical", @@ -716,11 +702,11 @@ dependencies = [ [[package]] name = "num_cpus" -version = "1.14.0" +version = "1.15.0" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "f6058e64324c71e02bc2b150e4f3bc8286db6c83092132ffa3f6b1eab0f9def5" +checksum = "0fac9e2da13b5eb447a6ce3d392f23a29d8694bff781bf03a16cd9ac8697593b" dependencies = [ - "hermit-abi 0.1.19", + "hermit-abi", "libc", ] @@ -732,15 +718,15 @@ checksum = "830b246a0e5f20af87141b25c173cd1b609bd7779a4617d6ec582abaf90870f3" [[package]] name = "once_cell" -version = "1.16.0" +version = "1.17.0" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "86f0b0d4bf799edbc74508c1e8bf170ff5f41238e5f8225603ca7caaae2b7860" +checksum = "6f61fba1741ea2b3d6a1e3178721804bb716a68a6aeba1149b5d52e3d464ea66" [[package]] name = "openssl" -version = "0.10.44" +version = "0.10.45" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "29d971fd5722fec23977260f6e81aa67d2f22cadbdc2aa049f1022d9a3be1566" +checksum = "b102428fd03bc5edf97f62620f7298614c45cedf287c271e7ed450bbaf83f2e1" dependencies = [ "bitflags", "cfg-if", @@ -779,9 +765,9 @@ dependencies = [ [[package]] name = "openssl-sys" -version = "0.9.79" +version = "0.9.80" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "5454462c0eced1e97f2ec09036abc8da362e66802f66fd20f86854d9d8cbcbc4" +checksum = "23bbbf7854cd45b83958ebe919f0e8e516793727652e27fda10a8384cfc790b7" dependencies = [ "autocfg", "cc", @@ -846,9 +832,9 @@ checksum = "6ac9a59f73473f1b8d852421e59e64809f025994837ef743615c6d0c5b305160" [[package]] name = "portable-atomic" -version = "0.3.18" +version = "0.3.19" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "81bdd679d533107e090c2704a35982fc06302e30898e63ffa26a81155c012e92" +checksum = "26f6a7b87c2e435a3241addceeeff740ff8b7e76b74c13bf9acb17fa454ea00b" [[package]] name = "ppv-lite86" @@ -1022,9 +1008,9 @@ dependencies = [ [[package]] name = "rustix" -version = "0.36.5" +version = "0.36.6" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "a3807b5d10909833d3e9acd1eb5fb988f79376ff10fce42937de71a449c4c588" +checksum = "4feacf7db682c6c329c4ede12649cd36ecab0f3be5b7d74e6a20304725db4549" dependencies = [ "bitflags", "errno", @@ -1103,18 +1089,18 @@ dependencies = [ [[package]] name = "serde" -version = "1.0.151" +version = "1.0.152" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "97fed41fc1a24994d044e6db6935e69511a1153b52c15eb42493b26fa87feba0" +checksum = "bb7d1f0d3021d347a83e556fc4683dea2ea09d87bccdf88ff5c12545d89d5efb" dependencies = [ "serde_derive", ] [[package]] name = "serde_derive" -version = "1.0.151" +version = "1.0.152" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "255abe9a125a985c05190d687b320c12f9b1f0b99445e608c21ba0782c719ad8" +checksum = "af487d118eecd09402d70a5d72551860e788df87b464af30e5ea6a38c75c541e" dependencies = [ "proc-macro2", "quote", @@ -1210,16 +1196,6 @@ dependencies = [ "winapi-util", ] -[[package]] -name = "terminal_size" -version = "0.1.17" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "633c1a546cee861a1a6d0dc69ebeca693bf4296661ba7852b9d21d159e0506df" -dependencies = [ - "libc", - "winapi", -] - [[package]] name = "terminal_size" version = "0.2.3" @@ -1403,12 +1379,11 @@ checksum = "a156c684c91ea7d62626509bce3cb4e1d9ed5c4d978f7b4352658f96a4c26b4a" [[package]] name = "ureq" -version = "2.5.0" +version = "2.6.1" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "b97acb4c28a254fd7a4aeec976c46a7fa404eac4d7c134b30c75144846d7cb8f" +checksum = "733b5ad78377302af52c0dbcb2623d78fe50e4b3bf215948ff29e9ee031d8566" dependencies = [ "base64", - "chunked_transfer", "flate2", "log", "native-tls", diff --git a/Cargo.toml b/Cargo.toml index a90a6de..d02582c 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -19,6 +19,8 @@ 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 dcc2ba4..ecefb79 100644 --- a/src/common.rs +++ b/src/common.rs @@ -1,4 +1,4 @@ -use std::collections::HashSet; +use std::collections::{HashMap, HashSet}; use std::convert::TryInto; use std::env; use std::path::{Path, PathBuf}; @@ -12,6 +12,7 @@ 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; @@ -93,14 +94,14 @@ impl XWinOptions { let cache_dir = xwin_cache_dir.parent().unwrap(); env_paths.push(cache_dir.to_path_buf()); + let workdir = cargo + .manifest_path + .as_ref() + .and_then(|p| p.parent().map(|x| x.to_path_buf())) + .or_else(|| env::current_dir().ok()) + .unwrap(); let mut targets = cargo.target.clone(); if targets.is_empty() { - let workdir = cargo - .manifest_path - .as_ref() - .and_then(|p| p.parent().map(|x| x.to_path_buf())) - .or_else(|| env::current_dir().ok()) - .unwrap(); if let Some(build_target) = default_build_target_from_config(&workdir)? { // if no target is specified, use the default build target // Note that this is required, otherwise it may fail with link errors @@ -196,8 +197,8 @@ impl XWinOptions { _ => target_arch, }; - let mut rustflags = env::var_os("RUSTFLAGS").unwrap_or_default(); - rustflags.push(format!( + let mut rustflags = get_rustflags(&workdir, target)?.unwrap_or_default(); + rustflags.push_str(&format!( " -C linker-flavor=lld-link -Lnative={dir}/crt/lib/{arch} -Lnative={dir}/sdk/lib/um/{arch} -Lnative={dir}/sdk/lib/ucrt/{arch}", dir = xwin_cache_dir.display(), arch = xwin_arch, @@ -541,3 +542,50 @@ fn default_build_target_from_config(workdir: &Path) -> Result> { let target = stdout.trim().trim_matches('"'); Ok(Some(target.to_string())) } + +#[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 +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()) + } + _ => Ok(cargo_config.build.and_then(|c| c.rustflags)), + } + } + } +}