Skip to content

Commit

Permalink
Auto merge of rust-lang#14317 - harmou01:dev/harmou01/remove-target-f…
Browse files Browse the repository at this point in the history
…lag-req, r=ehuss

Remove requirement for --target when invoking Cargo with -Zbuild-std

This PR addresses [this issue](rust-lang/wg-cargo-std-aware#25) re: build-std stabilization. We believe the requirement for --target to be specified when invoking cargo with -Zbuild-std, from our testing, is no longer needed. Now, with this change, by default Cargo will use the Host CompileKind, rather than a manually specified CompileTarget. We propose removing this restriction in order to test this more widely. Our own testing is detailed below.

This change has been tested in the following manner:
* Building crates depending on proc_macro, std, and build scripts (which themselves depend on proc_macro)
* Various RUSTFLAGS, such as `-Zsanitizer=cfi`, `-Cembed-bitcode=yes`, `-Cforce-frame-pointers`, `-Cforce-unwind-tables=yes`, `-Csoft-float=yes`, `-Zbranch-protection=pac-ret`.
  • Loading branch information
bors committed Oct 30, 2024
2 parents 06e0ef4 + 5a66672 commit 9abcaef
Show file tree
Hide file tree
Showing 7 changed files with 133 additions and 10 deletions.
5 changes: 0 additions & 5 deletions src/cargo/core/compiler/build_config.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down
5 changes: 0 additions & 5 deletions src/doc/src/reference/unstable.md
Original file line number Diff line number Diff line change
Expand Up @@ -435,10 +435,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
Expand Down Expand Up @@ -472,7 +468,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.

Expand Down
60 changes: 60 additions & 0 deletions tests/build-std/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand Down
4 changes: 4 additions & 0 deletions tests/testsuite/mock-std/dep_test/Cargo.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
[package]
name = "dep_test"
version = "0.1.0"
edition = "2021"
1 change: 1 addition & 0 deletions tests/testsuite/mock-std/dep_test/src/lib.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@

1 change: 1 addition & 0 deletions tests/testsuite/mock-std/library/std/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ edition = "2018"

[dependencies]
registry-dep-using-alloc = { version = "*", features = ['mockbuild'] }
dep_test = { path = "../../dep_test" }

[features]
feature1 = []
67 changes: 67 additions & 0 deletions tests/testsuite/standard_lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand Down

0 comments on commit 9abcaef

Please sign in to comment.