Skip to content

Commit 0079625

Browse files
committed
Auto merge of rust-lang#118448 - ZetaNumbers:link_arg_attribute, r=petrochenkov
Enable `link-arg` link kind inside of `#[link]` attribute rust-lang#99427 (comment) > ... > This would help to make `link-arg` usable in `#[link]` attributes and e.g. wrap libc and libgcc into a group (*) in the libc crate like > > ``` > #[link(kind = "link-arg", name = "--start-group")] > #[link(kind = "static", name = "c")] > #[link(kind = "static", name = "gcc")] > #[link(kind = "link-arg", name = "--end-group")] > ``` > > (*) to address cyclic dependencies between them > > This is an analogue of CMake's LINKER: prefix (https://cmake.org/cmake/help/git-stage/command/target_link_options.html#handling-compiler-driver-differences), and was discussed as a possible future extension in the link modifier RFC (https://github.com/rust-lang/rfcs/blob/master/text/2951-native-link-modifiers.md#support-linkarg--string-in-addition-to-the-modifiers).
2 parents 07921b5 + f7617c1 commit 0079625

File tree

19 files changed

+114
-26
lines changed

19 files changed

+114
-26
lines changed

compiler/rustc_feature/src/unstable.rs

+3
Original file line numberDiff line numberDiff line change
@@ -500,6 +500,9 @@ declare_features! (
500500
(incomplete, lazy_type_alias, "1.72.0", Some(112792), None),
501501
/// Allows `if/while p && let q = r && ...` chains.
502502
(unstable, let_chains, "1.37.0", Some(53667), None),
503+
/// Allows using `#[link(kind = "link-arg", name = "...")]`
504+
/// to pass custom arguments to the linker.
505+
(unstable, link_arg_attribute, "CURRENT_RUSTC_VERSION", Some(99427), None),
503506
/// Allows using `reason` in lint attributes and the `#[expect(lint)]` lint check.
504507
(unstable, lint_reasons, "1.31.0", Some(54503), None),
505508
/// Give access to additional metadata about declarative macro meta-variables.

compiler/rustc_metadata/messages.ftl

+1-1
Original file line numberDiff line numberDiff line change
@@ -269,7 +269,7 @@ metadata_unknown_import_name_type =
269269
unknown import name type `{$import_name_type}`, expected one of: decorated, noprefix, undecorated
270270
271271
metadata_unknown_link_kind =
272-
unknown link kind `{$kind}`, expected one of: static, dylib, framework, raw-dylib
272+
unknown link kind `{$kind}`, expected one of: static, dylib, framework, raw-dylib, link-arg
273273
.label = unknown link kind
274274
275275
metadata_unknown_link_modifier =

compiler/rustc_metadata/src/native_libs.rs

+12
Original file line numberDiff line numberDiff line change
@@ -160,6 +160,18 @@ impl<'tcx> Collector<'tcx> {
160160
}
161161
NativeLibKind::RawDylib
162162
}
163+
"link-arg" => {
164+
if !features.link_arg_attribute {
165+
feature_err(
166+
&sess.parse_sess,
167+
sym::link_arg_attribute,
168+
span,
169+
"link kind `link-arg` is unstable",
170+
)
171+
.emit();
172+
}
173+
NativeLibKind::LinkArg
174+
}
163175
kind => {
164176
sess.emit_err(errors::UnknownLinkKind { span, kind });
165177
continue;

compiler/rustc_span/src/symbol.rs

+1
Original file line numberDiff line numberDiff line change
@@ -934,6 +934,7 @@ symbols! {
934934
likely,
935935
line,
936936
link,
937+
link_arg_attribute,
937938
link_args,
938939
link_cfg,
939940
link_llvm_intrinsics,
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
# `link_arg_attribute`
2+
3+
The tracking issue for this feature is: [#99427]
4+
5+
------
6+
7+
The `link_arg_attribute` feature allows passing arguments into the linker
8+
from inside of the source code. Order is preserved for link attributes as
9+
they were defined on a single extern block:
10+
11+
```rust,no_run
12+
#![feature(link_arg_attribute)]
13+
14+
#[link(kind = "link-arg", name = "--start-group")]
15+
#[link(kind = "static", name = "c")]
16+
#[link(kind = "static", name = "gcc")]
17+
#[link(kind = "link-arg", name = "--end-group")]
18+
extern "C" {}
19+
```
20+
21+
[#99427]: https://github.com/rust-lang/rust/issues/99427

tests/run-make/pass-linker-flags-flavor/Makefile

+6-3
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,9 @@
33
include ../tools.mk
44

55
all:
6-
$(RUSTC) rs.rs -Z unstable-options -C linker-flavor=gnu-cc -l static=l1 -l link-arg=a1 -l static=l2 -l link-arg=a2 -l dylib=d1 -l link-arg=a3 --print link-args | $(CGREP) -e 'l1.*-Wl,a1.*l2.*-Wl,a2.*d1.*-Wl,a3'
7-
$(RUSTC) rs.rs -Z unstable-options -C linker-flavor=gnu-cc -l static=l1 -l link-arg:+verbatim=a1 -l static=l2 -l link-arg=a2 -l dylib=d1 -l link-arg=a3 --print link-args | $(CGREP) -e 'l1.*"a1".*l2.*-Wl,a2.*d1.*-Wl,a3'
8-
$(RUSTC) rs.rs -Z unstable-options -C linker-flavor=ld -l static=l1 -l link-arg=a1 -l static=l2 -l link-arg=a2 -l dylib=d1 -l link-arg=a3 --print link-args | $(CGREP) -e 'l1.*"a1".*l2.*"a2".*d1.*"a3"'
6+
$(RUSTC) empty.rs -Z unstable-options -C linker-flavor=gnu-cc -l static=l1 -l link-arg=a1 -l static=l2 -l link-arg=a2 -l dylib=d1 -l link-arg=a3 --print link-args | $(CGREP) -e 'l1.*-Wl,a1.*l2.*-Wl,a2.*d1.*-Wl,a3'
7+
$(RUSTC) empty.rs -Z unstable-options -C linker-flavor=gnu-cc -l static=l1 -l link-arg:+verbatim=a1 -l static=l2 -l link-arg=a2 -l dylib=d1 -l link-arg=a3 --print link-args | $(CGREP) -e 'l1.*"a1".*l2.*-Wl,a2.*d1.*-Wl,a3'
8+
$(RUSTC) empty.rs -Z unstable-options -C linker-flavor=ld -l static=l1 -l link-arg=a1 -l static=l2 -l link-arg=a2 -l dylib=d1 -l link-arg=a3 --print link-args | $(CGREP) -e 'l1.*"a1".*l2.*"a2".*d1.*"a3"'
9+
$(RUSTC) attribute.rs -Z unstable-options -C linker-flavor=gnu-cc --print link-args | $(CGREP) -e 'l1.*-Wl,a1.*l2.*-Wl,a2.*d1.*-Wl,a3'
10+
$(RUSTC) --cfg 'feature="verbatim"' attribute.rs -Z unstable-options -C linker-flavor=gnu-cc --print link-args | $(CGREP) -e 'l1.*"a1".*l2.*-Wl,a2.*d1.*-Wl,a3'
11+
$(RUSTC) attribute.rs -C linker-flavor=ld --print link-args | $(CGREP) -e 'l1.*"a1".*l2.*"a2".*d1.*"a3"'
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
#![feature(link_arg_attribute)]
2+
3+
#[link(kind = "static", name = "l1")]
4+
#[cfg_attr(feature = "verbatim", link(kind = "link-arg", name = "a1", modifiers = "+verbatim"))]
5+
#[cfg_attr(not(feature = "verbatim"), link(kind = "link-arg", name = "a1"))]
6+
#[link(kind = "static", name = "l2")]
7+
#[link(kind = "link-arg", name = "a2")]
8+
#[link(kind = "dylib", name = "d1")]
9+
#[link(kind = "link-arg", name = "a3")]
10+
extern "C" {}
11+
12+
fn main() {}

tests/run-make/pass-linker-flags-from-dep/Makefile

+4-2
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,9 @@ all:
44
# Build deps
55
$(RUSTC) native_dep_1.rs --crate-type=staticlib
66
$(RUSTC) native_dep_2.rs --crate-type=staticlib
7-
$(RUSTC) rust_dep.rs -l static:-bundle=native_dep_1 -l link-arg=some_flag -l static:-bundle=native_dep_2 --crate-type=lib -Z unstable-options
7+
$(RUSTC) rust_dep_flag.rs -l static:-bundle=native_dep_1 -l link-arg=some_flag -l static:-bundle=native_dep_2 --crate-type=lib -Z unstable-options
8+
$(RUSTC) rust_dep_attr.rs --crate-type=lib
89

910
# Check sequence of linker args
10-
$(RUSTC) main.rs --extern lib=$(TMPDIR)/librust_dep.rlib --crate-type=bin --print link-args | $(CGREP) -e 'native_dep_1.*some_flag.*native_dep_2'
11+
$(RUSTC) main.rs --extern lib=$(TMPDIR)/librust_dep_flag.rlib --crate-type=bin --print link-args | $(CGREP) -e 'native_dep_1.*some_flag.*native_dep_2'
12+
$(RUSTC) main.rs --extern lib=$(TMPDIR)/librust_dep_attr.rlib --crate-type=bin --print link-args | $(CGREP) -e 'native_dep_1.*some_flag.*native_dep_2'
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
#![feature(link_arg_attribute)]
2+
3+
#[link(kind = "static", name = "native_dep_1", modifiers = "-bundle")]
4+
#[link(kind = "link-arg", name = "some_flag")]
5+
#[link(kind = "static", name = "native_dep_2", modifiers = "-bundle")]
6+
extern "C" {
7+
pub fn foo();
8+
}
9+
10+
pub fn f() {
11+
unsafe {
12+
foo();
13+
}
14+
}
+2-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
include ../tools.mk
22

33
all:
4-
$(RUSTC) rs.rs -Z unstable-options -l static=l1 -l link-arg=a1 -l static=l2 -l link-arg=a2 -l dylib=d1 -l link-arg=a3 --print link-args | $(CGREP) -e 'l1.*a1.*l2.*a2.*d1.*a3'
4+
$(RUSTC) empty.rs -Z unstable-options -l static=l1 -l link-arg=a1 -l static=l2 -l link-arg=a2 -l dylib=d1 -l link-arg=a3 --print link-args | $(CGREP) -e 'l1.*a1.*l2.*a2.*d1.*a3'
5+
$(RUSTC) attribute.rs --print link-args | $(CGREP) -e 'l1.*a1.*l2.*a2.*d1.*a3'
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
#![feature(link_arg_attribute)]
2+
3+
#[link(kind = "static", name = "l1")]
4+
#[link(kind = "link-arg", name = "a1")]
5+
#[link(kind = "static", name = "l2")]
6+
#[link(kind = "link-arg", name = "a2")]
7+
#[link(kind = "dylib", name = "d1")]
8+
#[link(kind = "link-arg", name = "a3")]
9+
extern "C" {}
10+
11+
fn main() {}

tests/ui/error-codes/E0458.stderr

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
error[E0458]: unknown link kind `wonderful_unicorn`, expected one of: static, dylib, framework, raw-dylib
1+
error[E0458]: unknown link kind `wonderful_unicorn`, expected one of: static, dylib, framework, raw-dylib, link-arg
22
--> $DIR/E0458.rs:1:15
33
|
44
LL | #[link(kind = "wonderful_unicorn")] extern "C" {}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
#[link(kind = "link-arg", name = "foo")]
2+
//~^ ERROR link kind `link-arg` is unstable
3+
extern "C" {}
4+
5+
fn main() {}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
error[E0658]: link kind `link-arg` is unstable
2+
--> $DIR/feature-gate-link-arg-attribute.rs:1:15
3+
|
4+
LL | #[link(kind = "link-arg", name = "foo")]
5+
| ^^^^^^^^^^
6+
|
7+
= note: see issue #99427 <https://github.com/rust-lang/rust/issues/99427> for more information
8+
= help: add `#![feature(link_arg_attribute)]` to the crate attributes to enable
9+
10+
error: aborting due to 1 previous error
11+
12+
For more information about this error, try `rustc --explain E0658`.
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,7 @@
1-
// link-arg is not supposed to be usable in #[link] attributes
1+
#![feature(link_arg_attribute)]
22

3-
// compile-flags:
4-
// error-pattern: error[E0458]: unknown link kind `link-arg`, expected one of: static, dylib, framework, raw-dylib
5-
6-
#[link(kind = "link-arg")]
3+
#[link(kind = "link-arg", name = "arg", modifiers = "+bundle")]
4+
//~^ ERROR linking modifier `bundle` is only compatible with `static` linking kind
75
extern "C" {}
6+
87
pub fn main() {}
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,8 @@
1-
error[E0458]: unknown link kind `link-arg`, expected one of: static, dylib, framework, raw-dylib
2-
--> $DIR/link-arg-from-rs.rs:6:15
1+
error: linking modifier `bundle` is only compatible with `static` linking kind
2+
--> $DIR/link-arg-from-rs.rs:3:53
33
|
4-
LL | #[link(kind = "link-arg")]
5-
| ^^^^^^^^^^ unknown link kind
4+
LL | #[link(kind = "link-arg", name = "arg", modifiers = "+bundle")]
5+
| ^^^^^^^^^
66

7-
error[E0459]: `#[link]` attribute requires a `name = "string"` argument
8-
--> $DIR/link-arg-from-rs.rs:6:1
9-
|
10-
LL | #[link(kind = "link-arg")]
11-
| ^^^^^^^^^^^^^^^^^^^^^^^^^^ missing `name` argument
12-
13-
error: aborting due to 2 previous errors
7+
error: aborting due to 1 previous error
148

15-
Some errors have detailed explanations: E0458, E0459.
16-
For more information about an error, try `rustc --explain E0458`.

0 commit comments

Comments
 (0)