Skip to content

Commit

Permalink
Auto merge of rust-lang#99467 - BelovDV:add_option_link_arg, r=petroc…
Browse files Browse the repository at this point in the history
…henkov

flag '-l link-arg=___ was added

rust-lang#99427
  • Loading branch information
bors committed Jul 29, 2022
2 parents 9fa62f2 + 7d4a98e commit 5dda74a
Show file tree
Hide file tree
Showing 22 changed files with 115 additions and 20 deletions.
38 changes: 29 additions & 9 deletions compiler/rustc_codegen_ssa/src/back/link.rs
Original file line number Diff line number Diff line change
Expand Up @@ -336,6 +336,7 @@ fn link_rlib<'a, B: ArchiveBuilder<'a>>(
| NativeLibKind::Dylib { .. }
| NativeLibKind::Framework { .. }
| NativeLibKind::RawDylib
| NativeLibKind::LinkArg
| NativeLibKind::Unspecified => continue,
}
if let Some(name) = lib.name {
Expand Down Expand Up @@ -1289,6 +1290,7 @@ fn print_native_static_libs(sess: &Session, all_native_libs: &[NativeLib]) {
}
// These are included, no need to print them
NativeLibKind::Static { bundle: None | Some(true), .. }
| NativeLibKind::LinkArg
| NativeLibKind::RawDylib => None,
}
})
Expand Down Expand Up @@ -2243,6 +2245,9 @@ fn add_local_native_libraries(
NativeLibKind::RawDylib => {
// Ignore RawDylib here, they are handled separately in linker_with_args().
}
NativeLibKind::LinkArg => {
cmd.arg(name);
}
}
}
}
Expand Down Expand Up @@ -2380,19 +2385,34 @@ fn add_upstream_rust_crates<'a, B: ArchiveBuilder<'a>>(
(lib.name, lib.kind, lib.verbatim)
};

if let NativeLibKind::Static { bundle: Some(false), whole_archive } =
lib.kind
{
let verbatim = lib.verbatim.unwrap_or(false);
if whole_archive == Some(true) {
match lib.kind {
NativeLibKind::Static {
bundle: Some(false),
whole_archive: Some(true),
} => {
cmd.link_whole_staticlib(
name,
verbatim,
lib.verbatim.unwrap_or(false),
search_path.get_or_init(|| archive_search_paths(sess)),
);
} else {
cmd.link_staticlib(name, verbatim);
}
NativeLibKind::Static {
bundle: Some(false),
whole_archive: Some(false) | None,
} => {
cmd.link_staticlib(name, lib.verbatim.unwrap_or(false));
}
NativeLibKind::LinkArg => {
cmd.arg(name);
}
NativeLibKind::Dylib { .. }
| NativeLibKind::Framework { .. }
| NativeLibKind::Unspecified
| NativeLibKind::RawDylib => {}
NativeLibKind::Static {
bundle: Some(true) | None,
whole_archive: _,
} => {}
}
}
}
Expand Down Expand Up @@ -2583,7 +2603,7 @@ fn add_upstream_native_libraries(
// already included them in add_local_native_libraries and
// add_upstream_rust_crates
NativeLibKind::Static { .. } => {}
NativeLibKind::RawDylib => {}
NativeLibKind::RawDylib | NativeLibKind::LinkArg => {}
}
}
}
Expand Down
17 changes: 15 additions & 2 deletions compiler/rustc_session/src/config.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1944,9 +1944,22 @@ fn parse_native_lib_kind(
"static" => NativeLibKind::Static { bundle: None, whole_archive: None },
"dylib" => NativeLibKind::Dylib { as_needed: None },
"framework" => NativeLibKind::Framework { as_needed: None },
"link-arg" => {
if !nightly_options::is_unstable_enabled(matches) {
let why = if nightly_options::match_is_nightly_build(matches) {
" and only accepted on the nightly compiler"
} else {
", the `-Z unstable-options` flag must also be passed to use it"
};
early_error(error_format, &format!("library kind `link-arg` is unstable{why}"))
}
NativeLibKind::LinkArg
}
_ => early_error(
error_format,
&format!("unknown library kind `{kind}`, expected one of: static, dylib, framework"),
&format!(
"unknown library kind `{kind}`, expected one of: static, dylib, framework, link-arg"
),
),
};
match modifiers {
Expand Down Expand Up @@ -2045,7 +2058,7 @@ fn parse_libs(matches: &getopts::Matches, error_format: ErrorOutputType) -> Vec<
.into_iter()
.map(|s| {
// Parse string of the form "[KIND[:MODIFIERS]=]lib[:new_name]",
// where KIND is one of "dylib", "framework", "static" and
// where KIND is one of "dylib", "framework", "static", "link-arg" and
// where MODIFIERS are a comma separated list of supported modifiers
// (bundle, verbatim, whole-archive, as-needed). Each modifier is prefixed
// with either + or - to indicate whether it is enabled or disabled.
Expand Down
5 changes: 4 additions & 1 deletion compiler/rustc_session/src/utils.rs
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,9 @@ pub enum NativeLibKind {
/// Whether the framework will be linked only if it satisfies some undefined symbols
as_needed: Option<bool>,
},
/// Argument which is passed to linker, relative order with libraries and other arguments
/// is preserved
LinkArg,
/// The library kind wasn't specified, `Dylib` is currently used as a default.
Unspecified,
}
Expand All @@ -47,7 +50,7 @@ impl NativeLibKind {
NativeLibKind::Dylib { as_needed } | NativeLibKind::Framework { as_needed } => {
as_needed.is_some()
}
NativeLibKind::RawDylib | NativeLibKind::Unspecified => false,
NativeLibKind::RawDylib | NativeLibKind::Unspecified | NativeLibKind::LinkArg => false,
}
}
}
Expand Down
10 changes: 10 additions & 0 deletions src/test/run-make/pass-linker-flags-from-dep/Makefile
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
-include ../../run-make-fulldeps/tools.mk

