name: Build environment setup
description: Configure build env for Yosys builds

inputs:
  runs-on:
    required: true
    type: string
  get-build-deps:
    description: 'Install Yosys build dependencies'
    default: false
    required: false
    type: boolean
  get-docs-deps:
    description: 'Install Yosys docs dependencies'
    default: false
    required: false
    type: boolean
  get-test-deps:
    description: 'Install Yosys test dependencies'
    default: false
    required: false
    type: boolean
  get-iverilog:
    description: 'Install iverilog'
    default: false
    required: false
    type: boolean

runs:
  using: composite
  steps:
    # if updating common/build/docs dependencies, make sure to update README.md
    # and docs/source/getting_started/installation.rst to match.
    - name: Linux common dependencies
      if: runner.os == 'Linux'
      uses: awalsh128/cache-apt-pkgs-action@v1.6.0
      with:
        packages: gawk git make python3
        version: ${{ inputs.runs-on }}-commonys

    - name: Linux build dependencies
      if: runner.os == 'Linux' && inputs.get-build-deps == 'true'
      uses: awalsh128/cache-apt-pkgs-action@v1.6.0
      with:
        packages: bison clang flex libffi-dev libfl-dev libreadline-dev pkg-config tcl-dev zlib1g-dev libgtest-dev libgmock-dev
        version: ${{ inputs.runs-on }}-buildys

    - name: Linux docs dependencies
      if: runner.os == 'Linux' && inputs.get-docs-deps == 'true'
      uses: awalsh128/cache-apt-pkgs-action@v1.6.0
      with:
        packages: graphviz xdot
        version: ${{ inputs.runs-on }}-docsys

    - name: Install macOS Dependencies
      if: runner.os == 'macOS'
      shell: bash
      run: |
        brew bundle

    - name: Linux runtime environment
      if: runner.os == 'Linux'
      shell: bash
      run: |
        echo "${{ github.workspace }}/.local/bin" >> $GITHUB_PATH
        echo "procs=$(nproc)" >> $GITHUB_ENV

    - name: macOS runtime environment
      if: runner.os == 'macOS'
      shell: bash
      run: |
        echo "${{ github.workspace }}/.local/bin" >> $GITHUB_PATH
        echo "$(brew --prefix llvm@20)/bin" >> $GITHUB_PATH
        echo "$(brew --prefix bison)/bin" >> $GITHUB_PATH
        echo "$(brew --prefix flex)/bin" >> $GITHUB_PATH
        echo "procs=$(sysctl -n hw.ncpu)" >> $GITHUB_ENV

    - name: Setup iverilog
      if: inputs.get-iverilog == 'true'
      uses: ./.github/actions/setup-iverilog
      with:
        runs-on: ${{ inputs.runs-on }}
