Skip to content

Commit 9719228

Browse files
committed
standard_library_support
1 parent c48e43d commit 9719228

File tree

1 file changed

+160
-26
lines changed

1 file changed

+160
-26
lines changed

text/0000-build-std/4-build-std-always.md

Lines changed: 160 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -166,40 +166,59 @@ target in the project.
166166

167167
- [*Allow reusing sysroot artifacts if available*][future-reuse-sysroot]
168168

169-
## Default standard library crate for targets
170-
[default-std-crate-for-target]: #default-standard-library-crate-for-targets
171-
172-
A new `default_build_std_crate` field is added to the target specification
173-
([?][rationale-target-spec-purpose]), replacing the existing `metadata.std`
174-
field.
175-
176-
This field determines whether the corresponding crate is intended to be able to
177-
be built for that target. It will be set to one of three values, as appropriate
178-
for the target: "core", "core and alloc" or "core, alloc and std".
179-
180-
For example, `default_build_std_crate` will be set to "core, alloc and std" on
181-
"aarch64-unknown-linux-gnu", as all of the standard library crates are supported
182-
on this target, and only "core" on "aarch64-unknown-none", as this is the only
183-
standard library crate that is supported on this target.
169+
## Standard library crate stability
170+
[standard-library-crate-stability]: #standard-library-crate-stability
171+
172+
An optional `standard_library_support` field
173+
([?][rationale-why-standard-library-support]) is added to the target
174+
specification ([?][rationale-target-spec-purpose]), replacing the existing
175+
`metadata.std` field. `standard_library_support` has two fields:
176+
177+
- `supported`, which can be set to either "core", "core and alloc", or
178+
"core, alloc and std"
179+
- `default`, which can be set to either "core", "core and alloc", or
180+
"core, alloc and std"
181+
- `default` cannot be set to a value which is "less than" that of `supported`
182+
(i.e. "core and alloc" when `supported` was only set to "core")
183+
184+
The `supported` field determines which standard library crates Cargo will permit
185+
to be built for this target on a stable toolchain. On a nightly toolchain, Cargo
186+
will build whichever standard library crates are requested by the user.
187+
188+
The `default` field determines which crate will be built by Cargo if
189+
`build-std = "always"` and `build-std-crates` is not set. Users can specify
190+
`build-std-crates` to build more crates than included in the `default`, as long
191+
as those crates are included in `supported`.
192+
193+
The correct value for `standard_library_support` is independent of the tier of
194+
the target and depends on the set of crates that are intended to work for a
195+
given target, according to its maintainers.
196+
197+
If `standard_library_support` is unset for a target, then Cargo will not permit
198+
any standard library crates to be built for the target on a stable toolchain. It
199+
will be required to use a nightly toolchain to use build-std with that target.
184200

185201
Cargo's `build-std-crates` field will default to the value of the
186-
`default_build_std_crate` field (`std` for "core, alloc and std", `alloc` for
187-
"core and alloc", and `core` for "core"). This does not prevent users from
188-
building more crates than the default, it is only intended to be a sensible
189-
default for the target that is probably what the user expects.
202+
`standard_library_support.default` field (`std` for "core, alloc and std",
203+
`alloc` for "core and alloc", and `core` for "core"). This does not prevent
204+
users from building more crates than the default, it is only intended to be a
205+
sensible default for the target that is probably what the user expects.
190206

191-
The `target-default-build-std-crate` option will be supported by rustc's
207+
The `target-standard-library-support` option will be supported by rustc's
192208
`--print` flag and will be used by Cargo to query this value for a given target:
193209

194210
```shell-session
195-
$ rustc --print target-default-build-std-crate --target aarch64-unknown-linux-gnu
196-
std
197-
$ rustc --print target-default-build-std-crate --target aarch64-unknown-none
198-
core
211+
$ rustc --print target-standard-library-support --target armv7a-none-eabi
212+
default: core
213+
supported: core, alloc
214+
$ rustc --print target-standard-library-support --target aarch64-unknown-linux-gnu
215+
default: std
216+
supported: core, alloc, std
199217
```
200218

201219
*See the following sections for rationale/alternatives:*
202220

221+
- [*Why introduce `standard_library_support`?*][rationale-why-standard-library-support]
203222
- [*Should target specifications own knowledge of which standard library crates are supported?*][rationale-target-spec-purpose]
204223

205224
## Interactions with `#![no_std]`
@@ -816,7 +835,7 @@ script, respectively.
816835

817836
[*Proposal*][proposal]
818837

819-
### Should target specifications own knowledge of which standard library crates are supported?
838+
## Should target specifications own knowledge of which standard library crates are supported?
820839
[rationale-target-spec-purpose]: #should-target-specifications-own-knowledge-of-which-standard-library-crates-are-supported
821840

822841
It is much simpler to record this information in a target's specification than
@@ -832,7 +851,122 @@ there is no reason why the target specification could not be primarily
832851
maintained by the compiler team but in close coordination with library and other
833852
relevant teams.
834853

