name: CI - Linux/OSX - Conda

on:
  push:
    branches:
      - devel
      - master
  pull_request:

jobs:
  build-with-conda:
    name: '[conda:${{ matrix.os }}:${{ matrix.build_type }}]'
    runs-on: ${{ matrix.os }}
    env:
      CCACHE_BASEDIR: ${GITHUB_WORKSPACE}
      CCACHE_DIR: ${GITHUB_WORKSPACE}/.ccache
      CCACHE_COMPRESS: true
      CCACHE_COMPRESSLEVEL: 5
      # Since conda will install a compiler, the compiler mtime will be changed.
      # This can invalidate the cache (https://ccache.dev/manual/latest.html#config_compiler_check)
      CCACHE_COMPILERCHECK: content
      # Help ccache manage generated files and PCH (https://ccache.dev/manual/latest.html#_precompiled_headers)
      CCACHE_SLOPPINESS: include_file_ctime,include_file_mtime,pch_defines,time_macros

    strategy:
      fail-fast: false
      matrix:
        build_type: [Release, Debug]
        os: [ubuntu-latest, macos-latest]

    steps:
    - uses: actions/checkout@v6
      with:
        submodules: recursive

    - uses: actions/cache@v5
      with:
        path: .ccache
        key: ccache-macos-linux-conda-${{ matrix.os }}-${{ matrix.build_type }}_${{ github.sha }}
        restore-keys: ccache-macos-linux-conda-${{ matrix.os }}-${{ matrix.build_type }}_

    # Conda compiler is named x86_64-conda-linux-gnu-c++, ccache can't resolve it
    # (https://ccache.dev/manual/latest.html#config_compiler_type)
    - name: Configure ccache with conda GCC [Conda]
      if: contains(matrix.os, 'ubuntu')
      run: |
        echo "CCACHE_COMPILERTYPE=gcc" >> "$GITHUB_ENV"

    - uses: conda-incubator/setup-miniconda@v3
      with:
        miniforge-version: latest
        activate-environment: tsid
        environment-file: .github/workflows/conda/environment.yml
        auto-activate-base: false
        auto-update-conda: true
        conda-remove-defaults: true

    - name: Print environment [Conda]
      shell: bash -l {0}
      run: |
        conda info
        conda list
        env

    - name: Clear ccache statistics [Conda]
      shell: bash -l {0}
      run: |
        ccache -z

    - name: Configure [Conda]
      shell: bash -l {0}
      run: |
        mkdir build
        cd build
        cmake .. -GNinja\
          -DCMAKE_C_COMPILER_LAUNCHER=ccache\
          -DCMAKE_CXX_COMPILER_LAUNCHER=ccache\
          -DCMAKE_INSTALL_PREFIX=${CONDA_PREFIX}\
          -DCMAKE_BUILD_TYPE=${{ matrix.build_type }}\
          -DBUILD_PYTHON_INTERFACE:BOOL=ON\
          -DPYTHON_EXECUTABLE=$(which python3)\
          -DINSTALL_DOCUMENTATION:BOOL=ON\
          -DBUILD_WITH_PROXQP:BOOL=ON\
          -DBUILD_WITH_OSQP:BOOL=ON

    - name: Build [Conda]
      shell: bash -l {0}
      run: |
        cd build
        ninja

    - name: Build documentation [Conda]
      shell: bash -l {0}
      run: |
        cd build
        ninja doc

    - name: Install [Conda]
      shell: bash -l {0}
      run: |
        cd build
        ninja install

    - name: Test [Conda]
      shell: bash -l {0}
      run: |
        find $CONDA_PREFIX -name tsid*
        python -c "import tsid"
        cd build
        ctest --output-on-failure

    - name: Uninstall [Conda]
      shell: bash -l {0}
      run: |
        cd build
        ninja uninstall

    - name: Print ccache statistics [Conda]
      shell: bash -l {0}
      run: |
        ccache -sv
