admin管理员组文章数量:1290963
I have the following props file that I am including in my Visual Studio C++ project:
<?xml version="1.0" encoding="utf-8"?>
<Project ToolsVersion="15.0" xmlns=";>
<Target Name="GetGitRevision" BeforeTargets="ClCompile">
<Exec Command="git rev-parse --verify --short HEAD" ConsoleToMSBuild="true">
<Output TaskParameter="ConsoleOutput" PropertyName="GitRevision"/>
</Exec>
</Target>
<ItemDefinitionGroup>
<ClCompile>
<PreprocessorDefinitions>GIT_REVISION="$(GitRevision)";%(PreprocessorDefinitions)</PreprocessorDefinitions>
</ClCompile>
</ItemDefinitionGroup>
</Project>
I can see that the target GetGitRevision
does get called before the compile and the output matches the expectation in the build logs.
However, in the c++ code GIT_REVISION
macro is defined by empty, meaning that PreprocessorDefinitions
saw nothing when parsing $(GitRevision)
.
What am I doing wrong?
I have the following props file that I am including in my Visual Studio C++ project:
<?xml version="1.0" encoding="utf-8"?>
<Project ToolsVersion="15.0" xmlns="http://schemas.microsoft/developer/msbuild/2003">
<Target Name="GetGitRevision" BeforeTargets="ClCompile">
<Exec Command="git rev-parse --verify --short HEAD" ConsoleToMSBuild="true">
<Output TaskParameter="ConsoleOutput" PropertyName="GitRevision"/>
</Exec>
</Target>
<ItemDefinitionGroup>
<ClCompile>
<PreprocessorDefinitions>GIT_REVISION="$(GitRevision)";%(PreprocessorDefinitions)</PreprocessorDefinitions>
</ClCompile>
</ItemDefinitionGroup>
</Project>
I can see that the target GetGitRevision
does get called before the compile and the output matches the expectation in the build logs.
However, in the c++ code GIT_REVISION
macro is defined by empty, meaning that PreprocessorDefinitions
saw nothing when parsing $(GitRevision)
.
What am I doing wrong?
Share Improve this question asked Feb 13 at 16:00 antonppantonpp 2,39325 silver badges29 bronze badges 2 |1 Answer
Reset to default 1Thanks a lot to Jonathan Dodds for the explanation in the comments.
After applying the suggestion, the following worked for me:
<?xml version="1.0" encoding="utf-8"?>
<Project ToolsVersion="15.0" xmlns="http://schemas.microsoft/developer/msbuild/2003">
<Target Name="GetGitRevision" BeforeTargets="ClCompile">
<Exec Command="git rev-parse --verify --short HEAD" ConsoleToMSBuild="true">
<Output TaskParameter="ConsoleOutput" PropertyName="GitRevision"/>
</Exec>
<Exec Command="git rev-parse --abbrev-ref HEAD" ConsoleToMSBuild="true">
<Output TaskParameter="ConsoleOutput" PropertyName="GitBranch"/>
</Exec>
<ItemGroup>
<ClCompile>
<PreprocessorDefinitions>GIT_REVISION="$(GitRevision)";%(PreprocessorDefinitions)</PreprocessorDefinitions>
<PreprocessorDefinitions>GIT_BRANCH="$(GitBranch)";%(PreprocessorDefinitions)</PreprocessorDefinitions>
</ClCompile>
</ItemGroup>
</Target>
</Project>
本文标签: visual studioMsBuild use exec output in ClCompileStack Overflow
版权声明:本文标题:visual studio - MsBuild: use exec output in ClCompile - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1741518966a2383064.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
ItemDefinitionGroup
is handled in the evaluation phase. Targets are run in the execution phase. Tasks (e.g.Exec
) can only be used in a target. You need to change thePreprocessorDefinitions
metadata of theClCompile
inside theGetGitRevision
target after theExec
task has been run. – Jonathan Dodds Commented Feb 13 at 16:46