Skip to content

Commit 94cd7d6

Browse files
committed
better version generation (less rebuild churning)
1 parent 9fb6494 commit 94cd7d6

File tree

4 files changed

+139
-30
lines changed

4 files changed

+139
-30
lines changed

IntelPresentMon/Versioning/Versioning.vcxproj

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@
1818
</ItemGroup>
1919
<ItemGroup>
2020
<None Include="generated\.gitignore" />
21-
<None Include="scripts\pre-build.bat" />
21+
<None Include="scripts\pre-build.ps1" />
2222
</ItemGroup>
2323
<PropertyGroup Label="Globals">
2424
<VCProjectVersion>17.0</VCProjectVersion>
@@ -81,7 +81,7 @@
8181
<GenerateDebugInformation>true</GenerateDebugInformation>
8282
</Link>
8383
<CustomBuildStep>
84-
<Command>scripts\pre-build.bat</Command>
84+
<Command>powershell.exe -ExecutionPolicy Bypass -File "scripts\pre-build.ps1"</Command>
8585
</CustomBuildStep>
8686
<CustomBuildStep>
8787
<Message>Generating Build IDs</Message>
@@ -109,7 +109,7 @@
109109
<GenerateDebugInformation>true</GenerateDebugInformation>
110110
</Link>
111111
<CustomBuildStep>
112-
<Command>scripts\pre-build.bat</Command>
112+
<Command>powershell.exe -ExecutionPolicy Bypass -File "scripts\pre-build.ps1"</Command>
113113
</CustomBuildStep>
114114
<CustomBuildStep>
115115
<Message>Generating Build IDs</Message>

IntelPresentMon/Versioning/Versioning.vcxproj.filters

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@
2525
</ClCompile>
2626
</ItemGroup>
2727
<ItemGroup>
28-
<None Include="scripts\pre-build.bat">
28+
<None Include="scripts\pre-build.ps1">
2929
<Filter>Source Files</Filter>
3030
</None>
3131
<None Include="generated\.gitignore" />

IntelPresentMon/Versioning/scripts/pre-build.bat

