Skip to content

Commit 1d1de38

Browse files
authored
Add ability to set compiler defines (#10)
For Ticket #9 All 38 tests pass
1 parent 33c3e37 commit 1d1de38

4 files changed

Lines changed: 124 additions & 0 deletions

File tree

CHANGELOG.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,10 @@
33
All notable changes to this project will be documented in this file.
44

55
---
6+
## [0.3.0] - Unreleased
7+
8+
- Add support for passing compiler defines to MSBUILD
9+
[#9](https://github.com/continuous-delphi/delphi-msbuild/issues/9)
610

711
## [0.2.0] - 2026-03-16
812

README.md

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -115,6 +115,42 @@ The MSBuild verbosity level. Passed to MSBuild as `/v:<value>`.
115115

116116
Valid values: `quiet`, `minimal`, `normal`, `detailed`, `diagnostic`
117117

118+
## -Define
119+
120+
```text
121+
-Define <string[]>
122+
```
123+
124+
One or more additional MSBuild defines to pass to the compiler. When at least
125+
one value is supplied, the script appends the following to the MSBuild command
126+
line:
127+
128+
```text
129+
/p:DCC_Define="$(DCC_Define);DEFINE1;DEFINE2"
130+
```
131+
132+
The `$(DCC_Define)` prefix preserves the defines already set by the project's
133+
PropertyGroups (e.g. `DEBUG`, `RELEASE`). Without it, the property assignment
134+
would replace them entirely.
135+
136+
When no `-Define` values are supplied (the default), the `/p:DCC_Define`
137+
argument is omitted entirely.
138+
139+
Examples:
140+
141+
```powershell
142+
# Single define
143+
delphi-msbuild.ps1 -ProjectFile .\src\MyApp.dproj -RootDir $root -Define CI
144+
145+
# Multiple defines
146+
delphi-msbuild.ps1 -ProjectFile .\src\MyApp.dproj -RootDir $root `
147+
-Define MYFLAG, USE_JEDI_JCL
148+
149+
# Via pipeline with defines
150+
delphi-inspect.ps1 -DetectLatest -Platform Win32 -BuildSystem MSBuild |
151+
delphi-msbuild.ps1 -ProjectFile .\src\MyApp.dproj -Define CI, MYFLAG
152+
```
153+
118154
## -ShowOutput (switch)
119155

120156
```text

source/delphi-msbuild.ps1

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -74,6 +74,8 @@ param(
7474
[ValidateSet('quiet','minimal','normal','detailed','diagnostic')]
7575
[string]$Verbosity = 'normal',
7676

77+
[string[]]$Define = @(),
78+
7779
[switch]$ShowOutput
7880
)
7981

@@ -177,6 +179,7 @@ function Invoke-MsbuildProject {
177179
[string]$Config,
178180
[string]$Target,
179181
[string]$Verbosity,
182+
[string[]]$Define = @(),
180183
[switch]$ShowOutput
181184
)
182185

@@ -188,6 +191,11 @@ function Invoke-MsbuildProject {
188191
"/v:$Verbosity"
189192
)
190193

194+
if ($Define.Count -gt 0) {
195+
$defineValue = '$(DCC_Define);' + ($Define -join ';')
196+
$msbuildArgs += "/p:DCC_Define=$defineValue"
197+
}
198+
191199
return Invoke-MsbuildExe -Arguments $msbuildArgs -ShowOutput:$ShowOutput
192200
}
193201

@@ -236,6 +244,7 @@ try {
236244
-Config $Config `
237245
-Target $Target `
238246
-Verbosity $Verbosity `
247+
-Define $Define `
239248
-ShowOutput:$ShowOutput
240249

241250
$resultObj = [pscustomobject]@{

tests/pwsh/delphi-msbuild.Tests.ps1

Lines changed: 75 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,9 @@
2626
Passes correct MSBuild arguments to Invoke-MsbuildExe.
2727
Forwards -ShowOutput switch to Invoke-MsbuildExe.
2828
Returns the result object from Invoke-MsbuildExe.
29+
Omits /p:DCC_Define when no defines are supplied.
30+
Appends /p:DCC_Define with $(DCC_Define) prefix for a single define.
31+
Appends /p:DCC_Define with $(DCC_Define) prefix for multiple defines.
2932
3033
Describe 5 - Main flow (via Invoke-ToolProcess, no MSBuild calls):
3134
Exits 3 when no rootDir is provided (no pipeline, no -RootDir).
@@ -293,6 +296,78 @@ Describe 'Invoke-MsbuildProject' {
293296

294297
}
295298

299+
Context 'omits /p:DCC_Define when no -Define values are supplied' {
300+
301+
BeforeAll {
302+
$script:capturedArgs = $null
303+
Mock Invoke-MsbuildExe {
304+
$script:capturedArgs = $Arguments
305+
return [pscustomobject]@{ ExitCode = 0; Output = '' }
306+
}
307+
308+
Invoke-MsbuildProject `
309+
-ProjectFile 'C:\Projects\MyApp.dproj' `
310+
-Platform 'Win32' `
311+
-Config 'Debug' `
312+
-Target 'Build' `
313+
-Verbosity 'normal'
314+
}
315+
316+
It 'does not include any /p:DCC_Define argument' {
317+
$script:capturedArgs | Should -Not -Contain { $_ -like '/p:DCC_Define=*' }
318+
($script:capturedArgs | Where-Object { $_ -like '/p:DCC_Define=*' }) | Should -BeNullOrEmpty
319+
}
320+
321+
}
322+
323+
Context 'appends /p:DCC_Define with $(DCC_Define) prefix for a single define' {
324+
325+
BeforeAll {
326+
$script:capturedArgs = $null
327+
Mock Invoke-MsbuildExe {
328+
$script:capturedArgs = $Arguments
329+
return [pscustomobject]@{ ExitCode = 0; Output = '' }
330+
}
331+
332+
Invoke-MsbuildProject `
333+
-ProjectFile 'C:\Projects\MyApp.dproj' `
334+
-Platform 'Win32' `
335+
-Config 'Debug' `
336+
-Target 'Build' `
337+
-Verbosity 'normal' `
338+
-Define @('MYFLAG')
339+
}
340+
341+
It 'includes /p:DCC_Define=$(DCC_Define);MYFLAG' {
342+
$script:capturedArgs | Should -Contain '/p:DCC_Define=$(DCC_Define);MYFLAG'
343+
}
344+
345+
}
346+
347+
Context 'appends /p:DCC_Define with $(DCC_Define) prefix for multiple defines' {
348+
349+
BeforeAll {
350+
$script:capturedArgs = $null
351+
Mock Invoke-MsbuildExe {
352+
$script:capturedArgs = $Arguments
353+
return [pscustomobject]@{ ExitCode = 0; Output = '' }
354+
}
355+
356+
Invoke-MsbuildProject `
357+
-ProjectFile 'C:\Projects\MyApp.dproj' `
358+
-Platform 'Win32' `
359+
-Config 'Debug' `
360+
-Target 'Build' `
361+
-Verbosity 'normal' `
362+
-Define @('MYFLAG', 'USE_JEDI_JCL')
363+
}
364+
365+
It 'includes /p:DCC_Define=$(DCC_Define);MYFLAG;USE_JEDI_JCL' {
366+
$script:capturedArgs | Should -Contain '/p:DCC_Define=$(DCC_Define);MYFLAG;USE_JEDI_JCL'
367+
}
368+
369+
}
370+
296371
}
297372

298373
Describe 'Main flow -- pre-MSBuild validation (no MSBuild invoked)' {

0 commit comments

Comments
 (0)