diff --git a/.gitignore b/.gitignore index 20a1ea1..53eab58 100644 --- a/.gitignore +++ b/.gitignore @@ -1,3 +1,4 @@ .idea/ build/ examples/a.out +ios/ diff --git a/Makefile b/Makefile index 183d6c5..bb5e76e 100644 --- a/Makefile +++ b/Makefile @@ -1,25 +1,22 @@ IOS_OUT=ios ios-arm64: - GOOS=ios \ GOARCH=arm64 \ - CGO_ENABLED=1 \ - CLANGARCH=arm64 \ SDK=iphoneos \ - CC=$(PWD)/clangwrap.sh \ - CGO_CFLAGS="-fembed-bitcode" \ - go build -buildmode=c-archive -o $(IOS_OUT)/libpolygonid-ios.a ./cmd/polygonid + MIN_VERSION=16 \ + ./build-ios.sh +ios-sim-arm64: + GOARCH=arm64 \ + SDK=iphonesimulator \ + MIN_VERSION=16 \ + ./build-ios.sh -ios-simulator: - GOOS=darwin \ +ios-sim-amd64: GOARCH=amd64 \ - CGO_ENABLED=1 \ - CLANGARCH=x86_64 \ SDK=iphonesimulator \ - CC=$(PWD)/clangwrap.sh \ - CGO_CFLAGS="-fembed-bitcode" \ - go build -tags ios -buildmode=c-archive -o $(IOS_OUT)/libpolygonid-ios-simulator.a ./cmd/polygonid + MIN_VERSION=16 \ + ./build-ios.sh darwin-arm64: GOOS=darwin \ @@ -28,9 +25,26 @@ darwin-arm64: CLANGARCH=arm64 \ go build -buildmode=c-archive -o $(IOS_OUT)/libpolygonid-darwin-arm64.a ./cmd/polygonid -ios: ios-arm64 ios-simulator - lipo $(IOS_OUT)/libpolygonid-ios.a $(IOS_OUT)/libpolygonid-ios-simulator.a -create -output $(IOS_OUT)/libpolygonid.a - cp $(IOS_OUT)/libpolygonid-ios.h $(IOS_OUT)/libpolygonid.h +darwin-amd64: + GOOS=darwin \ + GOARCH=amd64 \ + CGO_ENABLED=1 \ + CLANGARCH=x86_64 \ + go build -buildmode=c-archive -o $(IOS_OUT)/libpolygonid-darwin-amd64.a ./cmd/polygonid + +ios-device: ios-arm64 + cp $(IOS_OUT)/libpolygonid-ios-arm64.a $(IOS_OUT)/libpolygonid-ios.a + cp $(IOS_OUT)/libpolygonid-ios-arm64.h $(IOS_OUT)/libpolygonid.h + +ios-simulator: ios-sim-arm64 ios-sim-amd64 + lipo $(IOS_OUT)/libpolygonid-ios-sim-amd64.a $(IOS_OUT)/libpolygonid-ios-sim-arm64.a -create -output $(IOS_OUT)/libpolygonid-ios-sim.a + cp $(IOS_OUT)/libpolygonid-ios-sim-arm64.h $(IOS_OUT)/libpolygonid.h + +ios: ios-device ios-simulator + +darwin: darwin-arm64 darwin-amd64 + lipo $(IOS_OUT)/libpolygonid-darwin-arm64.a $(IOS_OUT)/libpolygonid-darwin-amd64.a -create -output $(IOS_OUT)/libpolygonid-darwin.a + cp $(IOS_OUT)/libpolygonid-darwin-arm64.h $(IOS_OUT)/libpolygonid.h dylib: go build -buildmode=c-shared -o $(IOS_OUT)/libpolygonid.dylib ./cmd/polygonid diff --git a/build-ios.sh b/build-ios.sh new file mode 100755 index 0000000..7dc895e --- /dev/null +++ b/build-ios.sh @@ -0,0 +1,30 @@ +export GOOS=ios +export CGO_ENABLED=1 +export CC=$(PWD)/clangwrap.sh +export CGO_CFLAGS="-fembed-bitcode" + + +if [ "$GOARCH" = "amd64" ]; then + CLANGARCH="x86_64" +elif [ "$GOARCH" = "arm64" ]; then + CLANGARCH="arm64" +fi + + +if [ "$SDK" = "iphoneos" ]; then + export TARGET="$CLANGARCH-apple-ios$MIN_VERSION" +elif [ "$SDK" = "iphonesimulator" ]; then + export TARGET="$CLANGARCH-apple-ios$MIN_VERSION-simulator" +fi + +export SDK_PATH=$(xcrun --sdk "$SDK" --show-sdk-path) + +export CGO_LDFLAGS="-target ${TARGET} -syslibroot \"${SDK_PATH}\"" + +if [ "$SDK" = "iphoneos" ]; then + export LIB_FILE=ios/libpolygonid-ios-$CLANGARCH.a +elif [ "$SDK" = "iphonesimulator" ]; then + export LIB_FILE=ios/libpolygonid-ios-sim-$CLANGARCH.a +fi + +go build -buildmode c-archive -trimpath -o $LIB_FILE ./cmd/polygonid \ No newline at end of file diff --git a/clangwrap.sh b/clangwrap.sh index fa406f8..34a0e0d 100755 --- a/clangwrap.sh +++ b/clangwrap.sh @@ -1,10 +1,5 @@ -#!/bin/sh -# To select a specific SDK, run 'xcodebuild -showsdks' -# You need to specify SDK & CLANGARCH +#!/bin/zsh -SDK_PATH=`xcrun --sdk $SDK --show-sdk-path` -export IPHONEOS_DEPLOYMENT_TARGET=5.1 -# cmd/cgo doesn't support llvm-gcc-4.2, so we have to use clang. -CLANG=`xcrun --sdk $SDK --find clang` +CLANG=$(xcrun --sdk "$SDK" --find clang) -exec "$CLANG" -arch $CLANGARCH -isysroot "$SDK_PATH" -mios-version-min=10.0 "$@" +exec "$CLANG" -target "$TARGET" -isysroot "$SDK_PATH" "$@" \ No newline at end of file