name: linux

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

env:
  CPM_SOURCE_CACHE: ${{ github.workspace }}/cpm-cache

jobs:
  build-linux:
    runs-on: ubuntu-latest
    container: archlinux:base-devel
    steps:
    - name: Install dependencies
      run: pacman -Syu --noconfirm && pacman -S --noconfirm --needed freetype2 debuginfod wayland dbus libxkbcommon libglvnd meson cmake git wayland-protocols nodejs lua
    - name: Trust git repo
      run: git config --global --add safe.directory '*'
    - uses: actions/checkout@v4
    - name: Cache CPM packages
      uses: actions/cache@v4
      with:
        path: ${{ env.CPM_SOURCE_CACHE }}
        key: ${{ runner.os }}-cpm-${{ hashFiles('**/vendor.cmake', '**/CMakeLists.txt') }}
        restore-keys: ${{ runner.os }}-cpm-
    - name: Profiler GUI
      run: |
        cmake -B profiler/build -S profiler -DCMAKE_BUILD_TYPE=Release -DGIT_REV=${{ github.sha }}
        if [ "${ACT:-}" != "true" ] && [ "${FORGEJO_ACTIONS:-}" != "true" ]; then
          cmake --build profiler/build
        else
          cmake --build profiler/build --parallel 2
        fi
    - name: Update utility
      run: |
        cmake -B update/build -S update -DCMAKE_BUILD_TYPE=Release -DGIT_REV=${{ github.sha }}
        cmake --build update/build --parallel
    - name: Capture utility
      run: |
        cmake -B capture/build -S capture -DCMAKE_BUILD_TYPE=Release -DGIT_REV=${{ github.sha }}
        cmake --build capture/build --parallel
    - name: Csvexport utility
      run: |
        cmake -B csvexport/build -S csvexport -DCMAKE_BUILD_TYPE=Release -DGIT_REV=${{ github.sha }}
        cmake --build csvexport/build --parallel
    - name: Import utilities
      run: |
        cmake -B import/build -S import -DCMAKE_BUILD_TYPE=Release -DGIT_REV=${{ github.sha }}
        cmake --build import/build --parallel
    - name: Merge utility
      run: |
        cmake -B merge/build -S merge -DCMAKE_BUILD_TYPE=Release -DGIT_REV=${{ github.sha }}
        cmake --build merge/build --parallel
    - name: Library (cmake)
      run: |
        cmake -B build -DCMAKE_BUILD_TYPE=Release -DTRACY_ENABLE=ON
        cmake --build build
        cmake --install build
      env:
        CMAKE_INSTALL_PREFIX: ${{ github.workspace }}/bin
    - name: Library (meson)
      run: |
        meson setup -Dprefix=$GITHUB_WORKSPACE/bin/lib -Dtracy_enable=true build-meson
        meson compile -C build-meson
    - name: Test application
      uses: ./.github/actions/test-tracy
    - name: Find Artifacts
      id: find_artifacts
      run: |
        mkdir -p bin
        cp profiler/build/tracy-profiler bin
        cp update/build/tracy-update bin
        cp capture/build/tracy-capture bin
        cp csvexport/build/tracy-csvexport bin
        cp import/build/tracy-import-chrome bin
        cp import/build/tracy-import-fuchsia bin
        cp merge/build/tracy-merge bin
        strip bin/tracy-*
    - uses: actions/upload-artifact@v4
      with:
        name: arch-linux
        path: bin
