name: CMake build
on: [push, pull_request]
jobs:
  cmake-linux:
    runs-on: ubuntu-latest
    steps:
    - uses: actions/checkout@v4
    - name: Setup
      run: |
        sudo rm /etc/apt/sources.list.d/microsoft-prod.list
        sudo apt update
        sudo apt install -y libboost-dev ccache ninja-build
    - name: Cache
      uses: hendrikmuhs/ccache-action@v1.2
      with:
        key: cmake-linux-ci-build-${{ github.ref }}
        restore-keys: |
          cmake-linux-ci-build-${{ github.ref }}
          cmake-linux-ci-build-refs/heads/master
          cmake-linux-ci-build-
    - name: Compile
      run: |
        mkdir build
        cd build
        cmake .. -GNinja -DBOOST_ROOT=/usr -DCMAKE_BUILD_TYPE=Release -DQL_COMPILE_WARNING_AS_ERROR=ON -DCMAKE_CXX_COMPILER_LAUNCHER=ccache -L
        cat ql/config.hpp
        cmake --build . --verbose
        sudo cmake --install .
        sudo ldconfig
    - name: Test
      run: |
        quantlib-test-suite --log_level=message
    - name: Run benchmark
      run: |
        quantlib-benchmark --size=1
  cmake-linux-with-options:
    runs-on: ubuntu-latest
    steps:
    - uses: actions/checkout@v4
    - name: Setup
      run: |
        sudo rm /etc/apt/sources.list.d/microsoft-prod.list
        sudo apt update
        sudo apt install -y libboost-all-dev ccache ninja-build
    - name: Cache
      uses: hendrikmuhs/ccache-action@v1.2
      with:
        key: cmake-linux-ci-opts-${{ github.ref }}
        restore-keys: |
          cmake-linux-ci-opts-${{ github.ref }}
          cmake-linux-ci-opts-refs/heads/master
          cmake-linux-ci-opts-
    - name: Compile
      run: |
        cmake --preset linux-ci-build-with-nonstandard-options -L
        cd build/linux-ci-build-with-nonstandard-options
        cat ql/config.hpp
        cmake --build . --verbose
        sudo cmake --build . --target install
    - name: Test
      run: |
        quantlib-test-suite --log_level=message
  cmake-win:
    runs-on: windows-2022
    steps:
    - uses: actions/checkout@v4
    - name: Cache
      uses: hendrikmuhs/ccache-action@v1.2
      with:
        key: cmake-windows-ci-build-${{ github.ref }}
        restore-keys: |
          cmake-windows-ci-build-${{ github.ref }}
          cmake-windows-ci-build-refs/heads/master
          cmake-windows-ci-build-
        variant: sccache
    - name: Setup
      run: |
        $Url = "https://archives.boost.io/release/1.88.0/source/boost_1_88_0.zip"
        (New-Object System.Net.WebClient).DownloadFile($Url, "$RUNNER_TEMP\boost.zip")
        Expand-Archive -Path "$RUNNER_TEMP\boost.zip" -DestinationPath C:\local
        Rename-Item -Path "C:\local\boost_1_88_0" -NewName "boost"
        $Url = "https://github.com/ninja-build/ninja/releases/download/v1.11.1/ninja-win.zip"
        (New-Object System.Net.WebClient).DownloadFile($Url, "$RUNNER_TEMP\ninja-win.zip")
        Expand-Archive -Path "$RUNNER_TEMP\ninja-win.zip" -DestinationPath C:\local\ninja
        Add-Content $env:GITHUB_PATH "C:\local\ninja"
    - name: Setup MSVC++ environment
      uses: ilammy/msvc-dev-cmd@v1
      with:
        arch: x64
        vsversion: 2022
    - name: Compile
      env:
        BOOST_ROOT: C:\local\boost
      shell: cmd
      run: |
        mkdir build
        cd build
        cmake .. -GNinja -DCMAKE_BUILD_TYPE=Release -DQL_COMPILE_WARNING_AS_ERROR=ON -DCMAKE_CXX_COMPILER_LAUNCHER=sccache -L
        type ql\config.hpp
        cmake --build . --verbose
        cmake --install .
        dir ql\*.lib
    - name: Test
      run: |
        & "C:\Program Files (x86)\QuantLib\bin\quantlib-test-suite" --log_level=message
  cmake-win-dynamic-runtime:
    runs-on: windows-2022
    steps:
    - uses: actions/checkout@v4
    - name: Cache
      uses: hendrikmuhs/ccache-action@v1.2
      with:
        key: cmake-windows-ci-dyn-build-${{ github.ref }}
        restore-keys: |
          cmake-windows-ci-dyn-build-${{ github.ref }}
          cmake-windows-ci-dyn-build-refs/heads/master
          cmake-windows-ci-dyn-build-
        variant: sccache
    - name: Setup
      run: |
        $Url = "https://archives.boost.io/release/1.88.0/source/boost_1_88_0.zip"
        (New-Object System.Net.WebClient).DownloadFile($Url, "$RUNNER_TEMP\boost.zip")
        Expand-Archive -Path "$RUNNER_TEMP\boost.zip" -DestinationPath C:\local
        Rename-Item -Path "C:\local\boost_1_88_0" -NewName "boost"
        $Url = "https://github.com/ninja-build/ninja/releases/download/v1.11.1/ninja-win.zip"
        (New-Object System.Net.WebClient).DownloadFile($Url, "$RUNNER_TEMP\ninja-win.zip")
        Expand-Archive -Path "$RUNNER_TEMP\ninja-win.zip" -DestinationPath C:\local\ninja
        Add-Content $env:GITHUB_PATH "C:\local\ninja"
    - name: Setup MSVC++ environment
      uses: ilammy/msvc-dev-cmd@v1
      with:
        arch: x64
        vsversion: 2022
    - name: Compile
      env:
        BOOST_ROOT: C:\local\boost
      shell: cmd
      run: |
        mkdir build
        cd build
        cmake .. -GNinja -DCMAKE_MSVC_RUNTIME_LIBRARY=MultiThreadedDLL -DCMAKE_BUILD_TYPE=Release -DQL_COMPILE_WARNING_AS_ERROR=ON -DCMAKE_CXX_COMPILER_LAUNCHER=sccache -L
        type ql\config.hpp
        cmake --build . --verbose
        cmake --install .
        dir ql\*.lib
    - name: Test
      run: |
        & "C:\Program Files (x86)\QuantLib\bin\quantlib-test-suite" --log_level=message
  cmake-win-with-options:
    runs-on: windows-2022
    steps:
    - uses: actions/checkout@v4
    - name: Cache
      uses: hendrikmuhs/ccache-action@v1.2
      with:
        key: cmake-windows-ci-opts-${{ github.ref }}
        restore-keys: |
          cmake-windows-ci-opts-${{ github.ref }}
          cmake-windows-ci-opts-refs/heads/master
          cmake-windows-ci-opts-
        variant: sccache
    - name: Setup
      run: |
        $Url = "https://archives.boost.io/release/1.88.0/source/boost_1_88_0.zip"
        (New-Object System.Net.WebClient).DownloadFile($Url, "$RUNNER_TEMP\boost.zip")
        Expand-Archive -Path "$RUNNER_TEMP\boost.zip" -DestinationPath C:\local
        Rename-Item -Path "C:\local\boost_1_88_0" -NewName "boost"
        $Url = "https://github.com/ninja-build/ninja/releases/download/v1.11.1/ninja-win.zip"
        (New-Object System.Net.WebClient).DownloadFile($Url, "$RUNNER_TEMP\ninja-win.zip")
        Expand-Archive -Path "$RUNNER_TEMP\ninja-win.zip" -DestinationPath C:\local\ninja
        Add-Content $env:GITHUB_PATH "C:\local\ninja"
    - name: Setup MSVC++ environment
      uses: ilammy/msvc-dev-cmd@v1
      with:
        arch: x64
        vsversion: 2022
    - name: Compile
      env:
        BOOST_ROOT: C:\local\boost
      shell: cmd
      run: |
        cmake --preset windows-ci-build-with-nonstandard-options -L
        cd build/windows-ci-build-with-nonstandard-options
        type ql\config.hpp
        cmake --build . --verbose
        cmake --build . --target install
    - name: Test
      run: |
        & "C:\Program Files (x86)\QuantLib\bin\quantlib-test-suite" --log_level=message
  cmake-macos:
    runs-on: macos-latest
    steps:
    - uses: actions/checkout@v4
    - name: Setup
      env:
        HOMEBREW_NO_INSTALLED_DEPENDENTS_CHECK: 1
      run: |
        brew install boost ccache ninja
    - name: Cache
      uses: hendrikmuhs/ccache-action@v1.2
      with:
        key: cmake-macos-ci-${{ github.ref }}
        restore-keys: |
          cmake-macos-ci-${{ github.ref }}
          cmake-macos-ci-refs/heads/master
          cmake-macos-ci-
    - name: Compile
      run: |
        mkdir build
        cd build
        cmake .. -DCMAKE_BUILD_TYPE=Release -DQL_COMPILE_WARNING_AS_ERROR=ON -DCMAKE_CXX_COMPILER_LAUNCHER=ccache -GNinja -L
        cat ql/config.hpp
        cmake --build . --verbose
        sudo cmake --install .
    - name: Test
      run: |
        DYLD_LIBRARY_PATH=/usr/local/lib quantlib-test-suite --log_level=message
