name: build

on:
  push:
    branches: [main]
  pull_request:
    branches: [main]

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

jobs:
  build:
    name: ${{ matrix.os }} / ${{ matrix.compiler }} / ${{ matrix.build_type }}
    runs-on: ${{ matrix.os }}
    strategy:
      fail-fast: false
      matrix:
        os: [ubuntu-24.04]
        compiler: [gcc, clang]
        build_type: [Debug, Release]

    steps:
      - uses: actions/checkout@v5

      - name: Add PostgreSQL apt repository (pgdg)
        run: |
          sudo apt-get update
          sudo apt-get install -y curl ca-certificates lsb-release
          sudo install -d /usr/share/postgresql-common/pgdg
          sudo curl -fsSL -o /usr/share/postgresql-common/pgdg/apt.postgresql.org.asc \
            https://www.postgresql.org/media/keys/ACCC4CF8.asc
          echo "deb [signed-by=/usr/share/postgresql-common/pgdg/apt.postgresql.org.asc] https://apt.postgresql.org/pub/repos/apt $(lsb_release -cs)-pgdg main" \
            | sudo tee /etc/apt/sources.list.d/pgdg.list
          sudo apt-get update

      - name: Install build dependencies
        run: |
          set -eux
          sudo apt-get install -y \
            postgresql-server-dev-18 \
            libxerces-c-dev \
            cmake \
            ninja-build
          if [ "${{ matrix.compiler }}" = "clang" ]; then
            sudo apt-get install -y clang
          fi

      - name: Show toolchain versions
        run: |
          set -eux
          /usr/lib/postgresql/18/bin/pg_config --version
          cmake --version
          ninja --version
          if [ "${{ matrix.compiler }}" = "clang" ]; then
            clang --version
          else
            gcc --version
          fi

      - name: Configure
        env:
          CC: ${{ matrix.compiler == 'clang' && 'clang' || 'gcc' }}
          CXX: ${{ matrix.compiler == 'clang' && 'clang++' || 'g++' }}
        run: |
          cmake -S . -B build -G Ninja \
            -DCMAKE_BUILD_TYPE=${{ matrix.build_type }} \
            -DPG_CONFIG=/usr/lib/postgresql/18/bin/pg_config

      - name: Build
        run: cmake --build build -j "$(nproc)"

  build-centos:
    name: centos-stream9 / gcc / Debug + tests
    runs-on: ubuntu-latest
    container:
      image: quay.io/centos/centos:stream9

    steps:
      - name: Install OS packages
        run: |
          set -eux
          # ninja-build lives in CRB (CodeReady Builder), perl-IPC-Run in EPEL.
          dnf -y install dnf-plugins-core epel-release
          dnf -y config-manager --set-enabled crb
          dnf -qy module disable postgresql
          dnf -y install \
            https://download.postgresql.org/pub/repos/yum/reporpms/EL-9-x86_64/pgdg-redhat-repo-latest.noarch.rpm
          dnf -y install \
            postgresql18 postgresql18-server postgresql18-devel \
            xerces-c-devel \
            cmake ninja-build gcc-c++ \
            git diffutils \
            perl perl-IPC-Run

      - uses: actions/checkout@v5

      - name: Show toolchain versions
        run: |
          set -eux
          /usr/pgsql-18/bin/pg_config --version
          cmake --version
          ninja --version
          gcc --version

      - name: Configure
        run: |
          cmake -S . -B build -G Ninja \
            -DCMAKE_BUILD_TYPE=Debug \
            -DPG_CONFIG=/usr/pgsql-18/bin/pg_config

      - name: Build
        run: cmake --build build -j "$(nproc)"

      - name: Install
        run: cmake --install build

      - name: Run ORCA tests (--orca-tests)
        # pg_regress's --temp-instance calls initdb, which refuses to run as
        # root. Create a regular user, hand over the workspace, and re-exec.
        run: |
          set -eux
          useradd -m runner
          chown -R runner:runner "$GITHUB_WORKSPACE"
          runuser -u runner -- bash -c '
            set -eux
            export PATH=/usr/pgsql-18/bin:$PATH
            cd "$GITHUB_WORKSPACE"
            test/test.sh --orca-tests
          '

      - name: Upload test artifacts on failure
        if: failure()
        uses: actions/upload-artifact@v4
        with:
          name: centos-test-results
          path: |
            build/test_parallel/
            build/tmp_check_parallel/log/

  build-rocky:
    name: rockylinux:9 / gcc / Debug
    runs-on: ubuntu-latest
    container:
      image: rockylinux:9

    steps:
      - name: Install OS packages
        run: |
          set -eux
          dnf -y install dnf-plugins-core epel-release
          dnf -y config-manager --set-enabled crb
          dnf -qy module disable postgresql
          dnf -y install \
            https://download.postgresql.org/pub/repos/yum/reporpms/EL-9-x86_64/pgdg-redhat-repo-latest.noarch.rpm
          dnf -y install \
            postgresql18-devel \
            xerces-c-devel \
            cmake ninja-build gcc-c++ \
            git

      - uses: actions/checkout@v5

      - name: Show toolchain versions
        run: |
          set -eux
          /usr/pgsql-18/bin/pg_config --version
          cmake --version
          ninja --version
          gcc --version

      - name: Configure
        run: |
          cmake -S . -B build -G Ninja \
            -DCMAKE_BUILD_TYPE=Debug \
            -DPG_CONFIG=/usr/pgsql-18/bin/pg_config

      - name: Build
        run: cmake --build build -j "$(nproc)"

  build-arm64:
    name: ubuntu-24.04-arm / gcc / Debug
    runs-on: ubuntu-24.04-arm

    steps:
      - uses: actions/checkout@v5

      - name: Add PostgreSQL apt repository (pgdg)
        run: |
          sudo apt-get update
          sudo apt-get install -y curl ca-certificates lsb-release
          sudo install -d /usr/share/postgresql-common/pgdg
          sudo curl -fsSL -o /usr/share/postgresql-common/pgdg/apt.postgresql.org.asc \
            https://www.postgresql.org/media/keys/ACCC4CF8.asc
          echo "deb [signed-by=/usr/share/postgresql-common/pgdg/apt.postgresql.org.asc] https://apt.postgresql.org/pub/repos/apt $(lsb_release -cs)-pgdg main" \
            | sudo tee /etc/apt/sources.list.d/pgdg.list
          sudo apt-get update

      - name: Install build dependencies
        run: |
          set -eux
          sudo apt-get install -y \
            postgresql-server-dev-18 \
            libxerces-c-dev \
            cmake \
            ninja-build

      - name: Show toolchain versions
        run: |
          set -eux
          uname -m
          /usr/lib/postgresql/18/bin/pg_config --version
          cmake --version
          ninja --version
          gcc --version

      - name: Configure
        run: |
          cmake -S . -B build -G Ninja \
            -DCMAKE_BUILD_TYPE=Debug \
            -DPG_CONFIG=/usr/lib/postgresql/18/bin/pg_config

      - name: Build
        run: cmake --build build -j "$(nproc)"

  build-macos:
    name: macos-latest / clang / Debug
    runs-on: macos-latest

    steps:
      - uses: actions/checkout@v5

      - name: Install build dependencies
        run: |
          set -eux
          brew update
          brew install postgresql@18 xerces-c cmake ninja icu4c

      - name: Show toolchain versions
        run: |
          set -eux
          "$(brew --prefix postgresql@18)/bin/pg_config" --version
          cmake --version
          ninja --version
          clang --version

      - name: Configure
        # Homebrew's postgresql@18 server headers include <unicode/ucol.h>,
        # which lives in keg-only icu4c.  Add icu4c's include/lib paths so
        # clang finds the ICU headers and the linker sees libicu*.
        run: |
          ICU_PREFIX="$(brew --prefix icu4c)"
          export CPPFLAGS="-I${ICU_PREFIX}/include ${CPPFLAGS:-}"
          export LDFLAGS="-L${ICU_PREFIX}/lib ${LDFLAGS:-}"
          export PKG_CONFIG_PATH="${ICU_PREFIX}/lib/pkgconfig:${PKG_CONFIG_PATH:-}"
          cmake -S . -B build -G Ninja \
            -DCMAKE_BUILD_TYPE=Debug \
            -DPG_CONFIG="$(brew --prefix postgresql@18)/bin/pg_config"

      - name: Build
        run: cmake --build build -j "$(sysctl -n hw.ncpu)"
