Skip to content

Commit ecf6f98

Browse files
committed
Auto merge of #3511 - sdroege:build-all, r=alexcrichton
Add support for building all members of the workspace with "build --all" #3491
2 parents f70194a + 4df1375 commit ecf6f98

File tree

2 files changed

+114
-2
lines changed

2 files changed

+114
-2
lines changed

src/bin/build.rs

+13-2
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
use std::env;
22

33
use cargo::core::Workspace;
4-
use cargo::ops::{self, CompileOptions, MessageFormat};
4+
use cargo::ops::{self, CompileOptions, MessageFormat, Packages};
55
use cargo::util::important_paths::{find_root_manifest_for_wd};
66
use cargo::util::{CliResult, Config};
77

@@ -26,6 +26,7 @@ pub struct Options {
2626
flag_bench: Vec<String>,
2727
flag_locked: bool,
2828
flag_frozen: bool,
29+
flag_all: bool,
2930
}
3031

3132
pub const USAGE: &'static str = "
@@ -37,6 +38,7 @@ Usage:
3738
Options:
3839
-h, --help Print this message
3940
-p SPEC, --package SPEC ... Package to build
41+
--all Build all packages in the workspace
4042
-j N, --jobs N Number of parallel jobs, defaults to # of CPUs
4143
--lib Build only this package's library
4244
--bin NAME Build only the specified binary
@@ -61,6 +63,9 @@ which indicates which package should be built. If it is not given, then the
6163
current package is built. For more information on SPEC and its format, see the
6264
`cargo help pkgid` command.
6365
66+
All packages in the workspace are built if the `--all` flag is supplied. The
67+
`--all` flag may be supplied in the presence of a virtual manifest.
68+
6469
Compilation can be configured via the use of profiles which are configured in
6570
the manifest. The default profile for this command is `dev`, but passing
6671
the --release flag will use the `release` profile instead.
@@ -77,14 +82,20 @@ pub fn execute(options: Options, config: &Config) -> CliResult<Option<()>> {
7782

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

85+
let spec = if options.flag_all {
86+
Packages::All
87+
} else {
88+
Packages::Packages(&options.flag_package)
89+
};
90+
8091
let opts = CompileOptions {
8192
config: config,
8293
jobs: options.flag_jobs,
8394
target: options.flag_target.as_ref().map(|t| &t[..]),
8495
features: &options.flag_features,
8596
all_features: options.flag_all_features,
8697
no_default_features: options.flag_no_default_features,
87-
spec: ops::Packages::Packages(&options.flag_package),
98+
spec: spec,
8899
mode: ops::CompileMode::Build,
89100
release: options.flag_release,
90101
filter: ops::CompileFilter::new(options.flag_lib,

tests/build.rs

+101
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ use cargotest::{is_nightly, rustc_host, sleep_ms};
1212
use cargotest::support::paths::{CargoPathExt,root};
1313
use cargotest::support::{ProjectBuilder};
1414
use cargotest::support::{project, execs, main_file, basic_bin_manifest};
15+
use cargotest::support::registry::Package;
1516
use hamcrest::{assert_that, existing_file, is_not};
1617
use tempdir::TempDir;
1718

@@ -2554,3 +2555,103 @@ fn cargo_build_empty_target() {
25542555
execs().with_status(101)
25552556
.with_stderr_contains("[..] target was empty"));
25562557
}
2558+
2559+
#[test]
2560+
fn build_all_workspace() {
2561+
let p = project("foo")
2562+
.file("Cargo.toml", r#"
2563+
[project]
2564+
name = "foo"
2565+
version = "0.1.0"
2566+
2567+
[dependencies]
2568+
bar = { path = "bar" }
2569+
2570+
[workspace]
2571+
"#)
2572+
.file("src/main.rs", r#"
2573+
fn main() {}
2574+
"#)
2575+
.file("bar/Cargo.toml", r#"
2576+
[project]
2577+
name = "bar"
2578+
version = "0.1.0"
2579+
"#)
2580+
.file("bar/src/lib.rs", r#"
2581+
pub fn bar() {}
2582+
"#);
2583+
p.build();
2584+
2585+
assert_that(p.cargo_process("build")
2586+
.arg("--all"),
2587+
execs().with_stderr("[..] Compiling bar v0.1.0 ([..])\n\
2588+
[..] Compiling foo v0.1.0 ([..])\n\
2589+
[..] Finished debug [unoptimized + debuginfo] target(s) in [..]\n"));
2590+
}
2591+
2592+
#[test]
2593+
fn build_all_virtual_manifest() {
2594+
let p = project("workspace")
2595+
.file("Cargo.toml", r#"
2596+
[workspace]
2597+
members = ["foo", "bar"]
2598+
"#)
2599+
.file("foo/Cargo.toml", r#"
2600+
[project]
2601+
name = "foo"
2602+
version = "0.1.0"
2603+
"#)
2604+
.file("foo/src/lib.rs", r#"
2605+
pub fn foo() {}
2606+
"#)
2607+
.file("bar/Cargo.toml", r#"
2608+
[project]
2609+
name = "bar"
2610+
version = "0.1.0"
2611+
"#)
2612+
.file("bar/src/lib.rs", r#"
2613+
pub fn bar() {}
2614+
"#);
2615+
p.build();
2616+
2617+
// The order in which foo and bar are built is not guaranteed
2618+
assert_that(p.cargo_process("build")
2619+
.arg("--all"),
2620+
execs().with_stderr_contains("[..] Compiling bar v0.1.0 ([..])")
2621+
.with_stderr_contains("[..] Compiling foo v0.1.0 ([..])")
2622+
.with_stderr("[..] Compiling [..] v0.1.0 ([..])\n\
2623+
[..] Compiling [..] v0.1.0 ([..])\n\
2624+
[..] Finished debug [unoptimized + debuginfo] target(s) in [..]\n"));
2625+
}
2626+
2627+
#[test]
2628+
fn build_all_member_dependency_same_name() {
2629+
let p = project("workspace")
2630+
.file("Cargo.toml", r#"
2631+
[workspace]
2632+
members = ["a"]
2633+
"#)
2634+
.file("a/Cargo.toml", r#"
2635+
[project]
2636+
name = "a"
2637+
version = "0.1.0"
2638+
2639+
[dependencies]
2640+
a = "0.1.0"
2641+
"#)
2642+
.file("a/src/lib.rs", r#"
2643+
pub fn a() {}
2644+
"#);
2645+
p.build();
2646+
2647+
Package::new("a", "0.1.0").publish();
2648+
2649+
assert_that(p.cargo_process("build")
2650+
.arg("--all"),
2651+
execs().with_stderr("[..] Updating registry `[..]`\n\
2652+
[..] Downloading a v0.1.0 ([..])\n\
2653+
[..] Compiling a v0.1.0\n\
2654+
[..] Compiling a v0.1.0 ([..])\n\
2655+
[..] Finished debug [unoptimized + debuginfo] target(s) in [..]\n"));
2656+
}
2657+

0 commit comments

Comments
 (0)