Skip to content

Commit

Permalink
Add support for reading rustflags from Cargo config file
Browse files Browse the repository at this point in the history
  • Loading branch information
messense committed Jan 8, 2023
1 parent 9c5b2a1 commit a9c5ad6
Show file tree
Hide file tree
Showing 3 changed files with 93 additions and 70 deletions.
97 changes: 36 additions & 61 deletions Cargo.lock

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

2 changes: 2 additions & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -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 }

Expand Down
64 changes: 55 additions & 9 deletions src/common.rs
Original file line number Diff line number Diff line change
@@ -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};
Expand All @@ -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;

Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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,
Expand Down Expand Up @@ -541,3 +542,48 @@ fn default_build_target_from_config(workdir: &Path) -> Result<Option<String>> {
let target = stdout.trim().trim_matches('"');
Ok(Some(target.to_string()))
}

#[derive(Debug, Deserialize)]
struct CargoConfigRustflags {
rustflags: String,
}

#[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
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(target_config) => Ok(Some(target_config.rustflags.clone())),
None => Ok(cargo_config.build.map(|c| c.rustflags)),
}
}
}
}

0 comments on commit a9c5ad6

Please sign in to comment.