Skip to content

Instantly share code, notes, and snippets.

@sunnyyoung
Last active February 28, 2024 09:08
Show Gist options
  • Save sunnyyoung/8725983ade16bf8f201045a636067055 to your computer and use it in GitHub Desktop.
Save sunnyyoung/8725983ade16bf8f201045a636067055 to your computer and use it in GitHub Desktop.
#!/bin/bash
# shellcheck disable=2016,2046,SC2086
SRC=$(pwd)/libuv
DST=$(pwd)/libuv.builds
PLATFORMS="macosx iphoneos iphonesimulator appletvos appletvsimulator xros xrsimulator"
set -e
echo "Checking out libuv..."
if [ -d "$SRC" ] || [ -d "$DST" ]; then
echo "Source or Destination folder not empty." && exit 1
else
git clone https://github.com/libuv/libuv --depth 1 --branch v1.48.0
fi
for PLATFORM in $PLATFORMS; do
case $PLATFORM in
macosx)
DEPLOYMENT_TARGET=10.11
ARCHS="arm64 x86_64"
;;
iphoneos)
DEPLOYMENT_TARGET=9.0
ARCHS="arm64"
;;
iphonesimulator)
DEPLOYMENT_TARGET=9.0
ARCHS="arm64 x86_64"
;;
appletvos)
DEPLOYMENT_TARGET=9.0
ARCHS="arm64"
;;
appletvsimulator)
DEPLOYMENT_TARGET=9.0
ARCHS="arm64 x86_64"
;;
xros)
DEPLOYMENT_TARGET=1.0
ARCHS="arm64"
;;
xrsimulator)
DEPLOYMENT_TARGET=1.0
ARCHS="arm64 x86_64"
;;
esac
for ARCH in $ARCHS; do
echo "Building for $PLATFORM-$ARCH..."
cmake \
-DLIBUV_BUILD_TESTS=OFF \
-DLIBUV_BUILD_BENCH=OFF \
-DBUILD_SHARED_LIBS=OFF \
-DCMAKE_CXX_FLAGS="-Wno-quoted-include-in-framework-header" \
-DCMAKE_OSX_SYSROOT="$PLATFORM" \
-DCMAKE_OSX_DEPLOYMENT_TARGET="$DEPLOYMENT_TARGET" \
-DCMAKE_OSX_ARCHITECTURES="$ARCH" \
-GXcode \
-S "$SRC" \
-B "$DST/$PLATFORM-$ARCH"
pushd "$DST/$PLATFORM-$ARCH"
xcodebuild \
-project libuv.xcodeproj \
-target ALL_BUILD \
-configuration Release \
CODE_SIGNING_ALLOWED=NO \
GCC_WARN_INHIBIT_ALL_WARNINGS=YES \
clean \
build
cmake --install . --prefix .
popd
done
mkdir -p "$DST/libs/$PLATFORM"
libtool -static $(find $DST/$PLATFORM-*/lib -name libuv.a) -o "$DST/libs/$PLATFORM/libuv.a"
done
echo "Copying headers..."
rsync -a \
--include="*/" \
--include="*.h" \
--exclude='*' \
"$SRC/include" \
"$DST"
find "$DST/include" \
-name "*.h" \
-exec sed -i '' 's/include "\(.*\)"/include <\1>/g' {} \;
echo "Creating xcframework..."
pushd "$DST"
XCFRAMEWORK_ARGS=""
for PLATFORM in $PLATFORMS; do
XCFRAMEWORK_ARGS="$XCFRAMEWORK_ARGS -library $DST/libs/$PLATFORM/libuv.a -headers $DST/include"
done
xcodebuild -create-xcframework $XCFRAMEWORK_ARGS -output libuv.xcframework
zip -ry9 libuv.xcframework.zip libuv.xcframework
popd
echo "Creating framework..."
echo '
name: libuv
options:
deploymentTarget:
macOS: 10.11
iOS: 9.0
tvOS: 9.0
visionOS: 1.0
targets:
uv:
type: framework
supportedDestinations: [
macOS,
iOS,
tvOS,
visionOS
]
settings:
DEFINES_MODULE: NO
SKIP_INSTALL: NO
CODE_SIGNING_ALLOWED: NO
GENERATE_INFOPLIST_FILE: YES
BUILD_LIBRARY_FOR_DISTRIBUTION: YES
OTHER_LDFLAGS: -all_load
PRODUCT_BUNDLE_IDENTIFIER: framework.uv
sources:
- path: include
buildPhase: headers
headerVisibility: public
dependencies:
- framework: libuv.xcframework
aggregateTargets:
aggregate:
buildScripts:
- name: Create xcframework
script: |
xcodebuild archive \
-scheme uv \
-destination "generic/platform=macOS" \
-archivePath Archives/uv_macOS.xcarchive
xcodebuild archive \
-scheme uv \
-destination "generic/platform=iOS" \
-archivePath Archives/uv_iOS.xcarchive
xcodebuild archive \
-scheme uv \
-destination "generic/platform=iOS Simulator" \
-archivePath Archives/uv_iOS_Simulator.xcarchive
xcodebuild archive \
-scheme uv \
-destination "generic/platform=tvOS" \
-archivePath Archives/uv_tvOS.xcarchive
xcodebuild archive \
-scheme uv \
-destination "generic/platform=tvOS Simulator" \
-archivePath Archives/uv_tvOS_Simulator.xcarchive
xcodebuild archive \
-scheme uv \
-destination "generic/platform=visionOS" \
-archivePath Archives/uv_visionOS.xcarchive
xcodebuild archive \
-scheme uv \
-destination "generic/platform=visionOS Simulator" \
-archivePath Archives/uv_visionOS_Simulator.xcarchive
xcodebuild -create-xcframework \
-archive "Archives/uv_macOS.xcarchive" -framework uv.framework \
-archive "Archives/uv_iOS.xcarchive" -framework uv.framework \
-archive "Archives/uv_iOS_Simulator.xcarchive" -framework uv.framework \
-archive "Archives/uv_tvOS.xcarchive" -framework uv.framework \
-archive "Archives/uv_tvOS_Simulator.xcarchive" -framework uv.framework \
-archive "Archives/uv_visionOS.xcarchive" -framework uv.framework \
-archive "Archives/uv_visionOS_Simulator.xcarchive" -framework uv.framework \
-output uv.xcframework
' > "$DST/project.yml"
pushd "$DST"
xcodegen && xcodebuild build -scheme aggregate
zip -ry9 uv.xcframework.zip uv.xcframework
popd
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment