Skip to content

Commit b9497ab

Browse files
committed
Fix --all-targets in a crate without a lib.
Fixes #4615.
1 parent 8be175e commit b9497ab

File tree

2 files changed

+34
-3
lines changed

2 files changed

+34
-3
lines changed

src/cargo/ops/cargo_compile.rs

+6-3
Original file line numberDiff line numberDiff line change
@@ -170,6 +170,7 @@ pub enum CompileFilter<'a> {
170170
required_features_filterable: bool,
171171
},
172172
Only {
173+
all_targets: bool,
173174
lib: bool,
174175
bins: FilterRule<'a>,
175176
examples: FilterRule<'a>,
@@ -389,13 +390,15 @@ impl<'a> CompileFilter<'a> {
389390

390391
if all_targets {
391392
CompileFilter::Only {
393+
all_targets: true,
392394
lib: true, bins: FilterRule::All,
393395
examples: FilterRule::All, benches: FilterRule::All,
394396
tests: FilterRule::All,
395397
}
396398
} else if lib_only || rule_bins.is_specific() || rule_tsts.is_specific()
397399
|| rule_exms.is_specific() || rule_bens.is_specific() {
398400
CompileFilter::Only {
401+
all_targets: false,
399402
lib: lib_only, bins: rule_bins,
400403
examples: rule_exms, benches: rule_bens,
401404
tests: rule_tsts,
@@ -410,7 +413,7 @@ impl<'a> CompileFilter<'a> {
410413
pub fn matches(&self, target: &Target) -> bool {
411414
match *self {
412415
CompileFilter::Default { .. } => true,
413-
CompileFilter::Only { lib, bins, examples, tests, benches } => {
416+
CompileFilter::Only { lib, bins, examples, tests, benches, .. } => {
414417
let rule = match *target.kind() {
415418
TargetKind::Bin => bins,
416419
TargetKind::Test => tests,
@@ -637,7 +640,7 @@ fn generate_targets<'a>(pkg: &'a Package,
637640
};
638641
generate_auto_targets(mode, pkg.targets(), profile, deps, required_features_filterable)
639642
}
640-
CompileFilter::Only { lib, bins, examples, tests, benches } => {
643+
CompileFilter::Only { all_targets, lib, bins, examples, tests, benches } => {
641644
let mut targets = Vec::new();
642645

643646
if lib {
@@ -647,7 +650,7 @@ fn generate_targets<'a>(pkg: &'a Package,
647650
profile: profile,
648651
required: true,
649652
});
650-
} else {
653+
} else if !all_targets {
651654
bail!("no library targets found")
652655
}
653656
}

tests/build.rs

+28
Original file line numberDiff line numberDiff line change
@@ -3943,3 +3943,31 @@ fn build_filter_infer_profile() {
39433943
--emit=dep-info,link[..]")
39443944
);
39453945
}
3946+
3947+
#[test]
3948+
fn all_targets_no_lib() {
3949+
let p = project("foo")
3950+
.file("Cargo.toml", r#"
3951+
[package]
3952+
name = "foo"
3953+
version = "0.1.0"
3954+
authors = []
3955+
"#)
3956+
.file("src/main.rs", "fn main() {}")
3957+
.build();
3958+
assert_that(p.cargo("build").arg("-v").arg("--all-targets"),
3959+
execs().with_status(0)
3960+
// bin
3961+
.with_stderr_contains("\
3962+
[RUNNING] `rustc --crate-name foo src[/]main.rs --crate-type bin \
3963+
--emit=dep-info,link[..]")
3964+
// bench
3965+
.with_stderr_contains("\
3966+
[RUNNING] `rustc --crate-name foo src[/]main.rs --emit=dep-info,link \
3967+
-C opt-level=3 --test [..]")
3968+
// unit test
3969+
.with_stderr_contains("\
3970+
[RUNNING] `rustc --crate-name foo src[/]main.rs --emit=dep-info,link \
3971+
-C debuginfo=2 --test [..]")
3972+
);
3973+
}

0 commit comments

Comments
 (0)