Skip to content

Commit

Permalink
Script to update WDK version (#4080)
Browse files Browse the repository at this point in the history
Signed-off-by: Alan Jowett <[email protected]>
  • Loading branch information
Alan-Jowett authored Dec 11, 2024
1 parent 4290611 commit 2a75370
Show file tree
Hide file tree
Showing 4 changed files with 266 additions and 8 deletions.
238 changes: 238 additions & 0 deletions scripts/Update-WdkVersion.ps1
Original file line number Diff line number Diff line change
@@ -0,0 +1,238 @@
# Copyright (c) eBPF for Windows contributors
# SPDX-License-Identifier: MIT

<#
.SYNOPSIS
Updates the WDK version in all relevant files.
.DESCRIPTION
This script updates the WDK version in all relevant files. It queries the NuGet package manager for the version of the
'Microsoft.Windows.WDK.x64' package and updates the version number in the following files:
- wdk.props
- tools\bpf2c\templates\kernel_mode_bpf2c.vcxproj
- tools\bpf2c\templates\user_mode_bpf2c.vcxproj
- scripts\setup_build\packages.config
The script creates a backup of each file before updating it. If an error occurs during the update, the script rolls back
all changes.
The script requires the 'nuget' command to be available in the PATH.
.PARAMETER None
This script does not take any parameters.
.NOTES
This script is intended to be run from the root of the repository.
#>

function Get-PackageVersion(
[string]$packageName
) {
<#
.SYNOPSIS
Get the version of a NuGet package.
.DESCRIPTION
Queries the NuGet package manager for the version of a package.
.PARAMETER packageName
The name of the package to query.
.EXAMPLE
Get-PackageVersion "Microsoft.Windows.WDK.x64"
.NOTES
This function requires the 'nuget' command to be available in the PATH.
#>
if ([string]::IsNullOrWhiteSpace($packageName)) {
throw "Package name cannot be empty"
}

try {
$package = nuget list $packageName
if ($LASTEXITCODE -ne 0) {
throw "Failed to retrieve package information"
}
$packageLine = $package | Where-Object { $_ -match $packageName }
if (-not $packageLine) {
throw "Package '$packageName' not found"
}
if ($packageLine -is [array]) {
Write-Warning "Multiple versions found. Using the first one."
$packageLine = $packageLine[0]
}
$packageVersion = $packageLine -replace "$packageName\s+", ""
if (-not ($packageVersion -match '^\d+\.\d+\.\d+\.\d+$')) {
throw "Invalid version format: $packageVersion"
}
return $packageVersion
}
catch {
throw "Failed to get package version: $_"
}
}

function Update-VersionInVsFile(
[string]$vs_file_path,
[string]$version_number
) {
<#
.SYNOPSIS
Updates the WDK version in a Visual Studio file.
.DESCRIPTION
Updates the WDK version in a Visual Studio file by replacing the existing version number with the specified version
number.
.PARAMETER vs_file_path
The path to the Visual Studio file to update.
.PARAMETER version_number
The new version number to set in the file.
.NOTES
This function creates a backup of the file before updating it. If an error occurs during the update, the function rolls
back the changes.
#>
if ([string]::IsNullOrWhiteSpace($vs_file_path)) {
throw "File path cannot be empty"
}
if (-not ($version_number -match '^\d+\.\d+\.\d+\.\d+$')) {
throw "Invalid version format: $version_number"
}
if (-not (Test-Path $vs_file_path)) {
throw "File not found: $vs_file_path"
}
try {
# Create backup
$backup_path = "$vs_file_path.bak"
Copy-Item $vs_file_path $backup_path -Force
# Read the contents of the file
$vs_file_content = Get-Content $vs_file_path
# Replace the version number in the file
$vs_file_content = $vs_file_content -replace "<WDKVersion>.*</WDKVersion>", "<WDKVersion>$version_number</WDKVersion>"
# Write the updated contents back to the file
Set-Content $vs_file_path $vs_file_content
# Print success message
Write-Output "Updated WDK version in $vs_file_path to $version_number"
}
catch {
if (Test-Path $backup_path) {
Copy-Item $backup_path $vs_file_path -Force
Remove-Item $backup_path
}
throw "Failed to update version in file: $vs_file_path"
}
}

function Update-TemplateFile(
[string]$template_file_path,
[string]$output_file_path,
[string]$version_number
) {
<#
.SYNOPSIS
Updates the WDK version in a template file.
.DESCRIPTION
Updates the WDK version in a template file by replacing the existing version number placeholder with the specified
version number.
.PARAMETER template_file_path
The path to the template file to update.
.PARAMETER output_file_path
The path to the output file to write the updated contents.
.PARAMETER version_number
The new version number to set in the file.
.NOTES
This function creates a backup of the output file before updating it. If an error occurs during the update, the function
rolls back the changes.
#>

if ([string]::IsNullOrWhiteSpace($template_file_path) -or [string]::IsNullOrWhiteSpace($output_file_path)) {
throw "File paths cannot be empty"
}
if (-not ($version_number -match '^\d+\.\d+\.\d+\.\d+$')) {
throw "Invalid version format: $version_number"
}
if (-not (Test-Path $template_file_path)) {
throw "Template file not found: $template_file_path"
}
try {
# Create backup if output file exists
$backup_path = $null
if (Test-Path $output_file_path) {
$backup_path = "$output_file_path.bak"
Copy-Item $output_file_path $backup_path -Force
}

# Read the contents of the file
$template_file_content = Get-Content $template_file_path
# Replace the version number in the file
$template_file_content = $template_file_content -replace "\$\(WDKVersion\)", $version_number
# Write the updated contents back to the file
Set-Content $output_file_path $template_file_content
# Print success message
Write-Output "Updated WDK version in $output_file_path to $version_number"
}
catch {
if ($backup_path -and (Test-Path $backup_path)) {
Copy-Item $backup_path $output_file_path -Force
Remove-Item $backup_path
}
throw "Failed to update template file: $_"
}
}

# List of files updated by the script used for rollback.
$files_updated = @()

# Main script logic
try {
# Paths relative to the root of the repository
$vs_files_to_update = @(
"wdk.props",
"tools\bpf2c\templates\kernel_mode_bpf2c.vcxproj",
"tools\bpf2c\templates\user_mode_bpf2c.vcxproj"
)

# Get the current WDK version
$wdk_version_number = Get-PackageVersion "Microsoft.Windows.WDK.x64"

# Print the version number
Write-Output "Found WDK version: $wdk_version_number"

# Replace version in each VS file
foreach ($vs_file in $vs_files_to_update) {
Write-Host "Updating WDK version in $vs_file"
$vs_file = $PSScriptRoot + "\..\" + $vs_file
Update-VersionInVsFile $vs_file $wdk_version_number
$files_updated += $vs_file
}

# Generate the new packages.config file
Update-TemplateFile -template_file_path "$PSScriptRoot\..\scripts\setup_build\packages.config.template" -output_file_path "$PSScriptRoot\..\scripts\setup_build\packages.config" -version_number $wdk_version_number
$files_updated += "$PSScriptRoot\..\scripts\setup_build\packages.config"

# Print success message
Write-Output "Updated WDK version in all files"
}
catch {
# Rollback all changes
for ($i = $files_updated.Length - 1; $i -ge 0; $i--) {
$file = $files_updated[$i]
if (Test-Path "$file.bak") {
# Rolling back changes
Write-Host "Rolling back changes in $file"
Copy-Item "$file.bak" $file -Force
Remove-Item "$file.bak"
}
}

# Print error message
Write-Error $_
exit 1
}
14 changes: 14 additions & 0 deletions scripts/setup_build/packages.config.template
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
<?xml version="1.0" encoding="utf-8"?>
<!--
Copyright (c) eBPF for Windows contributors
SPDX-License-Identifier: MIT
-->
<packages>
<package id="Microsoft.Windows.SDK.CPP" version="$(WDKVersion)" targetFramework="native" />
<package id="Microsoft.Windows.SDK.CPP.arm64" version="$(WDKVersion)" targetFramework="native" />
<package id="Microsoft.Windows.SDK.CPP.x64" version="$(WDKVersion)" targetFramework="native" />
<package id="Microsoft.Windows.SDK.CPP.x86" version="$(WDKVersion)" targetFramework="native" />
<package id="Microsoft.Windows.WDK.ARM64" version="$(WDKVersion)" targetFramework="native" />
<package id="Microsoft.Windows.WDK.x64" version="$(WDKVersion)" targetFramework="native" />
<package id="Microsoft.Windows.WDK.x86" version="$(WDKVersion)" targetFramework="native" />
</packages>
11 changes: 7 additions & 4 deletions tools/bpf2c/templates/kernel_mode_bpf2c.vcxproj
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,9 @@
SPDX-License-Identifier: MIT
-->
<Project DefaultTargets="Build" ToolsVersion="12.0" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
<PropertyGroup>
<WDKVersion>10.0.26100.2161</WDKVersion>
</PropertyGroup>
<ItemGroup Label="ProjectConfigurations">
<ProjectConfiguration Include="Debug|x64">
<Configuration>Debug</Configuration>
Expand All @@ -26,11 +29,11 @@
<HostPlatform>$([System.Runtime.InteropServices.RuntimeInformation]::OSArchitecture)</HostPlatform>
<WindowsTargetPlatformVersion>10.0.26100.0</WindowsTargetPlatformVersion>
</PropertyGroup>
<Import Project="$(Packages)\Microsoft.Windows.SDK.CPP.10.0.26100.2161\build\native\Microsoft.Windows.SDK.cpp.props" Condition="Exists('$(Packages)\Microsoft.Windows.SDK.CPP.10.0.26100.2161\build\native\Microsoft.Windows.SDK.cpp.props')" />
<Import Project="$(Packages)\Microsoft.Windows.WDK.x64.10.0.26100.2161\build\native\Microsoft.Windows.WDK.$(Platform).props" Condition="Exists('$(Packages)\Microsoft.Windows.WDK.x64.10.0.26100.2161\build\native\Microsoft.Windows.WDK.$(Platform).props')" />
<Import Project="$(Packages)\Microsoft.Windows.SDK.CPP.x64.10.0.26100.2161\build\native\Microsoft.Windows.SDK.cpp.$(Platform).props" Condition="Exists('$(Packages)\Microsoft.Windows.SDK.CPP.x64.10.0.26100.2161\build\native\Microsoft.Windows.SDK.cpp.$(Platform).props')" />
<Import Project="$(Packages)\Microsoft.Windows.SDK.CPP.$(WDKVersion)\build\native\Microsoft.Windows.SDK.cpp.props" Condition="Exists('$(Packages)\Microsoft.Windows.SDK.CPP.$(WDKVersion)\build\native\Microsoft.Windows.SDK.cpp.props')" />
<Import Project="$(Packages)\Microsoft.Windows.WDK.x64.$(WDKVersion)\build\native\Microsoft.Windows.WDK.$(Platform).props" Condition="Exists('$(Packages)\Microsoft.Windows.WDK.x64.$(WDKVersion)\build\native\Microsoft.Windows.WDK.$(Platform).props')" />
<Import Project="$(Packages)\Microsoft.Windows.SDK.CPP.x64.$(WDKVersion)\build\native\Microsoft.Windows.SDK.cpp.$(Platform).props" Condition="Exists('$(Packages)\Microsoft.Windows.SDK.CPP.x64.$(WDKVersion)\build\native\Microsoft.Windows.SDK.cpp.$(Platform).props')" />
<ImportGroup Label="ExtensionTargets">
<Import Project="$(Packages)\Microsoft.Windows.SDK.CPP.10.0.26100.2161\build\native\Microsoft.Windows.SDK.cpp.targets" Condition="Exists('$(Packages)\Microsoft.Windows.SDK.CPP.10.0.26100.2161\build\native\Microsoft.Windows.SDK.cpp.targets')" />
<Import Project="$(Packages)\Microsoft.Windows.SDK.CPP.$(WDKVersion)\build\native\Microsoft.Windows.SDK.cpp.targets" Condition="Exists('$(Packages)\Microsoft.Windows.SDK.CPP.$(WDKVersion)\build\native\Microsoft.Windows.SDK.cpp.targets')" />
</ImportGroup>
<PropertyGroup Label="Globals">
<ProjectGuid>{172DFDF9-3B0D-44AD-B780-92627EA469A2}</ProjectGuid>
Expand Down
11 changes: 7 additions & 4 deletions tools/bpf2c/templates/user_mode_bpf2c.vcxproj
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,9 @@
SPDX-License-Identifier: MIT
-->
<Project DefaultTargets="Build" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
<PropertyGroup>
<WDKVersion>10.0.26100.2161</WDKVersion>
</PropertyGroup>
<ItemGroup Label="ProjectConfigurations">
<ProjectConfiguration Include="Debug|x64">
<Configuration>Debug</Configuration>
Expand Down Expand Up @@ -45,11 +48,11 @@
<HostPlatform>$([System.Runtime.InteropServices.RuntimeInformation]::OSArchitecture)</HostPlatform>
<WindowsTargetPlatformVersion>10.0.26100.0</WindowsTargetPlatformVersion>
</PropertyGroup>
<Import Project="$(Packages)\Microsoft.Windows.SDK.CPP.10.0.26100.2161\build\native\Microsoft.Windows.SDK.cpp.props" Condition="Exists('$(Packages)\Microsoft.Windows.SDK.CPP.10.0.26100.2161\build\native\Microsoft.Windows.SDK.cpp.props')" />
<Import Project="$(Packages)\Microsoft.Windows.WDK.x64.10.0.26100.2161\build\native\Microsoft.Windows.WDK.$(Platform).props" Condition="Exists('$(Packages)\Microsoft.Windows.WDK.x64.10.0.26100.2161\build\native\Microsoft.Windows.WDK.$(Platform).props')" />
<Import Project="$(Packages)\Microsoft.Windows.SDK.CPP.x64.10.0.26100.2161\build\native\Microsoft.Windows.SDK.cpp.$(Platform).props" Condition="Exists('$(Packages)\Microsoft.Windows.SDK.CPP.x64.10.0.26100.2161\build\native\Microsoft.Windows.SDK.cpp.$(Platform).props')" />
<Import Project="$(Packages)\Microsoft.Windows.SDK.CPP.$(WDKVersion)\build\native\Microsoft.Windows.SDK.cpp.props" Condition="Exists('$(Packages)\Microsoft.Windows.SDK.CPP.$(WDKVersion)\build\native\Microsoft.Windows.SDK.cpp.props')" />
<Import Project="$(Packages)\Microsoft.Windows.WDK.x64.$(WDKVersion)\build\native\Microsoft.Windows.WDK.$(Platform).props" Condition="Exists('$(Packages)\Microsoft.Windows.WDK.x64.$(WDKVersion)\build\native\Microsoft.Windows.WDK.$(Platform).props')" />
<Import Project="$(Packages)\Microsoft.Windows.SDK.CPP.x64.$(WDKVersion)\build\native\Microsoft.Windows.SDK.cpp.$(Platform).props" Condition="Exists('$(Packages)\Microsoft.Windows.SDK.CPP.x64.$(WDKVersion)\build\native\Microsoft.Windows.SDK.cpp.$(Platform).props')" />
<ImportGroup Label="ExtensionTargets">
<Import Project="$(Packages)\Microsoft.Windows.SDK.CPP.10.0.26100.2161\build\native\Microsoft.Windows.SDK.cpp.targets" Condition="Exists('$(Packages)\Microsoft.Windows.SDK.CPP.10.0.26100.2161\build\native\Microsoft.Windows.SDK.cpp.targets')" />
<Import Project="$(Packages)\Microsoft.Windows.SDK.CPP.$(WDKVersion)\build\native\Microsoft.Windows.SDK.cpp.targets" Condition="Exists('$(Packages)\Microsoft.Windows.SDK.CPP.$(WDKVersion)\build\native\Microsoft.Windows.SDK.cpp.targets')" />
</ImportGroup>
<PropertyGroup Label="Globals">
<VCProjectVersion>16.0</VCProjectVersion>
Expand Down

0 comments on commit 2a75370

Please sign in to comment.