Build Drivers from the Command Line

There are a lot of reasons to be able to build drivers from the command line.  Many release engineering teams want to have automated nightly builds, and there are plenty of other uses for the capability. Recently I encountered more than one experienced Windows driver developer who complained that what they dislike about the Visual Studio integration of the Windows Driver Kit (WDK) was the loss of the ability to build from the command line.  Actually, it is still available; in fact Microsoft recently released a command line only WDK.
This is another of those cases where in the transition to a new technology, it was assumed people were familiar with the details of Visual Studio.  The good news is that for the Windows 10 WDK, building from the command line is documented.  Below is my advice for those who wish to build from the command line for older WDK’s or if you are looking for a quick reference.
When the WDK was integrated into the recent Visual Studio’s, it also was migrated from the old NMAKE to the MsBuild tool.
Project versus Solution
When you create a driver in the WDK, you start with a project. In a logical sense, a project contains all the files that will be compiled into the driver or other specific product, along with all the compiler settings and other configuration data needed.  A solution is a container of one or more projects allowing them to be grouped together and built with a single command.
Command line building
To use Msbuild you need to set up the environment.  The easiest way to do this is to go to the Start menu folder for the version of the Visual Studio you are using and then choose Developer Command Prompt for VS201X where X is 2,3 or 5.   If you want to make this part of a script, invoke VsDevCmd.bat, which is located off the install directory for the appropriate Visual Studio in Common7\Tools.
If you go to the directory with the solution or project file and run MsBuild, it will build the item with the default configuration.  Most of us want to do this from a script.  To make this solid, you want a few arguments.  The most likely argument is a path to a solution or project file for the driver, so the script can be run from anywhere.  Note: if you install the Windows 8 WDK and Windows 8.1 WDK on the same system, the two versions of Visual Studio have a conflict.  Because of this you cannot use a project file from the Windows 8 WDK, but the solution file will still work.  Start by using a simple command line like:

Msbuild <path to solution or project>

Beyond this most of us want to build both 32 and 64 bit versions of the driver.  For that we need to add the switch /p:Platform=<arch> where arch is Win32, or x64.  For example:

Msbuild /p:Platform=Win32 <path to solution or project>

builds the 32-bit version of the driver.  Finally, a well designed script will need to specify the target version of Windows and whether to build the debug or release version of the driver.  The available options have changed depending on the version of the WDK. See the table below:

TargetWindows 8 WDKWindows 8.1 WDKWindows 10 WDK
Vista Debug/p:Configuration="Vista Debug"
Vista Release/p:Configuration="Vista Release"
Win7 Debug/p:Configuration="Win7 Debug"/p:Configuration="Win7 Debug"/p:TargetVersion=”Windows7”
/p:Configuration="Debug"
Win7 Release/p:Configuration="Win7 Release"/p:Configuration="Win7 Release"/p:TargetVersion=”Windows7”
/p:Configuration="Release"
Win8 Debug/p:Configuration="Win8 Debug"/p:Configuration="Win8 Debug"
Win8 Release/p:Configuration="Win8 Release"/p:Configuration="Win8 Release"
Win8.1 Debug/p:Configuration="Win8.1 Debug"/p:TargetVersion=”Windows8.1”
/p:Configuration="Debug"
Win8.1 Release/p:Configuration="Win8.1 Release"/p:TargetVersion=”Windows8.1”
/p:Configuration="Release"
Win10 Debug/p:TargetVersion=”Windows10”
/p:Configuration="Debug"
Win10 Release/p:TargetVersion=”Windows10”
/p:Configuration="Release"

For many script situations, you may want to rebuild the whole project; to do this add the switches /t:clean /t:rebuild to specify the targets of deleting all the files, then rebuilding.  The final result is:

Msbuild /p:platform=Win32 /p:Configuration=”Win7 Debug” /t:clean /t:rebuild <path>

Or

Msbuild /p:platform=Win32 /p:TargetVersion=”Windows7” /p:Configuration=”Debug” /t:clean /t:rebuild <path>

The first line works for the Window 8 or Windows 8.1 WDK and the other works for the Windows 10 WDK.  Finally, you may want to control the level of output.  In that case use the /v:<level> switch where level is: q[uiet], m[inimal], n[ormal], d[etailed], and diag[nostic].
There are a lot more capabilities in MsBuild; you can explore to go beyond this simple overview to get you started.