diff --git a/Cargo.lock b/Cargo.lock index dda00a2d0..2d276d732 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -5,7 +5,7 @@ name = "atty" version = "0.2.13" source = "registry+https://github.com/rust-lang/crates.io-index" dependencies = [ - "libc 0.2.62 (registry+https://github.com/rust-lang/crates.io-index)", + "libc 0.2.65 (registry+https://github.com/rust-lang/crates.io-index)", "winapi 0.3.8 (registry+https://github.com/rust-lang/crates.io-index)", ] @@ -16,7 +16,7 @@ source = "registry+https://github.com/rust-lang/crates.io-index" dependencies = [ "backtrace-sys 0.1.31 (registry+https://github.com/rust-lang/crates.io-index)", "cfg-if 0.1.10 (registry+https://github.com/rust-lang/crates.io-index)", - "libc 0.2.62 (registry+https://github.com/rust-lang/crates.io-index)", + "libc 0.2.65 (registry+https://github.com/rust-lang/crates.io-index)", "rustc-demangle 0.1.16 (registry+https://github.com/rust-lang/crates.io-index)", ] @@ -26,7 +26,7 @@ version = "0.1.31" source = "registry+https://github.com/rust-lang/crates.io-index" dependencies = [ "cc 1.0.45 (registry+https://github.com/rust-lang/crates.io-index)", - "libc 0.2.62 (registry+https://github.com/rust-lang/crates.io-index)", + "libc 0.2.65 (registry+https://github.com/rust-lang/crates.io-index)", ] [[package]] @@ -52,11 +52,12 @@ dependencies = [ "error-chain 0.12.1 (registry+https://github.com/rust-lang/crates.io-index)", "home 0.5.0 (registry+https://github.com/rust-lang/crates.io-index)", "lazy_static 1.4.0 (registry+https://github.com/rust-lang/crates.io-index)", - "libc 0.2.62 (registry+https://github.com/rust-lang/crates.io-index)", + "libc 0.2.65 (registry+https://github.com/rust-lang/crates.io-index)", "nix 0.15.0 (registry+https://github.com/rust-lang/crates.io-index)", "rustc_version 0.2.3 (registry+https://github.com/rust-lang/crates.io-index)", "semver 0.9.0 (registry+https://github.com/rust-lang/crates.io-index)", "toml 0.5.3 (registry+https://github.com/rust-lang/crates.io-index)", + "which 3.1.0 (registry+https://github.com/rust-lang/crates.io-index)", "winapi 0.3.8 (registry+https://github.com/rust-lang/crates.io-index)", ] @@ -69,6 +70,14 @@ dependencies = [ "version_check 0.1.5 (registry+https://github.com/rust-lang/crates.io-index)", ] +[[package]] +name = "failure" +version = "0.1.6" +source = "registry+https://github.com/rust-lang/crates.io-index" +dependencies = [ + "backtrace 0.3.38 (registry+https://github.com/rust-lang/crates.io-index)", +] + [[package]] name = "home" version = "0.5.0" @@ -85,7 +94,7 @@ source = "registry+https://github.com/rust-lang/crates.io-index" [[package]] name = "libc" -version = "0.2.62" +version = "0.2.65" source = "registry+https://github.com/rust-lang/crates.io-index" [[package]] @@ -96,7 +105,7 @@ dependencies = [ "bitflags 1.2.0 (registry+https://github.com/rust-lang/crates.io-index)", "cc 1.0.45 (registry+https://github.com/rust-lang/crates.io-index)", "cfg-if 0.1.10 (registry+https://github.com/rust-lang/crates.io-index)", - "libc 0.2.62 (registry+https://github.com/rust-lang/crates.io-index)", + "libc 0.2.65 (registry+https://github.com/rust-lang/crates.io-index)", "void 1.0.2 (registry+https://github.com/rust-lang/crates.io-index)", ] @@ -154,6 +163,15 @@ name = "void" version = "1.0.2" source = "registry+https://github.com/rust-lang/crates.io-index" +[[package]] +name = "which" +version = "3.1.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +dependencies = [ + "failure 0.1.6 (registry+https://github.com/rust-lang/crates.io-index)", + "libc 0.2.65 (registry+https://github.com/rust-lang/crates.io-index)", +] + [[package]] name = "winapi" version = "0.3.8" @@ -181,9 +199,10 @@ source = "registry+https://github.com/rust-lang/crates.io-index" "checksum cc 1.0.45 (registry+https://github.com/rust-lang/crates.io-index)" = "4fc9a35e1f4290eb9e5fc54ba6cf40671ed2a2514c3eeb2b2a908dda2ea5a1be" "checksum cfg-if 0.1.10 (registry+https://github.com/rust-lang/crates.io-index)" = "4785bdd1c96b2a846b2bd7cc02e86b6b3dbf14e7e53446c4f54c92a361040822" "checksum error-chain 0.12.1 (registry+https://github.com/rust-lang/crates.io-index)" = "3ab49e9dcb602294bc42f9a7dfc9bc6e936fca4418ea300dbfb84fe16de0b7d9" +"checksum failure 0.1.6 (registry+https://github.com/rust-lang/crates.io-index)" = "f8273f13c977665c5db7eb2b99ae520952fe5ac831ae4cd09d80c4c7042b5ed9" "checksum home 0.5.0 (registry+https://github.com/rust-lang/crates.io-index)" = "c07c315e106bd6f83f026a20ddaeef2706782e490db1dcdd37caad38a0e895b3" "checksum lazy_static 1.4.0 (registry+https://github.com/rust-lang/crates.io-index)" = "e2abad23fbc42b3700f2f279844dc832adb2b2eb069b2df918f455c4e18cc646" -"checksum libc 0.2.62 (registry+https://github.com/rust-lang/crates.io-index)" = "34fcd2c08d2f832f376f4173a231990fa5aef4e99fb569867318a227ef4c06ba" +"checksum libc 0.2.65 (registry+https://github.com/rust-lang/crates.io-index)" = "1a31a0627fdf1f6a39ec0dd577e101440b7db22672c0901fe00a9a6fbb5c24e8" "checksum nix 0.15.0 (registry+https://github.com/rust-lang/crates.io-index)" = "3b2e0b4f3320ed72aaedb9a5ac838690a8047c7b275da22711fddff4f8a14229" "checksum rustc-demangle 0.1.16 (registry+https://github.com/rust-lang/crates.io-index)" = "4c691c0e608126e00913e33f0ccf3727d5fc84573623b8d65b2df340b5201783" "checksum rustc_version 0.2.3 (registry+https://github.com/rust-lang/crates.io-index)" = "138e3e0acb6c9fb258b19b67cb8abd63c00679d2851805ea151465464fe9030a" @@ -194,6 +213,7 @@ source = "registry+https://github.com/rust-lang/crates.io-index" "checksum toml 0.5.3 (registry+https://github.com/rust-lang/crates.io-index)" = "c7aabe75941d914b72bf3e5d3932ed92ce0664d49d8432305a8b547c37227724" "checksum version_check 0.1.5 (registry+https://github.com/rust-lang/crates.io-index)" = "914b1a6776c4c929a602fafd8bc742e06365d4bcbe48c30f9cca5824f70dc9dd" "checksum void 1.0.2 (registry+https://github.com/rust-lang/crates.io-index)" = "6a02e4885ed3bc0f2de90ea6dd45ebcbb66dacffe03547fadbb0eeae2770887d" +"checksum which 3.1.0 (registry+https://github.com/rust-lang/crates.io-index)" = "5475d47078209a02e60614f7ba5e645ef3ed60f771920ac1906d7c1cc65024c8" "checksum winapi 0.3.8 (registry+https://github.com/rust-lang/crates.io-index)" = "8093091eeb260906a183e6ae1abdba2ef5ef2257a21801128899c3fc699229c6" "checksum winapi-i686-pc-windows-gnu 0.4.0 (registry+https://github.com/rust-lang/crates.io-index)" = "ac3b87c63620426dd9b991e5ce0329eff545bccbbb34f3be09ff6fb6ab51b7b6" "checksum winapi-x86_64-pc-windows-gnu 0.4.0 (registry+https://github.com/rust-lang/crates.io-index)" = "712e227841d057c1ee1cd2fb22fa7e5a5461ae8e48fa2ca79ec42cfc1931183f" diff --git a/Cargo.toml b/Cargo.toml index 8e4c6ccb1..ca550aa8e 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -19,6 +19,7 @@ libc = "0.2.18" rustc_version = "0.2" semver = "0.9" toml = "0.5" +which = "3.1.0" [target.'cfg(not(windows))'.dependencies] nix = "0.15" diff --git a/README.md b/README.md index 68abb3d01..c5068d92e 100644 --- a/README.md +++ b/README.md @@ -40,7 +40,8 @@ This project is developed and maintained by the [Tools team][team]. [binfmt_misc]: https://www.kernel.org/doc/html/latest/admin-guide/binfmt-misc.html -One of these container engines is required: +One of these container engines is required. If both are installed, `cross` will +default to `docker`. - [Docker](https://www.docker.com/). Note that on Linux non-sudo users need to be in the `docker` group. Read the official [post-installation steps][post]. Requires version @@ -82,18 +83,6 @@ $ cross rustc --target powerpc-unknown-linux-gnu --release -- -C lto You can place a `Cross.toml` file in the root of your Cargo project to tweak `cross`'s behavior: -### Using an Alternative Container Engine - -`cross` allows the user to specify the container engine they would like to use. -Currently, only Docker and Podman are supported. `cross` will default to using -Docker. For other container engines, you can use the -`target.{{TARGET}}.container_engine` field in `Cross.toml`: - -``` toml -[target.aarch64-unknown-linux-gnu] -container_engine = "podman" -``` - ### Custom Docker images `cross` provides default Docker images for the targets listed below. However, it diff --git a/src/docker.rs b/src/docker.rs index 0d2fcd682..1f9894817 100644 --- a/src/docker.rs +++ b/src/docker.rs @@ -13,16 +13,31 @@ use crate::id; const DOCKER_IMAGES: &[&str] = &include!(concat!(env!("OUT_DIR"), "/docker-images.rs")); const DOCKER: &str = "docker"; +const PODMAN: &str = "podman"; -pub fn docker_command(container_engine: &str, subcommand: &str) -> Command { - let mut docker = Command::new(container_engine); - docker.arg(subcommand); - docker.args(&["--userns", "host"]); - docker +fn get_container_engine() -> Option<&'static str> { + if which::which(DOCKER).is_ok() { + Some(DOCKER) + } else if which::which(PODMAN).is_ok() { + Some(PODMAN) + } else { + None + } +} + +pub fn docker_command(subcommand: &str) -> Result { + if let Some(ce) = get_container_engine() { + let mut command = Command::new(ce); + command.arg(subcommand); + command.args(&["--userns", "host"]); + Ok(command) + } else { + Err("no container engine found; install docker or podman".into()) + } } /// Register binfmt interpreters -pub fn register(target: &Target, toml: Option<&Toml>, verbose: bool) -> Result<()> { +pub fn register(target: &Target, verbose: bool) -> Result<()> { let cmd = if target.is_windows() { // https://www.kernel.org/doc/html/latest/admin-guide/binfmt-misc.html "mount binfmt_misc -t binfmt_misc /proc/sys/fs/binfmt_misc && \ @@ -32,14 +47,7 @@ pub fn register(target: &Target, toml: Option<&Toml>, verbose: bool) -> Result<( binfmt-support qemu-user-static" }; - let mut container_engine = DOCKER.to_string(); - if let Some(toml) = toml { - if let Some(engine) = toml.container_engine(target)? { - container_engine = engine; - } - } - - docker_command(&container_engine, "run") + docker_command("run")? .arg("--privileged") .arg("--rm") .arg("ubuntu:16.04") @@ -80,8 +88,7 @@ pub fn run(target: &Target, // container doesn't have write access to the root of the Cargo project let cargo_toml = root.join("Cargo.toml"); - let mut runner = None; - let mut container_engine = DOCKER.to_string(); + let runner = None; Command::new("cargo").args(&["fetch", "--manifest-path", @@ -89,13 +96,7 @@ pub fn run(target: &Target, .run(verbose) .chain_err(|| "couldn't generate Cargo.lock")?; - if let Some(toml) = toml { - runner = toml.runner(target)?; - if let Some(engine) = toml.container_engine(target)? { - container_engine = engine; - } - } - let mut docker = docker_command(&container_engine, "run"); + let mut docker = docker_command("run")?; if let Some(toml) = toml { for var in toml.env_passthrough(target)? { @@ -118,7 +119,7 @@ pub fn run(target: &Target, docker.arg("--rm"); // We need to specify the user for Docker, but not for Podman. - if container_engine == DOCKER { + if let Some(DOCKER) = get_container_engine() { docker.args(&["--user", &format!("{}:{}", id::user(), id::group())]); } diff --git a/src/main.rs b/src/main.rs index 555ee5598..0bcac5365 100644 --- a/src/main.rs +++ b/src/main.rs @@ -276,7 +276,7 @@ fn run() -> Result { needs_interpreter && target.needs_interpreter() && !interpreter::is_registered(&target)? { - docker::register(&target, toml.as_ref(), verbose)? + docker::register(&target, verbose)? } return docker::run(&target, @@ -329,27 +329,6 @@ impl Toml { } } - /// Returns the `target.{}.container_engine` part of `Cross.toml` - pub fn container_engine(&self, target: &Target) -> Result> { - let triple = target.triple(); - - if let Some(value) = self - .table - .get("target") - .and_then(|t| t.get(triple)) - .and_then(|t| t.get("container_engine")) - { - let value = value - .as_str() - .ok_or_else(|| format!("target.{}.container_engine must be a string", triple))? - .to_string(); - Ok(Some(value)) - } else { - Ok(None) - } - } - - /// Returns the `build.image` or the `target.{}.xargo` part of `Cross.toml` pub fn xargo(&self, target: &Target) -> Result> { let triple = target.triple();