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

allow cross-util install #1216

Draft
wants to merge 3 commits into
base: main
Choose a base branch
from
Draft
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
146 changes: 146 additions & 0 deletions src/bin/commands/install.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,146 @@
use clap::Args;
use cross::{docker, rustc, shell::MessageInfo};
use eyre::Context;

#[derive(Args, Debug)]
pub struct Install {
#[clap(long)]
target: Option<String>,
#[clap(long)]
root: String,
/// Provide verbose diagnostic output.
#[clap(short, long)]
pub verbose: bool,
/// Do not print
#[clap(short, long)]
pub quiet: bool,
/// Coloring: auto, always, never
#[clap(long)]
pub color: Option<String>,
/// Container engine (such as docker or podman).
#[clap(long)]
pub engine: Option<String>,
/// Path to crate
#[clap(long)]
pub path: Option<String>,
/// Path to Cross.toml
#[clap(long)]
pub config: Option<std::path::PathBuf>,
Comment on lines +23 to +28
Copy link
Member Author

Choose a reason for hiding this comment

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

this is not implemented

#[clap(name = "crate")]
krate: String,
}

impl Install {
pub fn verbose(&self) -> bool {
self.verbose
}

pub fn quiet(&self) -> bool {
self.quiet
}

pub fn color(&self) -> Option<&str> {
self.color.as_deref()
}

pub fn run(self, msg_info: &mut MessageInfo) -> cross::Result<std::process::ExitStatus> {
let target_list = rustc::target_list(&mut cross::shell::Verbosity::Quiet.into())?;

let host_version_meta = rustc::version_meta()?;
let mut command = vec!["install".to_owned(), self.krate];

if let Some(target) = self.target {
command.push(format!("--target={target}"));
}
command.push(format!("--root={}", self.root));

if let Some(engine) = self.engine {
std::env::set_var(docker::CROSS_CONTAINER_ENGINE_VAR, engine);
}

let args = cross::cli::parse(command, &target_list)?;
let Some(cross::CrossSetup {
config,
target,
uses_xargo,
uses_zig,
uses_build_std,
zig_version,
toolchain,
is_remote,
engine,
image,
}) = cross::setup(&host_version_meta, None, &args, target_list, msg_info)? else {
eyre::bail!("couldn't setup context for cross (see warning)")
};

let mut is_nightly = toolchain.channel.contains("nightly");
let mut rustc_version = None;
if let Some((version, channel, _)) = toolchain.rustc_version()? {
is_nightly = channel == rustc_version::Channel::Nightly;
rustc_version = Some(version);
}
Comment on lines +79 to +82
Copy link
Member Author

Choose a reason for hiding this comment

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

this doesn't emit warning on mismatch like normal cross does


let available_targets = cross::rustup::setup_rustup(&toolchain, msg_info)?;

cross::rustup::setup_components(
&target,
uses_xargo,
uses_build_std,
&toolchain,
is_nightly,
available_targets,
&args,
msg_info,
)?;

let filtered_args = cross::get_filtered_args(
zig_version,
&args,
&target,
&config,
is_nightly,
uses_build_std,
);

let cwd = std::env::current_dir()?;

let paths = docker::DockerPaths::create(
&engine,
cross::CargoMetadata {
workspace_root: cwd.clone(),
target_directory: cross::file::absolute_path(self.root)?,
Copy link
Member Author

Choose a reason for hiding this comment

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

this is misleading

cargo install creates a temporary directory where it builds, we don't use this path as the normal target-dir, rather as the target directory for the cargo install output

packages: vec![],
workspace_members: vec![],
},
cwd,
toolchain.clone(),
msg_info,
)?;
let mut options = docker::DockerOptions::new(
engine,
target.clone(),
config,
image,
cross::CargoVariant::create(uses_zig, uses_xargo)?,
rustc_version,
);

options.skip_target_dir = true;

cross::install_interpreter_if_needed(
&args,
host_version_meta,
&target,
&options,
msg_info,
)?;

let status = docker::run(options, paths, &filtered_args, msg_info)
.wrap_err("could not run container")?;
if !status.success() {
cross::warn_on_failure(&target, &toolchain, msg_info)?;
}
Ok(status)
}
}
2 changes: 2 additions & 0 deletions src/bin/commands/mod.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
mod clean;
mod containers;
mod images;
mod install;

pub use self::clean::*;
pub use self::containers::*;
pub use self::images::*;
pub use self::install::*;
10 changes: 10 additions & 0 deletions src/bin/cross-util.rs
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,8 @@ enum Commands {
Containers(commands::Containers),
/// Clean all cross data in local storage.
Clean(commands::Clean),
/// Install a binary crate
Install(commands::Install),
}

