@echo off
setlocal
rem ***************************************************************************
rem * Use this cmd file to build the entire project.  Assumes a               *
rem * clean "get" of the root of the entire project from SourceSafe.          *
rem *                                                                         *
rem * Must be called from an NTDDK checked or free build window.  Builds all  *
rem * components checked or free (debug or release) based on the build env.   *
rem *                                                                         *
rem * Parameters:                                                             *
rem *                                                                         *
rem *    %0    <options>                                                      *
rem *          /c                build /c (deletes all object files)          *
rem *          /?                display help                                 *
rem *                                                                         *
rem * Associated files: exports and makerelease.cmd.                          *
rem *                                                                         *
rem * exports                                                                 *
rem *    This is a text file containing a list of IDE-based projects which    *
rem *    need a makefile exported.                                            *
rem *                                                                         *
rem * makerelease.cmd                                                         *
rem *    This increments the buildnumber, labels the vss tree and then uses   *
rem *    buildall to build the project.                                       *
rem ***************************************************************************

echo Digi Build Utility

if /I "%1"=="/h" goto ExitDisplayHelp
if /I "%1"=="-h" goto ExitDisplayHelp
if    "%1"=="?"  goto ExitDisplayHelp
if    "%1"=="/?" goto ExitDisplayHelp
if    "%1"=="-?" goto ExitDisplayHelp

rem Use DebugScript Env var to *echo* commands to console instead of executing them.
if     defined DebugScript set DebugEcho=echo
if not defined DebugScript set DebugEcho=

rem ***************************************************************************
rem * Verify and remember the build environment.  IDE-based projects need     *
rem * this info passed on the nmake command line to build correctly.          *
rem ***************************************************************************

:GetBuildEnvironment
echo.
echo Verifying environment...
set DIGIBUILDENV=
if "%DDKBUILDENV%"==""          goto ExitNotBuildWindow
if "%DDKBUILDENV%"=="checked"   set DIGIBUILDENV=Debug
if "%DDKBUILDENV%"=="free"      set DIGIBUILDENV=Release
if "%DDKBUILDENV%"=="chk"       set DIGIBUILDENV=Debug
if "%DDKBUILDENV%"=="fre"       set DIGIBUILDENV=Release
if "%DIGIBUILDENV%"==""         goto ExitCorruptedBuildWindow

if "%DIGIBUILDENV%"=="Debug"   set BUILDTYPE=checked
if "%DIGIBUILDENV%"=="Release" set BUILDTYPE=free

rem Determine which OS we're building for.  We can detect the difference 
rem between NT4 and NT5 build environments based on the presence of a new 
rem environment variable added in the NT5 DDK -- DDK_INC_PATH.  If found, we're
rem building for NT5.
set NTOSVER=NT4
if DEFINED DDK_INC_PATH set NTOSVER=NT5

:GetPlatform
if "%Cpu%"==""         goto ExitNotBuildWindow
if /I "%Cpu%"=="IA64"  goto GetPlatformComplete
if /I "%Cpu%"=="i386"  goto GetPlatformComplete
if /I "%Cpu%"=="alpha" goto GetPlatformComplete
goto ExitCorruptCpu
:GetPlatformComplete
echo Verified, found:  
echo    Cpu         = %Cpu%
echo    DdkBuildEnv = %DDKBUILDENV%
echo    OS Version  = %NTOSVER%

set BinDir=.\bin\%Cpu%\%BUILDTYPE%
if not exist %BinDir% %DebugEcho% md %BinDir%

:GetOptions
set CompleteRebuild=0
set MsdevCmd=/MAKE
set MsdevCmdOpts=/USEENV
set NmakeOpts=
set BuildOpts=

rem The following lines make sure that we get the right symbols
set USE_PDB=1
set NTDEBUGTYPE=windbg

rem Default to symbols without any debug information
set NTDEBUG=ntsdnodbg
if "%DIGIBUILDENV%"=="Debug" set NTDEBUG=ntsd

if "%1"=="" goto GetOptionsDone
if /I "%1"=="/c" set CompleteRebuild=1
if "%CompleteRebuild%"=="1" set BuildOpts=%BuildOpts% /c
if "%CompleteRebuild%"=="1" set NmakeOpts=/a
if "%CompleteRebuild%"=="1" set MsdevCmdOpts=%MsdevCmdOpts% /REBUILD

