diff options
| author | Dylan DPC <99973273+Dylan-DPC@users.noreply.github.com> | 2022-03-31 00:26:28 +0200 |
|---|---|---|
| committer | GitHub <noreply@github.com> | 2022-03-31 00:26:28 +0200 |
| commit | b75f384d0b0227000eff393e5d4e11bda56b293f (patch) | |
| tree | 91e382173c58ebddd4542cece3d430ada9b8818e /src | |
| parent | c5cf08d37b85f953b132951e868df5b924250fdc (diff) | |
| parent | 1004783ef9bdcf006f0ed33badacf83a5934feb2 (diff) | |
| download | rust-b75f384d0b0227000eff393e5d4e11bda56b293f.tar.gz rust-b75f384d0b0227000eff393e5d4e11bda56b293f.zip | |
Rollup merge of #93901 - petrochenkov:linkmod, r=wesleywiser
Stabilize native library modifier syntax and the `whole-archive` modifier specifically Stabilization report: https://github.com/rust-lang/rust/pull/93901#issuecomment-1041325522 cc https://github.com/rust-lang/rust/issues/81490
Diffstat (limited to 'src')
22 files changed, 84 insertions, 86 deletions
diff --git a/src/doc/rustc/src/command-line-arguments.md b/src/doc/rustc/src/command-line-arguments.md index 3759cb632bb..7838696cc12 100644 --- a/src/doc/rustc/src/command-line-arguments.md +++ b/src/doc/rustc/src/command-line-arguments.md @@ -37,6 +37,8 @@ KIND=PATH` where `KIND` may be one of: <a id="option-l-link-lib"></a> ## `-l`: link the generated crate to a native library +Syntax: `-l [KIND[:MODIFIERS]=]NAME[:RENAME]`. + This flag allows you to specify linking to a specific native library when building a crate. @@ -47,7 +49,13 @@ where `KIND` may be one of: - `static` — A native static library (such as a `.a` archive). - `framework` — A macOS framework. -The kind of library can be specified in a [`#[link]` +If the kind is specified, then linking modifiers can be attached to it. +Modifiers are specified as a comma-delimited string with each modifier prefixed with +either a `+` or `-` to indicate that the modifier is enabled or disabled, respectively. +The last boolean value specified for a given modifier wins. \ +Example: `-l static:+whole-archive=mylib`. + +The kind of library and the modifiers can also be specified in a [`#[link]` attribute][link-attribute]. If the kind is not specified in the `link` attribute or on the command-line, it will link a dynamic library if available, otherwise it will use a static library. If the kind is specified on the @@ -59,6 +67,22 @@ and `LINK_NAME` is the name of the actual library that will be linked. [link-attribute]: ../reference/items/external-blocks.html#the-link-attribute +### Linking modifiers: `whole-archive` + +This modifier is only compatible with the `static` linking kind. +Using any other kind will result in a compiler error. + +`+whole-archive` means that the static library is linked as a whole archive +without throwing any object files away. + +This modifier translates to `--whole-archive` for `ld`-like linkers, +to `/WHOLEARCHIVE` for `link.exe`, and to `-force_load` for `ld64`. +The modifier does nothing for linkers that don't support it. + +The default for this modifier is `-whole-archive`. \ +NOTE: The default may currently be different when building dylibs for some targets, +but it is not guaranteed. + <a id="option-crate-type"></a> ## `--crate-type`: a list of types of crates for the compiler to emit diff --git a/src/doc/unstable-book/src/language-features/native-link-modifiers-whole-archive.md b/src/doc/unstable-book/src/language-features/native-link-modifiers-whole-archive.md deleted file mode 100644 index 4961e88cad1..00000000000 --- a/src/doc/unstable-book/src/language-features/native-link-modifiers-whole-archive.md +++ /dev/null @@ -1,18 +0,0 @@ -# `native_link_modifiers_whole_archive` - -The tracking issue for this feature is: [#81490] - -[#81490]: https://github.com/rust-lang/rust/issues/81490 - ------------------------- - -The `native_link_modifiers_whole_archive` feature allows you to use the `whole-archive` modifier. - -Only compatible with the `static` linking kind. Using any other kind will result in a compiler error. - -`+whole-archive` means that the static library is linked as a whole archive without throwing any object files away. - -This modifier translates to `--whole-archive` for `ld`-like linkers, to `/WHOLEARCHIVE` for `link.exe`, and to `-force_load` for `ld64`. -The modifier does nothing for linkers that don't support it. - -The default for this modifier is `-whole-archive`. diff --git a/src/doc/unstable-book/src/language-features/native-link-modifiers.md b/src/doc/unstable-book/src/language-features/native-link-modifiers.md deleted file mode 100644 index fc8b5754621..00000000000 --- a/src/doc/unstable-book/src/language-features/native-link-modifiers.md +++ /dev/null @@ -1,11 +0,0 @@ -# `native_link_modifiers` - -The tracking issue for this feature is: [#81490] - -[#81490]: https://github.com/rust-lang/rust/issues/81490 - ------------------------- - -The `native_link_modifiers` feature allows you to use the `modifiers` syntax with the `#[link(..)]` attribute. - -Modifiers are specified as a comma-delimited string with each modifier prefixed with either a `+` or `-` to indicate that the modifier is enabled or disabled, respectively. The last boolean value specified for a given modifier wins. diff --git a/src/test/run-make/native-link-modifier-whole-archive/native_lib_in_src.rs b/src/test/run-make/native-link-modifier-whole-archive/native_lib_in_src.rs index 373d89b7936..2436c36e6eb 100644 --- a/src/test/run-make/native-link-modifier-whole-archive/native_lib_in_src.rs +++ b/src/test/run-make/native-link-modifier-whole-archive/native_lib_in_src.rs @@ -1,6 +1,4 @@ #![feature(native_link_modifiers_bundle)] -#![feature(native_link_modifiers_whole_archive)] -#![feature(native_link_modifiers)] use std::io::Write; diff --git a/src/test/run-make/raw-dylib-c/lib.rs b/src/test/run-make/raw-dylib-c/lib.rs index d8e6301f38e..e185c4aec12 100644 --- a/src/test/run-make/raw-dylib-c/lib.rs +++ b/src/test/run-make/raw-dylib-c/lib.rs @@ -1,4 +1,4 @@ -#![feature(raw_dylib, native_link_modifiers, native_link_modifiers_verbatim)] +#![feature(raw_dylib, native_link_modifiers_verbatim)] #[link(name = "extern_1.dll", kind = "raw-dylib", modifiers = "+verbatim")] extern { diff --git a/src/test/ui/feature-gates/feature-gate-native_link_modifiers.rs b/src/test/ui/feature-gates/feature-gate-native_link_modifiers.rs deleted file mode 100644 index 2d00aa2a3cf..00000000000 --- a/src/test/ui/feature-gates/feature-gate-native_link_modifiers.rs +++ /dev/null @@ -1,5 +0,0 @@ -#[link(name = "foo", modifiers = "")] -//~^ ERROR: native link modifiers are experimental -extern "C" {} - -fn main() {} diff --git a/src/test/ui/feature-gates/feature-gate-native_link_modifiers.stderr b/src/test/ui/feature-gates/feature-gate-native_link_modifiers.stderr deleted file mode 100644 index 20a2d6a26fa..00000000000 --- a/src/test/ui/feature-gates/feature-gate-native_link_modifiers.stderr +++ /dev/null @@ -1,12 +0,0 @@ -error[E0658]: native link modifiers are experimental - --> $DIR/feature-gate-native_link_modifiers.rs:1:22 - | -LL | #[link(name = "foo", modifiers = "")] - | ^^^^^^^^^^^^^^ - | - = note: see issue #81490 <https://github.com/rust-lang/rust/issues/81490> for more information - = help: add `#![feature(native_link_modifiers)]` to the crate attributes to enable - -error: aborting due to previous error - -For more information about this error, try `rustc --explain E0658`. diff --git a/src/test/ui/feature-gates/feature-gate-native_link_modifiers_as_needed.rs b/src/test/ui/feature-gates/feature-gate-native_link_modifiers_as_needed.rs index 4cf8067592e..fedee812398 100644 --- a/src/test/ui/feature-gates/feature-gate-native_link_modifiers_as_needed.rs +++ b/src/test/ui/feature-gates/feature-gate-native_link_modifiers_as_needed.rs @@ -1,6 +1,3 @@ -#![allow(incomplete_features)] -#![feature(native_link_modifiers)] - #[link(name = "foo", modifiers = "+as-needed")] //~^ ERROR: `#[link(modifiers="as-needed")]` is unstable extern "C" {} diff --git a/src/test/ui/feature-gates/feature-gate-native_link_modifiers_as_needed.stderr b/src/test/ui/feature-gates/feature-gate-native_link_modifiers_as_needed.stderr index 08ce807851b..96750aa6e80 100644 --- a/src/test/ui/feature-gates/feature-gate-native_link_modifiers_as_needed.stderr +++ b/src/test/ui/feature-gates/feature-gate-native_link_modifiers_as_needed.stderr @@ -1,5 +1,5 @@ error[E0658]: `#[link(modifiers="as-needed")]` is unstable - --> $DIR/feature-gate-native_link_modifiers_as_needed.rs:4:34 + --> $DIR/feature-gate-native_link_modifiers_as_needed.rs:1:34 | LL | #[link(name = "foo", modifiers = "+as-needed")] | ^^^^^^^^^^^^ diff --git a/src/test/ui/feature-gates/feature-gate-native_link_modifiers_bundle-2.rs b/src/test/ui/feature-gates/feature-gate-native_link_modifiers_bundle-2.rs index 1b5fa78ee55..e229564950f 100644 --- a/src/test/ui/feature-gates/feature-gate-native_link_modifiers_bundle-2.rs +++ b/src/test/ui/feature-gates/feature-gate-native_link_modifiers_bundle-2.rs @@ -1,7 +1,6 @@ // Test native_link_modifiers_bundle don't need static-nobundle // check-pass -#![feature(native_link_modifiers)] #![feature(native_link_modifiers_bundle)] #[link(name = "foo", kind = "static", modifiers = "-bundle")] diff --git a/src/test/ui/feature-gates/feature-gate-native_link_modifiers_bundle-3.stderr b/src/test/ui/feature-gates/feature-gate-native_link_modifiers_bundle-3.stderr index 86ccb4e860b..900605c3b37 100644 --- a/src/test/ui/feature-gates/feature-gate-native_link_modifiers_bundle-3.stderr +++ b/src/test/ui/feature-gates/feature-gate-native_link_modifiers_bundle-3.stderr @@ -1,2 +1,2 @@ -error: linking modifiers are currently unstable, the `-Z unstable-options` flag must also be passed to use it +error: bundle linking modifier is currently unstable and only accepted on the nightly compiler diff --git a/src/test/ui/feature-gates/feature-gate-native_link_modifiers_bundle.rs b/src/test/ui/feature-gates/feature-gate-native_link_modifiers_bundle.rs index b2b1dc28e47..c3c3cff17c4 100644 --- a/src/test/ui/feature-gates/feature-gate-native_link_modifiers_bundle.rs +++ b/src/test/ui/feature-gates/feature-gate-native_link_modifiers_bundle.rs @@ -1,6 +1,3 @@ -#![allow(incomplete_features)] -#![feature(native_link_modifiers)] - #[link(name = "foo", modifiers = "+bundle")] //~^ ERROR: `#[link(modifiers="bundle")]` is unstable extern "C" {} diff --git a/src/test/ui/feature-gates/feature-gate-native_link_modifiers_bundle.stderr b/src/test/ui/feature-gates/feature-gate-native_link_modifiers_bundle.stderr index b3e22b0644a..984b90d9b6c 100644 --- a/src/test/ui/feature-gates/feature-gate-native_link_modifiers_bundle.stderr +++ b/src/test/ui/feature-gates/feature-gate-native_link_modifiers_bundle.stderr @@ -1,5 +1,5 @@ error[E0658]: `#[link(modifiers="bundle")]` is unstable - --> $DIR/feature-gate-native_link_modifiers_bundle.rs:4:34 + --> $DIR/feature-gate-native_link_modifiers_bundle.rs:1:34 | LL | #[link(name = "foo", modifiers = "+bundle")] | ^^^^^^^^^ diff --git a/src/test/ui/feature-gates/feature-gate-native_link_modifiers_verbatim.rs b/src/test/ui/feature-gates/feature-gate-native_link_modifiers_verbatim.rs index 042ce0b3f65..57527be1112 100644 --- a/src/test/ui/feature-gates/feature-gate-native_link_modifiers_verbatim.rs +++ b/src/test/ui/feature-gates/feature-gate-native_link_modifiers_verbatim.rs @@ -1,6 +1,3 @@ -#![allow(incomplete_features)] -#![feature(native_link_modifiers)] - #[link(name = "foo", modifiers = "+verbatim")] //~^ ERROR: `#[link(modifiers="verbatim")]` is unstable extern "C" {} diff --git a/src/test/ui/feature-gates/feature-gate-native_link_modifiers_verbatim.stderr b/src/test/ui/feature-gates/feature-gate-native_link_modifiers_verbatim.stderr index 8159416edfa..5c64c0d21bd 100644 --- a/src/test/ui/feature-gates/feature-gate-native_link_modifiers_verbatim.stderr +++ b/src/test/ui/feature-gates/feature-gate-native_link_modifiers_verbatim.stderr @@ -1,5 +1,5 @@ error[E0658]: `#[link(modifiers="verbatim")]` is unstable - --> $DIR/feature-gate-native_link_modifiers_verbatim.rs:4:34 + --> $DIR/feature-gate-native_link_modifiers_verbatim.rs:1:34 | LL | #[link(name = "foo", modifiers = "+verbatim")] | ^^^^^^^^^^^ diff --git a/src/test/ui/feature-gates/feature-gate-native_link_modifiers_whole_archive.rs b/src/test/ui/feature-gates/feature-gate-native_link_modifiers_whole_archive.rs deleted file mode 100644 index ca801e59114..00000000000 --- a/src/test/ui/feature-gates/feature-gate-native_link_modifiers_whole_archive.rs +++ /dev/null @@ -1,8 +0,0 @@ -#![allow(incomplete_features)] -#![feature(native_link_modifiers)] - -#[link(name = "foo", modifiers = "+whole-archive")] -//~^ ERROR: `#[link(modifiers="whole-archive")]` is unstable -extern "C" {} - -fn main() {} diff --git a/src/test/ui/feature-gates/feature-gate-native_link_modifiers_whole_archive.stderr b/src/test/ui/feature-gates/feature-gate-native_link_modifiers_whole_archive.stderr deleted file mode 100644 index cacaa789ecb..00000000000 --- a/src/test/ui/feature-gates/feature-gate-native_link_modifiers_whole_archive.stderr +++ /dev/null @@ -1,12 +0,0 @@ -error[E0658]: `#[link(modifiers="whole-archive")]` is unstable - --> $DIR/feature-gate-native_link_modifiers_whole_archive.rs:4:34 - | -LL | #[link(name = "foo", modifiers = "+whole-archive")] - | ^^^^^^^^^^^^^^^^ - | - = note: see issue #81490 <https://github.com/rust-lang/rust/issues/81490> for more information - = help: add `#![feature(native_link_modifiers_whole_archive)]` to the crate attributes to enable - -error: aborting due to previous error - -For more information about this error, try `rustc --explain E0658`. diff --git a/src/test/ui/native-library-link-flags/mix-bundle-and-whole-archive-link-attr.rs b/src/test/ui/native-library-link-flags/mix-bundle-and-whole-archive-link-attr.rs index c3714a38451..b153ef94626 100644 --- a/src/test/ui/native-library-link-flags/mix-bundle-and-whole-archive-link-attr.rs +++ b/src/test/ui/native-library-link-flags/mix-bundle-and-whole-archive-link-attr.rs @@ -2,9 +2,7 @@ // build-fail // error-pattern: the linking modifiers `+bundle` and `+whole-archive` are not compatible with each other when generating rlibs -#![feature(native_link_modifiers)] #![feature(native_link_modifiers_bundle)] -#![feature(native_link_modifiers_whole_archive)] #[link(name = "mylib", kind = "static", modifiers = "+bundle,+whole-archive")] extern "C" { } diff --git a/src/test/ui/native-library-link-flags/modifiers-override-2.rs b/src/test/ui/native-library-link-flags/modifiers-override-2.rs new file mode 100644 index 00000000000..333f6786b0f --- /dev/null +++ b/src/test/ui/native-library-link-flags/modifiers-override-2.rs @@ -0,0 +1,3 @@ +// compile-flags:-lstatic:+whole-archive,-whole-archive=foo + +fn main() {} diff --git a/src/test/ui/native-library-link-flags/modifiers-override-2.stderr b/src/test/ui/native-library-link-flags/modifiers-override-2.stderr new file mode 100644 index 00000000000..9200d7bfb0c --- /dev/null +++ b/src/test/ui/native-library-link-flags/modifiers-override-2.stderr @@ -0,0 +1,2 @@ +error: duplicating linking modifier is currently unstable and only accepted on the nightly compiler + diff --git a/src/test/ui/native-library-link-flags/modifiers-override.rs b/src/test/ui/native-library-link-flags/modifiers-override.rs new file mode 100644 index 00000000000..f6d770559e6 --- /dev/null +++ b/src/test/ui/native-library-link-flags/modifiers-override.rs @@ -0,0 +1,17 @@ +// compile-flags:-ldylib:+as-needed=foo -lstatic=bar -Zunstable-options + +#![feature(native_link_modifiers_bundle)] + +#[link(name = "foo")] +#[link( //~ ERROR multiple `modifiers` arguments in a single `#[link]` attribute + name = "bar", + kind = "static", + modifiers = "+whole-archive,-whole-archive", + //~^ ERROR same modifier is used multiple times in a single `modifiers` argument + modifiers = "+bundle" +)] +extern "C" {} +//~^ ERROR overriding linking modifiers from command line is not supported +//~| ERROR overriding linking modifiers from command line is not supported + +fn main() {} diff --git a/src/test/ui/native-library-link-flags/modifiers-override.stderr b/src/test/ui/native-library-link-flags/modifiers-override.stderr new file mode 100644 index 00000000000..8644d2382d2 --- /dev/null +++ b/src/test/ui/native-library-link-flags/modifiers-override.stderr @@ -0,0 +1,32 @@ +error: same modifier is used multiple times in a single `modifiers` argument + --> $DIR/modifiers-override.rs:9:5 + | +LL | modifiers = "+whole-archive,-whole-archive", + | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ + +error: multiple `modifiers` arguments in a single `#[link]` attribute + --> $DIR/modifiers-override.rs:6:1 + | +LL | / #[link( +LL | | name = "bar", +LL | | kind = "static", +LL | | modifiers = "+whole-archive,-whole-archive", +LL | | +LL | | modifiers = "+bundle" +LL | | )] + | |__^ + +error: overriding linking modifiers from command line is not supported + --> $DIR/modifiers-override.rs:13:1 + | +LL | extern "C" {} + | ^^^^^^^^^^^^^ + +error: overriding linking modifiers from command line is not supported + --> $DIR/modifiers-override.rs:13:1 + | +LL | extern "C" {} + | ^^^^^^^^^^^^^ + +error: aborting due to 4 previous errors + |
