# Part of the Carbon Language project, under the Apache License v2.0 with LLVM
# Exceptions. See /LICENSE for license information.
# SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception

name: Setup build environment (macOS)
inputs:
  matrix_runner:
    required: true
runs:
  using: composite
  steps:
    # Free up disk space as the macOS runners end up using most for Xcode
    # versions we don't need and iOS simulators.
    - name: Free up disk space
      shell: bash
      run: |
        # The xcrun occasionally fails (maybe a race condition?), so retry a few
        # times. Example failure:
        #   "data" couldn't be moved to "Deleting-<ID>"
        echo '*** Delete iOS simulators'
        xcrun simctl delete all || \
          xcrun simctl delete all || \
          xcrun simctl delete all
        sudo rm -rf ~/Library/Developer/CoreSimulator/Caches/*

    # Install and cache LLVM 19 from Homebrew. Some runners may have LLVM 19,
    # but this is reliable (including with libc++), and gives us testing at the
    # minimum supported LLVM version.
    - name: Cache Homebrew
      id: cache-homebrew-macos
      uses: actions/cache@1bd1e32a3bdc45362d1e726936510720a7c30a57 # v4.2.0
      with:
        # Cover all the critical parts of Homebrew here. Homebrew on Arm macOS
        # uses its own prefix making this easy to cover, but we need a few
        # different paths for Intel.
        path: |
          ${{
            runner.arch == 'ARM64' && '/opt/homebrew' ||
            '
              /usr/local/Homebrew
              /usr/local/Cellar
              /usr/local/Frameworks
              /usr/local/bin
              /usr/local/opt
            '
          }}
        # Note the key needs to include all the packages we're adding.
        key:
          Homebrew-Cache-${{ inputs.matrix_runner }}-${{ runner.arch }}-llvm@19

    - name: Install LLVM and Clang with Homebrew
      if: steps.cache-homebrew-macos.outputs.cache-hit != 'true'
      shell: bash
      run: |
        echo '*** Prune brew leaves'
        # We prune all the leaf packages to have a minimal environment. This
        # both minimizes the install space and avoids accidental dependencies
        # on installed packages.
        brew leaves
        LEAVES=$(brew leaves | egrep -v '^(bazelisk|gh|git|git-lfs|gnu-tar|go@.*|jq|pipx|node@.*|openssl@.*|wget|yq|zlib)$')
        brew uninstall -f --ignore-dependencies $LEAVES
        echo '*** Installing LLVM deps'
        brew install --force-bottle --only-dependencies llvm@19
        echo '*** Installing LLVM itself'
        brew install --force-bottle --force --verbose llvm@19
        echo '*** brew info llvm@19'
        brew info llvm@19
        echo '*** brew autoremove'
        brew autoremove
        echo '*** brew info'
        brew info
        echo '*** brew leaves'
        brew leaves
        echo '*** brew config'
        brew config

    - name: Setup LLVM and Clang
      shell: bash
      run: |
        LLVM_PATH="$(brew --prefix llvm@19)"
        echo "Using ${LLVM_PATH}"
        echo "${LLVM_PATH}/bin" >> $GITHUB_PATH
        echo '*** ls "${LLVM_PATH}"'
        ls "${LLVM_PATH}"
        echo '*** ls "${LLVM_PATH}/bin"'
        ls "${LLVM_PATH}/bin"
