【问题标题】:Overriding project properties with a custom task使用自定义任务覆盖项目属性
【发布时间】:2019-09-23 06:35:59
【问题描述】:

我们的 C++ 项目使用 MSBuild 在 Windows 上构建,并在 *nix 上使用 GNU make。我正在尝试在 MSBuild 中重新创建以下单行 GNU make 的功能:

GENN_PATH:=$(abspath $(dir $(shell which genn-buildmodel.sh))../userproject/include)

本质上是将变量设置为相对于路径中的可执行文件的路径。然而,事实证明这是一场在 MSBuild 中实施的战斗......

以下是我的 vcxproj 中的(希望是)相关部分。出于测试目的,我首先将要覆盖的变量设置为明显的:

<?xml version="1.0" encoding="utf-8"?>
<Project DefaultTargets="Build" ToolsVersion="12.0" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
...
  <PropertyGroup Label="Configuration">
    ...
    <GeNNUserProject>UNDEFINED</GeNNUserProject>
  </PropertyGroup>

然后,在我的 ClCompile 项定义中,我将此属性的值添加到其他包含目录

  <ItemDefinitionGroup>
    <ClCompile>
      ...
      <AdditionalIncludeDirectories>include;$(GeNNUserProject)</AdditionalIncludeDirectories>
    </ClCompile>
    ...
  </ItemDefinitionGroup>

为了找到这个路径,我使用 where 命令并将它的输出重定向到一个属性。然后,从这里,我找到包含目录并将其打印出来 - 这有效!

  <Target Name="FindUserProjects">
    <Exec Command="where genn-buildmodel.bat" ConsoleToMsBuild="true">
      <Output TaskParameter="ConsoleOutput" PropertyName="GeNNBuildModelPath" />
    </Exec>
    <PropertyGroup>
        <GeNNUserProject>$([System.IO.Path]::GetFullPath($([System.IO.Path]::GetDirectoryName($(GeNNBuildModelPath)))\..\userproject\include))</GeNNUserProject>
    </PropertyGroup>
    <Message Text="MAGIC GENN-FINDING! $(GeNNBuildModelPath) -> $(GeNNUserProject)"/>
  </Target>

我尝试了多种方法使其成为 ClCompile 的依赖项,包括将 Target 设置为 BeforeTargets="PrepareForBuild" 以及以下内容:

  <PropertyGroup>
    <BeforeClCompileTargets>
        FindUserProjects;
        $(BeforeClCompileTargets);
    </BeforeClCompileTargets>
  </PropertyGroup>  
</Project>

无论我做什么,我的自定义目标都会运行,但该属性不会被覆盖。 Google 建议,如果属性在依赖中被覆盖,它们应该从目标中可见,并且从挖掘到 Microsoft.CPP*.targets 这就是设置BeforeClCompileTargets 正在做的事情。

【问题讨论】:

    标签: visual-c++ msbuild


    【解决方案1】:

    这里的问题不是目标没有设置属性,而是 AdditionalIncludeDirectories 项元数据是从原始值设置的。解决方案是直接从目标设置它:

    <ItemGroup>
          <ClCompile>
           <AdditionalIncludeDirectories>%(AdditionalIncludeDirectories);$([System.IO.Path]::GetFullPath($([System.IO.Path]::GetDirectoryName($(GeNNBuildModelPath)))\..\userproject\include))</AdditionalIncludeDirectories>
          </ClCompile>
        </ItemGroup>
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2018-05-08
      • 2014-08-18
      • 1970-01-01
      • 1970-01-01
      • 2021-09-24
      相关资源
      最近更新 更多