windows_build_ceda_projects.bat

@echo off
call:%~1 %~2 %~3 %~4 %~5
goto exit

::-------------------------------------------------------------------------------------------------
:: Build
::
::      SOURCE = source folder containing top level CMakeLists.txt
::      BUILD = build folder
::
::      ENABLE_WIN64 = 0/1
::      ENABLE_WIN32 = 0/1
::      ENABLE_ANDROID = 0/1
::
::      ENABLE_RELEASE = 0/1
::      ENABLE_DEBUG   = 0/1 
::      ENABLE_SHARED  = 0/1 
::      ENABLE_STATIC  = 0/1
::
::		VCVARS32=%ProgramFiles(x86)%\Microsoft Visual Studio\2017\Community\VC\Auxiliary\Build\vcvars32.bat
::	    VCVARS64=%ProgramFiles(x86)%\Microsoft Visual Studio\2017\Community\VC\Auxiliary\Build\vcvars64.bat
::      VCPKG_ROOT_DIR = vcpkg folder
::      WINDOWS_COMPILER = cl.exe or C:/Program Files/LLVM/bin/clang-cl.exe
::
::      NDK=%LOCALAPPDATA%\Android\Sdk\ndk-bundle

:Build
	ECHO SOURCE=%SOURCE%

	::-------------------------------------------------------------------------------------------------
	:: Win64

	IF %ENABLE_WIN64% EQU 1 (
		ECHO.
		CALL "%VCVARS64%"
		:: Fails because we haven't installed libraries using the custom x64-windows-static-boost triplet yet
		::CALL:BuildForWindows x64-windows-static-boost Win64 RelWithDebInfo shared
		:: Fails because under x64-windows-static we have static linking to CRT but under shared we have dynamic linking to CRT
		:: and it's not allowable to mix these.
		:: CALL:BuildForWindows x64-windows-static Win64 RelWithDebInfo shared
		CALL:BuildForWindows x64-windows Windows-x64 shared RelWithDebInfo
		IF errorlevel 1 GOTO:OnFailure
		CALL:BuildForWindows x64-windows Windows-x64 shared Debug
		IF errorlevel 1 GOTO:OnFailure
		CALL:BuildForWindows x64-windows-static Windows-x64 static RelWithDebInfo
		IF errorlevel 1 GOTO:OnFailure
		CALL:BuildForWindows x64-windows-static Windows-x64 static Debug
		IF errorlevel 1 GOTO:OnFailure
	)

	::-------------------------------------------------------------------------------------------------
	:: Win32

	IF %ENABLE_WIN32% EQU 1 (
		ECHO.
		CALL "%VCVARS32%"
		CALL:BuildForWindows x86-windows Windows-x86 shared RelWithDebInfo
		IF errorlevel 1 GOTO:OnFailure
		CALL:BuildForWindows x86-windows Windows-x86 shared Debug
		IF errorlevel 1 GOTO:OnFailure
		CALL:BuildForWindows x86-windows-static  Windows-x86 static RelWithDebInfo
		IF errorlevel 1 GOTO:OnFailure
		CALL:BuildForWindows x86-windows-static  Windows-x86 static Debug
		IF errorlevel 1 GOTO:OnFailure
	)

	::-------------------------------------------------------------------------------------------------
	:: Android

	IF %ENABLE_ANDROID% EQU 1 (
		ECHO.
		CALL "%VCVARS64%"
		CALL:BuildForAndroid arm64-v8a Android-aarch64 shared RelWithDebInfo
		IF errorlevel 1 GOTO:OnFailure
		CALL:BuildForAndroid arm64-v8a Android-aarch64 shared Debug
		IF errorlevel 1 GOTO:OnFailure
		CALL:BuildForAndroid arm64-v8a Android-aarch64 static RelWithDebInfo
		IF errorlevel 1 GOTO:OnFailure
		CALL:BuildForAndroid arm64-v8a Android-aarch64 static Debug
		IF errorlevel 1 GOTO:OnFailure
	)
GOTO:EOF


::-------------------------------------------------------------------------------------------------
:: BuildForWindows %1=triplet %2=platform %3=libtype %4=config
::
::      triplet = x64-windows / x86-windows
::      platform = Windows-x64 / Windows-x86 
::		libtype = shared / static
::		config = RelWithDebInfo / Debug
::
::      SOURCE = source folder containing top level CMakeLists.txt
::      BUILD = build folder
::      ENABLE_RELEASE = 0/1
::      ENABLE_DEBUG   = 0/1 
::      ENABLE_SHARED  = 0/1 
::      ENABLE_STATIC  = 0/1
::      VCPKG_ROOT_DIR = vcpkg folder
::      WINDOWS_COMPILER = cl.exe or C:/Program Files/LLVM/bin/clang-cl.exe

::-------------------------------------------------------------------------------------------------

