Skip to content

Commit 12db56c

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 + fb02ade commit 12db56c

File tree

4 files changed

+154
-1
lines changed

4 files changed

+154
-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

+66-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,68 @@ 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+
// Test that CARGO_PRIMARY_PACKAGE is enabled only for "foo" and not for any dependency.
5186+
5187+
let is_primary_package = r#"
5188+
pub fn is_primary_package() -> bool {{
5189+
option_env!("CARGO_PRIMARY_PACKAGE").is_some()
5190+
}}
5191+
"#;
5192+
5193+
Package::new("qux", "0.1.0")
5194+
.file("src/lib.rs", is_primary_package)
5195+
.publish();
5196+
5197+
let baz = git::new("baz", |project| {
5198+
project
5199+
.file("Cargo.toml", &basic_manifest("baz", "0.1.0"))
5200+
.file("src/lib.rs", is_primary_package)
5201+
});
5202+
5203+
let foo = project()
5204+
.file(
5205+
"Cargo.toml",
5206+
&format!(
5207+
r#"
5208+
[package]
5209+
name = "foo"
5210+
version = "0.1.0"
5211+
5212+
[dependencies]
5213+
bar = {{ path = "bar" }}
5214+
baz = {{ git = '{}' }}
5215+
qux = "0.1"
5216+
"#,
5217+
baz.url()
5218+
),
5219+
)
5220+
.file(
5221+
"src/lib.rs",
5222+
&format!(
5223+
r#"
5224+
extern crate bar;
5225+
extern crate baz;
5226+
extern crate qux;
5227+
5228+
{}
5229+
5230+
#[test]
5231+
fn verify_primary_package() {{
5232+
assert!(!bar::is_primary_package());
5233+
assert!(!baz::is_primary_package());
5234+
assert!(!qux::is_primary_package());
5235+
assert!(is_primary_package());
5236+
}}
5237+
"#,
5238+
is_primary_package
5239+
),
5240+
)
5241+
.file("bar/Cargo.toml", &basic_manifest("bar", "0.1.0"))
5242+
.file("bar/src/lib.rs", is_primary_package)
5243+
.build();
5244+
5245+
foo.cargo("test").run();
5246+
}

tests/testsuite/workspaces.rs

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

0 commit comments

Comments
 (0)