name: QB64-PE Build Process

on:
  workflow_call:

jobs:
  Build:
    strategy:
      fail-fast: false
      matrix:
        include:
          - os: ubuntu-24.04
            platform: x64
            prefix: lnx
          - os: macos-15-intel
            platform: x64
            prefix: osx
          - os: windows-2022
            platform: x64
            prefix: win
          - os: windows-2022
            platform: x86
            prefix: win
          - os: windows-11-arm
            platform: arm64
            prefix: win

    runs-on: ${{ matrix.os }}

    env:
      PLATFORM: ${{ matrix.platform }}

    steps:
    - uses: actions/checkout@v6
      with:
        ref: ${{ github.event.pull_request.head.sha }}
        repository: ${{ github.event.pull_request.head.repo.full_name }}
        allow-unsafe-pr-checkout: true
        fetch-depth: 0

    - uses: maxim-lobanov/setup-xcode@v1
      if: ${{ matrix.prefix == 'osx' }}
      with:
        xcode-version: '16.4' # macOS 15 default

      # A deploy key is setup so that the potential push of ./internal/source can
      # trigger a new build. Care is taken to make sure loops cannot happen.
    - uses: webfactory/ssh-agent@v0.10.0
      if: ${{ github.event_name == 'push' && matrix.prefix == 'lnx' }}
      with:
        ssh-private-key: ${{ secrets.ACTION_DEPLOY_KEY }}

    - name: Install dependencies
      if: ${{ matrix.prefix == 'lnx' }}
      run: sudo apt update && sudo apt install build-essential x11-utils mesa-common-dev libglu1-mesa-dev libasound2-dev libpng-dev pulseaudio dbus-x11 libportaudio2 libcurl4-openssl-dev libx11-dev libxcursor-dev libxrandr-dev libxinerama-dev libxi-dev

      # Pulseaudio puts a dummy ALSA device in place, which allows us to do
      # audio testing on Linux
    - name: Start Pulseaudio
      if: ${{ matrix.prefix == 'lnx' }}
      run: dbus-launch pulseaudio --start

    - name: Calculate Version
      shell: bash
      run: .ci/calculate_version.sh

    - name: Read version for artifacts
      shell: bash
      run: echo "version=$(.ci/read-version.sh)" >> $GITHUB_ENV

    - name: Bootstrap compiler Linux/OSX
      if: ${{ matrix.prefix == 'lnx' || matrix.prefix  == 'osx' }}
      run: .ci/bootstrap.sh ${{ matrix.prefix }}

    - name: Bootstrap compiler Windows
      if: ${{ matrix.prefix == 'win' }}
      env:
        GITHUB_TOKEN: ${{ github.token }}
      run: .ci/bootstrap.bat

    - name: Compile Linux/OSX
      if: ${{ matrix.prefix == 'lnx' || matrix.prefix == 'osx' }}
      run: .ci/compile.sh ${{ matrix.prefix }}

    - name: Compile Windows
      if: ${{ matrix.prefix == 'win' }}
      run: .ci/compile.bat

    - name: Test libqb
      shell: bash
      run: tests/run_c_tests.sh

    - name: Print QB64-PE Version
      run: ./qb64pe -?

    # Note we can use a short wait time for whitelist propagation throughout the
    # Cloudflare network, as we only need whitelisting for the 'Create Atrifact'
    # step. Before that the 'Testing' step is running for approx. 20-30 minutes,
    # which should be more than enough time for the propagation.
    - name: Whitelist runner IP at Cloudflare
      id: whitelist
      uses: DiyRex/gh-cf-ip-whitelist@v1
      with:
        api-token: ${{ secrets.CLOUDFLARE_API_TOKEN }}
        zone-id: ${{ secrets.CLOUDFLARE_ZONE_ID }}
        action: 'create'
        wait-time: '5'

    - name: Testing
      timeout-minutes: 105
      shell: bash
      env:
        CI_TESTING: y
        CI_OS: ${{ matrix.prefix }}
      run: |
        echo "pcm.!default { type null }" > $GITHUB_WORKSPACE/silence-alsa.conf
        export ALSA_CONFIG_PATH=$GITHUB_WORKSPACE/silence-alsa.conf
        tests/run_tests.sh

    - name: Create QB64-PE Artifact
      timeout-minutes: 45
      shell: bash
      run: .ci/make-dist.sh ${{ matrix.prefix }} "${{ env.version }}"

      # Note that compiling programs in dist does modify the ./dist and make it
      # dirty, but that's ok because we've already create the .7z or .tar.gz
      # artifacts in the previous step
    - name: Test QB64-PE Artifact
      shell: bash
      run: tests/run_dist_tests.sh ./dist/qb64pe ${{ matrix.prefix }}

      # Only update ./internal/source if we're building on Linux, building on
      # the main branch, and building a Pull request merge. Otherwise the repo
      # is left alone.
    - name: Update ./internal/source
      if: ${{ github.event_name == 'push' &&  matrix.prefix == 'lnx' && github.ref == 'refs/heads/main' && contains(github.event.head_commit.message, 'Merge pull request') }}
      run: .ci/push-internal-source.sh

    - uses: actions/upload-artifact@v6
      if: always()
      with:
        name: qb64pe-${{ matrix.prefix }}${{ matrix.prefix == 'win' && format('-{0}', matrix.platform) || '' }}-${{ env.version }}
        path: |
          tests/results/
          qb64pe_win-x86*.7z
          qb64pe_win-x64*.7z
          qb64pe_win-arm64*.7z
          qb64pe_lnx*.tar.gz
          qb64pe_osx*.tar.gz

    - name: Create release
      if: startsWith(github.ref, 'refs/tags/v')
      uses: softprops/action-gh-release@v1
      with:
        draft: true
        files: |
          qb64pe_win-x86*.7z
          qb64pe_win-x64*.7z
          qb64pe_win-arm64*.7z
          qb64pe_lnx*.tar.gz
          qb64pe_osx*.tar.gz
      env:
        GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}

    - name: Remove Cloudflare IP whitelisting
      if: always() || cancelled()
      uses: DiyRex/gh-cf-ip-whitelist@v1
      with:
        api-token: ${{ secrets.CLOUDFLARE_API_TOKEN }}
        zone-id: ${{ secrets.CLOUDFLARE_ZONE_ID }}
        action: 'delete'
        rule-id: ${{ steps.whitelist.outputs.rule-id }}
