4

I'm trying to get Resharper for c++ to work with our internal build system. Resharper needs to have all include folders defined statically (not part of any target), but we set up the include paths in a target which runs before the build.

We have a nuget like system set up where we declare which components are needed by adding them to an itemgroup. Once in this group, a component will be downloaded and set up correctly. We don't want to set up include paths to components which aren't in use, because it will cause the compiler to bail since the folders does not exist.

I need to conditionally define Properties based on whether an item is in an ItemGroup. Is there a way of doing this outside of a target?

Currently I'm trying to use Property functions, but I can't get to to work for some reason. Am I barking up the wrong tree? This is what I have:

<?xml version="1.0" encoding="utf-8"?>
<Project ToolsVersion="4.0" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
    <ItemGroup>
        <TheList Include="itemA"/>
        <TheList Include="itemB"/>
        <TheList Include="itemC"/>
    </ItemGroup>

    <PropertyGroup>
        <_TheList>@(TheList)</_TheList>
        <FoundIt>No</FoundIt>
        <FoundIt Condition='$(_TheList.Contains("itemA"))'>Yes</FoundIt>
    </PropertyGroup>

    <Target Name="Test">
        <Message Text="$(_TheList)" Importance="High" />
        <Message Text="It was found: $(FoundIt)" Importance="High" />
    </Target>
</Project>

This prints:

itemA;itemB;itemC
It was found: No

Moving the PropertyGroup definition into the target makes it work, which is strange because the property function documentation states that it should work outside of targets.

Yngve Hammersland
  • 1,634
  • 2
  • 14
  • 28
  • 1
    Apparently converting your ItemGroup to a PropertyGroup is not supported before build time. "Items that are outside Target elements are assigned values during the evaluation phase of a build." https://msdn.microsoft.com/en-us/library/ms171453.aspx. The Condition evaluates to false because it's looking at literally `@(TheList)`, not `itemA;itemB;itemC`. Despite all that you may want to open up a bug report with https://github.com/Microsoft/msbuild to get some attention and verify that this is definitely not possible statically. – makhdumi Feb 04 '16 at 23:00
  • 1
    There are also Item functions, like `@(TheList->Contains('itemC'))`, but you'll get an error if you try to use them in a Condition outside a target. – makhdumi Feb 04 '16 at 23:13
  • 1
    Thanks! This is exactly the answer. If I change the code to look for 'The' it returns true. You should write this up as an answer to this question. – Yngve Hammersland Feb 09 '16 at 13:01

0 Answers0