RalfJung/cargo-careful

`no-std` support

mkroening opened this issue · 2 comments

Building for no-std targets does not work, since cargo-careful always builds with std:

   Compiling test v0.0.0 (/Users/mkroening/.rustup/toolchains/nightly-aarch64-apple-darwin/lib/rustlib/src/rust/library/test)
error[E0658]: use of unstable library feature 'restricted_std'
  |
  = help: add `#![feature(restricted_std)]` to the crate attributes to enable

error[E0658]: use of unstable library feature 'restricted_std'
   --> /Users/mkroening/.rustup/toolchains/nightly-aarch64-apple-darwin/lib/rustlib/src/rust/library/test/src/lib.rs:125:25
    |
125 |                         std::mem::forget(std::io::stderr().lock());
    |                         ^^^^^^^^^^^^^^^^
    |
    = help: add `#![feature(restricted_std)]` to the crate attributes to enable

error[E0658]: use of unstable library feature 'restricted_std'
   --> /Users/mkroening/.rustup/toolchains/nightly-aarch64-apple-darwin/lib/rustlib/src/rust/library/test/src/lib.rs:125:42
    |
125 |                         std::mem::forget(std::io::stderr().lock());
    |                                          ^^^^^^^^^^^^^^^
    |
    = help: add `#![feature(restricted_std)]` to the crate attributes to enable

error[E0658]: use of unstable library feature 'restricted_std'
   --> /Users/mkroening/.rustup/toolchains/nightly-aarch64-apple-darwin/lib/rustlib/src/rust/library/test/src/lib.rs:126:60
    |
126 |                         let mut stdout = ManuallyDrop::new(std::io::stdout().lock());
    |                                                            ^^^^^^^^^^^^^^^
    |
    = help: add `#![feature(restricted_std)]` to the crate attributes to enable

error[E0658]: use of unstable library feature 'restricted_std'
   --> /Users/mkroening/.rustup/toolchains/nightly-aarch64-apple-darwin/lib/rustlib/src/rust/library/test/src/lib.rs:452:17
    |
452 |                 std::mem::forget(rx);
    |                 ^^^^^^^^^^^^^^^^
    |
    = help: add `#![feature(restricted_std)]` to the crate attributes to enable

error[E0658]: use of unstable library feature 'restricted_std'
   --> /Users/mkroening/.rustup/toolchains/nightly-aarch64-apple-darwin/lib/rustlib/src/rust/library/test/src/lib.rs:740:13
    |
740 |         let std::process::Output { stdout, stderr, status } = output;
    |             ^^^^^^^^^^^^^^^^^^^^
    |
    = help: add `#![feature(restricted_std)]` to the crate attributes to enable

error[E0658]: use of unstable library feature 'restricted_std'
  --> /Users/mkroening/.rustup/toolchains/nightly-aarch64-apple-darwin/lib/rustlib/src/rust/library/test/src/bench.rs:23:5
   |
23 |     std::hint::black_box(dummy)
   |     ^^^^^^^^^^^^^^^^^^^^
   |
   = help: add `#![feature(restricted_std)]` to the crate attributes to enable

error[E0658]: use of unstable library feature 'restricted_std'
   --> /Users/mkroening/.rustup/toolchains/nightly-aarch64-apple-darwin/lib/rustlib/src/rust/library/test/src/formatters/json.rs:224:21
    |
224 | impl<S: AsRef<str>> std::fmt::Display for EscapedString<S> {
    |                     ^^^^^^^^^^^^^^^^^
    |
    = help: add `#![feature(restricted_std)]` to the crate attributes to enable

error[E0658]: use of unstable library feature 'restricted_std'
   --> /Users/mkroening/.rustup/toolchains/nightly-aarch64-apple-darwin/lib/rustlib/src/rust/library/test/src/formatters/json.rs:225:27
    |
225 |     fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> ::std::fmt::Result {
    |                           ^^^^^^^^^^^^^^^^^^^^^^^
    |
    = help: add `#![feature(restricted_std)]` to the crate attributes to enable

error[E0658]: use of unstable library feature 'restricted_std'
   --> /Users/mkroening/.rustup/toolchains/nightly-aarch64-apple-darwin/lib/rustlib/src/rust/library/test/src/formatters/json.rs:225:55
    |
225 |     fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> ::std::fmt::Result {
    |                                                       ^^^^^^^^^^^^^^^^^^
    |
    = help: add `#![feature(restricted_std)]` to the crate attributes to enable

error[E0658]: use of unstable library feature 'restricted_std'
   --> /Users/mkroening/.rustup/toolchains/nightly-aarch64-apple-darwin/lib/rustlib/src/rust/library/test/src/formatters/junit.rs:100:49
    |
100 |         for (desc, result, duration, stdout) in std::mem::take(&mut self.results) {
    |                                                 ^^^^^^^^^^^^^^
    |
    = help: add `#![feature(restricted_std)]` to the crate attributes to enable

For more information about this error, try `rustc --explain E0658`.
warning: `test` (lib) generated 1 warning (1 duplicate)
error: could not compile `test` (lib) due to 11 previous errors; 1 warning emitted
warning: build failed, waiting for other jobs to finish...
thread 'main' panicked at 'failed to build sysroot; run `cargo careful setup` to see what went wrong: sysroot build failed', /Users/mkroening/.cargo/registry/src/index.crates.io-6f17d22bba15001f/cargo-careful-0.3.4/src/main.rs:215:10
note: run with `RUST_BACKTRACE=1` environment variable to display a backtrace

Building without std is easy, just replace SysrootConfig::WithStd with SysrootConfig::NoStd. The only question, though, is how to determine for which targets to build without std?

There are three approaches I can imagine:

  1. Search the rust source for #![no_std].

    This would be very reliable and work seamlessly for users, but add complexity to cargo-careful.

  2. Add a -Zcareful-no-std flag.

    This would be the easiest one for cargo-careful, but would require users to figure out that the (unhelpful) error message is due to building std for a no-std target.

  3. Mirror rustc's bootstrap behavior.

    See rust/src/bootstrap/config.rs. This would work seamlessly, but would require updating if new rules got into bootstrap.

I think approach 3 would be best. I'll prepare a PR.

What does -Zbuild-std do?

What does -Zbuild-std do?

"Using -Z build-std will implicitly compile the stable crates core, std, alloc, and proc_macro." (https://doc.rust-lang.org/cargo/reference/unstable.html#build-std)

If we would want to omit std from build-std, we'd have to use -Zbuild-std=core,alloc.