Lines changed: 0 additions & 26 deletions
This file was deleted.
Lines changed: 135 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,135 @@
1+
Param() # No parameters for now
2+
3+
Set-StrictMode -Version Latest
4+
$ErrorActionPreference = 'Stop'
5+
6+
# Ensure the "generated" folder exists
7+
if (-not (Test-Path -LiteralPath 'generated')) {
8+
New-Item -ItemType Directory -Path 'generated' | Out-Null
9+
}
10+
11+
if (Test-Path 'generated\build_id.h') {
12+
# ------------------------------------------------------------------------------
13+
# 1) CAPTURE SIGNATURE A (LIST OF UNCOMMITTED CHANGES WITH LINES ADDED/REMOVED)
14+
# ------------------------------------------------------------------------------
15+
Write-Host "[build_id_gen] Gathering diff info (Signature A)..."
16+
17+
# 1A) Normal 'numstat' lines for tracked changes
18+
$signatureA_current_lines = git diff --numstat
19+
20+
# 1B) Collect untracked files so we can treat them like "newly added"
21+
# (i.e., lines = total lines, removed = 0)
22+
$untrackedFiles = git ls-files --others --exclude-standard
23+
24+
foreach ($uf in $untrackedFiles) {
25+
# Ensure file still exists (and isn't a folder)
26+
if (Test-Path $uf) {
27+
# Count lines. If the file is huge or binary, consider try/catch or skip.
28+
$lineCount = (Get-Content $uf -ErrorAction SilentlyContinue | Measure-Object -Line).Lines
29+
30+
# Append path to make relative to solution (repo) root
31+
$uf = 'IntelPresentMon/Versioning/' + $uf
32+
33+
# Append a "numstat-like" line: "<lines> 0 <filePath>"
34+
# Use backtick-t (`t) for tabs, to mirror git diff --numstat output.
35+
$signatureA_current_lines += "$lineCount`t0`t$uf"
36+
}
37+
}
38+
39+
# 1C) Build a single signature string (space-separated or however you like).
40+
# If you want each record separated by a newline instead, use "-join [Environment]::NewLine"
41+
$signatureA_current = $signatureA_current_lines -join ' '
42+
43+
# Retrieve previous signature A if it exists
44+
$signatureA_prev = ''
45+
if (Test-Path 'generated\signature_a_prev.txt') {
46+
$signatureA_prev = Get-Content 'generated\signature_a_prev.txt' -Raw
47+
}
48+
49+
# ------------------------------------------------------------------------------
50+
# 2) CAPTURE SIGNATURE B (PER-FILE MD5 FOR ALL UNCOMMITTED FILES)
51+
# ------------------------------------------------------------------------------
52+
Write-Host "[build_id_gen] Calculating per-file hashes (Signature B)..."
53+
$signatureB_current = ''
54+
55+
foreach ($line in $signatureA_current_lines) {
56+
# Each line is something like: 10 2 src\main.cpp
57+
# Split on whitespace
58+
$fields = $line -split '\s+'
59+
if ($fields.Count -ge 3) {
60+
$linesAdded = $fields[0]
61+
$linesRemoved = $fields[1]
62+
$changedFile = '../../' + $fields[2]
63+
64+
# Edge case: file might be deleted or not present
65+
if (-not (Test-Path $changedFile)) {
66+
# Write-Host "[build_id_gen] File '$changedFile' does not exist (possibly deleted). Skipping..."
67+
continue
68+
}
69+
70+
# Get MD5 hash of the file
71+
# (You can change -Algorithm to e.g. SHA256 if desired)
72+
$hash = (Get-FileHash $changedFile -Algorithm MD5).Hash
73+
$signatureB_current += $hash
74+
}
75+
}
76+
77+
# Retrieve previous signature B if it exists s
78+
$signatureB_prev = ''
79+
if (Test-Path 'generated\signature_b_prev.txt') {
80+
$signatureB_prev = Get-Content 'generated\signature_b_prev.txt' -Raw
81+
}
82+
83+
# Store the updated Signature B
84+
$signatureB_current | Set-Content 'generated\signature_b_prev.txt'
85+
86+
# If Signature A is unchanged, we must check Signature B
87+
if ($signatureA_current.Trim() -eq $signatureA_prev.Trim()) {
88+
# If Signature B is unchanged, skip generating build_id.h
89+
if ($signatureB_current.Trim() -eq $signatureB_prev.Trim()) {
90+
Write-Host "[build_id_gen] Signature B is unchanged. Skipping Versioning\generated\build_id.h regeneration."
91+
return
92+
}
93+
Write-Host "[build_id_gen] Signature B mismatch detected."
94+
} else {
95+
# Store the updated Signature A
96+
$signatureA_current | Set-Content 'generated\signature_a_prev.txt'
97+
Write-Host "[build_id_gen] Signature A mismatch detected."
98+
}
99+
} else {
100+
Write-Host "[build_id_gen] Versioning\generated\build_id.h not present; generating..."
101+
}
102+
103+
104+
# ------------------------------------------------------------------------------
105+
# 3) GENERATE/UPDATE BUILD_ID.H
106+
# ------------------------------------------------------------------------------
107+
Write-Host "[build_id_gen] Generating new build_id.h..."
108+
109+
# Get the current git commit hash
110+
$gitHash = (git rev-parse HEAD) | Out-String
111+
$gitHashShort = (git rev-parse --short HEAD)| Out-String
112+
$gitHash = $gitHash.Trim()
113+
$gitHashShort = $gitHashShort.Trim()
114+
115+
# Format: yyyy.M.d.HH:mm:ss (e.g., 2025.1.8.12:20:11)
116+
$dateTime = Get-Date -Format 'yyyy.M.d.HH:mm:ss'
117+
118+
# Check if there are uncommitted changes (sets $LASTEXITCODE = 0 if none)
119+
git diff --quiet
120+
if ($LASTEXITCODE -eq 0) {
121+
$uncommittedChanges = 'false'
122+
} else {
123+
$uncommittedChanges = 'true'
124+
}
125+
126+
@"
127+
#pragma once
128+
// Auto-generated by build_id_gen.ps1
129+
130+
#define PM_BID_GIT_HASH "$gitHash"
131+
#define PM_BID_GIT_HASH_SHORT "$gitHashShort"
132+
#define PM_BID_TIME "$dateTime"
133+
#define PM_BID_UID "$gitHash-$dateTime"
134+
#define PM_BID_DIRTY $uncommittedChanges
135+
"@ | Set-Content 'generated\build_id.h'

0 commit comments

Comments
 (0)