Skip to content

Instantly share code, notes, and snippets.

@gfreezy
Last active January 23, 2024 17:06
Show Gist options
  • Save gfreezy/d53adf6869a39d1fdad2d88233c1e7c0 to your computer and use it in GitHub Desktop.
Save gfreezy/d53adf6869a39d1fdad2d88233c1e7c0 to your computer and use it in GitHub Desktop.
Hacking native ARM64 binaries to run on the iOS Simulator
#!/usr/bin/env bash
set -ex
# https://bogo.wtf/arm64-to-sim.html
ios_plat=ios
simulator_plat=simulator
make() {
for name in $@; do
extension=${name##*.}
if [ "$extension" = "a" ]; then
make_static_lib_xcframework $name
elif [ "$extension" = "framework" ]; then
make_xcframework $name
fi
done
}
clear_dirs() {
rm -rf $ios_plat $simulator_plat
}
make_xcframework() {
cwd=$(pwd)
framework=$1
framework_name="${framework%%.*}"
rm -rf $framework_name.xcframework
make_ios_framework $framework_name $ios_plat
ios_framework_dir=$ios_plat/$framework_name.framework
make_simulator_framework $framework_name $simulator_plat
simulator_framework_dir=$simulator_plat/$framework_name.framework
clear_dirs
cd $cwd
xcodebuild -create-xcframework -output ./$framework_name.xcframework -framework ./$simulator_framework_dir -framework ./$ios_framework_dir
clear_dirs
}
make_static_lib_xcframework() {
cwd=$(pwd)
lib=$1
lib_name="${lib%%.*}"
rm -rf $lib_name.xcframework
make_ios_lib $lib_name $ios_plat
ios_framework_dir=$ios_plat/$lib_name/$lib_name.a
make_simulator_lib $lib_name $simulator_plat
simulator_framework_dir=$simulator_plat/$lib_name/$lib_name.a
clear_dirs
cd $cwd
xcodebuild -create-xcframework -output ./$lib_name.xcframework -library ./$simulator_framework_dir -library ./$ios_framework_dir
clear_dirs
}
make_ios_lib() {
framework_name=$1
plat=$2
framework_dir=$plat/$framework_name
mkdir -p $framework_dir
cp -a $framework_name.a $framework_dir/
remove_simulator_archs $framework_dir/$framework_name.a
}
make_simulator_lib() {
framework_name=$1
plat=$2
framework_dir=$plat/$framework_name
mkdir -p $framework_dir
cp -a $framework_name.a $framework_dir/
merge_simulator_archs $framework_dir/$framework_name.a
}
make_ios_framework() {
framework_name=$1
plat=$2
framework_dir=$plat/$framework_name.framework
mkdir -p $framework_dir
cp -a $framework_name.framework/ $framework_dir/
remove_simulator_archs $framework_dir/$framework_name
}
make_simulator_framework() {
framework_name=$1
plat=$2
framework_dir=$plat/$framework_name.framework
mkdir -p $framework_dir
cp -a $framework_name.framework/ $framework_dir/
merge_simulator_archs $framework_dir/$framework_name
}
remove_simulator_archs() {
binary=$1
for arch in x86_64 i386; do
lipo -remove $arch $binary -o $binary || true
done
}
extract_simulator_archs() {
binary=$1
for arch in arm64 x86_64; do
lipo -thin $arch $binary -output $binary.$arch
done
}
merge_simulator_archs() {
binary_path=$1
framework_dir="$(dirname $binary_path)"
framework_name="$(basename $binary_path)"
extract_simulator_archs $framework_dir/$framework_name
mkdir -p $framework_dir/arm64
cd $framework_dir/arm64
ar -x ../$framework_name.arm64
rm -f __.SYMDEF '__.SYMDEF SORTED'
for file in *.o; do
# https://github.com/bogo/arm64-to-sim
~/Developer/arm64-to-sim/.build/release/arm64-to-sim "$file"
done
cd ..
ar crv $framework_name.arm64-simulator arm64/*
lipo -create -output ${framework_name} $framework_name.x86_64 $framework_name.arm64-simulator
rm -rf arm64
}
make $@⏎
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment