In our case we use Atlassian Bamboo as build server with build agents running Windows Server 2012 (not R2). So installing the Windows 10 SDK or Visual Studio Build Tools 2017 or 2019 was not an option because it won't install on Windows Server 2012 because it does not meet system requirements.
I got it working with a pre-build event added to the VS project. (Thanks Joseph Haslinger for the idea)
The pre-build event added to the project properties calls a PowerShell script with the following command:
powershell.exe -ExecutionPolicy Bypass -NoProfile -NonInteractive -File $(ProjectDir)pre-build.ps1 $(ProjectDir) $(TargetFileName)
The command is based on the one from Jiří Činčura of tabs ↹ over ␣ ␣ ␣ spaces
I’m setting ExecutionPolicy
to Bypass
in case somebody would not have it set to Unrestricted
. 😉 I’m using NoProfile
as I often use plain PowerShell and loading my profile would just slow it a bit. Of course NonInteractive
because, well, there is no interaction possible. And finally the File
with my script file.
I added $(ProjectDir)
both to call the PowerShell script and as an argument to the script, the former to make sure the script will be found on the build agent (probably not needed but for peace of mind...) the latter because LC.exe
won't find the licenses.licx
file and the output directory otherwise.
Another argument $(TargetFileName)
adds the projects target filename to use in the /target:
argument of the LC.exe
command.
The PowerShell script itself...
param(
[String]$projectDir,
[String]$targetFileName
)
if(Test-Path 'env:bamboo_agentId') {
if(-not($projectDir)) { Throw "A value for -projectDir must be supplied!" }
if(-not($targetFileName)) { Throw "A value for -targetFileName must be supplied!" }
Write-Host "`r`nOn Bamboo build server, executing pre-build script"
Write-Host "Supplied -projectDir: $projectDir"
Import-Module NTFSSecurity
trap {"Error found: $_"; exit 1;}
$lcpath = Get-ChildItem2 -Path "C:\Program Files (x86)" -Filter "lc.exe" -Recurse -File |
Sort-Object -Property LastWriteTime -Descending |
Select-String -inputobject {$_.FullName} -Pattern ".*(x64|amd64).*" |
Select-Object -First 1
$argTarget = "/target:$targetFileName"
$argCompList = "/complist:$($projectDir)My Project\licenses.licx"
$argOutdir = "/outdir:$($projectDir)obj\Release"
Write-Host "`r`nCreating licenses file"
Write-Host "Executing command: $lcpath $argTarget $argCompList $argOutdir"
& $lcpath $argTarget $argCompList $argOutdir
} else {
Write-Host "Not on Bamboo build server, skipping pre-build actions."
}
First we check for existence of the bamboo_agentId
environment variable that indicates we're on the Bamboo build agent, on a local development machine it's not necessary to execute the script. When we know we are on a Bamboo build agent we check if both arguments are supplied.Then we'll search C:\Program Files (x86)
for the most recent 64-bit copy of LC.exe
. Following we'll create the target
, complist
and outdir
arguments for LC.exe
. And last we'll execute the LC.exe
command with its arguments.
lc.exe
with no.licx
file present. This is also present:Task attempted to find "LC.exe" using the SdkToolsPath value "". Make sure the SdkToolsPath is set to the correct value and the tool exists in the correct processor specific location below it.
– gwin003