Skip to content

Commit

Permalink
build(swiftui): add support for arm64 arch and build universal swiftu…
Browse files Browse the repository at this point in the history
…i client app

We build the go source to static lib of arm64 and amd64 arch and merge them into a single static lib
using `lipo` tool. Then we can build and link an universal swiftui client app using `xcodebuild`.
  • Loading branch information
genshen committed Aug 2, 2023
1 parent 6153c63 commit a1792ba
Show file tree
Hide file tree
Showing 5 changed files with 34 additions and 16 deletions.
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
extra/go-api/libwssocks_go_api.a
extra/go-api/libwssocks_go_api_arm64.a
extra/go-api/libwssocks_go_api_amd64.a
extra/go-api/libwssocks_go_api.h

.DS_Store
Expand Down
23 changes: 23 additions & 0 deletions extra/go-api/Makefile
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
# this makefile builds go api into an archive file (static library)

BUILD_FLAGS=--trimpath -gcflags "-N"

BUILD_ENVS=CGO_ENABLED=1 CGO_CFLAGS="-mmacosx-version-min=12.1" CGO_CXXFLAGS="-mmacosx-version-min=12.1" CC=clang

.PHONY: all clean

all: libwssocks_go_api.a

libwssocks_go_api_amd64.a: wssocks_client_wrapper.go
$(BUILD_ENVS) GOOS=darwin GOARCH=amd64 go build $(BUILD_FLAGS) --buildmode=c-archive -o libwssocks_go_api_amd64.a
mv libwssocks_go_api_amd64.h libwssocks_go_api.h

libwssocks_go_api_arm64.a: wssocks_client_wrapper.go
$(BUILD_ENVS) GOOS=darwin GOARCH=arm64 go build $(BUILD_FLAGS) --buildmode=c-archive -o libwssocks_go_api_arm64.a
mv libwssocks_go_api_arm64.h libwssocks_go_api.h

libwssocks_go_api.a: libwssocks_go_api_amd64.a libwssocks_go_api_arm64.a
lipo libwssocks_go_api_amd64.a libwssocks_go_api_arm64.a -create -output libwssocks_go_api.a

clean:
rm libwssocks_go_api.a libwssocks_go_api_arm64.a libwssocks_go_api_amd64.a libwssocks_go_api.h
7 changes: 0 additions & 7 deletions extra/go-api/build_archive.sh

This file was deleted.

12 changes: 5 additions & 7 deletions swiftui-client/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,12 +6,10 @@
1. build C header file and archive file from go API.
```bash
cd extra/go-api
./build_archive.sh
make
```

2. Update target’s build settings
In Xcode, set `SWIFT_INCLUDE_PATHS` (Header Search Paths) to `$(SRCROOT)`,
where `$(SRCROOT)` is the same directory as .xcodeproj.
Then `LIBRARY_SEARCH_PATHS` (Library Search Paths) to `$(SRCROOT)/../extra/go-api`.

3. Build application in Xcode.
2. Build App
```bash
xcodebuild -arch=x86_64 -arch=arm64
```
6 changes: 4 additions & 2 deletions swiftui-client/wssocks-ustb-client.xcodeproj/project.pbxproj
Original file line number Diff line number Diff line change
Expand Up @@ -441,6 +441,7 @@
8021423027BA823700643775 /* Debug */ = {
isa = XCBuildConfiguration;
buildSettings = {
ALLOW_TARGET_PLATFORM_SPECIALIZATION = NO;
ARCHS = "$(ARCHS_STANDARD)";
ASSETCATALOG_COMPILER_APPICON_NAME = AppIcon;
ASSETCATALOG_COMPILER_GLOBAL_ACCENT_COLOR_NAME = AccentColor;
Expand All @@ -453,7 +454,7 @@
DEVELOPMENT_TEAM = "";
ENABLE_HARDENED_RUNTIME = YES;
ENABLE_PREVIEWS = YES;
EXCLUDED_ARCHS = arm64;
EXCLUDED_ARCHS = "";
GENERATE_INFOPLIST_FILE = YES;
HEADER_SEARCH_PATHS = "${SRCROOT}/";
INFOPLIST_FILE = "wssocks-ustb-client/Info.plist";
Expand All @@ -476,6 +477,7 @@
8021423127BA823700643775 /* Release */ = {
isa = XCBuildConfiguration;
buildSettings = {
ALLOW_TARGET_PLATFORM_SPECIALIZATION = NO;
ARCHS = "$(ARCHS_STANDARD)";
ASSETCATALOG_COMPILER_APPICON_NAME = AppIcon;
ASSETCATALOG_COMPILER_GLOBAL_ACCENT_COLOR_NAME = AccentColor;
Expand All @@ -488,7 +490,7 @@
DEVELOPMENT_TEAM = "";
ENABLE_HARDENED_RUNTIME = YES;
ENABLE_PREVIEWS = YES;
EXCLUDED_ARCHS = arm64;
EXCLUDED_ARCHS = "";
GENERATE_INFOPLIST_FILE = YES;
HEADER_SEARCH_PATHS = "${SRCROOT}/";
INFOPLIST_FILE = "wssocks-ustb-client/Info.plist";
Expand Down

0 comments on commit a1792ba

Please sign in to comment.