Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Merged
merged 4 commits into from
Oct 30, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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() {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Should we add some build-std tests for relying on host target?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This might not be easy to set up though. Maybe we can only do this on certain platforms like x86_64 linux.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@ehuss I meant this but now it seems to be a separate issue so you're right.

// Fixing rust-lang/rust#117839.
// on macOS it never gets remapped.
// Might be a separate issue, so only run on Linux.
#[cargo_test(build_std_real)]
#[cfg(target_os = "linux")]
fn remap_path_scope() {

// 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 @@ -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
Expand Down Expand Up @@ -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.

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