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

more features for build-docker-image #791

Merged
merged 3 commits into from
Jun 14, 2022
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
8 changes: 2 additions & 6 deletions .github/actions/cargo-install-upload-artifacts/action.yml
Original file line number Diff line number Diff line change
Expand Up @@ -8,15 +8,13 @@ inputs:
runs:
using: composite
steps:
- name: Check if crate has binaries
- name: Setup
id: metadata
run: |
metadata="$(cargo metadata --format-version 1 --no-deps)"

package_name="$(jq -r '.packages[0].name' <<< "${metadata}")"
has_binaries="$(jq '.packages[0].targets | any(.kind | contains(["bin"]))' <<< "${metadata}")"
package_name="cross"
echo "::set-output name=package-name::${package_name}"
echo "::set-output name=has-binaries::${has_binaries}"

out_dir="$(mktemp -d)"
artifacts_dir="$(mktemp -d)"
Expand All @@ -31,7 +29,6 @@ runs:
shell: bash

- name: Build with all features
if: ${{ fromJson(steps.metadata.outputs.has-binaries) }}
uses: actions-rs/cargo@v1
with:
command: install
Expand All @@ -45,7 +42,6 @@ runs:
use-cross: true

- name: Archive artifacts
if: ${{ fromJson(steps.metadata.outputs.has-binaries) }}
id: archive
run: |
set -x
Expand Down
2 changes: 1 addition & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,7 @@ This project adheres to [Semantic Versioning](http://semver.org/).
### Internal

- #787 - add installer for git hooks.
- #786 - Migrate build script to rust: `cargo build-docker-image $TARGET`
- #786, #791 - Migrate build script to rust: `cargo build-docker-image $TARGET`
- #730 - make FreeBSD builds more resilient.
- #670 - Use serde for deserialization of Cross.toml
- Change rust edition to 2021 and bump MSRV for the cross binary to 1.58.1
Expand Down
29 changes: 25 additions & 4 deletions Cargo.lock

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

4 changes: 2 additions & 2 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -18,10 +18,10 @@ members = ["xtask"]

[dependencies]
atty = "0.2"
# Pinned until further action to migrate to newer clap, see https://github.com/clap-rs/clap/issues/3822#issuecomment-1154069623
clap = { version = "~3.1", features = ["derive"] }
clap = { version = "3.2.2", features = ["derive", "unstable-v4"] }
color-eyre = "0.6"
eyre = "0.6"
thiserror = "1"
home = "0.5"
rustc_version = "0.4"
toml = "0.5"
Expand Down
2 changes: 1 addition & 1 deletion src/bin/cross-util.rs
Original file line number Diff line number Diff line change
Expand Up @@ -166,7 +166,7 @@ fn remove_images(
}
command.args(images);
if execute {
command.run(verbose)
command.run(verbose).map_err(Into::into)
} else {
println!("{:?}", command);
Ok(())
Expand Down
7 changes: 5 additions & 2 deletions src/cargo.rs
Original file line number Diff line number Diff line change
Expand Up @@ -147,11 +147,14 @@ pub fn cargo_metadata_with_args(
}

/// Pass-through mode
pub fn run(args: &[String], verbose: bool) -> Result<ExitStatus> {
pub fn run(args: &[String], verbose: bool) -> Result<ExitStatus, CommandError> {
Command::new("cargo").args(args).run_and_get_status(verbose)
}

/// run cargo and get the output, does not check the exit status
pub fn run_and_get_output(args: &[String], verbose: bool) -> Result<std::process::Output> {
pub fn run_and_get_output(
args: &[String],
verbose: bool,
) -> Result<std::process::Output, CommandError> {
Command::new("cargo").args(args).run_and_get_output(verbose)
}
2 changes: 2 additions & 0 deletions src/docker.rs
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,7 @@ pub fn register(target: &Target, verbose: bool) -> Result<()> {
.arg("ubuntu:16.04")
.args(&["sh", "-c", cmd])
.run(verbose)
.map_err(Into::into)
}

fn validate_env_var(var: &str) -> Result<(&str, Option<&str>)> {
Expand Down Expand Up @@ -363,6 +364,7 @@ pub fn run(
.arg(&image(config, target)?)
.args(&["sh", "-c", &format!("PATH=$PATH:/rust/bin {:?}", cmd)])
.run_and_get_status(verbose)
.map_err(Into::into)
}

pub fn image(config: &Config, target: &Target) -> Result<String> {
Expand Down
10 changes: 10 additions & 0 deletions src/errors.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,3 +7,13 @@ pub fn install_panic_hook() -> Result<()> {
.display_env_section(false)
.install()
}

#[derive(Debug, thiserror::Error)]
pub enum CommandError {
#[error("`{1}` failed with exit code: {0}")]
NonZeroExitCode(std::process::ExitStatus, String),
#[error("could not execute `{0}`")]
CouldNotExecute(#[source] Box<dyn std::error::Error + Send + Sync>, String),
#[error("`{0:?}` output was not UTF-8")]
Utf8Error(#[source] std::string::FromUtf8Error, std::process::Output),
}
34 changes: 16 additions & 18 deletions src/extensions.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,11 +6,11 @@ use crate::errors::*;

pub trait CommandExt {
fn print_verbose(&self, verbose: bool);
fn status_result(&self, status: ExitStatus) -> Result<()>;
fn run(&mut self, verbose: bool) -> Result<()>;
fn run_and_get_status(&mut self, verbose: bool) -> Result<ExitStatus>;
fn run_and_get_stdout(&mut self, verbose: bool) -> Result<String>;
fn run_and_get_output(&mut self, verbose: bool) -> Result<std::process::Output>;
fn status_result(&self, status: ExitStatus) -> Result<(), CommandError>;
fn run(&mut self, verbose: bool) -> Result<(), CommandError>;
fn run_and_get_status(&mut self, verbose: bool) -> Result<ExitStatus, CommandError>;
fn run_and_get_stdout(&mut self, verbose: bool) -> Result<String, CommandError>;
fn run_and_get_output(&mut self, verbose: bool) -> Result<std::process::Output, CommandError>;
}

impl CommandExt for Command {
Expand All @@ -20,29 +20,29 @@ impl CommandExt for Command {
}
}

fn status_result(&self, status: ExitStatus) -> Result<()> {
fn status_result(&self, status: ExitStatus) -> Result<(), CommandError> {
if status.success() {
Ok(())
} else {
eyre::bail!("`{:?}` failed with exit code: {:?}", self, status.code())
Err(CommandError::NonZeroExitCode(status, format!("{self:?}")))
}
}

/// Runs the command to completion
fn run(&mut self, verbose: bool) -> Result<()> {
fn run(&mut self, verbose: bool) -> Result<(), CommandError> {
let status = self.run_and_get_status(verbose)?;
self.status_result(status)
}

/// Runs the command to completion
fn run_and_get_status(&mut self, verbose: bool) -> Result<ExitStatus> {
fn run_and_get_status(&mut self, verbose: bool) -> Result<ExitStatus, CommandError> {
self.print_verbose(verbose);
self.status()
.wrap_err_with(|| format!("couldn't execute `{:?}`", self))
.map_err(|e| CommandError::CouldNotExecute(Box::new(e), format!("{self:?}")))
}

/// Runs the command to completion and returns its stdout
fn run_and_get_stdout(&mut self, verbose: bool) -> Result<String> {
fn run_and_get_stdout(&mut self, verbose: bool) -> Result<String, CommandError> {
let out = self.run_and_get_output(verbose)?;
self.status_result(out.status)?;
out.stdout()
Expand All @@ -53,22 +53,20 @@ impl CommandExt for Command {
/// # Notes
///
/// This command does not check the status.
fn run_and_get_output(&mut self, verbose: bool) -> Result<std::process::Output> {
fn run_and_get_output(&mut self, verbose: bool) -> Result<std::process::Output, CommandError> {
self.print_verbose(verbose);
self.output()
.wrap_err_with(|| format!("couldn't execute `{:?}`", self))
.map_err(Into::into)
.map_err(|e| CommandError::CouldNotExecute(Box::new(e), format!("{self:?}")))
}
}

pub trait OutputExt {
fn stdout(&self) -> Result<String>;
fn stdout(&self) -> Result<String, CommandError>;
}

impl OutputExt for std::process::Output {
fn stdout(&self) -> Result<String> {
String::from_utf8(self.stdout.clone())
.wrap_err_with(|| format!("`{:?}` output was not UTF-8", self))
fn stdout(&self) -> Result<String, CommandError> {
String::from_utf8(self.stdout.clone()).map_err(|e| CommandError::Utf8Error(e, self.clone()))
}
}

Expand Down
4 changes: 2 additions & 2 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ mod cli;
mod config;
mod cross_toml;
mod docker;
mod errors;
pub mod errors;
mod extensions;
mod file;
mod id;
Expand Down Expand Up @@ -464,7 +464,7 @@ pub fn run() -> Result<ExitStatus> {
}
Ok(out.status)
}
_ => cargo::run(&argv, verbose),
_ => cargo::run(&argv, verbose).map_err(Into::into),
}
}

Expand Down
1 change: 1 addition & 0 deletions src/rustc.rs
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@ pub fn target_list(verbose: bool) -> Result<TargetList> {
.map(|s| TargetList {
triples: s.lines().map(|l| l.to_owned()).collect(),
})
.map_err(Into::into)
}

pub fn sysroot(host: &Host, target: &Target, verbose: bool) -> Result<PathBuf> {
Expand Down
2 changes: 1 addition & 1 deletion xtask/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ cross = { path = "..", features = ["dev"] }
walkdir = { version = "2" }
color-eyre = "0.6"
eyre = "0.6"
clap = { version = "~3.1", features = ["derive", "env"] }
clap = { version = "3.2.2", features = ["derive", "env", "unstable-v4"] }
which = { version = "4", default_features = false }
serde = { version = "1", features = ["derive"] }
serde_yaml = "0.8"
Loading