name: llvm

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

permissions:
    contents: read

jobs:
    build:
        runs-on: macos-latest

        steps:
            - uses: actions/checkout@v3
            - uses: actions/cache@v3
              id: cache-llvm-16
              with:
                  path: |
                      /usr/local/opt/llvm@16/

                      /usr/local/Cellar/llvm/
                  key: ${{ runner.os }}-llvm-16
            - uses: actions/cache@v3
              with:
                  path: |
                      /Users/runner/.xmake

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

                  key: ${{ runner.os }}-vcpkg-${{ hashFiles('**/xmake.lua') }}
            - uses: actions/cache@v3
              with:
                  key: ${{ runner.os }}-deno-${{ hashFiles('**/deps.ts') }}-${{ hashFiles('**/import_map.json') }}
                  path: |
                      ~/.deno
                      ~/Library/Caches/deno
            - name: Install LLVM and Clang
              if: ${{ steps.cache-llvm-16.outputs.cache-hit!= 'true' }}
              run: |
                  brew install llvm@16
                  export PATH="/usr/local/opt/llvm@16/bin:$PATH"
                  export LDFLAGS="-L/usr/local/opt/llvm@16/lib"
                  export CPPFLAGS="-I/usr/local/opt/llvm@16/include"

            - name: install
              run: |
                  brew install xmake python@3.11 six z3 zstd 

                  brew install libtool automake
                   export LDFLAGS="-L/usr/local/opt/llvm@16/lib"
                       export CPPFLAGS="-I/usr/local/opt/llvm@16/include"
                       echo 'export PATH="/usr/local/opt/llvm@16/bin:$PATH"' >> /Users/runner/.bash_profile

            - name: vcpkg-action

              uses: johnwason/vcpkg-action@v4
              with:
                  token: ${{ github.token }}
                  triplet: x64-osx-release
                  pkgs: "gtest cppunit"

            - 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: deno run -A xmake.ts --mode=test --group=test  --toolchain=llvm --sdk=/usr/local/opt/llvm@16/
            - run: xmake clean  -a
            - run: xmake f --toolchain=llvm --sdk=/usr/local/opt/llvm@16/ -y -v --project=. "--file=./xmake.lua"
            - run: xmake build -v -y  -w --project=. "--file=./xmake.lua"
            - run: xmake clean  -a
            - run: xmake f --toolchain=llvm --sdk=/usr/local/opt/llvm@16/ -y -v --project=. "--file=./xmake.lua" --mode=test
            - run: xmake build -v -y  -w --project=. "--file=./xmake.lua" --group=test
            - run: xmake run --group=test
