Skip to content

Commit 5dda74a

Browse files
committed
Auto merge of rust-lang#99467 - BelovDV:add_option_link_arg, r=petrochenkov
flag '-l link-arg=___ was added rust-lang#99427
2 parents 9fa62f2 + 7d4a98e commit 5dda74a

22 files changed

+115
-20
lines changed

compiler/rustc_codegen_ssa/src/back/link.rs

+29-9
Original file line numberDiff line numberDiff line change
@@ -336,6 +336,7 @@ fn link_rlib<'a, B: ArchiveBuilder<'a>>(
336336
| NativeLibKind::Dylib { .. }
337337
| NativeLibKind::Framework { .. }
338338
| NativeLibKind::RawDylib
339+
| NativeLibKind::LinkArg
339340
| NativeLibKind::Unspecified => continue,
340341
}
341342
if let Some(name) = lib.name {
@@ -1289,6 +1290,7 @@ fn print_native_static_libs(sess: &Session, all_native_libs: &[NativeLib]) {
12891290
}
12901291
// These are included, no need to print them
12911292
NativeLibKind::Static { bundle: None | Some(true), .. }
1293+
| NativeLibKind::LinkArg
12921294
| NativeLibKind::RawDylib => None,
12931295
}
12941296
})
@@ -2243,6 +2245,9 @@ fn add_local_native_libraries(
22432245
NativeLibKind::RawDylib => {
22442246
// Ignore RawDylib here, they are handled separately in linker_with_args().
22452247
}
2248+
NativeLibKind::LinkArg => {
2249+
cmd.arg(name);
2250+
}
22462251
}
22472252
}
22482253
}
@@ -2380,19 +2385,34 @@ fn add_upstream_rust_crates<'a, B: ArchiveBuilder<'a>>(
23802385
(lib.name, lib.kind, lib.verbatim)
23812386
};
23822387

2383-
if let NativeLibKind::Static { bundle: Some(false), whole_archive } =
2384-
lib.kind
2385-
{
2386-
let verbatim = lib.verbatim.unwrap_or(false);
2387-
if whole_archive == Some(true) {
2388+
match lib.kind {
2389+
NativeLibKind::Static {
2390+
bundle: Some(false),
2391+
whole_archive: Some(true),
2392+
} => {
23882393
cmd.link_whole_staticlib(
23892394
name,
2390-
verbatim,
2395+
lib.verbatim.unwrap_or(false),
23912396
search_path.get_or_init(|| archive_search_paths(sess)),
23922397
);
2393-
} else {
2394-
cmd.link_staticlib(name, verbatim);
23952398
}
2399+
NativeLibKind::Static {
2400+
bundle: Some(false),
2401+
whole_archive: Some(false) | None,
2402+
} => {
2403+
cmd.link_staticlib(name, lib.verbatim.unwrap_or(false));
2404+
}
2405+
NativeLibKind::LinkArg => {
2406+
cmd.arg(name);
2407+
}
2408+
NativeLibKind::Dylib { .. }
2409+
| NativeLibKind::Framework { .. }
2410+
| NativeLibKind::Unspecified
2411+
| NativeLibKind::RawDylib => {}
2412+
NativeLibKind::Static {
2413+
bundle: Some(true) | None,
2414+
whole_archive: _,
2415+
} => {}
23962416
}
23972417
}
23982418
}
@@ -2583,7 +2603,7 @@ fn add_upstream_native_libraries(
25832603
// already included them in add_local_native_libraries and
25842604
// add_upstream_rust_crates
25852605
NativeLibKind::Static { .. } => {}
2586-
NativeLibKind::RawDylib => {}
2606+
NativeLibKind::RawDylib | NativeLibKind::LinkArg => {}
25872607
}
25882608
}
25892609
}

compiler/rustc_session/src/config.rs

