Skip to content

Commit

Permalink
Do not rely on implicit use of cross
Browse files Browse the repository at this point in the history
Trying to conditionally set the CARGO environment variable is too
complex due to the xtask wrapper and the environment within an xshell
Shell, so instead we'll just add an explicit option flag to select the
command. I believe it's only necessary for Linux when not targetting the
native host triple.
  • Loading branch information
elasticdog committed Jun 10, 2023
1 parent b1233c2 commit 5b2ad8c
Show file tree
Hide file tree
Showing 5 changed files with 42 additions and 23 deletions.
19 changes: 10 additions & 9 deletions .github/cue/draft-release.cue
Original file line number Diff line number Diff line change
Expand Up @@ -44,12 +44,12 @@ draftRelease: {
strategy: {
"fail-fast": false
matrix: include: [
{target: "aarch64-apple-darwin", os: "macos-latest"},
{target: "aarch64-pc-windows-msvc", os: "windows-latest"},
{target: "aarch64-unknown-linux-musl", os: "ubuntu-latest"},
{target: "x86_64-apple-darwin", os: "macos-latest"},
{target: "x86_64-pc-windows-msvc", os: "windows-latest"},
{target: "x86_64-unknown-linux-musl", os: "ubuntu-latest"},
{target: "aarch64-apple-darwin", os: "macos-latest", build_tool: "cargo"},
{target: "aarch64-pc-windows-msvc", os: "windows-latest", build_tool: "cargo"},
{target: "aarch64-unknown-linux-musl", os: "ubuntu-latest", build_tool: "cross"},
{target: "x86_64-apple-darwin", os: "macos-latest", build_tool: "cargo"},
{target: "x86_64-pc-windows-msvc", os: "windows-latest", build_tool: "cargo"},
{target: "x86_64-unknown-linux-musl", os: "ubuntu-latest", build_tool: "cargo"},
]}
"runs-on": "${{ matrix.os }}"
env: GH_TOKEN: "${{ secrets.GITHUB_TOKEN }}"
Expand All @@ -66,10 +66,11 @@ draftRelease: {
{
name: "Building release assets"
run: """
if [[ "$OSTYPE" == "linux"* ]]; then
export CARGO="cross"
if [[ "${{ matrix.build_tool }}" == "cross" ]]; then
cargo xtask dist --target "${{ matrix.target }}" --cross
else
cargo xtask dist --target "${{ matrix.target }}"
fi
cargo xtask dist --target "${{ matrix.target }}"
"""
},
{
Expand Down
13 changes: 10 additions & 3 deletions .github/workflows/draft-release.yml
Original file line number Diff line number Diff line change
Expand Up @@ -43,16 +43,22 @@ jobs:
include:
- target: aarch64-apple-darwin
os: macos-latest
build_tool: cargo
- target: aarch64-pc-windows-msvc
os: windows-latest
build_tool: cargo
- target: aarch64-unknown-linux-musl
os: ubuntu-latest
build_tool: cross
- target: x86_64-apple-darwin
os: macos-latest
build_tool: cargo
- target: x86_64-pc-windows-msvc
os: windows-latest
build_tool: cargo
- target: x86_64-unknown-linux-musl
os: ubuntu-latest
build_tool: cargo
runs-on: ${{ matrix.os }}
env:
GH_TOKEN: ${{ secrets.GITHUB_TOKEN }}
Expand All @@ -78,9 +84,10 @@ jobs:
tool: cross
- name: Building release assets
run: |-
if [[ "$OSTYPE" == "linux"* ]]; then
export CARGO="cross"
if [[ "${{ matrix.build_tool }}" == "cross" ]]; then
cargo xtask dist --target "${{ matrix.target }}" --cross
else
cargo xtask dist --target "${{ matrix.target }}"
fi
cargo xtask dist --target "${{ matrix.target }}"
- name: Uploading release assets
run: "if [[ \"${{ matrix.os }}\" == \"windows-latest\" ]]; then\n extension=\"zip\"\nelse\n extension=\"tar.gz\" \nfi\nfilename=\"spellout-${GITHUB_REF_NAME:1}-${{ matrix.target }}.${extension}\"\n\necho \"Uploading ${filename} to: ${{ needs.create_release.outputs.upload_url }}\"\ngh release upload \"$GITHUB_REF_NAME\" \"target/dist/${filename}\"\n\necho \":arrow_up: Uploaded release asset ${filename}\" >>\"$GITHUB_STEP_SUMMARY\""
4 changes: 4 additions & 0 deletions xtask/src/commands.rs
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,10 @@ pub fn cargo_cmd<'a>(config: &Config, sh: &'a Shell) -> Option<Cmd<'a>> {
)
}

