Skip to content

Commit 900f981

Browse files
committed
Auto merge of #8758 - ebroto:cargo_primary_package, r=ehuss
Reinstate CARGO_PRIMARY_PACKAGE (take 2) As discussed in [this Zulip thread](https://rust-lang.zulipchat.com/#narrow/stream/246057-t-cargo/topic/RUSTC_WORKSPACE_WRAPPER/near/212461098) r? `@ehuss`
2 parents 655e122 + 6a94a3b commit 900f981

File tree

4 files changed

+181
-1
lines changed

4 files changed

+181
-1
lines changed

src/cargo/core/compiler/mod.rs

+5
Original file line numberDiff line numberDiff line change
@@ -530,6 +530,11 @@ fn prepare_rustc(
530530
let mut base = cx
531531
.compilation
532532
.rustc_process(unit, is_primary, is_workspace)?;
533+
534+
if is_primary {
535+
base.env("CARGO_PRIMARY_PACKAGE", "1");
536+
}
537+
533538
if cx.bcx.config.cli_unstable().jobserver_per_rustc {
534539
let client = cx.new_jobserver()?;
535540
base.inherit_jobserver(&client);

src/doc/src/reference/environment-variables.md

+6
Original file line numberDiff line numberDiff line change
@@ -265,6 +265,12 @@ let out_dir = env::var("OUT_DIR").unwrap();
265265
Currently Cargo doesn't set the `MAKEFLAGS` variable,
266266
but it's free for build scripts invoking GNU Make
267267
to set it to the contents of `CARGO_MAKEFLAGS`.
268+
* `CARGO_PRIMARY_PACKAGE` — This environment variable will be set if the package being
269+
built is primary. Primary packages are the ones the user
270+
selected on the command-line, either with `-p` flags or
271+
the defaults based on the current directory and the default
272+
workspace members.
273+
This environment variable will not be set when building dependencies.
268274
* `CARGO_FEATURE_<name>` — For each activated feature of the package being
269275
built, this environment variable will be present
270276
where `<name>` is the name of the feature uppercased

tests/testsuite/build.rs

+81-1
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ use cargo::{
1010
use cargo_test_support::paths::{root, CargoPathExt};
1111
use cargo_test_support::registry::Package;
1212
use cargo_test_support::{
13-
basic_bin_manifest, basic_lib_manifest, basic_manifest, is_nightly, lines_match_unordered,
13+
basic_bin_manifest, basic_lib_manifest, basic_manifest, git, is_nightly, lines_match_unordered,
1414
main_file, paths, project, rustc_host, sleep_ms, symlink_supported, t, Execs, ProjectBuilder,
1515
};
1616
use std::env;
@@ -5179,3 +5179,83 @@ fn build_script_o0_default_even_with_release() {
51795179
.with_stderr_does_not_contain("[..]build_script_build[..]opt-level[..]")
51805180
.run();
51815181
}
5182+
5183+
#[cargo_test]
5184+
fn primary_package_env_var() {
5185+
// "foo" depends on "bar" (local) which depends on "baz" (git) which depends on "qux" (registry)
5186+
// Test that CARGO_PRIMARY_PACKAGE is enabled only for "foo".
5187+
5188+
Package::new("qux", "1.0.0")
5189+
.file("src/lib.rs", "")
5190+
.publish();
5191+
5192+
let baz = git::new("baz", |project| {
5193+
project
5194+
.file(
5195+
"Cargo.toml",
5196+
r#"
5197+
[package]
5198+
name = "baz"
5199+
version = "1.0.0"
5200+
5201+
[dependencies]
5202+
qux = "1.0"
5203+
"#,
5204+
)
5205+
.file("src/lib.rs", "extern crate qux;")
5206+
});
5207+
5208+
let foo = project()
5209+
.file(
5210+
"Cargo.toml",
5211+
r#"
5212+
[package]
5213+
name = "foo"
5214+
version = "1.0.0"
5215+
5216+
[dependencies]
5217+
bar = {path = "bar"}
5218+
"#,
5219+
)
5220+
.file("src/lib.rs", "extern crate bar;")
5221+
.file(
5222+
"bar/Cargo.toml",
5223+
&format!(
5224+
r#"
5225+
[package]
5226+
name = "bar"
5227+
version = "1.0.0"
5228+
5229+
[dependencies]
5230+
baz = {{ git = '{}' }}
5231+
"#,
5232+
baz.url()
5233+
),
5234+
)
5235+
.file("bar/src/lib.rs", "extern crate baz;")
5236+
.build();
5237+
5238+
foo.cargo("build -vv")
5239+
.with_stderr_contains("[COMPILING] qux[..]")
5240+
.with_stderr_line_without(
5241+
&["[RUNNING]", "CARGO_CRATE_NAME=qux"],
5242+
&["CARGO_PRIMARY_PACKAGE=1"],
5243+
)
5244+
.with_stderr_contains("[COMPILING] baz[..]")
5245+
.with_stderr_line_without(
5246+
&["[RUNNING]", "CARGO_CRATE_NAME=baz"],
5247+
&["CARGO_PRIMARY_PACKAGE=1"],
5248+
)
5249+
.with_stderr_contains("[COMPILING] bar[..]")
5250+
.with_stderr_line_without(
5251+
&["[RUNNING]", "CARGO_CRATE_NAME=bar"],
5252+
&["CARGO_PRIMARY_PACKAGE=1"],
5253+
)
5254+
.with_stderr_contains(
5255+
"\
5256+
[COMPILING] foo[..]
5257+
[RUNNING] [..] CARGO_CRATE_NAME=foo [..] CARGO_PRIMARY_PACKAGE=1 [..]
5258+
[FINISHED] dev [unoptimized + debuginfo] target(s) in [..]",
5259+
)
5260+
.run();
5261+
}

tests/testsuite/workspaces.rs

+89
Original file line numberDiff line numberDiff line change
@@ -2319,3 +2319,92 @@ Caused by:
23192319
)
23202320
.run();
23212321
}
2322+
2323+
#[cargo_test]
2324+
fn simple_primary_package_env_var() {
2325+
let p = project()
2326+
.file(
2327+
"Cargo.toml",
2328+
r#"
2329+
[project]
2330+
name = "foo"
2331+
version = "0.1.0"
2332+
authors = []
2333+
2334+
[workspace]
2335+
members = ["bar"]
2336+
"#,
2337+
)
2338+
.file("src/main.rs", "fn main() {}")
2339+
.file(
2340+
"bar/Cargo.toml",
2341+
r#"
2342+
[project]
2343+
name = "bar"
2344+
version = "0.1.0"
2345+
authors = []
2346+
workspace = ".."
2347+
"#,
2348+
)
2349+
.file("bar/src/main.rs", "fn main() {}");
2350+
let p = p.build();
2351+
2352+
p.cargo("build -vv")
2353+
.with_stderr_contains(
2354+
"[RUNNING] [..] CARGO_CRATE_NAME=foo [..] CARGO_PRIMARY_PACKAGE=1 [..]",
2355+
)
2356+
.run();
2357+
2358+
// Again, this time selecting a specific crate
2359+
p.cargo("clean").run();
2360+
p.cargo("build -vv -p bar")
2361+
.with_stderr_contains(
2362+
"[RUNNING] [..] CARGO_CRATE_NAME=bar [..] CARGO_PRIMARY_PACKAGE=1 [..]",
2363+
)
2364+
.run();
2365+
2366+
// Again, this time selecting all crates
2367+
p.cargo("clean").run();
2368+
p.cargo("build -vv --all")
2369+
.with_stderr_contains(
2370+
"[RUNNING] [..] CARGO_CRATE_NAME=foo [..] CARGO_PRIMARY_PACKAGE=1 [..]",
2371+
)
2372+
.with_stderr_contains(
2373+
"[RUNNING] [..] CARGO_CRATE_NAME=bar [..] CARGO_PRIMARY_PACKAGE=1 [..]",
2374+
)
2375+
.run();
2376+
}
2377+
2378+
#[cargo_test]
2379+
fn virtual_primary_package_env_var() {
2380+
let p = project()
2381+
.file(
2382+
"Cargo.toml",
2383+
r#"
2384+
[workspace]
2385+
members = ["foo", "bar"]
2386+
"#,
2387+
)
2388+
.file("foo/Cargo.toml", &basic_manifest("foo", "0.1.0"))
2389+
.file("foo/src/main.rs", "fn main() {}")
2390+
.file("bar/Cargo.toml", &basic_manifest("bar", "0.1.0"))
2391+
.file("bar/src/main.rs", "fn main() {}");
2392+
let p = p.build();
2393+
2394+
p.cargo("build -vv")
2395+
.with_stderr_contains(
2396+
"[RUNNING] [..] CARGO_CRATE_NAME=foo [..] CARGO_PRIMARY_PACKAGE=1 [..]",
2397+
)
2398+
.with_stderr_contains(
2399+
"[RUNNING] [..] CARGO_CRATE_NAME=bar [..] CARGO_PRIMARY_PACKAGE=1 [..]",
2400+
)
2401+
.run();
2402+
2403+
// Again, this time selecting a specific crate
2404+
p.cargo("clean").run();
2405+
p.cargo("build -vv -p foo")
2406+
.with_stderr_contains(
2407+
"[RUNNING] [..] CARGO_CRATE_NAME=foo [..] CARGO_PRIMARY_PACKAGE=1 [..]",
2408+
)
2409+
.run();
2410+
}

0 commit comments

Comments
 (0)