name: Python bindings

on:
   push:
      branches: [ RC_1_2 RC_2_0 master ]
   pull_request:

concurrency:
   group: ${{ github.ref }}-${{ github.workflow }}-${{ github.event_name }}
   cancel-in-progress: true

jobs:

  test:
    name: build
    runs-on: ${{ matrix.os }}

    strategy:
      matrix:
        os: [ubuntu-20.04, macos-latest, windows-latest ]

    steps:
    - uses: actions/checkout@v2
      with:
        submodules: true

    - name: dependencies (MacOS)
      if: runner.os == 'macOS'
      run: |
        brew update
        brew install boost-build boost boost-python3 python@3.10 openssl@1.1
        export PATH=$(brew --prefix)/opt/python@3.10/bin:$PATH

    - name: update package lists (linux)
      if: runner.os == 'Linux'
      continue-on-error: true
      run: |
        sudo apt update

    - uses: Chocobo1/setup-ccache-action@v1
      if: runner.os != 'Windows'
      with:
        update_packager_index: false
        override_cache_key: ccache-python-${{ matrix.os }}-${{ github.base_ref }}
        ccache_options: |
          max_size=500M

    - name: dependencies (linux)
      if: runner.os == 'Linux'
      run: |
        sudo apt install libboost-tools-dev libboost-python-dev libboost-dev libboost-system-dev python3 python3-setuptools libssl-dev

    - name: install boost (windows)
      if: runner.os == 'Windows'
      shell: cmd
      run: |
        git clone --depth=1 --recurse-submodules -j10 --branch=boost-1.78.0 https://github.com/boostorg/boost.git
        cd boost
        bootstrap.bat

    - name: boost headers (windows)
      if: runner.os == 'Windows'
      shell: cmd
      run: |
        cd boost
        .\b2 headers

    - name: install openssl (windows)
      if: runner.os == 'Windows'
      uses: nick-invision/retry@v2
      with:
        shell: cmd
        timeout_minutes: 5
        retry_wait_seconds: 4
        max_attempts: 3
        command: choco install openssl --limitoutput --no-progress

    - name: build/install (windows)
      if: runner.os == 'Windows'
      shell: cmd
      run: |
        set BOOST_ROOT=%CD%\boost
        set BOOST_BUILD_PATH=%BOOST_ROOT%\tools\build
        set PATH=%BOOST_ROOT%;%PATH%

        cd bindings\python
        python setup.py build_ext install --user --prefix=

    - name: tests (windows)
      if: runner.os == 'Windows'
      shell: cmd
      run: |
        cd bindings\python
        python test.py

    - name: build no-deprecated (Linux)
      if: runner.os == 'Linux'
      run: |
        cd bindings/python
        python3 setup.py build_ext --b2-args "deprecated-functions=off"

    - name: build/install (Linux)
      if: runner.os == 'Linux'
      run: |
        cd bindings/python
        python3 setup.py build_ext install --user --prefix=

    - name: build/install (MacOS)
      if: runner.os == 'macOS'
      run: |
        # Install to Homebrew's python site-packages. no need for --user and --prefix
        cd bindings/python
        export PATH=$(brew --prefix)/opt/python@3.10/bin:$PATH
        python3 setup.py build_ext install --install-lib $(brew --prefix)/lib/python3.10/site-packages

    - name: tests (Linux)
      if: runner.os == 'Linux'
      run: |
        cd bindings/python
        python3 test.py

    - name: tests (MacOS)
      if: runner.os == 'macOS'
      run: |
        cd bindings/python
        export PATH=$(brew --prefix)/opt/python@3.10/bin:$PATH
        python3 test.py
