Search code examples
visual-studiovisual-studio-2012build-processprojects-and-solutionspost-build-event

Ignore Post-Build event errors


I have a C++ project which has a post-build event defined through the UI. The event starts an executable and its log messages are printed to the Output window. These messages can be error ones and the VS picks them up as build errors. However, I don't want a failing build, if that command reports errors.

I did some research and found out that using the <Target> element in the file of the project, I can ignore the errors. I defined it at the end of the file.

  <Target Name="PostBuildEvent" Condition="'$(PostBuildEvent)'!=''" DependsOnTargets="$(PostBuildEventDependsOn)">
    <Exec WorkingDirectory="$(OutDir)" Command="$(PostBuildEvent)" IgnoreExitCode="true" />
  </Target>
</Project>

This only works if the post build event is defined in the <PropertyGroup> element.

  <PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Release|Win32'">
    <PostBuildEvent>Start the executable</PostBuildEvent>
  </PostBuildEvent>

However, if I set the command through the UI, the value is put in the <ItemDefinitionGroup> part.

  <ItemDefinitionGroup Condition="'$(Configuration)|$(Platform)'=='Release|Win32'">
      <PostBuildEvent>
        <Command>Start the executable</Command>
      </PostBuildEvent>
  </ItemDefinitionGroup>

If I have the event defined in the <ItemDefinitionGroup> part, the $(PostBuildEvent) will be empty and nothing will be called.

My problem having the property defined in the <PropertyGroup> is that it's not synchronized with the UI. Changing the post-build event on the UI, the <ItemDefinitionGroup> definition will be updated. and not the updated command will be called.

  1. Is it possible to access the <ItemDefinitionGroup>/<PostBuildEvent>/<Command> value in the <Target> element?

  2. If 1. isn't possible, how can I change the <PropertyGroup>/<PostBuildEvent> through the UI?


Solution

  • I managed to find the solution for my problem. Use the following formatted <Target>:

      <Target Name="PostBuildEvent" DependsOnTargets="$(PostBuildEventDependsOn)">
        <Message Text="%(PostBuildEvent.Message)" Condition="'%(PostBuildEvent.Message)' != '' and '%(PostBuildEvent.Command)' != ''" Importance="High" />
        <Exec WorkingDirectory="$(OutDir)" Command="%(PostBuildEvent.Command)" Condition="'%(PostBuildEvent.Command)' != ''" IgnoreStandardErrorWarningFormat="True" />
      </Target>
    

    Using %(...) you can reach the elements defined under <ItemDefinitionGroup>. This way the PostBuildEvent command is still editable through the UI.

    Side note: if you want to have a custom error message parser, define a regex in the CustomErrorRegularExpression property of Exec.

    Example for GoogleTests ran as post-build event:

    <Exec ... CustomErrorRegularExpression="\[  FAILED  \]" />