Skip to content

Commit

Permalink
Rollup merge of rust-lang#118644 - madsmtm:macos-weak-linking-test, r…
Browse files Browse the repository at this point in the history
…=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.
  • Loading branch information
GuillaumeGomez authored Dec 16, 2023
2 parents 02ad667 + d63d1a1 commit c127648
Show file tree
Hide file tree
Showing 10 changed files with 83 additions and 21 deletions.
23 changes: 23 additions & 0 deletions tests/run-make/link-framework/Makefile
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
# only-macos
#
# Check that linking to a framework actually makes it to the linker.

include ../tools.mk

all:
$(RUSTC) dep-link-framework.rs
$(RUSTC) dep-link-weak-framework.rs

$(RUSTC) empty.rs
otool -L $(TMPDIR)/no-link | $(CGREP) -v CoreFoundation

$(RUSTC) link-framework.rs
otool -L $(TMPDIR)/link-framework | $(CGREP) CoreFoundation | $(CGREP) -v weak

$(RUSTC) link-weak-framework.rs
otool -L $(TMPDIR)/link-weak-framework | $(CGREP) CoreFoundation | $(CGREP) weak

# When linking the framework both normally, and weakly, the weak linking takes preference

$(RUSTC) link-both.rs
otool -L $(TMPDIR)/link-both | $(CGREP) CoreFoundation | $(CGREP) weak
4 changes: 4 additions & 0 deletions tests/run-make/link-framework/dep-link-framework.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
#![crate_type = "rlib"]

#[link(name = "CoreFoundation", kind = "framework")]
extern "C" {}
6 changes: 6 additions & 0 deletions tests/run-make/link-framework/dep-link-weak-framework.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
#![crate_type = "rlib"]
#![feature(link_arg_attribute)]

#[link(name = "-weak_framework", kind = "link-arg", modifiers = "+verbatim")]
#[link(name = "CoreFoundation", kind = "link-arg", modifiers = "+verbatim")]
extern "C" {}
1 change: 1 addition & 0 deletions tests/run-make/link-framework/empty.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
fn main() {}
4 changes: 4 additions & 0 deletions tests/run-make/link-framework/link-both.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
extern crate dep_link_framework;
extern crate dep_link_weak_framework;

fn main() {}
3 changes: 3 additions & 0 deletions tests/run-make/link-framework/link-framework.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
extern crate dep_link_framework;

fn main() {}
3 changes: 3 additions & 0 deletions tests/run-make/link-framework/link-weak-framework.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
extern crate dep_link_weak_framework;

fn main() {}
21 changes: 0 additions & 21 deletions tests/run-pass-valgrind/osx-frameworks.rs

This file was deleted.

12 changes: 12 additions & 0 deletions tests/ui/linkage-attr/framework.omit.stderr
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
error: linking with `cc` failed: exit status: 1
|
= note: [linker command]
= note: ld: warning: search path '$TEST_BUILD_DIR/linkage-attr/framework.omit/auxiliary' not found
ld: Undefined symbols:
_CFRunLoopGetTypeID, referenced from:
framework::main::HASH in framework.framework.HASH-cgu.0.rcgu.o
clang: error: linker command failed with exit code 1 (use -v to see invocation)


error: aborting due to 1 previous error

27 changes: 27 additions & 0 deletions tests/ui/linkage-attr/framework.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
// Check that linking frameworks on Apple platforms works.
// only-macos
// revisions: omit link weak both
// [omit]build-fail
// [link]run-pass
// [weak]run-pass
// [both]run-pass
// normalize-stderr-test: "note: env .*" -> "note: [linker command]"
// 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"

#![cfg_attr(any(weak, both), feature(link_arg_attribute))]

#[cfg_attr(any(link, both), link(name = "CoreFoundation", kind = "framework"))]
#[cfg_attr(
any(weak, both),
link(name = "-weak_framework", kind = "link-arg", modifiers = "+verbatim"),
link(name = "CoreFoundation", kind = "link-arg", modifiers = "+verbatim")
)]
extern "C" {
fn CFRunLoopGetTypeID() -> core::ffi::c_ulong;
}

pub fn main() {
unsafe {
CFRunLoopGetTypeID();
}
}

0 comments on commit c127648

Please sign in to comment.