# ---------------
# Conda CI for Fr
# ---------------

include:
  # https://computing.docs.ligo.org/gitlab-ci-templates/
  - project: computing/gitlab-ci-templates
    # https://computing.docs.ligo.org/gitlab-ci-templates/conda/
    file: conda.yml
  # local test configuration
  - local: '/.gitlab/ci/test.yml'

.conda:
  image: igwn/base:conda
  extends: .conda:base

# -- build with conda

build:conda:
  stage: binary packages
  extends: .conda
  needs: [dist]
  variables:
    GIT_STRATEGY: none
  before_script:
    - !reference [".conda:base", before_script]
    - conda create --quiet --yes --name build
          cmake
          c-compiler
          make
          numpy
          python
          ctypesgen
    - conda activate build
    - PREFIX=${CI_PROJECT_DIR}/installdir
    - tar -xf framel-*.tar.* --strip-components=1
  script:
    - mkdir build
    - cd build
    - cmake ..
          -DBUILD_TESTING:BOOL=yes
          -DCMAKE_BUILD_TYPE:STRING=Debug
          -DCMAKE_INSTALL_PREFIX:PATH="${PREFIX}"
          -DENABLE_C:BOOL=yes
          -DENABLE_MATLAB:BOOL=no
          -DENABLE_PACKAGING:BOOL=no
          -DENABLE_PYTHON:BOOL=yes
    - cmake --build . --verbose
    - ctest --verbose --target install
    - cmake --build . --verbose --target install
  artifacts:
    paths:
      - installdir

build:conda:split:
  extends: build:conda
  script:
    - mkdir build
    - cd build
    - cmake ..
          -DBUILD_TESTING:BOOL=yes
          -DCMAKE_BUILD_TYPE:STRING=Debug
          -DCMAKE_INSTALL_PREFIX:PATH=${PREFIX}
          -DENABLE_C:BOOL=yes
          -DENABLE_MATLAB:BOOL=no
          -DENABLE_PACKAGING:BOOL=no
          -DENABLE_PYTHON:BOOL=no
    - cmake --build . --verbose
    - ctest --verbose --target install
    - cmake --build . --verbose --target install
    - cd ../
    # build Python library
    - mkdir build_python
    - cd build_python
    - cmake ..
          -DCMAKE_BUILD_TYPE:STRING=Debug
          -DCMAKE_INSTALL_PREFIX:PATH=${PREFIX}
          -DCMAKE_LIBRARY_PATH:PATH=${PREFIX}/lib
          -DENABLE_C:BOOL=no
          -DENABLE_MATLAB:BOOL=no
          -DENABLE_PACKAGING:BOOL=no
          -DENABLE_PYTHON:BOOL=yes
    - cmake --build Python/ --verbose
    - cmake --build Python/ --verbose --target install

# -- test the conda-based builds

test:conda:
  image: igwn/base:conda
  extends:
    - .conda:base
    - .test
  needs:
    - build:conda
  before_script:
    - !reference [".conda:base", before_script]
    - conda create --quiet --yes --name test
          python
          pytest
          numpy
    - conda activate test
    # set variables to point at the custom install location
    - export LD_LIBRARY_PATH="${CI_PROJECT_DIR}/installdir/lib:${LD_LIBRARY_PATH}"
    - export PATH="${CI_PROJECT_DIR}/installdir/bin:${PATH}"
    - |
      export PYTHONPATH=${CI_PROJECT_DIR}/installdir/$(python -c "import sysconfig; print(sysconfig.get_path('platlib', vars={'base': '.', 'platbase': '.', 'platlibdir': 'lib'}))")

test:conda:split:
  extends: test:conda
  needs:
    - build:conda:split
