Skip to content

Commit

Permalink
Generate XCFramework
Browse files Browse the repository at this point in the history
iOS arm64, iPhoneSimulator amd64+arm64, macOS amd64+arm64
  • Loading branch information
tadaskay committed Mar 24, 2023
1 parent 338ee5b commit cf57856
Show file tree
Hide file tree
Showing 5 changed files with 46 additions and 84 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
build/
lib
DerivedData
Frameworks

## Various settings
*.pbxuser
Expand Down
8 changes: 2 additions & 6 deletions WireGuardKit.podspec
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
Pod::Spec.new do |spec|
spec.name = "WireGuardKit"
spec.version = "0.1"
spec.version = "0.2"
spec.summary = "WireGuard for iOS and macOS"

spec.description = <<-DESC
Expand All @@ -27,11 +27,9 @@ Pod::Spec.new do |spec|

spec.prepare_command = <<-CMD
./build-libwg.sh
./build-framework.sh
CMD

spec.ios.vendored_frameworks = "frameworks/ios/libwg-go.framework"
spec.osx.vendored_frameworks = "frameworks/macos/libwg-go.framework"
spec.vendored_frameworks = "Frameworks/wg-go.xcframework"
spec.swift_version = "5.7"

spec.source_files = [
Expand All @@ -58,7 +56,5 @@ Pod::Spec.new do |spec|
"${PODS_TARGET_SRCROOT}/WireGuardKit/Sources/WireGuardKitGo/wireguard.h",
],
"APPLICATION_EXTENSION_API_ONLY" => "YES",
"EXCLUDED_ARCHS[sdk=iphonesimulator*]" => "arm64",
}
spec.user_target_xcconfig = { "EXCLUDED_ARCHS[sdk=iphonesimulator*]" => "arm64" }
end
49 changes: 0 additions & 49 deletions build-framework.sh

This file was deleted.

72 changes: 43 additions & 29 deletions build-libwg.sh
Original file line number Diff line number Diff line change
Expand Up @@ -7,16 +7,18 @@ SRC_ROOT="$ROOT/Sources/WireGuardKitGo"

cd "$SRC_ROOT"

BUILD_ROOT="$SRC_ROOT/out/build/"
ARTIFACT_IOS="$ROOT/lib/ios/libwg-go.a"
ARTIFACT_MACOS="$ROOT/lib/macos/libwg-go.a"
BUILD_ROOT="$ROOT/build/libwg-go"
LIPO_ARTIFACT_IOS="$BUILD_ROOT/lipo/iphoneos/libwg-go.a"
LIPO_ARTIFACT_IPHONESIM="$BUILD_ROOT/lipo/iphonesimulator/libwg-go.a"
LIPO_ARTIFACT_MACOS="$BUILD_ROOT/lipo/macosx/libwg-go.a"
XCFRAMEWORK_PATH="$ROOT/Frameworks/wg-go.xcframework"

rm -rf "$ROOT/lib"
mkdir -p $(dirname "$ARTIFACT_IOS")
mkdir -p $(dirname "$ARTIFACT_MACOS")

rm -rf "$BUILD_ROOT" "$ARTIFACT"
rm -rf "$XCFRAMEWORK_PATH"
rm -rf "$BUILD_ROOT"
mkdir -p "$BUILD_ROOT"
mkdir -p $(dirname "$LIPO_ARTIFACT_IOS")
mkdir -p $(dirname "$LIPO_ARTIFACT_IPHONESIM")
mkdir -p $(dirname "$LIPO_ARTIFACT_MACOS")

# Create a patched goroot using patches needed for iOS
GOROOT="$BUILD_ROOT/goroot/" # Not exported yet, still need the original GOROOT to copy
Expand All @@ -27,22 +29,30 @@ cat goruntime-*.diff | patch -p1 -fN -r- -d "$GOROOT"

BUILD_CFLAGS="-fembed-bitcode -Wno-unused-command-line-argument"

LIPO_IOS_INPUT_LIBS=()
LIPO_MACOS_INPUT_LIBS=()
IOS_LIBS=()
IPHONESIM_LIBS=()
MACOS_LIBS=()

