- name: Install dependencies
  env:
    # For compatibility with macOS 10.13
    ZLIB_VERSION: 1.3.1
    MBEDTLS_VERSION: 2.28.5
    PCRE2_VERSION: 10.42
  run: |
    set -ex
    brew update
    brew bundle --file=tests/Brewfile --no-upgrade
    curl -L https://github.com/madler/zlib/releases/download/v$ZLIB_VERSION/zlib-$ZLIB_VERSION.tar.gz | tar xz
    cd zlib-$ZLIB_VERSION
    ./configure
    sudo make && sudo make install
    cd ..
    curl -L https://github.com/ARMmbed/mbedtls/archive/v$MBEDTLS_VERSION.tar.gz | tar xz
    cd mbedtls-$MBEDTLS_VERSION
    sudo make && sudo make install
    cd ..
    curl -L https://github.com/PCRE2Project/pcre2/releases/download/pcre2-$PCRE2_VERSION/pcre2-$PCRE2_VERSION.tar.gz | tar xz
    cd pcre2-$PCRE2_VERSION
    ./configure --enable-unicode --enable-pcre2-8 --enable-pcre2-16 --enable-pcre2-32 --enable-unicode-properties --enable-pcre2grep-libz --enable-pcre2grep-libbz2 --enable-jit
    sudo make && sudo make install
    cd ..

- name: Install OCaml libraries
  if: steps.cache-opam.outputs.cache-hit != 'true'
  run: |
    set -ex
    opam init # --disable-sandboxing
    opam update
    opam switch create ${{env.OCAML_VERSION}}
    eval $(opam env)
    opam env
    opam pin add haxe . --no-action
    opam install haxe --deps-only --assume-depexts
    opam list
    ocamlopt -v

- name: Set ADD_REVISION=1 for non-release
  if: ${{ !startsWith(github.ref, 'refs/tags/') }}
  run: echo "ADD_REVISION=1" >> $GITHUB_ENV

- name: Build Haxe
  run: |
    set -ex
    eval $(opam env)
    opam config exec -- make -s STATICLINK=1 "LIB_PARAMS=\"/usr/local/lib/libz.a\" \"/usr/local/lib/libpcre2-8.a\" \"/usr/local/lib/libmbedtls.a\" \"/usr/local/lib/libmbedcrypto.a\" \"/usr/local/lib/libmbedx509.a\"" haxe
    opam config exec -- make -s haxelib
    make -s package_unix package_installer_mac
    ls -l out
    otool -L ./haxe
    otool -L ./haxelib

- name: Upload artifact (x64)
  if: runner.arch == 'X64'
  uses: actions/upload-artifact@v4
  with:
    name: macX64Binaries
    path: out

- name: Upload artifact (arm)
  if: runner.arch == 'ARM64'
  uses: actions/upload-artifact@v4
  with:
    name: macArmBinaries
    path: out
