本文介绍了在msbuild中过滤项目的元数据的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想使用修改后的元数据创建一个新的项目集合.例如,更改ClCompile.AdditionalIncludeDirectories的定界符.为此,我首先从AdditionalIncludeDirectories元数据创建一个项目集合,然后对其进行转换:

I would like to create a new item collection with modified metadata.For example, change the delimiters of the ClCompile.AdditionalIncludeDirectories.In order to do so I first create an item collection from the AdditionalIncludeDirectories metadata, and then transform it:

<ItemGroup>
  <IncludeDirs0 Include="%(ClCompile.AdditionalIncludeDirectories)">
    <key>@(ClCompile)</key>
  </IncludeDirs0>
</ItemGroup>

<ItemGroup>
  <IncludeDirs Include="@(IncludeDirs0 -> '-I %(Identity)', ' ')">
    <key>%(IncludeDirs0.key)</key>
  </IncludeDirs>
</ItemGroup>

<ItemGroup>
  <Compile Include="@(ClCompile)">
    <IncludeDirs>@(IncludeDirs)</IncludeDirs>
  </Compile>
</ItemGroup>

问题在于如何在编译项"组上过滤IncludeDirs,以使每个编译"项都具有其正确的包含目录. (这样ClCompile身份等于IncludeDirs键).我尝试添加条件,例如:Condition="'%(IncludeDirs.key)'=='%(ClCompile.Identity)'",但没有成功.
我本可以直接使用IncludeDirs:<Message Text="%(IncludeDirs.key) : @(IncludeDirs)"/>,但是我觉得这很重要,因为Compile集合应该包含所有元数据.

The problem is how to filter IncludeDirs on Compile item group, such that each Compile item will have its right include dir. (so that ClCompile identity equals IncludeDirs key).I've tried adding a condition such as: Condition="'%(IncludeDirs.key)'=='%(ClCompile.Identity)'" but without any success.
I could have used the IncludeDirs directly: <Message Text="%(IncludeDirs.key) : @(IncludeDirs)"/> but I feel this misses the point, since the Compile collection should contain all the metadata.

我在这里想念什么?

推荐答案

您可以使用一个附加目标来完成此任务,该目标可以收集所需的物品.请参见 MSBuild批处理.

You can do it with one additional target which collects needed items.See MSBuild Batching.

<?xml version="1.0" encoding="utf-8"?>
<Project DefaultTargets="Build" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
  <ItemGroup>
    <ClCompile Include="1" />
    <ClCompile Include="2">
      <AdditionalIncludeDirectories>2.1;2.2;2.3</AdditionalIncludeDirectories>
    </ClCompile>
  </ItemGroup>

  <Target Name="TransformClCompile"
          Inputs="%(ClCompile.Identity)"
          Outputs="_Non_Existent_Item_To_Batch_">
    <PropertyGroup>
        <IncludeDirs>%(ClCompile.AdditionalIncludeDirectories)</IncludeDirs>
    </PropertyGroup>
    <ItemGroup>
          <IncludeDirs Include="$(IncludeDirs)" />
          <Compile Include="@(ClCompile)">
            <IncludeDirs>@(IncludeDirs ->'-I %(Identity)', ' ')</IncludeDirs>
          </Compile>
    </ItemGroup>
  </Target>

  <Target Name="Build" DependsOnTargets="TransformClCompile">
     <Message Text="compile %(Compile.Identity) %(Compile.IncludeDirs)" />
  </Target>
</Project>

输出:

  compile 1
  compile 2 -I 2.1 -I 2.2 -I 2.3

这篇关于在msbuild中过滤项目的元数据的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

06-30 13:51