name: CI
on:
  push:
    branches:
      - "*"
    tags:
      - "*"
  pull_request:

jobs:
  test:
    runs-on: ubuntu-latest
    steps:
      - name: Clone
        uses: actions/checkout@v4
      - name: Install dependencies
        run: |
          sudo apt update
          sudo apt install automake autoconf libtool build-essential patch perl libc-bin libunwind-dev
      - name: Install Go
        uses: actions/setup-go@v5
        with:
          go-version: 'stable'
      - name: Install pprof
        run: |
          go install github.com/google/pprof@latest
          echo "PATH=$HOME/go/bin:$PATH" >> $GITHUB_ENV
      - run: echo GPERFTOOLS_ENSURE_PACKAGE_VERSION=1 >> $GITHUB_ENV
      - name: Build and test
        run: |
          ./autogen.sh
          ./configure
          export NUMCPUS=`getconf _NPROCESSORS_ONLN`
          export MORECPUS="$(($NUMCPUS * 3))"
          printf "NUMCPUS=%d MORECPUS=%d\n" "$NUMCPUS" "$MORECPUS"
          make "-j${NUMCPUS}"
          make "-j${MORECPUS}" check || make "-j${MORECPUS}" recheck
          # A number of features in gperftools is optional. Lets make
          # sure that on a common x86/Linux platform all the optional
          # pieces are actually convigured and built. See
          # e1f821e56f80ad63d37407267e0022b9ca07a458 for the curious
          # example of how things could be subtly broken.
          #
          # getpc_test is proxy for libprofiler being enabled
          ./getpc_test
          # Ensure that debugallocator is present as well.
          ./tcmalloc_minimal_debug_unittest
          ./tcmalloc_debug_unittest
          # Verify that heap profiler is built and works.
          ./heap-profiler_unittest
      - name: Upload Test Logs
        uses: actions/upload-artifact@v4
        if: ${{ always() }}
        with:
          name: test-logs-${{ github.job }}
          retention-days: 7
          path: |
            *.log
  test-i386:
    runs-on: ubuntu-latest
    steps:
      - name: Clone
        uses: actions/checkout@v4
      - name: Install dependencies
        run: |
          sudo apt update
          sudo apt install automake autoconf libtool build-essential patch perl libc-bin g++-multilib
      - name: Install Go
        uses: actions/setup-go@v5
        with:
          go-version: 'stable'
      - name: Install pprof
        run: |
          go install github.com/google/pprof@latest
          echo "PATH=$HOME/go/bin:$PATH" >> $GITHUB_ENV
      - name: Configure ccache action
        uses: hendrikmuhs/ccache-action@v1.2
        with:
          key: ${{ github.job }}-${{ matrix.os }}
      - name: Add ccache to PATH
        run: echo "PATH=/usr/lib/ccache:$PATH" >> $GITHUB_ENV
      # when testing autotools-based build ensure that we check PACKAGE_VERSION matches versions in tcmalloc.h
      - run: echo GPERFTOOLS_ENSURE_PACKAGE_VERSION=1 >> $GITHUB_ENV
      - name: Build and test
        # GNU/Linux i386 currently fails with libgcc backtracer due to wrong unwind info in memcpy-ia32.S. So we force frame pointers.
        run: |
          ./autogen.sh
          ./configure CXX='g++ -m32' CC='gcc -m32' --enable-frame-pointers
          export NUMCPUS=`getconf _NPROCESSORS_ONLN`
          export MORECPUS="$(($NUMCPUS * 3))"
          printf "NUMCPUS=%d MORECPUS=%d\n" "$NUMCPUS" "$MORECPUS"
          make "-j${NUMCPUS}"
          make "-j${MORECPUS}" check || make "-j${MORECPUS}" recheck
          # Ensure that notable optional features are built and work.
          ./getpc_test
          ./tcmalloc_minimal_debug_unittest
          ./tcmalloc_debug_unittest
          ./heap-profiler_unittest
      - name: Upload Test Logs
        uses: actions/upload-artifact@v4
        if: ${{ always() }}
        with:
          name: test-logs-${{ github.job }}
          retention-days: 7
          path: |
            *.log
  test-cmake:
    strategy:
      matrix:
        os: [macos-latest, ubuntu-latest]
        build_type: [RelWithDebInfo, Debug]
    runs-on: ${{ matrix.os }}
    steps:
      - name: Clone
        uses: actions/checkout@v4
      - uses: seanmiddleditch/gha-setup-ninja@v5
      - name: Configure ccache action
        uses: hendrikmuhs/ccache-action@v1.2
        with:
          key: ${{ github.job }}-${{ matrix.os }}-${{ matrix.build_type }}
      - name: Add ccache to PATH
        run: echo "PATH=/usr/lib/ccache:$PATH" >> $GITHUB_ENV
      - name: Install Go
        uses: actions/setup-go@v5
        with:
          go-version: 'stable'
      - name: Install pprof
        run: |
          go install github.com/google/pprof@latest
          echo "PATH=$HOME/go/bin:$PATH" >> $GITHUB_ENV
          PATH=$HOME/go/bin:$PATH which -a pprof
      - name: mkdir build/
        run: cmake -E make_directory ${{runner.workspace}}/build
      - name: cmake setup
        working-directory: ${{runner.workspace}}/build
        run: cmake -DCMAKE_BUILD_TYPE=${{ matrix.build_type }} -G Ninja $GITHUB_WORKSPACE
      - name: cmake build
        working-directory: ${{runner.workspace}}/build
        run: ninja -v
      - name: test
        working-directory: ${{runner.workspace}}/build
        run: ctest -j12 --test-dir . || ctest -j12 --test-dir . --rerun-failed
      - name: Upload Test Logs
        uses: actions/upload-artifact@v4
        if: ${{ always() }}
        with:
          name: test-logs-${{ github.job }}-${{ matrix.os }}-${{ matrix.build_type }}
          retention-days: 7
          path: |
            ${{runner.workspace}}/build/Testing/Temporary/LastTest.log
  test-cmake-windows:
    strategy:
      matrix:
        os: [windows-latest]
        build_type: [RelWithDebInfo, Debug]
    runs-on: ${{ matrix.os }}
    steps:
      - name: Clone
        uses: actions/checkout@v4
      - uses: seanmiddleditch/gha-setup-ninja@v5
      - name: mkdir build/
        run: cmake -E make_directory ${{runner.workspace}}/build
      - name: cmake setup
        working-directory: ${{runner.workspace}}/build
        shell: bash
        run: |
          ruby $GITHUB_WORKSPACE/.github/workflows/scripts/msvc-env.rb cmake -DCMAKE_BUILD_TYPE=${{ matrix.build_type }} -G Ninja -DGPERFTOOLS_BUILD_HEAP_PROFILER=OFF -Dgperftools_build_minimal=ON $GITHUB_WORKSPACE
      - name: cmake build
        working-directory: ${{runner.workspace}}/build
        shell: bash
        run: ruby $GITHUB_WORKSPACE/.github/workflows/scripts/msvc-env.rb ninja -v
      - name: test
        working-directory: ${{runner.workspace}}/build
        env:
          CTEST_PARALLEL_LEVEL: 12
        run: ninja -v test
      - name: Upload Test Logs
        uses: actions/upload-artifact@v4
        if: ${{ always() }}
        with:
          name: test-logs-${{ github.job }}-${{ matrix.os }}-${{ matrix.build_type }}
          retention-days: 7
          path: |
            ${{runner.workspace}}/build/Testing/Temporary/LastTest.log
