@@ -79,90 +79,87 @@ jobs:
7979 languages : cpp
8080 debug : true
8181
82- - name : Set up MSVC environment
83- shell : cmd
84- run : |
85- "C:\Program Files (x86)\Microsoft Visual Studio\2022\BuildTools\VC\Auxiliary\Build\vcvars64.bat" && powershell -File build-cpp.ps1
86-
8782 - name : Build C++ code
88- shell : powershell
83+ shell : pwsh
8984 id : build-cpp
9085 run : |
91- # Set required environment variables
92- $env:APPINSIGHTS_WIN10_SDK_PATH = "C:\Program Files (x86)\Windows Kits\10"
93- $env:APPINSIGHTS_VS_PATH = $env:VsInstallRoot
94- $env:JAVA_HOME = $env:JAVA_HOME_17_X64
95-
96- # Explicitly define which C++ file we're interested in
97- $sourceDir = "etw/native/src/main/cpp"
98- $headerDir = "etw/native/src/main/headers"
99- $cppFile = "$sourceDir/etw_provider.cpp"
100-
101- Write-Host "Analyzing C++ file: $cppFile"
102-
103- # Create compile_commands.json for CodeQL to use
104- $compileCommandsJson = @"
105- [
106- {
107- "directory": "${PWD}/$sourceDir",
108- "command": "cl.exe /W4 /EHsc /sdl /std:c++14 /I\"${env:APPINSIGHTS_WIN10_SDK_PATH}/include/10.0.22621.0/um\" /I\"${env:JAVA_HOME}/include\" /I\"${env:JAVA_HOME}/include/win32\" /I\"${PWD}/$headerDir\" /c $cppFile",
109- "file": "$cppFile"
110- }
111- ]
112- "@
113-
114- $compileCommandsFile = "compile_commands.json"
115- Write-Host "Creating $compileCommandsFile..."
116- Set-Content -Path $compileCommandsFile -Value $compileCommandsJson
117-
118- # Create a simple C++ file in the same directory to ensure the compiler is called
119- $simpleCode = @"
120- // Simple file to ensure compiler is run
121- #include <windows.h>
122- #include <jni.h>
123- #include "etw_provider.h"
124- int main() { return 0; }
125- "@
126-
127- Set-Content -Path "codeql_trigger.cpp" -Value $simpleCode
128-
129- # Use a try/catch block to handle errors without failing the job
130- try {
131- # List files for debugging
132- Write-Host "C++ files that will be analyzed:"
133- Get-ChildItem -Path $sourceDir -Recurse -Include "*.cpp" | ForEach-Object {
134- Write-Host " $($_.FullName)"
135- }
136- Get-ChildItem -Path $headerDir -Recurse -Include "*.h" | ForEach-Object {
137- Write-Host " $($_.FullName)"
138- }
139-
140- # Try a minimal compile to help CodeQL recognize the files
141- Write-Host "Running minimal compile..."
142- # Print the Java home path to verify it
143- Write-Host "Using JAVA_HOME: $env:JAVA_HOME"
144- # Check if the JNI include directory exists
145- $jniIncludePath = "$env:JAVA_HOME/include"
146- $jniIncludeWinPath = "$env:JAVA_HOME/include/win32"
147-
148- if (Test-Path $jniIncludePath) {
149- Write-Host "JNI include path exists: $jniIncludePath"
150- } else {
151- Write-Host "WARNING: JNI include path doesn't exist: $jniIncludePath"
152- }
153-
154- # Compile with explicit include paths
155- & cl.exe /c codeql_trigger.cpp /I"$headerDir" /I"$sourceDir" /I"$jniIncludePath" /I"$jniIncludeWinPath" /EHsc
156-
157- Write-Host "C++ preparation completed successfully"
158- echo "CPP_BUILD_SUCCEEDED=true" | Out-File -FilePath $env:GITHUB_ENV -Append
86+ # Use vswhere to find the path to the latest installed Visual Studio Build Tools
87+ $vsPath = & "${env:ProgramFiles(x86)}\Microsoft Visual Studio\Installer\vswhere.exe" -latest -products * -requires Microsoft.VisualStudio.Component.VC.Tools.x86.x64 -property installationPath
88+ if (-not $vsPath) {
89+ Write-Host "Could not find Visual Studio Build Tools installation."
90+ exit 1
15991 }
160- catch {
161- Write-Host "Warning: C++ build step encountered an error: $_"
162- Write-Host "Proceeding with CodeQL analysis anyway "
163- echo "CPP_BUILD_SUCCEEDED=false" | Out-File -FilePath $env:GITHUB_ENV -Append
92+ $vcvarsPath = Join-Path $vsPath 'VC\Auxiliary\Build\vcvars64.bat'
93+ if (-not (Test-Path $vcvarsPath)) {
94+ Write-Host "Could not find vcvars64.bat at $vcvarsPath "
95+ exit 1
16496 }
16597
98+ # Prepare the build script as a string
99+ $buildScript = @'
100+ # Set required environment variables
101+ set APPINSIGHTS_WIN10_SDK_PATH="C:\Program Files (x86)\Windows Kits\10"
102+ set APPINSIGHTS_VS_PATH=%VsInstallRoot%
103+ set JAVA_HOME=%JAVA_HOME_17_X64%
104+
105+ set sourceDir=etw/native/src/main/cpp
106+ set headerDir=etw/native/src/main/headers
107+ set cppFile=%sourceDir%/etw_provider.cpp
108+
109+ echo Analyzing C++ file: %cppFile%
110+
111+ REM Create compile_commands.json for CodeQL to use
112+ echo [ > compile_commands.json
113+ echo { >> compile_commands.json
114+ echo "directory": "%CD%/%sourceDir%", >> compile_commands.json
115+ echo "command": "cl.exe /W4 /EHsc /sdl /std:c++14 /I\"%APPINSIGHTS_WIN10_SDK_PATH%/include/10.0.22621.0/um\" /I\"%JAVA_HOME%/include\" /I\"%JAVA_HOME%/include/win32\" /I\"%CD%/%headerDir%\" /c %cppFile%", >> compile_commands.json
116+ echo "file": "%cppFile%" >> compile_commands.json
117+ echo } >> compile_commands.json
118+ echo ] >> compile_commands.json
119+
120+ REM Create a simple C++ file in the same directory to ensure the compiler is called
121+ echo // Simple file to ensure compiler is run > codeql_trigger.cpp
122+ echo #include <windows.h> >> codeql_trigger.cpp
123+ echo #include <jni.h> >> codeql_trigger.cpp
124+ echo #include "etw_provider.h" >> codeql_trigger.cpp
125+ echo int main() { return 0; } >> codeql_trigger.cpp
126+
127+ REM Use a try/catch block to handle errors without failing the job
128+ REM (not available in batch, so use errorlevel)
129+ REM List files for debugging
130+ echo C++ files that will be analyzed:
131+ dir %sourceDir% /s /b *.cpp
132+ dir %headerDir% /s /b *.h
133+
134+ REM Try a minimal compile to help CodeQL recognize the files
135+ echo Running minimal compile...
136+ echo Using JAVA_HOME: %JAVA_HOME%
137+ if exist %JAVA_HOME%/include (
138+ echo JNI include path exists: %JAVA_HOME%/include
139+ ) else (
140+ echo WARNING: JNI include path doesn't exist: %JAVA_HOME%/include
141+ )
142+
143+ REM Compile with explicit include paths
144+ cl.exe /c codeql_trigger.cpp /I"%headerDir%" /I"%sourceDir%" /I"%JAVA_HOME%/include" /I"%JAVA_HOME%/include/win32" /EHsc
145+
146+ if %errorlevel%==0 (
147+ echo C++ preparation completed successfully
148+ echo CPP_BUILD_SUCCEEDED=true>>%GITHUB_ENV%
149+ ) else (
150+ echo Warning: C++ build step encountered an error
151+ echo Proceeding with CodeQL analysis anyway
152+ echo CPP_BUILD_SUCCEEDED=false>>%GITHUB_ENV%
153+ )
154+ '@
155+
156+ # Write the batch script to a file
157+ $batchFile = 'run-cpp-build.bat'
158+ Set-Content -Path $batchFile -Value $buildScript -NoNewline
159+
160+ # Call vcvars64.bat and then the batch file
161+ & cmd /c "\"$vcvarsPath\" && $batchFile"
162+
166163 - name : Perform CodeQL analysis
167164 uses : github/codeql-action/analyze@v3
168165 with :
0 commit comments