name: build

on:
  push:
  pull_request:

jobs:
  ubuntu64:
    runs-on: ubuntu-24.04
    steps:
    - uses: actions/checkout@v4
      with:
        fetch-depth: 1
        submodules: recursive
    - name: apt-update
      run: sudo apt-get update
    - name: apt-install
      run: sudo apt-get install -qq -y --no-install-recommends gcc g++ libsnappy-dev zlib1g-dev libpng-dev libbrotli-dev libdwarf-dev libgtest-dev qtbase5-dev qtdeclarative5-dev python3 ninja-build cmake
    - name: cmake
      run: cmake -S. -Bbuild -G Ninja -DENABLE_STATIC_SNAPPY=0 -DENABLE_GUI=1
    - name: build
      run: cmake --build build --use-stderr
    - name: check
      run: cmake --build build --use-stderr --target check
    - name: package
      run: cmake --build build --use-stderr --target package

  ubuntu32:
    runs-on: ubuntu-latest
    steps:
    - uses: actions/checkout@v4
      with:
        fetch-depth: 1
        submodules: recursive
    - run: echo ${{ secrets.GITHUB_TOKEN }} | docker login docker.pkg.github.com -u $GITHUB_ACTOR --password-stdin
    - run: ci/build-ubuntu32.sh

  ubuntu64-clang:
    runs-on: ubuntu-24.04
    steps:
    - uses: actions/checkout@v4
      with:
        fetch-depth: 1
        submodules: recursive
    - name: apt-update
      run: sudo apt-get update
    - name: apt-install
      run: sudo apt-get install -qq -y --no-install-recommends clang libc++-dev libc++abi-dev zlib1g-dev libdwarf-dev qtbase5-dev qtdeclarative5-dev python3 ninja-build cmake
    - name: cmake
      run: cmake -S. -Bbuild -G Ninja -DCMAKE_C_COMPILER=clang -DCMAKE_CXX_COMPILER=clang++ -DENABLE_GUI=1
    - name: build
      run: cmake --build build --use-stderr
    - name: check
      run: cmake --build build --use-stderr --target check

  ubuntu-20_04:
    runs-on: ubuntu-24.04
    env:
      PACKAGE: ${{ github.event_name == 'push' && (github.ref == 'refs/heads/master' || startsWith(github.ref, 'refs/tags/')) }}
    steps:
    - uses: actions/checkout@v4
      with:
        fetch-depth: 1
        submodules: recursive
    - run: echo ${{ secrets.GITHUB_TOKEN }} | docker login docker.pkg.github.com -u $GITHUB_ACTOR --password-stdin
    - run: ci/build-ubuntu-20.04.sh
    - if: env.PACKAGE == 'true'
      uses: actions/upload-artifact@v4
      with:
        name: apitrace-ubuntu-20.04
        path: build/docker-ubuntu-20.04/apitrace-*.tar.bz2
        if-no-files-found: error

  ubuntu-arm64-cross:
    runs-on: ubuntu-24.04
    env:
      PACKAGE: ${{ github.event_name == 'push' && (github.ref == 'refs/heads/master' || startsWith(github.ref, 'refs/tags/')) }}
    steps:
    - uses: actions/checkout@v4
      with:
        fetch-depth: 1
        submodules: recursive
    - run: echo ${{ secrets.GITHUB_TOKEN }} | docker login docker.pkg.github.com -u $GITHUB_ACTOR --password-stdin
    - run: ci/build-ubuntu-arm64-cross.sh
    - if: env.PACKAGE == 'true'
      uses: actions/upload-artifact@v4
      with:
        name: apitrace-ubuntu-arm64
        path: build/docker-ubuntu-arm64-cross/apitrace-*.tar.bz2
        if-no-files-found: error

  mingw32:
    runs-on: ubuntu-24.04
    steps:
    - uses: actions/checkout@v4
      with:
        fetch-depth: 1
        submodules: recursive
    - run: sudo apt-get update
    - run: sudo apt-get install -qq -y --no-install-recommends g++-mingw-w64-i686 ninja-build cmake
    - run: wget -q -O - https://github.com/apitrace/dxsdk/archive/master.tar.gz | tar -xz
    - run: ci/build-mingw.sh mingw32

  msvc:
    runs-on: windows-2022
    strategy:
      matrix:
        arch: [x86, arm]
        target: [win32, win64]
        exclude:
          # https://learn.microsoft.com/en-us/windows/arm/arm32-to-arm64
          - arch: arm
            target: win32
    steps:
    - uses: actions/checkout@v4
      with:
        fetch-depth: 1
        submodules: recursive
    - name: Cache Downloads
      id: cache-downloads
      uses: actions/cache@v4
      with:
        path: |
          *qtbase-*.7z
          *qttools-*.7z
        key: ${{ runner.os }}-${{ matrix.target }}-${{ matrix.arch }}-5.15.2-1
    - name: Build
      run: .\ci\build-msvc.ps1 -arch ${{ matrix.arch }} -target ${{ matrix.target }}
    - name: Upload artifacts
      uses: actions/upload-artifact@v4
      with:
        name: apitrace-${{ matrix.target }}-${{ matrix.arch }}
        path: build/apitrace-*.7z
        if-no-files-found: error

  mac64:
    runs-on: macos-latest
    steps:
    - uses: actions/checkout@v4
      with:
        fetch-depth: 1
        submodules: recursive
    - name: cmake
      run: ci/build-macos.sh

  release:
    if: github.event_name == 'push' && startsWith(github.ref, 'refs/tags/')
    needs: [ubuntu-20_04, ubuntu-arm64-cross, msvc]
    runs-on: ubuntu-latest
    steps:
    - uses: actions/checkout@v4
      with:
        fetch-depth: 1
        submodules: recursive
    - uses: actions/download-artifact@v4
      id: download
      with:
        path: artifacts
    - run: ls -lR ${{ steps.download.outputs.download-path }}
    - name: Create Release
      env:
        GH_TOKEN: ${{ secrets.GITHUB_TOKEN }}
      run: >
        gh release create --draft --title "${{ github.ref_name }}" --generate-notes "${{ github.ref_name }}"
        ${{ steps.download.outputs.download-path }}/*/apitrace-*.tar.bz2
        ${{ steps.download.outputs.download-path }}/*/apitrace-*.7z
