name: Build Artifacts (iOS)
on:
  workflow_dispatch:
    inputs:
      logLevel:
        description: 'Log level'
        required: true
        default: 'warning'
        type: choice
        options:
        - info
        - warning
        - debug
      tags:
        description: 'Test scenario tags'
        required: false
        type: boolean
  pull_request:
    branches: [master, develop]
    paths-ignore:
    - '**/*.md'
    - '.circleci/**'
    - '.cirrus.yml'
  push:
    branches: [master, develop, ios]
    paths-ignore:
    - '**/*.md'
    - '.circleci/**'
    - '.cirrus.yml'
  release:
    types: [published]
  schedule:
    - cron: '0 16 * * *'
concurrency:
  group: ${{ github.workflow }} / ${{ startsWith(github.event_name, 'pull') && github.ref_name || github.run_id }}
  cancel-in-progress: ${{ startsWith(github.event_name, 'pull') }}
env:
  CACHE_EPOCH: 148-2
  GOPROXY: direct
jobs:
  ios-simulator-release:
    runs-on: macos-26
    permissions:
      contents: write
    strategy:
      fail-fast: false
      matrix:
        include:
          - arch: x64
            build_type: Release
            rust_target: x86_64-apple-ios
            xcode_version: 26.3
            build_options: -build-test -build-benchmark
          - arch: arm64
            build_type: Release
            rust_target: aarch64-apple-ios-sim
            xcode_version: 26.3
            build_options: -run-test
    env:
      BUILD_TYPE: '${{ matrix.build_type }}'
    steps:
      - uses: actions/checkout@v5
      - name: Checkout with shallow submodules
        run: |
          # unshallow must come first otherwise submodule may be get unshallowed
          git fetch --tags --unshallow
          git submodule update --init --depth 1
      - name: Cache tun2proxy build stage
        id: tun2proxy-cache
        uses: actions/cache@v5
        with:
          path: |
            third_party/tun2proxy/target/${{ matrix.rust_target }}/release/libtun2proxy.a
          key: ${{ runner.os }}-rust-${{ matrix.rust_target }}-tun2proxy-${{ hashFiles('RUST_REVISION') }}-${{ hashFiles('RUST_CARGO_REVISION') }}-${{ hashFiles('third_party/tun2proxy/Cargo.toml') }}-v${{ env.CACHE_EPOCH }}-v1
      - uses: actions/setup-go@v6
        with:
          go-version: '>=1.20.0'
          cache-dependency-path: |
            tools/go.sum
            third_party/boringssl/src/go.sum
      - name: Build build tool
        run: |
          cd tools
          go build
      - name: Build Binary (tun2proxy)
        if: ${{ steps.tun2proxy-cache.outputs.cache-hit != 'true' }}
        run: |
          # required by m1 runner
          # https://github.com/actions/runner-images/blob/main/images/macos/scripts/build/install-rust.sh
          rm -rf $HOME/.cargo
          # brew install rustup
          rustup-init -y --no-modify-path --default-toolchain=stable --profile=minimal
          source "$HOME/.cargo/env"
          ./scripts/setup-ios-rust.sh
          WITH_OS=ios-sim WITH_CPU=${{ matrix.arch }} ./scripts/build-tun2proxy.sh
      - name: Set up Xcode version
        run: |
          sudo xcode-select -s /Applications/Xcode_${{ matrix.xcode_version }}.app
      - name: Build Binary (GUI)
        run: |
          ./tools/build --system ios --subsystem simulator --arch ${{ matrix.arch }} -nc ${{ matrix.build_options }}
