# 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.6

before_script:
  - case "$CI_BUILD_NAME" in
      *-clang) export CXX=clang++ EXTRA_PKGS="$EXTRA_PKGS clang llvm4-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/llvm4/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-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