name: Build CCExtractor on Windows

env:
  RUSTFLAGS: -Ctarget-feature=+crt-static
  VCPKG_DEFAULT_TRIPLET: x64-windows-static
  VCPKG_DEFAULT_BINARY_CACHE: C:\vcpkg\.cache
  VCPKG_COMMIT: ab2977be50c702126336e5088f4836060733c899

on:
  workflow_dispatch:
  push:
    paths:
      - ".github/workflows/build_windows.yml"
      - "**.c"
      - "**.h"
      - "windows/**"
      - "src/rust/**"
  pull_request:
    types: [opened, synchronize, reopened]
    paths:
      - ".github/workflows/build_windows.yml"
      - "**.c"
      - "**.h"
      - "windows/**"
      - "src/rust/**"

jobs:
  build_release:
    runs-on: windows-2022
    steps:
      - name: Check out repository
        uses: actions/checkout@v6
      - name: Setup MSBuild.exe
        uses: microsoft/setup-msbuild@v2.0.0
        with:
          msbuild-architecture: x64
      - name: Install gpac
        run: choco install gpac --version 2.4.0
      - name: Setup vcpkg
        run: mkdir C:\vcpkg\.cache
      - name: Cache vcpkg
        id: cache
        uses: actions/cache@v5
        with:
          path: |
            C:\vcpkg\.cache
          key: vcpkg-${{ runner.os }}-${{ env.VCPKG_COMMIT }}
      - name: Build vcpkg
        run: |
          git clone https://github.com/microsoft/vcpkg
          ./vcpkg/bootstrap-vcpkg.bat
      - name: Install dependencies
        run: ${{ github.workspace }}/vcpkg/vcpkg.exe install --x-install-root ${{ github.workspace }}/vcpkg/installed/
        working-directory: windows
      - uses: actions-rs/toolchain@v1
        with:
          toolchain: stable
          override: true
      - name: Install Win 10 SDK
        uses: ilammy/msvc-dev-cmd@v1
      - name: build Release-Full
        env:
          LIBCLANG_PATH: "C:\\Program Files\\LLVM\\lib"
          LLVM_CONFIG_PATH: "C:\\Program Files\\LLVM\\bin\\llvm-config"
          CARGO_TARGET_DIR: "..\\..\\windows"
          BINDGEN_EXTRA_CLANG_ARGS: -fmsc-version=0
          VCPKG_ROOT: ${{ github.workspace }}/vcpkg
        run: msbuild ccextractor.sln /p:Configuration=Release-Full /p:Platform=x64
        working-directory: ./windows
      - name: Display version information
        run: ./ccextractorwinfull.exe --version
        working-directory: ./windows/x64/Release-Full
      - uses: actions/upload-artifact@v6
        with:
          name: CCExtractor Windows Release build
          path: |
            ./windows/x64/Release-Full/ccextractorwinfull.exe
            ./windows/x64/Release-Full/*.dll
  build_debug:
    runs-on: windows-2022
    steps:
      - name: Check out repository
        uses: actions/checkout@v6
      - name: Setup MSBuild.exe
        uses: microsoft/setup-msbuild@v2.0.0
        with:
          msbuild-architecture: x64
      - name: Install gpac
        run: choco install gpac --version 2.4.0
      - name: Setup vcpkg
        run: mkdir C:\vcpkg\.cache
      - name: Cache vcpkg
        id: cache
        uses: actions/cache@v5
        with:
          path: |
            C:\vcpkg\.cache
          key: vcpkg-${{ runner.os }}-${{ env.VCPKG_COMMIT }}
      - name: Build vcpkg
        run: |
          git clone https://github.com/microsoft/vcpkg
          ./vcpkg/bootstrap-vcpkg.bat
      - name: Install dependencies
        run: ${{ github.workspace }}/vcpkg/vcpkg.exe install --x-install-root ${{ github.workspace }}/vcpkg/installed/
        working-directory: windows
      - uses: actions-rs/toolchain@v1
        with:
          toolchain: stable
          override: true
      - name: Install Win 10 SDK
        uses: ilammy/msvc-dev-cmd@v1
      - name: build Debug-Full
        env:
          LIBCLANG_PATH: "C:\\Program Files\\LLVM\\lib"
          LLVM_CONFIG_PATH: "C:\\Program Files\\LLVM\\bin\\llvm-config"
          CARGO_TARGET_DIR: "..\\..\\windows"
          BINDGEN_EXTRA_CLANG_ARGS: -fmsc-version=0
          VCPKG_ROOT: ${{ github.workspace }}/vcpkg
        run: msbuild ccextractor.sln /p:Configuration=Debug-Full /p:Platform=x64
        working-directory: ./windows
      - name: Display version information
        continue-on-error: true
        run: ./ccextractorwinfull.exe --version
        working-directory: ./windows/x64/Debug-Full
      - uses: actions/upload-artifact@v6
        with:
          name: CCExtractor Windows Debug build
          path: |
            ./windows/x64/Debug-Full/ccextractorwinfull.exe
            ./windows/x64/Debug-Full/ccextractorwinfull.pdb
            ./windows/x64/Debug-Full/*.dll
