@ECHO OFF

::
:: build.bat --
::
:: Extensible Adaptable Generalized Logic Engine (Eagle)
:: Build Tool
::
:: Copyright (c) 2007-2012 by Joe Mistachkin.  All rights reserved.
::
:: See the file "license.terms" for information on usage and redistribution of
:: this file, and for a DISCLAIMER OF ALL WARRANTIES.
::
:: RCS: @(#) $Id: $
::

SETLOCAL

REM SET __ECHO=ECHO
REM SET __ECHO2=ECHO
IF NOT DEFINED _AECHO (SET _AECHO=REM)
IF NOT DEFINED _CECHO (SET _CECHO=REM)
IF NOT DEFINED _VECHO (SET _VECHO=REM)

%_AECHO% Running %0 %*

IF NOT DEFINED MSBUILD (
  SET MSBUILD=MSBuild.exe
)

%_VECHO% MsBuild = '%MSBUILD%'

IF NOT DEFINED CSC (
  SET CSC=csc.exe
)

%_VECHO% Csc = '%CSC%'

IF DEFINED NETFX20ONLY (
  %_AECHO% Forcing the use of the .NET Framework 2.0...
  SET YEAR=2005
  SET VCPRJEXT=.vcproj
  CALL :fn_CheckFramework v2.0.50727
  GOTO skip_netFxCheck
)

IF DEFINED NETFX35ONLY (
  %_AECHO% Forcing the use of the .NET Framework 3.5...
  SET YEAR=2008
  SET VCPRJEXT=.vcproj
  CALL :fn_CheckFramework v3.5
  GOTO skip_netFxCheck
)

IF DEFINED NETFX40ONLY (
  %_AECHO% Forcing the use of the .NET Framework 4.0...
  SET YEAR=2010
  SET VCPRJEXT=.vcxproj
  CALL :fn_CheckFramework v4.0.30319
  GOTO skip_netFxCheck
)

IF DEFINED NETFX45ONLY (
  %_AECHO% Forcing the use of the .NET Framework 4.5...
  SET YEAR=2012
  SET VCPRJEXT=.vcxproj
  CALL :fn_CheckFramework v4.0.30319
  GOTO skip_netFxCheck
)

IF DEFINED NETFX451ONLY (
  %_AECHO% Forcing the use of the .NET Framework 4.5.1...
  SET YEAR=2013
  SET VCPRJEXT=.vcxproj
  CALL :fn_CheckFramework v4.0.30319
  GOTO skip_netFxCheck
)

REM
REM TODO: When the next version of Visual Studio is released, this section
REM       may need updating.
REM
IF DEFINED NETFX452ONLY (
  %_AECHO% Forcing the use of the .NET Framework 4.5.2...
  SET YEAR=2013
  SET VCPRJEXT=.vcxproj
  CALL :fn_CheckFramework v4.0.30319
  GOTO skip_netFxCheck
)

IF DEFINED FRAMEWORKDIR (
  IF NOT EXIST "%FRAMEWORKDIR%" (
    CALL :fn_UnsetVariable FRAMEWORKDIR
  )
)

IF DEFINED FRAMEWORKDIR (
  IF NOT EXIST "%FRAMEWORKDIR%\%MSBUILD%" (
    CALL :fn_UnsetVariable FRAMEWORKDIR
  )
  IF NOT EXIST "%FRAMEWORKDIR%\%CSC%" (
    CALL :fn_UnsetVariable FRAMEWORKDIR
  )
)

IF NOT DEFINED FRAMEWORKDIR (
  %_AECHO% Checking for the .NET Framework 4.0...
  SET YEAR=2010
  SET VCPRJEXT=.vcxproj
  CALL :fn_CheckFramework v4.0.30319
)

IF NOT EXIST "%FRAMEWORKDIR%" (
  %_AECHO% Checking for the .NET Framework 3.5...
  SET YEAR=2008
  SET VCPRJEXT=.vcproj
  CALL :fn_CheckFramework v3.5
)

IF NOT EXIST "%FRAMEWORKDIR%" (
  %_AECHO% Checking for the .NET Framework 2.0...
  SET YEAR=2005
  SET VCPRJEXT=.vcproj
  CALL :fn_CheckFramework v2.0.50727
)

:skip_netFxCheck

%_VECHO% Year = '%YEAR%'
%_VECHO% VcPrjExt = '%VCPRJEXT%'
%_VECHO% FrameworkDir = '%FRAMEWORKDIR%'

IF NOT DEFINED FRAMEWORKDIR (
  ECHO.
  ECHO The .NET Framework directory "FRAMEWORKDIR" is not defined.
  ECHO.
  ECHO Please install the .NET Framework or set the "FRAMEWORKDIR"
  ECHO environment variable to the location where it is installed.
  ECHO.
  GOTO errors
)

IF NOT EXIST "%FRAMEWORKDIR%" (
  ECHO.
  ECHO The .NET Framework directory "%FRAMEWORKDIR%" was not found.
  ECHO.
  ECHO Please install the .NET Framework or set the "FRAMEWORKDIR"
  ECHO environment variable to the location where it is installed.
  ECHO.
  GOTO errors
)

SET ROOT=%~dp0\..\..
SET ROOT=%ROOT:\\=\%

%_VECHO% Root = '%ROOT%'

SET CONFIGURATION=%1

IF DEFINED CONFIGURATION (
  CALL :fn_UnquoteVariable CONFIGURATION
) ELSE (
  %_AECHO% No configuration specified, using default...
  SET CONFIGURATION=Release
)

%_VECHO% Configuration = '%CONFIGURATION%'

SET BASE_CONFIGURATION=%CONFIGURATION%
SET BASE_CONFIGURATION=%BASE_CONFIGURATION:All=%
SET BASE_CONFIGURATION=%BASE_CONFIGURATION:Dll=%

%_VECHO% BaseConfiguration = '%BASE_CONFIGURATION%'

SET ARGS=%*

%_VECHO% Args = '%ARGS%'

CALL :fn_ResetErrorLevel

%__ECHO2% PUSHD "%ROOT%"

IF ERRORLEVEL 1 (
  ECHO Could not change directory to "%ROOT%".
  GOTO errors
)

SET PATH=%FRAMEWORKDIR%;%PATH%

%_VECHO% Path = '%PATH%'

REM
REM NOTE: Make sure Eagle can find the script library even when being built
REM       with an arbitrary configuration suffix.
REM
SET TCL_LIBRARY=%ROOT:\\=\%/lib/Eagle1.0
SET TCL_LIBRARY=%TCL_LIBRARY:\=/%

%_VECHO% TclLibrary = '%TCL_LIBRARY%'

REM
REM NOTE: Unless we are prevented from doing so, try for the "commercial" and
REM       "enterprise" solution files first.  Failing that, fall back to the
REM       standard solution files.
REM
IF DEFINED SOLUTION GOTO skip_setSolution

IF DEFINED COREONLY (
  %_AECHO% Building core library project only...
  SET SOLUTION=.\Library\Eagle%YEAR%.csproj
  SET CONFIGURATION=%BASE_CONFIGURATION%
  GOTO skip_setSolution
)

IF DEFINED SHELLONLY (
  %_AECHO% Building core library and shell projects only...
  SET SOLUTION=.\EagleCore%YEAR%.sln
  SET CONFIGURATION=%BASE_CONFIGURATION%
  GOTO skip_setSolution
)

IF DEFINED PACKAGEONLY (
  %_AECHO% Building native package only...
  SET SOLUTION=.\Native\Package\Garuda%YEAR%%VCPRJEXT%
  GOTO skip_setSolution
)

IF DEFINED UTILITYONLY (
  %_AECHO% Building native utility library only...
  SET SOLUTION=.\Native\Utility\Spilornis%YEAR%%VCPRJEXT%
  GOTO skip_setSolution
)

IF DEFINED NOCOMMERCIAL (
  %_AECHO% Building non-commercial projects only...
  SET SOLUTION=.\EagleNonCommercial%YEAR%.sln
) ELSE (
  %_AECHO% Building all commercial projects...
  SET SOLUTION=.\EagleCommercial%YEAR%.sln
)

IF DEFINED SOLUTION IF EXIST "%SOLUTION%" GOTO skip_setSolution

IF DEFINED NOENTERPRISE (
  %_AECHO% Building non-enterprise projects only...
  SET SOLUTION=.\EagleNonEnterprise%YEAR%.sln
) ELSE (
  %_AECHO% Building all enterprise projects...
  SET SOLUTION=.\EagleEnterprise%YEAR%.sln
)

IF DEFINED SOLUTION IF EXIST "%SOLUTION%" GOTO skip_setSolution

%_AECHO% Building all core projects...
SET SOLUTION=.\Eagle%YEAR%.sln

%_VECHO% Configuration = '%CONFIGURATION%'

:skip_setSolution

%_VECHO% Solution = '%SOLUTION%'

FOR /F %%E IN ('ECHO %SOLUTION%') DO (SET SOLUTIONEXT=%%~xE)

%_VECHO% SolutionExt = '%SOLUTIONEXT%'

REM
REM NOTE: When building a C++ project, automatically change the "Configuration"
REM       property to be "DebugDll" or "ReleaseDll", if necessary.  This allows
REM       this tool to be called for a C++ project with no build configuration
REM       specified on the command line.  It also allows the build configuration
REM       specified on the command line to be "DebugAll" or "ReleaseAll", which
REM       are the ones used by the solution files when building all the managed
REM       and native code projects.
REM
IF /I "%SOLUTIONEXT%" == ".vcproj" (
  REM
  REM NOTE: Visual C++ 2005/2008 project, allow the build configuration to be
  REM       adjusted, if necessary.
  REM
) ELSE IF /I "%SOLUTIONEXT%" == ".vcxproj" (
  REM
  REM NOTE: Visual C++ 2010/2012 project, allow the build configuration to be
  REM       adjusted, if necessary.
  REM
) ELSE (
  REM
  REM NOTE: Adjusting the configuration should not be necessary.
  REM
  GOTO skip_checkConfiguration
)

IF /I "%CONFIGURATION%" == "Debug" (
  SET CONFIGURATION=DebugDll
) ELSE IF /I "%CONFIGURATION%" == "DebugAll" (
  SET CONFIGURATION=DebugDll
) ELSE IF /I "%CONFIGURATION%" == "Release" (
  SET CONFIGURATION=ReleaseDll
) ELSE IF /I "%CONFIGURATION%" == "ReleaseAll" (
  SET CONFIGURATION=ReleaseDll
)

%_VECHO% Configuration = '%CONFIGURATION%'

:skip_checkConfiguration

IF NOT DEFINED DEFAULT_PLATFORM (
  SET DEFAULT_PLATFORM=Win32
)

%_VECHO% DefaultPlatform = '%DEFAULT_PLATFORM%'

IF DEFINED PLATFORM (
  CALL :fn_UnquoteVariable PLATFORM
) ELSE (
  REM
  REM NOTE: It seems that MSBuild is very picky about the precise value of
  REM       the "Platform" property.  When building a solution file, using
  REM       a value of "Any CPU" is required.  When building a C# project,
  REM       a value of "AnyCPU" is required.  When building a C++ project,
  REM       a value of either "Win32" or "x64" is required.  Other values
  REM       will most likely cause the build to fail.
  REM
  %_AECHO% No platform specified, using default...
  IF /I "%SOLUTIONEXT%" == ".csproj" (
    SET PLATFORM=AnyCPU
  ) ELSE IF /I "%SOLUTIONEXT%" == ".vcproj" (
    SET PLATFORM=%DEFAULT_PLATFORM%
  ) ELSE IF /I "%SOLUTIONEXT%" == ".vcxproj" (
    SET PLATFORM=%DEFAULT_PLATFORM%
  ) ELSE (
    SET PLATFORM=Any CPU
  )
)

%_VECHO% Platform = '%PLATFORM%'

IF NOT DEFINED TARGET (
  SET TARGET=Rebuild
)

%_VECHO% Target = '%TARGET%'

IF NOT DEFINED TEMP (
  ECHO The TEMP environment variable must be set first.
  GOTO errors
)

%_VECHO% Temp = '%TEMP%'

IF NOT DEFINED LOGDIR (
  SET LOGDIR=%TEMP%
)

%_VECHO% LogDir = '%LOGDIR%'

IF NOT DEFINED LOGPREFIX (
  SET LOGPREFIX=EagleBuild
)

%_VECHO% LogPrefix = '%LOGPREFIX%'

IF NOT DEFINED LOGSUFFIX (
  SET LOGSUFFIX=Unknown
)

%_VECHO% LogSuffix = '%LOGSUFFIX%'

IF DEFINED LOGGING GOTO skip_setLogging
IF DEFINED NOLOG GOTO skip_setLogging

SET LOGGING="/logger:FileLogger,Microsoft.Build.Engine;Logfile=%LOGDIR%\%LOGPREFIX%%CONFIGURATION%%LOGSUFFIX%.log;Verbosity=diagnostic"

:skip_setLogging

%_VECHO% Logging = '%LOGGING%'

%_CECHO% "%MSBUILD%" "%SOLUTION%" "/target:%TARGET%" "/property:Configuration=%CONFIGURATION%" "/property:Platform=%PLATFORM%" %LOGGING% /property:BuildType=%ARGS%
%__ECHO% "%MSBUILD%" "%SOLUTION%" "/target:%TARGET%" "/property:Configuration=%CONFIGURATION%" "/property:Platform=%PLATFORM%" %LOGGING% /property:BuildType=%ARGS%

IF ERRORLEVEL 1 (
  ECHO Build failed.
  GOTO errors
)

%__ECHO2% POPD

IF ERRORLEVEL 1 (
  ECHO Could not restore directory.
  GOTO errors
)

GOTO no_errors

:fn_CheckFramework
  SET FRAMEWORKVER=%1
  IF NOT DEFINED FRAMEWORKVER GOTO :EOF
  IF DEFINED NOMSBUILD64 (
    %_AECHO% Forced into using 32-bit version of MSBuild...
    SET FRAMEWORKDIR=%windir%\Microsoft.NET\Framework\%FRAMEWORKVER%
    GOTO :EOF
  )
  IF NOT "%PROCESSOR_ARCHITECTURE%" == "x86" (
    %_AECHO% The operating system appears to be 64-bit.
    IF EXIST "%windir%\Microsoft.NET\Framework64\%FRAMEWORKVER%" (
      IF EXIST "%windir%\Microsoft.NET\Framework64\%FRAMEWORKVER%\%MSBUILD%" (
        IF EXIST "%windir%\Microsoft.NET\Framework64\%FRAMEWORKVER%\%CSC%" (
          %_AECHO% Using 64-bit version of MSBuild...
          SET FRAMEWORKDIR=%windir%\Microsoft.NET\Framework64\%FRAMEWORKVER%
          GOTO :EOF
        ) ELSE (
          %_AECHO% Missing 64-bit version of "%CSC%".
        )
      ) ELSE (
        %_AECHO% Missing 64-bit version of "%MSBUILD%".
      )
    ) ELSE (
      %_AECHO% Missing 64-bit version of .NET Framework "%FRAMEWORKVER%".
    )
  ) ELSE (
    %_AECHO% The operating system appears to be 32-bit.
  )
  %_AECHO% Using 32-bit version of MSBuild...
  SET FRAMEWORKDIR=%windir%\Microsoft.NET\Framework\%FRAMEWORKVER%
  GOTO :EOF

:fn_UnquoteVariable
  IF NOT DEFINED %1 GOTO :EOF
  SETLOCAL
  SET __ECHO_CMD=ECHO %%%1%%
  FOR /F "delims=" %%V IN ('%__ECHO_CMD%') DO (
    SET VALUE=%%V
  )
  SET VALUE=%VALUE:"=%
  REM "
  ENDLOCAL && SET %1=%VALUE%
  GOTO :EOF

:fn_UnsetVariable
  IF NOT "%1" == "" (
    SET %1=
    CALL :fn_ResetErrorLevel
  )
  GOTO :EOF

:fn_ResetErrorLevel
  VERIFY > NUL
  GOTO :EOF

:fn_SetErrorLevel
  VERIFY MAYBE 2> NUL
  GOTO :EOF

:usage
  ECHO.
  ECHO Usage: %~nx0 [configuration] [...]
  GOTO errors

:errors
  CALL :fn_SetErrorLevel
  ENDLOCAL
  ECHO.
  ECHO Build failure, errors were encountered.
  GOTO end_of_file

:no_errors
  CALL :fn_ResetErrorLevel
  ENDLOCAL
  ECHO.
  ECHO Build success, no errors were encountered.
  GOTO end_of_file

:end_of_file
%__ECHO% EXIT /B %ERRORLEVEL%
