name: Ficus build

on:
  push:
    branches: [ "main" ]
  pull_request:
    branches: [ "main" ]

env:
  BUILD_TYPE: Debug

jobs:
  build-ficus-toolchain:
    runs-on: ubuntu-latest
    steps:
      - name: Checkout repo
        uses: actions/checkout@v3

      - name: Install sudo for ACT runner
        run: apt-get update && apt-get install -y sudo
        if: env.ACT=='true'

      - name: Install dependencies
        run: sudo apt-get update && sudo apt-get install -y cmake build-essential pax gcc g++ nasm bison bzip2 flex mtools texinfo libgmp-dev libmpfr-dev libmpc-dev xorriso ccache

      - name: CCache cache
        id: cache-ccache
        uses: actions/cache@v4
        with:
          path: ~/.cache/ccache
          key: ${{ runner.os }}-ccache-${{ github.run_id }}-${{ github.run_attempt }}
          restore-keys: |
            ${{ runner.os }}-ccache-${{ github.run_id }}-${{ github.run_attempt }}
            ${{ runner.os }}-ccache-

      - name: Add ccache to path
        run: echo "/usr/lib/ccache" >> $GITHUB_PATH

      - name: Print ccache info
        run: |
          ccache -p
          which gcc
          which g++

      - name: Cache generic toolchain
        id: cache-toolchain
        uses: actions/cache/restore@v4
        env:
          cache-name: cache-toolchain
        with:
          path: |
            toolchain/gcc-i686-elf-prefix
            toolchain/gcc-x86_64-elf-prefix
            toolchain/grub
            toolchain/limine
          key: ${{ runner.os }}-build-${{ env.cache-name }}-${{ hashFiles('toolchain_scripts/*') }}

      - if: ${{ steps.cache-toolchain.outputs.cache-hit != 'true' }}
        name: Build generic toolchain
        run: source env.sh && toolchain_scripts/build-all.sh && ccache -s

      - name: Save generic toolchain
        id: cache-toolchain-save
        uses: actions/cache/save@v4
        env:
          cache-name: cache-toolchain
        with:
          path: |
            toolchain/gcc-i686-elf-prefix
            toolchain/gcc-x86_64-elf-prefix
            toolchain/grub
            toolchain/limine
          key: ${{ runner.os }}-build-${{ env.cache-name }}-${{ hashFiles('toolchain_scripts/*') }}

      - name: Cache toolchain sources
        id: cache-toolchain-sources
        uses: actions/cache/restore@v4
        env:
          cache-name: cache-toolchain-sources
        with:
          path: |
            ficus-toolchain/binutils-x86_64-ficus/source
            ficus-toolchain/gcc-x86_64-ficus/source
          key: ${{ runner.os }}-build-${{ env.cache-name }}-${{ hashFiles('ficus-toolchain/*.sh') }}-${{ hashFiles('ficus-toolchain/binutils-x86_64-ficus/*.patch') }}-${{ hashFiles('ficus-toolchain/gcc-x86_64-ficus/*.patch') }}

      - if: ${{ steps.cache-toolchain-sources.outputs.cache-hit != 'true' }}
        name: Download toolchain sources
        run: source ${{github.workspace}}/env.sh && ${{github.workspace}}/ficus-toolchain/build-all.sh sources

      - name: Save toolchain sources
        id: cache-toolchain-sources-save
        uses: actions/cache/save@v4
        env:
          cache-name: cache-toolchain-sources
        with:
          path: |
            ficus-toolchain/binutils-x86_64-ficus/source
            ficus-toolchain/gcc-x86_64-ficus/source
          key: ${{ runner.os }}-build-${{ env.cache-name }}-${{ hashFiles('ficus-toolchain/*.sh') }}-${{ hashFiles('ficus-toolchain/binutils-x86_64-ficus/*.patch') }}-${{ hashFiles('ficus-toolchain/gcc-x86_64-ficus/*.patch') }}

      - name: Cache s1 toolchain
        id: cache-toolchain-s1
        uses: actions/cache/restore@v4
        env:
          cache-name: cache-toolchain-s1
        with:
          path: |
            toolchain/gcc-x86_64-ficus-prefix
          key: ${{ runner.os }}-build-${{ env.cache-name }}-${{ hashFiles('ficus-toolchain/*.sh') }}-${{ hashFiles('ficus-toolchain/binutils-x86_64-ficus/*.patch') }}-${{ hashFiles('ficus-toolchain/gcc-x86_64-ficus/*.patch') }}

      - if: ${{ steps.cache-toolchain-s1.outputs.cache-hit != 'true' }}
        name: Build ficus toolchain s1
        run: source ${{github.workspace}}/env.sh && ${{github.workspace}}/ficus-toolchain/build-all.sh s1only && ccache -s

      - name: Save s1 toolchain
        id: cache-toolchain-s1-save
        uses: actions/cache/save@v4
        env:
          cache-name: cache-toolchain-s1
        with:
          path: |
            toolchain/gcc-x86_64-ficus-prefix
          key: ${{ runner.os }}-build-${{ env.cache-name }}-${{ hashFiles('ficus-toolchain/*.sh') }}-${{ hashFiles('ficus-toolchain/binutils-x86_64-ficus/*.patch') }}-${{ hashFiles('ficus-toolchain/gcc-x86_64-ficus/*.patch') }}

      - name: Cache s2 toolchain
        id: cache-toolchain-s2
        uses: actions/cache/restore@v4
        env:
          cache-name: cache-toolchain-s2-v2
        with:
          path: |
            toolchain/gcc-x86_64-ficus-prefix
            sysroot
          key: ${{ runner.os }}-build-${{ env.cache-name }}-${{ hashFiles('ficus-toolchain/*.sh') }}-${{ hashFiles('ficus-toolchain/newlib/source/**') }}-${{ hashFiles('ficus-toolchain/binutils-x86_64-ficus/*.patch') }}-${{ hashFiles('ficus-toolchain/gcc-x86_64-ficus/*.patch') }}

      - if: ${{ steps.cache-toolchain-s2.outputs.cache-hit != 'true' }}
        name: Build ficus toolchain s2
        run: source ${{github.workspace}}/env.sh && ${{github.workspace}}/ficus-toolchain/build-all.sh s2only && ccache -s

      - name: Save s2 toolchain
        id: cache-toolchain-s2-save
        uses: actions/cache/save@v4
        env:
          cache-name: cache-toolchain-s2-v2
        with:
          path: |
            toolchain/gcc-x86_64-ficus-prefix
            sysroot
          key: ${{ runner.os }}-build-${{ env.cache-name }}-${{ hashFiles('ficus-toolchain/*.sh') }}-${{ hashFiles('ficus-toolchain/newlib/source/**') }}-${{ hashFiles('ficus-toolchain/binutils-x86_64-ficus/*.patch') }}-${{ hashFiles('ficus-toolchain/gcc-x86_64-ficus/*.patch') }}

      - name: Tar the toolchain
        run: cd ${{github.workspace}} && tar -czvf toolchain-ficus.tar.xz toolchain sysroot

      - name: Upload ficus toolchain
        uses: actions/upload-artifact@v3
        with:
          name: toolchain-ficus
          path: ${{github.workspace}}/toolchain-ficus.tar.xz
          retention-days: 5

  iso:
    needs: build-ficus-toolchain
    runs-on: ubuntu-latest

    steps:
      - name: Checkout repo
        uses: actions/checkout@v3

      - name: Install sudo for ACT runner
        run: apt-get update && apt-get install -y sudo
        if: env.ACT=='true'

      - name: Install dependencies
        run: sudo apt-get update && sudo apt-get install -y cmake build-essential pax gcc g++ nasm bison bzip2 flex mtools texinfo libgmp-dev libmpfr-dev libmpc-dev xorriso

      - name: Download toolchain
        uses: actions/download-artifact@v3
        with:
          name: toolchain-ficus
          path: ${{github.workspace}}

      - name: Untar the toolchain
        run: cd ${{github.workspace}} && tar xf toolchain-ficus.tar.xz

      - name: Configure CMake
        # Configure CMake in a 'build' subdirectory. `CMAKE_BUILD_TYPE` is only required if you are using a single-configuration generator such as make.
        # See https://cmake.org/cmake/help/latest/variable/CMAKE_BUILD_TYPE.html?highlight=cmake_build_type
        run: source ${{github.workspace}}/env.sh && cmake -B ${{github.workspace}}/build -DCMAKE_BUILD_TYPE=${{env.BUILD_TYPE}} -DFICUS_ROOT=${{github.workspace}} -DCMAKE_TOOLCHAIN_FILE=${{github.workspace}}/CMake-x86_64-ficus-toolchain.cmake

      - name: Build iso
        # Build your program with the given configuration
        run: source ${{github.workspace}}/env.sh && cmake --build ${{github.workspace}}/build --target iso --config ${{env.BUILD_TYPE}}  --parallel $(nproc)

      - name: Upload isos
        uses: actions/upload-artifact@v3
        with:
          name: iso
          path: ${{github.workspace}}/build/**/*.iso
          retention-days: 5
