Skip to content

Commit c127648

Browse files
Rollup merge of rust-lang#118644 - madsmtm:macos-weak-linking-test, r=b-naber
Add test for Apple's `-weak_framework` linker argument The [`-weak_framework`](https://developer.apple.com/library/archive/documentation/MacOSX/Conceptual/BPFrameworks/Concepts/WeakLinking.html) linker argument can sometimes be useful to reduce startup times, and to link newer frameworks while still having older deployment targets. So I made a test to ensure that it continues to work. Discussed in rust-lang#99427.
2 parents 02ad667 + d63d1a1 commit c127648

10 files changed

+83
-21
lines changed
+23
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
# only-macos
2+
#
3+
# Check that linking to a framework actually makes it to the linker.
4+
5+
include ../tools.mk
6+
7+
all:
8+
$(RUSTC) dep-link-framework.rs
9+
$(RUSTC) dep-link-weak-framework.rs
10+
11+
$(RUSTC) empty.rs
12+
otool -L $(TMPDIR)/no-link | $(CGREP) -v CoreFoundation
13+
14+
$(RUSTC) link-framework.rs
15+
otool -L $(TMPDIR)/link-framework | $(CGREP) CoreFoundation | $(CGREP) -v weak
16+
17+
$(RUSTC) link-weak-framework.rs
18+
otool -L $(TMPDIR)/link-weak-framework | $(CGREP) CoreFoundation | $(CGREP) weak
19+
20+
# When linking the framework both normally, and weakly, the weak linking takes preference
21+
22+
$(RUSTC) link-both.rs
23+
otool -L $(TMPDIR)/link-both | $(CGREP) CoreFoundation | $(CGREP) weak
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
#![crate_type = "rlib"]
2+
3+
#[link(name = "CoreFoundation", kind = "framework")]
4+
extern "C" {}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
#![crate_type = "rlib"]
2+
#![feature(link_arg_attribute)]
3+
4+
#[link(name = "-weak_framework", kind = "link-arg", modifiers = "+verbatim")]
5+
#[link(name = "CoreFoundation", kind = "link-arg", modifiers = "+verbatim")]
6+
extern "C" {}
+1
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
fn main() {}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
extern crate dep_link_framework;
2+
extern crate dep_link_weak_framework;
3+
4+
fn main() {}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
extern crate dep_link_framework;
2+
3+
fn main() {}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
extern crate dep_link_weak_framework;
2+
3+
fn main() {}

tests/run-pass-valgrind/osx-frameworks.rs

-21
This file was deleted.
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
error: linking with `cc` failed: exit status: 1
2+
|
3+
= note: [linker command]
4+
= note: ld: warning: search path '$TEST_BUILD_DIR/linkage-attr/framework.omit/auxiliary' not found
5+
ld: Undefined symbols:
6+
_CFRunLoopGetTypeID, referenced from:
7+
framework::main::HASH in framework.framework.HASH-cgu.0.rcgu.o
8+
clang: error: linker command failed with exit code 1 (use -v to see invocation)
9+
10+
11+
error: aborting due to 1 previous error
12+

tests/ui/linkage-attr/framework.rs

+27
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
// Check that linking frameworks on Apple platforms works.
2+
// only-macos
3+
// revisions: omit link weak both
4+
// [omit]build-fail
5+
// [link]run-pass
6+
// [weak]run-pass
7+
// [both]run-pass
8+
// normalize-stderr-test: "note: env .*" -> "note: [linker command]"
9+
// normalize-stderr-test: "framework::main::\w* in framework\.framework\.\w*-cgu\.0\.rcgu\.o" -> "framework::main::HASH in framework.framework.HASH-cgu.0.rcgu.o"
10+
11+
#![cfg_attr(any(weak, both), feature(link_arg_attribute))]
12+
13+
#[cfg_attr(any(link, both), link(name = "CoreFoundation", kind = "framework"))]
14+
#[cfg_attr(
15+
any(weak, both),
16+
link(name = "-weak_framework", kind = "link-arg", modifiers = "+verbatim"),
17+
link(name = "CoreFoundation", kind = "link-arg", modifiers = "+verbatim")
18+
)]
19+
extern "C" {
20+
fn CFRunLoopGetTypeID() -> core::ffi::c_ulong;
21+
}
22+
23+
pub fn main() {
24+
unsafe {
25+
CFRunLoopGetTypeID();
26+
}
27+
}

0 commit comments

Comments
 (0)