Skip to content

Commit 9abcaef

Browse files
committed
Auto merge of #14317 - harmou01:dev/harmou01/remove-target-flag-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`.
2 parents 06e0ef4 + 5a66672 commit 9abcaef

File tree

7 files changed

+133
-10
lines changed

7 files changed

+133
-10
lines changed

src/cargo/core/compiler/build_config.rs

-5
Original file line numberDiff line numberDiff line change
@@ -99,11 +99,6 @@ impl BuildConfig {
9999
},
100100
};
101101

102-
if gctx.cli_unstable().build_std.is_some() && requested_kinds[0].is_host() {
103-
// TODO: This should eventually be fixed.
104-
anyhow::bail!("-Zbuild-std requires --target");
105-
}
106-
107102
Ok(BuildConfig {
108103
requested_kinds,
109104
jobs,

src/doc/src/reference/unstable.md

-5
Original file line numberDiff line numberDiff line change
@@ -435,10 +435,6 @@ component:
435435
$ rustup component add rust-src --toolchain nightly
436436
```
437437

438-
It is also required today that the `-Z build-std` flag is combined with the
439-
`--target` flag. Note that you're not forced to do a cross compilation, you're
440-
just forced to pass `--target` in one form or another.
441-
442438
Usage looks like:
443439

444440
```console
@@ -472,7 +468,6 @@ The value here is a comma-separated list of standard library crates to build.
472468
As a summary, a list of requirements today to use `-Z build-std` are:
473469

474470
* You must install libstd's source code through `rustup component add rust-src`
475-
* You must pass `--target`
476471
* You must use both a nightly Cargo and a nightly rustc
477472
* The `-Z build-std` flag must be passed to all `cargo` invocations.
478473

tests/build-std/main.rs

+60
Original file line numberDiff line numberDiff line change
@@ -154,6 +154,66 @@ fn basic() {
154154
assert_eq!(p.glob(deps_dir.join("*.dylib")).count(), 0);
155155
}
156156

157+
#[cargo_test(build_std_real)]
158+
fn host_proc_macro() {
159+
let p = project()
160+
.file(
161+
"Cargo.toml",
162+
r#"
163+
[package]
164+
name = "foo"
165+
version = "0.1.0"
166+
edition = "2021"
167+
168+
[dependencies]
169+
macro_test = { path = "macro_test" }
170+
"#,
171+
)
172+
.file(
173+
"src/main.rs",
174+
r#"
175+
extern crate macro_test;
176+
use macro_test::make_answer;
177+
178+
make_answer!();
179+
180+
fn main() {
181+
println!("Hello, World: {}", answer());
182+
}
183+
"#,
184+
)
185+
.file(
186+
"macro_test/Cargo.toml",
187+
r#"
188+
[package]
189+
name = "macro_test"
190+
version = "0.1.0"
191+
edition = "2021"
192+
193+
[lib]
194+
proc-macro = true
195+
"#,
196+
)
197+
.file(
198+
"macro_test/src/lib.rs",
199+
r#"
200+
extern crate proc_macro;
201+
use proc_macro::TokenStream;
202+
203+
#[proc_macro]
204+
pub fn make_answer(_item: TokenStream) -> TokenStream {
205+
"fn answer() -> u32 { 42 }".parse().unwrap()
206+
}
207+
"#,
208+
)
209+
.build();
210+
211+
p.cargo("build")
212+
.build_std_arg("std")
213+
.build_std_arg("proc_macro")
214+
.run();
215+
}
216+
157217
#[cargo_test(build_std_real)]
158218
fn cross_custom() {
159219
let p = project()
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
[package]
2+
name = "dep_test"
3+
version = "0.1.0"
4+
edition = "2021"
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+

tests/testsuite/mock-std/library/std/Cargo.toml

+1
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ edition = "2018"
66

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

1011
[features]
1112
feature1 = []

tests/testsuite/standard_lib.rs

+67
Original file line numberDiff line numberDiff line change
@@ -246,6 +246,73 @@ fn basic() {
246246
p.cargo("test").build_std(&setup).target_host().run();
247247
}
248248

249+
#[cargo_test(build_std_mock)]
250+
fn shared_std_dependency_rebuild() {
251+
let manifest_dir = std::env::var("CARGO_MANIFEST_DIR").unwrap();
252+
let setup = setup();
253+
let p = project()
254+
.file(
255+
"Cargo.toml",
256+
format!(
257+
"
258+
[package]
259+
name = \"foo\"
260+
version = \"0.1.0\"
261+
edition = \"2021\"
262+
263+
[build-dependencies]
264+
dep_test = {{ path = \"{}/tests/testsuite/mock-std/dep_test\" }}
265+
",
266+
manifest_dir
267+
)
268+
.as_str(),
269+
)
270+
.file(
271+
"src/main.rs",
272+
r#"
273+
fn main() {
274+
println!("Hello, World!");
275+
}
276+
"#,
277+
)
278+
.file(
279+
"build.rs",
280+
r#"
281+
fn main() {
282+
println!("cargo::rerun-if-changed=build.rs");
283+
}
284+
"#,
285+
)
286+
.build();
287+
288+
p.cargo("build -v")
289+
.build_std(&setup)
290+
.target_host()
291+
.with_stderr_data(str![[r#"
292+
...
293+
[RUNNING] `[..] rustc --crate-name dep_test [..]`
294+
...
295+
[RUNNING] `[..] rustc --crate-name dep_test [..]`
296+
...
297+
"#]])
298+
.run();
299+
300+
// TODO: Because of the way in which std is resolved, it's mandatory that this is left commented
301+
// out as it will fail. This case should result in `dep_test` only being built once, however
302+
// it's still being built twice. This is a bug.
303+
//
304+
// p.cargo("build -v")
305+
// .build_std(&setup)
306+
// .with_stderr_does_not_contain(str![[r#"
307+
//...
308+
//[RUNNING] `[..] rustc --crate-name dep_test [..]`
309+
//...
310+
//[RUNNING] `[..] rustc --crate-name dep_test [..]`
311+
//...
312+
//"#]])
313+
// .run();
314+
}
315+
249316
#[cargo_test(build_std_mock)]
250317
fn simple_lib_std() {
251318
let setup = setup();

0 commit comments

Comments
 (0)