name: Build Native
on:
  workflow_dispatch:
    inputs:
      sdl_ref:
        description: SDL3 commit/branch/tag to build
        required: true
        default: main
        type: string
#  schedule:
#    - cron: '0 0 * * *'
env:
  BUILD_TYPE: Release

jobs:
  build:
    name: ${{ matrix.platform.name }}
    runs-on: ${{ matrix.platform.os }}
    strategy:
      fail-fast: false
      matrix:
        platform:
          - { name: win-x64,     os: windows-latest, flags: -A x64 }
          - { name: win-x86,     os: windows-latest, flags: -A Win32 }
          - { name: win-arm64,   os: windows-latest, flags: -A ARM64 }
          - { name: linux-x64,   os: ubuntu-20.04,   flags: -GNinja, target_apt_arch: ":amd64"}
          - { name: linux-x86,   os: ubuntu-20.04,   flags: -GNinja -DCMAKE_C_FLAGS=-m32 -DCMAKE_CXX_FLAGS=-m32", target_apt_arch: ":i386" }
          - { name: linux-arm64, os: ubuntu-20.04,   flags: -GNinja, target_apt_arch: ":arm64", container: arm64v8/ubuntu }
          - { name: linux-arm,   os: ubuntu-20.04,   flags: -GNinja, target_apt_arch: ":armhf", container: arm32v7/ubuntu }
          - { name: osx-x64,     os: macos-latest,   flags: -DCMAKE_OSX_ARCHITECTURES=x86_64 -DCMAKE_OSX_DEPLOYMENT_TARGET=10.14 }
          # NOTE: macOS 11.0 is the first released supported by Apple Silicon.
          - { name: osx-arm64,   os: macos-latest,   flags: -DCMAKE_OSX_ARCHITECTURES=arm64 -DCMAKE_OSX_DEPLOYMENT_TARGET=11.0 }
    steps:
      - uses: actions/checkout@v4
        with:
          repository: 'libsdl-org/SDL'
          ref: ${{ inputs.sdl_ref }}

      - uses: actions/checkout@v4
        with:
          path: 'SDL3-CS'

      - name: Set up QEMU
        if: contains(matrix.platform.container, 'arm')
        uses: docker/setup-qemu-action@v3

      - name: Build (Linux ARM)
        if: contains(matrix.platform.container, 'arm')
        uses: addnab/docker-run-action@v3
        with:
          image: ${{ matrix.platform.container }}
          options: >
            -v ${{ github.workspace }}:/workspace
            -e NAME=${{ matrix.platform.name }}
            -e TARGET_APT_ARCH=${{ matrix.platform.target_apt_arch }}
            -e RUNNER_OS=${{ runner.os }}
            -e FLAGS=${{ matrix.platform.flags }}
            -e BUILD_TYPE=${{ env.BUILD_TYPE }}
          run: |
            cd /workspace
            ./SDL3-CS/build.sh

      - name: Build
        if: ${{ !contains(matrix.platform.container, 'arm') }}
        shell: bash
        env:
          NAME: ${{ matrix.platform.name }}
          TARGET_APT_ARCH: ${{ matrix.platform.target_apt_arch }}
          RUNNER_OS: ${{ runner.os }}
          FLAGS: ${{ matrix.platform.flags }}
        run: ./SDL3-CS/build.sh

      - name: Get Actions user id
        if: runner.os == 'Linux'
        id: get_uid
        run: echo "uid=$(id -u $USER)" >> $GITHUB_OUTPUT

      - name: Correct Ownership in GITHUB_WORKSPACE directory
        if: runner.os == 'Linux'
        uses: peter-murray/reset-workspace-ownership-action@v1
        with:
          user_id: ${{ steps.get_uid.outputs.uid }}

      - name: Create pull request
        uses: peter-evans/create-pull-request@v6
        with:
          commit-message: Update ${{ matrix.platform.name }} SDL binaries
          title: Update ${{ matrix.platform.name }} SDL binaries
          body: This PR has been auto-generated to update the ${{ matrix.platform.name }} SDL binaries.
          branch: update-${{ matrix.platform.name }}-binaries
          path: 'SDL3-CS'
        env:
          ACTIONS_ALLOW_UNSECURE_COMMANDS: 'true'

  build-ios:
    name: ios
    runs-on: macos-latest
    steps:
      - uses: actions/checkout@v4
        with:
          repository: 'libsdl-org/SDL'
          ref: ${{ inputs.sdl_ref }}

      - uses: actions/checkout@v4
        with:
          path: 'SDL3-CS'

      - name: Build (iOS)
        run: xcodebuild -project Xcode/SDL/SDL.xcodeproj -target SDL3.xcframework -configuration Release

      - name: Prepare release directory (iOS)
        run: mkdir -p SDL3-CS/native/ios

      - name: Prepare release (iOS)
        run: |
          mkdir -p SDL3-CS/native/ios/SDL3.xcframework/ios-arm64/SDL3.framework;
          mkdir -p SDL3-CS/native/ios/SDL3.xcframework/ios-arm64_x86_64-simulator/SDL3.framework;
          cp Xcode/SDL/build/SDL3.xcframework/Info.plist                                           SDL3-CS/native/ios/SDL3.xcframework/Info.plist;
          cp Xcode/SDL/build/SDL3.xcframework/ios-arm64/SDL3.framework/SDL3                        SDL3-CS/native/ios/SDL3.xcframework/ios-arm64/SDL3.framework/SDL3;
          cp Xcode/SDL/build/SDL3.xcframework/ios-arm64/SDL3.framework/Info.plist                  SDL3-CS/native/ios/SDL3.xcframework/ios-arm64/SDL3.framework/Info.plist;
          cp Xcode/SDL/build/SDL3.xcframework/ios-arm64_x86_64-simulator/SDL3.framework/SDL3       SDL3-CS/native/ios/SDL3.xcframework/ios-arm64_x86_64-simulator/SDL3.framework/SDL3;
          cp Xcode/SDL/build/SDL3.xcframework/ios-arm64_x86_64-simulator/SDL3.framework/Info.plist SDL3-CS/native/ios/SDL3.xcframework/ios-arm64_x86_64-simulator/SDL3.framework/Info.plist;

      - name: Create pull request
        uses: peter-evans/create-pull-request@v6
        with:
          commit-message: Update iOS SDL binaries
          title: Update iOS SDL binaries
          body: This PR has been auto-generated to update the iOS SDL binaries
          branch: update-ios-binaries
          path: 'SDL3-CS'
        env:
          ACTIONS_ALLOW_UNSECURE_COMMANDS: 'true'

  build-android:
    name: android
    runs-on: ubuntu-20.04
    env:
        NDK_VER: 23.1.7779620
        PLATFORM_VER: android-34
    steps:
      - uses: actions/checkout@v4
        with:
          repository: 'libsdl-org/SDL'
          ref: ${{ inputs.sdl_ref }}

      - uses: actions/checkout@v4
        with:
          path: 'SDL3-CS'

      - name: Setup JDK
        uses: actions/setup-java@v3
        with:
          distribution: microsoft
          java-version: |
            11
            17

      - name: Install Android SDK Manager
        uses: android-actions/setup-android@v3
        with:
          packages: ''

      - name: Install Android SDK
        run: |
          sdkmanager --install "platform-tools" "platforms;$PLATFORM_VER"
          sdkmanager --install "ndk;$NDK_VER" --channel=3

      - name: Build (Android)
        run: |
          export PATH=$ANDROID_HOME/ndk/$NDK_VER:$PATH
          export OUTPUT=$PWD/SDL3-CS/native/android
          rm -rf $OUTPUT && mkdir -p $OUTPUT

          # Build SDL3
          sed -i 's/abi=.*/abi="armeabi-v7a arm64-v8a x86 x86_64"/g' ./build-scripts/androidbuildlibs.sh
          ./build-scripts/androidbuildlibs.sh NDK_LIBS_OUT="$OUTPUT"

      - name: Build SDL3 Android Java
        run: |
          export JAVA_HOME=$JAVA_HOME_11_X64
          export PATH=$JAVA_HOME_11_X64/bin:$PATH
          export OUTPUT=$PWD/SDL3-CS/SDL3-CS.Android/Jars/
          rm -rf $OUTPUT && mkdir -p $OUTPUT

          # Build SDL3 Android Java part
          cd android-project/app/src/main/java
          javac -cp $ANDROID_HOME/platforms/$PLATFORM_VER/android.jar -encoding utf8 org/libsdl/app/*.java
          jar cvf $OUTPUT/SDL3AndroidBridge.jar org/libsdl/app/*.class

      - name: Create pull request
        uses: peter-evans/create-pull-request@v6
        with:
          commit-message: Update Android SDL binaries
          title: Update Android SDL binaries
          body: This PR has been auto-generated to update the Android SDL binaries
          branch: update-android-binaries
          path: 'SDL3-CS'
        env:
          ACTIONS_ALLOW_UNSECURE_COMMANDS: 'true'
