9
9
Enable the compiler to select whether a target dynamically or statically links
10
10
to a platform's standard C runtime through the introduction of three orthogonal
11
11
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.
14
14
15
15
The end result is that rustc will be able to reuse its existing standard library
16
16
binaries for the MSVC and musl targets for building code that links either
@@ -85,27 +85,39 @@ In summary the new mechanics are:
85
85
the feature, as well as the automatic lowering to ` cfg ` values, are crucial to
86
86
later aspects of the design. This target feature will be added to targets via
87
87
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
91
92
libc crate, since std is distributed in binary form, and it may yet be a long
92
93
time before Cargo itself can rebuild std.
93
94
94
95
### Specifying dynamic/static C runtime linkage
95
96
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:
97
99
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.
102
114
103
115
### Lowering ` cfg ` values to Cargo build script environment variables
104
116
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.
109
121
110
122
When Cargo runs a build script it already sets a [ number of environment
111
123
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.
147
159
One important complication here is that ` cfg ` values in Rust may be defined
148
160
multiple times, and this is the case with target features. When a
149
161
` 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.
151
163
152
164
So for a target with the following features enabled
153
165
@@ -173,8 +185,9 @@ for the C code they might be compiling.
173
185
174
186
After this change, the gcc-rs crate will be modified to check for the
175
187
` 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 ` .
178
191
179
192
### Lazy link attributes
180
193
@@ -201,7 +214,7 @@ in two ways:
201
214
202
215
* When deciding what native libraries should be linked, the compiler will
203
216
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.
205
218
206
219
### Customizing linkage to the C runtime
207
220
@@ -262,8 +275,16 @@ would be a breaking change today due to how C components are compiled, if this
262
275
RFC is implemented it should not be a breaking change to switch the defaults in
263
276
the future.
264
277
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.
267
288
268
289
# Drawbacks
269
290
[ drawbacks ] : #drawbacks
@@ -289,7 +310,7 @@ to the standard library's code-generation.
289
310
heavyweight solution as we'd have to start distributing new artifacts and
290
311
such.
291
312
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"
293
314
along the lines of ` x86_64-pc-windows-msvc+static ` . This is a pretty big
294
315
design space, though, which may not play well with Cargo and build scripts, so
295
316
for now it's preferred to avoid this rabbit hole of design if possible.
0 commit comments