MSBuild:使用我的自定义目标更新C++预处理器定义

1wnzp6jl  于 2022-12-30  发布在  其他
关注(0)|答案(2)|浏览(157)

我正在尝试创建一个自定义的MSBuild目标来自动获取SVN版本号到某个定义中,比如MY_VERSION.因此,我的目标文件(get-svn-info.targets):

<?xml version="1.0" encoding="utf-8"?>
<Project ToolsVersion="4.0" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
  <Target Name="GetSvnRevision" BeforeTargets="PrepareForBuild">
    <Exec Command="svn info --show-item revision" ConsoleToMSBuild="true">
      <Output TaskParameter="ConsoleOutput" PropertyName="SvnRevisionNum" />
    </Exec>
    <ItemGroup>
      <ItemDefinitionGroup>
        <ClCompile>
          <PreprocessorDefinitions>%(PreprocessorDefinitions);MY_VERSION=$(SvnRevisionNum);</PreprocessorDefinitions>
        </ClCompile>
      </ItemDefinitionGroup>
    </ItemGroup>
    <Message Text="MY_VERSION=$(SvnRevisionNum)" Importance="high"></Message>
  </Target>
</Project>

最后,修改了项目文件:

<Project ...
  <Import Project="get-svn-info.targets" />
...
</Project>

但编译器显示“MY_VERSION”:此处未声明标识符

std::cout << "Hello World!\n" << std::to_string(MY_VERSION) << std::endl;

我做错了什么?为什么PreprocessorDefinitions仍然具有旧值?谢谢
构建日志输出(跳过许多不重要的行):

...
1>Target "GetSvnRevision" in file "D:\ConsoleApplication1\get-svn-info.targets":
1>  Using "Exec" task from assembly "Microsoft.Build.Tasks.Core, Version=15.1.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a".
1>  Task "Exec"
1>    Task Parameter:ConsoleToMSBuild=True
1>    Task Parameter:Command=svn info --show-item revision
1>    svn info --show-item revision
1>    30
1>    Output Property: SvnRevisionNum=30
1>  Done executing task "Exec".
1>  Task "Message"
1>    Task Parameter:Importance=high
1>    Task Parameter:Text=MY_VERSION=30
1>    MY_VERSION=30
1>  Done executing task "Message".
1>Done building target "GetSvnRevision" in project "ConsoleApplication1.vcxproj".
...
1>Target "ClCompile" in file "C:\Program Files (x86)\Microsoft Visual Studio\2019\Community\MSBuild\Microsoft\VC\v160\Microsoft.CppCommon.targets":
1>  Task "CL"
...
1>    Task Parameter:
1>        Sources=
1>            ConsoleApplication1.cpp
1>                    PreprocessorDefinitions=WIN32;_DEBUG;_CONSOLE;_UNICODE;UNICODE;
...
1>    Task Parameter:
1>        PreprocessorDefinitions=
1>            WIN32
1>            _DEBUG
1>            _CONSOLE
1>            _UNICODE
1>            UNICODE
...
1>    D:\ConsoleApplication1\ConsoleApplication1.cpp(9,53): error C2065: 'MY_VERSION': undeclared identifier
1>    The command exited with code 2.
1>  Done executing task "CL" -- FAILED.
1>Done building target "ClCompile" in project "ConsoleApplication1.vcxproj" -- FAILED.
1>
1>Done building project "ConsoleApplication1.vcxproj" -- FAILED.
...
uqdfh47h

uqdfh47h1#

ItemDefinitionGroup的语法不正确。ItemDefinitionGroup不应位于ItemGroup内。
ItemDefinitionGroup将添加已更改的PreprocessorDefinitions作为ClCompile ItemGroup中项目的默认值。
但是ItemDefinitionGroup不能在Target中使用。
但是我们仍然可以修改ClCompile ItemGroup中的项目的元数据。
我建议设两个目标。

<Target Name="GetSvnRevision" BeforeTargets="PrepareForBuild">
    <Exec Command="svn info --show-item revision" ConsoleToMSBuild="true">
      <Output TaskParameter="ConsoleOutput" PropertyName="SvnRevisionNum" />
    </Exec>
    ...
  </Target>

  <Target Name="ApplySvnRevision" BeforeTargets="ClCompile">
    <ItemGroup>
      <ClCompile>
        <PreprocessorDefinitions>%(PreprocessorDefinitions);MY_VERSION=$(SvnRevisionNum);</PreprocessorDefinitions>
      </ClCompile>
    </ItemGroup>
  </Target>

“GetSvnRevision”可以提前执行。但“ApplySvnRevision”应在编译之前执行。

gk7wooem

gk7wooem2#

这个解决方案是可行的,但也许有一个更优雅的解决方案?毕竟,MY_VERSION不仅可以在一个文件ConsoleApplication1.cpp中使用?谢谢

<Target Name="GetSvnRevision" BeforeTargets="PrepareForBuild">
<Exec Command="svn info --show-item revision" ConsoleToMSBuild="true">
  <Output TaskParameter="ConsoleOutput" PropertyName="SvnRevisionNum" />
</Exec>
<ItemGroup>
  <ClCompile Remove="ConsoleApplication1.cpp" />
  <ClCompile Include="ConsoleApplication1.cpp">
    <PreprocessorDefinitions>%(PreprocessorDefinitions);MY_VERSION=$(SvnRevisionNum);</PreprocessorDefinitions>
  </ClCompile>
</ItemGroup>
<Message Text="ME_VERSION=$(SvnRevisionNum)" Importance="high"></Message>

相关问题