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

Read target architecture from cross-compiled binaries. #822

Closed
wants to merge 1 commit into from

Conversation

Alexhuszagh
Copy link
Contributor

Use readelf -A on generated binaries to read the target architecture information and output the data as JSON, simplifying verifying that new targets produce code for the desired architecture.

Closes #821.
Related to #426.

@Alexhuszagh Alexhuszagh added enhancement meta issues/PRs related to the maintenance of the crate. labels Jun 18, 2022
@Alexhuszagh Alexhuszagh requested a review from a team as a code owner June 18, 2022 15:56
@Alexhuszagh
Copy link
Contributor Author

Alexhuszagh commented Jun 18, 2022

I also removed the -it flag from target-info, since this can cause a wait status to generate, and it isn't necessary for the command to function (well, if we ever change from run to run_and_get_stdout or something similar, and need to capture the output or stdout).

Copy link
Member

@Emilgardis Emilgardis left a comment

Choose a reason for hiding this comment

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

looks good!

why not run readelf on the host?

Comment on lines 88 to 105
println!("{{");
for (key, value) in lines {
let value = value.trim_matches('"');
println!(" \"{key}\": \"{value}\"",);
}
println!("}}");
}
Copy link
Member

Choose a reason for hiding this comment

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

same spirit as #818, can you store this in a struct/serde_json::Value, make the function return the struct and make the print in the readelf function

Comment on lines 60 to 68

if !verbose {
// capture stderr to avoid polluting table
command.stderr(Stdio::null());
}
let stdout = command.run_and_get_stdout(verbose)?;
let trimmed = stdout.trim();
if trimmed.is_empty() {
eprintln!("no target information: target is likely same architecture as host.");
Copy link
Member

Choose a reason for hiding this comment

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

Suggested change
if !verbose {
// capture stderr to avoid polluting table
command.stderr(Stdio::null());
}
let stdout = command.run_and_get_stdout(verbose)?;
let trimmed = stdout.trim();
if trimmed.is_empty() {
eprintln!("no target information: target is likely same architecture as host.");
let out = command.run_and_get_output(verbose)?;
let stdout = out.stdout()?;
if stdout.trim().is_empty() {
let stderr = out.stderr()?;
if !stderr.trim().is_empty() {
if stderr.contains("target may not be installed") {
eprintln!("warning: target {target} is not installed");
return Ok(());
} else if !stderr.contains("Finished dev") {
eprintln!("{}", stderr.header("stderr"));
return Ok(());
}
}
eprintln!("no target information: target `{target}` is likely same architecture as host.");
if verbose {
eprintln!("{}", stderr.header("stderr"));
}

Copy link
Member

Choose a reason for hiding this comment

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

uses color_eyre::SectionExt to output it nicely

Comment on lines 12 to 15
cargo init --bin hello >/dev/null
cd hello
cargo build --target "${TARGET}" "${@}" >/dev/null
readelf -A "target/${TARGET}/debug/hello"
Copy link
Member

Choose a reason for hiding this comment

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

Suggested change
cargo init --bin hello >/dev/null
cd hello
cargo build --target "${TARGET}" "${@}" >/dev/null
readelf -A "target/${TARGET}/debug/hello"
cargo init --bin hello 1>&2
cd hello
cargo build --target "${TARGET}" "${@}" 1>&2
readelf -A "target/${TARGET}/debug/hello"

Comment on lines 91 to 96
if !verbose {
// capture output to avoid polluting table
command.stdout(Stdio::null());
command.stderr(Stdio::null());
}
command.run(verbose, false).map_err(Into::into)
Copy link
Member

Choose a reason for hiding this comment

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

I know this is old but I think this should be changed to

Suggested change
if !verbose {
// capture output to avoid polluting table
command.stdout(Stdio::null());
command.stderr(Stdio::null());
}
command.run(verbose, false).map_err(Into::into)
let out = command.run_and_get_output(verbose)?;
command.status_result(verbose, out.status, Some(&out))?;
Ok(())

Use `readelf -A` on generated binaries to read the target architecture
information and output the data as JSON, simplifying verifying that
new targets produce code for the desired architecture.

Closes cross-rs#821.
Related to cross-rs#426.
@Alexhuszagh
Copy link
Contributor Author

This actually needs to install the target architecture via rustup if it's not present.

@Emilgardis
Copy link
Member

yup, I don't know if maybe it makes sense to do it only when wanted, eg --download-targets or something

@Alexhuszagh
Copy link
Contributor Author

Actually this might be more difficult than I thought. MIPS is giving me some very weird data. Probably will close this. You can submit the other changes as a separate PR.

bors bot added a commit that referenced this pull request Jun 19, 2022
824: Minor xtask restructuring. r=Emilgardis a=Alexhuszagh

Remove the `-it` flag from `target-info`, which can the command to not complete. Also changes `pull_image` to use `run_and_get_output` rather than `run`, which is how we should capture the output. Finally, it moves `format_repo` and `pull_image` to `util`, so they can be used by other commands.

This applies a few of the changes mentioned in #822.

Co-authored-by: Alex Huszagh <ahuszagh@gmail.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement meta issues/PRs related to the maintenance of the crate.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Read Target Architecture Information in XTask
2 participants