name: CodeQL

on: 
  push:
    paths-ignore:
      - '.gitignore'
      - '.gitattributes'
      - '**.cmd'
      - '**.md'
      - 'AUTHORS'
      - 'NEWS'
      - 'ChangeLog'
  pull_request:
    paths-ignore:
      - '.gitignore'
      - '.gitattributes'
      - '**.cmd'
      - '**.md'
      - 'AUTHORS'
      - 'NEWS'
      - 'ChangeLog'

env:
  WDK_URL: https://go.microsoft.com/fwlink/p/?LinkID=253170
  LIBUSB0_URL: https://github.com/mcuee/libusb-win32/releases/download/release_1.4.0.0/libusb-win32-bin-1.4.0.0.zip
  LIBUSBK_URL: https://github.com/mcuee/libusbk/releases/download/V3.1.0.0/libusbK-3.1.0.0-bin.7z
  SOLUTION_FILE_PATH: ./libwdi.sln
  TARGET_PLATFORM: x64
  BUILD_CONFIGURATION: Debug
  BUILD_MACROS: '"WDK_DIR=\"../wdk/Windows Kits/8.0\";LIBUSB0_DIR=\"../libusb0\";LIBUSBK_DIR=\"../libusbk/bin\""'

jobs:
  CodeQL-Build:
    runs-on: windows-latest

    permissions:
      security-events: write

    steps:
    - name: Checkout repository
      uses: actions/checkout@v4
      with:
        fetch-depth: 0
        submodules: recursive

    - name: Download support files
      shell: cmd
      run: |
        curl -L ${{ env.WDK_URL }} -o wdk-redist.msi
        curl -L ${{ env.LIBUSB0_URL }} -o libusb0-redist.zip
        curl -L ${{ env.LIBUSBK_URL }} -o libusbk-redist.7z
        msiexec /a wdk-redist.msi /qn TARGETDIR=%CD%\wdk
        7z x libusb0-redist.zip
        7z x libusbk-redist.7z
        del *.zip
        del *.7z
        move libusb-win32* libusb0
        move libusbK* libusbk

    - name: Initialize CodeQL
      uses: github/codeql-action/init@v3
      with:
        languages: cpp

    - name: Add MSBuild to PATH
      uses: microsoft/setup-msbuild@v2

    - name: Build
      shell: cmd
      run: msbuild ${{ env.SOLUTION_FILE_PATH }} /m /p:Configuration=${{ env.BUILD_CONFIGURATION}},Platform=${{ env.TARGET_PLATFORM }},BuildMacros=${{ env.BUILD_MACROS }}

    - name: Perform CodeQL Analysis
      uses: github/codeql-action/analyze@v3
