image: irmb/virtualfluids-python-deps

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


###############################################################################
##                                Builds                                     ##
###############################################################################
gcc_9_python:
  stage: build

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

  artifacts:
    paths:
      - dist/

  script:
    - python3 setup.py bdist_wheel

gcc_9:
  stage: build

  tags:
    - gpu
    - linux

  cache:
    key: "$CI_JOB_NAME-$CI_COMMIT_REF_SLUG-gcc9-hybrid"
    paths:
      - build_gcc9

  artifacts:
    paths:
      - build_gcc9

  before_script:
    - export DEBIAN_FRONTEND=noninteractive
    - apt-get update
    - apt-get install -y libboost-serialization-dev
    - cd /tmp
    - apt-get install unzip
    - wget https://github.com/NVIDIA/cuda-samples/archive/10.1.2.zip
    - unzip 10.1.2.zip
    - export NVCUDASAMPLES_ROOT=$(pwd)/cuda-samples-10.1.2

  script:
    - cd $CI_PROJECT_DIR
    - mkdir -p build_gcc9
    - cd build_gcc9
    - cmake .. -DBUILD_VF_CPU=ON -DBUILD_VF_GPU=ON
    - cmake . -LAH
    - make -j4

clang_10:
  stage: build

  tags:
    - gpu
    - linux

  cache:
    key: "$CI_JOB_NAME-$CI_COMMIT_REF_SLUG-clang10-hybrid"
    paths:
      - build_clang10

  artifacts:
    paths:
      - build_clang10

  before_script:
    - export DEBIAN_FRONTEND=noninteractive
    - apt-get update
    - export CC=clang
    - export CXX=clang++
    - $CXX --version
    - apt-get install -y libboost-serialization-dev
    - cd /tmp
    - apt-get install unzip
    - wget https://github.com/NVIDIA/cuda-samples/archive/10.1.2.zip
    - unzip 10.1.2.zip
    - export NVCUDASAMPLES_ROOT=$(pwd)/cuda-samples-10.1.2

  script:
    - cd $CI_PROJECT_DIR
    - mkdir -p build_clang10
    - cd build_clang10
    - cmake .. -DBUILD_VF_CPU=ON -DBUILD_VF_GPU=ON
    - make -j4

msvc_16:
  stage: build

  tags:
    - win
    - gpu

  variables:
    CMAKE_GENERATOR: "Visual Studio 16 2019"
    BUILD_CONFIGURATION: "Release"
    BUILD_FOLDER: "build"
    BUILD_VF_CPU: "ON"
    BUILD_VF_GPU: "ON"
    BUILD_VF_UNIT_TESTS: "ON"

  # 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 .. -DBUILD_VF_CPU=$env:BUILD_VF_CPU -DBUILD_VF_GPU=$env:BUILD_VF_GPU -DBUILD_VF_UNIT_TESTS=$env:BUILD_VF_UNIT_TESTS -G "$env:CMAKE_GENERATOR" ..
    - MSBuild.exe VirtualFluids.sln /property:Configuration=$env:BUILD_CONFIGURATION /verbosity:minimal /maxcpucount:4

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

  artifacts:
    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 Containers/VirtualFluidsOpenMPI.sif Containers/VirtualFluidsOpenMPI.def
    - ls -sh Containers/VirtualFluidsOpenMPI.sif



