-
Notifications
You must be signed in to change notification settings - Fork 44
Honor linker flavor #270
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
Merged
+168
−37
Merged
Honor linker flavor #270
Changes from all commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
0cb6467
Honor linker flavor flag
Shnatsel 37d2352
Add tests for linker flavor flag parsing
Shnatsel f564ce8
Add a function to query linker flavor from rustc where possible
Shnatsel b1d1e73
Wire up detection of default linker flavor from the target platform JSON
Shnatsel d5109f2
Avoid obscure slice operation, use find_map instead
Shnatsel File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,66 @@ | ||
| use std::{error::Error, ffi::OsStr, process::Command}; | ||
|
|
||
| use serde::Deserialize; | ||
|
|
||
| #[derive(Debug, Deserialize)] | ||
| pub struct RustcTargetJson { | ||
| #[serde(rename = "linker-flavor")] | ||
| pub linker_flavor: String, | ||
| } | ||
|
|
||
| /// Queries target defaults, without applying command-line linker overrides. | ||
| /// | ||
| /// This function relies on nightly-only rustc functionality and may break. | ||
| /// This kind of linker target querying is really only needed for obscure | ||
| /// embedded platforms, and is not essential to the core functionality. | ||
| pub fn rustc_target_json( | ||
| rustc_path: &OsStr, | ||
| target_triple: &str, | ||
| ) -> Result<RustcTargetJson, Box<dyn Error>> { | ||
| let output = Command::new(rustc_path) | ||
| // Enable this unstable query only for the child process, including on stable rustc. | ||
| .env("RUSTC_BOOTSTRAP", "1") | ||
| .args(["-Z", "unstable-options", "--print", "target-spec-json"]) | ||
| .arg(format!("--target={target_triple}")) //not being parsed by the shell, so not a vulnerability | ||
| .output() | ||
| .map_err(|error| { | ||
| format!("Failed to invoke rustc to get target-spec-json for '{target_triple}': {error}") | ||
| })?; | ||
|
|
||
| if !output.status.success() { | ||
| return Err(format!( | ||
| "rustc returned an error when asked for target-spec-json for '{target_triple}' ({}): {}", | ||
| output.status, | ||
| String::from_utf8_lossy(&output.stderr) | ||
| ) | ||
| .into()); | ||
| } | ||
|
|
||
| serde_json::from_slice(&output.stdout).map_err(|error| { | ||
| format!("Failed to parse rustc target specification for '{target_triple}': {error}").into() | ||
| }) | ||
| } | ||
|
|
||
| #[cfg(test)] | ||
| mod tests { | ||
| use super::*; | ||
|
|
||
| #[test] | ||
| fn query_linker_flavor() { | ||
| let rustc = std::env::var_os("RUSTC").unwrap_or_else(|| "rustc".into()); | ||
| for (target, expected_flavors) in [ | ||
| ( | ||
| "x86_64-unknown-linux-gnu", | ||
| &["gcc", "gnu-cc", "gnu-lld-cc"][..], | ||
| ), | ||
| ("thumbv7em-none-eabihf", &["ld.lld", "gnu-lld"][..]), | ||
| ] { | ||
| let spec = rustc_target_json(&rustc, target).unwrap(); | ||
| assert!( | ||
| expected_flavors.contains(&spec.linker_flavor.as_str()), | ||
| "unexpected linker flavor for {target}: {}", | ||
| spec.linker_flavor | ||
| ); | ||
| } | ||
| } | ||
| } |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
rustc --print host-tupleis simpler, though I don't know if it was available on your MSRV already.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I want to support older rustc versions, so I aped this from Cargo and it never broke so I never changed it.