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

Add support for building all members of the workspace with "build --all" #3511

Merged
merged 2 commits into from
Jan 9, 2017
Merged
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
15 changes: 13 additions & 2 deletions src/bin/build.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
use std::env;

use cargo::core::Workspace;
use cargo::ops::{self, CompileOptions, MessageFormat};
use cargo::ops::{self, CompileOptions, MessageFormat, Packages};
use cargo::util::important_paths::{find_root_manifest_for_wd};
use cargo::util::{CliResult, Config};

Expand All @@ -26,6 +26,7 @@ pub struct Options {
flag_bench: Vec<String>,
flag_locked: bool,
flag_frozen: bool,
flag_all: bool,
}

pub const USAGE: &'static str = "
Expand All @@ -37,6 +38,7 @@ Usage:
Options:
-h, --help Print this message
-p SPEC, --package SPEC ... Package to build
--all Build all packages in the workspace
-j N, --jobs N Number of parallel jobs, defaults to # of CPUs
--lib Build only this package's library
--bin NAME Build only the specified binary
Expand All @@ -61,6 +63,9 @@ which indicates which package should be built. If it is not given, then the
current package is built. For more information on SPEC and its format, see the
`cargo help pkgid` command.

All packages in the workspace are built if the `--all` flag is supplied. The
`--all` flag may be supplied in the presence of a virtual manifest.

Compilation can be configured via the use of profiles which are configured in
the manifest. The default profile for this command is `dev`, but passing
the --release flag will use the `release` profile instead.
Expand All @@ -77,14 +82,20 @@ pub fn execute(options: Options, config: &Config) -> CliResult<Option<()>> {

let root = find_root_manifest_for_wd(options.flag_manifest_path, config.cwd())?;

let spec = if options.flag_all {
Packages::All
} else {
Packages::Packages(&options.flag_package)
};

let opts = CompileOptions {
config: config,
jobs: options.flag_jobs,
target: options.flag_target.as_ref().map(|t| &t[..]),
features: &options.flag_features,
all_features: options.flag_all_features,
no_default_features: options.flag_no_default_features,
spec: ops::Packages::Packages(&options.flag_package),
spec: spec,
mode: ops::CompileMode::Build,
release: options.flag_release,
filter: ops::CompileFilter::new(options.flag_lib,
Expand Down
101 changes: 101 additions & 0 deletions tests/build.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ use cargotest::{is_nightly, rustc_host, sleep_ms};
use cargotest::support::paths::{CargoPathExt,root};
use cargotest::support::{ProjectBuilder};
use cargotest::support::{project, execs, main_file, basic_bin_manifest};
use cargotest::support::registry::Package;
use hamcrest::{assert_that, existing_file, is_not};
use tempdir::TempDir;

Expand Down Expand Up @@ -2554,3 +2555,103 @@ fn cargo_build_empty_target() {
execs().with_status(101)
.with_stderr_contains("[..] target was empty"));
}

#[test]
fn build_all_workspace() {
let p = project("foo")
.file("Cargo.toml", r#"
[project]
name = "foo"
version = "0.1.0"

[dependencies]
bar = { path = "bar" }

[workspace]
"#)
.file("src/main.rs", r#"
fn main() {}
"#)
.file("bar/Cargo.toml", r#"
[project]
name = "bar"
version = "0.1.0"
"#)
.file("bar/src/lib.rs", r#"
pub fn bar() {}
"#);
p.build();

assert_that(p.cargo_process("build")
.arg("--all"),
execs().with_stderr("[..] Compiling bar v0.1.0 ([..])\n\
[..] Compiling foo v0.1.0 ([..])\n\
[..] Finished debug [unoptimized + debuginfo] target(s) in [..]\n"));
}

#[test]
fn build_all_virtual_manifest() {
let p = project("workspace")
.file("Cargo.toml", r#"
[workspace]
members = ["foo", "bar"]
Copy link
Contributor Author

Choose a reason for hiding this comment

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

These are called "a" and "b" in the "test --all" test, but doing so here would make the test fail on the second run as the output would be different: it would use the "a" that is published to the registry in the following test.

Might want to change that in the "test --all" test too, it's running different things on a clean build and on the second and following times.

"#)
.file("foo/Cargo.toml", r#"
[project]
name = "foo"
version = "0.1.0"
"#)
.file("foo/src/lib.rs", r#"
pub fn foo() {}
"#)
.file("bar/Cargo.toml", r#"
[project]
name = "bar"
version = "0.1.0"
"#)
.file("bar/src/lib.rs", r#"
pub fn bar() {}
"#);
p.build();

// The order in which foo and bar are built is not guaranteed
assert_that(p.cargo_process("build")
.arg("--all"),
execs().with_stderr_contains("[..] Compiling bar v0.1.0 ([..])")
.with_stderr_contains("[..] Compiling foo v0.1.0 ([..])")
.with_stderr("[..] Compiling [..] v0.1.0 ([..])\n\
[..] Compiling [..] v0.1.0 ([..])\n\
[..] Finished debug [unoptimized + debuginfo] target(s) in [..]\n"));
}

#[test]
fn build_all_member_dependency_same_name() {
let p = project("workspace")
.file("Cargo.toml", r#"
[workspace]
members = ["a"]
"#)
.file("a/Cargo.toml", r#"
[project]
name = "a"
version = "0.1.0"

[dependencies]
a = "0.1.0"
"#)
.file("a/src/lib.rs", r#"
pub fn a() {}
"#);
p.build();

Package::new("a", "0.1.0").publish();

assert_that(p.cargo_process("build")
.arg("--all"),
execs().with_stderr("[..] Updating registry `[..]`\n\
[..] Downloading a v0.1.0 ([..])\n\
[..] Compiling a v0.1.0\n\
[..] Compiling a v0.1.0 ([..])\n\
[..] Finished debug [unoptimized + debuginfo] target(s) in [..]\n"));
}