name: CI

on:
  push:
    paths-ignore:
      - "**.md"
  pull_request:
    paths-ignore:
      - "**.md"

jobs:
  code-formatting:
    runs-on: ubuntu-18.04
    timeout-minutes: 30
    env:
      VFLAGS: -cc /var/tmp/tcc/bin/tcc -cflags -bt10
    steps:
    - uses: actions/checkout@v2
    - name: Environment info
      run: echo $VFLAGS $GITHUB_SHA $GITHUB_REF
    - name: Build local v
      run: make -j4
    - name: v test-cleancode
      run: ./v -silent test-cleancode
    - name: v test-fmt
      run: ./v -silent test-fmt

#  v-fmt:
#    runs-on: ubuntu-18.04
#    timeout-minutes: 30
#    steps:
#    - uses: actions/checkout@v2
#    - name: getting all branch metainfo from github
#      run: |
#          git fetch --no-tags --prune --depth=1 origin +refs/heads/*:refs/remotes/origin/*
#          echo "Changed files compared to origin/master are:" && git diff --name-status origin/master HEAD -- '*.v'
#    - name: Build v (there is no need for dependencies for fmt)
#      run: make -j4
#    - name: Build a production cmd/tools/vfmt
#      run: ./v -prod -d vfmt cmd/tools/vfmt.v
#    - name: Run v fmt -diff on only the changed files. Does NOT fail for now.
#      run: git diff --name-status origin/master HEAD -- '*.v' |grep -v '^D'|rev|cut -f1|rev| xargs ./v fmt -noerror -diff
#    - name: Run v test-fmt
#      run: echo "TODO" #./v test-fmt

  performance-regressions:
    runs-on: ubuntu-18.04
    timeout-minutes: 30
    env:
      VFLAGS: -cc /var/tmp/tcc/bin/tcc -cflags -bt10
    steps:
    - uses: actions/checkout@v2
    - name: Environment info
      run: echo $VFLAGS $GITHUB_SHA $GITHUB_REF
    - name: Build local v
      run: (make -j4 && ./v -cg -o v cmd/v)
    - name: Clone & Build vmaster/v
      run: |
        git clone --depth=1 https://github.com/vlang/v vmaster/
        (cd vmaster; make -j4 && ./v -cg -o v cmd/v)
    - name: V versions
      run: ./v version && ./vmaster/v version
    - name: Build the repeat tool
      run: ./v cmd/tools/repeat.v
    - name: Repeat -o hw.c examples/hello_world.v
      run: cmd/tools/repeat --max_time 150 --series 3 --count 15 --warmup 3 --fail_percent 10 './v -show-timings -o hw.c examples/hello_world.v' './vmaster/v -show-timings -o hw.c examples/hello_world.v'
    - name: Repeat -o v.c cmd/v
      run: cmd/tools/repeat --max_time 750 --series 3 --count 15 --warmup 3 --fail_percent 10 './v -show-timings -o v.c cmd/v' './vmaster/v -show-timings -o v.c cmd/v'

  ubuntu-tcc:
    runs-on: ubuntu-18.04
    timeout-minutes: 30
    env:
      VFLAGS: -cc /var/tmp/tcc/bin/tcc -cflags -bt10
    steps:
    - uses: actions/checkout@v2
    - name: Install dependencies
      run: |
           sudo rm -f /etc/apt/sources.list.d/dotnetdev.list /etc/apt/sources.list.d/microsoft-prod.list; sudo apt-get update;
           sudo apt-get install --quiet -y libssl-dev sqlite3 libsqlite3-dev valgrind
           sudo apt-get install --quiet -y libglfw3 libglfw3-dev  libfreetype6-dev  libxi-dev libxcursor-dev libasound2-dev
           ## sudo apt-get install --quiet -y libsdl2-dev libsdl2-ttf-dev libsdl2-mixer-dev libsdl2-image-dev
    - name: Build v
      run: |
        echo $VFLAGS
        make -j4
        ./v -cg -o v cmd/v
    - name: Test v->c
      run: |
        sudo ln -s /var/tmp/tcc/bin/tcc /usr/local/bin/tcc
        tcc -version
        ./v -cg -o v cmd/v # Make sure vtcc can build itself twice
