66
77/// Link a Swift library.
88#[ cfg( target_os = "macos" ) ]
9- pub fn link_swift_library ( name : & str , source : impl AsRef < std:: path:: Path > ) {
9+ pub fn link_apple_library ( name : & str , source : impl AsRef < std:: path:: Path > ) {
10+ if source. as_ref ( ) . join ( "Package.swift" ) . exists ( ) {
11+ link_swift_library ( name, source) ;
12+ } else {
13+ link_xcode_library ( name, source) ;
14+ }
15+ }
16+
17+ /// Link a Swift library.
18+ #[ cfg( target_os = "macos" ) ]
19+ fn link_swift_library ( name : & str , source : impl AsRef < std:: path:: Path > ) {
1020 let source = source. as_ref ( ) ;
1121
1222 let sdk_root = std:: env:: var_os ( "SDKROOT" ) ;
@@ -23,3 +33,59 @@ pub fn link_swift_library(name: &str, source: impl AsRef<std::path::Path>) {
2333 std:: env:: set_var ( "SDKROOT" , root) ;
2434 }
2535}
36+
37+ /// Link a Xcode library.
38+ #[ cfg( target_os = "macos" ) ]
39+ fn link_xcode_library ( name : & str , source : impl AsRef < std:: path:: Path > ) {
40+ use std:: { path:: PathBuf , process:: Command } ;
41+
42+ let source = source. as_ref ( ) ;
43+ let configuration = if std:: env:: var ( "DEBUG" )
44+ . map ( |v| v == "true" )
45+ . unwrap_or_default ( )
46+ {
47+ "Debug"
48+ } else {
49+ "Release"
50+ } ;
51+
52+ let ( sdk, arch) = match std:: env:: var ( "TARGET" ) . unwrap ( ) . as_str ( ) {
53+ "aarch64-apple-ios" => ( "iphoneos" , "arm64" ) ,
54+ "aarch64-apple-ios-sim" => ( "iphonesimulator" , "arm64" ) ,
55+ "x86_64-apple-ios" => ( "iphonesimulator" , "x86_64" ) ,
56+ _ => return ,
57+ } ;
58+
59+ let out_dir = std:: env:: var_os ( "OUT_DIR" ) . map ( PathBuf :: from) . unwrap ( ) ;
60+ let derived_data_path = out_dir. join ( format ! ( "derivedData-{name}" ) ) ;
61+
62+ let status = Command :: new ( "xcodebuild" )
63+ . arg ( "build" )
64+ . arg ( "-scheme" )
65+ . arg ( name)
66+ . arg ( "-configuration" )
67+ . arg ( configuration)
68+ . arg ( "-sdk" )
69+ . arg ( sdk)
70+ . arg ( "-arch" )
71+ . arg ( arch)
72+ . arg ( "-derivedDataPath" )
73+ . arg ( & derived_data_path)
74+ . arg ( "BUILD_LIBRARY_FOR_DISTRIBUTION=YES" )
75+ . arg ( "OTHER_SWIFT_FLAGS=-no-verify-emitted-module-interface" )
76+ . current_dir ( source)
77+ . env_clear ( )
78+ . status ( )
79+ . unwrap ( ) ;
80+
81+ assert ! ( status. success( ) ) ;
82+
83+ let lib_out_dir = derived_data_path
84+ . join ( "Build" )
85+ . join ( "Products" )
86+ . join ( format ! ( "{configuration}-{sdk}" ) ) ;
87+
88+ println ! ( "cargo:rerun-if-changed={}" , source. display( ) ) ;
89+ println ! ( "cargo:rustc-link-search=native={}" , lib_out_dir. display( ) ) ;
90+ println ! ( "cargo:rustc-link-lib=static={name}" ) ;
91+ }
0 commit comments