# NOTE #1: Our GitLab CI runs jobs inside Alpine Linux container by default.
#
# NOTE #2: Keep in mind that each job (e.g. test-gcc) is executed in a separate
# isolated environment (it may be even on different machine). There are no
# files implicitly passed between jobs or stages, only artifacts and maybe
# cache (depends on configuration).

image: alpine:3.7

before_script:
  - case "$CI_BUILD_NAME" in
      *-clang) export CXX=clang++ EXTRA_PKGS="$EXTRA_PKGS clang llvm5-dev";;
      *) export CXX=g++;;
    esac
  - export JOBS=$(grep -c processor /proc/cpuinfo)
  - apk add --no-cache build-base bash libexecinfo-dev
        cppunit-dev libxml2-dev tclap-dev readline-dev $EXTRA_PKGS
  - case "$CI_BUILD_NAME" in
      *-clang) ln -s /usr/lib/llvm5/lib/LLVMgold.so /usr/lib/LLVMgold.so;;
      *) ;;
    esac


#==========  Stage build  ==========

.build-cmake: &build-cmake
  variables:
    EXTRA_PKGS: cmake python3 py3-click
  script:
    - cd CMake
    - ./alib_cmake.py -w -m
    - cd ..
    - mkdir release
    - cd release
    - cmake -DCMAKE_BUILD_TYPE=Release ..
    - make
    - mkdir bin
    - cd bin
    - find .. -type f \( -perm -u=x -o -perm -g=x -o -perm -o=x \) -exec test -x {} \; -print | grep -v 'CMakeFiles' | while read line; do ln -s $line $(basename $line); done
    - cp ../../translateAddresses .
    - cp ../../xmlFormat .
  artifacts:
    paths:
      - release/bin
      - "release/*/a*2" #select binaries
      - release/tniceprint/tniceprint #explicit binary
      - "release/*/lib*.so" #select libraries
      - "release/*/test-alib2*" #select libraries test
    expire_in: 1 day
  stage: build

build-cmake-clang:
  <<: *build-cmake

build-cmake-gcc:
  <<: *build-cmake

build-doc:
  variables:
    EXTRA_PKGS: doxygen graphviz
  script:
    - doxygen
  allow_failure: true
  artifacts:
    name: docs
    paths:
      - doc/
    expire_in: 1 week
  stage: build


#==========  Stage test  ==========

.test-cmake: &test-cmake
  variables:
    # coreutils are needed because of timeout command used in tests.
    EXTRA_PKGS: bc coreutils python3
  script:
    - cd release
    - for test in $(ls */test-alib2*); do ./${test} || exit 1; done
    - cd ..
    - for test in $(ls tests.*.sh); do ./${test} release/bin ${JOBS} || exit 1; touch release/bin/log_tests.txt; cat release/bin/log_tests.txt; done
  stage: test

test-cmake-gcc:
  <<: *test-cmake
  dependencies:
    - build-cmake-gcc

test-cmake-clang:
  <<: *test-cmake
  dependencies:
    - build-cmake-clang