#        ./v -silent test-compiler
    - name: v self compilation
      run: ./v -o v2 cmd/v && ./v2 -o v3 cmd/v && ./v3 -o v4 cmd/v
    - name: Fixed tests
      run: ./v -silent test-fixed
    - name: Test building v tools
      run: ./v -silent build-tools
    - name: v doctor
      run: |
        ./v doctor
    - name: Test v binaries
      run: ./v -silent build-vbinaries

  # Alpine docker pre-built container
  alpine-docker-musl-gcc:
    name: alpine-musl
    runs-on: ubuntu-latest
    timeout-minutes: 30
    container:
      image: thevlang/vlang:alpine-build
      env:
        V_CI_MUSL: 1

      volumes:
        - ${{github.workspace}}:/opt/vlang
    steps:
      - name: Checkout
        uses: actions/checkout@v2

      - name: Build V
        run: |
          make CC=clang
      - name: Test V fixed tests
        run: |
          v -silent test-fixed

  macos:
    runs-on: ${{ matrix.os }}
    timeout-minutes: 30
    strategy:
      matrix:
        os: [macOS-latest]
    steps:
    - uses: actions/checkout@v2
    - uses: actions/setup-node@v1
      with:
        node-version: 12.x
    - name: Install dependencies
      run: |
        ##brew install libpq openssl freetype ### these are *already installed* on Catalina ...
        brew uninstall --ignore-dependencies libpq ## libpq is a dependency of PHP
        brew install postgresql
        brew install glfw
        ## brew install sdl2 sdl2_ttf sdl2_mixer sdl2_image
        export LIBRARY_PATH="$LIBRARY_PATH:/usr/local/opt/openssl/lib/"
    - name: Build V
      run:  make -j4 && ./v -cg -o v cmd/v
    - name: Build V using V
      run:  ./v -o v2 cmd/v && ./v2 -o v3 cmd/v
    - name: Test symlink
      run:  sudo ./v symlink
#    - name: Set up pg database
#      run: |
#        pg_ctl -D /usr/local/var/postgres -l /usr/local/var/postgres/server.log start
#        psql -d postgres -c 'select rolname from pg_roles'
#        psql -d postgres -c 'create database customerdb;'
#        psql -d customerdb -f examples/database/pg/mydb.sql
#    - name: Test v->c
#      run: ./v -silent test-compiler
#    - name: Test v binaries
#      run: ./v -silent build-vbinaries
##    - name: Test v->js
##      run: ./v -o hi.js examples/hello_v_js.v && node hi.js
    - name: Test symlink
      run:  ./v symlink && v -o v2 cmd/v
    - name: Fixed tests
      run: VJOBS=1 ./v -silent test-fixed
    - name: Build examples
      run: ./v -silent build-examples
    - name: Build examples with -autofree
      run: |
        ./v -autofree -experimental -o tetris examples/tetris/tetris.v && ./tetris
    - name: v doctor
      run: |
        ./v doctor
    - name: Cross-compilation to Linux
      run: ./v -os linux cmd/v
