Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Script to update WDK version #848

Closed
wants to merge 3 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
154 changes: 154 additions & 0 deletions scripts/Update-WdkVersion.ps1
Original file line number Diff line number Diff line change
@@ -0,0 +1,154 @@
# Copyright (c) eBPF for Windows contributors
# SPDX-License-Identifier: MIT

function Get-PackageVersion(
[string]$packageName
) {
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
) {
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
)
{
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: $_"
}
}

$files_updated = @()

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
}

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"
}
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
}

# Print success message
Write-Output "Updated WDK version in all files"
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>
Alan-Jowett marked this conversation as resolved.
Show resolved Hide resolved
<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
Loading