# Build the library for each target
function build_arch() {
local ARCH="$1"
local GOARCH="$2"
local SDKNAME="$3"
local SDKNAME="$2"
local GOTAG=""
if [[ "$SDKNAME" == "iphoneos" ]]; then
local GOTAG="ios"
elif [[ "$SDKNAME" == "iphonesimulator" ]]; then
local GOOS=""
local GOARCH=""
if [[ "$SDKNAME" == "iphoneos" || "$SDKNAME" == "iphonesimulator" ]]; then
local GOTAG="ios"
local GOOS="ios"
elif [[ "$SDKNAME" == "macosx" ]]; then
local GOTAG="darwin"
local GOOS="darwin"
fi
if [[ "$ARCH" == "x86_64" ]]; then
local GOARCH="amd64"
elif [[ "$ARCH" == "arm64" ]]; then
local GOARCH="arm64"
fi

# Find the SDK path
local SDKPATH
SDKPATH="$(xcrun --sdk "$SDKNAME" --show-sdk-path)"
Expand All @@ -57,27 +67,31 @@ function build_arch() {
local FULL_CFLAGS="$BUILD_CFLAGS -isysroot $SDKPATH -arch $ARCH $PLATFORM_CFLAGS"
local LIBPATH="$BUILD_ROOT/$SDKNAME/libwg-go-$ARCH.a"

CGO_ENABLED=1 CGO_CFLAGS="$FULL_CFLAGS" CGO_LDFLAGS="$FULL_CFLAGS" GOOS=darwin GOARCH="$GOARCH" \
CGO_ENABLED=1 CGO_CFLAGS="$FULL_CFLAGS" CGO_LDFLAGS="$FULL_CFLAGS" GOOS="$GOOS" GOARCH="$GOARCH" \
go build -tags $GOTAG -ldflags=-w -trimpath -v -o "$LIBPATH" -buildmode c-archive
rm -f "$BUILD_ROOT/libwg-go-$ARCH.h"
# rm -f "$BUILD_ROOT/libwg-go-$ARCH.h"
if [[ "$SDKNAME" == "iphoneos" ]]; then
LIPO_IOS_INPUT_LIBS+=($LIBPATH)
IOS_LIBS+=($LIBPATH)
elif [[ "$SDKNAME" == "iphonesimulator" ]]; then
LIPO_IOS_INPUT_LIBS+=($LIBPATH)
IPHONESIM_LIBS+=($LIBPATH)
elif [[ "$SDKNAME" == "macosx" ]]; then
LIPO_MACOS_INPUT_LIBS+=($LIBPATH)
MACOS_LIBS+=($LIBPATH)
fi
}

build_arch x86_64 amd64 iphonesimulator
build_arch arm64 arm64 iphoneos
build_arch arm64 arm64 macosx
build_arch x86_64 amd64 macosx

# Create the fat static library including all architectures
build_arch arm64 iphonesimulator
build_arch x86_64 iphonesimulator
build_arch arm64 iphoneos
build_arch arm64 macosx
build_arch x86_64 macosx

LIPO="${LIPO:-lipo}"
"$LIPO" -create -output "$ARTIFACT_IOS" "${LIPO_IOS_INPUT_LIBS[@]}"
"$LIPO" -create -output "$LIPO_ARTIFACT_IOS" "${IOS_LIBS[@]}"
"$LIPO" -create -output "$LIPO_ARTIFACT_IPHONESIM" "${IPHONESIM_LIBS[@]}"
"$LIPO" -create -output "$LIPO_ARTIFACT_MACOS" "${MACOS_LIBS[@]}"

# MacOS
"$LIPO" -create -output "$ARTIFACT_MACOS" "${LIPO_MACOS_INPUT_LIBS[@]}"
xcodebuild -create-xcframework \
-library "$LIPO_ARTIFACT_IOS" -headers "$BUILD_ROOT/iphoneos/libwg-go-arm64.h" \
-library "$LIPO_ARTIFACT_IPHONESIM" -headers "$BUILD_ROOT/iphonesimulator/libwg-go-arm64.h" \
-library "$LIPO_ARTIFACT_MACOS" -headers "$BUILD_ROOT/macosx/libwg-go-arm64.h" \
-output "$XCFRAMEWORK_PATH"
Empty file removed frameworks/.gitkeep
Empty file.

0 comments on commit cf57856

Please sign in to comment.