name: 'Linux (Cross-compile)'

on:
  push:
    branches:
      - 'master'
  pull_request:
    types: [ opened, synchronize ]
  workflow_dispatch:

concurrency: 
  group: ${{ github.workflow }}-${{ github.ref }}
  cancel-in-progress: true

jobs:
  Build:
    strategy:
      fail-fast: false
      matrix:
        include:
        - BuildType: Release
          Platform: aarch64
          Backend: GLFW
          CC: gcc
          CXX: g++
          
        - BuildType: Release
          Platform: aarch64
          Backend: SDL2
          CC: gcc
          CXX: g++

    runs-on: 'ubuntu-22.04'
    #runs-on: 'ubuntu-22.04-arm'

    steps:
    - name: 'Checkout Repository'
      uses: actions/checkout@v4
      with:
        fetch-depth: 0

    - name: 'Configure Cross-Compile Support (ARM64)'
      uses: cyberjunk/gha-ubuntu-cross@v4
      with:
        arch: arm64

    - name: 'Create Build Environment'
      run: |
        sudo apt install -y cmake curl
        sudo apt install -y libgl1-mesa-dev:arm64 libglfw3-dev:arm64 libsdl2-dev:arm64 libopenal-dev:arm64 libopenmpt-dev:arm64 libcurl4-openssl-dev:arm64 zlib1g:arm64
        #sudo apt install -y libgl1-mesa-dev libglfw3-dev libsdl2-dev libopenal-dev libopenmpt-dev libcurl4-openssl-dev zlib1g

    - name: 'Configure CMake'
      run: |
        export CC=aarch64-linux-gnu-${{ matrix.CC }}
        export CXX=aarch64-linux-gnu-${{ matrix.CXX }}
        #export CC=${{ matrix.CC }}
        #export CXX=${{ matrix.CXX }}
        
        rm -f ./Content/Translations/*.po
        
        cmake -B ./_build/ -D CMAKE_BUILD_TYPE=${{ matrix.BuildType }} -D CMAKE_SYSTEM_NAME=Linux -D CMAKE_SYSTEM_PROCESSOR=aarch64 -D CMAKE_FIND_ROOT_PATH=/usr/aarch64-linux-gnu -D NCINE_STRIP_BINARIES=ON -D NCINE_PREFERRED_BACKEND=${{ matrix.Backend }} -D NCINE_WITH_GLEW=OFF
        #cmake -B ./_build/ -D CMAKE_BUILD_TYPE=${{ matrix.BuildType }} -D NCINE_STRIP_BINARIES=ON -D NCINE_PREFERRED_BACKEND=${{ matrix.Backend }} -D NCINE_WITH_GLEW=OFF

    - name: 'Build'
      run: |
        make -j $(nproc) -C ./_build/

    - name: 'Create Package'
      run: |
        mkdir ./_package/
        cp -f ./_build/jazz2 ./_package/jazz2
        cp -f -r ./Content/ ./_package/Content/
        cp -f ./LICENSE ./_package/LICENSE
        
        case "${{ matrix.CC }}" in
          gcc ) artifactPath="Jazz2_Linux_${{ matrix.Platform }}_${{ matrix.Backend }}" ;;
          clang ) artifactPath="Jazz2_Linux_${{ matrix.Platform }}_${{ matrix.Backend }}_Clang" ;;
          * ) artifactPath="Jazz2_Linux_${{ matrix.Platform }}_${{ matrix.Backend }}_${{ matrix.CC }}" ;;
        esac
        echo "artifactPath=$artifactPath" >> $GITHUB_ENV
        
    - name: 'Upload Package'
      uses: actions/upload-artifact@v4
      with:
        name: ${{ env.artifactPath }}
        path: ./_package/