Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

flag '-l link-arg=___ was added #1

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 11 additions & 1 deletion 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 @@ -1287,6 +1288,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 @@ -2225,6 +2227,9 @@ fn add_local_native_libraries(
// FIXME(#58713): Proper handling for raw dylibs.
bug!("raw_dylib feature not yet implemented");
}
NativeLibKind::LinkArg => {
cmd.cmd().arg(name);
}
}
}
}
Expand Down Expand Up @@ -2376,6 +2381,11 @@ fn add_upstream_rust_crates<'a, B: ArchiveBuilder<'a>>(
cmd.link_staticlib(name, verbatim);
}
}
if NativeLibKind::LinkArg == lib.kind {
if let Some(name) = lib.name {
cmd.cmd().arg(name.as_str());
}
}
}
}
}
Expand Down Expand Up @@ -2565,7 +2575,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
5 changes: 4 additions & 1 deletion compiler/rustc_session/src/config.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1942,9 +1942,12 @@ 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" => 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
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
12 changes: 12 additions & 0 deletions src/test/run-make/pass-linker-flags-upwork/Makefile
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
-include ../../run-make-fulldeps/tools.mk

flag_1 = some_flag

all:
# Build deps
$(RUSTC) dep_1.rs --crate-type=staticlib
$(RUSTC) dep_2.rs --crate-type=staticlib
$(RUSTC) lib.rs -l static:-bundle=dep_1 -llink-arg=${flag_1} -l static:-bundle=dep_2 --crate-type=lib

# Check sequence of linker args
$(RUSTC) main.rs --extern lib=$(TMPDIR)/liblib.rlib --crate-type=bin --print link-args | $(CGREP) -e '"-ldep_1" "some_flag" "-ldep_2"'
1 change: 1 addition & 0 deletions src/test/run-make/pass-linker-flags-upwork/dep_1.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
pub fn f1() {}
1 change: 1 addition & 0 deletions src/test/run-make/pass-linker-flags-upwork/dep_2.rs
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-upwork/lib.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();
}
}
3 changes: 3 additions & 0 deletions src/test/run-make/pass-linker-flags-upwork/main.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
fn main() {
lib::f();
}
6 changes: 6 additions & 0 deletions src/test/run-make/pass-linker-flags/Makefile
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
-include ../../run-make-fulldeps/tools.mk

# TODO: test it without makefile

all:
$(RUSTC) rs.rs -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 '"-ll1" "a1" "-ll2" "a2" "-Wl,-Bdynamic" "-ld1" "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

6 changes: 6 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,6 @@
// Unexpected linker modifier should fail with an error

// compile-flags: -l link-arg:+bundle=arg
// 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