Last active
October 30, 2023 07:28
-
-
Save banjun/a8b46c26e7328137dc278ec045f0d1d5 to your computer and use it in GitHub Desktop.
create an xcframework from a universal static lib .a or universal static .framework without rebuild
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/bin/zsh | |
if [[ -z $BOGO_ARM64_TO_SIM ]]; then | |
echo "❌ requires BOGO_ARM64_TO_SIM: set an executable binary path built from https://github.com/bogo/arm64-to-sim" | |
exit 1; | |
fi | |
set -eu | |
# set -x # debug log | |
function lib_a_to_xcfw() { | |
TARGET_A="$(readlink -f "$1")" || {echo "⚠️ skip $1"; return 0} | |
TARGET_BASENAME="$(basename "$TARGET_A")" | |
TARGET_DIR="$(dirname "$TARGET_A")" | |
X86_64_A="$TARGET_A.x86_64.a" | |
ARM64_SIM_A="$TARGET_A.arm64-sim.a" | |
XCFW="$(dirname $TARGET_A)/${${TARGET_BASENAME//lib/}//.a/}.xcframework" | |
WORK_DIR="$(dirname $TARGET_A)/bogo-work" | |
ARM64_A="$WORK_DIR/device/$TARGET_BASENAME" | |
SIM_A="$WORK_DIR/sim/$TARGET_BASENAME" | |
echo "🦾 work on $TARGET_A" | |
mkdir -p "$WORK_DIR" | |
mkdir -p $(dirname "$ARM64_A") | |
mkdir -p $(dirname "$SIM_A") | |
lipo -thin x86_64 "$TARGET_A" -output "$X86_64_A" | |
lipo -thin arm64 "$TARGET_A" -output "$ARM64_A" | |
echo "🦾 device => sim" | |
cd "$WORK_DIR" | |
ar x "$ARM64_A" | |
for f in *.o; do "$BOGO_ARM64_TO_SIM" "$f"; done | |
ar crv "$ARM64_SIM_A" *.o > /dev/null | |
cd - | |
echo "🦾 sim (x86_64 + arm64) + device (arm64) => xcframework" | |
lipo -create "$X86_64_A" "$ARM64_SIM_A" -output "$SIM_A" | |
rm -rf "$XCFW" | |
xcodebuild -create-xcframework -library "$ARM64_A" -library "$SIM_A" -output "$XCFW" | |
rm "$TARGET_A" "$X86_64_A" "$ARM64_A" "$ARM64_SIM_A" "$SIM_A" | |
rm -rf "$WORK_DIR" | |
echo "✅ done" | |
} | |
function fw_to_xcfw() { | |
TARGET_FW="$(readlink -f "$1")" || {echo "⚠️ skip $1"; return 0} | |
FW_BASENAME="${$(basename "$TARGET_FW")//.framework/}" | |
FW_DIR="$(dirname "$TARGET_FW")" | |
TARGET_A=$(readlink -f "$TARGET_FW/Versions/A/$FW_BASENAME" || echo -n "$TARGET_FW/$FW_BASENAME") | |
TARGET_A_RELATIVE_TO_FW="${TARGET_A//"$TARGET_FW\/"/}" | |
X86_64_A="$TARGET_A.x86_64.a" | |
ARM64_A="$TARGET_A.arm64.a" | |
ARM64_SIM_A="$TARGET_A.arm64-sim.a" | |
WORK_DIR="$FW_DIR/bogo-work" | |
WORK_FW_SIM="$WORK_DIR/sim/$FW_BASENAME.framework" | |
WORK_FW_SIM_A="$WORK_FW_SIM/$TARGET_A_RELATIVE_TO_FW" | |
WORK_FW_DEVICE="$WORK_DIR/device/$FW_BASENAME.framework" | |
WORK_FW_DEVICE_A="$WORK_FW_DEVICE/$TARGET_A_RELATIVE_TO_FW" | |
XCFW="$FW_DIR/$FW_BASENAME.xcframework" | |
echo "🦾 copy to platform frameworks" | |
mkdir -p "$WORK_FW_SIM" | |
cp -a "$TARGET_FW/" "$WORK_FW_SIM" | |
rm "$WORK_FW_SIM_A" | |
mkdir -p "$WORK_FW_DEVICE" | |
cp -a "$TARGET_FW/" "$WORK_FW_DEVICE" | |
rm "$WORK_FW_DEVICE_A" | |
echo "🦾 work on $TARGET_A" | |
lipo -thin x86_64 "$TARGET_A" -output "$X86_64_A" | |
lipo -thin arm64 "$TARGET_A" -output "$ARM64_A" | |
if file "$TARGET_A" | grep -q 'Mach-O 64-bit object'; then | |
echo "🦾 (Mach-O object) device => sim" | |
cp "$ARM64_A" "$ARM64_SIM_A" | |
"$BOGO_ARM64_TO_SIM" "$ARM64_SIM_A" | |
elif file "$TARGET_A" | grep -q 'ar archive'; then | |
echo "🦾 (ar archive) device => sim" | |
mkdir -p $WORK_DIR | |
cd $WORK_DIR | |
ar x "$ARM64_A" | |
for f in *.o; do "$BOGO_ARM64_TO_SIM" "$f"; done | |
ar crv "$ARM64_SIM_A" *.o > /dev/null | |
cd - | |
else | |
echo "❌ unknown binary: $TARGET_A" | |
return 1 | |
fi | |
echo "🦾 sim (x86_64 + arm64) framework" | |
lipo -create "$X86_64_A" "$ARM64_SIM_A" -output "$WORK_FW_SIM_A" | |
echo "🦾 device (arm64) framework" | |
cp -p "$ARM64_A" "$WORK_FW_DEVICE_A" | |
echo "🦾 sim (x86_64 + arm64) + device (arm64) => xcframework" | |
rm -rf "$XCFW" | |
xcodebuild -create-xcframework -framework "$WORK_FW_SIM" -framework "$WORK_FW_DEVICE" -output "$XCFW" | |
rm "$X86_64_A" "$ARM64_A" "$ARM64_SIM_A" | |
rm -rf "$WORK_DIR" | |
rm -rf "$TARGET_FW" | |
echo "✅ done" | |
} | |
# convert libs & frameworks to xcframeworks | |
# | |
# call one of the functions for each static libs/frameworks | |
# example: | |
lib_a_to_xcfw Pods/GoogleAnalytics/Libraries/libGoogleAnalytics.a | |
fw_to_xcfw Pods/OpenCV/opencv2.framework |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
for example, use this:
and convert them to xcframeworks
output:
your todo after them