name: Compile
on:
  - pull_request
  - push

concurrency:
  group: ${{ github.workflow }}-${{ github.event.pull_request.number || github.ref }}
  cancel-in-progress: true

permissions: 
  actions: read
  packages: read
  contents: read

jobs:
  compile:
    runs-on: ubuntu-latest
    strategy:
      fail-fast: false
      matrix:
        name:
          - GCC
          - GCC Strict
          - GCC Static Analyzer
          - Clang
          - Clang -Weverything

        include:
          - name: GCC
            image: quay.io/389ds/ci-images:fedora
            compiler: gcc
            cpp-compiler: g++
            cflags:  "-O2 -g"

          - name: GCC strict
            image: quay.io/389ds/ci-images:fedora
            compiler: gcc
            cpp-compiler: g++
            cflags:  "-O2 -g -Wall -Wextra -Wundef -Wpointer-arith -Wfloat-equal \
            -Wstrict-prototypes -Wstrict-overflow=5 -Wwrite-strings -Winit-self \
            -Wuninitialized -Wno-sign-compare -Wshadow -Wformat-security"

          - name: GCC Static Analyzer
            image: quay.io/389ds/ci-images:fedora
            compiler: gcc
            cpp-compiler: g++
            cflags:  "-O2 -g -fanalyzer"

          - name: Clang
            image: quay.io/389ds/ci-images:fedora
            compiler: clang
            cpp-compiler: clang++
            cflags: "-O2 -g -Qunused-arguments"

          - name: Clang -Weverything
            image: quay.io/389ds/ci-images:fedora
            compiler: clang
            cpp-compiler: clang++
            cflags: "-O2 -g -Weverything -Qunused-arguments"

    container:
      image: ${{ matrix.image }}

    steps:
      - uses: actions/checkout@v4
      - name: Checkout and configure
        run: autoreconf -fvi && ./configure
        env:
          CC: ${{ matrix.compiler }}
          CXX: ${{ matrix.cpp-compiler }}
          CFLAGS: ${{ matrix.cflags || env.CFLAGS }}
          CXXFLAGS: ${{ matrix.cxxflags || env.CXXFLAGS }}
          LDFLAGS: ${{ matrix.ldflags || env.LDFLAGS }}

      - uses: ammaraskar/gcc-problem-matcher@master
      - name: Build using ${{ matrix.compiler }}
        run: bash -c "(make V=0 2> >(tee /dev/stderr)) > log.txt"

      - uses: actions/upload-artifact@v4
        with:
          name: ${{ matrix.name }}
          path: log.txt