#    - name: Test vsh
#      run:  ./v examples/v_script.vsh
    - name: Test ved
      run: |
        git clone --depth 1 https://github.com/vlang/ved
        cd ved && ../v -o ved .
        ../v -autofree -experimental .
    - name: Build V UI examples
      run: |
        git clone --depth 1 https://github.com/vlang/ui
        cd ui
        mkdir -p ~/.vmodules
        ln -s $(pwd) ~/.vmodules/ui
        ../v examples/rectangles.v
        ../v run examples/build_examples.vsh

  ubuntu:
    runs-on: ubuntu-18.04
    timeout-minutes: 30
    steps:
    - uses: actions/checkout@v2
    - uses: actions/setup-node@v1
      with:
        node-version: 12.x
    - name: Install dependencies
      run: |
         sudo rm -f /etc/apt/sources.list.d/dotnetdev.list /etc/apt/sources.list.d/microsoft-prod.list;
         sudo apt-get update;
         sudo apt-get install --quiet -y postgresql libpq-dev libssl-dev sqlite3 libsqlite3-dev valgrind
         sudo apt-get install --quiet -y libglfw3 libglfw3-dev  libfreetype6-dev  libxi-dev libxcursor-dev libasound2-dev
         ## sudo apt-get install --quiet -y libsdl2-dev libsdl2-ttf-dev libsdl2-mixer-dev libsdl2-image-dev
    - name: Build V
      run: make -j4 && ./v -cc gcc -o v cmd/v
#    - name: Test V
#      run: ./v -silent test-compiler
#    - name: Test v binaries
#      run: ./v -silent build-vbinaries
##    - name: Test v->js
##      run: ./v -o hi.js examples/hello_v_js.v && node hi.js
#    - name: Build Vorum
#      run: git clone --depth 1 https://github.com/vlang/vorum && cd vorum && ../v . && cd ..
#    - name: Build vpm
#      run: git clone --depth 1 https://github.com/vlang/vpm && cd vpm && ../v . && cd ..
#    - name: Freestanding
#      run: ./v -freestanding -o bare vlib/os/bare/bare_example_linux.v
    - name: v self compilation
      run: ./v -o v2 cmd/v && ./v2 -o v3 cmd/v && ./v3 -o v4 cmd/v
    - name: Fixed tests
      run: |
        ./v -silent test-fixed
    - name: Fixed tests (-prod)
      run: ./v -o vprod -prod cmd/v && ./vprod -silent test-fixed
    - name: Build examples
      run: ./v -silent build-examples
    - name: Build examples with -autofree
      run: |
        ./v -autofree -experimental -o tetris examples/tetris/tetris.v && ./tetris
    - name: Build modules
      run: |
        ./v build-module vlib/os
        ./v build-module vlib/builtin
        ./v build-module vlib/strconv
        ./v build-module vlib/time
        ./v build-module vlib/term
        ./v build-module vlib/math
        ./v build-module vlib/strings
        ./v build-module vlib/v/token
        ./v build-module vlib/v/ast
        ./v build-module vlib/v/parser
        ./v build-module vlib/v/gen
        ./v build-module vlib/v/depgraph
        ./v build-module vlib/os/cmdline
    - name: x64 machine code generation
      run: |
        exit
        ./v -o vprod -prod cmd/v
        cd cmd/tools
        echo "Generating a 1m line V file..."
        ../../vprod gen1m.v
        ./gen1m > 1m.v
        echo "Building it..."
        ../../vprod -backend x64 -o 1m 1m.v
        echo "Running it..."
        ls
#    - name: SDL examples
#      run: git clone --depth 1 https://github.com/vlang/sdl && cd sdl

#        ./1m
      #run: echo "TODO" #cd examples/x64 && ../../v -x64 hello_world.v && ./hello_world
#    - name: Coveralls GitHub Action
#      uses: coverallsapp/github-action@v1.0.1
#      with:
#        github-token: ${{ secrets.GITHUB_TOKEN }}


