# This workflow uses actions that are not certified by GitHub.
# They are provided by a third-party and are governed by
# separate terms of service, privacy policy, and support
# documentation.

name: gcc

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

env:
    # Path to the solution file relative to the root of the project.
    SOLUTION_FILE_PATH: .

    # Configuration type to build.
    # You can convert this to a build matrix if you need coverage of multiple configuration types.
    # https://docs.github.com/actions/learn-github-actions/managing-complex-workflows#using-a-build-matrix
    BUILD_CONFIGURATION: Release

permissions:
    contents: read

jobs:
    build:
        runs-on: ubuntu-latest

        steps:
            - uses: actions/checkout@v3
            - uses: actions/cache@v3
              with:
                  path: |
                      ~/.deno
                      ~/.cache/deno
                  key: ${{ runner.os }}-deno-${{ hashFiles('**/deps.ts') }}-${{ hashFiles('**/import_map.json') }}
            - uses: actions/cache@v3
              id: cache-gcc-13
              with:
                  path: |
                      /opt/gcc-latest/

                  key: ${{ runner.os }}-gcc-13
            - uses: actions/cache@v3
              with:
                  path: |
                      /usr/local/share/vcpkg
                      ./vcpkg

                  key: ${{ runner.os }}-vcpkg-${{ hashFiles('**/xmake.lua') }}
            - uses: actions/cache@v3
              with:
                  path: |
                    /home/runner/.xmake
                      ~/.xmake

                  key: ${{ runner.os }}-xmake-${{ hashFiles('**/xmake.lua') }}

            - name: install-gcc-13
              if: steps.cache-gcc-13.outputs.cache-hit != 'true'
              run: |
                  wget http://kayari.org/gcc-latest/gcc-latest.deb && sudo dpkg -i gcc-latest.deb && rm gcc-latest.deb
            - run: |
                  sudo chmod 777 /etc/apt/sources.list
                  sudo cat >>/etc/apt/sources.list<<EOF

                  deb http://azure.archive.ubuntu.com/ubuntu/ lunar-proposed main restricted universe multiverse
                  deb http://azure.archive.ubuntu.com/ubuntu/ lunar main restricted universe multiverse
                  deb http://azure.archive.ubuntu.com/ubuntu/ lunar-backports main restricted universe multiverse
                  deb http://azure.archive.ubuntu.com/ubuntu/ lunar-security main restricted universe multiverse
                  deb http://azure.archive.ubuntu.com/ubuntu/ lunar-updates main restricted universe multiverse
                  EOF
                  sudo apt update
                  sudo apt install gcc-13 g++-13 gcc g++ gcc-13-locales g++-multilib -y
                  export PATH="/opt/gcc-latest/bin:$PATH"
                  export LD_RUN_PATH="/opt/gcc-latest/lib64"
                  echo 'export PATH="/opt/gcc-latest/bin:$PATH"' >> ~/.profile
                  sudo rm -vf /usr/bin/gcc
                  sudo ln -sv /opt/gcc-latest/bin/gcc /usr/bin/gcc
                  sudo rm -vf /usr/bin/g++
                  sudo ln -sv /opt/gcc-latest/bin/g++ /usr/bin/g++

            - name: vcpkg-action

              uses: johnwason/vcpkg-action@v4
              with:
                  triplet: x64-linux-release
                  pkgs: "gtest cppunit"

                  token: ${{ github.token }}
            - name: Setup Deno

              uses: denoland/setup-deno@v1.1.0
              with:
                  deno-version: v1.33.1
            - run: deno task cache
            - uses: xmake-io/github-action-setup-xmake@v1
              with:
                  xmake-version: branch@dev
                  actions-cache-folder: ".xmake-cache"

            - run: xmake l install.lua
            - run: xmake clean  -a
            - run: xmake f --toolchain=gcc --sdk=/opt/gcc-latest/ -y -v --project=. "--file=./xmake.lua"
            - run: xmake build -v -y  -w --project=. "--file=./xmake.lua"
              continue-on-error: true
            - run: xmake clean  -a
            - run: deno run -A xmake.ts --mode=test --group=test  --toolchain=gcc --sdk=/opt/gcc-latest/
              continue-on-error: true

            - run: xmake clean  -a
            - run: xmake f --toolchain=gcc --sdk=/opt/gcc-latest/ -y -v --project=. "--file=./xmake.lua" --mode=test
            - run: xmake build -v -y  -w --project=. "--file=./xmake.lua" --group=test
              continue-on-error: true
            - run: xmake run --group=test
              continue-on-error: true
