How to link a library based on C++ runtime library of a project?

Topics: General
Dec 20, 2014 at 9:07 PM
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?