#  ubuntu-autofree-selfcompile:
#    runs-on: ubuntu-18.04
#    timeout-minutes: 30
#    env:
#      VFLAGS: -cc gcc
#    steps:
#    - uses: actions/checkout@v2
#    - name: Build V
#      run: make -j4
#    - name: V self compilation with -autofree
#      run: ./v -o v2 -autofree cmd/v && ./v2 -o v3 -autofree cmd/v && ./v3 -o v4 -autofree cmd/v


  # Ubuntu docker pre-built container
  ubuntu-musl:
    name: ubuntu-musl
    runs-on: ubuntu-latest
    timeout-minutes: 30
    container:
      image: thevlang/vlang:ubuntu-build
      env:
        V_CI_MUSL: 1
        V_CI_UBUNTU_MUSL: 1
        VFLAGS: -cc musl-gcc
      volumes:
        - ${{github.workspace}}:/opt/vlang
    steps:
      - name: Checkout
        uses: actions/checkout@v2

      - name: Build V
        run: |
          echo $VFLAGS && make -j4 && ./v -cg -o v cmd/v
      - name: Test V fixed tests
        run: |
          ./v -silent test-fixed

#   ubuntu-musl:
#     runs-on: ubuntu-18.04
#     timeout-minutes: 30
#     env:
#       VFLAGS: -cc musl-gcc
#       V_CI_MUSL: 1
#     steps:
#     - uses: actions/checkout@v2
#     - uses: actions/setup-node@v1
#       with:
#         node-version: 12.x
#     - name: Install dependencies
#       run: |
#          sudo rm -f /etc/apt/sources.list.d/dotnetdev.list /etc/apt/sources.list.d/microsoft-prod.list;
#          sudo apt-get update;
#          sudo apt-get install --quiet -y musl musl-tools libssl-dev sqlite3 libsqlite3-dev valgrind
#          sudo apt-get install --quiet -y libsdl2-dev libsdl2-ttf-dev libsdl2-mixer-dev libsdl2-image-dev
#     - name: Build v
#       run: echo $VFLAGS && make -j4 && ./v -cg -o v cmd/v
# #    - name: Test v binaries
# #      run: ./v -silent build-vbinaries
# ##    - name: Test v->js
# ##      run: ./v -o hi.js examples/hello_v_js.v && node hi.js
#     - name: quick debug
#       run: ./v -stats vlib/strconv/format_test.v
#     - name: Fixed tests
#       run: ./v -silent test-fixed

  ubuntu-llvm-mingw:
    runs-on: ubuntu-18.04
    timeout-minutes: 30
    steps:
    - uses: actions/checkout@v2
#    - name: Cross-compile V
#      run: docker build . -f Dockerfile.cross

  windows-gcc:
    runs-on: windows-2019
    timeout-minutes: 30
    env:
        VFLAGS: -cc gcc
    steps:
    - uses: actions/checkout@v2
    #- uses: actions/setup-node@v1
    #  with:
    #    node-version: 12.x
    - name: Build
      run: |
        gcc --version
        .\make.bat -gcc
    - name: Test new v.c
      run: .\v.exe -o v.c cmd/v && gcc -municode -w v.c
    - name: Install dependencies
      run: |
        .\v.exe setup-freetype
        .\.github\workflows\windows-install-sqlite.bat
        ## .\.github\workflows\windows-install-sdl.bat
    - name: Fixed tests
      run: |
        .\v.exe -silent test-fixed
#    - name: Test
#      run: |
#        .\v.exe -silent test-compiler
      ## v.js dosent work on windows
        #.\v.exe -o hi.js examples/hello_v_js.v
        #node hi.js
#    - name: Test v binaries
#      run: ./v -silent build-vbinaries
#    - name: v2 self compilation
#      run: .\v.exe -o v2.exe cmd/v && .\v2.exe -o v3.exe cmd/v

  windows-msvc:
    runs-on: windows-2019
    timeout-minutes: 30
    env:
        VFLAGS: -cc msvc
    steps:
    - uses: actions/checkout@v2
    #- uses: actions/setup-node@v1
    #  with:
    #    node-version: 12.x
    - name: Build
      run: |
        echo %VFLAGS%
        echo $VFLAGS
        .\make.bat -msvc
    - name: Install dependencies
      run: |
        .\v.exe setup-freetype
        .\.github\workflows\windows-install-sqlite.bat
        ## .\.github\workflows\windows-install-sdl.bat
    - name: Fixed tests
      run: |
        ./v -cg cmd\tools\vtest-fixed.v
        ./v -silent test-fixed
    - name: v doctor
      run: |
        ./v doctor