835-
[*Default standard library crate for targets*][default-std-crate-for-target]
854+
[*Standard library crate stability*][standard-library-crate-stability]
855+
856+
## Why introduce `standard_library_support`?
857+
[rationale-why-standard-library-support]: #why-introduce-standard_library_support
858+
859+
Attempting to compile the standard library crates may fail for some targets
860+
depending on which standard library crates that target intends to support. When
861+
enabled, build-std should default to only building those crates that are
862+
expected to succeed, and should prevent the user from attempting to build those
863+
crates that are expected to fail. This will provide a much improved user
864+
experience than attempting to build standard library crates and encountering
865+
complex and unexpected compilation failures.
866+
867+
For example, `no_std` targets often do not support `std` and so should inform
868+
the error with a helpful error message that `std` cannot be built for the target
869+
rather than attempt to build it and fail with confusing and unexpected errors.
870+
Similarly, many `no_std` targets do support `alloc` if a global allocator is
871+
provided, but if build-std built `alloc` by default for these targets then it
872+
would often be unnecessary and could often fail.
873+
874+
It is not sufficient to determine which crates should be supported for a target
875+
based on its the tier. For example, targets like `aarch64-apple-tvos` are tier
876+
three while intending to fully support the standard library. It would be
877+
needlessly limiting to prevent build-std from building `std` for this target.
878+
However, build-std does provide a stable mechanism to build `std` for this
879+
target that did not previously exist, so there must be clarity about what
880+
guarantees and level of support is provided by the Rust project:
881+
882+
1. Whether a standard library crate is part of the stable interface of
883+
the standard library as a whole is determined by the library team and the set
884+
of crates that comprise this interface is the same for all targets
885+
886+
2. Whether any given standard library crate can be built with build-std is
887+
determined on a per-target basis depending on whether it is intended that the
888+
target be able to support that crate
889+
890+
3. Whether the Rust project provide guarantees or support for the standard
891+
library on a target is determined by the tier of the target
892+
893+
4. Whether the pre-built standard library is distributed for a target is
894+
determined by the tier of the target and which crates it intends to support
895+
896+
5. Which crate is built by default by build-std is determined on a per-target
897+
basis
898+
899+
For example, consider the following targets:
900+
901+
- `armv7a-none-eabihf`
902+
903+
1. As with any other target, the `std`, `alloc` and `core` crates are stable
904+
interfaces to the standard library
905+
906+
2. It intends to support the `core` and `alloc` crates, which build-std will
907+
permit to be built. `std` cannot be built by build-std for this target (on
908+
stable)
909+
910+
3. It is a tier three target, so no support or guarantees are provided for the
911+
standard library crates
912+
913+
4. It is a tier three target, so no standard library crates are distributed
914+
915+
5. `alloc` would not build without a global allocator crate being provided by
916+
the user and may not be required by all users, so only `core` will be built
917+
by default
918+
919+
- `aarch64-apple-tvos`
920+
921+
1. As with any other target, the `std`, `alloc` and `core` crates are stable
922+
interfaces to the standard library
923+
924+
2. It intends to support `core`, `alloc` and `std` crates, which build-std
925+
will permit to be built
926+
927+
3. It is a tier three target, so no support or guarantees are provided for the
928+
standard library crates
929+
930+
4. It is a tier three target, so no standard library crates are distributed
931+
932+
5. All of `core`, `alloc` and `std` will be built by default
933+
934+
- `armv7a-none-eabi`
935+
936+
1. As with any other target, the `std`, `alloc` and `core` crates are stable
937+
interfaces to the standard library
938+
939+
2. It intends to support the `core` and `alloc` crates, which build-std will
940+
permit to be built. `std` cannot be built by build-std for this target (on
941+
stable)
942+
943+
3. It is a tier two target, so the project guarantees that the `core` and
944+
`alloc` crates will build
945+
946+
4. It is a tier two target, so there are distributed artefacts for the `core`
947+
and `alloc` crates
948+
949+
5. `alloc` would not build without a global allocator crate being provided by
950+
the user and may not be required by all users, so only `core` will be built
951+
by default
952+
953+
- `aarch64-unknown-linux-gnu`
954+
955+
1. As with any other target, the `std`, `alloc` and `core` crates are stable
956+
interfaces to the standard library
957+
958+
2. It intends to support the `core`, `alloc` and `std` crates, which build-std
959+
will permit to be built
960+
961+
3. It is a tier one target, so the project guarantees that the `core`, `alloc`
962+
and `std` will build and that they have been tested
963+
964+
4. It is a tier one target, so there are distributed artefacts for the `core`,
965+
`alloc` and `std` crates
966+
967+
5. All of `core`, `alloc` and `std` will be built by default
968+
969+
[*Standard library crate stability*][standard-library-crate-stability]
836970

837971
## Why remove `restricted_std`?
838972
[rationale-remove-restricted-std]: #why-remove-restricted_std

0 commit comments

Comments
 (0)