【发布时间】:2016-05-21 16:27:03
【问题描述】:
我有一个 msbuild 自定义目标和一个计算值的任务。 任务将值作为属性输出。 我想将此属性用作编译器调用的附加选项。
但是当用作附加选项时,该属性为空。
我的 *.targets 文件如下所示:
<?xml version="1.0" encoding="utf-8"?>
<Project ToolsVersion="12.0" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
<UsingTask TaskName="GetBranchName_TASK" TaskFactory="CodeTaskFactory" AssemblyFile="$(MSBuildToolsPath)\Microsoft.Build.Tasks.v4.0.dll" >
<ParameterGroup>
<sPath ParameterType="System.String" Required="true" />
<sBranchName ParameterType="System.String" Output="true" />
</ParameterGroup>
<Task>
<Code Type="Fragment" Language="cs">
<![CDATA[
... some Code ...
]]>
</Code>
</Task>
</UsingTask>
<Target Name="GetBranchName_TARGET">
<GetBranchName_TASK sPath="$(MSBuildThisFileDirectory)">
<Output PropertyName="BranchName" TaskParameter="sBranchName" />
</GetBranchName_TASK>
<Message Importance="High" Text="BranchName = $(BranchName)" />
</Target>
<PropertyGroup>
<BuildDependsOn>
GetBranchName_TARGET;
$(BuildDependsOn);
</BuildDependsOn>
</PropertyGroup>
</Project>
我的 *.props 文件是这样的:
<?xml version="1.0" encoding="utf-8"?>
<Project ToolsVersion="4.0" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
<PropertyGroup Label="Configuration">
... some Properties here ...
</PropertyGroup>
<Import Project="$(VCTargetsPath)\Microsoft.Cpp.targets" />
<Import Project="IRSGetBranchName.targets" />
<ItemDefinitionGroup>
<ClCompile>
<AdditionalOptions>/DBRANCHNAME=$(BranchName) /DMORE=BAR</AdditionalOptions>
<ClCompile>
<ItemDefinitionGroup>
</Project>
这个.props文件然后被导入几个.vcxproj
在我的 GetBranchName_TARGET 中打印为消息的值与预期一样正确(显示正确的 TFS 分支名称)。 但是当查看 Detailed Build Output 时,Value 似乎是空的:
1>ClCompile
1> ..\FOO.cpp
1> AdditionalOptions = /DBRANCHNAME= /DMORE=BAR
我尝试了几个小时,但没有找到解决方案,我真的希望有人帮助这里出了什么问题......
a) 属性 BranchName 是否在全球范围内不可用?我尝试从其他自定义目标打印属性并且效果很好!
b) 还是在执行我的 Target 之前评估/构建了 ClCompile.AdditionalOptions?在这种情况下,我该如何重新评估?
c) ...
我非常感谢任何输入。
【问题讨论】:
-
a) 是的,它是 b) 是的,因为它是在目标之外全局定义的,它在目标运行之前被评估,这就是你的问题。我不是 100% 确定,但将 itemDefinitionGroup 与 ClCompile inside GetBranchName_TARGET 应该可以工作
-
@stijn:谢谢。您的评论使我找到了完美的解决方案。就像 stukselbax 的答案一样。谢谢!
标签: msbuild msbuild-task compiler-options