#    - name: Test
#      run: |
#        .\v.exe -silent test-compiler
#      ## v.js dosent work on windows
        #.\v.exe -o hi.js examples/hello_v_js.v
        #node hi.js
#    - name: Test v binaries
#      run: ./v -silent build-vbinaries

  windows-tcc:
    runs-on: windows-2019
    timeout-minutes: 30
    # We are simulating a user with no cc installed.
    # This way, v's cc detection on Windows is also tested.
    # env:
    #   VFLAGS: -cc tcc
    steps:
    - uses: actions/checkout@v2
    #- uses: actions/setup-node@v1
    #  with:
    #    node-version: 12.x
    - name: Build
      # We need to move gcc and msvc, so that V can't find an existing C compiler and downloads tcc
      run: |
        'for /f "usebackq tokens=*" %i in (`where gcc.exe`) do move /Y "%i" "%i.old"'     | cmd
        'for /f "usebackq tokens=*" %i in (`where vswhere.exe`) do move /Y "%i" "%i.old"' | cmd
        move "${env:ProgramFiles(x86)}\Microsoft Visual Studio\Installer\vswhere.exe" "${env:ProgramFiles(x86)}\Microsoft Visual Studio\Installer\vswhere.exe.old"
        .\make.bat
    - name: Test new v.c
      run: .\v.exe -o v.c cmd/v && .\thirdparty\tcc\tcc.exe -w -ladvapi32 -bt10 v.c
    - name: Install dependencies
      run: |
        .\v.exe setup-freetype
        .\.github\workflows\windows-install-sqlite.bat
        ## .\.github\workflows\windows-install-sdl.bat
    - name: Fixed tests
      run: |
        .\v.exe -silent test-fixed
#    - name: Test
#      run: |
#        .\v.exe -silent test-compiler
      ## v.js dosent work on windows
        #.\v.exe -o hi.js examples/hello_v_js.v
        #node hi.js
#    - name: Test v binaries
#      run: ./v -silent build-vbinaries
#    - name: v2 self compilation
#      run: .\v.exe -o v2.exe cmd/v && .\v2.exe -o v3.exe cmd/v


  compilable-v-c-and-v-win-c:
    runs-on: ubuntu-latest
    timeout-minutes: 30
    steps:
    - uses: actions/checkout@v2
    - name: Install dependencies
      run: |
        sudo rm -f /etc/apt/sources.list.d/dotnetdev.list /etc/apt/sources.list.d/microsoft-prod.list
        sudo apt-get update
        sudo apt install --quiet -y mingw-w64 wine-stable winetricks
    - name: Build V
      run: make -j4
    - name: v.c can be compiled and run
      run: |
        ./v -os cross -o /tmp/v.c cmd/v
        gcc  -g -std=gnu11 -w -o v_from_vc /tmp/v.c  -lm -lpthread
        ls -lart v_from_vc
        ./v_from_vc version
    - name: v_win.c can be compiled and run
      run: |
        ./v -os windows -o /tmp/v_win.c cmd/v
        x86_64-w64-mingw32-gcc  /tmp/v_win.c -std=c99 -w -municode -o v_from_vc.exe
        ls -lart v_from_vc.exe
        winetricks nocrashdialog
        wine v_from_vc.exe version


  ubuntu-c-plus-plus:
    runs-on: ubuntu-18.04
    timeout-minutes: 30
    steps:
    - uses: actions/checkout@v2
    - name: Install dependencies
      run: |
        sudo rm -f /etc/apt/sources.list.d/dotnetdev.list /etc/apt/sources.list.d/microsoft-prod.list;
        sudo apt-get update;
        sudo apt-get install --quiet -y postgresql libpq-dev libssl-dev sqlite3 libsqlite3-dev
        sudo apt-get install --quiet -y libglfw3 libglfw3-dev  libfreetype6-dev  libxi-dev libxcursor-dev libasound2-dev
        sudo apt-get install --quiet -y valgrind g++-9
        ## sudo apt-get install --quiet -y libsdl2-dev libsdl2-ttf-dev libsdl2-mixer-dev libsdl2-image-dev
    - name: Build V
      run: make -j4
    - name: g++ version
      run: g++-9 --version
    - name: V self compilation with g++
      run: ./v -cc g++-9 -o v2 cmd/v && ./v2 -cc g++-9 -o v3 cmd/v