+15-2
Original file line numberDiff line numberDiff line change
@@ -1944,9 +1944,22 @@ fn parse_native_lib_kind(
19441944
"static" => NativeLibKind::Static { bundle: None, whole_archive: None },
19451945
"dylib" => NativeLibKind::Dylib { as_needed: None },
19461946
"framework" => NativeLibKind::Framework { as_needed: None },
1947+
"link-arg" => {
1948+
if !nightly_options::is_unstable_enabled(matches) {
1949+
let why = if nightly_options::match_is_nightly_build(matches) {
1950+
" and only accepted on the nightly compiler"
1951+
} else {
1952+
", the `-Z unstable-options` flag must also be passed to use it"
1953+
};
1954+
early_error(error_format, &format!("library kind `link-arg` is unstable{why}"))
1955+
}
1956+
NativeLibKind::LinkArg
1957+
}
19471958
_ => early_error(
19481959
error_format,
1949-
&format!("unknown library kind `{kind}`, expected one of: static, dylib, framework"),
1960+
&format!(
1961+
"unknown library kind `{kind}`, expected one of: static, dylib, framework, link-arg"
1962+
),
19501963
),
19511964
};
19521965
match modifiers {
@@ -2045,7 +2058,7 @@ fn parse_libs(matches: &getopts::Matches, error_format: ErrorOutputType) -> Vec<
20452058
.into_iter()
20462059
.map(|s| {
20472060
// Parse string of the form "[KIND[:MODIFIERS]=]lib[:new_name]",
2048-
// where KIND is one of "dylib", "framework", "static" and
2061+
// where KIND is one of "dylib", "framework", "static", "link-arg" and
20492062
// where MODIFIERS are a comma separated list of supported modifiers
20502063
// (bundle, verbatim, whole-archive, as-needed). Each modifier is prefixed
20512064
// with either + or - to indicate whether it is enabled or disabled.

compiler/rustc_session/src/utils.rs

+4-1
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,9 @@ pub enum NativeLibKind {
3434
/// Whether the framework will be linked only if it satisfies some undefined symbols
3535
as_needed: Option<bool>,
3636
},
37+
/// Argument which is passed to linker, relative order with libraries and other arguments
38+
/// is preserved
39+
LinkArg,
3740
/// The library kind wasn't specified, `Dylib` is currently used as a default.
3841
Unspecified,
3942
}
@@ -47,7 +50,7 @@ impl NativeLibKind {
4750
NativeLibKind::Dylib { as_needed } | NativeLibKind::Framework { as_needed } => {
4851
as_needed.is_some()
4952
}
50-
NativeLibKind::RawDylib | NativeLibKind::Unspecified => false,
53+
NativeLibKind::RawDylib | NativeLibKind::Unspecified | NativeLibKind::LinkArg => false,
5154
}
5255
}
5356
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
-include ../../run-make-fulldeps/tools.mk
2+
3+
all:
4+
# Build deps
5+
$(RUSTC) native_dep_1.rs --crate-type=staticlib
6+
$(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
8+
9+
# 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'
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
fn main() {
2+
lib::f();
3+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
pub fn f1() {}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
pub fn f2() {}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
extern "C" {
2+
pub fn foo();
3+
}
4+
5+
pub fn f() {
6+
unsafe {
7+
foo();
8+
}
9+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
-include ../../run-make-fulldeps/tools.mk
2+
3+
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'
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
fn main() {}
+1-1
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
// compile-flags:-l bar=foo
2-
// error-pattern: unknown library kind `bar`, expected one of: static, dylib, framework
2+
// error-pattern: unknown library kind `bar`, expected one of: static, dylib, framework, link-arg
33

44
fn main() {
55
}
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,2 @@
1-
error: unknown library kind `bar`, expected one of: static, dylib, framework
1+
error: unknown library kind `bar`, expected one of: static, dylib, framework, link-arg
22

Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
// compile-flags:-l raw-dylib=foo
2-
// error-pattern: unknown library kind `raw-dylib`, expected one of: static, dylib, framework
2+
// error-pattern: unknown library kind `raw-dylib`, expected one of: static, dylib, framework, link-arg
33

44
fn main() {
55
}
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,2 @@
1-
error: unknown library kind `raw-dylib`, expected one of: static, dylib, framework
1+
error: unknown library kind `raw-dylib`, expected one of: static, dylib, framework, link-arg
22

Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
// Unspecified kind should fail with an error
22

33
// compile-flags: -l =mylib
4-
// error-pattern: unknown library kind ``, expected one of: static, dylib, framework
4+
// error-pattern: unknown library kind ``, expected one of: static, dylib, framework, link-arg
55

66
fn main() {}
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,2 @@
1-
error: unknown library kind ``, expected one of: static, dylib, framework
1+
error: unknown library kind ``, expected one of: static, dylib, framework, link-arg
22

Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
// Unspecified kind should fail with an error
22

33
// compile-flags: -l :+bundle=mylib
4-
// error-pattern: unknown library kind ``, expected one of: static, dylib, framework
4+
// error-pattern: unknown library kind ``, expected one of: static, dylib, framework, link-arg
55

66
fn main() {}
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,2 @@
1-
error: unknown library kind ``, expected one of: static, dylib, framework
1+
error: unknown library kind ``, expected one of: static, dylib, framework, link-arg
22

Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
// compile-flags: -l link-arg:+bundle=arg -Z unstable-options
2+
// error-pattern: linking modifier `bundle` is only compatible with `static` linking kind
3+
4+
fn main() {}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
error: linking modifier `bundle` is only compatible with `static` linking kind
2+
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
// link-arg is not supposed to be usable in #[link] attributes
2+
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")]
7+
extern "C" {}
8+
pub fn main() {}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
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
3+
|
4+
LL | #[link(kind = "link-arg")]
5+
| ^^^^^^^^^^ unknown link kind
6+
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
14+
15+
Some errors have detailed explanations: E0458, E0459.
16+
For more information about an error, try `rustc --explain E0458`.

0 commit comments

Comments
 (0)