:GetOptionsDone
set BuildOpts=%BuildOpts% /owZ


rem ***************************************************************************
rem * Build all components.                                                   *
rem *                                                                         *
rem * Start with ddk environment builds, i.e. using build utility.  Then walk *
rem * through all exported projects building one at a time.                   *
rem ***************************************************************************

if defined SkipDdkBuild goto DdkBuildComplete
echo.
echo Build all DDK components

%DebugEcho% build %BuildOpts%
if ERRORLEVEL 1 goto ExitDdkBuildError
:DdkBuildComplete

rem Export, build and copy all components as specified in Exports file
for /F "eol=; tokens=1-14" %%i in (exports) do if NOT ERRORLEVEL 1 call :MakeSingle %%i %%j %%k %%l %%m %%n %%o %%p %%q %%r %%s %%t %%u %%v
if ERRORLEVEL 1 goto ExitMakeError

echo.
echo Done!
goto Exit

:ExitCorruptCpu
call :PrintErrorBanner
echo Corrupted Cpu in the build environment.  Open a new checked or
echo free build window and try again!
goto Exit

:ExitNotBuildWindow
call :PrintErrorBanner
echo %0 must be called from an NTDDK Checked or Free build window.
goto Exit

:ExitCorruptedBuildWindow
call :PrintErrorBanner
echo Corrupted DDKBUILDENV in the build environment.  Open a new checked
echo or free build window and try again!
goto Exit

:ExitDdkBuildError
call :PrintErrorBanner
echo DDK build returned errors, stopping build.  See build.* for details.
goto Exit

:ExitMakeError
call :PrintErrorBanner
echo Project returned build errors, stopping build.
goto Exit

:ExitNoOSVer
call :PrintErrorBanner
echo NT OS Version needs to be passed in as the first argument. 
echo Valid values are NT4 or NT5
echo (e.g. buildall NT5 /c)
goto Exit

:Exit
endlocal
goto :EOF

rem ***************************************************************************
rem * SubRoutines                                                             *
rem ***************************************************************************

:MakeSingle
setlocal
rem Parameters:
rem   %1  <ProjDir>      // Relative project path
rem   %2  <DspName>      // DevStudio project filename (sans .dsp extension)
rem   %3  <CfgPrefix>    // DevStudio project configuration name (sans cpu, build)
rem   %4  <fExport>      // Boolean: export a makefile (yes,no)
rem   %5  <fBuild>       // Boolean: call nmake to build this component (yes,no)
rem   %6  <fCopy>        // Boolean: copy the output file to staging directory (yes,no)
rem   %7  <OutFile>      // Output file name
rem   %8  <OutDir>       // Optional output file path (.,default)
rem   %9  <DestFile>     // Destination file name (after copied to bin directory)
rem   %10 <BuildOS>      // For which NT OS should the project be built (NT4,NT5,*)
rem   %11 <BuildCPU>     // For which CPU should the project be built (i386,ia64,*)
rem   %12 <NT5CFG>       // Boolean: DSP has a CFG specific for NT5 (yes,no)
rem   %13 <PdbName>      // Name of corresponding PDB output filename for NT5 configs
rem   %14 <PdbDir>       // Target path for PDB filename (relative to staging directory)

set ProjDir=%1
set DspName=%2
set CfgPrefix=%3
set fExport=%4
set fBuild=%5
set fCopy=%6
set OutFile=%7
set OutDir=%8
set DestFile=%9
shift
set BuildOS=%9
shift
set BuildCPU=%9
shift
set NT5CFG=%9
shift
set PdbName=%9
shift
set PdbDir=%9
echo.
echo MakeSingle %ProjDir% %DspName% %CfgPrefix% %fExport% %fBuild% %fCopy% %OutFile% %OutDir% %DestFile% %BuildOS% %BuildCPU% %NT5CFG% %PdbName% %PdbDir%

rem decide if this project will be built/copied etc.

:CheckCPU

if /I (%BuildCPU%)==(*)        goto CheckOS
if /I (%BuildCPU%)==(%Cpu%)    goto CheckOS
echo Skipping build of project %ProjDir%\%DspName%, not needed for CPU=%Cpu%
goto ExitSingle

