Search code examples
xmlmsbuildvisual-studio-2019

The order of MSBuild build Events in Visual Studio 2019


According to this Microsoft's documentation, for each project in a Visual Studio 2019 solution, build events run in the following order with other build steps (including custom build steps and tools):

  • Pre-Build event
  • Custom build tools on individual files
  • MIDL
  • Resource compiler
  • The C/C++ compiler
  • Pre-Link event
  • Linker or Librarian (as appropriate)
  • Manifest Tool
  • BSCMake
  • Custom build step on the project
  • Post-Build event

The custom build step on the project and a post-build event run sequentially after all other build processes finish.

However, when the WDK v10.0.19041.0 is installed, the StampInf and InfVerif are executed before the Pre-Build Event.

This is evidenced by the following Microsoft's code:

<Target Name="StampInf"
          Condition="'@(Inf)' != ''"
          BeforeTargets="InfVerif">

...

<Target Name="InfVerif"
          Condition="'@(Inf)' != '' and '$(IsDriverAppToolset)' != 'true'"
          AfterTargets="StampInf"
          BeforeTargets="PreBuildEvent">

The Microsoft's code above is excerpted from:
C:\Program Files (x86)\Windows Kits\10\build\WindowsDriver.Common.targets

Notice, how the AfterTargets and BeforeTargets directives establish the order of execution of StampInf, InfVerify and PreBuildEvent.

Question 1: Is the execution of StampInf and InfVerify before the Pre-Build Event a bug in implementation or an error in Microsoft's documentation ?
Question 2: How to execute a custom command before InfVerif in a portable manner (without modifying Microsoft's source files of the Visual Studio or the WDK) ?


Solution

  • Seems like a bug that InfVerif is implemented to explicitly reference being before the PreBuildEvent target. The WDK shouldn't be interfering with the semantics of PreBuildEvent.

    However, MSBuild will ignore a reference to a target that doesn't exist in the current project. You can set your target to be BeforeTargets="PreBuildEvent;InfVerif;StampInf" and for projects that don't have an InfVerif or StampInf target there should not be an error. You don't need to modify Microsoft's MSBuild code to get around this issue.