Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Try to determine if --target was passed to Cargo #25

Merged
merged 3 commits into from
Aug 21, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[package]
name = "autocfg"
version = "1.0.0"
version = "1.0.1"
authors = ["Josh Stone <cuviper@gmail.com>"]
license = "Apache-2.0 OR MIT"
repository = "https://github.com/cuviper/autocfg"
Expand Down
3 changes: 3 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,9 @@ should only be used when the compiler supports it.

## Release Notes

- 1.0.1 (2020-08-20)
- Apply `RUSTFLAGS` for more `--target` scenarios, by @adamreichold.

- 1.0.0 (2020-01-08)
- 🎉 Release 1.0! 🎉 (no breaking changes)
- Add `probe_expression` and `emit_expression_cfg` to test arbitrary expressions.
Expand Down
30 changes: 28 additions & 2 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -157,6 +157,8 @@ impl AutoCfg {
let rustc: PathBuf = rustc.into();
let rustc_version = try!(Version::from_rustc(&rustc));

let target = env::var_os("TARGET");

// Sanity check the output directory
let dir = dir.into();
let meta = try!(fs::metadata(&dir).map_err(error::from_io));
Expand All @@ -170,7 +172,9 @@ impl AutoCfg {
// so for now we only apply RUSTFLAGS when cross-compiling an artifact.
//
// See https://github.com/cuviper/autocfg/pull/10#issuecomment-527575030.
let rustflags = if env::var_os("TARGET") != env::var_os("HOST") {
let rustflags = if target != env::var_os("HOST")
|| dir_contains_target(&target, &dir, env::var_os("CARGO_TARGET_DIR"))
{
env::var("RUSTFLAGS").ok().map(|rustflags| {
// This is meant to match how cargo handles the RUSTFLAG environment
// variable.
Expand All @@ -190,7 +194,7 @@ impl AutoCfg {
out_dir: dir,
rustc: rustc,
rustc_version: rustc_version,
target: env::var_os("TARGET"),
target: target,
no_std: false,
rustflags: rustflags,
};
Expand Down Expand Up @@ -410,3 +414,25 @@ fn mangle(s: &str) -> String {
})
.collect()
}

fn dir_contains_target(
target: &Option<OsString>,
dir: &PathBuf,
cargo_target_dir: Option<OsString>,
) -> bool {
target
.as_ref()
.and_then(|target| {
dir.to_str().and_then(|dir| {
let mut cargo_target_dir = cargo_target_dir
.map(PathBuf::from)
.unwrap_or_else(|| PathBuf::from("target"));
cargo_target_dir.push(target);

cargo_target_dir
.to_str()
.map(|cargo_target_dir| dir.contains(&cargo_target_dir))
})
})
.unwrap_or(false)
}
36 changes: 36 additions & 0 deletions src/tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -131,3 +131,39 @@ fn probe_constant() {
ac.assert_min(1, 33, ac.probe_constant("{ let x = 1 + 2 + 3; x * x }"));
ac.assert_min(1, 39, ac.probe_constant(r#""test".len()"#));
}

#[test]
fn dir_does_not_contain_target() {
assert!(!super::dir_contains_target(
&Some("x86_64-unknown-linux-gnu".into()),
&"/project/target/debug/build/project-ea75983148559682/out".into(),
None,
));
}

#[test]
fn dir_does_contain_target() {
assert!(super::dir_contains_target(
&Some("x86_64-unknown-linux-gnu".into()),
&"/project/target/x86_64-unknown-linux-gnu/debug/build/project-0147aca016480b9d/out".into(),
None,
));
}

#[test]
fn dir_does_not_contain_target_with_custom_target_dir() {
assert!(!super::dir_contains_target(
&Some("x86_64-unknown-linux-gnu".into()),
&"/project/custom/debug/build/project-ea75983148559682/out".into(),
Some("custom".into()),
));
}

#[test]
fn dir_does_contain_target_with_custom_target_dir() {
assert!(super::dir_contains_target(
&Some("x86_64-unknown-linux-gnu".into()),
&"/project/custom/x86_64-unknown-linux-gnu/debug/build/project-0147aca016480b9d/out".into(),
Some("custom".into()),
));
}