:BuildForWindows
	SETLOCAL
	SET TRIPLET=%1
	SET PLATFORM=%2
	SET LIBTYPE=%3
	SET CONFIG=%4

	SET ENABLE_BUILD=1
	IF %ENABLE_RELEASE% EQU 0 (
		IF %CONFIG% EQU RelWithDebInfo SET ENABLE_BUILD=0
	)
	IF %ENABLE_DEBUG% EQU 0 (
		IF %CONFIG% EQU Debug SET ENABLE_BUILD=0
	)
	IF %ENABLE_SHARED% EQU 0 (
		IF %LIBTYPE% EQU shared SET ENABLE_BUILD=0
	)
	IF %ENABLE_STATIC% EQU 0 (
		IF %LIBTYPE% EQU static SET ENABLE_BUILD=0
	)

	SET SHARED_LIBS=OFF
	IF %LIBTYPE% EQU shared SET SHARED_LIBS=ON

	SET LIBTYPE2=-static
	IF %LIBTYPE% EQU shared SET LIBTYPE2=

	SET FOLDER=%PLATFORM%%LIBTYPE2%-%CONFIG%

	IF %ENABLE_BUILD% EQU 1 (
		ECHO.
		ECHO.
		ECHO -------------------------------------------- Build %FOLDER% --------------------------------------------
		MKDIR "%BUILD%\%FOLDER%"
		CD "%BUILD%\%FOLDER%"
		cmake -G "Ninja" ^
			-DBUILD_SHARED_LIBS=%SHARED_LIBS% ^
			-DCMAKE_INSTALL_PREFIX:PATH="install" ^
			-DCMAKE_TOOLCHAIN_FILE="%VCPKG_ROOT_DIR%/scripts/buildsystems/vcpkg.cmake" ^
			-DVCPKG_TARGET_TRIPLET=%TRIPLET% ^
			-DCMAKE_WINDOWS_EXPORT_ALL_SYMBOLS="TRUE" ^
			-DCMAKE_CXX_COMPILER=%WINDOWS_COMPILER% ^
			-DCMAKE_C_COMPILER=%WINDOWS_COMPILER% ^
			-DCMAKE_BUILD_TYPE="%CONFIG%" ^
			-DCMAKE_MAKE_PROGRAM=ninja ^
			--config %CONFIG% ^
			"%SOURCE%"
		IF errorlevel 1 GOTO:EOF
		cmake --build . --target install --config %CONFIG%
	)

	ENDLOCAL
GOTO:EOF

::-------------------------------------------------------------------------------------------------
:: BuildForAndroid %1=abi %2=platform %3=libtype %4=config 
::
::		abi = arm64-v8a / armeabi-v7a / x86 / x86_64
::		platform = Android-aarch64
::		libtype = shared / static
::		config = RelWithDebInfo / Debug
::
::      CEDA_SDK_INSTALL_DIR = %ProgramFiles%/Ceda
::      SOURCE = source folder containing top level CMakeLists.txt
::      BUILD = build folder
::      ENABLE_RELEASE = 0/1
::      ENABLE_DEBUG   = 0/1 
::      ENABLE_SHARED  = 0/1 
::      ENABLE_STATIC  = 0/1
::		NDK = %LOCALAPPDATA%\Android\Sdk\ndk-bundle
::
:: See https://developer.android.com/ndk/guides/cmake
::-------------------------------------------------------------------------------------------------

:BuildForAndroid
	SETLOCAL
	SET ABI=%1
	SET PLATFORM=%2
	SET LIBTYPE=%3
	SET CONFIG=%4

	SET ENABLE_BUILD=1
	IF %ENABLE_RELEASE% EQU 0 (
		IF %CONFIG% EQU RelWithDebInfo SET ENABLE_BUILD=0
	)
	IF %ENABLE_DEBUG% EQU 0 (
		IF %CONFIG% EQU Debug SET ENABLE_BUILD=0
	)
	IF %ENABLE_SHARED% EQU 0 (
		IF %LIBTYPE% EQU shared SET ENABLE_BUILD=0
	)
	IF %ENABLE_STATIC% EQU 0 (
		IF %LIBTYPE% EQU static SET ENABLE_BUILD=0
	)

	SET SHARED_LIBS=OFF
	IF %LIBTYPE% EQU shared SET SHARED_LIBS=ON

	SET LIBTYPE2=-static
	IF %LIBTYPE% EQU shared SET LIBTYPE2=

	SET FOLDER=%PLATFORM%%LIBTYPE2%-%CONFIG%

	IF %ENABLE_BUILD% EQU 1 (
		ECHO.
		ECHO.
		ECHO -------------------------------------------- Build %FOLDER% --------------------------------------------
		MKDIR "%BUILD%\%FOLDER%"
		CD "%BUILD%\%FOLDER%"
		cmake -G "Ninja" ^
			-DCeda_DIR="%CEDA_SDK_INSTALL_DIR%/cmake" ^
			-DCEDA_CUSTOM_BUILD_ANDROID=ON ^
			-DBOOST_INCLUDEDIR="C:/cedanet/boost-android/install/%ABI%/include" ^
			-DBOOST_LIBRARYDIR="C:/cedanet/boost-android/install/%ABI%/lib" ^
			-DBUILD_SHARED_LIBS=%SHARED_LIBS% ^
			-DCMAKE_INSTALL_PREFIX:PATH="install" ^
			-DANDROID_ABI=%ABI% ^
			-DANDROID_NDK=%NDK% ^
			-DCMAKE_BUILD_TYPE=%CONFIG% ^
			-DCMAKE_MAKE_PROGRAM=ninja ^
			-DCMAKE_TOOLCHAIN_FILE="%NDK%/build/cmake/android.toolchain.cmake" ^
			-DANDROID_NATIVE_API_LEVEL="21" ^
			-DANDROID_TOOLCHAIN="clang" ^
			-DCMAKE_CXX_FLAGS=-fPIC -frtti -fexceptions -std=c++17 ^
			--config %CONFIG% ^
			"%SOURCE%"
		IF errorlevel 1 GOTO:EOF
		cmake --build . --target install --config %CONFIG%
	)

	ENDLOCAL
GOTO:EOF

::-------------------------------------------------------------------------------------------------
:OnFailure
	ECHO.
	ECHO ERROR. Terminating batch file. errorlevel=%errorlevel%
GOTO:EOF

::-------------------------------------------------------------------------------------------------
:exit
exit /b