Skip to content

Instantly share code, notes, and snippets.

@pavel-a
Last active May 8, 2022 14:44
Show Gist options
  • Save pavel-a/1489f41e7da4c12973e6 to your computer and use it in GitHub Desktop.
Save pavel-a/1489f41e7da4c12973e6 to your computer and use it in GitHub Desktop.
Set of scripts for building Windows Driver Kit projects (pre-Win8 WDKs)
:: Replacement for WDK build env shortcuts
:: Opens interactive cmd box for WDK build.
:: This is *not* intended to run build from Visual Studio!
:: Rev.b 08-may-2009 pavel_a@fastmail.fm -- for WDK 7600 (7.1.0) or 6xxx
@echo off
@IF NOT CMDEXTVERSION 2 EXIT
set BASEDIR=%~dp0
: *** SET BASEDIR TO YOUR WDK PATH if this file is not there ***
:- set BASEDIR=%SystemDrive%\winddk\7601.0
if NOT exist %BASEDIR%\bin\setenv.bat (@echo ERROR: bad WDK path! & goto :ERR )
:: *** Choose where to CD initially. Set the desired path here, or CD later.
:- set _a_workdir=%~dp0
set _a_workdir=%cd%
@COLOR B0
@title [ WDK build: %BASEDIR% %_a_workdir% ]
@ECHO Starting WDK Build environment ...
: *** To skip prompts, select your favorite target, build options
: *** and uncomment goto :CFN below.
set _a_targetenv=WXP
set _a_chkfre=chk
set _a_build_flags=-wgz
:- goto :CFN
:: Prompt for target OS and checked/free:
:RTEQ
@ECHO What do you want to build today?
@ECHO 32-bit: [1] WinXP [2] Win2003 [3] Vista [4] Win7
@ECHO 64-bit: [5] Win2003 or WinXP [6] Vista or Win2008 [7] Win7 or Win2008R2
SET /P _a_n=Target OS?
if %_a_n% LEQ 7 goto :RTE%_a_n%
goto :RTEQ
:RTE0
set _a_targetenv=W2K
goto :RTEN
:RTE1
set _a_targetenv=WXP
goto :RTEN
:RTE2
set _a_targetenv=WNET
goto :RTEN
:RTE3
set _a_targetenv=WLH
goto :RTEN
:RTE4
set _a_targetenv=WIN7
goto :RTEN
:RTE5
set _a_targetenv=WNET x64
goto :RTEN
:RTE6
set _a_targetenv=WLH x64
goto :RTEN
:RTE7
set _a_targetenv=WIN7 x64
goto :RTEN
:RTEN
:CF0
SET /P _a_n=Checked(1) or Free(2)?
if %_a_n% LEQ 2 goto :CF%_a_n%
goto :CF0
:CF1
set _a_chkfre=chk
goto :CFN
:CF2
set _a_chkfre=fre
:CFN
:- echo OS=%_a_targetenv% %_a_chkfre%
:: Reset PATH to default. We don't want all the random junk that builds up there.
PATH %windir%\system32;%windir%;%windir%\system32\wbem;
:: Remove all MAKE*, VS80*, VS90*, VC* and so on
: VS2005+ can set VS_UNICODE_OUTPUT. Affects the WDK compiler too, so remove it.
: QNX IDE can set MAKEFLAGS
for /F "useback tokens=1-2 delims==" %%i IN (`2^>NUL set VC`) DO set %%i=
for /F "useback tokens=1-2 delims==" %%i IN (`2^>NUL set VS`) DO set %%i=
for /F "useback tokens=1-2 delims==" %%i IN (`2^>NUL set MAKE`) DO set %%i=
set INC=
set INCLUDE=
set LIB=
:: Comment out the next line for WDKs that do not have OACR, or if you do want OACR
if exist %BASEDIR%\bin\x86\OACR set _a_no_oacr=NO_OACR
: Save username, setenv.bat clobbers it. I use it in privatebuild version string --pa01
set _a_username=%USERNAME%
: Now call the WDK's setenv.bat
call %BASEDIR%\bin\setenv.bat %BASEDIR%\ %_a_chkfre% %_a_targetenv% %_a_no_oacr%
if NOT "%_a_workdir%" == "" cd /d %_a_workdir%
@title [ WDK build: %DDK_TARGET_OS% %DDKBUILDENV% %cd% ]
: Restore username
if NOT "%_a_username%" == "" set USERNAME=%_a_username%
color 07
::**************************************************
:: TODO add here your env tweaks after WDK setenv
::**************************************************
:: Now spawn another cmd for command input:
@start "WDK build %DDK_TARGET_OS% %DDKBUILDENV%" %comspec% /k echo Now cd to your project and type build commands. ^& doskey/macros^|findstr build
exit /B
@rem Errors arrive here:
:ERR
pause
goto :EOF
: Wrapper to run WDK build from Visual Studio
: It is similar to "ddkbuild.bat" but simpler.
: pa04 21-jun-11 For WDK 7600 (7.1.0) or 6xxx
: Usage:
: ddkwrap [-c] ConfigName [platform]
: ddkwrap -c
: Args:
: -c : force rebuild/clean build
: ConfigName: the project configuration name from the VC project. Target OS and chk/fre are derived from this.
: Platform: "Win32" (default) or "x64"
:
: NOTES: Visual Studio runs this script in the directory where the .vcproj file is located.
: You can set WDKROOT_LOCAL to override WDKROOT hardcoded elsewhere.
: TODO: Customize this script: add or change configurations,
: Mix and match build products for every ConfigName,platform combination
: For example, you can build 32-bit and 64-bit drivers, or WinXP and Win7 drivers altogether.
: TODO: consider using binplace for postprocessing
: Assume that .vcproj dir can be at most one level below the SOURCES dir
if not exist SOURCES if not exist DIRS cd ..
if not exist SOURCES if not exist DIRS (@echo ERROR: no SOURCES or DIRS here! & exit /B 1 )
if NOT "%WDKROOT_LOCAL%" == "" (
@echo Using WDKROOT_LOCAL path = %WDKROOT_LOCAL%
set WDKROOT=%WDKROOT_LOCAL%
)
if "%WDKROOT%" == "" (@echo ERROR: WDKROOT or WDKROOT_LOCAL not set & exit /B 1 )
if NOT exist %WDKROOT%\bin\setenv.bat (@echo ERROR: WDKROOT incorrect? & exit /B 1 )
set a_tooldir=%~dp0
set a_rebuild=
if /I "%1" == "-c" ( set a_rebuild=-c & shift )
if "%1" == "" (
if not "%a_rebuild%" == "" goto LBL_CLEAN
@echo ERROR: No config name & exit /B 1
)
set a_conf=%1
set a_platf=
if /I "%2" == "x64" set a_platf=x64
@echo [ddkwrap.cmd 2.3 (c) pavel_a@fastmail.fm] Config=%a_conf% clean=%a_rebuild%
goto LABEL_%a_conf%%a_platf%
exit /B 2
: ##############################################################
:LABEL_Debug
@rem Batch build for all 32-bit platforms, checked
call :ddkbld1 WXP CHK
call :postbld XP CHK
:- call :ddkbld1 WLH CHK
:- call :postbld WLH CHK
goto :AFTERBUILD
:LABEL_Release
@rem Batch build for all 32-bit platforms, free
call :ddkbld1 WXP FRE
call :postbld XP FRE
:- call :ddkbld1 WLH FRE
:- call :postbld WLH FRE
goto :AFTERBUILD
:LABEL_Debug_XP
@rem This is checked build, XP 32-bit only
call :ddkbld1 WXP CHK
call :postbld XP CHK
goto :AFTERBUILD
:LABEL_Release_XP
call :ddkbld1 WXP FRE
call :postbld XP FRE
goto :AFTERBUILD
:LABEL_Debug_Vista
@rem This is checked build, Vista 32-bit only
call :ddkbld1 VISTA CHK
call :postbld VISTA CHK
goto :AFTERBUILD
:LABEL_Release_Vista
@rem This is free build, Vista 32-bit only
call :ddkbld1 VISTA FRE
call :postbld VISTA FRE
goto :AFTERBUILD
:LABEL_Debugx64
:LABEL_Debug_Vistax64
@rem This is checked build, Vista x64
call :ddkbld1 VISTA x64 chk
:TODO*** postbld
goto :AFTERBUILD
:LABEL_Releasex64
:LABEL_Release_Vistax64
@rem This is free build, Vista x64
call :ddkbld1 VISTA x64 fre
:TODO*** postbld
goto :AFTERBUILD
: ############## Subroutine: run WDK build once ############
: ############## Using WDK 6000+
:ddkbld1
@echo ==== Build for [%1] [%2] [%3] ===========
: Reset PATH. We don't want VC paths and all random junk that builds up there.
PATH %windir%\system32;%windir%;%windir%\system32\wbem;
: Remove other nasty env variables:
set VS_UNICODE_OUTPUT=
set MAKEFLAGS=
set MAKE=
set INCLUDE=
set LIB=
set INC=
: Clear DDKBUILDENV if remains from previous run
set DDKBUILDENV=
: Save username. setenv.bat clobbers it, and we sometimes want it.
set _a_username=%USERNAME%
pushd .
: For WDKs that have OACR: turn it OFF.
if exist %WDKROOT%\bin\x86\OACR set _a_no_oacr=NO_OACR
call %WDKROOT%\bin\setenv.bat %WDKROOT%\ %1 %2 %3 %_a_no_oacr%
: Restore username saved above
if NOT "%_a_username%" == "" set USERNAME=%_a_username%
popd
%WDKROOT%\bin\x86\BUILD.EXE -zwe %a_rebuild%
@echo ====== wdk build end ========
goto :EOF
: #############################################################
: ################# subroutine: run post build script #########
:postbld
@if exist binplc.cmd binplc.cmd %1 %2
goto :EOF
: #############################################################
:AFTERBUILD
goto :EOF
: ################# subroutine: cleanup #######################
:LBL_CLEAN
call %a_tooldir%\cleanup.cmd
goto :EOF
<?xml version="1.0" encoding="Windows-1252"?>
<!-- Add-=on property sheet for WDK projects, for VS2005 and 2008 -->
<VisualStudioPropertySheet
ProjectType="Visual C++"
Version="8.00"
Name="driverpp"
>
<Tool
Name="VCCLCompilerTool"
UseUnicodeResponseFiles="false"
AdditionalIncludeDirectories="&quot;$(WDKROOT)\inc\crt&quot;;&quot;$(WDKROOT)\inc\ddk&quot;;&quot;$(WDKROOT)\inc\api&quot;"
Detect64BitPortabilityProblems="true"
/>
<Tool
Name="VCNMakeTool"
BuildCommandLine="$(BLDCMD) $(ConfigurationName) $(PlatformName)"
ReBuildCommandLine="$(BLDCMD) -c $(ConfigurationName) $(PlatformName)"
CleanCommandLine="$(A_TOOLS)\cleanup.cmd"
Output=""
PreprocessorDefinitions=""
IncludeSearchPath="&quot;$(WDKROOT)\inc\api&quot;;&quot;$(WDKROOT)\inc\ddk&quot;;&quot;$(WDKROOT)\inc\crt&quot;"
ForcedIncludes=""
AssemblySearchPath=""
ForcedUsingAssemblies=""
CompileAsManaged=""
/>
<UserMacro
Name="WDKROOT"
Value="$(systemdrive)\winddk\7600"
PerformEnvironmentSet="true"
/>
<UserMacro
Name="BLDCMD"
Value="$(A_TOOLS)\ddkwrap.cmd"
PerformEnvironmentSet="true"
/>
<UserMacro
Name="A_TOOLS"
Value="$(SolutionDir)\Scripts"
PerformEnvironmentSet="true"
/>
</VisualStudioPropertySheet>
' Re-create Start Menu shortcuts after xcopy-instaling WDK (copying without running installer)
' Run in the directory where you want to create shortcuts. Fix the WDK path below!
Set Shell = CreateObject("WScript.Shell")
Set Fso = CreateObject("Scripting.FileSystemObject")
Set Env = Shell.Environment("Process")
CmdPath = Env("windir")+"\System32\cmd.exe"
DestPath = Shell.CurrentDirectory
' *** Write the actual WDK path below ***
WdkPath = Env("SystemDrive")+"\winddk\7600"
OacrOpt = ""
if Fso.FolderExists( WdkPath & "\bin\x86\OACR") then
OacrOpt = " no_oacr"
end if
' WinXP FRE
Set link = Shell.CreateShortcut(DestPath & "\WinXP_free.lnk")
link.TargetPath = CmdPath
link.Arguments = "/k"+WdkPath+"\bin\setenv.bat " _
+WdkPath+"\ "+"fre WXP"+OacrOpt
link.Description = "WXP FRE "+WdkPath
link.IconLocation = "%windir%\system32\moricons.dll,4"
link.Save()
' WinXP CHK
Set link = Shell.CreateShortcut(DestPath & "\WinXP_chk.lnk")
link.TargetPath = CmdPath
link.Arguments = "/k"+WdkPath+"\bin\setenv.bat "+WdkPath+"\ " _
+"CHK WXP"+OacrOpt
link.Description = "WXP CHK "+WdkPath
link.IconLocation = "%windir%\system32\moricons.dll,2"
link.Save()
' ..... and so on for other targets ...
: Clean up after WDK BUILD
for /D %%f in (objfre_* objchk_*) do rmdir /s /q %%f
for %%f in (buildchk_*.log buildchk_*.wrn buildchk_*.err) do del /s %%f
for %%f in (buildfre_*.log buildfre_*.wrn buildfre_*.err) do del /s %%f
@if "%1" == "VSCLEAN" (
rem Delete VC++ temp stuff
del *.ncb
del *.suo
del *.vcproj*.user
rmdir /s Debug
rmdir /s Release
)
@pavel-a
Copy link
Author

