From 879efa69ccc50ede0b3eeaf702507072ec0f7871 Mon Sep 17 00:00:00 2001 From: Harry Moulton Date: Tue, 16 Jul 2024 13:31:12 +0100 Subject: [PATCH 1/4] Remove requirement for --target when invoking Cargo with -Zbuild-std There is no longer a need for the --target to be specified in every case when using -Zbuild-std. Cargo will default to the Host CompileKind when no --target is specified. --- src/cargo/core/compiler/build_config.rs | 5 ----- 1 file changed, 5 deletions(-) diff --git a/src/cargo/core/compiler/build_config.rs b/src/cargo/core/compiler/build_config.rs index 4c804f27b68..ad881bb318d 100644 --- a/src/cargo/core/compiler/build_config.rs +++ b/src/cargo/core/compiler/build_config.rs @@ -99,11 +99,6 @@ impl BuildConfig { }, }; - if gctx.cli_unstable().build_std.is_some() && requested_kinds[0].is_host() { - // TODO: This should eventually be fixed. - anyhow::bail!("-Zbuild-std requires --target"); - } - Ok(BuildConfig { requested_kinds, jobs, From 3597f4cd9b9df721e55bd0195294fcb95a18ed86 Mon Sep 17 00:00:00 2001 From: Harry Moulton Date: Thu, 1 Aug 2024 14:51:38 +0100 Subject: [PATCH 2/4] test: build-std proc_macro test case without --target requirement Add a new test case for building a crate with -Zbuild-std, without the requirement for the --target flag, and that uses a proc_macro. --- tests/build-std/main.rs | 60 +++++++++++++++++++++++++++++++++++++++++ 1 file changed, 60 insertions(+) diff --git a/tests/build-std/main.rs b/tests/build-std/main.rs index aec951a9578..01e3fae5f82 100644 --- a/tests/build-std/main.rs +++ b/tests/build-std/main.rs @@ -154,6 +154,66 @@ fn basic() { assert_eq!(p.glob(deps_dir.join("*.dylib")).count(), 0); } +#[cargo_test(build_std_real)] +fn host_proc_macro() { + let p = project() + .file( + "Cargo.toml", + r#" + [package] + name = "foo" + version = "0.1.0" + edition = "2021" + + [dependencies] + macro_test = { path = "macro_test" } + "#, + ) + .file( + "src/main.rs", + r#" + extern crate macro_test; + use macro_test::make_answer; + + make_answer!(); + + fn main() { + println!("Hello, World: {}", answer()); + } + "#, + ) + .file( + "macro_test/Cargo.toml", + r#" + [package] + name = "macro_test" + version = "0.1.0" + edition = "2021" + + [lib] + proc-macro = true + "#, + ) + .file( + "macro_test/src/lib.rs", + r#" + extern crate proc_macro; + use proc_macro::TokenStream; + + #[proc_macro] + pub fn make_answer(_item: TokenStream) -> TokenStream { + "fn answer() -> u32 { 42 }".parse().unwrap() + } + "#, + ) + .build(); + + p.cargo("build") + .build_std_arg("std") + .build_std_arg("proc_macro") + .run(); +} + #[cargo_test(build_std_real)] fn cross_custom() { let p = project() From b6e1942ff2c0b75ca9a848a46be29a9195f24fcb Mon Sep 17 00:00:00 2001 From: Harry Moulton Date: Tue, 3 Sep 2024 15:46:22 +0100 Subject: [PATCH 3/4] test: mock-std test case for shared dependencies without --target req Add a test case which ensures that -Zbuild-std without --target correctly handles building a crate that has a shared dependency between it's own build script, and std. --- tests/testsuite/mock-std/dep_test/Cargo.toml | 4 ++ tests/testsuite/mock-std/dep_test/src/lib.rs | 1 + .../testsuite/mock-std/library/std/Cargo.toml | 1 + tests/testsuite/standard_lib.rs | 67 +++++++++++++++++++ 4 files changed, 73 insertions(+) create mode 100644 tests/testsuite/mock-std/dep_test/Cargo.toml create mode 100644 tests/testsuite/mock-std/dep_test/src/lib.rs diff --git a/tests/testsuite/mock-std/dep_test/Cargo.toml b/tests/testsuite/mock-std/dep_test/Cargo.toml new file mode 100644 index 00000000000..9a9f067fe21 --- /dev/null +++ b/tests/testsuite/mock-std/dep_test/Cargo.toml @@ -0,0 +1,4 @@ +[package] +name = "dep_test" +version = "0.1.0" +edition = "2021" \ No newline at end of file diff --git a/tests/testsuite/mock-std/dep_test/src/lib.rs b/tests/testsuite/mock-std/dep_test/src/lib.rs new file mode 100644 index 00000000000..8b137891791 --- /dev/null +++ b/tests/testsuite/mock-std/dep_test/src/lib.rs @@ -0,0 +1 @@ + diff --git a/tests/testsuite/mock-std/library/std/Cargo.toml b/tests/testsuite/mock-std/library/std/Cargo.toml index d2cfdea3986..2a31b514709 100644 --- a/tests/testsuite/mock-std/library/std/Cargo.toml +++ b/tests/testsuite/mock-std/library/std/Cargo.toml @@ -6,6 +6,7 @@ edition = "2018" [dependencies] registry-dep-using-alloc = { version = "*", features = ['mockbuild'] } +dep_test = { path = "../../dep_test" } [features] feature1 = [] diff --git a/tests/testsuite/standard_lib.rs b/tests/testsuite/standard_lib.rs index 08d371e2fdb..c4108cdfad3 100644 --- a/tests/testsuite/standard_lib.rs +++ b/tests/testsuite/standard_lib.rs @@ -246,6 +246,73 @@ fn basic() { p.cargo("test").build_std(&setup).target_host().run(); } +#[cargo_test(build_std_mock)] +fn shared_std_dependency_rebuild() { + let manifest_dir = std::env::var("CARGO_MANIFEST_DIR").unwrap(); + let setup = setup(); + let p = project() + .file( + "Cargo.toml", + format!( + " + [package] + name = \"foo\" + version = \"0.1.0\" + edition = \"2021\" + + [build-dependencies] + dep_test = {{ path = \"{}/tests/testsuite/mock-std/dep_test\" }} + ", + manifest_dir + ) + .as_str(), + ) + .file( + "src/main.rs", + r#" + fn main() { + println!("Hello, World!"); + } + "#, + ) + .file( + "build.rs", + r#" + fn main() { + println!("cargo::rerun-if-changed=build.rs"); + } + "#, + ) + .build(); + + p.cargo("build -v") + .build_std(&setup) + .target_host() + .with_stderr_data(str![[r#" +... +[RUNNING] `[..] rustc --crate-name dep_test [..]` +... +[RUNNING] `[..] rustc --crate-name dep_test [..]` +... +"#]]) + .run(); + + // TODO: Because of the way in which std is resolved, it's mandatory that this is left commented + // out as it will fail. This case should result in `dep_test` only being built once, however + // it's still being built twice. This is a bug. + // + // p.cargo("build -v") + // .build_std(&setup) + // .with_stderr_does_not_contain(str![[r#" + //... + //[RUNNING] `[..] rustc --crate-name dep_test [..]` + //... + //[RUNNING] `[..] rustc --crate-name dep_test [..]` + //... + //"#]]) + // .run(); +} + #[cargo_test(build_std_mock)] fn simple_lib_std() { let setup = setup(); From 5a6667222c0f3fe4aa8d2b6666ec3767a38a2bdb Mon Sep 17 00:00:00 2001 From: Harry Moulton Date: Wed, 4 Sep 2024 10:02:23 +0100 Subject: [PATCH 4/4] docs: update documentation for removal of build-std --target requirement --- src/doc/src/reference/unstable.md | 5 ----- 1 file changed, 5 deletions(-) diff --git a/src/doc/src/reference/unstable.md b/src/doc/src/reference/unstable.md index 84c08860eca..0ef0376cd4d 100644 --- a/src/doc/src/reference/unstable.md +++ b/src/doc/src/reference/unstable.md @@ -434,10 +434,6 @@ component: $ rustup component add rust-src --toolchain nightly ``` -It is also required today that the `-Z build-std` flag is combined with the -`--target` flag. Note that you're not forced to do a cross compilation, you're -just forced to pass `--target` in one form or another. - Usage looks like: ```console @@ -471,7 +467,6 @@ The value here is a comma-separated list of standard library crates to build. As a summary, a list of requirements today to use `-Z build-std` are: * You must install libstd's source code through `rustup component add rust-src` -* You must pass `--target` * You must use both a nightly Cargo and a nightly rustc * The `-Z build-std` flag must be passed to all `cargo` invocations.