Skip to content

Commit

Permalink
Merge pull request #1125 from hermit-os/cargo-proxy
Browse files Browse the repository at this point in the history
fix(xtask): properly use cargo proxy for foreign toolchains
  • Loading branch information
mkroening authored Apr 9, 2024
2 parents 6e6d6f3 + 2239624 commit a308906
Show file tree
Hide file tree
Showing 4 changed files with 81 additions and 52 deletions.
22 changes: 12 additions & 10 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -161,6 +161,8 @@ jobs:
with:
components: rust-src
- uses: mkroening/rust-toolchain-toml@main
- run: rustup component add llvm-tools
working-directory: .
- uses: mkroening/rust-toolchain-toml@main
with:
toolchain-file: 'kernel/rust-toolchain.toml'
Expand Down Expand Up @@ -190,25 +192,25 @@ jobs:
if: ${{ matrix.arch != 'aarch64' }}
- run: cargo xtask ci qemu --arch ${{ matrix.arch }} --profile ${{ matrix.profile }} ${{ matrix.flags }} --package rftrace-example --virtiofsd
if: ${{ matrix.arch == 'x86_64' }}
- run: cargo xtask ci qemu --arch ${{ matrix.arch }} --profile ${{ matrix.profile }} ${{ matrix.flags }} --package httpd --features ci,dhcpv4 --netdev virtio-net-pci
- run: cargo xtask ci qemu --arch ${{ matrix.arch }} --profile ${{ matrix.profile }} ${{ matrix.flags }} --package httpd --features ci,hermit/dhcpv4 --netdev virtio-net-pci
if: ${{ matrix.arch != 'riscv64' }}
- run: cargo xtask ci qemu --arch ${{ matrix.arch }} --profile ${{ matrix.profile }} ${{ matrix.flags }} --package httpd --features ci,dhcpv4 --netdev rtl8139 --features rtl8139
- run: cargo xtask ci qemu --arch ${{ matrix.arch }} --profile ${{ matrix.profile }} ${{ matrix.flags }} --package httpd --features ci,hermit/dhcpv4 --netdev rtl8139 --features hermit/rtl8139
if: ${{ matrix.arch == 'x86_64' }}
- run: cargo xtask ci qemu --arch ${{ matrix.arch }} --profile ${{ matrix.profile }} ${{ matrix.flags }} --package testudp --features udp,dhcpv4 --netdev virtio-net-pci
- run: cargo xtask ci qemu --arch ${{ matrix.arch }} --profile ${{ matrix.profile }} ${{ matrix.flags }} --package testudp --features hermit/udp,hermit/dhcpv4 --netdev virtio-net-pci
if: ${{ matrix.arch != 'riscv64' }}
- run: cargo xtask ci qemu --arch ${{ matrix.arch }} --profile ${{ matrix.profile }} ${{ matrix.flags }} --package testudp --features udp,dhcpv4 --netdev rtl8139 --features rtl8139
- run: cargo xtask ci qemu --arch ${{ matrix.arch }} --profile ${{ matrix.profile }} ${{ matrix.flags }} --package testudp --features hermit/udp,hermit/dhcpv4 --netdev rtl8139 --features hermit/rtl8139
if: ${{ matrix.arch == 'x86_64' }}
- run: cargo xtask ci qemu --arch ${{ matrix.arch }} --profile ${{ matrix.profile }} ${{ matrix.flags }} --package miotcp --features dhcpv4 --netdev virtio-net-pci
- run: cargo xtask ci qemu --arch ${{ matrix.arch }} --profile ${{ matrix.profile }} ${{ matrix.flags }} --package miotcp --features hermit/dhcpv4 --netdev virtio-net-pci
if: ${{ matrix.arch != 'riscv64' }}
- run: cargo xtask ci qemu --arch ${{ matrix.arch }} --profile ${{ matrix.profile }} ${{ matrix.flags }} --package miotcp --features dhcpv4 --netdev rtl8139 --features rtl8139
- run: cargo xtask ci qemu --arch ${{ matrix.arch }} --profile ${{ matrix.profile }} ${{ matrix.flags }} --package miotcp --features hermit/dhcpv4 --netdev rtl8139 --features hermit/rtl8139
if: ${{ matrix.arch == 'x86_64' }}
- run: cargo xtask ci qemu --arch ${{ matrix.arch }} --profile ${{ matrix.profile }} ${{ matrix.flags }} --package poll --features dhcpv4 --netdev virtio-net-pci
- run: cargo xtask ci qemu --arch ${{ matrix.arch }} --profile ${{ matrix.profile }} ${{ matrix.flags }} --package poll --features hermit/dhcpv4 --netdev virtio-net-pci
if: ${{ matrix.arch != 'riscv64' }}
- run: cargo xtask ci qemu --arch ${{ matrix.arch }} --profile ${{ matrix.profile }} ${{ matrix.flags }} --package poll --features dhcpv4 --netdev rtl8139 --features rtl8139
- run: cargo xtask ci qemu --arch ${{ matrix.arch }} --profile ${{ matrix.profile }} ${{ matrix.flags }} --package poll --features hermit/dhcpv4 --netdev rtl8139 --features hermit/rtl8139
if: ${{ matrix.arch == 'x86_64' }}
- run: cargo xtask ci qemu --arch ${{ matrix.arch }} --profile ${{ matrix.profile }} ${{ matrix.flags }} --package mioudp --features udp,dhcpv4 --netdev virtio-net-pci
- run: cargo xtask ci qemu --arch ${{ matrix.arch }} --profile ${{ matrix.profile }} ${{ matrix.flags }} --package mioudp --features hermit/udp,hermit/dhcpv4 --netdev virtio-net-pci
if: ${{ matrix.arch != 'riscv64' }}
- run: cargo xtask ci qemu --arch ${{ matrix.arch }} --profile ${{ matrix.profile }} ${{ matrix.flags }} --package mioudp --features udp,dhcpv4 --netdev rtl8139 --features rtl8139
- run: cargo xtask ci qemu --arch ${{ matrix.arch }} --profile ${{ matrix.profile }} ${{ matrix.flags }} --package mioudp --features hermit/udp,hermit/dhcpv4 --netdev rtl8139 --features hermit/rtl8139
if: ${{ matrix.arch == 'x86_64' }}