gcc_9_rebuild:
  stage: build

  #only: ["schedules"]

  tags:
    - gpu
    - linux

  before_script:
    - export DEBIAN_FRONTEND=noninteractive
    - apt-get update
    - apt-get install -y libboost-serialization-dev
    - cd /tmp
    - apt-get install unzip
    - wget https://github.com/NVIDIA/cuda-samples/archive/10.1.2.zip
    - unzip 10.1.2.zip
    - export NVCUDASAMPLES_ROOT=$(pwd)/cuda-samples-10.1.2

  script:
    - cd $CI_PROJECT_DIR
    - mkdir -p build_gcc_rebuild
    - cd build_gcc_rebuild
    - rm -r -f ./*
    - cmake -E env CXXFLAGS="-fdiagnostics-show-option" cmake .. -DBUILD_VF_CPU=ON -DBUILD_VF_GPU=ON
    - make -j4  >&1 | tee gcc_warnings.txt

  artifacts:
    paths:
      - build_gcc_rebuild/gcc_warnings.txt

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

  needs: ["gcc_9"]

  before_script:
    - export VF_UNITTESTS=$(find build_gcc9 -name basicsTests)

  script:
    - $VF_UNITTESTS

msvc_16_unit_tests:
  stage: test

  tags:
    - win
    - gpu

  needs: ["msvc_16"]

  before_script:
    - cd $CI_PROJECT_DIR/build

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

gcc_9_python_bindings_test:
  stage: test

  needs: ["gcc_9_python"]

  before_script:
    - export PYTHONPATH="Python"
    - export VF_WHEEL=$(find dist/*.whl)
    - pip3 install $VF_WHEEL

  script:
    - python3 -m unittest discover -s Python -v


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

  only: ["schedules"]

  needs: []

  before_script:
    - export DEBIAN_FRONTEND=noninteractive
    - apt-get update
    - cmake --version
    - mpirun --version
    - 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
    - rm -r -f ./*
    - cmake .. -DBUILD_VF_CPU=ON -DUSE_OPENMP=OFF -DCMAKE_CXX_FLAGS=-ftime-trace
    - ClangBuildAnalyzer --start .
    - make
    - ClangBuildAnalyzer --stop . CBA
    - ClangBuildAnalyzer --analyze CBA


include_what_you_use_gcc_9:
  stage: analyze

  only: ["schedules"]

  needs: []

  before_script:
    - export DEBIAN_FRONTEND=noninteractive
    - apt-get update
    - 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
    - echo $PATH

  script:
    - mkdir -p $CI_PROJECT_DIR/build
    - cd $CI_PROJECT_DIR/build
    - rm -r -f ./*
    - cmake .. -DBUILD_VF_CPU=ON -DUSE_OPENMP=OFF -DBUILD_VF_INCLUDE_WHAT_YOU_USE=ON
    - make


cppcheck:
  stage: analyze

  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
    - cppcheck-htmlreport --file=cppcheck.xml --report-dir=html_report --source-dir=.

  artifacts:
    paths:
      - html_report/
      - cppcheck.xml

# lizard - Cyclomatic Complexity Analyzer
# Ignore warnings is manually set to 191. This job will fail when new warnings are added.
lizard:
  stage: analyze

  needs: []

  before_script:
    - export DEBIAN_FRONTEND=noninteractive
    - apt-get update
    - cd /tmp
    - git clone https://github.com/terryyin/lizard.git
    - cd lizard
    - python3 setup.py install
    - which lizard
    - lizard --version

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

  artifacts:
    paths:
      - lizard.txt

# code coverage
gcov_gcc_9:
  stage: analyze

  needs: []

  before_script:
    - export DEBIAN_FRONTEND=noninteractive
    - apt-get update
    - pip3 install gcovr
    - gcovr --version
    - apt-get install -y libboost-serialization-dev
    - cd /tmp
    - apt-get install unzip
    - wget https://github.com/NVIDIA/cuda-samples/archive/10.1.2.zip
    - unzip 10.1.2.zip
    - export NVCUDASAMPLES_ROOT=$(pwd)/cuda-samples-10.1.2

  script:
    - mkdir -p $CI_PROJECT_DIR/gcov_build
    - cd $CI_PROJECT_DIR/gcov_build
    - cmake .. -DBUILD_VF_CPU=ON -DBUILD_VF_GPU=ON -DBUILD_VF_COVERAGE=ON -DBUILD_VF_UNIT_TESTS=ON
    - make -j4
    - ./bin/basicsTests
    - cd ..
    - mkdir coverage
    - gcovr -r $CI_PROJECT_DIR -k gcov_build -f "src" --print-summary --html coverage/coverage.html --html-details --xml coverage/coverage.xml

  artifacts:
    paths:
      - coverage/
      - gcov_build/

    reports:
      cobertura: coverage/coverage.xml

  cache:
    key: "$CI_JOB_NAME-$CI_COMMIT_REF_SLUG-gcov"
    paths:
      - gcov_build

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

  needs: []

  before_script:
    - run-clang-tidy -h

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

  artifacts:
    paths:
      - build_clang_tidy/clangtidy.txt
      - build_clang_tidy/compile_commands.json

###############################################################################
##                               Deploy                                      ##
###############################################################################
vf_to_phoenix:
  stage: deploy
  
  needs: ["gcc_9_python", "gcc_9_unit_tests", "gcc_9_python_bindings_test"]

  before_script:
    - 'command -v ssh-agent >/dev/null || ( apt-get update -y && apt-get install openssh-client -y )'
    - apt-get install -y rsync
    - mkdir -p ~/.ssh
    - chmod 700 ~/.ssh
    - eval $(ssh-agent -s)
    - echo "$SSH_PRIVATE_KEY" | tr -d '\r' | ssh-add -
    - echo $SSH_PRIVATE_KEY >> ansible/private_key
    - ssh-keyscan -t rsa phoenix.hlr.rz.tu-bs.de >> ~/.ssh/known_hosts
    - pip3 install ansible

  script:
    - ansible-playbook -i ansible/hosts.cfg -u $REMOTE_USER ansible/playbook_vf_deploy.yml

  parallel:
    matrix:
      - ANSIBLE_MATRIX: 0
        REMOTE_USER:
          - y0054816

vf_wheel_to_jupyterhub:
  stage: deploy

  needs: ["gcc_9_python", "gcc_9_unit_tests", "gcc_9_python_bindings_test"]

  variables:
    HOST: "finrod.irmb.bau.tu-bs.de"
    SSH_KEY: "$SSH_PRIVATE_KEY_JUPYTER_HOST_AT_FINROD"
    REMOTE_USER: "jupyter_host"

  before_script:
    - 'command -v ssh-agent >/dev/null || ( apt-get update -y && apt-get install openssh-client -y )'
    - apt-get install -y rsync
    - mkdir -p ~/.ssh
    - chmod 700 ~/.ssh
    - eval $(ssh-agent -s)
    - echo "$SSH_KEY" | tr -d '\r' | ssh-add -
    - echo $SSH_KEY >> ansible/private_key
    - ssh-keyscan -t rsa $HOST >> ~/.ssh/known_hosts
    - pip3 install ansible

  script:
    - ansible-playbook -i ansible/hosts.cfg -u $REMOTE_USER ansible/playbook_jupyter_update.yml

cppcheck_to_elladan:
  stage: deploy
  needs: ["cppcheck"]

  before_script:
    - 'command -v ssh-agent >/dev/null || ( apt-get update -y && apt-get install openssh-client -y )'
    - apt-get install -y rsync
    - mkdir -p ~/.ssh
    - chmod 700 ~/.ssh
    - eval $(ssh-agent -s)
    - echo "$SSH_PRIVATE_KEY_ELLADAN" | tr -d '\r' | ssh-add -
    - ssh-keyscan -t rsa elladan.irmb.bau.tu-bs.de >> ~/.ssh/known_hosts
    - pip3 install ansible

  script:
    - ansible-playbook -i ansible/hosts.cfg -u public_pages ansible/playbook_cppcheck.yml


gcov_to_elladan:
  stage: deploy

  needs: ["gcov_gcc_9"]

  before_script:
    - 'command -v ssh-agent >/dev/null || ( apt-get update -y && apt-get install openssh-client -y )'
    - apt-get install -y rsync
    - mkdir -p ~/.ssh
    - chmod 700 ~/.ssh
    - eval $(ssh-agent -s)
    - echo "$SSH_PRIVATE_KEY_ELLADAN" | tr -d '\r' | ssh-add -
    - ssh-keyscan -t rsa elladan.irmb.bau.tu-bs.de >> ~/.ssh/known_hosts
    - pip3 install ansible

  script:
    - ansible-playbook -i ansible/hosts.cfg -u public_pages ansible/playbook_gcov.yml

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

  variables:
    SONAR_HOST_URL: "http://finrod.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=http://finrod.irmb.bau.tu-bs.de/sonarqube' >> conf/sonar-scanner.properties
    - echo 'sonar.sourceEncoding=UTF-8' >> conf/sonar-scanner.properties
    - cat conf/sonar-scanner.properties
    - export PATH+=:$(pwd)/bin
    - echo $PATH
    - sonar-scanner -v

  script:
    - cd $CI_PROJECT_DIR
    - sonar-scanner

###############################################################################
##                              Release                                      ##
###############################################################################
create_release:
  image: registry.gitlab.com/gitlab-org/release-cli:latest
  stage: release
  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