Skip to content

Commit 91223e4

Browse files
committed
Removing container_engine option and adding auto-detection
- Instead of requiring the user to specify which container engine they would prefer via the container_engine option, cross will now check to see which one is installed and use it. Defaults to docker if both are installed.
1 parent 42f2410 commit 91223e4

File tree

5 files changed

+56
-66
lines changed

5 files changed

+56
-66
lines changed

Cargo.lock

Lines changed: 27 additions & 7 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Cargo.toml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ libc = "0.2.18"
1919
rustc_version = "0.2"
2020
semver = "0.9"
2121
toml = "0.5"
22+
which = "3.1.0"
2223

2324
[target.'cfg(not(windows))'.dependencies]
2425
nix = "0.15"

README.md

Lines changed: 2 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,8 @@ This project is developed and maintained by the [Tools team][team].
4040

4141
[binfmt_misc]: https://www.kernel.org/doc/html/latest/admin-guide/binfmt-misc.html
4242

43-
One of these container engines is required:
43+
One of these container engines is required. If both are installed, `cross` will
44+
default to `docker`.
4445

4546
- [Docker](https://www.docker.com/). Note that on Linux non-sudo users need to be in the
4647
`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
8283
You can place a `Cross.toml` file in the root of your Cargo project to tweak
8384
`cross`'s behavior:
8485

85-
### Using an Alternative Container Engine
86-
87-
`cross` allows the user to specify the container engine they would like to use.
88-
Currently, only Docker and Podman are supported. `cross` will default to using
89-
Docker. For other container engines, you can use the
90-
`target.{{TARGET}}.container_engine` field in `Cross.toml`:
91-
92-
``` toml
93-
[target.aarch64-unknown-linux-gnu]
94-
container_engine = "podman"
95-
```
96-
9786
### Custom Docker images
9887

9988
`cross` provides default Docker images for the targets listed below. However, it

src/docker.rs

Lines changed: 25 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -13,16 +13,31 @@ use crate::id;
1313

1414
const DOCKER_IMAGES: &[&str] = &include!(concat!(env!("OUT_DIR"), "/docker-images.rs"));
1515
const DOCKER: &str = "docker";
16+
const PODMAN: &str = "podman";
1617

17-
pub fn docker_command(container_engine: &str, subcommand: &str) -> Command {
18-
let mut docker = Command::new(container_engine);
19-
docker.arg(subcommand);
20-
docker.args(&["--userns", "host"]);
21-
docker
18+
fn get_container_engine() -> Option<&'static str> {
19+
if which::which(DOCKER).is_ok() {
20+
Some(DOCKER)
21+
} else if which::which(PODMAN).is_ok() {
22+
Some(PODMAN)
23+
} else {
24+
None
25+
}
26+
}
27+
28+
pub fn docker_command(subcommand: &str) -> Result<Command> {
29+
if let Some(ce) = get_container_engine() {
30+
let mut command = Command::new(ce);
31+
command.arg(subcommand);
32+
command.args(&["--userns", "host"]);
33+
Ok(command)
34+
} else {
35+
Err("no container engine found; install docker or podman".into())
36+
}
2237
}
2338

2439
/// Register binfmt interpreters
25-
pub fn register(target: &Target, toml: Option<&Toml>, verbose: bool) -> Result<()> {
40+
pub fn register(target: &Target, verbose: bool) -> Result<()> {
2641
let cmd = if target.is_windows() {
2742
// https://www.kernel.org/doc/html/latest/admin-guide/binfmt-misc.html
2843
"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<(
3247
binfmt-support qemu-user-static"
3348
};
3449

35-
let mut container_engine = DOCKER.to_string();
36-
if let Some(toml) = toml {
37-
if let Some(engine) = toml.container_engine(target)? {
38-
container_engine = engine;
39-
}
40-
}
41-
42-
docker_command(&container_engine, "run")
50+
docker_command("run")?
4351
.arg("--privileged")
4452
.arg("--rm")
4553
.arg("ubuntu:16.04")
@@ -80,22 +88,15 @@ pub fn run(target: &Target,
8088
// container doesn't have write access to the root of the Cargo project
8189
let cargo_toml = root.join("Cargo.toml");
8290

83-
let mut runner = None;
84-
let mut container_engine = DOCKER.to_string();
91+
let runner = None;
8592

8693
Command::new("cargo").args(&["fetch",
8794
"--manifest-path",
8895
&cargo_toml.display().to_string()])
8996
.run(verbose)
9097
.chain_err(|| "couldn't generate Cargo.lock")?;
9198

92-
if let Some(toml) = toml {
93-
runner = toml.runner(target)?;
94-
if let Some(engine) = toml.container_engine(target)? {
95-
container_engine = engine;
96-
}
97-
}
98-
let mut docker = docker_command(&container_engine, "run");
99+
let mut docker = docker_command("run")?;
99100

100101
if let Some(toml) = toml {
101102
for var in toml.env_passthrough(target)? {
@@ -118,7 +119,7 @@ pub fn run(target: &Target,
118119
docker.arg("--rm");
119120

120121
// We need to specify the user for Docker, but not for Podman.
121-
if container_engine == DOCKER {
122+
if let Some(DOCKER) = get_container_engine() {
122123
docker.args(&["--user", &format!("{}:{}", id::user(), id::group())]);
123124
}
124125

src/main.rs

Lines changed: 1 addition & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -276,7 +276,7 @@ fn run() -> Result<ExitStatus> {
276276
needs_interpreter &&
277277
target.needs_interpreter() &&
278278
!interpreter::is_registered(&target)? {
279-
docker::register(&target, toml.as_ref(), verbose)?
279+
docker::register(&target, verbose)?
280280
}
281281

282282
return docker::run(&target,
@@ -329,27 +329,6 @@ impl Toml {
329329
}
330330
}
331331

332-
/// Returns the `target.{}.container_engine` part of `Cross.toml`
333-
pub fn container_engine(&self, target: &Target) -> Result<Option<String>> {
334-
let triple = target.triple();
335-
336-
if let Some(value) = self
337-
.table
338-
.get("target")
339-
.and_then(|t| t.get(triple))
340-
.and_then(|t| t.get("container_engine"))
341-
{
342-
let value = value
343-
.as_str()
344-
.ok_or_else(|| format!("target.{}.container_engine must be a string", triple))?
345-
.to_string();
346-
Ok(Some(value))
347-
} else {
348-
Ok(None)
349-
}
350-
}
351-
352-
353332
/// Returns the `build.image` or the `target.{}.xargo` part of `Cross.toml`
354333
pub fn xargo(&self, target: &Target) -> Result<Option<bool>> {
355334
let triple = target.triple();

0 commit comments

Comments
 (0)