run-x86_64-kvm:
Expand Down
6 changes: 3 additions & 3 deletions xtask/src/build.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ use anyhow::Result;
use clap::Args;
use xshell::cmd;

use crate::cargo_build::{CargoBuild, CmdExt};
use crate::cargo_build::CargoBuild;

/// Build the kernel.
#[derive(Args)]
Expand All @@ -32,7 +32,7 @@ impl Build {
cmd!(sh, "cargo build")
.env("CARGO_ENCODED_RUSTFLAGS", self.cargo_encoded_rustflags()?)
.args(self.cargo_build.artifact.arch.cargo_args())
.cargo_build_args(&self.cargo_build)
.args(self.cargo_build.cargo_build_args())
.run()?;

let build_archive = self.cargo_build.artifact.build_archive();
Expand All @@ -52,7 +52,7 @@ impl Build {
cmd!(sh, "cargo build --release")
.arg("--manifest-path=hermit-builtins/Cargo.toml")
.args(self.cargo_build.artifact.arch.builtins_cargo_args())
.target_dir_args(&self.cargo_build)
.args(self.cargo_build.target_dir_args())
.run()?;

eprintln!("Exporting hermit-builtins symbols");
Expand Down
48 changes: 22 additions & 26 deletions xtask/src/cargo_build.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,4 @@
use std::ffi::OsStr;

use clap::Args;
use xshell::Cmd;

use crate::artifact::Artifact;

Expand All @@ -19,36 +16,35 @@ pub struct CargoBuild {
pub features: Vec<String>,
}

pub trait CmdExt {
fn cargo_build_args(self, cargo_build: &CargoBuild) -> Self;
fn target_dir_args(self, cargo_build: &CargoBuild) -> Self;
}

impl CmdExt for Cmd<'_> {
fn cargo_build_args(self, cargo_build: &CargoBuild) -> Self {
let cmd = self
.target_dir_args(cargo_build)
.args(cargo_build.no_default_features_args())
.args(cargo_build.features_args())
.args(cargo_build.release_args());

if let Some(profile) = &cargo_build.artifact.profile {
cmd.args(&["--profile", profile])
} else {
cmd
impl CargoBuild {
pub fn cargo_build_args(&self) -> Vec<String> {
let mut args = vec![];
args.extend(self.target_dir_args());
args.extend(
self.no_default_features_args()
.iter()
.map(|s| s.to_string()),
);
args.extend(self.features_args().map(|s| s.to_string()));
args.extend(self.release_args().iter().map(|s| s.to_string()));
if let Some(profile) = &self.artifact.profile {
args.push("--profile".to_string());
args.push(profile.to_string());
}
args
}

fn target_dir_args(self, cargo_build: &CargoBuild) -> Self {
if let Some(target_dir) = &cargo_build.artifact.target_dir {
self.args::<&[&OsStr]>(&["--target-dir".as_ref(), target_dir.as_ref()])
pub fn target_dir_args(&self) -> Vec<String> {
if let Some(target_dir) = &self.artifact.target_dir {
vec![
"--target-dir".to_string(),
target_dir.to_str().unwrap().to_string(),
]
} else {
self
vec![]
}
}
}

impl CargoBuild {
fn release_args(&self) -> &'static [&'static str] {
if self.artifact.release {
&["--release"]
Expand Down
57 changes: 44 additions & 13 deletions xtask/src/ci/build.rs
Original file line number Diff line number Diff line change
@@ -1,10 +1,11 @@
use std::env;
use std::path::PathBuf;
use std::process::Command;

use anyhow::Result;
use clap::Args;
use xshell::cmd;

use crate::cargo_build::{CargoBuild, CmdExt};
use crate::cargo_build::CargoBuild;

/// Build hermit-rs images.
#[derive(Args)]
Expand All @@ -24,23 +25,25 @@ impl Build {
eprintln!("::group::cargo build")
}

let sh = crate::sh()?;
let mut cargo = cargo();

let _push_env = if self.package.contains("rftrace") {
Some(sh.push_env(
if self.package.contains("rftrace") {
cargo.env(
"RUSTFLAGS",
"-Zinstrument-mcount -Cpasses=ee-instrument<post-inline>",
))
} else {
None
);
};

sh.change_dir("..");
cmd!(sh, "cargo build")
cargo
.current_dir("..")
.arg("build")
.args(self.cargo_build.artifact.arch.ci_cargo_args())
.cargo_build_args(&self.cargo_build)
.args(&["--package", self.package.as_str()])
.run()?;
.args(self.cargo_build.cargo_build_args())
.args(["--package", self.package.as_str()]);

eprintln!("$ {cargo:?}");
let status = cargo.status()?;
assert!(status.success());

if super::in_ci() {
eprintln!("::endgroup::")
Expand All @@ -53,3 +56,31 @@ impl Build {
self.cargo_build.artifact.ci_image(&self.package)
}
}

fn cargo() -> Command {
let cargo = {
let exe = format!("cargo{}", env::consts::EXE_SUFFIX);
// On windows, the userspace toolchain ends up in front of the rustup proxy in $PATH.
// To reach the rustup proxy nonetheless, we explicitly query $CARGO_HOME.
let mut cargo_home = PathBuf::from(env::var_os("CARGO_HOME").unwrap());
cargo_home.push("bin");
cargo_home.push(&exe);
if cargo_home.exists() {
cargo_home
} else {
PathBuf::from(exe)
}
};

let mut cargo = Command::new(cargo);

// Remove rust-toolchain-specific environment variables from kernel cargo
cargo.env_remove("LD_LIBRARY_PATH");
env::vars()
.filter(|(key, _value)| key.starts_with("CARGO") || key.starts_with("RUST"))
.for_each(|(key, _value)| {
cargo.env_remove(&key);
});

cargo
}

0 comments on commit a308906

Please sign in to comment.