Skip to content

Commit 23fb079

Browse files
committed
Auto merge of rust-lang#118644 - madsmtm:macos-weak-linking-test, r=compiler-errors
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 d31b6fb + 47cab49 commit 23fb079

File tree

11 files changed

+83
-21
lines changed

11 files changed

+83
-21
lines changed

src/tools/compiletest/src/header.rs

+1
Original file line numberDiff line numberDiff line change
@@ -693,6 +693,7 @@ const KNOWN_DIRECTIVE_NAMES: &[&str] = &[
693693
"check-run-results",
694694
"check-stdout",
695695
"check-test-line-numbers-match",
696+
"compare-output-lines-by-subset",
696697
"compile-flags",
697698
"dont-check-compiler-stderr",
698699
"dont-check-compiler-stdout",
+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,8 @@
1+
error: linking with `cc` failed: exit status: 1
2+
|
3+
ld: Undefined symbols:
4+
_CFRunLoopGetTypeID, referenced from:
5+
clang: error: linker command failed with exit code 1 (use -v to see invocation)
6+
7+
8+
error: aborting due to 1 previous error

tests/ui/linkage-attr/framework.rs

+30
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
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+
9+
// The linker's exact error output changes between Xcode versions.
10+
//@ compare-output-lines-by-subset
11+
//@ normalize-stderr-test: "Undefined symbols for architecture arm64:" "ld: Undefined symbols:"
12+
//@ normalize-stderr-test: "._CFRunLoopGetTypeID.," "_CFRunLoopGetTypeID,"
13+
14+
#![cfg_attr(any(weak, both), feature(link_arg_attribute))]
15+
16+
#[cfg_attr(any(link, both), link(name = "CoreFoundation", kind = "framework"))]
17+
#[cfg_attr(
18+
any(weak, both),
19+
link(name = "-weak_framework", kind = "link-arg", modifiers = "+verbatim"),
20+
link(name = "CoreFoundation", kind = "link-arg", modifiers = "+verbatim")
21+
)]
22+
extern "C" {
23+
fn CFRunLoopGetTypeID() -> core::ffi::c_ulong;
24+
}
25+
26+
pub fn main() {
27+
unsafe {
28+
CFRunLoopGetTypeID();
29+
}
30+
}

0 commit comments

Comments
 (0)