name: CI

on:
  push:
    branches:
      - main
  pull_request:
    branches:
      - main

permissions:
  contents: read
  pull-requests: write

jobs:
  build:
    strategy:
      matrix:
        include:
        - os: ubuntu-latest
        - os: ubuntu-latest
          cxxflags: -std=c++11
        - os: macos-latest
      fail-fast: false

    runs-on: ${{ matrix.os }}

    steps:
    - name: deps [macOS]
      if: runner.os == 'macOS'
      run: |
        export HOMEBREW_NO_INSTALL_CLEANUP=1
        brew update
        brew upgrade || true
        brew uninstall --ignore-dependencies --force pkg-config@0.29.2
        brew install \
          autoconf \
          automake \
          libtool \
          pkgconf \
          coreutils \
          gnu-sed
    - uses: actions/checkout@v7
      with:
        submodules: true
    - name: bootstrap
      run: autoreconf -vfi
    - name: configure
      run: ./configure
    - name: make distcheck
      run: |
        [[ -n "${{ matrix.cxxflags }}" ]] && export DISTCHECK_CONFIGURE_FLAGS='CXXFLAGS="${CXXFLAGS} ${{ matrix.cxxflags }}"'
        if [ '${{ runner.os }}' == 'macOS' ]; then
          # Assumes `sed` is GNU sed:
          # sed: illegal option -- r
          export PATH="$(brew --prefix)/opt/gnu-sed/libexec/gnubin:$PATH"
        fi
        make distcheck

  build-mingw:
    runs-on: windows-latest
    defaults:
      run:
        shell: msys2 {0}

    steps:
    - uses: msys2/setup-msys2@v2
      with:
        msystem: UCRT64
        update: true
        install: >-
          git
          zip
          upx
          base-devel
        pacboy: >-
          toolchain:p
          autotools:p
    - uses: actions/checkout@v7
      with:
        submodules: true
    - name: build
      run: |
        export LDFLAGS="-s -Wl,-no-undefined -Wl,-O1 -static"
        export CXXFLAGS="-O2 -march=x86-64 -fomit-frame-pointer -pipe -DUNICODE -D_UNICODE"
        export PKG_CONFIG="pkg-config --static"
        autoreconf -vfi
        ./configure --disable-shared --enable-static --with-simd=runtime
        make distcheck
