name: macOS CI
on:
  pull_request:
  push:
  schedule:
    - cron: '0 0 * * 0'
jobs:
  build:
    if: "!contains(github.event.head_commit.message, '[skip macos ci]')"
    runs-on: macos-latest
    env:
      HOMEBREW_NO_AUTO_UPDATE: 1
      HOMEBREW_NO_INSTALL_CLEANUP: 1
      HOMEBREW_NO_INSTALLED_DEPENDENTS_CHECK: 1
      TEST_BUILD_MODULES: argon2 geo_maxmind ldap mysql pgsql regex_pcre regex_pcre2 regex_posix regex_re2 regex_stdlib regex_tre sqlite3 ssl_gnutls ssl_mbedtls ssl_openssl sslrehashsignal
    steps:
      - uses: actions/checkout@v4

      - name: Prepare the build environment
        run: |
          echo "CPPFLAGS=-I$(brew --prefix)/include" >> $GITHUB_ENV
          echo "LDFLAGS=-L$(brew --prefix)/lib" >> $GITHUB_ENV

      - name: Install dependencies
        run: |
          brew update || true
          for PACKAGE in pkg-config argon2 gnutls libmaxminddb libpq mbedtls mysql-client openssl openldap pcre pcre2 re2 sqlite tre;
          do
            brew install $PACKAGE || brew upgrade $PACKAGE

            BREW_PREFIX=$(brew --prefix $PACKAGE)
            if [ -d "$BREW_PREFIX/bin" ]
            then
              export PATH="$PATH:$BREW_PREFIX/bin"
            fi
            if [ -d "$BREW_PREFIX/lib/pkgconfig" ]
            then
              export PKG_CONFIG_PATH="$PKG_CONFIG_PATH:$BREW_PREFIX/lib/pkgconfig"
            fi
          done
          echo "PATH=$PATH" >> $GITHUB_ENV
          echo "PKG_CONFIG_PATH=$PKG_CONFIG_PATH" >> $GITHUB_ENV

      - name: Run test-build
        run: ./tools/test-build c++
