Skip to content

Commit f766077

Browse files
committed
add test + small fix
1 parent 42a200d commit f766077

File tree

7 files changed

+76
-14
lines changed

7 files changed

+76
-14
lines changed

compiler/rustc_codegen_ssa/src/back/link.rs

Lines changed: 16 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -2391,21 +2391,23 @@ fn add_upstream_rust_crates<'a, B: ArchiveBuilder<'a>>(
23912391
} else {
23922392
cmd.link_rlib(&bundle_lib);
23932393
}
2394+
2395+
continue;
23942396
}
2395-
} else {
2396-
if let NativeLibKind::Static { bundle: Some(false), whole_archive } =
2397-
lib.kind
2398-
{
2399-
let verbatim = lib.verbatim.unwrap_or(false);
2400-
if whole_archive == Some(true) {
2401-
cmd.link_whole_staticlib(
2402-
name,
2403-
verbatim,
2404-
search_path.get_or_init(|| archive_search_paths(sess)),
2405-
);
2406-
} else {
2407-
cmd.link_staticlib(name, verbatim);
2408-
}
2397+
}
2398+
2399+
if let NativeLibKind::Static { bundle: Some(false), whole_archive } =
2400+
lib.kind
2401+
{
2402+
let verbatim = lib.verbatim.unwrap_or(false);
2403+
if whole_archive == Some(true) {
2404+
cmd.link_whole_staticlib(
2405+
name,
2406+
verbatim,
2407+
search_path.get_or_init(|| archive_search_paths(sess)),
2408+
);
2409+
} else {
2410+
cmd.link_staticlib(name, verbatim);
24092411
}
24102412
}
24112413
}
Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
# ignore-cross-compile
2+
# ignore-windows-msvc
3+
4+
-include ../../run-make-fulldeps/tools.mk
5+
6+
# We're using the llvm-nm instead of the system nm to ensure it is compatible
7+
# with the LLVM bitcode generated by rustc.
8+
NM = "$(LLVM_BIN_DIR)"/llvm-nm
9+
SPLIT = "-Zsplit-bundled-libs"
10+
BUNDLED_LIB = "libbundled.rlib.bundle.native-staticlib.a"
11+
12+
all: $(call NATIVE_STATICLIB,native-staticlib)
13+
# Build a staticlib and a rlib, the `native_func` symbol will be bundled only into staticlib
14+
$(RUSTC) bundled.rs --crate-type=staticlib --crate-type=rlib $(SPLIT)
15+
$(NM) $(TMPDIR)/libbundled.a | $(CGREP) -e "T _*native_func"
16+
$(NM) $(TMPDIR)/libbundled.a | $(CGREP) -e "U _*native_func"
17+
$(NM) $(TMPDIR)/libbundled.rlib | $(CGREP) -ve "T _*native_func"
18+
$(NM) $(TMPDIR)/libbundled.rlib | $(CGREP) -e "U _*native_func"
19+
20+
# Build a staticlib and a rlib, the `native_func` symbol will not be bundled into it
21+
$(RUSTC) non-bundled.rs --crate-type=staticlib --crate-type=rlib $(SPLIT)
22+
$(NM) $(TMPDIR)/libnon_bundled.a | $(CGREP) -ve "T _*native_func"
23+
$(NM) $(TMPDIR)/libnon_bundled.a | $(CGREP) -e "U _*native_func"
24+
$(NM) $(TMPDIR)/libnon_bundled.rlib | $(CGREP) -ve "T _*native_func"
25+
$(NM) $(TMPDIR)/libnon_bundled.rlib | $(CGREP) -e "U _*native_func"
26+
27+
# Build a cdylib, 'BUNDLED_LIB' will appear on the linker line
28+
# The cdylib will contain the `native_func` symbol in the end
29+
$(RUSTC) cdylib-bundled.rs --crate-type=cdylib --print link-args $(SPLIT) | $(CGREP) -e $(BUNDLED_LIB)
30+
$(NM) $(call DYLIB,cdylib_bundled) | $(CGREP) -e "[Tt] _*native_func"
31+
32+
# Build a cdylib, `native-staticlib` will appear on the linker line because it was not bundled previously
33+
# The cdylib will contain the `native_func` symbol in the end
34+
$(RUSTC) cdylib-non-bundled.rs --crate-type=cdylib --print link-args $(SPLIT) > $(TMPDIR)/tmp
35+
$(NM) $(call DYLIB,cdylib_non_bundled) | $(CGREP) -e "[Tt] _*native_func"
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
#[link(name = "native-staticlib", kind = "static", modifiers = "+bundle")]
2+
extern "C" {
3+
pub fn native_func();
4+
}
5+
6+
#[no_mangle]
7+
pub extern "C" fn wrapped_func() {
8+
unsafe {
9+
native_func();
10+
}
11+
}
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
extern crate bundled;
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
extern crate non_bundled;
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
void native_func() {}
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
#[link(name = "native-staticlib", kind = "static", modifiers = "-bundle")]
2+
extern "C" {
3+
pub fn native_func();
4+
}
5+
6+
#[no_mangle]
7+
pub extern "C" fn wrapped_func() {
8+
unsafe {
9+
native_func();
10+
}
11+
}

0 commit comments

Comments
 (0)