image: alpine:3.11

stages:
 - build
 - test
 - notify

.template:only:merge: &only-merge
  only:
    - merge_requests

.template:only:master: &only-master
  only:
    refs:
      - master

.template:only:tag: &only-tag
  only:
    - /^v.*$/
  except:
    - branches

#######################################################################################################################
# build + test

.template:build:
  dependencies: []
  stage: build
  tags:
    - altbuilder
  script:
    - extra/scripts/build.sh -d build -m Release -n -j8
  artifacts:
    paths:
      - build/
    expire_in: 1 day

.template:test:
  dependencies: []
  stage: test
  tags:
    - altbuilder
  script:
    - cd build
    - ctest . --output-on-failure -j $(grep -c processor /proc/cpuinfo)

.config:alpine: &distro_alpine
  <<: *only-merge
  image: alpine:3.11

.config:archlinux: &distro_archlinux
  <<: *only-merge
  image: archlinux/base

.config:builder: &distro_builder
  image: gitlab.fit.cvut.cz:5000/algorithms-library-toolkit/ci-docker-images/alt-builder:latest

.config:debian: &distro_debian
  <<: *only-merge
  image: amd64/debian:testing-slim

.config:opensuse:leap: &distro_opensuse-leap
  <<: *only-merge
  image: opensuse/leap:latest

.config:ubuntu:lts: &distro_ubuntu-lts
  <<: *only-merge
  image: ubuntu:latest

.config:ubuntu:rolling: &distro_ubuntu-rolling
  <<: *only-merge
  image: ubuntu:rolling

# -----------------------------------------------------------------------------

build:alpine:
  <<: *distro_alpine
  extends: .template:build
  before_script:
    - apk add --no-cache bash build-base cmake ninja python3 libexecinfo-dev libxml2-dev tclap-dev readline-dev qt5-qtbase-dev graphviz-dev jsoncpp-dev

test:alpine:
  <<: *distro_alpine
  extends: .template:test
  dependencies:
    - build:alpine
  before_script:
    - apk add --no-cache bash bc coreutils python3 cmake libexecinfo libxml2 tclap readline qt5-qtbase qt5-qtbase-x11 graphviz jsoncpp

# -----------------------------------------------------------------------------

build:archlinux:
  <<: *distro_archlinux
  extends: .template:build
  before_script:
    - pacman -Syu --noconfirm base-devel cmake ninja python libxml2 tclap readline qt5-base graphviz jsoncpp

test:archlinux:
  <<: *distro_archlinux
  extends: .template:test
  dependencies:
    - build:archlinux
  before_script:
    - pacman -Syu --noconfirm base-devel bash bc coreutils cmake ninja python libxml2 tclap readline qt5-base graphviz jsoncpp

# -----------------------------------------------------------------------------

.build:builder:
  <<: *distro_builder
  extends: .template:build

.test:builder:
  <<: *distro_builder
  extends: .template:test

build:builder:gcc:
  extends: .build:builder

#build:builder:clang:
#  extends: .build:builder
#  variables:
#    CXX: clang++

build:builder:clang-sanitizers:
  extends: .build:builder
  variables:
    CXX: clang++
    CXXFLAGS: "-fsanitize=address -fsanitize=undefined -fno-sanitize-recover=all"

test:builder:gcc:
  extends: .test:builder
  dependencies:
    - build:builder:gcc

#test:builder:clang:
#  extends: .test:builder
#  dependencies:
#    - build:builder:clang

test:builder:clang-sanitizers:
  extends: .test:builder
  dependencies:
    - build:builder:clang-sanitizers

# -----------------------------------------------------------------------------

build:debian:
  <<: *distro_debian
  extends: .template:build
  before_script:
    - apt-get update && DEBIAN_FRONTEND=noninteractive apt-get install -y build-essential cmake ninja-build python3 libxml2-dev libtclap-dev libreadline-dev qtbase5-dev graphviz-dev libjsoncpp-dev

test:debian:
  <<: *distro_debian
  extends: .template:test
  dependencies:
    - build:debian
  before_script:
    - apt-get update && DEBIAN_FRONTEND=noninteractive apt-get install -y build-essential bash cmake libxml2 libreadline8 libqt5widgets5 libqt5xml5 graphviz libjsoncpp1

# -----------------------------------------------------------------------------

build:opensuse:leap:
  <<: *distro_opensuse-leap
  extends: .template:build
  before_script:
    - zypper -n install python3 cmake gcc-c++ bash ninja libxml2-devel tclap readline-devel libQt5Widgets-devel libQt5Xml-devel graphviz-devel jsoncpp-devel

