name: "VcpkgPackage"

# Tests that:
# 1. the vcpkg package build successfully
#    - on all platforms
#    - for all rendering bindings (opengl3-binding, metal-binding, opengl3-binding, vulkan-binding, dx11-binding, dx12-binding)
#    - for all window backends (glfw-binding, sdl2-binding)
#    - with all optional dependencies (freetype, freetype-lunasvg)
# 2. An app can be built with hello_imgui as a vcpkg package
#    (with all combinations of rendering bindings, window backends, optional dependencies)
# 3. An app can be run
#    (only on selected backends and platforms)


on:
  workflow_dispatch:
  pull_request:
  push:


jobs:
  build:
    name: VcpkgPackage
    runs-on: ${{ matrix.config.os }}
    strategy:
      fail-fast: false
      matrix:
        config:
          - os: ubuntu-latest
            vcpkg_triplet: x64-linux-release
            #
            # macOS disabled: we have intermittent failures when downloading the vcpkg binary cache
            # See failing action
            #       https://github.com/pthom/hello_imgui/actions/runs/10248206833/job/28348983900
            #          https://api.github.com/repos/pthom/hello_imgui/git/refs/heads/master: curl failed to download with exit code 22 / curl: (22) The requested URL returned error: 403
            #          - os: macos-latest
            #            vcpkg_triplet: x64-osx-release
            #
            # Window disabled:
            # See failing action:https://github.com/pthom/hello_imgui/actions/runs/8967371818/job/24624780079
            #    " unresolved external symbol _Thrd_sleep_for"
            # The symbol that is missing was added to the microsoft STL in october 2023, and later removed...
            # See https://github.com/actions/runner-images/issues/9398
            #          - os: windows-latest
            #            vcpkg_triplet: x64-windows-release

    steps:
      - uses: actions/checkout@v4
        with:
          submodules: false

      - name: install glfw & sdl requirements
        if : ${{ matrix.config.os == 'ubuntu-latest' }}
        shell: bash
        run: |
          sudo apt-get update && sudo apt-get install -y libxinerama-dev libxcursor-dev xorg-dev libglu1-mesa-dev pkg-config libx11-dev libxft-dev libxext-dev

      - name: vcpkg install
        uses: johnwason/vcpkg-action@v6
        id: vcpkg
        with:
          triplet: ${{ matrix.config.vcpkg_triplet }}
          cache-key: ${{ matrix.config.os }}
          revision: master
          token: ${{ github.token }}
          github-binarycache: true

      #
      # Install utilities to be able to run a graphical OpenGL application
      #
      - name: Ubuntu - Install & Start Xvfb
        if: ${{ matrix.platform == 'ubuntu-latest' }}
        run: |
          sudo apt-get install -y xvfb

      - name: Windows - install mesa OpenGL software renderer
        shell: bash
        if: ${{ matrix.config.os == 'windows-latest' }}
        run: |
          # Download the archive
          curl -L -O https://github.com/pal1000/mesa-dist-win/releases/download/23.1.9/mesa3d-23.1.9-release-msvc.7z

          # Extract the archive using 7-zip which is preinstalled on the GitHub Actions Windows runner
          7z x mesa3d-23.1.9-release-msvc.7z -omesa3d

#      - name: Setup interactive tmate session
#        uses: mxschmitt/action-tmate@v3

        # Run the test
      - name: run test
        shell: bash
        run: |
          export VCPKG_ROOT=$(pwd)/vcpkg
          cd .github/ci_vcpkg_package_tests
          python run_ci_vcpkg_package_tests.py
