Skip to content

Commit efd28c6

Browse files
committed
Resolve some TODO
1 parent 5864dd3 commit efd28c6

File tree

1 file changed

+42
-21
lines changed

1 file changed

+42
-21
lines changed

text/0000-crt-link.md

Lines changed: 42 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -9,8 +9,8 @@
99
Enable the compiler to select whether a target dynamically or statically links
1010
to a platform's standard C runtime through the introduction of three orthogonal
1111
and otherwise general purpose features, one of which will likely never become
12-
stable and can be considered an implementation detail of std. These features
13-
require rustc to have no intrinsic knowledge of the existence of C runtimes.
12+
stable and can be considered an implementation detail of std. These features do
13+
not require rustc to have intrinsic knowledge of the existence of C runtimes.
1414

1515
The end result is that rustc will be able to reuse its existing standard library
1616
binaries for the MSVC and musl targets for building code that links either
@@ -85,27 +85,39 @@ In summary the new mechanics are:
8585
the feature, as well as the automatic lowering to `cfg` values, are crucial to
8686
later aspects of the design. This target feature will be added to targets via
8787
a small extension to the compiler's target specification.
88-
- Lowering `cfg` values to Cargo build script environment variables. TODO describe
89-
key points.
90-
- Lazy link attributes. TODO. This feature is only required by std's own copy of the
88+
- Lowering `cfg` values to Cargo build script environment variables. This will
89+
enable build scripts to understand all enabled features of a target (like
90+
`crt-static` above) to, for example, compile C code correctly on MSVC.
91+
- Lazy link attributes. This feature is only required by std's own copy of the
9192
libc crate, since std is distributed in binary form, and it may yet be a long
9293
time before Cargo itself can rebuild std.
9394

9495
### Specifying dynamic/static C runtime linkage
9596

96-
`-C target-feature=crt-static`
97+
A new `target-feature` flag will now be supported by the compiler for relevant
98+
targets: `crt-static`. This can be enabled and disabled in the compiler via:
9799

98-
TODO An extension to target specifications that allows custom target-features to be
99-
defined, as well as to indicate whether that feature is on by default. Most
100-
existing targets will define `crt-static`; the existing "musl" targets will
101-
enable `crt-static` by default.
100+
```
101+
rustc -C target-feature=+crt-static ...
102+
rustc -C target-feature=-crt-static ...
103+
```
104+
105+
Currently all `target-feature` flags are passed through straight to LLVM, but
106+
this proposes extending the meaning of `target-feature` to Rust-target-specific
107+
features as well. Target specifications will be able to indicate what custom
108+
target-features can be defined, and most existing target will define a new
109+
`crt-static` feature which is turned off by default (except for musl).
110+
111+
The default of `crt-static` will be different depending on the target. For
112+
example `x86_64-unknown-linux-musl` will have it on by default, whereas
113+
`arm-unknown-linux-musleabi` will have it turned off by default.
102114

103115
### Lowering `cfg` values to Cargo build script environment variables
104116

105-
The first feature proposed is enabling Cargo to forward `#[cfg]` directives from
106-
the compiler into build scripts. Currently the compiler supports `--print cfg`
107-
as a flag to print out internal cfg directives, which Cargo currently uses to
108-
implement platform-specific dependencies.
117+
Cargo will begin to forward `#[cfg]` directives from the compiler into build
118+
scripts. Currently the compiler supports `--print cfg` as a flag to print out
119+
internal cfg directives, which Cargo currently uses to implement
120+
platform-specific dependencies.
109121

110122
When Cargo runs a build script it already sets a [number of environment
111123
variables][cargo-build-env], and it will now set a family of `CARGO_CFG_*`
@@ -147,7 +159,7 @@ will be specified as a target feature, which is lowered to a `cfg` value.
147159
One important complication here is that `cfg` values in Rust may be defined
148160
multiple times, and this is the case with target features. When a
149161
`cfg` value is defined multiple times, Cargo will create a single environment
150-
variable with a comma-seperated list of values.
162+
variable with a comma-separated list of values.
151163

152164
So for a target with the following features enabled
153165

@@ -173,8 +185,9 @@ for the C code they might be compiling.
173185

174186
After this change, the gcc-rs crate will be modified to check for the
175187
`CARGO_CFG_TARGET_FEATURE` directive, and parse it into a list of enabled
176-
features. If the `crt-static` feature is not enabled it will compile C code with
177-
`/MD`. Otherwise if the value is `static` it will compile code with `/MT`.
188+
features. If the `crt-static` feature is not enabled it will compile C code on
189+
the MSVC target with `/MD`. Otherwise if the value is `static` it will compile
190+
code with `/MT`.
178191

179192
### Lazy link attributes
180193

@@ -201,7 +214,7 @@ in two ways:
201214

202215
* When deciding what native libraries should be linked, the compiler will
203216
evaluate whether they should be linked or not depending on the current
204-
compilation's `#[cfg]` directives nad the upstream `#[link]` directives.
217+
compilation's `#[cfg]` directives and the upstream `#[link]` directives.
205218

206219
### Customizing linkage to the C runtime
207220

@@ -262,8 +275,16 @@ would be a breaking change today due to how C components are compiled, if this
262275
RFC is implemented it should not be a breaking change to switch the defaults in
263276
the future.
264277

265-
TODO: discuss how this could with std-aware cargo to apply dllimport/export correctly
266-
to the standard library's code-generation.
278+
The support in this RFC implies that the exact artifacts that we're shipping
279+
will be usable for both dynamically and statically linking the CRT.
280+
Unfortunately, however, on MSVC code is compiled differently if it's linking to
281+
a dynamic library or not. The standard library uses very little of the MSVCRT,
282+
so this won't be a problem in practice for now, but runs the risk of binding our
283+
hands in the future. It's intended, though, that Cargo [will eventually support
284+
custom-compiling the standard library][std-aware cargo]. The `crt-static`
285+
feature would simply be another input to this logic, so Cargo would
286+
custom-compile the standard library if it differed from the upstream artifacts,
287+
solving this problem.
267288

268289
# Drawbacks
269290
[drawbacks]: #drawbacks
@@ -289,7 +310,7 @@ to the standard library's code-generation.
289310
heavyweight solution as we'd have to start distributing new artifacts and
290311
such.
291312

292-
* Another possibility would be to start storing metdata in the "target name"
313+
* Another possibility would be to start storing metadata in the "target name"
293314
along the lines of `x86_64-pc-windows-msvc+static`. This is a pretty big
294315
design space, though, which may not play well with Cargo and build scripts, so
295316
for now it's preferred to avoid this rabbit hole of design if possible.

0 commit comments

Comments
 (0)