Skip to content

Commit

Permalink
If target triple cannot be determined from toolchain, default to host…
Browse files Browse the repository at this point in the history
… triple
  • Loading branch information
smoelius committed Jun 13, 2022
1 parent 6cdc2c3 commit ff4a069
Show file tree
Hide file tree
Showing 4 changed files with 30 additions and 23 deletions.
3 changes: 3 additions & 0 deletions dylint-link/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,9 @@ toml_edit = "0.14.4"

dylint_internal = { version = "=2.0.5", path = "../internal" }

[build-dependencies]
dylint_internal = { version = "=2.0.5", path = "../internal" }

[dev-dependencies]
assert_cmd = "2.0.4"
curl = "0.4.43"
Expand Down
5 changes: 5 additions & 0 deletions dylint-link/build.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
use dylint_internal::env;

fn main() {
println!("cargo:rustc-env=TARGET={}", env::var(env::TARGET).unwrap());
}
44 changes: 21 additions & 23 deletions dylint-link/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -33,31 +33,29 @@ fn main() -> Result<()> {

fn linker() -> Result<PathBuf> {
let rustup_toolchain = env::var(env::RUSTUP_TOOLCHAIN)?;
let target = parse_toolchain(&rustup_toolchain)
.map_or_else(|| env!("TARGET").to_owned(), |(_, target)| target);
let cargo_home = cargo_home()?;
let config_toml = cargo_home.join("config.toml");
if_chain! {
if let Some((_, target)) = parse_toolchain(&rustup_toolchain);
if config_toml.is_file();
then {
let file = read_to_string(&config_toml).with_context(|| {
format!(
"`read_to_string` failed for `{}`",
config_toml.to_string_lossy()
)
})?;
let document = file.parse::<Document>()?;
document
.as_table()
.get("target")
.and_then(Item::as_table)
.and_then(|table| table.get(&target))
.and_then(Item::as_table)
.and_then(|table| table.get("linker"))
.and_then(Item::as_str)
.map_or_else(default_linker, |s| Ok(PathBuf::from(s)))
} else {
default_linker()
}
if config_toml.is_file() {
let file = read_to_string(&config_toml).with_context(|| {
format!(
"`read_to_string` failed for `{}`",
config_toml.to_string_lossy()
)
})?;
let document = file.parse::<Document>()?;
document
.as_table()
.get("target")
.and_then(Item::as_table)
.and_then(|table| table.get(&target))
.and_then(Item::as_table)
.and_then(|table| table.get("linker"))
.and_then(Item::as_str)
.map_or_else(default_linker, |s| Ok(PathBuf::from(s)))
} else {
default_linker()
}
}

Expand Down
1 change: 1 addition & 0 deletions internal/src/env.rs
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ pub const RUST_BACKTRACE: &str = "RUST_BACKTRACE";
pub const RUSTFLAGS: &str = "RUSTFLAGS";
pub const RUSTUP_HOME: &str = "RUSTUP_HOME";
pub const RUSTUP_TOOLCHAIN: &str = "RUSTUP_TOOLCHAIN";
pub const TARGET: &str = "TARGET";

#[must_use]
pub fn enabled(key: &str) -> bool {
Expand Down

0 comments on commit ff4a069

Please sign in to comment.