image: vladyslavusenko/b_image:latest

variables:
  GIT_SUBMODULE_STRATEGY: recursive
  BUILD_TYPE: Release
  CXX_MARCH: native
  CMAKE_INSTALL_PREFIX: /usr/
  DEB_DIR: deb

stages:
  - build
  - test
  - eval
  - eval_results
  - deploy

# template for docker builds with ccache
.prepare_docker_template: &prepare_docker_definition
  tags:
    - docker
  before_script:
    - mkdir -p ccache
    - export CCACHE_BASEDIR=${PWD}
    - export CCACHE_DIR=${PWD}/ccache
    - ccache -s
  cache:
    paths:
    - ccache/
    key: ${CI_JOB_NAME}

# template for build & unit test & make deb configurations
.compile_test_package_template: &compile_test_package_definition
  stage: build
  script:
    - mkdir build
    - cd build
    - cmake .. -DCMAKE_BUILD_TYPE=${BUILD_TYPE} -DCXX_MARCH=${CXX_MARCH} -DCMAKE_INSTALL_PREFIX=${CMAKE_INSTALL_PREFIX}
    - make -j3
    - ctest
    - make package
    - cd ..
    - mkdir $DEB_DIR
    - mv build/*.deb $DEB_DIR/
    - dpkg -i $DEB_DIR/*.deb
    # smoke test to see if all executables at least start up
    - basalt_calibrate --help
    - basalt_calibrate_imu --help
    - basalt_mapper --help
    - basalt_mapper_sim --help
    - basalt_mapper_sim_naive --help
    - basalt_opt_flow --help
    - basalt_vio --help
    - basalt_vio_sim --help

# template for build & unit test configurations (no deb)
.compile_test_template: &compile_test_definition
  stage: build
  script:
    - mkdir build
    - cd build
    - cmake .. -DCMAKE_BUILD_TYPE=${BUILD_TYPE} -DCXX_MARCH=${CXX_MARCH} -DCMAKE_INSTALL_PREFIX=${CMAKE_INSTALL_PREFIX}
    - make -j3
    - ctest
    # smoke test to see if all executables at least start up
    - ./basalt_calibrate --help
    - ./basalt_calibrate_imu --help
    - ./basalt_mapper --help
    - ./basalt_mapper_sim --help
    - ./basalt_mapper_sim_naive --help
    - ./basalt_opt_flow --help
    - ./basalt_vio --help
    - ./basalt_vio_sim --help

# template to test debian
.test_deb_template: &test_deb_definition
  stage: test
  tags:
    - docker
  script:
    - dpkg -i $DEB_DIR/*.deb
    # smoke test to see if all executables at least start up
    - basalt_calibrate --help
    - basalt_calibrate_imu --help
    - basalt_mapper --help
    - basalt_mapper_sim --help
    - basalt_mapper_sim_naive --help
    - basalt_opt_flow --help
    - basalt_vio --help
    - basalt_vio_sim --help

bionic-release-compile:
  <<: *prepare_docker_definition
  <<: *compile_test_package_definition
  only:
    - master
  variables:
    CXX_MARCH: 'sandybridge'
    DEB_DIR: deb_bionic
  artifacts:
    paths:
    - deb_bionic/*.deb
    - scripts/eval_full/*
    expire_in: 1 week

xenial-release-compile:
  <<: *prepare_docker_definition
  <<: *compile_test_package_definition
  image: vladyslavusenko/b_image_xenial:latest
  only:
    - master
  variables:
    CXX_MARCH: 'sandybridge'
    DEB_DIR: deb_xenial
  artifacts:
    paths:
    - deb_xenial/*.deb
    expire_in: 1 week

bionic-debug-compile:
  <<: *prepare_docker_definition
  <<: *compile_test_package_definition
  only:
    - master
  variables:
    BUILD_TYPE: CiDebug

bionic-relwithdebinfo-compile:
  <<: *prepare_docker_definition
  <<: *compile_test_package_definition
  variables:
    BUILD_TYPE: CiRelWithDebInfo

elcapitan-relwithdebinfo-compile:
  <<: *compile_test_definition
  tags: [macos, "10.11"]
  only:
    - master
  variables:
    BUILD_TYPE: CiRelWithDebInfo

mojave-relwithdebinfo-compile:
  <<: *compile_test_definition
  tags: [macos, "10.14"]
  only:
    - master
  variables:
    BUILD_TYPE: CiRelWithDebInfo

# check if clang-format would make any changes
clang-format:
  tags:
    - docker
  stage: build
  variables:
    GIT_SUBMODULE_STRATEGY: none
  script:
    - ./scripts/clang-format-all.sh
    # check if any files are now modified and error if yes
    - (if [ -n "`git diff --name-only --diff-filter=M --ignore-submodules`" ]; then echo $'\n    Some files are not properly formatted. You can use "./scripts/clang-format-all.sh".\n'; git diff --diff-filter=M; false; fi)

bionic-test:
  <<: *test_deb_definition
  only:
    - master
  variables:
    DEB_DIR: deb_bionic
    GIT_STRATEGY: none

xenial-test:
  <<: *test_deb_definition
  image: vladyslavusenko/b_image_xenial:latest
  only:
    - master
  variables:
    DEB_DIR: deb_xenial
    GIT_STRATEGY: none

# evaluate on EuRoC sequences
eval_euroc:
  stage: eval
  parallel: 10
  tags: [docker, dataset-eval]
  variables:
    GIT_STRATEGY: none
  only:
    - master
  artifacts:
    paths:
    - scripts/eval_full/eval_results/*
    expire_in: 1 week
  script:
    - dpkg -i deb_bionic/*.deb
    - cd scripts/eval_full
    - ./run_evaluations.sh

# aggregate results for all EuRoC sequences
gen_results_euroc:
  stage: eval_results
  variables:
    GIT_STRATEGY: none
  tags: 
    - docker
  when: on_success
  only:
    - master
  artifacts:
    paths:
    - euroc_results.txt
  script:
    - cd scripts/eval_full
    - ./gen_results.py eval_results > euroc_results.txt
    - cat euroc_results.txt
    - mv euroc_results.txt ../../

# deploy deb packages
deploy:
  stage: deploy
  when: manual
  variables:
    GIT_STRATEGY: none
  tags: 
    - docker
  only:
    - master
  before_script:
    - eval $(ssh-agent -s)
    - echo "$SSH_PRIVATE_KEY" | tr -d '\r' | ssh-add - > /dev/null
    - mkdir -p ~/.ssh
    - chmod 700 ~/.ssh
    - echo "$SSH_KNOWN_HOSTS" > ~/.ssh/known_hosts
    - chmod 644 ~/.ssh/known_hosts
  script:
    - scp $SCP_FLAGS deb_xenial/*.deb $REPOSITORY_URL/xenial/    
    - scp $SCP_FLAGS deb_bionic/*.deb $REPOSITORY_URL/bionic/