all:
# Build deps
$(RUSTC) native_dep_1.rs --crate-type=staticlib
$(RUSTC) native_dep_2.rs --crate-type=staticlib
$(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

# Check sequence of linker args
$(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'
3 changes: 3 additions & 0 deletions src/test/run-make/pass-linker-flags-from-dep/main.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
fn main() {
lib::f();
}
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
pub fn f1() {}
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
pub fn f2() {}
9 changes: 9 additions & 0 deletions src/test/run-make/pass-linker-flags-from-dep/rust_dep.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
extern "C" {
pub fn foo();
}

pub fn f() {
unsafe {
foo();
}
}
4 changes: 4 additions & 0 deletions src/test/run-make/pass-linker-flags/Makefile
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
-include ../../run-make-fulldeps/tools.mk

all:
$(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'
1 change: 1 addition & 0 deletions src/test/run-make/pass-linker-flags/rs.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
fn main() {}
2 changes: 1 addition & 1 deletion src/test/ui/manual/manual-link-bad-kind.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
// compile-flags:-l bar=foo
// error-pattern: unknown library kind `bar`, expected one of: static, dylib, framework
// error-pattern: unknown library kind `bar`, expected one of: static, dylib, framework, link-arg

fn main() {
}
2 changes: 1 addition & 1 deletion src/test/ui/manual/manual-link-bad-kind.stderr
Original file line number Diff line number Diff line change
@@ -1,2 +1,2 @@
error: unknown library kind `bar`, expected one of: static, dylib, framework
error: unknown library kind `bar`, expected one of: static, dylib, framework, link-arg

2 changes: 1 addition & 1 deletion src/test/ui/manual/manual-link-unsupported-kind.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
// compile-flags:-l raw-dylib=foo
// error-pattern: unknown library kind `raw-dylib`, expected one of: static, dylib, framework
// error-pattern: unknown library kind `raw-dylib`, expected one of: static, dylib, framework, link-arg

fn main() {
}
2 changes: 1 addition & 1 deletion src/test/ui/manual/manual-link-unsupported-kind.stderr
Original file line number Diff line number Diff line change
@@ -1,2 +1,2 @@
error: unknown library kind `raw-dylib`, expected one of: static, dylib, framework
error: unknown library kind `raw-dylib`, expected one of: static, dylib, framework, link-arg

2 changes: 1 addition & 1 deletion src/test/ui/native-library-link-flags/empty-kind-1.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
// Unspecified kind should fail with an error

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

fn main() {}
2 changes: 1 addition & 1 deletion src/test/ui/native-library-link-flags/empty-kind-1.stderr
Original file line number Diff line number Diff line change
@@ -1,2 +1,2 @@
error: unknown library kind ``, expected one of: static, dylib, framework
error: unknown library kind ``, expected one of: static, dylib, framework, link-arg

2 changes: 1 addition & 1 deletion src/test/ui/native-library-link-flags/empty-kind-2.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
// Unspecified kind should fail with an error

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

fn main() {}
2 changes: 1 addition & 1 deletion src/test/ui/native-library-link-flags/empty-kind-2.stderr
Original file line number Diff line number Diff line change
@@ -1,2 +1,2 @@
error: unknown library kind ``, expected one of: static, dylib, framework
error: unknown library kind ``, expected one of: static, dylib, framework, link-arg

4 changes: 4 additions & 0 deletions src/test/ui/native-library-link-flags/link-arg-error.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
// compile-flags: -l link-arg:+bundle=arg -Z unstable-options
// error-pattern: linking modifier `bundle` is only compatible with `static` linking kind

fn main() {}
2 changes: 2 additions & 0 deletions src/test/ui/native-library-link-flags/link-arg-error.stderr
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
error: linking modifier `bundle` is only compatible with `static` linking kind

8 changes: 8 additions & 0 deletions src/test/ui/native-library-link-flags/link-arg-from-rs.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
// link-arg is not supposed to be usable in #[link] attributes

// compile-flags:
// error-pattern: error[E0458]: unknown link kind `link-arg`, expected one of: static, dylib, framework, raw-dylib

#[link(kind = "link-arg")]
extern "C" {}
pub fn main() {}
16 changes: 16 additions & 0 deletions src/test/ui/native-library-link-flags/link-arg-from-rs.stderr
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
error[E0458]: unknown link kind `link-arg`, expected one of: static, dylib, framework, raw-dylib
--> $DIR/link-arg-from-rs.rs:6:15
|
LL | #[link(kind = "link-arg")]
| ^^^^^^^^^^ unknown link kind

error[E0459]: `#[link]` attribute requires a `name = "string"` argument
--> $DIR/link-arg-from-rs.rs:6:1
|
LL | #[link(kind = "link-arg")]
| ^^^^^^^^^^^^^^^^^^^^^^^^^^ missing `name` argument

error: aborting due to 2 previous errors

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

0 comments on commit 5dda74a

Please sign in to comment.