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

Improve default_target to respect current toolchain and config #365

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
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
191 changes: 108 additions & 83 deletions Cargo.lock

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

2 changes: 1 addition & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ edition = "2021"

[dependencies]
anyhow = "1.0.66"
current_platform = "0.2.0"
cargo-config2 = "0.1.19"
clap = { version = "4.0.29", features = ["derive", "deprecated"] }
tempfile = "3.3.0"
toml = "0.5.9"
Expand Down
18 changes: 17 additions & 1 deletion src/utils.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,20 @@
use std::sync::OnceLock;

/// The default target to pass to cargo, to workaround issue #11.
pub fn default_target() -> &'static str {
current_platform::CURRENT_PLATFORM
// Use OnceLock because clap's default_value only accepts reference.
static DEFAULT_TARGET: OnceLock<String> = OnceLock::new();
DEFAULT_TARGET.get_or_init(|| {
let config = cargo_config2::Config::load().unwrap();
// Get the target specified in config.
let mut targets = config.build_target_for_cli(None::<&str>).unwrap();
if targets.len() > 1 {
// Config can contain multiple targets, but we don't support it: https://doc.rust-lang.org/cargo/reference/config.html#buildtarget
panic!("multi-target build is not supported: {targets:?}");
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Let's return a result from this function and properly propagate errors rather than panicking here.

}
targets
.pop()
// Get the host triple if the target is not specified in config.
.unwrap_or_else(|| config.host_triple().unwrap().to_owned())
Comment on lines +11 to +18
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think this would be cleaner as match targets.len() { ... }.

})
}