name: ContinuousDelivery

on:
  push:
    branches: [ master ]
  pull_request:
    branches: [ master ]

env:
  BUILD_TYPE: RelWithDebInfo
  BRANCH_NAME: ${{ github.head_ref || github.ref_name }}

jobs:
  linux-ubuntu:
    timeout-minutes: 10

    runs-on: ubuntu-20.04

    steps:
    - uses: actions/checkout@v4
      with:
        submodules: recursive

    - name: Install Dependencies
      run: sudo apt update && sudo apt -y install libsodium-dev cmake libvpx-dev libopus-dev

    - name: Configure CMake
      run: cmake -B ${{github.workspace}}/build -DCMAKE_BUILD_TYPE=${{env.BUILD_TYPE}} -DTOMATO_BREAKPAD=ON -DTOMATO_TOX_AV=ON -DCMAKE_EXE_LINKER_FLAGS=-gz

    - name: Build
      run: cmake --build ${{github.workspace}}/build --config ${{env.BUILD_TYPE}} -j 4 -t tomato

    - name: Determine tag name
      id: tag
      shell: bash
      # taken from llama.cpp
      run: |
        SHORT_HASH="$(git rev-parse --short=7 HEAD)"
        if [[ "${{ env.BRANCH_NAME }}" == "master" ]]; then
          echo "name=dev-${SHORT_HASH}" >> $GITHUB_OUTPUT
        else
          SAFE_NAME=$(echo "${{ env.BRANCH_NAME }}" | tr '/' '-')
          echo "name=dev-${SAFE_NAME}-${SHORT_HASH}" >> $GITHUB_OUTPUT
        fi

    - name: Compress artifacts
      shell: bash
      run: |
        tar -czvf ${{ github.event.repository.name }}-${{ steps.tag.outputs.name }}-${{ runner.os }}-ubuntu20.04-x86_64.tar.gz -C ${{github.workspace}}/build/bin/ .

    - uses: actions/upload-artifact@v4
      with:
        name: ${{ github.event.repository.name }}-${{ steps.tag.outputs.name }}-${{ runner.os }}-ubuntu20.04-x86_64
        # TODO: do propper packing
        path: |
          ${{github.workspace}}/${{ github.event.repository.name }}-${{ steps.tag.outputs.name }}-${{ runner.os }}-ubuntu20.04-x86_64.tar.gz

  android:
    timeout-minutes: 30
    # contains sections copied from sdl repo

    runs-on: ubuntu-latest

    strategy:
      matrix:
        platform:
          - vcpkg_toolkit: arm-neon-android
            ndk_abi: armeabi-v7a
          - vcpkg_toolkit: arm64-android
            ndk_abi: arm64-v8a
          - vcpkg_toolkit: x64-android
            ndk_abi: x86_64

    steps:
    - uses: actions/checkout@v4
      with:
        submodules: recursive

    - uses: nttld/setup-ndk@v1
      id: setup_ndk
      with:
        local-cache: false # https://github.com/nttld/setup-ndk/issues/518
        ndk-version: r26d

    - uses: actions/setup-java@v4
      with:
        distribution: 'temurin'
        java-version: '17'

    - name: Install Dependencies (host)
      run: sudo apt update && sudo apt -y install cmake pkg-config nasm

    - name: Export GitHub Actions cache environment variables
      uses: actions/github-script@v7
      with:
        script: |
          core.exportVariable('ACTIONS_CACHE_URL', process.env.ACTIONS_CACHE_URL || '');
          core.exportVariable('ACTIONS_RUNTIME_TOKEN', process.env.ACTIONS_RUNTIME_TOKEN || '');

    - name: Install Dependencies (target)
      env:
        ANDROID_NDK_HOME: ${{steps.setup_ndk.outputs.ndk-path}}
        VCPKG_BINARY_SOURCES: "clear;x-gha,readwrite"
      #run: vcpkg install --triplet ${{matrix.platform.vcpkg_toolkit}} --overlay-ports=vcpkg/ports libsodium opus libvpx libpng libjpeg-turbo
      run: vcpkg install --triplet ${{matrix.platform.vcpkg_toolkit}} libsodium opus libvpx libpng libjpeg-turbo freetype

    # vcpkg scripts root /usr/local/share/vcpkg/scripts
    - name: Configure CMake
      env:
        ANDROID_NDK_HOME: ${{steps.setup_ndk.outputs.ndk-path}}
      run: cmake -B ${{github.workspace}}/build -DCMAKE_BUILD_TYPE=${{env.BUILD_TYPE}} -DCMAKE_TOOLCHAIN_FILE=/usr/local/share/vcpkg/scripts/buildsystems/vcpkg.cmake -DVCPKG_TARGET_TRIPLET=${{matrix.platform.vcpkg_toolkit}} -DANDROID=1 -DANDROID_PLATFORM=23 -DANDROID_ABI=${{matrix.platform.ndk_abi}} -DVCPKG_CHAINLOAD_TOOLCHAIN_FILE=${{steps.setup_ndk.outputs.ndk-path}}/build/cmake/android.toolchain.cmake -DSDL_ANDROID_JAR=ON -DSDLIMAGE_JPG_SHARED=OFF -DSDLIMAGE_PNG_SHARED=OFF -DTOMATO_MAIN_SO=ON -DTOMATO_TOX_AV=ON -DCMAKE_EXE_LINKER_FLAGS=-gz

    - name: Build (tomato)
      run: cmake --build ${{github.workspace}}/build --config ${{env.BUILD_TYPE}} -j 4 -t tomato

    - name: Build (SDL3-jar) (workaround)
      run: cmake --build ${{github.workspace}}/build --config ${{env.BUILD_TYPE}} -j 4 -t SDL3-jar

    - name: Build (apk)
      run: cmake --build ${{github.workspace}}/build --config ${{env.BUILD_TYPE}} -j 4 -t tomato-apk

    - name: Determine tag name
      id: tag
      shell: bash
      # taken from llama.cpp
      run: |
        SHORT_HASH="$(git rev-parse --short=7 HEAD)"
        if [[ "${{ env.BRANCH_NAME }}" == "master" ]]; then
          echo "name=dev-${SHORT_HASH}" >> $GITHUB_OUTPUT
        else
          SAFE_NAME=$(echo "${{ env.BRANCH_NAME }}" | tr '/' '-')
          echo "name=dev-${SAFE_NAME}-${SHORT_HASH}" >> $GITHUB_OUTPUT
        fi

    - name: rename apk
      id: rename_apk
      shell: bash
      run: mv "${{github.workspace}}/build/android/tomato.apk" "${{github.workspace}}/build/android/${{github.event.repository.name}}-${{steps.tag.outputs.name}}-Android-${{matrix.platform.ndk_abi}}.apk"

    - uses: actions/upload-artifact@v4
      with:
        name: ${{github.event.repository.name}}-${{steps.tag.outputs.name}}-${{runner.os}}-Android-${{matrix.platform.ndk_abi}}
        path: |
          ${{github.workspace}}/build/android/${{github.event.repository.name}}-${{steps.tag.outputs.name}}-Android-${{matrix.platform.ndk_abi}}.apk

  windows:
    timeout-minutes: 15

    runs-on: windows-2019

    steps:
    - uses: actions/checkout@v4
      with:
        submodules: recursive

    - name: Export GitHub Actions cache environment variables
      uses: actions/github-script@v7
      with:
        script: |
          core.exportVariable('ACTIONS_CACHE_URL', process.env.ACTIONS_CACHE_URL || '');
          core.exportVariable('ACTIONS_RUNTIME_TOKEN', process.env.ACTIONS_RUNTIME_TOKEN || '');

    - name: Install Dependencies
      env:
        VCPKG_BINARY_SOURCES: "clear;x-gha,readwrite"
      run: vcpkg install pkgconf:x64-windows libsodium:x64-windows-static pthreads:x64-windows-static opus:x64-windows-static libvpx:x64-windows-static libpng:x64-windows-static libjpeg-turbo:x64-windows-static freetype:x64-windows-static

    # setup vs env
    - uses: ilammy/msvc-dev-cmd@v1
      with:
        arch: amd64

    ## sdl_image vendored needs nasm for dav1d
    #- uses: ilammy/setup-nasm@v1

    - name: Configure CMake
      run: cmake -G Ninja -B ${{github.workspace}}/build -DCMAKE_BUILD_TYPE=${{env.BUILD_TYPE}} -DCMAKE_TOOLCHAIN_FILE=C:/vcpkg/scripts/buildsystems/vcpkg.cmake -DVCPKG_TARGET_TRIPLET=x64-windows-static -DSDLIMAGE_VENDORED=OFF -DSDLIMAGE_DEPS_SHARED=OFF -DSDLIMAGE_JXL=OFF -DSDLIMAGE_AVIF=OFF -DPKG_CONFIG_EXECUTABLE=C:/vcpkg/installed/x64-windows/tools/pkgconf/pkgconf.exe -DTOMATO_BREAKPAD=ON -DTOMATO_TOX_AV=ON

    - name: Build
      run: cmake --build ${{github.workspace}}/build --config ${{env.BUILD_TYPE}} -t tomato

    - name: Determine tag name
      id: tag
      shell: bash
      # taken from llama.cpp
      run: |
        SHORT_HASH="$(git rev-parse --short=7 HEAD)"
        if [[ "${{ env.BRANCH_NAME }}" == "master" ]]; then
          echo "name=dev-${SHORT_HASH}" >> $GITHUB_OUTPUT
        else
          SAFE_NAME=$(echo "${{ env.BRANCH_NAME }}" | tr '/' '-')
          echo "name=dev-${SAFE_NAME}-${SHORT_HASH}" >> $GITHUB_OUTPUT
        fi

    - name: Clean temporary artifacts
      # msvc sometimes produces .ilk files, which are used for linking only
      shell: powershell
      run: |
        Remove-Item "${{github.workspace}}/build/bin/*.ilk"

    - name: Compress artifacts
      shell: powershell
      run: |
        Compress-Archive -Path ${{github.workspace}}/build/bin/* -Destination ${{ github.event.repository.name }}-${{ steps.tag.outputs.name }}-${{ runner.os }}-msvc-x86_64.zip

    - uses: actions/upload-artifact@v4
      with:
        name: ${{github.event.repository.name}}-${{steps.tag.outputs.name}}-${{runner.os}}-msvc-x86_64
        # TODO: do propper packing
        path: |
          ${{github.workspace}}/${{ github.event.repository.name }}-${{ steps.tag.outputs.name }}-${{ runner.os }}-msvc-x86_64.zip


  windows-asan:
    timeout-minutes: 15

    runs-on: windows-2019

    steps:
    - uses: actions/checkout@v4
      with:
        submodules: recursive

    - name: Export GitHub Actions cache environment variables
      uses: actions/github-script@v7
      with:
        script: |
          core.exportVariable('ACTIONS_CACHE_URL', process.env.ACTIONS_CACHE_URL || '');
          core.exportVariable('ACTIONS_RUNTIME_TOKEN', process.env.ACTIONS_RUNTIME_TOKEN || '');

    - name: Install Dependencies
      env:
        VCPKG_BINARY_SOURCES: "clear;x-gha,readwrite"
      run: vcpkg install pkgconf:x64-windows libsodium:x64-windows-static pthreads:x64-windows-static opus:x64-windows-static libvpx:x64-windows-static libpng:x64-windows-static libjpeg-turbo:x64-windows-static freetype:x64-windows-static


    # setup vs env
    - uses: ilammy/msvc-dev-cmd@v1
      with:
        arch: amd64

    ## sdl_image vendored needs nasm for dav1d
    #- uses: ilammy/setup-nasm@v1

    - name: Configure CMake
      run: cmake -G Ninja -B ${{github.workspace}}/build -DCMAKE_BUILD_TYPE=${{env.BUILD_TYPE}} -DCMAKE_TOOLCHAIN_FILE=C:/vcpkg/scripts/buildsystems/vcpkg.cmake -DVCPKG_TARGET_TRIPLET=x64-windows-static -DTOMATO_ASAN=ON -DCMAKE_MSVC_RUNTIME_LIBRARY=MultiThreaded -DSDLIMAGE_VENDORED=OFF -DSDLIMAGE_DEPS_SHARED=OFF -DSDLIMAGE_JXL=OFF -DSDLIMAGE_AVIF=OFF -DPKG_CONFIG_EXECUTABLE=C:/vcpkg/installed/x64-windows/tools/pkgconf/pkgconf.exe -DTOMATO_TOX_AV=ON

    - name: Build
      run: cmake --build ${{github.workspace}}/build --config ${{env.BUILD_TYPE}} -j 4 -t tomato

    - name: Determine tag name
      id: tag
      shell: bash
      # taken from llama.cpp
      run: |
        SHORT_HASH="$(git rev-parse --short=7 HEAD)"
        if [[ "${{ env.BRANCH_NAME }}" == "master" ]]; then
          echo "name=dev-${SHORT_HASH}" >> $GITHUB_OUTPUT
        else
          SAFE_NAME=$(echo "${{ env.BRANCH_NAME }}" | tr '/' '-')
          echo "name=dev-${SAFE_NAME}-${SHORT_HASH}" >> $GITHUB_OUTPUT
        fi

    - name: Clean temporary artifacts
      # msvc sometimes produces .ilk files, which are used for linking only
      shell: powershell
      run: |
        Remove-Item "${{github.workspace}}/build/bin/*.ilk"

    - name: Compress artifacts
      shell: powershell
      run: |
        Compress-Archive -Path ${{github.workspace}}/build/bin/* -Destination ${{ github.event.repository.name }}-${{ steps.tag.outputs.name }}-${{ runner.os }}-msvc-asan-x86_64.zip

    - uses: actions/upload-artifact@v4
      with:
        name: ${{ github.event.repository.name }}-${{ steps.tag.outputs.name }}-${{ runner.os }}-msvc-asan-x86_64
        # TODO: do propper packing
        path: |
          ${{github.workspace}}/${{ github.event.repository.name }}-${{ steps.tag.outputs.name }}-${{ runner.os }}-msvc-asan-x86_64.zip

  dumpsyms:
    runs-on: ubuntu-latest

    needs:
      - linux-ubuntu
      - android
      - windows
      - windows-asan

    permissions:
      contents: write

    steps:
    - uses: actions/checkout@v4
      with:
        submodules: recursive

    - name: Determine tag name
      id: tag
      shell: bash
      # taken from llama.cpp
      run: |
        SHORT_HASH="$(git rev-parse --short=7 HEAD)"
        if [[ "${{ env.BRANCH_NAME }}" == "master" ]]; then
          echo "name=dev-${SHORT_HASH}" >> $GITHUB_OUTPUT
        else
          SAFE_NAME=$(echo "${{ env.BRANCH_NAME }}" | tr '/' '-')
          echo "name=dev-${SAFE_NAME}-${SHORT_HASH}" >> $GITHUB_OUTPUT
        fi

    - name: Install Dependencies
      run: sudo apt update && sudo apt -y install unar

    - name: Install dump_syms
      run: |
        curl --proto '=https' --tlsv1.2 -LsSf https://github.com/mozilla/dump_syms/releases/download/v2.3.4/dump_syms-installer.sh | sh

    - name: Download artifacts
      id: download-artifact
      uses: actions/download-artifact@v4
      with:
        path: ./artifacts/

    - name: Dump symbols
      shell: bash
      run: |
        mkdir ./artifacts/extract

        echo $(unar -D -o ./artifacts/extract/ ./artifacts/${{ github.event.repository.name }}-${{ steps.tag.outputs.name }}-Linux-ubuntu20.04-x86_64/*)
        dump_syms -s tomato-dev-ubuntu20.04-x86_64-sym ./artifacts/extract/tomato
        rm -rf ./artifacts/extract/*

        echo $(unar -D -o ./artifacts/extract/ ./artifacts/${{ github.event.repository.name }}-${{ steps.tag.outputs.name }}-Windows-msvc-x86_64/*)
        dump_syms -s tomato-dev-Windows-msvc-x86_64-sym ./artifacts/extract/tomato.pdb
        rm -rf ./artifacts/extract/*

    - name: Compress symbol stores
      shell: bash
      run: |
        tar -czvf ${{ github.event.repository.name }}-${{ steps.tag.outputs.name }}-Linux-ubuntu20.04-x86_64-symbol_store.tar.gz -C ./tomato-dev-ubuntu20.04-x86_64-sym .
        tar -czvf ${{ github.event.repository.name }}-${{ steps.tag.outputs.name }}-Windows-msvc-x86_64-symbol_store.tar.gz -C ./tomato-dev-Windows-msvc-x86_64-sym .

    - uses: actions/upload-artifact@v4
      with:
        name: ${{ github.event.repository.name }}-${{ steps.tag.outputs.name }}-symbol_stores
        path: |
          ${{ github.event.repository.name }}-${{ steps.tag.outputs.name }}-Linux-ubuntu20.04-x86_64-symbol_store.tar.gz
          ${{ github.event.repository.name }}-${{ steps.tag.outputs.name }}-Windows-msvc-x86_64-symbol_store.tar.gz

  release:
    if: ${{ ( github.event_name == 'push' && github.ref == 'refs/heads/master' ) }}

    runs-on: ubuntu-latest

    needs:
      - linux-ubuntu
      - android
      - windows
      - windows-asan
      - dumpsyms

    permissions:
      contents: write

    steps:
    - uses: actions/checkout@v4
      with:
        submodules: recursive

    - name: Determine tag name
      id: tag
      shell: bash
      # taken from llama.cpp
      run: |
        SHORT_HASH="$(git rev-parse --short=7 HEAD)"
        if [[ "${{ env.BRANCH_NAME }}" == "master" ]]; then
          echo "name=dev-${SHORT_HASH}" >> $GITHUB_OUTPUT
        else
          SAFE_NAME=$(echo "${{ env.BRANCH_NAME }}" | tr '/' '-')
          echo "name=dev-${SAFE_NAME}-${SHORT_HASH}" >> $GITHUB_OUTPUT
        fi

    - name: Download artifacts
      id: download-artifact
      uses: actions/download-artifact@v4
      with:
        path: ./artifacts/

    - name: Create release
      env:
        GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
        tag: ${{ steps.tag.outputs.name }}
      shell: bash
      run: |
        gh release create "$tag" \
            --repo="$GITHUB_REPOSITORY" \
            --title="${tag#v}" \
            --notes="preview build of the latest commit" \
            --prerelease

    - name: Upload artifacts
      env:
        GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
        tag: ${{ steps.tag.outputs.name }}
      shell: bash
      run: |
        ls -laR ./artifacts
        gh release upload "$tag" ./artifacts/*/* \
            --repo="$GITHUB_REPOSITORY"