###############################################################################
##                       VirtualFluids CI Pipeline                           ##
###############################################################################
image: git.rz.tu-bs.de:4567/irmb/virtualfluids/ubuntu20_04:1.2

stages:
  - build
  - test
  - analyze
  - deploy
  - release


###############################################################################
##                                Builds                                     ##
###############################################################################
.gnu_build_template:
  stage: build

  tags:
    - gpu
    - linux

  cache:
    key: "$CI_JOB_NAME-$CI_COMMIT_REF_SLUG"
    paths:
      - $BUILD_FOLDER
      - $CI_PROJECT_DIR/cache

  artifacts:
    expire_in: 1 hrs
    paths:
      - $BUILD_FOLDER

  script:
    - export CCACHE_BASEDIR=$CI_PROJECT_DIR
    - export CCACHE_DIR=$CI_PROJECT_DIR/cache
    - export CCACHE_COMPILERCHECK=content
    - ccache --zero-stats
    - ccache --show-stats
    - $CXX --version
    - $CC --version
    - cmake --version
    - mpirun --version
    - mkdir -p $CI_PROJECT_DIR/$BUILD_FOLDER
    - cd $CI_PROJECT_DIR/$BUILD_FOLDER
    - rm -r -f ./*
    - cmake .. -LAH
      --preset=all_make
      -DBUILD_WARNINGS_AS_ERRORS=ON
      -DCMAKE_CUDA_ARCHITECTURES=60
    - make -j4
    - ccache --show-stats

  variables:
    BUILD_FOLDER: "build"

###############################################################################
gcc_9:
  extends: .gnu_build_template

  before_script:
    - export CC=gcc
    - export CXX=g++
###############################################################################
clang_10:
  extends: .gnu_build_template

  before_script:
    - export CC=clang
    - export CXX=clang++

###############################################################################
msvc_16:
  stage: build

  tags:
    - win
    - gpu

  variables:
    BUILD_CONFIGURATION: "Release"
    BUILD_FOLDER: "build"

  # add cmake and MSBuild.exe to the path.
  # This Needs to be adapted when moved to a new build machine.
  before_script:
    - git --version
    - $env:Path += ";C:\Program Files\CMake\bin\"
    - cmake --version
    - $env:Path += ";C:\Program Files (x86)\Microsoft Visual Studio\2019\Enterprise\MSBuild\Current\Bin"
    - MSBuild.exe -version

  script:
    - cd $CI_PROJECT_DIR
    - md -force $env:BUILD_FOLDER
    - cd $env:BUILD_FOLDER
    - cmake .. --preset=all_msvc -DCMAKE_CUDA_ARCHITECTURES=61 -DBUILD_WARNINGS_AS_ERRORS=ON
    - MSBuild.exe VirtualFluids.sln /property:Configuration=$env:BUILD_CONFIGURATION /verbosity:minimal /maxcpucount:4

  cache:
    key: "$CI_JOB_NAME-$CI_COMMIT_REF_SLUG"
    paths:
      - $CI_PROJECT_DIR/$env:BUILD_FOLDER/

  artifacts:
    expire_in: 1 hrs
    paths:
      - $CI_PROJECT_DIR/$env:BUILD_FOLDER/

###############################################################################
build_singularity_image:
  stage: build

  tags:
    - priviliged
    - linux

  rules:
    - if: $CI_COMMIT_TAG

  artifacts:
    paths:
      - Containers/VirtualFluidsOpenMPI.sif

  script:
    - singularity build --fakeroot Containers/VirtualFluidsOpenMPI.sif Containers/VirtualFluidsOpenMPI.def
    - ls -sh Containers/VirtualFluidsOpenMPI.sif

###############################################################################
##                                Tests                                      ##
###############################################################################
gcc_9_unit_tests:
  stage: test

  needs: ["gcc_9"]

  before_script:
    - cd $CI_PROJECT_DIR/build

  script:
    - ./bin/basicsTests

###############################################################################
msvc_16_unit_tests:
  stage: test

  tags:
    - win
    - gpu

  needs: ["msvc_16"]

  before_script:
    - cd $CI_PROJECT_DIR/build

  script:
    - .\bin\Release\basicsTests.exe

###############################################################################
##                        Code analysis                                      ##
###############################################################################
clang_build_analyzer_clang_10:
  stage: analyze

  only: ["schedules"]

  needs: []

  before_script:
    - export CC=clang
    - export CXX=clang++
    - $CXX --version
    - cd /tmp
    - git clone https://github.com/aras-p/ClangBuildAnalyzer.git
    - cd ClangBuildAnalyzer
    - cmake .
    - make
    - export PATH+=:$(pwd)

  script:
    - mkdir -p $CI_PROJECT_DIR/build
    - cd $CI_PROJECT_DIR/build
    - cmake ..
      -DBUILD_VF_CPU=ON
      -DBUILD_VF_GPU=ON
      -DCMAKE_CUDA_ARCHITECTURES=60
      -DCMAKE_CXX_FLAGS=-ftime-trace
    - ClangBuildAnalyzer --start .
    - make
    - ClangBuildAnalyzer --stop . CBA
    - ClangBuildAnalyzer --analyze CBA

###############################################################################
include_what_you_use_clang_10:
  stage: analyze

  only: ["schedules"]

  needs: []

  before_script:
    - export CC=clang
    - export CXX=clang++
    - $CXX --version
    - cd /tmp
    - git clone https://github.com/include-what-you-use/include-what-you-use.git
    - cd include-what-you-use
    - git checkout clang_10
    - cmake . -DCMAKE_PREFIX_PATH=/usr/lib/llvm-10
    - make
    - export PATH+=:$(pwd)/bin

  script:
    - mkdir -p $CI_PROJECT_DIR/build
    - cd $CI_PROJECT_DIR/build
    - cmake ..
      -DBUILD_VF_CPU=ON
      -DBUILD_VF_GPU=ON
      -DCMAKE_CUDA_ARCHITECTURES=60
      -DBUILD_VF_INCLUDE_WHAT_YOU_USE=ON
    - make

###############################################################################
cppcheck:
  stage: analyze

  only:
    - open_source@irmb/VirtualFluids_dev

  needs: []

  before_script:
    - export DEBIAN_FRONTEND=noninteractive
    - apt-get update
    - apt-get install -y cppcheck

  script:
    - cd $CI_PROJECT_DIR
    - cppcheck --version
    - cppcheck src --enable=all --xml 2> cppcheck.xml
  artifacts:
    expire_in: 1 week
    paths:
      - cppcheck.xml

###############################################################################
# lizard - Cyclomatic Complexity Analyzer
# Ignore warnings is manually set to 400, as we do not want this job to fail.
lizard:
  stage: analyze


  only:
    - open_source@irmb/VirtualFluids_dev

  needs: []

  before_script:
    - cd /tmp
    - git clone https://github.com/terryyin/lizard.git
    - cd lizard
    - python3 setup.py install
    - lizard --version

  script:
    - cd $CI_PROJECT_DIR
    - lizard -l cpp src/ > lizard.txt --warnings_only --ignore_warnings 400

  artifacts:
    expire_in: 1 week
    paths:
      - lizard.txt

###############################################################################
# code coverage
gcov_gcc_9:
  stage: analyze


  only:
    - open_source@irmb/VirtualFluids_dev

  needs: []

  before_script:
    - export DEBIAN_FRONTEND=noninteractive
    - apt-get update
    - pip3 install 'jinja2<3.1' gcovr==5.0
    - gcovr --version

  script:
    - mkdir -p $CI_PROJECT_DIR/build
    - cd $CI_PROJECT_DIR/build
    - cmake ..
      --preset=all_make
      -DCMAKE_CUDA_ARCHITECTURES=60
      -DBUILD_VF_COVERAGE=ON
    - make -j4
    - ./bin/basicsTests
    - cd ..
    - mkdir coverage
    - gcovr -r $CI_PROJECT_DIR -k build -f "src" --print-summary --html coverage/coverage.html --html-details --xml coverage/coverage.xml

  artifacts:
    expire_in: 1 week
    paths:
      - coverage/

    reports:
      coverage_report: 
        coverage_format: cobertura
        path: coverage/coverage.xml

  cache:
    key: "$CI_JOB_NAME-$CI_COMMIT_REF_SLUG"
    paths:
      - build

###############################################################################
# this job also produces a compile commands json file.
clang-tidy:
  stage: analyze


  only:
    - open_source@irmb/VirtualFluids_dev

  needs: []

  allow_failure: true

  before_script:
    - run-clang-tidy -h

  script:
    - mkdir -p $CI_PROJECT_DIR/build
    - cd $CI_PROJECT_DIR/build
    - cmake ..
      -DBUILD_VF_CPU=ON
      -DBUILD_VF_GPU=OFF
    - python3 ../utilities/filterCompileCommands.py compile_commands.json
    - run-clang-tidy -quiet > clangtidy.txt

  artifacts:
    when: always
    expire_in: 1 week
    paths:
      - build/clangtidy.txt
      - build/compile_commands.json

###############################################################################
# doxygen
pages:
  stage: analyze

  image: alpine

  only:
    - open_source@irmb/virtualfluids

  needs: []

  script:
  - apk update && apk add doxygen
  - doxygen docs/Doxyfile
  - mv docs/build/html/ public/

  artifacts:
    expire_in: 1 hrs
    paths:
    - public


###############################################################################
##                               Deploy                                      ##
###############################################################################

###############################################################################
# sonar-scanner runs sonar-project.properties
# the reports in this file needs to match the artifacts.
sonar-scanner:
  stage: deploy
  tags:
    - linux

  only:
    - open_source@irmb/VirtualFluids_dev

  variables:
    SONAR_HOST_URL: "http://gitlab-runner01.irmb.bau.tu-bs.de/sonarqube/"

  needs: ["cppcheck","clang-tidy","gcov_gcc_9"]

  before_script:
    - cd /tmp
    - apt-get install unzip
    - wget https://binaries.sonarsource.com/Distribution/sonar-scanner-cli/sonar-scanner-cli-4.0.0.1744-linux.zip
    - unzip sonar-scanner-cli-4.0.0.1744-linux.zip
    - cd sonar-scanner-4.0.0.1744-linux/
    - echo "sonar.host.url=$SONAR_HOST_URL" >> conf/sonar-scanner.properties
    - echo "sonar.sourceEncoding=UTF-8" >> conf/sonar-scanner.properties
    - cat conf/sonar-scanner.properties
    - export PATH+=:$(pwd)/bin
    - sonar-scanner -v

  script:
    - cd $CI_PROJECT_DIR
    - sonar-scanner -Dsonar.login=$SONAR_SECURITY_TOKEN

###############################################################################
##                              Release                                      ##
###############################################################################
create_release:
  stage: release

  image: registry.gitlab.com/gitlab-org/release-cli:latest

  needs: ["build_singularity_image"]

  rules:
    - if: $CI_COMMIT_TAG

  script:
    - echo "Creating release with tag $CI_COMMIT_TAG"
    - release-cli create --name "VirtualFluids $CI_COMMIT_TAG" \
      --description "VirtualFluids CFD Simulator" \
      --tag-name "$CI_COMMIT_TAG" \
      --ref "$CI_COMMIT_SHA" \
      --job-token "$CI_JOB_TOKEN" \
      --assets-link="{'name':'VirtualFluidsSingularityImage_OpenMPI','url':'','type':'other','filepath':'Containers/VirtualFluidsOpenMPI.sif'}"
    - build/bin/basicsTests
