Skip to content

Commit

Permalink
ios,build: support iOS simulator on arm64 hosts
Browse files Browse the repository at this point in the history
  • Loading branch information
staltz committed Oct 12, 2023
1 parent 776a94d commit 7e96401
Show file tree
Hide file tree
Showing 3 changed files with 61 additions and 19 deletions.
4 changes: 2 additions & 2 deletions common.gypi
Original file line number Diff line number Diff line change
Expand Up @@ -602,7 +602,7 @@
['target_arch=="x64"', {
'xcode_settings': {'ARCHS': ['x86_64']},
}],
[ 'target_arch in "arm64 arm armv7s"', {
['iossim!="true" and target_arch in "arm64 arm armv7s"', {
'xcode_settings': {
'OTHER_CFLAGS': [
'-fembed-bitcode'
Expand All @@ -628,7 +628,7 @@
'CLANG_CXX_LIBRARY': 'libc++',
},
}],
['target_arch=="x64" or target_arch=="ia32"', {
['target_arch=="x64" or target_arch=="ia32" or (target_arch=="arm64" and iossim=="true")', {
'xcode_settings': { 'SDKROOT': 'iphonesimulator' },
}, {
'xcode_settings': { 'SDKROOT': 'iphoneos', 'ENABLE_BITCODE': 'YES' },
Expand Down
6 changes: 6 additions & 0 deletions configure.py
Original file line number Diff line number Diff line change
Expand Up @@ -867,6 +867,11 @@
default=None,
help=argparse.SUPPRESS)

parser.add_argument('--ios-simulator',
action='store_true',
dest='ios_simulator',
help=argparse.SUPPRESS)

(options, args) = parser.parse_known_args()

# Expand ~ in the install prefix now, it gets written to multiple files.
Expand Down Expand Up @@ -1259,6 +1264,7 @@ def configure_node_lib_files(o):
def configure_node(o):
if options.dest_os == 'ios':
o['variables']['OS'] = 'ios'
o['variables']['iossim'] = b(options.ios_simulator)
if options.dest_os == 'android':
o['variables']['OS'] = 'android'
o['variables']['node_prefix'] = options.prefix
Expand Down
70 changes: 53 additions & 17 deletions tools/ios_framework_prepare.sh
Original file line number Diff line number Diff line change
Expand Up @@ -73,15 +73,37 @@ export GYP_DEFINES
make -j$(getconf _NPROCESSORS_ONLN)

# Move compilation outputs
mkdir -p $TARGET_LIBRARY_PATH/arm64
mkdir -p $TARGET_LIBRARY_PATH/arm64-device
for output_file in "${outputs_arm64[@]}"; do
cp $LIBRARY_PATH/$output_file $TARGET_LIBRARY_PATH/arm64/
cp $LIBRARY_PATH/$output_file $TARGET_LIBRARY_PATH/arm64-device/
done

# Compile Node.js for iOS arm64 simulator
make clean
GYP_DEFINES="target_arch=arm64 host_os=mac target_os=ios"
export GYP_DEFINES
./configure \
--dest-os=ios \
--dest-cpu=arm64 \
--with-intl=none \
--cross-compiling \
--enable-static \
--openssl-no-asm \
--v8-options=--jitless \
--without-node-code-cache \
--without-node-snapshot \
--ios-simulator
make -j$(getconf _NPROCESSORS_ONLN)

# Move compilation outputs
mkdir -p $TARGET_LIBRARY_PATH/arm64-simulator
for output_file in "${outputs_arm64[@]}"; do
cp $LIBRARY_PATH/$output_file $TARGET_LIBRARY_PATH/arm64-simulator/
done

# Compile Node.js for iOS simulators on x64 Macs
ARCH=$(arch)
if [ "$ARCH" = "arm64" ]; then
HOST_ARCH=$(arch)
if [ "$HOST_ARCH" = "arm64" ]; then
# Build with the command arch -x86_64 on arm64.
make clean
GYP_DEFINES="target_arch=x64 host_os=mac target_os=ios"
Expand Down Expand Up @@ -115,9 +137,9 @@ else
fi

# Move compilation outputs
mkdir -p $TARGET_LIBRARY_PATH/x64
mkdir -p $TARGET_LIBRARY_PATH/x64-simulator
for output_file in "${outputs_x64[@]}"; do
cp $LIBRARY_PATH/$output_file $TARGET_LIBRARY_PATH/x64/
cp $LIBRARY_PATH/$output_file $TARGET_LIBRARY_PATH/x64-simulator/
done

# Create a path to build the frameworks into
Expand All @@ -130,39 +152,53 @@ cd ../
# Compile the Framework Xcode project for arm64 device
for output_file in "${outputs_arm64[@]}"; do
rm -f $TARGET_LIBRARY_PATH/$output_file
mv $TARGET_LIBRARY_PATH/arm64/$output_file $TARGET_LIBRARY_PATH/$output_file
mv $TARGET_LIBRARY_PATH/arm64-device/$output_file $TARGET_LIBRARY_PATH/$output_file
done
xcodebuild build \
-project $NODELIB_PROJECT_PATH/NodeMobile.xcodeproj \
-target "NodeMobile" \
-configuration Release \
-arch arm64 \
-sdk "iphoneos" \
SYMROOT=$FRAMEWORK_TARGET_DIR
SYMROOT=$FRAMEWORK_TARGET_DIR/iphoneos-arm64

# Compile the Framework Xcode project for arm64 simulator
for output_file in "${outputs_arm64[@]}"; do
rm -f $TARGET_LIBRARY_PATH/$output_file
mv $TARGET_LIBRARY_PATH/arm64-simulator/$output_file $TARGET_LIBRARY_PATH/$output_file
done
xcodebuild build \
-project $NODELIB_PROJECT_PATH/NodeMobile.xcodeproj \
-target "NodeMobile" \
-configuration Release \
-arch arm64 \
-sdk "iphonesimulator" \
SYMROOT=$FRAMEWORK_TARGET_DIR/iphonesimulator-arm64

# Compile the Framework Xcode project for iOS simulators on x64 Macs
for output_file in "${outputs_x64[@]}"; do
rm -f $TARGET_LIBRARY_PATH/$output_file
mv $TARGET_LIBRARY_PATH/x64/$output_file $TARGET_LIBRARY_PATH/$output_file
mv $TARGET_LIBRARY_PATH/x64-simulator/$output_file $TARGET_LIBRARY_PATH/$output_file
done
xcodebuild build \
-project $NODELIB_PROJECT_PATH/NodeMobile.xcodeproj \
-target "NodeMobile" \
-configuration Release \
-arch x86_64 \
-sdk "iphonesimulator" \
SYMROOT=$FRAMEWORK_TARGET_DIR
SYMROOT=$FRAMEWORK_TARGET_DIR/iphonesimulator-x64

cp -RL $FRAMEWORK_TARGET_DIR/Release-iphoneos $FRAMEWORK_TARGET_DIR/Release-universal
# Join both simulator outputs into one
mkdir -p $FRAMEWORK_TARGET_DIR/iphonesimulator-universal/NodeMobile.framework
lipo -create \
$FRAMEWORK_TARGET_DIR/Release-iphoneos/NodeMobile.framework/NodeMobile \
$FRAMEWORK_TARGET_DIR/Release-iphonesimulator/NodeMobile.framework/NodeMobile \
-output $FRAMEWORK_TARGET_DIR/Release-universal/NodeMobile.framework/NodeMobile
$FRAMEWORK_TARGET_DIR/iphonesimulator-arm64/Release-iphonesimulator/NodeMobile.framework/NodeMobile \
$FRAMEWORK_TARGET_DIR/iphonesimulator-x64/Release-iphonesimulator/NodeMobile.framework/NodeMobile \
-output $FRAMEWORK_TARGET_DIR/iphonesimulator-universal/NodeMobile.framework/NodeMobile

# Create a .xcframework
# Create a .xcframework combining both iphoneos and iphonesimulator
xcodebuild -create-xcframework \
-framework $FRAMEWORK_TARGET_DIR/Release-iphoneos/NodeMobile.framework \
-framework $FRAMEWORK_TARGET_DIR/Release-iphonesimulator/NodeMobile.framework \
-framework $FRAMEWORK_TARGET_DIR/iphoneos-arm64/Release-iphoneos/NodeMobile.framework \
-framework $FRAMEWORK_TARGET_DIR/iphonesimulator-universal/NodeMobile.framework \
-output $FRAMEWORK_TARGET_DIR/NodeMobile.xcframework

echo "Frameworks built to $FRAMEWORK_TARGET_DIR"
Expand Down

0 comments on commit 7e96401

Please sign in to comment.