【问题标题】:How to access item metadata outside <Target> node in MsBuild?如何在 MsBuild 中访问 <Target> 节点之外的项目元数据?
【发布时间】:2015-02-19 11:12:28
【问题描述】:

我正在尝试为我的 NuGet 包创建一个 .targets 文件,该文件将链接到一个正确的 .lib 文件,具体取决于项目的 C++ 运行时库。 This answer 建议为此使用 %(ClCompile.RuntimeLibrary) 元数据。但似乎无法在&lt;Target&gt; 节点之外访问元数据!并且库依赖添加在&lt;ItemDefinitionGroup&gt; 节点中,就在根&lt;Project&gt; 节点下。

这里是 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>

使用 MsBuild 运行它会产生:

Property = %(ClCompile.RuntimeLibrary)
Metadata = MultiThreadedDebugDLL

同样的语句%(ClCompile.RuntimeLibrary)&lt;Target&gt;节点内使用时扩展为值,但在&lt;Target&gt;节点外的&lt;PropertyGroup&gt;节点中使用时不会扩展为值。

那么如何访问运行时库元数据值以添加引用正确的库?

更新:建议但不令人满意的解决方法是定义RuntimeLibrary,如下所示:

<RuntimeLibrary>@(ClCompile->'%(RuntimeLibrary)')</RuntimeLibrary>

在这种情况下,初始脚本的输出是正确的,但是我的任务仍然没有解决,因为我想在一个条件下使用这个属性。因此,如果我添加以下内容:

<PropertyGroup Condition="'$(RuntimeLibrary)'=='MultiThreadedDebugDLL'">
  <TestProp>defined</TestProp>
</PropertyGroup>
...
<Message Text="TestProp = $(TestProp)" Importance="high" />

TestProp 未定义。如何使这项工作适应条件?

【问题讨论】:

  • re:您的编辑:如果您将该 PropertyGroup 放入 Target 本身,或者如果您使用 Condition="'@(ClCompile-&gt;'%(RuntimeLibrary)')',这将起作用。我不是 100% 确定,但我认为由于 msbuild 使用的评估顺序,您编写它的方式将行不通。
  • @stijn 我不能把这个属性放在&lt;Target&gt; 中,因为我需要在&lt;ItemDefinitionGroup&gt; 的条件下使用它,它不能放在&lt;Target&gt; 中:(
  • 那么@ 的条件似乎是唯一的解决方案?
  • @stijn 是的,但它也不起作用。此外,@(ClCompile-&gt;'%(RuntimeLibrary)') 扩展为类似MultiThreadedDebugDLL;MultiThreadedDebugDLL;MultiThreadedDebugDLL 的字符串,以防项目中有多个文件(几乎总是如此),并且需要在使用前进行拆分。不确定是否可以在条件本身中进行这种拆分。

标签: c++ visual-studio msbuild nuget msvcrt


【解决方案1】:

请尝试以下方法:

  <PropertyGroup>
    <RuntimeLibrary>@(ClCompile->'%(RuntimeLibrary)')</RuntimeLibrary>
  </PropertyGroup>

使用 @ 符号可以引用项目列表。

还有an example on StackOverflow

【讨论】:

  • 抱歉,这个修复不适合我。查看更新后的问题。
  • 帮助我根据使用通配符从包含解析的项目的存在有条件地调用目标。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2012-06-23
  • 1970-01-01
  • 2012-01-03
  • 2012-05-23
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多