Auto merge of #95818 - petrochenkov:stabundle, r=wesleywiser

Stabilize the `bundle` native library modifier

And remove the legacy `static-nobundle` linking kind.

Stabilization report - https://github.com/rust-lang/rust/pull/95818#issuecomment-1120470945.

cc #81490
Closes #37403
This commit is contained in:
bors 2022-06-10 06:25:02 +00:00
commit 3dea0033f7
35 changed files with 91 additions and 196 deletions

View File

@ -1952,7 +1952,7 @@ fn linker_with_args<'a, B: ArchiveBuilder<'a>>(
add_local_native_libraries(cmd, sess, codegen_results);
}
// Upstream rust libraries and their nobundle static libraries
// Upstream rust libraries and their non-bundled static libraries
add_upstream_rust_crates::<B>(cmd, sess, codegen_results, crate_type, tmpdir);
// Upstream dynamic native libraries linked with `#[link]` attributes at and `-l`
@ -2237,7 +2237,7 @@ fn add_local_native_libraries(
}
}
/// # Linking Rust crates and their nobundle static libraries
/// # Linking Rust crates and their non-bundled static libraries
///
/// Rust crates are not considered at all when creating an rlib output. All dependencies will be
/// linked when producing the final output (instead of the intermediate rlib version).

View File

@ -16,10 +16,10 @@ Using this declaration, it must be called with at least one argument, so
simply calling `printf()` is invalid. But the following uses are allowed:
```
# #![feature(static_nobundle)]
# use std::os::raw::{c_char, c_int};
# #[cfg_attr(all(windows, target_env = "msvc"),
# link(name = "legacy_stdio_definitions", kind = "static-nobundle"))]
# link(name = "legacy_stdio_definitions",
# kind = "static", modifiers = "-bundle"))]
# extern "C" { fn printf(_: *const c_char, ...) -> c_int; }
# fn main() {
unsafe {

View File

@ -219,6 +219,8 @@ declare_features! (
(accepted, move_ref_pattern, "1.49.0", Some(68354), None),
/// Allows specifying modifiers in the link attribute: `#[link(modifiers = "...")]`
(accepted, native_link_modifiers, "1.61.0", Some(81490), None),
/// Allows specifying the bundle link modifier
(accepted, native_link_modifiers_bundle, "1.63.0", Some(81490), None),
/// Allows specifying the whole-archive link modifier
(accepted, native_link_modifiers_whole_archive, "1.61.0", Some(81490), None),
/// Allows using non lexical lifetimes (RFC 2094).

View File

