# Copyright (C) 2019 Intel Corporation.  All rights reserved.
# SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
name: Reusable workflow-build_llvm_libraries

on:
  workflow_call:
    inputs:
      os:
        required: true
        type: string
      arch:
        required: true
        type: string
      container_image:
        required: false
        type: string
      extra_build_llvm_options:
        required: false
        type: string
        default: ""
      cache_key_suffix:
        required: false
        type: string
        default: ""
    outputs:
      cache_key:
        description: "A cached key of LLVM libraries"
        value: ${{ jobs.build_llvm_libraries.outputs.key}}

permissions:
  contents: read

jobs:
  build_llvm_libraries:
    runs-on: ${{ inputs.os }}
    # Using given container image if it is specified.
    # Otherwise, it will be ignored by the runner.
    container:
      image: ${{ inputs.container_image }}
    outputs:
      key: ${{ steps.create_lib_cache_key.outputs.key}}
    permissions:
      contents: read
      actions: write # for uploading cached artifact

    steps:
      - name: checkout
        uses: actions/checkout@v6.0.1

      - name: install dependencies for non macos
        if: ${{ !startsWith(inputs.os, 'macos') }}
        shell: bash
        run: /usr/bin/env python3 -m pip install -r requirements.txt
        working-directory: build-scripts

      - name: install dependencies for macos
        if: startsWith(inputs.os, 'macos')
        run: /usr/bin/env python3 -m pip install -r requirements.txt --break-system-packages
        working-directory: build-scripts

      - name: Retrieve the last commit ID
        id: get_last_commit
        env:
          GH_TOKEN: ${{ secrets.GITHUB_TOKEN }}
        shell: bash
        run: |
          echo "last_commit=$(GH_TOKEN=${{ secrets.GITHUB_TOKEN }} /usr/bin/env python3 ./build_llvm.py ${{ inputs.extra_build_llvm_options }} --llvm-ver)" >> $GITHUB_OUTPUT
        working-directory: build-scripts

      # Bump the prefix number to evict all previous caches and
      # enforce a clean build, in the unlikely case that some
      # weird build error occur and llvm/build becomes a potential
      # suspect.
      - name: form the cache key of libraries
        id: create_lib_cache_key
        shell: bash
        run: |
          echo "key=0-llvm-libraries-${{ inputs.os }}-${{ inputs.arch }}-${{ steps.get_last_commit.outputs.last_commit }}${{ inputs.cache_key_suffix }}" >> $GITHUB_OUTPUT

      - name: Cache LLVM libraries
        id: retrieve_llvm_libs
        uses: actions/cache@v5
        with:
          path: |
            ./core/deps/llvm/build/bin
            ./core/deps/llvm/build/include
            ./core/deps/llvm/build/lib
            ./core/deps/llvm/build/libexec
            ./core/deps/llvm/build/share
          key: ${{ steps.create_lib_cache_key.outputs.key}}

      - uses: actions/cache@v5
        with:
          path: ~/.cache/ccache
          key: 0-ccache-${{ inputs.os }}-${{ steps.get_last_commit.outputs.last_commit }}
          restore-keys: |
            0-ccache-${{ inputs.os }}
        if: steps.retrieve_llvm_libs.outputs.cache-hit != 'true' && inputs.os == 'ubuntu-22.04'

      # Don't install dependencies if the cache is hit or running in docker container
      - run: sudo apt install -y ccache ninja-build
        if: steps.retrieve_llvm_libs.outputs.cache-hit != 'true' && startsWith(inputs.os, 'ubuntu') && inputs.container_image == ''

      - uses: actions/cache@v5
        with:
          path: ~/Library/Caches/ccache
          key: 0-ccache-${{ inputs.os }}-${{ steps.get_last_commit.outputs.last_commit }}
          restore-keys: |
            0-ccache-${{ inputs.os }}
        if: steps.retrieve_llvm_libs.outputs.cache-hit != 'true' && startsWith(inputs.os, 'macos')

      - run: brew install ccache ninja
        if: steps.retrieve_llvm_libs.outputs.cache-hit != 'true' && startsWith(inputs.os, 'macos')

      - uses: actions/cache@v5
        with:
          path: ~/.cache/ccache
          key: 0-ccache-${{ inputs.os }}-${{ steps.get_last_commit.outputs.last_commit }}
          restore-keys: |
            0-ccache-${{ inputs.os }}
        if: steps.retrieve_llvm_libs.outputs.cache-hit != 'true' && inputs.os == 'windows-2022'

      # Install tools on Windows
      - run: choco install -y ccache ninja
        if: steps.retrieve_llvm_libs.outputs.cache-hit != 'true' && inputs.os == 'windows-2022'

      - name: Build LLVM libraries
        if: steps.retrieve_llvm_libs.outputs.cache-hit != 'true'
        shell: bash
        run: /usr/bin/env python3 ./build_llvm.py ${{ inputs.extra_build_llvm_options }} --arch ${{ inputs.arch }}
        working-directory: build-scripts
