name: Tools CI

on:
  push:
    paths:
      - 'cmd/**'
      - '!cmd/tools/vpm/**'
      - 'vlib/**'
      - 'thirdparty/**'
      - '!**.md'
      - '**/tools_ci.yml'
  pull_request:
    paths:
      - 'cmd/**'
      - '!cmd/tools/vpm/**'
      - 'vlib/**'
      - 'thirdparty/**'
      - '!**.md'
      - '**/tools_ci.yml'

concurrency:
  group: tools-${{ github.workflow }}-${{ github.ref == 'refs/heads/master' && github.sha || github.ref }}
  cancel-in-progress: true

jobs:
  tools-linux:
    runs-on: ubuntu-22.04
    strategy:
      matrix:
        cc: [tcc, gcc, clang]
      fail-fast: false
    env:
      VFLAGS: -cc ${{ matrix.cc }}
    steps:
      - uses: actions/checkout@v4
      - name: Build V
        run: make -j4 && ./v -showcc -o v cmd/v && ./v doctor
      - name: Code in cmd/ is formatted
        run: ./v fmt -verify cmd/
      - name: Check build-tools
        run: ./v -silent -N -W -check build-tools
      - name: Test tools
        run: ./v -silent test-self cmd
      - name: Test tools (-cstrict)
        if: ${{ matrix.cc != 'tcc' }}
        run: ./v -silent -W -cstrict test-self cmd
      - name: Test sanitized
        if: ${{ matrix.cc != 'tcc' }}
        run: |
          export VNATIVE_SKIP_LIBC_VV=1
          if [[ ${{ matrix.cc }} == "clang" ]]; then
            cmd="./v -silent -cc clang -cflags -fsanitize=undefined test-self -ubsan-compiler cmd"
            echo "$cmd" && $cmd
            cmd="./v -silent -cc clang -cflags -fsanitize=memory    test-self -msan-compiler cmd"
            echo "$cmd" && $cmd
          else
            cmd="./v -silent -cc gcc -cflags -fsanitize=thread test-self cmd"
            echo "$cmd" && $cmd
            cmd="./v -silent -cc gcc -cflags -fsanitize=address,leak,undefined,shift,shift-exponent,shift-base,integer-divide-by-zero,unreachable,vla-bound,null,return,signed-integer-overflow,bounds,bounds-strict,alignment,object-size,float-divide-by-zero,float-cast-overflow,nonnull-attribute,returns-nonnull-attribute,bool,enum,vptr test-self -asan-compiler cmd/tools"
            echo "$cmd" && $cmd
            ## cmd="./v -silent -cc gcc -cflags -fsanitize=kernel-address test-self -asan-compiler cmd"
            ## echo "$cmd" && $cmd
          fi

  tools-macos:
    runs-on: macos-14
    strategy:
      matrix:
        cc: [clang]
      fail-fast: false
    env:
      VFLAGS: -cc ${{ matrix.cc }}
    steps:
      - uses: actions/checkout@v4
      - name: Build V
        run: make -j4 && ./v -showcc -o v cmd/v && ./v doctor
      - name: Check build-tools
        run: ./v -silent -N -W -check build-tools
      - name: Test tools
        run: ./v -silent test-self cmd
      - name: Test tools (-cstrict)
        run: ./v -silent -W -cstrict test-self cmd

  tools-windows:
    runs-on: windows-2025
    strategy:
      matrix:
        cc: [tcc, gcc, msvc]
      fail-fast: false
    env:
      VFLAGS: -cc ${{ matrix.cc }}
    steps:
      - uses: actions/checkout@v4
      - name: Build V
        run: ./make.bat -${{ matrix.cc }} && ./v -o v2.exe cmd/v && ./v2 -showcc -o v.exe cmd/v && ./v doctor
      - name: Check build tools
        run: ./v -silent -N -W -check build-tools
      - name: Test tools
        run: ./v -silent test-self cmd
      - name: Test tools (-cstrict)
        if: ${{ matrix.cc == 'msvc' }}
        run: ./v -silent -W -cstrict test-self cmd

  tools-docker-ubuntu-musl:
    runs-on: ubuntu-24.04
    timeout-minutes: 121
    container:
      image: thevlang/vlang:ubuntu-build
      env:
        V_CI_MUSL: 1
        V_CI_UBUNTU_MUSL: 1
        VFLAGS: -cc musl-gcc -gc none
      volumes:
        - ${{github.workspace}}:/opt/vlang
    steps:
      - name: Checkout
        uses: actions/checkout@v4
      - name: Build V
        run: make -j4 && ./v -cg -o v cmd/v
      - name: Ensure git commands can be used with no prompts on modern Git versions
        run: git config --global --add safe.directory /__w/v/v
      - name: Verify `v test` works
        run: |
          ./v cmd/tools/test_if_v_test_system_works.v
          ./cmd/tools/test_if_v_test_system_works
      - name: Check build tools
        run: ./v -silent -N -W -check build-tools
      - name: Test tools
        run: ./v -silent test-self cmd
      - name: Test tools (-cstrict)
        run: ./v -silent -W -cstrict test-self cmd
