name: macOS builds
permissions:  read-all

on:
  push:
    paths-ignore:
      - '.clang-format'
      - '.mdl-styles'
      - '*.md'
      - 'docs/**'
      - 'licenses/**'
      - 'website/**'

  pull_request:
    paths-ignore:
      - '.clang-format'
      - '.mdl-styles'
      - '*.md'
      - 'docs/**'
      - 'licenses/**'
      - 'website/**'

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

jobs:
  build_macos:
    name: ${{ matrix.conf.name }} (${{ matrix.conf.arch }})
    runs-on: ${{ matrix.conf.host }}
    if: github.event_name == 'push' || github.event.pull_request.head.repo.full_name != github.repository
    strategy:
      fail-fast: false
      matrix:
        conf:
          - name: Clang
            host: macos-13
            arch: x86_64
            needs_deps: true
            run_tests: true
            build_flags: -Denable_debugger=normal
            brew_path: /usr/local/homebrew
            max_warnings: 80

          - name: Clang
            host: macos-14
            arch: arm64
            needs_deps: true
            run_tests: false # the shell_cmds_tests hang on arm64
            build_flags: -Dbuildtype=debug
            brew_path: /opt/homebrew
            max_warnings: 0

    steps:
      - name: Checkout repository
        uses: actions/checkout@v4
        with:
          submodules: false

      - name: Install C++ compiler and libraries
        if:   matrix.conf.needs_deps
        run: |
          arch -arch=${{ matrix.conf.arch }} brew unlink openssl@1.1 || true
          rm -f /usr/local/bin/openssl || true
          arch -arch=${{ matrix.conf.arch }} brew install --overwrite openssl@3 || true
          arch -arch=${{ matrix.conf.arch }} brew link --overwrite openssl@3 || true
          arch -arch=${{ matrix.conf.arch }} brew install --overwrite \
            ${{ matrix.conf.packages }} \
            $(cat packages/macos-12-brew.txt) || true

      - name: Cache subprojects
        id:   cache-subprojects
        uses: actions/cache@v4
        with:
          path: subprojects.tar
          key: subprojects-${{ hashFiles('subprojects/*.wrap') }}-1
          enableCrossOsArchive: true

      - if:   steps.cache-subprojects.outputs.cache-hit != 'true'
        name: Generate subprojects cache
        run:  scripts/fetch-and-tar-subprojects.sh

      - name: Extract subprojects cache
        run:  scripts/extract-subprojects-tar.sh

      - name: Setup and build
        run: |
          set -xo pipefail
          export PATH="/usr/local/bin:/usr/bin:/bin:/usr/sbin:/sbin"
          eval "$(${{ matrix.conf.brew_path }}/bin/brew shellenv)"
          ./scripts/log-env.sh
          ./scripts/retry_command.sh 3 meson setup ${{ matrix.conf.build_flags }} build
          meson compile -C build 2>&1 | tee build.log

      - name: Run tests
        if:   matrix.conf.run_tests
        run: |
          export PATH="/usr/local/bin:/usr/bin:/bin:/usr/sbin:/sbin"
          eval "$(${{ matrix.conf.brew_path }}/bin/brew shellenv)"
          meson test --num-processes 128 -t 0 -C build --print-errorlogs

      - name: Summarize warnings
        if:   matrix.conf.run_tests != true
        env:
          MAX_WARNINGS: ${{ matrix.conf.max_warnings }}
        run:  python3 ./scripts/count-warnings.py -lf build.log

  build_macos_release:
    name: Release build (${{ matrix.runner.arch }})
    runs-on: ${{ matrix.runner.host }}
    if: github.event_name == 'push' || github.event.pull_request.head.repo.full_name != github.repository
    env:
      MACOSX_DEPLOYMENT_TARGET: ${{ matrix.runner.minimum_deployment }}

    strategy:
      matrix:
        runner:
          - host: macos-13
            arch: x86_64
            build_flags: -Db_lto=true
            brew_path: /usr/local/homebrew
            minimum_deployment: '11.0'
            needs_deps: true
            needs_libintl_workaround: false

          - host: macos-14
            arch: arm64
            build_flags: -Db_lto=true
            brew_path: /opt/homebrew
            minimum_deployment: '11.0'
            needs_deps: true

    steps:
      - name: Checkout repository
        uses: actions/checkout@v4
        with:
          submodules: false

      - name: Install C++ compiler and libraries
        if:   matrix.runner.needs_deps
        run: |
          brew install librsvg tree libpng ninja opusfile speexdsp meson python-setuptools

      - name: Cache subprojects
        id: cache-subprojects
        uses: actions/cache@v4
        with:
          path: subprojects.tar
          key:  subprojects-${{ hashFiles('subprojects/*.wrap') }}-1
          enableCrossOsArchive: true

      - if:   steps.cache-subprojects.outputs.cache-hit != 'true'
        name: Generate subprojects cache
        run:  scripts/fetch-and-tar-subprojects.sh

      - name: Extract subprojects cache
        run:  scripts/extract-subprojects-tar.sh

      - name: Log environment
        run:  arch -arch=${{ matrix.runner.arch }} ./scripts/log-env.sh

      - name: Inject version string
        run: |
          set -x
          VERSION=$(./scripts/get-version.sh version-and-hash)
          echo "VERSION=$VERSION" >> $GITHUB_ENV

      - name: Setup and build release
        run: |
          set -x
          eval "$(${{ matrix.runner.brew_path }}/bin/brew shellenv)"
          meson setup \
            ${{ matrix.runner.build_flags }} \
            -Ddefault_library=static \
            -Dwrap_mode=forcefallback \
            -Duse_zlib_ng=sse2,ssse3,neon \
            build
          meson compile -C build

      - name: Upload binary
        uses: actions/upload-artifact@v4
        with:
          name: dosbox-${{ matrix.runner.arch }}
          path: build/dosbox
          overwrite: true

      - name: Upload resources
        uses: actions/upload-artifact@v4
        with:
          name: Resources
          path: Resources
          overwrite: true

  publish_universal_build:
    name: Publish universal build
    needs: build_macos_release
    runs-on: macos-14
    steps:
      - name: Checkout repository
        uses: actions/checkout@v4
        with:
          submodules: false

      - name: Inject version string
        run: |
          set -x
          VERSION=$(./scripts/get-version.sh version-and-hash)
          echo "VERSION=$VERSION" >> $GITHUB_ENV

      - name: Install brew depedencies
        run: HOMEBREW_NO_INSTALLED_DEPENDENTS_CHECK=1 brew install --overwrite librsvg

      - name: Download binaries
        uses: actions/download-artifact@v4

      - name: Package
        run: |
          mv Resources ../
          ./scripts/create-package.sh \
            -p macos \
            -v "${{ env.VERSION }}" \
            -f \
            "$(pwd)" \
            "$(pwd)"

      - name: Create dmg
        run: |
          ln -s /Applications dist/

          codesign -s "-" "dist/DOSBox Staging.app" --force --deep -v

          hdiutil create \
              -volname "DOSBox Staging" \
              -srcfolder dist \
              -ov -format UDZO "dosbox-staging-macOS-universal-${{ env.VERSION }}.dmg"

      - name: Upload disk image
        uses: actions/upload-artifact@v4
        # GitHub automatically zips the artifacts, and there's no option
        # to skip it or upload a file only.
        with:
          name: dosbox-staging-macOS-universal-${{ env.VERSION }}
          path: dosbox-staging-macOS-universal-${{ env.VERSION }}.dmg
