name: build

on:
  # Run this workflow once every 6 hours against the master branch
  schedule:
   - cron: "0 */6 * * *"

  push:
    branches:
      - 'master'

    tags:
      - '*'

  pull_request:
    branches:
      - '*'

jobs:
  linux:
    strategy:
      # Notes
      # - Ubuntu 18.04 is not compatible due to Python being too old
      # - Pasta does not compile on LLVM 9 or 10, so they have been
      #   removed from the strategy matrix
      matrix:
        build_type: [
          'Release',
          ]
        image:
          - { name: 'ubuntu', tag: '22.04' }
        llvm_version: [18]

    runs-on: ubuntu-22.04

    steps:
    - name: Get the 🍝
      uses: actions/checkout@v2
      with:
        path: src

    - name: Select the build job count
      shell: bash
      id: build_job_count
      run: |
        echo ::set-output name=VALUE::$(($(nproc) + 1))

    - name: Install system dependencies
      shell: bash
      run: |
        sudo apt update
        sudo apt install -y build-essential \
                            ninja-build \
                            curl \
                            ccache \
                            python3-dev \
                            libncurses-dev \
                            clang-15

    - name: Setup the build paths
      shell: bash
      id: build_paths
      run: |
        rel_src_path="src"
        rel_build_path="build"
        rel_downloads_path="downloads"
        rel_install_path="install"
        rel_ccache_path="ccache"


        mkdir -p ${rel_build_path} \
                 ${rel_downloads_path} \
                 ${rel_install_path} \
                 ${rel_ccache_path}

        echo ::set-output name=SOURCE::$(realpath ${rel_src_path})
        echo ::set-output name=BUILD::$(realpath ${rel_build_path})
        echo ::set-output name=INSTALL::$(realpath ${rel_install_path})

        echo ::set-output name=REL_DOWNLOADS::${rel_downloads_path}
        echo ::set-output name=DOWNLOADS::$(realpath ${rel_downloads_path})

        echo ::set-output name=REL_CCACHE::${rel_ccache_path}
        echo ::set-output name=CCACHE::$(realpath ${rel_ccache_path})

    - name: Update the cache (ccache)
      uses: actions/cache@v3
      with:
        path: ${{ steps.build_paths.outputs.REL_CCACHE }}

        key: |
          gitmodules_${{ matrix.image.name }}-${{ matrix.image.tag }}_${{ matrix.build_type }}_${{ matrix.llvm_version }}_${{ github.sha }}

        restore-keys: |
          gitmodules_${{ matrix.image.name }}-${{ matrix.image.tag }}_${{ matrix.build_type }}_${{ matrix.llvm_version }}

    - name: Update the cache (downloads)
      uses: actions/cache@v3
      with:
        path: ${{ steps.build_paths.outputs.REL_DOWNLOADS }}

        key: |
          gitmodules_${{ matrix.image.name }}-${{ matrix.image.tag }}_${{ matrix.build_type }}_${{ matrix.llvm_version }}_${{ github.sha }}

        restore-keys: |
          gitmodules_${{ matrix.image.name }}-${{ matrix.image.tag }}_${{ matrix.build_type }}_${{ matrix.llvm_version }}

    - name: Configure the project
      working-directory: ${{ steps.build_paths.outputs.BUILD }}
      shell: bash

      env:
        CCACHE_DIR: ${{ steps.build_paths.outputs.CCACHE }}

      run: |
        cmake -G "Ninja" \
          -DCMAKE_BUILD_TYPE=${{ matrix.build_type }} \
          -DCMAKE_C_COMPILER="$(which clang-15)" \
          -DCMAKE_CXX_COMPILER="$(which clang++-15)" \
          -DPASTA_BOOTSTRAP_MACROS=OFF \
          -DPASTA_BOOTSTRAP_TYPES=OFF \
          -DPASTA_ENABLE_TESTING=OFF \
          -DPASTA_ENABLE_PY_BINDINGS=OFF \
          -DPASTA_ENABLE_INSTALL=OFF \
          "${{ steps.build_paths.outputs.SOURCE }}"

    - name: Build the project
      working-directory: ${{ steps.build_paths.outputs.BUILD }}
      shell: bash

      env:
        CCACHE_DIR: ${{ steps.build_paths.outputs.CCACHE }}

      run: |
        cmake --build . -j ${{ steps.build_job_count.outputs.VALUE }}

    - name: Reclaim disk space
      shell: bash
      run: |
        rm -rf ${{ steps.build_paths.outputs.BUILD }}
