[Feature Request]: Add a Defined('propname') condition
See original GitHub issueSummary
The proposal is about adding a Defined('propname')
condition in addition to available ones. The condition returns true if the property is present among project properties. More specifically it should evaluate to true if Microsoft.Build.Evaluation.Project.GetProperty(name)
returns a non null value.
Background and Motivation
I’m trying to add preprocessor definitions and conditionally exclude files in a CSharp project, based on these definitions. A documented approach, which basically suggest to to use regex directly on $(DefineConstants)
, becomes unworkable for me even for simple use cases.
Most build systems provides the following facilities to ease such tasks:
- Coercion of strings to boolean (eg. CMake)
- Check for definition of external properties.
MSBuild provides none of the above two. The current approach I’m using is defining externally some FEATURE_A=1
, FEATURE_B=1
, etc. properties (1
is just an arbitrary value), then using these conditionals in the CSharp project:
<ItemGroup Condition="$(FEATURE_A) == ''">
<None Include="FeatureA\*.cs" />
<Compile Remove="FeatureA\*.cs" />
</ItemGroup>
Proposed Feature
Since I’m not seeing MSBuild to implement coercion of properties to booleans, I suggest adding a Defined('propname')
condition that will allow to implement other commonly used approaches when conditionally including/excluding stuff in msbuild projects. The example above would become:
<ItemGroup Condition="!Defined('FEATURE_A')">
<None Include="FeatureA\*.cs" />
<Compile Remove="FeatureA\*.cs" />
</ItemGroup>
This both allows to avoid defining properties with arbitrarily values and makes the condition much more readable.
Alternative Designs
No response
Issue Analytics
- State:
- Created 2 months ago
- Reactions:1
- Comments:10 (6 by maintainers)
Team triage: we would like to collect community interest for this issue.
Instead of being limited to a
Condition
attribute, I’d like to see this implemented as an MSBuild property function.e.g.