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 MSBuild builds projects in two phases: evaluation followed by execution. The top level 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 the PreprocessorDefinitions metadata of the ClCompile inside the GetGitRevision target after the Exec task has been run. – Jonathan Dodds Commented Feb 13 at 16:46
  • See "Updating metadata on items in an ItemGroup of a Target". – Jonathan Dodds Commented Feb 13 at 16:46
Add a comment  | 

1 Answer 1

Reset to default 1

Thanks 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