From 6a0a35ca453c9ba0c82e086ed2515c1f90b27720 Mon Sep 17 00:00:00 2001 From: Eduardo Broto Date: Tue, 6 Oct 2020 23:06:44 +0200 Subject: [PATCH 1/5] Reinstate CARGO_PRIMARY_PACKAGE --- src/cargo/core/compiler/mod.rs | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/src/cargo/core/compiler/mod.rs b/src/cargo/core/compiler/mod.rs index 79d8c47abed..0fc9a8145d9 100644 --- a/src/cargo/core/compiler/mod.rs +++ b/src/cargo/core/compiler/mod.rs @@ -530,6 +530,11 @@ fn prepare_rustc( let mut base = cx .compilation .rustc_process(unit, is_primary, is_workspace)?; + + if is_primary { + base.env("CARGO_PRIMARY_PACKAGE", "1"); + } + if cx.bcx.config.cli_unstable().jobserver_per_rustc { let client = cx.new_jobserver()?; base.inherit_jobserver(&client); From f7ddbed11e48d573859bc26ebd6548b4c9d35bcf Mon Sep 17 00:00:00 2001 From: Eduardo Broto Date: Mon, 12 Oct 2020 15:25:33 +0200 Subject: [PATCH 2/5] Add documentation for CARGO_PRIMARY_PACKAGE --- src/doc/src/reference/environment-variables.md | 6 ++++++ 1 file changed, 6 insertions(+) diff --git a/src/doc/src/reference/environment-variables.md b/src/doc/src/reference/environment-variables.md index eec0d0aac04..4e9f36bc20e 100644 --- a/src/doc/src/reference/environment-variables.md +++ b/src/doc/src/reference/environment-variables.md @@ -265,6 +265,12 @@ let out_dir = env::var("OUT_DIR").unwrap(); Currently Cargo doesn't set the `MAKEFLAGS` variable, but it's free for build scripts invoking GNU Make to set it to the contents of `CARGO_MAKEFLAGS`. +* `CARGO_PRIMARY_PACKAGE` — This environment variable will be set if the package being + built is primary. Primary packages are the ones the user + selected on the command-line, either with `-p` flags or + the defaults based on the current directory and the default + workspace members. + This environment variable will not be set when building dependencies. * `CARGO_FEATURE_` — For each activated feature of the package being built, this environment variable will be present where `` is the name of the feature uppercased From 0f20b2e478faf4f5d45c4ce1487f0208a8b45f3d Mon Sep 17 00:00:00 2001 From: Eduardo Broto Date: Mon, 12 Oct 2020 15:26:40 +0200 Subject: [PATCH 3/5] Add test for CARGO_PRIMARY_PACKAGE --- tests/testsuite/build.rs | 82 +++++++++++++++++++++++++++++++++++++++- 1 file changed, 81 insertions(+), 1 deletion(-) diff --git a/tests/testsuite/build.rs b/tests/testsuite/build.rs index 268ddbdf36f..b58c4febcde 100644 --- a/tests/testsuite/build.rs +++ b/tests/testsuite/build.rs @@ -10,7 +10,7 @@ use cargo::{ use cargo_test_support::paths::{root, CargoPathExt}; use cargo_test_support::registry::Package; use cargo_test_support::{ - basic_bin_manifest, basic_lib_manifest, basic_manifest, is_nightly, lines_match_unordered, + basic_bin_manifest, basic_lib_manifest, basic_manifest, git, is_nightly, lines_match_unordered, main_file, paths, project, rustc_host, sleep_ms, symlink_supported, t, Execs, ProjectBuilder, }; use std::env; @@ -5179,3 +5179,83 @@ fn build_script_o0_default_even_with_release() { .with_stderr_does_not_contain("[..]build_script_build[..]opt-level[..]") .run(); } + +#[cargo_test] +fn primary_package_env_var() { + // "foo" depends on "bar" (local) which depends on "baz" (git) which depends on "qux" (registry) + // Test that CARGO_PRIMARY_PACKAGE is enabled only for "foo". + + Package::new("qux", "1.0.0") + .file("src/lib.rs", "") + .publish(); + + let baz = git::new("baz", |project| { + project + .file( + "Cargo.toml", + r#" + [package] + name = "baz" + version = "1.0.0" + + [dependencies] + qux = "1.0" + "#, + ) + .file("src/lib.rs", "extern crate qux;") + }); + + let foo = project() + .file( + "Cargo.toml", + r#" + [package] + name = "foo" + version = "1.0.0" + + [dependencies] + bar = {path = "bar"} +"#, + ) + .file("src/lib.rs", "extern crate bar;") + .file( + "bar/Cargo.toml", + &format!( + r#" + [package] + name = "bar" + version = "1.0.0" + + [dependencies] + baz = {{ git = '{}' }} + "#, + baz.url() + ), + ) + .file("bar/src/lib.rs", "extern crate baz;") + .build(); + + foo.cargo("build -vv") + .with_stderr_contains("[COMPILING] qux[..]") + .with_stderr_line_without( + &["[RUNNING]", "CARGO_CRATE_NAME=qux"], + &["CARGO_PRIMARY_PACKAGE=1"], + ) + .with_stderr_contains("[COMPILING] baz[..]") + .with_stderr_line_without( + &["[RUNNING]", "CARGO_CRATE_NAME=baz"], + &["CARGO_PRIMARY_PACKAGE=1"], + ) + .with_stderr_contains("[COMPILING] bar[..]") + .with_stderr_line_without( + &["[RUNNING]", "CARGO_CRATE_NAME=bar"], + &["CARGO_PRIMARY_PACKAGE=1"], + ) + .with_stderr_contains( + "\ +[COMPILING] foo[..] +[RUNNING] [..] CARGO_CRATE_NAME=foo [..] CARGO_PRIMARY_PACKAGE=1 [..] +[FINISHED] dev [unoptimized + debuginfo] target(s) in [..]", + ) + .run(); +} From c12aed2fe40dbcd19c82401ef226cac70615c25e Mon Sep 17 00:00:00 2001 From: Eduardo Broto Date: Mon, 12 Oct 2020 16:01:49 +0200 Subject: [PATCH 4/5] Add tests for CARGO_PRIMARY_PACKAGE (workspaces) --- tests/testsuite/workspaces.rs | 89 +++++++++++++++++++++++++++++++++++ 1 file changed, 89 insertions(+) diff --git a/tests/testsuite/workspaces.rs b/tests/testsuite/workspaces.rs index c9ca991708d..2b83fcca32b 100644 --- a/tests/testsuite/workspaces.rs +++ b/tests/testsuite/workspaces.rs @@ -2319,3 +2319,92 @@ Caused by: ) .run(); } + +#[cargo_test] +fn simple_primary_package_env_var() { + let p = project() + .file( + "Cargo.toml", + r#" + [project] + name = "foo" + version = "0.1.0" + authors = [] + + [workspace] + members = ["bar"] + "#, + ) + .file("src/main.rs", "fn main() {}") + .file( + "bar/Cargo.toml", + r#" + [project] + name = "bar" + version = "0.1.0" + authors = [] + workspace = ".." + "#, + ) + .file("bar/src/main.rs", "fn main() {}"); + let p = p.build(); + + p.cargo("build -vv") + .with_stderr_contains( + "[RUNNING] [..] CARGO_CRATE_NAME=foo [..] CARGO_PRIMARY_PACKAGE=1 [..]", + ) + .run(); + + // Again, this time selecting a specific crate + p.cargo("clean").run(); + p.cargo("build -vv -p bar") + .with_stderr_contains( + "[RUNNING] [..] CARGO_CRATE_NAME=bar [..] CARGO_PRIMARY_PACKAGE=1 [..]", + ) + .run(); + + // Again, this time selecting all crates + p.cargo("clean").run(); + p.cargo("build -vv --all") + .with_stderr_contains( + "[RUNNING] [..] CARGO_CRATE_NAME=foo [..] CARGO_PRIMARY_PACKAGE=1 [..]", + ) + .with_stderr_contains( + "[RUNNING] [..] CARGO_CRATE_NAME=bar [..] CARGO_PRIMARY_PACKAGE=1 [..]", + ) + .run(); +} + +#[cargo_test] +fn virtual_primary_package_env_var() { + let p = project() + .file( + "Cargo.toml", + r#" + [workspace] + members = ["foo", "bar"] + "#, + ) + .file("foo/Cargo.toml", &basic_manifest("foo", "0.1.0")) + .file("foo/src/main.rs", "fn main() {}") + .file("bar/Cargo.toml", &basic_manifest("bar", "0.1.0")) + .file("bar/src/main.rs", "fn main() {}"); + let p = p.build(); + + p.cargo("build -vv") + .with_stderr_contains( + "[RUNNING] [..] CARGO_CRATE_NAME=foo [..] CARGO_PRIMARY_PACKAGE=1 [..]", + ) + .with_stderr_contains( + "[RUNNING] [..] CARGO_CRATE_NAME=bar [..] CARGO_PRIMARY_PACKAGE=1 [..]", + ) + .run(); + + // Again, this time selecting a specific crate + p.cargo("clean").run(); + p.cargo("build -vv -p foo") + .with_stderr_contains( + "[RUNNING] [..] CARGO_CRATE_NAME=foo [..] CARGO_PRIMARY_PACKAGE=1 [..]", + ) + .run(); +} From fb02ade261c753eed940890934697f212d658085 Mon Sep 17 00:00:00 2001 From: Eduardo Broto Date: Thu, 15 Oct 2020 00:44:04 +0200 Subject: [PATCH 5/5] Rework tests to avoid using -vv Environment variables are represented differently in differents OSes in the output. Add tests checking if the variable is set instead. --- tests/testsuite/build.rs | 99 +++++++++++++++-------------------- tests/testsuite/workspaces.rs | 58 ++++++++------------ 2 files changed, 65 insertions(+), 92 deletions(-) diff --git a/tests/testsuite/build.rs b/tests/testsuite/build.rs index b58c4febcde..a350286d55c 100644 --- a/tests/testsuite/build.rs +++ b/tests/testsuite/build.rs @@ -5182,80 +5182,65 @@ fn build_script_o0_default_even_with_release() { #[cargo_test] fn primary_package_env_var() { - // "foo" depends on "bar" (local) which depends on "baz" (git) which depends on "qux" (registry) - // Test that CARGO_PRIMARY_PACKAGE is enabled only for "foo". + // Test that CARGO_PRIMARY_PACKAGE is enabled only for "foo" and not for any dependency. - Package::new("qux", "1.0.0") - .file("src/lib.rs", "") + let is_primary_package = r#" + pub fn is_primary_package() -> bool {{ + option_env!("CARGO_PRIMARY_PACKAGE").is_some() + }} + "#; + + Package::new("qux", "0.1.0") + .file("src/lib.rs", is_primary_package) .publish(); let baz = git::new("baz", |project| { project - .file( - "Cargo.toml", - r#" - [package] - name = "baz" - version = "1.0.0" - - [dependencies] - qux = "1.0" - "#, - ) - .file("src/lib.rs", "extern crate qux;") + .file("Cargo.toml", &basic_manifest("baz", "0.1.0")) + .file("src/lib.rs", is_primary_package) }); let foo = project() .file( "Cargo.toml", - r#" - [package] - name = "foo" - version = "1.0.0" + &format!( + r#" + [package] + name = "foo" + version = "0.1.0" - [dependencies] - bar = {path = "bar"} -"#, + [dependencies] + bar = {{ path = "bar" }} + baz = {{ git = '{}' }} + qux = "0.1" + "#, + baz.url() + ), ) - .file("src/lib.rs", "extern crate bar;") .file( - "bar/Cargo.toml", + "src/lib.rs", &format!( r#" - [package] - name = "bar" - version = "1.0.0" - - [dependencies] - baz = {{ git = '{}' }} - "#, - baz.url() + extern crate bar; + extern crate baz; + extern crate qux; + + {} + + #[test] + fn verify_primary_package() {{ + assert!(!bar::is_primary_package()); + assert!(!baz::is_primary_package()); + assert!(!qux::is_primary_package()); + assert!(is_primary_package()); + }} + "#, + is_primary_package ), ) - .file("bar/src/lib.rs", "extern crate baz;") + .file("bar/Cargo.toml", &basic_manifest("bar", "0.1.0")) + .file("bar/src/lib.rs", is_primary_package) .build(); - foo.cargo("build -vv") - .with_stderr_contains("[COMPILING] qux[..]") - .with_stderr_line_without( - &["[RUNNING]", "CARGO_CRATE_NAME=qux"], - &["CARGO_PRIMARY_PACKAGE=1"], - ) - .with_stderr_contains("[COMPILING] baz[..]") - .with_stderr_line_without( - &["[RUNNING]", "CARGO_CRATE_NAME=baz"], - &["CARGO_PRIMARY_PACKAGE=1"], - ) - .with_stderr_contains("[COMPILING] bar[..]") - .with_stderr_line_without( - &["[RUNNING]", "CARGO_CRATE_NAME=bar"], - &["CARGO_PRIMARY_PACKAGE=1"], - ) - .with_stderr_contains( - "\ -[COMPILING] foo[..] -[RUNNING] [..] CARGO_CRATE_NAME=foo [..] CARGO_PRIMARY_PACKAGE=1 [..] -[FINISHED] dev [unoptimized + debuginfo] target(s) in [..]", - ) - .run(); + foo.cargo("test").run(); } diff --git a/tests/testsuite/workspaces.rs b/tests/testsuite/workspaces.rs index 2b83fcca32b..a20ef11ba70 100644 --- a/tests/testsuite/workspaces.rs +++ b/tests/testsuite/workspaces.rs @@ -2322,6 +2322,13 @@ Caused by: #[cargo_test] fn simple_primary_package_env_var() { + let is_primary_package = r#" + #[test] + fn verify_primary_package() {{ + assert!(option_env!("CARGO_PRIMARY_PACKAGE").is_some()); + }} + "#; + let p = project() .file( "Cargo.toml", @@ -2335,7 +2342,7 @@ fn simple_primary_package_env_var() { members = ["bar"] "#, ) - .file("src/main.rs", "fn main() {}") + .file("src/lib.rs", is_primary_package) .file( "bar/Cargo.toml", r#" @@ -2346,37 +2353,29 @@ fn simple_primary_package_env_var() { workspace = ".." "#, ) - .file("bar/src/main.rs", "fn main() {}"); + .file("bar/src/lib.rs", is_primary_package); let p = p.build(); - p.cargo("build -vv") - .with_stderr_contains( - "[RUNNING] [..] CARGO_CRATE_NAME=foo [..] CARGO_PRIMARY_PACKAGE=1 [..]", - ) - .run(); + p.cargo("test").run(); // Again, this time selecting a specific crate p.cargo("clean").run(); - p.cargo("build -vv -p bar") - .with_stderr_contains( - "[RUNNING] [..] CARGO_CRATE_NAME=bar [..] CARGO_PRIMARY_PACKAGE=1 [..]", - ) - .run(); + p.cargo("test -p bar").run(); // Again, this time selecting all crates p.cargo("clean").run(); - p.cargo("build -vv --all") - .with_stderr_contains( - "[RUNNING] [..] CARGO_CRATE_NAME=foo [..] CARGO_PRIMARY_PACKAGE=1 [..]", - ) - .with_stderr_contains( - "[RUNNING] [..] CARGO_CRATE_NAME=bar [..] CARGO_PRIMARY_PACKAGE=1 [..]", - ) - .run(); + p.cargo("test --all").run(); } #[cargo_test] fn virtual_primary_package_env_var() { + let is_primary_package = r#" + #[test] + fn verify_primary_package() {{ + assert!(option_env!("CARGO_PRIMARY_PACKAGE").is_some()); + }} + "#; + let p = project() .file( "Cargo.toml", @@ -2386,25 +2385,14 @@ fn virtual_primary_package_env_var() { "#, ) .file("foo/Cargo.toml", &basic_manifest("foo", "0.1.0")) - .file("foo/src/main.rs", "fn main() {}") + .file("foo/src/lib.rs", is_primary_package) .file("bar/Cargo.toml", &basic_manifest("bar", "0.1.0")) - .file("bar/src/main.rs", "fn main() {}"); + .file("bar/src/lib.rs", is_primary_package); let p = p.build(); - p.cargo("build -vv") - .with_stderr_contains( - "[RUNNING] [..] CARGO_CRATE_NAME=foo [..] CARGO_PRIMARY_PACKAGE=1 [..]", - ) - .with_stderr_contains( - "[RUNNING] [..] CARGO_CRATE_NAME=bar [..] CARGO_PRIMARY_PACKAGE=1 [..]", - ) - .run(); + p.cargo("test").run(); // Again, this time selecting a specific crate p.cargo("clean").run(); - p.cargo("build -vv -p foo") - .with_stderr_contains( - "[RUNNING] [..] CARGO_CRATE_NAME=foo [..] CARGO_PRIMARY_PACKAGE=1 [..]", - ) - .run(); + p.cargo("test -p foo").run(); }