##    - name: Running tests with g++
##      run: ./v -cc g++-9 -silent test-fixed

  install-modules:
    runs-on: ubuntu-latest
    timeout-minutes: 30
    steps:
    - uses: actions/checkout@v2
    - name: Install dependencies
      run: sudo apt-get install --quiet -y libssl-dev
    - name: Build V
      run: make -j4
    - name: Installing V modules
      run: |
        ./v install ui
        ./v install nedpals.args

  gitly-compiles:
    runs-on: ubuntu-18.04
    timeout-minutes: 30
    steps:
    - uses: actions/checkout@v2
    - name: Install dependencies
      run: sudo apt-get install --quiet -y libssl-dev sqlite3 libsqlite3-dev
    - name: Build V
      run: make -j2 && ./v -cc gcc -o v cmd/v
    - name: Install markdown
      run: ./v install markdown
    - name: Build Gitly
      run: |
        git clone --depth 1 https://github.com/vlang/gitly
        cd gitly
        ../v .
        ../v -autofree -experimental .
        cd ..

  websocket_autobahn:
    name: Autobahn integrations tests
    runs-on: ubuntu-latest
    timeout-minutes: 30
    steps:
      - name: Checkout
        uses: actions/checkout@v2

      - name: Run autobahn services
        run: docker-compose -f ${{github.workspace}}/vlib/x/websocket/tests/autobahn/docker-compose.yml up -d
      - name: Build client test
        run: docker exec autobahn_client "/src/v" "/src/vlib/x/websocket/tests/autobahn/autobahn_client.v"
      - name: Run client test
        run: docker exec autobahn_client "/src/vlib/x/websocket/tests/autobahn/autobahn_client"
      - name: Build client wss test
        run: docker exec autobahn_client "/src/v" "/src/vlib/x/websocket/tests/autobahn/autobahn_client_wss.v"
      - name: Run client wss test
        run: docker exec autobahn_client "/src/vlib/x/websocket/tests/autobahn/autobahn_client_wss"
      - name: Run server test
        run: docker exec autobahn_server "wstest" "-m" "fuzzingclient" "-s" "/config/fuzzingclient.json"
      - name: Copy reports
        run: docker cp autobahn_server:/reports ${{github.workspace}}/reports
      - name: Copy reports wss
        run: docker cp autobahn_server_wss:/reports ${{github.workspace}}/reports_wss
      - name: Test success
        run: docker exec autobahn_server "python" "/check_results.py"
      - name: Test success WSS
        run: docker exec autobahn_server_wss "python" "/check_results.py"

      - name: Publish all reports
        uses: actions/upload-artifact@v2
        with:
            name: full report
            path: ${{github.workspace}}/reports
      - name: Publish report client
        uses: actions/upload-artifact@v2
        with:
            name: client
            path: ${{github.workspace}}/reports/clients/index.html
      - name: Publish report server
        uses: actions/upload-artifact@v2
        with:
            name: server
            path: ${{github.workspace}}/reports/servers/index.html
      - name: Publish all reports WSS
        uses: actions/upload-artifact@v2
        with:
            name: full report wss
            path: ${{github.workspace}}/reports_wss
      - name: Publish report client wss
        uses: actions/upload-artifact@v2
        with:
            name: client wss
            path: ${{github.workspace}}/reports_wss/clients/index.html