diff --git a/.github/workflows/build.yml b/.github/workflows/build.yml index ae64993..ea69c3f 100644 --- a/.github/workflows/build.yml +++ b/.github/workflows/build.yml @@ -23,24 +23,28 @@ jobs: uses: actions/checkout@v4 with: path: ios-client + submodules: true - - name: Checkout netbird - uses: actions/checkout@v4 - with: - repository: netbirdio/netbird - path: netbird +# - name: Checkout netbird +# uses: actions/checkout@v4 +# with: +# repository: netbirdio/netbird +# path: ios-client/libs/netbird - name: Setup Go uses: actions/setup-go@v5 with: go-version: '1.24' - cache-dependency-path: netbird/go.sum + cache-dependency-path: ios-client/libs/netbird/go.sum + + - name: Install gomobile + run: go install golang.org/x/mobile/cmd/gomobile@v0.0.0-20251113184115-a159579294ab - name: Install gomobile-netbird run: GOPROXY=direct go install github.com/netbirdio/gomobile-tvos-fork/cmd/gomobile-netbird@latest - name: Add gomobile-tvos-fork to netbird dependencies - working-directory: netbird + working-directory: ios-client/libs/netbird run: go get github.com/netbirdio/gomobile-tvos-fork@latest - name: Debug - List files before xcframework build @@ -51,7 +55,7 @@ jobs: - name: Build NetBirdSDK xcframework working-directory: ios-client - run: ./build-go-lib.sh ../netbird + run: ./build-go-lib.sh both - name: Debug - List files after xcframework build working-directory: ios-client diff --git a/.gitmodules b/.gitmodules new file mode 100644 index 0000000..93b8e0a --- /dev/null +++ b/.gitmodules @@ -0,0 +1,3 @@ +[submodule "libs/netbird"] + path = libs/netbird + url = https://github.com/netbirdio/netbird diff --git a/build-go-lib.sh b/build-go-lib.sh index 7951e23..7d1f0dc 100755 --- a/build-go-lib.sh +++ b/build-go-lib.sh @@ -1,22 +1,145 @@ #!/bin/bash -set -e +# Script to build NetBird iOS/tvOS bindings using gomobile +# +# Usage: ./build-go-lib.sh [platform] [version] +# +# Parameters: +# 1. platform (optional): Target platform(s) to build for +# - ios : Build for iOS and iOS Simulator only (uses standard gomobile) +# - tvos : Build for tvOS and tvOS Simulator only (uses gomobile-netbird) +# - both : Build for all platforms (uses gomobile-netbird) [default] +# +# 2. version (optional): Git tag to build +# - If provided, validates the tag exists in the submodule and checks it out +# - If omitted and current commit is exactly tagged, uses that tag +# - Otherwise, uses "dev-" (or "ci-" in GitHub Actions) +# - Leading 'v' is stripped from semver tags (v1.2.3 -> 1.2.3) +# +# Examples: +# ./build-go-lib.sh # Build both platforms using current commit +# ./build-go-lib.sh ios # Build iOS only using current commit +# ./build-go-lib.sh tvos v0.64.1 # Build tvOS only using tag v0.64.1 +# ./build-go-lib.sh both v0.64.1 # Build both platforms using tag v0.64.1 +# +# Output: +# Creates NetBirdSDK.xcframework in the current directory -rn_app_path=$(pwd) -netbirdPath=$1 -if [ -z "${1+x}" ] -then - netbirdPath=${GOPATH}/src/github.com/netbirdio/netbird -fi +set -euo pipefail -version=$2 -if [ -z "${2+x}" ] -then - version=development -fi +# Normalize semantic versions to drop a leading 'v' (e.g., v1.2.3 -> 1.2.3). +# Only strips if the string starts with 'v' followed by a digit, so it won't affect +# dev/ci strings or other non-semver values. +normalize_version() { + local ver="$1" + if [[ "$ver" =~ ^v[0-9] ]]; then + ver="${ver#v}" + fi + echo "$ver" +} -cd $netbirdPath +# Checkout a git tag in the specified repository path +# Tries the tag as provided, then with 'v' prefix if needed +checkout_tag() { + local tag="$1" + local repo_path="$2" -gomobile-netbird init -CGO_ENABLED=0 gomobile-netbird bind -target=ios,iossimulator,tvos,tvossimulator -bundleid=io.netbird.framework -ldflags="-X github.com/netbirdio/netbird/version.version=$version" -o $rn_app_path/NetBirdSDK.xcframework $netbirdPath/client/ios/NetBirdSDK + if git -C "$repo_path" rev-parse "refs/tags/$tag" >/dev/null 2>&1; then + git -C "$repo_path" checkout "$tag" + return 0 + fi -cd - + # Try with 'v' prefix if not provided + if [[ ! "$tag" =~ ^v ]] && git -C "$repo_path" rev-parse "refs/tags/v$tag" >/dev/null 2>&1; then + git -C "$repo_path" checkout "v$tag" + return 0 + fi + + echo "Error: Tag '$tag' does not exist" >&2 + exit 1 +} + +# Get version string, optionally checking out a tag if provided +get_version() { + local version_arg="${1:-}" + local repo_path="$2" + + if [ -n "$version_arg" ]; then + # Version provided - validate and checkout the tag + checkout_tag "$version_arg" "$repo_path" + normalize_version "$version_arg" + return + fi + + # No version provided - try to get an exact tag for current commit + local tag + tag=$(git -C "$repo_path" describe --tags --exact-match 2>/dev/null || true) + + if [ -n "$tag" ]; then + normalize_version "$tag" + return + fi + + # Fallback to dev/ci prefix with short hash + local short_hash + short_hash=$(git -C "$repo_path" rev-parse --short HEAD) + + if [ "${GITHUB_ACTIONS:-}" = "true" ]; then + echo "ci-$short_hash" + else + echo "dev-$short_hash" + fi +} + +project_root=$(pwd) +netbirdPath=$project_root/libs/netbird + +# Parse arguments +platform="${1:-both}" +version_arg="${2:-}" + +# Validate platform argument +case "$platform" in + ios|tvos|both) + ;; + *) + echo "Error: Invalid platform '$platform'. Must be 'ios', 'tvos', or 'both'." >&2 + exit 1 + ;; +esac + +# Get version (this also checks out the tag if provided) +version=$(get_version "$version_arg" "$netbirdPath") + +cd "$netbirdPath" + +echo "Using version: $version" +echo "Building for platform: $platform" + +# Set targets and gomobile command based on platform +case "$platform" in + ios) + targets="ios,iossimulator" + gomobile_cmd="gomobile" + ;; + tvos) + targets="tvos,tvossimulator" + gomobile_cmd="gomobile-netbird" + ;; + both) + targets="ios,iossimulator,tvos,tvossimulator" + gomobile_cmd="gomobile-netbird" + ;; +esac + +# Initialize gomobile +$gomobile_cmd init + +# Build +$gomobile_cmd bind \ + -target="$targets" \ + -bundleid=io.netbird.framework \ + -ldflags="-X github.com/netbirdio/netbird/version.version=$version" \ + -o "$project_root/NetBirdSDK.xcframework" \ + "$netbirdPath/client/ios/NetBirdSDK" + +cd - > /dev/null diff --git a/libs/netbird b/libs/netbird new file mode 160000 index 0000000..5333e55 --- /dev/null +++ b/libs/netbird @@ -0,0 +1 @@ +Subproject commit 5333e55a8134b7f0feb58777e92c32509abcd037