:CheckOS

if /I (%BuildOS%)==(*)         goto Buildit
if /I (%BuildOS%)==(%NTOSVER%) goto Buildit
echo Skipping build of project %ProjDir%\%DspName%, not needed for OS=%NTOSVER%
goto ExitSingle

:Buildit

if defined SkipBuild goto BuildComplete
if /I (%fBuild%)==(no) goto BuildComplete

set ConfigExt=
if /I (%NT5CFG%)==(no) goto MakeIt
if /I (%NTOSVER%)==(NT4) goto MakeIt
set ConfigExt= NT5

:MakeIt
pushd %ProjDir%
%DebugEcho% msdev "%DspName%.dsp" %MsdevCmd% "%CfgPrefix% - Win32 %Cpu% %DIGIBUILDENV%%ConfigExt%" %MsdevCmdOpts%
popd
if ERRORLEVEL 1 goto ExitMsdevError

:BuildComplete

if defined SkipCopy goto CopyComplete
if /I (%fCopy%)==(no) goto CopyComplete
if /I (%OutDir%)==(default) goto CopyVcDefault
if /I (%OutDir%)==(ddkdef) goto CopyDdkDefault
set OutFilePath=.\%ProjDir%\%OutDir%
goto :DoCopy

:CopyVcDefault
rem VC projects default are always same as NT4 DDK default.
set OutFilePath=.\%ProjDir%\%Cpu%\%BUILDTYPE%
goto DoCopy

:CopyDdkDefault
rem NT4 DDK has a default output directory of .\cpu\ddkbuildenv
rem NT5 DDK has a default output directory of .\cpu
if (%NTOSVER%)==(NT4)   set OutFilePath=.\%ProjDir%\%Cpu%\%BUILDTYPE%
if (%NTOSVER%)==(NT5)   set OutFilePath=.\%ProjDir%\%Cpu%
goto DoCopy

:DoCopy
%DebugEcho% copy %OutFilePath%\%OutFile% %BinDir%\%DestFile%
if ERRORLEVEL 1 goto ExitCopyError

:CopyPDB
if (%PdbName%)==(*)     goto CopyComplete
if not exist %BinDir%\%PdbDir% %DebugEcho% md %BinDir%\%PdbDir%
%DebugEcho% copy %OutFilePath%\%PdbName%  %BinDir%\%PdbDir%\%PdbName%
if ERRORLEVEL 1 goto ExitCopyPDBError

:CopyComplete
goto ExitSingle

:ExitMsdevError
call :PrintErrorBanner
echo MsDev error building .\%ProjDir%\%DspName%.dsp, aborting!
goto ExitSingle

:ExitCopyError
call :PrintErrorBanner
echo Error copying file %OutFilePath%\%OutFile%, aborting!
goto ExitSingle

:ExitCopyPDBError
call :PrintErrorBanner
echo Error copying file %OutFilePath%\%PdbName%, aborting!
goto ExitSingle

:ExitSingle
endlocal
goto :EOF


:PrintErrorBanner
rem Parameters
rem   None
echo.
echo !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
echo.
goto :EOF


:ExitDisplayHelp
echo ***************************************************************************
echo * Use this cmd file to build the entire project.  Assumes a               *
echo * clean "get" of the root of the entire project from SourceSafe.          *
echo *                                                                         *
echo * Must be called from an NTDDK checked or free build window.  Builds all  *
echo * components checked or free (debug or release) based on the build env.   *
echo *                                                                         *
echo * Parameters:                                                             *
echo *                                                                         *
echo *    %0 ^<options^>                                                       *
echo *             /c                build /c (deletes all object files)       *
echo *             /?                display help                              *
echo *                                                                         *
echo * Associated files: exports and makerelease.cmd.                          *
echo *                                                                         *
echo * exports                                                                 *
echo *    This is a text file containing a list of IDE-based projects which    *
echo *    need a makefile exported.                                            *
echo *                                                                         *
echo * makerelease.cmd                                                         *
echo *    This increments the buildnumber, labels the vss tree and then uses   *
echo *    buildall to build the project.                                       *
echo ***************************************************************************
goto Exit

