image: docker:latest

variables:
  DOCKER_DRIVER: overlay
  BRANCH: $CI_REGISTRY_IMAGE:$CI_COMMIT_REF_NAME
  COMMIT: $CI_REGISTRY_IMAGE:$CI_COMMIT_SHA
  NIGHTLY: $CI_REGISTRY_IMAGE:nightly
  TAG: $CI_REGISTRY_IMAGE:$CI_COMMIT_TAG
  GSTLAL_DIR: $CI_PROJECT_DIR/opt/gstlal

before_script:
    # Set up directory structure and copy over built-dependencies from container:
    - mkdir public
    - mkdir -p ${GSTLAL_DIR}
    - cp -r /opt/gstlal/* ${GSTLAL_DIR}/
    
    # Update apt info
    - apt update
    
    # Define GstLAL build parameters:
    - export PATH="${GSTLAL_DIR}/bin:/usr/lib/ccache:/opt/local/libexec/ccache:${PATH}"
    - export LD_LIBRARY_PATH="${GSTLAL_DIR}/lib:${GSTLAL_DIR}/lib64:${LD_LIBRARY_PATH}"
    - export LIBRARY_PATH="${GSTLAL_DIR}/lib:${GSTLAL_DIR}/lib64:${LIBRARY_PATH}"
    - export PKG_CONFIG_PATH="${GSTLAL_DIR}/lib/pkgconfig:${GSTLAL_DIR}/lib64/pkgconfig:${PKG_CONFIG_PATH}"
    - export GI_TYPELIB_PATH="${GSTLAL_DIR}/lib/girepository-1.0:${GSTLAL_DIR}/lib64/girepository-1.0:${GI_TYPELIB_PATH}"
    - export GST_PLUGIN_PATH="${GSTLAL_DIR}/lib/gstreamer-1.0:${GSTLAL_DIR}/lib64/gstreamer-1.0:${GST_PLUGIN_PATH}"
    - export PYTHONPATH="${GSTLAL_DIR}/lib64/python2.7/site-packages:${GSTLAL_DIR}/lib/python2.7/site-packages:${PYTHONPATH}"
    - export GST_REGISTRY_1_0="${GSTLAL_DIR}/registry.bin"
    - export CCACHE_DIR=${PWD}/ccache
    
    # Define MKL environment variables:
    - export MKLROOT="/opt/intel/compilers_and_libraries_2018.3.222/linux/mkl"
    - export LIBRARY_PATH="/opt/intel/compilers_and_libraries_2018.3.222/linux/tbb/lib/intel64_lin/gcc4.7:/opt/intel/compilers_and_libraries_2018.3.222/linux/compiler/lib/intel64_lin:/opt/intel/compilers_and_libraries_2018.3.222/linux/mkl/lib/intel64_lin"
    - export CPATH="/opt/intel/compilers_and_libraries_2018.3.222/linux/mkl/include"
    - export PATH=PATH="/opt/intel/compilers_and_libraries_2018.3.222/linux/bin/intel64:${PATH}"
    - export PKG_CONFIG_PATH="/opt/intel/compilers_and_libraries_2018.3.222/linux/mkl/bin/pkgconfig:${PKG_CONFIG_PATH}"
    - export LD_LIBRARY_PATH="/opt/intel/compilers_and_libraries_2018.3.222/linux/compiler/lib/intel64:/opt/intel/compilers_and_libraries_2018.3.222/linux/compiler/lib/intel64_lin:/opt/intel/compilers_and_libraries_2018.3.222/linux/tbb/lib/intel64_lin/gcc4.7:/opt/intel/compilers_and_libraries_2018.3.222/linux/compiler/lib/intel64_lin:/opt/intel/compilers_and_libraries_2018.3.222/linux/mkl/lib/intel64_lin"
    
    # Define build and linking parameters:
    - export CFLAGS="-O3 -fPIC -DMKL_ILP64 -m64 -I${MKLROOT}/include -I${GSTLAL_DIR}/include"
    - export LDFLAGS="-L${GSTLAL_DIR}/lib -L${MKLROOT}/lib/intel64 -Wl,--no-as-needed -lmkl_intel_ilp64 -lmkl_sequential -lmkl_core -lpthread -lm -ldl"
    - export GSTLAL_FIR_WHITEN=0
    
cache:
  key: $CI_JOB_NAME
  paths:
    - ccache

stages:
    - level0
    - level1
    - level2
    - offline-test
    - nightly-pages

level0:gstlal:
  image: aepace/gstlal-dev:stretch-mkl
  stage: level0
  script:
    - echo $GSTLAL_FIR_WHITEN
    - cd gstlal
    - ./00init.sh
    - ./configure --prefix=${GSTLAL_DIR}
    - make
    - make install
  artifacts:
    expire_in: 3h
    paths:
      - ${GSTLAL_DIR}
      - gstlal
  only:
    - pushes
    - schedules
    
level1:gstlal-ugly:
  image: aepace/gstlal-dev:stretch-mkl
  stage: level1
  dependencies:
    - level0:gstlal
  script:
    - cd gstlal-ugly
    - ./00init.sh
    - ./configure --prefix=${GSTLAL_DIR}
    - make
    - make install
  artifacts:
    expire_in: 3h
    paths:
      - ${GSTLAL_DIR}
      - gstlal-ugly
  only:
    - pushes
    - schedules

level2:gstlal-calibration:
  image: aepace/gstlal-dev:stretch-mkl
  stage: level2
  dependencies:
    - level1:gstlal-ugly
  script:
    - cd gstlal-calibration
    - ./00init.sh
    - ./configure --prefix=${GSTLAL_DIR}
    - make
    - make install
  artifacts:
    expire_in: 3h
    paths:
      - ${GSTLAL_DIR}
      - gstlal-calibration
  only:
    - pushes
    - schedules

level2:gstlal-inspiral:
  image: aepace/gstlal-dev:stretch-mkl
  stage: level2
  dependencies:
    - level1:gstlal-ugly
  script:
    - cd gstlal-inspiral
    - ./00init.sh
    - ./configure --prefix=${GSTLAL_DIR}
    - make
    - make install
  artifacts:
    expire_in: 3h
    paths:
      - ${GSTLAL_DIR}
      - gstlal-inspiral
  only:
    - pushes
    - schedules
    
level2:gstlal-burst:
  image: aepace/gstlal-dev:stretch-mkl
  stage: level2
  dependencies:
    - level1:gstlal-ugly
  script:
    - cd gstlal-burst
    - ./00init.sh
    - ./configure --prefix=${GSTLAL_DIR}
    - make
    - make install
  artifacts:
    expire_in: 3h
    paths:
      - ${GSTLAL_DIR}
      - gstlal-burst
    when: always
  only:
    - pushes
    - schedules
  
offline-test:
  image: aepace/gstlal-dev:stretch-mkl
  stage: offline-test
  dependencies:
    - level2:gstlal-inspiral
  script:
    - cd gstlal-inspiral/tests
    - make -f Makefile_offline_tutorial_test
  artifacts:
    expire_in: 3h
    paths:
      - gstlal-inspiral/tests
  only:
    - pushes
    - schedules
  allow_failure: true
  
pages:
  image: aepace/gstlal-dev:stretch-mkl
  stage: nightly-pages
  script:
    - echo "Building Documentation"
    # Download and install sphynx, texlive, etc. I'm installing these here 
    # since they don't need to be part of the dev container for running, only
    # for building documentation. This shaves ~600MB off the size of the gstlal-dev
    # container:
    - apt-get install --assume-yes python-sphinx texlive-base texlive-binaries texlive-extra-utils texlive-font-utils texlive-fonts-extra texlive-fonts-extra-doc texlive-fonts-recommended
    - apt-get install --assume-yes texlive-fonts-recommended-doc texlive-generic-extra texlive-generic-recommended texlive-latex-base texlive-latex-base-doc texlive-latex-extra
    - apt-get install --assume-yes texlive-latex-extra-doc texlive-latex-recommended texlive-latex-recommended-doc texlive-pictures texlive-pictures-doc texlive-pstricks texlive-pstricks-doc texlive-science texlive-science-doc
    - export TMPDIR=tmp/
    - cd doc; make html
    - cd ..; cp -rf doc/_build/* public/
  dependencies:
    - level2:gstlal-inspiral
    - level2:gstlal-calibration
  artifacts:
    paths:
      - public
  only:
    - master@lscsoft/gstlal
    - schedules
  except:
    - web
    - pushes