pavel-a commented Mar 12, 2015

These batch files are for Win7 & Vista WDKs (that are not integrated with Visual Studio)
To use: set the environment variable WDKROOT to the WDK location.

Variable WDKROOT_LOCAL, if set, overrides WDKROOT. This is useful when WDKROOT is defined in common read-only project files, but on certain machine the WDK is in different location or you want to try a different WDK version.

build_box_wdk.cmd is replacement of the "dos box" shortcuts of the WDK.
It can be copied into the project directory and tweaked as you like.
Or you can copy it into the bin directory of the WDK and run from there, to pick the WDK location automatically.

WDKs 7.x and earlier can be archived and xcopied to other machine without installation.
In this case the start menu shortcuts won't be created; you can re-create the shortcuts with make-WDK-shortcuts.vbs or use build_box_wdk.cmd instead.
Also, the WDK path won't be put in the registry, so you have to define WDKROOT or WDKROOT_LOCAL env. variables.

To run WDK build from Visual Studio with wdkwrap.bat the env. variable WDKROOT can be specified via a common property sheet added to every WDK based project. See driverpp.vsprops as example.

Format of property sheets has changed from VC++ 2005/8 (.vsprops) to VC++ 2010 and newer (.props); I may add both versions later.

All this is not actual with Win8 and newer WDKs which use Visual Studio.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment