# 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
  - echo USE_RAMDISK=0 >> build.conf
  - case "$CI_BUILD_NAME" in
      *-clang) echo USE_LTO=1 >> build.conf;;
      *) echo USE_LTO=0 >> build.conf;;
    esac
  - echo USE_MARCH= >> build.conf


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

.build: &build
  script:
    - make release
    - make install-release
  artifacts:
    paths:
      - bin-release
      - "*/bin-release"
      - "*/lib-release"
    expire_in: 1 day
  stage: build

build-gcc:
  <<: *build

build-clang:
  <<: *build

build-cmake-clang:
  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-doc:
  variables:
    EXTRA_PKGS: doxygen graphviz
  script:
    - make doc
  allow_failure: true
  artifacts:
    name: docs
    paths:
      - alib2std/doc/
      - alib2data/doc/
      - alib2common/doc/
      - alib2str/doc/
      - alib2raw/doc/
      - alib2algo/doc/
      - alib2elgo/doc/
      - alib2measurepp/doc/
    expire_in: 1 week
  stage: build


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

.test: &test
  variables:
    # coreutils are needed because of timeout command used in tests.
    EXTRA_PKGS: bc coreutils python3
  script:
    - make test-release
  stage: test

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

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

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