pub fn cross_cmd<'a>(config: &Config, sh: &'a Shell) -> Option<Cmd<'a>> {
create_cmd("cross", "https://github.com/cross-rs/cross", config, sh)
}

pub fn cue_cmd<'a>(config: &Config, sh: &'a Shell) -> Option<Cmd<'a>> {
create_cmd("cue", "https://cuelang.org/", config, sh)
}
Expand Down
18 changes: 9 additions & 9 deletions xtask/src/dist.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ use nanoserde::DeJson;
use rustc_version::version_meta;
use xshell::{cmd, Shell};

use crate::commands::cargo_cmd;
use crate::commands::{cargo_cmd, cross_cmd};
use crate::utils::project_root;
use crate::Config;

Expand Down Expand Up @@ -56,16 +56,16 @@ pub fn dist(config: &Config) -> Result<()> {
fn build_binary(config: &Config, binary: &str, dest_dir: &Path) -> Result<()> {
let sh = Shell::new()?;

let target_args = config
.target
.as_ref()
.map_or_else(String::new, |target| format!("--target={target}"));

let cmd_option = cargo_cmd(config, &sh);
let cmd_option = if config.cross {
cross_cmd(config, &sh)
} else {
cargo_cmd(config, &sh)
};
if let Some(cmd) = cmd_option {
let mut args = vec!["build", "--release", "--bin", binary];
if !target_args.is_empty() {
args.push(&target_args);
if let Some(target) = &config.target {
args.push("--target");
args.push(target);
}
cmd.args(args).run()?;
}
Expand Down
11 changes: 9 additions & 2 deletions xtask/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ USAGE:
cargo xtask [OPTIONS] [TASK]...
OPTIONS:
--cross Uses cross for cross-compilation instead of cargo
-i, --ignore-missing Ignores any missing tools; only warns during fixup
--target <TRIPLE> Sets the target triple that dist will build
-h, --help Prints help information
Expand Down Expand Up @@ -54,6 +55,7 @@ enum Task {
}

pub struct Config {
cross: bool,
run_tasks: Vec<Task>,
ignore_missing_commands: bool,
target: Option<String>,
Expand Down Expand Up @@ -91,9 +93,10 @@ fn parse_args() -> Result<Config> {
use lexopt::prelude::*;

// default config values
let mut run_tasks = Vec::new();
let mut cross = false;
let mut ignore_missing_commands = false;
let mut target: Option<String> = None;
let mut run_tasks = Vec::new();

let mut parser = lexopt::Parser::from_env();
while let Some(arg) = parser.next()? {
Expand All @@ -102,10 +105,13 @@ fn parse_args() -> Result<Config> {
print!("{HELP}");
std::process::exit(0);
}
Long("cross") => {
cross = true;
}
Short('i') | Long("ignore-missing") => {
ignore_missing_commands = true;
}
Short('t') | Long("target") => {
Long("target") => {
target = Some(parser.value()?.string()?);
}
Value(value) => {
Expand Down Expand Up @@ -138,6 +144,7 @@ fn parse_args() -> Result<Config> {
}

Ok(Config {
cross,
run_tasks,
ignore_missing_commands,
target,
Expand Down

0 comments on commit 5b2ad8c

Please sign in to comment.