@ -451,8 +451,6 @@ declare_features! (
(active, naked_functions, "1.9.0", Some(32408), None),
/// Allows specifying the as-needed link modifier
(active, native_link_modifiers_as_needed, "1.53.0", Some(81490), None),
/// Allows specifying the bundle link modifier
(active, native_link_modifiers_bundle, "1.53.0", Some(81490), None),
/// Allows specifying the verbatim link modifier
(active, native_link_modifiers_verbatim, "1.53.0", Some(81490), None),
/// Allow negative trait implementations.
@ -502,8 +500,6 @@ declare_features! (
(active, simd_ffi, "1.0.0", Some(27731), None),
/// Allows specialization of implementations (RFC 1210).
(incomplete, specialization, "1.7.0", Some(31844), None),
/// Allows `#[link(kind="static-nobundle"...)]`.
(active, static_nobundle, "1.16.0", Some(37403), None),
/// Allows attributes on expressions and non-item statements.
(active, stmt_expr_attributes, "1.6.0", Some(15701), None),
/// Allows lints part of the strict provenance effort.

View File

@ -169,6 +169,9 @@ declare_features! (
(removed, sanitizer_runtime, "1.17.0", None, None, None),
(removed, simd, "1.0.0", Some(27731), None,
Some("removed in favor of `#[repr(simd)]`")),
/// Allows `#[link(kind = "static-nobundle", ...)]`.
(removed, static_nobundle, "1.16.0", Some(37403), None,
Some(r#"subsumed by `#[link(kind = "static", modifiers = "-bundle", ...)]`"#)),
(removed, struct_inherit, "1.0.0", None, None, None),
(removed, test_removed_feature, "1.0.0", None, None, None),
/// Allows using items which are missing stability attributes

View File

@ -97,24 +97,6 @@ impl<'tcx> Collector<'tcx> {
let span = item.name_value_literal_span().unwrap();
let link_kind = match link_kind.as_str() {
"static" => NativeLibKind::Static { bundle: None, whole_archive: None },
"static-nobundle" => {
sess.struct_span_warn(
span,
"link kind `static-nobundle` has been superseded by specifying \
modifier `-bundle` with link kind `static`",
)
.emit();
if !features.static_nobundle {
feature_err(
&sess.parse_sess,
sym::static_nobundle,
span,
"link kind `static-nobundle` is unstable",
)
.emit();
}
NativeLibKind::Static { bundle: Some(false), whole_archive: None }
}
"dylib" => NativeLibKind::Dylib { as_needed: None },
"framework" => {
if !sess.target.is_like_osx {
@ -264,7 +246,6 @@ impl<'tcx> Collector<'tcx> {
};
match (modifier, &mut kind) {
("bundle", Some(NativeLibKind::Static { bundle, .. })) => {
report_unstable_modifier!(native_link_modifiers_bundle);
assign_modifier(bundle)
}
("bundle", _) => {

View File

@ -1920,21 +1920,6 @@ fn parse_native_lib_kind(
let kind = match kind {
"static" => NativeLibKind::Static { bundle: None, whole_archive: None },
"static-nobundle" => {
early_warn(
error_format,
"library kind `static-nobundle` has been superseded by specifying \
modifier `-bundle` with library kind `static`. Try `static:-bundle`",
);
if !nightly_options::match_is_nightly_build(matches) {
early_error(
error_format,
"library kind `static-nobundle` is unstable \
and only accepted on the nightly compiler",
);
}
NativeLibKind::Static { bundle: Some(false), whole_archive: None }
}
"dylib" => NativeLibKind::Dylib { as_needed: None },
"framework" => NativeLibKind::Framework { as_needed: None },
_ => early_error(
@ -1987,10 +1972,7 @@ fn parse_native_lib_modifiers(
}
};
match (modifier, &mut kind) {
("bundle", NativeLibKind::Static { bundle, .. }) => {
report_unstable_modifier();
assign_modifier(bundle)
}
("bundle", NativeLibKind::Static { bundle, .. }) => assign_modifier(bundle),
("bundle", _) => early_error(
error_format,
"linking modifier `bundle` is only compatible with `static` linking kind",

View File

@ -1,7 +1,7 @@
#![no_std]
#![unstable(feature = "panic_unwind", issue = "32837")]
#![feature(link_cfg)]
#![feature(native_link_modifiers_bundle)]
#![cfg_attr(bootstrap, feature(native_link_modifiers_bundle))]
#![feature(staged_api)]
#![feature(c_unwind)]
#![feature(cfg_target_abi)]

View File

@ -84,6 +84,26 @@ The default for this modifier is `-whole-archive`. \
NOTE: The default may currently be different in some cases for backward compatibility,
but it is not guaranteed. If you need whole archive semantics use `+whole-archive` explicitly.
### Linking modifiers: `bundle`
This modifier is only compatible with the `static` linking kind.
Using any other kind will result in a compiler error.
When building a rlib or staticlib `+bundle` means that all object files from the native static
library will be added to the rlib or staticlib archive, and then used from it during linking of
the final binary.
When building a rlib `-bundle` means that the native static library is registered as a dependency
of that rlib "by name", and object files from it are included only during linking of the final
binary, the file search by that name is also performed during final linking. \
When building a staticlib `-bundle` means that the native static library is simply not included
into the archive and some higher level build system will need to add it later during linking of
the final binary.
This modifier has no effect when building other targets like executables or dynamic libraries.
The default for this modifier is `+bundle`.
<a id="option-crate-type"></a>
## `--crate-type`: a list of types of crates for the compiler to emit

View File

@ -1,19 +0,0 @@
# `native_link_modifiers_bundle`
The tracking issue for this feature is: [#81490]
[#81490]: https://github.com/rust-lang/rust/issues/81490
------------------------
The `native_link_modifiers_bundle` feature allows you to use the `bundle` modifier.
Only compatible with the `static` linking kind. Using any other kind will result in a compiler error.
`+bundle` means objects from the static library are bundled into the produced crate (a rlib, for example) and are used from this crate later during linking of the final binary.
`-bundle` means the static library is included into the produced rlib "by name" and object files from it are included only during linking of the final binary, the file search by that name is also performed during final linking.
This modifier is supposed to supersede the `static-nobundle` linking kind defined by [RFC 1717](https://github.com/rust-lang/rfcs/pull/1717).
The default for this modifier is currently `+bundle`, but it could be changed later on some future edition boundary.

View File

@ -1,23 +0,0 @@
-include ../tools.mk
# aaa is a native static library
# bbb is a rlib
# ccc is a dylib
# ddd is an executable
all: $(call NATIVE_STATICLIB,aaa)
$(RUSTC) bbb.rs --crate-type=rlib
# Check that bbb does NOT contain the definition of `native_func`
# We're using the llvm-nm instead of the system nm to ensure it
# is compatible with the LLVM bitcode generated by rustc.
"$(LLVM_BIN_DIR)/llvm-nm" $(TMPDIR)/libbbb.rlib | $(CGREP) -ve "T _*native_func"
"$(LLVM_BIN_DIR)/llvm-nm" $(TMPDIR)/libbbb.rlib | $(CGREP) -e "U _*native_func"
# Check that aaa gets linked (either as `-l aaa` or `aaa.lib`) when building ccc.
$(RUSTC) ccc.rs -C prefer-dynamic --crate-type=dylib --print link-args | $(CGREP) -e '-l[" ]*aaa|aaa\.lib'
# Check that aaa does NOT get linked when building ddd.
$(RUSTC) ddd.rs --print link-args | $(CGREP) -ve '-l[" ]*aaa|aaa\.lib'
$(call RUN,ddd)

View File

@ -1,13 +0,0 @@
#![crate_type = "rlib"]
#![feature(static_nobundle)]
#[link(name = "aaa", kind = "static-nobundle")]
extern "C" {
pub fn native_func();
}
pub fn wrapped_func() {
unsafe {
native_func();
}
}

View File

@ -1,13 +0,0 @@
#![crate_type = "dylib"]
extern crate bbb;
pub fn do_work() {
unsafe { bbb::native_func(); }
bbb::wrapped_func();
}
pub fn do_work_generic<T>() {
unsafe { bbb::native_func(); }
bbb::wrapped_func();
}

View File

@ -1,7 +0,0 @@
extern crate ccc;
fn main() {
ccc::do_work();
ccc::do_work_generic::<i16>();
ccc::do_work_generic::<i32>();
}

View File

@ -120,7 +120,7 @@ else
# So we end up with the following hack: we link use static:-bundle to only
# link the parts of libstdc++ that we actually use, which doesn't include
# the dependency on the pthreads DLL.
EXTRARSCXXFLAGS := -l static:-bundle=stdc++ -Z unstable-options
EXTRARSCXXFLAGS := -l static:-bundle=stdc++
endif
else
ifeq ($(UNAME),Darwin)

View File

@ -0,0 +1,29 @@
# ignore-cross-compile
# ignore-windows-msvc
-include ../../run-make-fulldeps/tools.mk
all: $(call NATIVE_STATICLIB,native-staticlib)
# Build a staticlib and a rlib, the `native_func` symbol will be bundled into them
$(RUSTC) bundled.rs --crate-type=staticlib --crate-type=rlib
nm $(TMPDIR)/libbundled.a | $(CGREP) -e "T _*native_func"
nm $(TMPDIR)/libbundled.a | $(CGREP) -e "U _*native_func"
nm $(TMPDIR)/libbundled.rlib | $(CGREP) -e "T _*native_func"
nm $(TMPDIR)/libbundled.rlib | $(CGREP) -e "U _*native_func"
# Build a staticlib and a rlib, the `native_func` symbol will not be bundled into it
$(RUSTC) non-bundled.rs --crate-type=staticlib --crate-type=rlib
nm $(TMPDIR)/libnon_bundled.a | $(CGREP) -ve "T _*native_func"
nm $(TMPDIR)/libnon_bundled.a | $(CGREP) -e "U _*native_func"
nm $(TMPDIR)/libnon_bundled.rlib | $(CGREP) -ve "T _*native_func"
nm $(TMPDIR)/libnon_bundled.rlib | $(CGREP) -e "U _*native_func"
# Build a cdylib, `native-staticlib` will not appear on the linker line because it was bundled previously
# The cdylib will contain the `native_func` symbol in the end
$(RUSTC) cdylib-bundled.rs --crate-type=cdylib --print link-args | $(CGREP) -ve '-l[" ]*native-staticlib'
nm $(call DYLIB,cdylib_bundled) | $(CGREP) -e "[Tt] _*native_func"
# Build a cdylib, `native-staticlib` will appear on the linker line because it was not bundled previously
# The cdylib will contain the `native_func` symbol in the end
$(RUSTC) cdylib-non-bundled.rs --crate-type=cdylib --print link-args | $(CGREP) -e '-l[" ]*native-staticlib'
nm $(call DYLIB,cdylib_non_bundled) | $(CGREP) -e "[Tt] _*native_func"

View File

@ -0,0 +1,11 @@
#[link(name = "native-staticlib", kind = "static", modifiers = "+bundle")]
extern "C" {
pub fn native_func();
}
#[no_mangle]
pub extern "C" fn wrapped_func() {
unsafe {
native_func();
}
}

View File

@ -0,0 +1 @@
extern crate bundled;

View File

@ -0,0 +1 @@
extern crate non_bundled;

View File

@ -0,0 +1,11 @@
#[link(name = "native-staticlib", kind = "static", modifiers = "-bundle")]
extern "C" {
pub fn native_func();
}
#[no_mangle]
pub extern "C" fn wrapped_func() {
unsafe {
native_func();
}
}

View File

@ -17,7 +17,7 @@ all: $(TMPDIR)/$(call BIN,directly_linked) $(TMPDIR)/$(call BIN,indirectly_linke
# Native lib linked directly into executable
$(TMPDIR)/$(call BIN,directly_linked): $(call NATIVE_STATICLIB,c_static_lib_with_constructor)
$(RUSTC) directly_linked.rs -Z unstable-options -l static:+whole-archive=c_static_lib_with_constructor
$(RUSTC) directly_linked.rs -l static:+whole-archive=c_static_lib_with_constructor
# Native lib linked into RLIB via `-l static:-bundle,+whole-archive`, RLIB linked into executable
$(TMPDIR)/$(call BIN,indirectly_linked): $(TMPDIR)/librlib_with_cmdline_native_lib.rlib
@ -29,7 +29,7 @@ $(TMPDIR)/$(call BIN,indirectly_linked_via_attr): $(TMPDIR)/libnative_lib_in_src
# Native lib linked into rlib with via commandline
$(TMPDIR)/librlib_with_cmdline_native_lib.rlib: $(call NATIVE_STATICLIB,c_static_lib_with_constructor)
$(RUSTC) rlib_with_cmdline_native_lib.rs -Z unstable-options --crate-type=rlib -l static:-bundle,+whole-archive=c_static_lib_with_constructor
$(RUSTC) rlib_with_cmdline_native_lib.rs --crate-type=rlib -l static:-bundle,+whole-archive=c_static_lib_with_constructor
# Native lib linked into rlib via `#[link()]` attribute on extern block.
$(TMPDIR)/libnative_lib_in_src.rlib: $(call NATIVE_STATICLIB,c_static_lib_with_constructor)

View File

@ -1,5 +1,3 @@
#![feature(native_link_modifiers_bundle)]
use std::io::Write;
#[link(name = "c_static_lib_with_constructor",

View File

@ -1,9 +0,0 @@
// Test native_link_modifiers_bundle don't need static-nobundle
// check-pass
#![feature(native_link_modifiers_bundle)]
#[link(name = "foo", kind = "static", modifiers = "-bundle")]
extern "C" {}
fn main() {}

View File

@ -1,3 +0,0 @@
// compile-flags: -l static:-bundle=nonexistent
fn main() {}

View File

@ -1,2 +0,0 @@
error: linking modifier `bundle` is unstable and only accepted on the nightly compiler

View File

@ -1,5 +0,0 @@
#[link(name = "foo", kind = "static", modifiers = "+bundle")]
//~^ ERROR: linking modifier `bundle` is unstable
extern "C" {}
fn main() {}

View File

@ -1,12 +0,0 @@
error[E0658]: linking modifier `bundle` is unstable
--> $DIR/feature-gate-native_link_modifiers_bundle.rs:1:51
|
LL | #[link(name = "foo", kind = "static", modifiers = "+bundle")]
| ^^^^^^^^^
|
= note: see issue #81490 <https://github.com/rust-lang/rust/issues/81490> for more information
= help: add `#![feature(native_link_modifiers_bundle)]` to the crate attributes to enable
error: aborting due to previous error
For more information about this error, try `rustc --explain E0658`.

View File

@ -1,4 +0,0 @@
// check-pass
// compile-flags: -l static-nobundle=nonexistent
fn main() {}

View File

@ -1,2 +0,0 @@
warning: library kind `static-nobundle` has been superseded by specifying modifier `-bundle` with library kind `static`. Try `static:-bundle`

View File

@ -1,6 +0,0 @@
#[link(name = "foo", kind = "static-nobundle")]
//~^ WARNING: link kind `static-nobundle` has been superseded by specifying modifier `-bundle` with link kind `static`
//~^^ ERROR: link kind `static-nobundle` is unstable
extern "C" {}
fn main() {}

View File

@ -1,18 +0,0 @@
warning: link kind `static-nobundle` has been superseded by specifying modifier `-bundle` with link kind `static`
--> $DIR/feature-gate-static-nobundle.rs:1:29
|
LL | #[link(name = "foo", kind = "static-nobundle")]
| ^^^^^^^^^^^^^^^^^
error[E0658]: link kind `static-nobundle` is unstable
--> $DIR/feature-gate-static-nobundle.rs:1:29
|
LL | #[link(name = "foo", kind = "static-nobundle")]
| ^^^^^^^^^^^^^^^^^
|
= note: see issue #37403 <https://github.com/rust-lang/rust/issues/37403> for more information
= help: add `#![feature(static_nobundle)]` to the crate attributes to enable
error: aborting due to previous error; 1 warning emitted
For more information about this error, try `rustc --explain E0658`.

View File

@ -2,8 +2,6 @@
// build-fail
// error-pattern: the linking modifiers `+bundle` and `+whole-archive` are not compatible with each other when generating rlibs
#![feature(native_link_modifiers_bundle)]
#[link(name = "mylib", kind = "static", modifiers = "+bundle,+whole-archive")]
extern "C" { }

View File

@ -1,7 +1,5 @@
// compile-flags:-ldylib:+as-needed=foo -lstatic=bar -Zunstable-options
#![feature(native_link_modifiers_bundle)]
#[link(name = "foo")]
#[link(
name = "bar",

View File

@ -1,23 +1,23 @@
error: multiple `modifiers` arguments in a single `#[link]` attribute
--> $DIR/modifiers-override.rs:11:5
--> $DIR/modifiers-override.rs:9:5
|
LL | modifiers = "+bundle"
| ^^^^^^^^^^^^^^^^^^^^^
error: multiple `whole-archive` modifiers in a single `modifiers` argument
--> $DIR/modifiers-override.rs:9:17
--> $DIR/modifiers-override.rs:7:17
|
LL | modifiers = "+whole-archive,-whole-archive",
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
error: overriding linking modifiers from command line is not supported
--> $DIR/modifiers-override.rs:14:1
--> $DIR/modifiers-override.rs:12:1
|
LL | extern "C" {}
| ^^^^^^^^^^^^^
error: overriding linking modifiers from command line is not supported
--> $DIR/modifiers-override.rs:14:1
--> $DIR/modifiers-override.rs:12:1
|
LL | extern "C" {}
| ^^^^^^^^^^^^^