fn is_toolchain(toolchain: &str) -> cross::Result<Toolchain> {
Expand Down Expand Up @@ -103,6 +105,14 @@ pub fn main() -> cross::Result<()> {
let engine = get_engine!(args, false, msg_info)?;
args.run(engine, &mut msg_info)?;
}
Commands::Install(args) => {
let mut msg_info = get_msg_info!(args)?;
let status = args.run(&mut msg_info)?;
let code = status
.code()
.ok_or_else(|| eyre::Report::msg("Cargo process terminated by signal"))?;
std::process::exit(code)
}
}

Ok(())
Expand Down
2 changes: 1 addition & 1 deletion src/bin/cross.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ pub fn main() -> cross::Result<()> {
cross::install_termination_hook()?;

let target_list = rustc::target_list(&mut Verbosity::Quiet.into())?;
let args = cli::parse(&target_list)?;
let args = cli::parse(env::args().skip(1), &target_list)?;
let subcommand = args.subcommand;
let mut msg_info = shell::MessageInfo::create(args.verbose, args.quiet, args.color.as_deref())?;
let status = match cross::run(args, target_list, &mut msg_info)? {
Expand Down
5 changes: 3 additions & 2 deletions src/cli.rs
Original file line number Diff line number Diff line change
Expand Up @@ -157,7 +157,7 @@ fn store_target_dir(_: String) -> Result<String> {
Ok("/target".to_owned())
}

pub fn parse(target_list: &TargetList) -> Result<Args> {
pub fn parse(args: impl IntoIterator<Item = String>, target_list: &TargetList) -> Result<Args> {
let mut channel = None;
let mut target = None;
let mut features = Vec::new();
Expand All @@ -171,8 +171,9 @@ pub fn parse(target_list: &TargetList) -> Result<Args> {
let mut verbose = 0;
let mut color = None;

let mut args = args.into_iter();

{
let mut args = env::args().skip(1);
while let Some(arg) = args.next() {
if arg.is_empty() {
continue;
Expand Down
3 changes: 2 additions & 1 deletion src/docker/engine.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ use super::{Architecture, ContainerOs};

pub const DOCKER: &str = "docker";
pub const PODMAN: &str = "podman";
pub const CROSS_CONTAINER_ENGINE_VAR: &str = "CROSS_CONTAINER_ENGINE";

#[derive(Copy, Clone, Debug, PartialEq, Eq)]
pub enum EngineType {
Expand Down Expand Up @@ -260,7 +261,7 @@ fn get_custom_info(
}

pub fn get_container_engine() -> Result<PathBuf, which::Error> {
if let Ok(ce) = env::var("CROSS_CONTAINER_ENGINE") {
if let Ok(ce) = env::var(CROSS_CONTAINER_ENGINE_VAR) {
which::which(ce)
} else {
which::which(DOCKER).or_else(|_| which::which(PODMAN))
Expand Down
21 changes: 11 additions & 10 deletions src/docker/local.rs
Original file line number Diff line number Diff line change
Expand Up @@ -87,19 +87,20 @@ pub(crate) fn run(
package_dirs.mount_root()
),
]);
docker
.args([
"-v",
&format!(
"{}:{}:z,ro",
toolchain_dirs.get_sysroot().to_utf8()?,
toolchain_dirs.sysroot_mount_path()
),
])
.args([
docker.args([
"-v",
&format!(
"{}:{}:z,ro",
toolchain_dirs.get_sysroot().to_utf8()?,
toolchain_dirs.sysroot_mount_path()
),
]);
if !options.skip_target_dir {
docker.args([
"-v",
&format!("{}:/target:z", package_dirs.target().to_utf8()?),
]);
}
docker.add_cwd(&paths)?;

// When running inside NixOS or using Nix packaging we need to add the Nix
Expand Down
7 changes: 6 additions & 1 deletion src/docker/shared.rs
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ pub struct DockerOptions {
pub target: Target,
pub config: Config,
pub image: Image,
pub skip_target_dir: bool,
pub cargo_variant: CargoVariant,
// not all toolchains will provide this
pub rustc_version: Option<RustcVersion>,
Expand All @@ -52,6 +53,7 @@ impl DockerOptions {
target,
config,
image,
skip_target_dir: false,
cargo_variant,
rustc_version,
}
Expand Down Expand Up @@ -1039,8 +1041,11 @@ impl DockerCommandExt for Command {
"-e",
&format!("CROSS_RUST_SYSROOT={}", dirs.sysroot_mount_path()),
])
.args(["-e", "CARGO_TARGET_DIR=/target"])
.args(["-e", &cross_runner]);

if !options.skip_target_dir {
self.args(["-e", "CARGO_TARGET_DIR=/target"]);
}
if options.cargo_variant.uses_zig() {
// otherwise, zig has a permission error trying to create the cache
self.args(["-e", "XDG_CACHE_HOME=/target/.zig-cache"]);
Comment on lines -1042 to 1051
Copy link
Member Author

Choose a reason for hiding this comment

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

is this comment about zig a problem?

Expand Down
Loading