test:opensuse:leap:
  <<: *distro_opensuse-leap
  extends: .template:test
  dependencies:
    - build:opensuse:leap
  before_script:
    - zypper -n install bash cmake libxml2 readline libQt5Widgets5 libQt5Xml5 graphviz libjsoncpp19

# -----------------------------------------------------------------------------

build:ubuntu:lts:
  <<: *distro_ubuntu-lts
  extends: .template:build
  before_script:
    - apt-get update && DEBIAN_FRONTEND=noninteractive apt-get install -y build-essential cmake ninja-build python3 libxml2-dev libtclap-dev libreadline-dev qtbase5-dev graphviz-dev libjsoncpp-dev

test:ubuntu:lts:
  <<: *distro_ubuntu-lts
  extends: .template:test
  dependencies:
    - build:ubuntu:lts
  before_script:
    - apt-get update && DEBIAN_FRONTEND=noninteractive apt-get install -y build-essential bash cmake libxml2 libreadline8 libqt5widgets5 libqt5xml5 graphviz libjsoncpp1

# -----------------------------------------------------------------------------

build:ubuntu:rolling:
  <<: *distro_ubuntu-rolling
  extends: .template:build
  before_script:
    - apt-get update && DEBIAN_FRONTEND=noninteractive apt-get install -y build-essential cmake ninja-build python3 libxml2-dev libtclap-dev libreadline-dev qtbase5-dev graphviz-dev libjsoncpp-dev

test:ubuntu:rolling:
  <<: *distro_ubuntu-rolling
  extends: .template:test
  before_script:
    - apt-get update && DEBIAN_FRONTEND=noninteractive apt-get install -y build-essential bash cmake libxml2 libreadline8 libqt5widgets5 libqt5xml5 graphviz libjsoncpp1
  dependencies:
    - build:ubuntu:rolling


#######################################################################################################################
# doc

build:doc:
  stage: build
  before_script:
    - apk add --no-cache doxygen graphviz cmake ninja python3
  script:
    - mkdir -p build && cd build
    - ../CMake/generate.py -wm
    - cmake -DBUILD_TYPE=Snapshot -DCMAKE_EXPORT_COMPILE_COMMANDS=ON -DDOXYGEN_ONLY=1 -GNinja ..
    - ninja doxygen
  artifacts:
    paths:
      - apidoc/
    expire_in: 1 day

#######################################################################################################################
# static analysis

.template:static-analysis:
  stage: build
  image: gitlab.fit.cvut.cz:5000/algorithms-library-toolkit/ci-docker-images/alt-builder:latest
  before_script:
    - mkdir -p build && pushd build
    - ../CMake/generate.py -wm
    - cmake -DBUILD_TYPE=Debug -DCMAKE_EXPORT_COMPILE_COMMANDS=ON .. -GNinja
    - popd
  allow_failure: true # TODO: Remove
  dependencies: []

# -----------------------------------------------------------------------------

static-analysis:cppcheck:
  extends: .template:static-analysis
  script:
    - cppcheck -q --enable=all --project=build/compile_commands.json --suppress="*:*/contrib/*" --error-exitcode=1


static-analysis:clang-tidy:
  extends: .template:static-analysis
  script:
    - jq ".[].file" build/compile_commands.json | tr -d "\"" | grep -v "test-src" | xargs -n1 -P$(grep -c processor /proc/cpuinfo) clang-tidy -p build/


#######################################################################################################################
# notify

.template:notify:
  stage: notify
  before_script:
    - apk add --no-cache curl
  dependencies: []

# -----------------------------------------------------------------------------

notify:pyalib:
  <<: *only-master
  extends: .template:notify
  script:
    - curl -X POST -F token="$TOKEN_TRIGGER_PYALIB" -F ref=master https://gitlab.fit.cvut.cz/api/v4/projects/11497/trigger/pipeline

notify:release:stable:
  <<: *only-tag
  extends: .template:notify
  script:
    - curl -X POST -F token="$TOKEN_TRIGGER_RELEASE" -F ref=master -F "variables[RELEASE_REF]=$CI_COMMIT_TAG" -F "variables[RELEASE_MODE]=stable"   https://gitlab.fit.cvut.cz/api/v4/projects/17683/trigger/pipeline

notify:release:snapshot:
  <<: *only-master
  extends: .template:notify
  script:
    - curl -X POST -F token="$TOKEN_TRIGGER_RELEASE" -F ref=master -F "variables[RELEASE_REF]=$CI_COMMIT_SHA"  -F "variables[RELEASE_MODE]=snapshot" https://gitlab.fit.cvut.cz/api/v4/projects/17683/trigger/pipeline
