Skip to content

Commit

Permalink
feat: Parse Cargo's --manifest-path option to determine mounted doc…
Browse files Browse the repository at this point in the history
…ker root

This commits adds support for parsing the `--manifest-path` option to cross. So
far, the `Cargo.toml` manifest of the crate (or its Cargo workspace) to compile
has been assumed to be in the current working directory. This means, that
relative crate dependencies were not supported, because those paths were not
mapped into the docker container.

Take the following example structure, where `my-bin` depends on `my-lib`:
.
├── my-bin
│   ├── Cargo.toml
│   └── src
│       └── main.rs
└── my-lib
    ├── Cargo.toml
    └── src
        └── lib.rs

This commits enables such scenarios, by running cross from `.` like so:
`cross build --manifest-path=my-lib/Cargo.toml --target x86_64-pc-windows-gnu`,
as `.` is mapped as the container's root, and the options passed through to
Cargo.

Related cross-rs#388 cross-rs#139 cross-rs#277 cross-rs#78

Co-authored-by: Kviring Alexey <[email protected]>
  • Loading branch information
2 people authored and datdenkikniet committed Jan 28, 2022
1 parent 95739b6 commit 3cb0c90
Show file tree
Hide file tree
Showing 5 changed files with 31 additions and 6 deletions.
3 changes: 3 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -354,6 +354,9 @@ $ QEMU_STRACE=1 cross run --target aarch64-unknown-linux-gnu
- path dependencies (in Cargo.toml) that point outside the Cargo project won't
work because `cross` use docker containers only mounts the Cargo project so
the container doesn't have access to the rest of the filesystem.
However, you may use Cargo's `--manifest-path` option to reference your
target crate, executed from a common root directory from which all your
dependencies are available.

## Minimum Supported Rust Version (MSRV)

Expand Down
7 changes: 5 additions & 2 deletions src/cargo.rs
Original file line number Diff line number Diff line change
Expand Up @@ -73,8 +73,11 @@ impl Root {
}

/// Cargo project root
pub fn root() -> Result<Option<Root>> {
let cd = env::current_dir().chain_err(|| "couldn't get current directory")?;
pub fn root(project_dir: Option<PathBuf>) -> Result<Option<Root>> {
let cd = match project_dir {
Some(dir) => dir,
None => env::current_dir().chain_err(|| "couldn't get project directory")?,
};

let mut dir = &*cd;
loop {
Expand Down
18 changes: 17 additions & 1 deletion src/cli.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,11 +13,13 @@ pub struct Args {
pub target: Option<Target>,
pub target_dir: Option<PathBuf>,
pub docker_in_docker: bool,
pub project_dir: Option<PathBuf>,
}

pub fn parse(target_list: &TargetList) -> Args {
let mut channel = None;
let mut target = None;
let mut project_dir: Option<PathBuf> = None;
let mut target_dir = None;
let mut sc = None;
let mut all: Vec<String> = Vec::new();
Expand All @@ -28,7 +30,20 @@ pub fn parse(target_list: &TargetList) -> Args {
if arg.is_empty() {
continue;
}
if let ("+", ch) = arg.split_at(1) {
if arg == "--manifest-path" {
project_dir = args
.next()
.map(PathBuf::from)
.and_then(|p| env::current_dir().ok().map(|cwd| cwd.join(p)));
all.push(arg);
} else if arg.starts_with("--manifest-path=") {
project_dir = arg
.splitn(2, '=')
.nth(1)
.map(PathBuf::from)
.and_then(|p| env::current_dir().ok().map(|cwd| cwd.join(p)));
all.push(arg);
} else if let ("+", ch) = arg.split_at(1) {
channel = Some(ch.to_string());
} else if arg == "--target" {
all.push(arg);
Expand Down Expand Up @@ -74,5 +89,6 @@ pub fn parse(target_list: &TargetList) -> Args {
target,
target_dir,
docker_in_docker,
project_dir,
}
}
3 changes: 2 additions & 1 deletion src/docker.rs
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,7 @@ pub fn run(
args: &[String],
target_dir: &Option<PathBuf>,
root: &Root,
docker_root: &PathBuf,
config: &Config,
uses_xargo: bool,
sysroot: &PathBuf,
Expand Down Expand Up @@ -93,7 +94,7 @@ pub fn run(
let cargo_dir = mount_finder.find_mount_path(&cargo_dir);
let xargo_dir = mount_finder.find_mount_path(&xargo_dir);
let target_dir = mount_finder.find_mount_path(&target_dir);
let mount_root = mount_finder.find_mount_path(&root);
let mount_root = mount_finder.find_mount_path(&docker_root);
let sysroot = mount_finder.find_mount_path(&sysroot);

let mut cmd = if uses_xargo {
Expand Down
6 changes: 4 additions & 2 deletions src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -271,8 +271,8 @@ fn run() -> Result<ExitStatus> {
.any(|a| a == "--verbose" || a == "-v" || a == "-vv");

let version_meta =
rustc_version::version_meta().chain_err(|| "couldn'toml.t fetch the `rustc` version")?;
if let Some(root) = cargo::root()? {
rustc_version::version_meta().chain_err(|| "couldn'toml.t fetch the `rustc` version")?;
if let Some(root) = cargo::root(args.project_dir)? {
let host = version_meta.host();

if host.is_supported(args.target.as_ref()) {
Expand Down Expand Up @@ -372,11 +372,13 @@ fn run() -> Result<ExitStatus> {
docker::register(&target, verbose)?
}

let docker_root = env::current_dir()?;
return docker::run(
&target,
&filtered_args,
&args.target_dir,
&root,
&docker_root,
&config,
uses_xargo,
&sysroot,
Expand Down

0 comments on commit 3cb0c90

Please sign in to comment.