# This ensures only commits on master, PRs towards master, and all tags are built.
# Otherwise, build does not start.
if: branch = master OR tag IS present

language: cpp

os: linux

dist: xenial

stages:
  - ccpp
  - python

env:
  global:
    - TWINE_USERNAME=Martinsos
    # Encrypted TWINE_PASSWORD:
    - secure: "VfgRi/hcUMLDYzATLS8JOuZo+wg0VNCmEojIJ3gpy89LAXgPN1Qx/2n3c52PPgFWgWFdDgns2SdxhQ1/810qb+65choy2fOgTULnyFcH3ZFTM5nINVHlGB5dLDUesKjrqqfIKHnwB6LAskHqHHPm/qv7WxUjQbcpR6mICsKB7h3NI7yUSc3tRPmd8tVBc/mTUXpMRVznTxGFCo/rC4f2y57GI9odjDxS4VqGS8T9Igvgyteg+ougXYbl84FIi3Bij6TQITa1rP9cFw/lfXMTM2m1czTonzAbgGPC6q7798MCGhLpEdxr+Zu9NPCS8aWFYdxMUYmsisjffDtTdIagZFh/14YrnQb2RxqnLTqSi20E+xXx16GcHhnMOxw/sSOh6HUEaXcbLNtx/mqMEa7mQEcIFBdXHWs/czTHz8xEoGnHIVHI5zqItoK3nUD0W/X6XUex8GP8obvjSKVw2k8X/0bZUaqSyNVbTfYcLmOk53Kx8vfXd82BbTOWiJVZmvuugqrshIEaLOyRhSaoC4Bj5KjfijM/gKPa+n8wVEQ/4hF4HpJKr/33cAm1URDGsDnfvHfxfeEwizTq8gCIxnbpOzSsd1M6+3MQLr48DE8uYM+wkG2F+KsCKe3OexLiaqHG7rVZh1R99nOykoqfjhxek3H1KGfx7qFbdK80AaVBiQo="

.ccpp: &ccpp
  stage: ccpp # Builds C/CPP code.
    
  before_install:
    - sudo -H pip3 install -U pip # Needed for Meson.
    - sudo -H pip3 install -U setuptools # Needed for Meson.
    - eval "${MATRIX_EVAL}"
    
  install:
    - sudo -H pip3 install meson ninja # We use Ninja both with Meson and CMake.
    
  script:
    # Build library and binaries using Meson and test them.
    # Static library.
    - make CXXFLAGS="-Werror" LIBRARY_TYPE=static BUILD_DIR=meson-build-static
    # Shared library.
    - make CXXFLAGS="-Werror" LIBRARY_TYPE=shared BUILD_DIR=meson-build-shared

    # Check for memory leaks.
    # I run this only on linux because osx returns errors from
    # system libraries, which I would have to supress.
    - |
      if [ $TRAVIS_OS_NAME == "linux" ]; then
        make check-memory-leaks BUILD_DIR=meson-build-static
      fi

    # Build library and binaries with CMake and test them.
    - mkdir -p build && cd build
    - CXXFLAGS="-Werror" cmake -GNinja ..
    - ninja -v
    - bin/runTests
    - cd ..

.python: &python
  stage: python # Builds python binding.

  install:
    - wget -qO- https://bootstrap.pypa.io/get-pip.py | python3
    - python3 -m pip install cibuildwheel==1.6.1

  before_script:
    - cd bindings/python

  script:
    # Build the wheels, put them into './wheelhouse'.
    # cibuildwheel needs to be run from python directory.
    # The python directory is mounted in the docker build containers,
    # so we need to copy the edlib source before running cibuild wheel.
    - make edlib # Copies edlib source to python dir.
    - |
      CIBW_SKIP="cp27-* pp* *-manylinux_i686" \
      CIBW_BEFORE_BUILD="make sdist" \
      CIBW_TEST_COMMAND="python3 {project}/test.py" \
      python3 -m cibuildwheel --output-dir wheelhouse
    - if [ $DEPLOY_SDIST_TO_PYPI ]; then make sdist; fi

    # Deploy wheel(s) and source (sdist) to PYPI (if commit is tagged).
    # NOTE: We are doing this in script section, and not in after_success,
    #   because we want build to fail if deploying fails, which does not
    #   happen if deployment is done inside after_success (that is how
    #   travis works).
    # NOTE: We check TRAVIS_TEST_RESULT because we don't want to deploy
    #   if building failed.
    - |
      if [ $TRAVIS_TEST_RESULT -eq 0 ]; then
        if [ $TRAVIS_TAG ]; then
          # upgrade and upgrade-strategy-eager are here because of https://travis-ci.community/t/cant-deploy-to-pypi-anymore-pkg-resources-contextualversionconflict-importlib-metadata-0-18/10494/14 .
          python3 -m pip install --upgrade --upgrade-strategy eager twine
          python3 -m twine upload wheelhouse/*.whl
          if [ $DEPLOY_SDIST_TO_PYPI ]; then
            python3 -m twine upload dist/edlib-*.tar.gz
          fi
        else
          echo "Skipping twine upload because not a tag, files built:"
          ls -l wheelhouse
          ls -l dist
        fi
      fi

jobs:
  include:
    # In order to catch as many weird edge cases in the code with -Werror as
    # possible, we want to test a large range of old and new compilers on both
    # Linux and macOS. This gives us the best possible coverage while maintaining
    # compatibility with a large number of compilers.
    - name: "C/CPP (Linux; GCC 4.9)"
      os: linux
      addons:
        apt:
          packages:
            - g++-4.9
            - valgrind
            - python3
            - python3-pip
      env:
         - MATRIX_EVAL="export CC=gcc-4.9 && export CXX=g++-4.9"
      <<: *ccpp

    - name: "C/CPP (Linux; GCC 9)"
      os: linux
      addons:
        apt:
          sources:
            - ubuntu-toolchain-r-test # For g++-9
          packages:
            - g++-9
            - valgrind
            - python3
            - python3-pip
      env:
        - MATRIX_EVAL="export CC=gcc-9 && export CXX=g++-9"
      <<: *ccpp

    - name: "C/CPP (Linux; Clang 5.0)"
      os: linux
      addons:
        apt:
          packages:
            - clang-5.0
            - valgrind
            - python3
            - python3-pip
      env:
        - MATRIX_EVAL="export CC=clang-5.0 && export CXX=clang++-5.0"
      <<: *ccpp

    - name: "C/CPP (OSX; XCode 11; Clang)"
      os: osx
      osx_image: xcode11
      <<: *ccpp

    - name: "Python wheel (Linux)"
      os: linux
      language: python
      python:
        - "3.6"
      services: docker
      env:
        # We need to upload sdist only once, since it is same for all jobs,
        # so with this flag we specify which job will take care of that (this one).
        - DEPLOY_SDIST_TO_PYPI="true"
      <<: *python

    - name: "Python wheel (OSX)"
      os: osx
      language: shell
      <<: *python

    # TODO: Add windows x64 and linux aarch64 python wheel builds? Edlib users said they need them.
