Making Targets conditional to Debug build

Hi,

I am attempt to execute some targets only on my Debug build, but I want to build both Debug and Release.

My attempt was to use target's Condition attribute with %(ConfigurationToBuild.FlavorToBuild) == 'Debug' eval'd. The result was the following error:

TFSBuild.proj(188,31): error MSB4116: The condition "%(ConfigurationToBuild.FlavorToBuild) == 'Debug'" on the "UpdateSprocs" target has a reference to item metadata. References to item metadata are not allowed in target conditions unless they are part of an item transform.

I can't come up with another approach and was hoping someone else had one.

Thanks.

[657 byte] By [Surly] at [2008-2-19]
# 1

Do not put condition on the target, but put it on your custom task. for example

<Target Name="SomeTarget">

<!-- Creating flavor property -->
<CreateProperty Value="%(ConfigurationToBuild.FlavorToBuild)" >
<Output TaskParameter="Value" PropertyName="Flavor" />
</CreateProperty>

<YourCustomTask
Condition="'$(Flavor)'!='Debug'"

...

/>


</Target>

ManishAgarwal at 2007-9-9 > top of Msdn Tech,Visual Studio Team System,Team Foundation Server - Build Automation...
# 2
I'm not using a custom task. I simply have a collection of tasks grouped in a target so I'd like to make that target execution conditional.

Not sure how I could use <CreateProperty> at a level above and key a target conditional on it....

THanks for the help.

Surly at 2007-9-9 > top of Msdn Tech,Visual Studio Team System,Team Foundation Server - Build Automation...
# 3

Surly,

You can not have target condition refering to item metadata {%(ConfigurationToBuild.FlavorToBuild)} as this will affect target batching. To overcome this, you can use either of the following approaches

1) In this approach, you add the condition for debug to all the tasks inside your custom target. Thus the target will be executed but no task will be invoked for any flavor other than "debug". This is the easy workaround.

<Project xmlns="http://schemas.microsoft.com/developer/msbuild/2003">

<ItemGroup>
<ConfigurationToBuild Include="Release|Any CPU">
<FlavorToBuild>Release</FlavorToBuild>
<PlatformToBuild>Any CPU</PlatformToBuild>
</ConfigurationToBuild>
<ConfigurationToBuild Include="Debug|Any CPU">
<FlavorToBuild>Debug</FlavorToBuild>
<PlatformToBuild>Any CPU</PlatformToBuild>
</ConfigurationToBuild>
</ItemGroup>

<Target Name="CustomTarget">

<Message

Condition=" '%(ConfigurationToBuild.FlavorToBuild)'=='Debug' "

Text="%(ConfigurationToBuild.FlavorToBuild);%(ConfigurationToBuild.PlatformToBuild)"/>

</Target>
</Project>

2) Alternatingly you can convert the item metadata to property and do the check on the property. For example -

- Note that inside CustomTarget - all the reference to itemmetadata %(ConfigurationToBuild.PlatformToBuild); has changed to property $(PlatformToBuild)

- Note that CustomTarget will invoked for Debug configuration only

- Disadvantage of this approach is that a new child process (msbuild) will be created that can have performance implication. Moreover you are still invoking extra target (ConditionalTarget). Another important thing to keep in mind is that batching %(ConfigurationToBuild) will now happen on ConditionalTarget rather than CustomTarget.

<Target Name="ConditionalTarget">

<MSBuild
Condition=" '%(ConfigurationToBuild.FlavorToBuild)'=='Debug' "
Projects="$(MSBuildProjectFile)"
Properties="PlatformToBuild=%(ConfigurationToBuild.PlatformToBuild);FlavorToBuild=%(ConfigurationToBuild.FlavorToBuild);"
Targets="CustomTarget"/>

</Target>

<Target Name="CustomTarget">

<Message Text="$(FlavorToBuild);$(PlatformToBuild)"/>

</Target>

Hope it helps.

ManishAgarwal at 2007-9-9 > top of Msdn Tech,Visual Studio Team System,Team Foundation Server - Build Automation...
# 4
Ok. Thanks for the two options.

I'm just curious about a third....

Is there anyway to invoke a target besides placing in DependsOnTargets list? I was thinking I could make a top level target that wraps the underlying targets but first turns the item into a property to be able to eval condition.

I'm guessing this is not possible or you would have mentioned but just thought I'd ask.

Thanks!

Surly at 2007-9-9 > top of Msdn Tech,Visual Studio Team System,Team Foundation Server - Build Automation...

Visual Studio Team System

Site Classified