@@ -3,23 +3,9 @@ import cp from "node:child_process";
33import fs from "node:fs" ;
44import path from "node:path" ;
55
6- import type { SupportedTriplet } from "./triplets.js" ;
76import { spawn } from "bufout" ;
87
9- export const APPLE_TRIPLETS = [
10- "arm64;x86_64-apple-darwin" ,
11- "x86_64-apple-darwin" ,
12- "arm64-apple-darwin" ,
13- "arm64-apple-ios" ,
14- "arm64-apple-ios-sim" ,
15- "arm64-apple-tvos" ,
16- "arm64-apple-tvos-sim" ,
17- // "x86_64-apple-tvos",
18- "arm64-apple-visionos" ,
19- "arm64-apple-visionos-sim" ,
20- ] as const ;
21-
22- export type AppleTriplet = ( typeof APPLE_TRIPLETS ) [ number ] ;
8+ import { AppleTriplet , isAppleTriplet } from "./triplets.js" ;
239
2410export const DEFAULT_APPLE_TRIPLETS = [
2511 "arm64;x86_64-apple-darwin" ,
@@ -41,7 +27,7 @@ type XcodeSDKName =
4127 | "appletvsimulator"
4228 | "macosx" ;
4329
44- const SDK_NAMES = {
30+ const XCODE_SDK_NAMES = {
4531 "x86_64-apple-darwin" : "macosx" ,
4632 "arm64-apple-darwin" : "macosx" ,
4733 "arm64;x86_64-apple-darwin" : "macosx" ,
@@ -54,9 +40,24 @@ const SDK_NAMES = {
5440 "arm64-apple-visionos-sim" : "xrsimulator" ,
5541} satisfies Record < AppleTriplet , XcodeSDKName > ;
5642
43+ type CMakeSystemName = "Darwin" | "iOS" | "tvOS" | "watchOS" | "visionOS" ;
44+
45+ const CMAKE_SYSTEM_NAMES = {
46+ "x86_64-apple-darwin" : "Darwin" ,
47+ "arm64-apple-darwin" : "Darwin" ,
48+ "arm64;x86_64-apple-darwin" : "Darwin" ,
49+ "arm64-apple-ios" : "iOS" ,
50+ "arm64-apple-ios-sim" : "iOS" ,
51+ "arm64-apple-tvos" : "tvOS" ,
52+ // "x86_64-apple-tvos": "appletvos",
53+ "arm64-apple-tvos-sim" : "tvOS" ,
54+ "arm64-apple-visionos" : "visionOS" ,
55+ "arm64-apple-visionos-sim" : "visionOS" ,
56+ } satisfies Record < AppleTriplet , CMakeSystemName > ;
57+
5758type AppleArchitecture = "arm64" | "x86_64" | "arm64;x86_64" ;
5859
59- export const ARCHITECTURES = {
60+ export const APPLE_ARCHITECTURES = {
6061 "x86_64-apple-darwin" : "x86_64" ,
6162 "arm64-apple-darwin" : "arm64" ,
6263 "arm64;x86_64-apple-darwin" : "arm64;x86_64" ,
@@ -69,17 +70,15 @@ export const ARCHITECTURES = {
6970 "arm64-apple-visionos-sim" : "arm64" ,
7071} satisfies Record < AppleTriplet , AppleArchitecture > ;
7172
72- export function isAppleTriplet (
73- triplet : SupportedTriplet
74- ) : triplet is AppleTriplet {
75- return APPLE_TRIPLETS . includes ( triplet as AppleTriplet ) ;
76- }
77-
7873export function getAppleSDKPath ( triplet : AppleTriplet ) {
7974 return cp
80- . spawnSync ( "xcrun" , [ "--sdk" , SDK_NAMES [ triplet ] , "--show-sdk-path" ] , {
81- encoding : "utf-8" ,
82- } )
75+ . spawnSync (
76+ "xcrun" ,
77+ [ "--sdk" , XCODE_SDK_NAMES [ triplet ] , "--show-sdk-path" ] ,
78+ {
79+ encoding : "utf-8" ,
80+ }
81+ )
8382 . stdout . trim ( ) ;
8483}
8584
@@ -98,23 +97,27 @@ export function createPlistContent(values: Record<string, string>) {
9897 ] . join ( "\n" ) ;
9998}
10099
101- export function getAppleConfigureCmakeArgs ( triplet : AppleTriplet ) {
100+ type AppleConfigureOptions = {
101+ triplet : AppleTriplet ;
102+ } ;
103+
104+ export function getAppleConfigureCmakeArgs ( { triplet } : AppleConfigureOptions ) {
102105 assert ( isAppleTriplet ( triplet ) ) ;
103106 const sdkPath = getAppleSDKPath ( triplet ) ;
107+ const systemName = CMAKE_SYSTEM_NAMES [ triplet ] ;
104108
105109 return [
106110 // Use the XCode as generator for Apple platforms
107111 "-G" ,
108112 "Xcode" ,
109- // Pass linker flags to avoid errors from undefined symbols
110113 "-D" ,
111- `CMAKE_SHARED_LINKER_FLAGS="-Wl,-undefined,dynamic_lookup" ` ,
114+ `CMAKE_SYSTEM_NAME= ${ systemName } ` ,
112115 // Set the SDK path for the target platform
113116 "-D" ,
114117 `CMAKE_OSX_SYSROOT=${ sdkPath } ` ,
115118 // Set the target architecture
116119 "-D" ,
117- `CMAKE_OSX_ARCHITECTURES=${ ARCHITECTURES [ triplet ] } ` ,
120+ `CMAKE_OSX_ARCHITECTURES=${ APPLE_ARCHITECTURES [ triplet ] } ` ,
118121 ] ;
119122}
120123
@@ -126,6 +129,7 @@ export function getAppleBuildArgs() {
126129type XCframeworkOptions = {
127130 frameworkPaths : string [ ] ;
128131 outputPath : string ;
132+ autoLink : boolean ;
129133} ;
130134
131135export function createFramework ( libraryPath : string ) {
@@ -171,6 +175,7 @@ export function createFramework(libraryPath: string) {
171175export async function createXCframework ( {
172176 frameworkPaths,
173177 outputPath,
178+ autoLink,
174179} : XCframeworkOptions ) {
175180 // Delete any existing xcframework to prevent the error:
176181 // - A library with the identifier 'macos-arm64' already exists.
@@ -189,13 +194,15 @@ export async function createXCframework({
189194 outputMode : "buffered" ,
190195 }
191196 ) ;
192- // Write a file to mark the xcframework is a Node-API module
193- // TODO: Consider including this in the Info.plist file instead
194- fs . writeFileSync (
195- path . join ( outputPath , "react-native-node-api-module" ) ,
196- "" ,
197- "utf8"
198- ) ;
197+ if ( autoLink ) {
198+ // Write a file to mark the xcframework is a Node-API module
199+ // TODO: Consider including this in the Info.plist file instead
200+ fs . writeFileSync (
201+ path . join ( outputPath , "react-native-node-api-module" ) ,
202+ "" ,
203+ "utf8"
204+ ) ;
205+ }
199206}
200207
201208/**
0 commit comments