I'm trying to make a .targets
file for my NuGet package, which will link to a proper .lib
file depending on the C++ runtime library of a project. This answer recommends to use %(ClCompile.RuntimeLibrary)
metadata for this. But it seems like metadata cannot be accessed outside the <Target>
node! And library dependencies are added in <ItemDefinitionGroup>
node just under the root <Project>
node.
Here is the SSCCE:
<?xml version="1.0" encoding="us-ascii"?>
<Project ToolsVersion="12.0" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
<ItemGroup>
<ClCompile Include="main.cpp">
<RuntimeLibrary>MultiThreadedDebugDLL</RuntimeLibrary>
</ClCompile>
</ItemGroup>
<PropertyGroup>
<RuntimeLibrary>%(ClCompile.RuntimeLibrary)</RuntimeLibrary>
</PropertyGroup>
<Target Name="Build">
<Message Text="Property = $(RuntimeLibrary)" Importance="high" />
<Message Text="Metadata = %(ClCompile.RuntimeLibrary)" Importance="high" />
</Target>
</Project>
Running it with MsBuild yields:
Property = %(ClCompile.RuntimeLibrary)
Metadata = MultiThreadedDebugDLL
The same statement %(ClCompile.RuntimeLibrary)
is expanded to the value when used inside <Target>
node, but not when used in <PropertyGroup>
node outside <Target>
node.
So how can I access runtime library metadata value to add reference the proper library?
Update: The suggested, but not satisfying fix is to define RuntimeLibrary
like the following:
<RuntimeLibrary>@(ClCompile->'%(RuntimeLibrary)')</RuntimeLibrary>
Output of the initial script is proper in this case, but my task is still not solved, since I want to use this property in a condition. So if I add the following:
<PropertyGroup Condition="'$(RuntimeLibrary)'=='MultiThreadedDebugDLL'">
<TestProp>defined</TestProp>
</PropertyGroup>
...
<Message Text="TestProp = $(TestProp)" Importance="high" />
TestProp
is undefined. How do I make this work for conditions?