#
# Build GoogleTest
#

SET(GW_DEPS_ROOT $ENV{GW_DEPS_ROOT})

FIND_PACKAGE(GoogleTest REQUIRED)

GET_FILENAME_COMPONENT(GOOGLETEST_ROOT_DIR ${GOOGLETEST_SRC_DIR} DIRECTORY)

IF(TARGET_BUILD_PLATFORM STREQUAL "Windows")

	SET(GOOGLETEST_PLATFORM_INCLUDES
	)
	
	SET(GOOGLETEST_COMPILE_DEFS
		# Common to all configurations
		
		_LIB;WIN32;WIN64;_CRT_SECURE_NO_DEPRECATE;
		
		$<$<CONFIG:debug>:_DEBUG;>
		$<$<CONFIG:checked>:NDEBUG;>
		$<$<CONFIG:profile>:NDEBUG;>
		$<$<CONFIG:release>:NDEBUG;>
	)
	
ELSEIF(TARGET_BUILD_PLATFORM STREQUAL "PS4")
	
	SET(GOOGLETEST_PLATFORM_INCLUDES
		$ENV{SCE_ORBIS_SDK_DIR}/target/include
	)

	# Use generator expressions to set config specific preprocessor definitions
	SET(GOOGLETEST_COMPILE_DEFS 

		# Common to all configurations
		${PHYSX_PS4_COMPILE_DEFS};

		$<$<CONFIG:debug>:${PHYSX_PS4_DEBUG_COMPILE_DEFS};>
		$<$<CONFIG:checked>:${PHYSX_PS4_CHECKED_COMPILE_DEFS};>
		$<$<CONFIG:profile>:${PHYSX_PS4_PROFILE_COMPILE_DEFS};>
		$<$<CONFIG:release>:${PHYSX_PS4_RELEASE_COMPILE_DEFS};>
	)	

ELSEIF(TARGET_BUILD_PLATFORM STREQUAL "XboxOne")
	SET(GOOGLETEST_PLATFORM_INCLUDES
	)
	
	SET(GOOGLETEST_COMPILE_DEFS
		# Common to all configurations
		
		_CRT_SECURE_NO_DEPRECATE;_VARIADIC_MAX=10;
		
		
		
		$<$<CONFIG:debug>:_ITERATOR_DEBUG_LEVEL=2; _SECURE_SCL=1>
		$<$<NOT:$<CONFIG:debug>>:_ITERATOR_DEBUG_LEVEL=0; _SECURE_SCL=0>
		
		$<$<CONFIG:debug>:_DEBUG;>
		$<$<CONFIG:checked>:NDEBUG;>
		$<$<CONFIG:profile>:NDEBUG;>
		$<$<CONFIG:release>:NDEBUG;>
	)
	
	FIND_PATH(		XBONE_MODULE_PATH xboxone/Microsoft.Cpp.Durango.user.props
				HINTS 
				${CMAKE_MODULE_PATH})
	MESSAGE("XBONE_MODULE_PATH=${XBONE_MODULE_PATH}")
	FILE(GENERATE OUTPUT "./GoogleTest.vcxproj.user" INPUT  "${XBONE_MODULE_PATH}/xboxone/Microsoft.Cpp.Durango.user.props" CONDITION  1)
	
	SET(CMAKE_CXX_FLAGS "/GS /W3 /Zc:wchar_t /Zi /Gm- /Od /fp:precise /WX- /Zc:forScope /Gd /EHsc /ZW")
	SET(CMAKE_CXX_FLAGS_DEBUG   "/MTd")
	SET(CMAKE_CXX_FLAGS_CHECKED "/MT" )
	SET(CMAKE_CXX_FLAGS_PROFILE "/MT" )
	SET(CMAKE_CXX_FLAGS_RELEASE "/MT" )


ELSEIF(TARGET_BUILD_PLATFORM STREQUAL "Linux")
	
	SET(GOOGLETEST_PLATFORM_INCLUDES
	)
	
	SET(GOOGLETEST_COMPILE_DEFS
		# Common to all configurations
	)	
	
	if(${CMAKE_BUILD_TYPE_LOWERCASE} STREQUAL "debug")
		LIST(APPEND GOOGLETEST_COMPILE_DEFS
			_DEBUG;
		)
	elseif(${CMAKE_BUILD_TYPE_LOWERCASE} STREQUAL "checked")
		LIST(APPEND GOOGLETEST_COMPILE_DEFS
			NDEBUG;
		)
	elseif(${CMAKE_BUILD_TYPE_LOWERCASE} STREQUAL "profile")
		LIST(APPEND GOOGLETEST_COMPILE_DEFS
			NDEBUG;
		)
	elseif(${CMAKE_BUILD_TYPE_LOWERCASE} STREQUAL "release")
		LIST(APPEND GOOGLETEST_COMPILE_DEFS
			NDEBUG;
		)
	else(${CMAKE_BUILD_TYPE_LOWERCASE} STREQUAL "debug")
		MESSAGE(FATAL_ERROR "Unknown configuration ${CMAKE_BUILD_TYPE}")
	endif(${CMAKE_BUILD_TYPE_LOWERCASE} STREQUAL "debug")	
	
ELSEIF(TARGET_BUILD_PLATFORM STREQUAL "Android")
	
	SET(PXTOOLKIT_PLATFORM_INCLUDES
	)
	
	SET(PXTOOLKIT_COMPILE_DEFS
		# Common to all configurations
		GLES2;ANDROID;
	)	
	
	if(${CMAKE_BUILD_TYPE_LOWERCASE} STREQUAL "debug")
		LIST(APPEND PXTOOLKIT_COMPILE_DEFS
			${PHYSX_ANDROID_DEBUG_COMPILE_DEFS};
		)
	elseif(${CMAKE_BUILD_TYPE_LOWERCASE} STREQUAL "checked")
		LIST(APPEND PXTOOLKIT_COMPILE_DEFS
			${PHYSX_ANDROID_CHECKED_COMPILE_DEFS};
		)
	elseif(${CMAKE_BUILD_TYPE_LOWERCASE} STREQUAL "profile")
		LIST(APPEND PXTOOLKIT_COMPILE_DEFS
			${PHYSX_ANDROID_PROFILE_COMPILE_DEFS};
		)
	elseif(${CMAKE_BUILD_TYPE_LOWERCASE} STREQUAL "release")
		LIST(APPEND PXTOOLKIT_COMPILE_DEFS
			${PHYSX_ANDROID_RELEASE_COMPILE_DEFS};
		)
	else(${CMAKE_BUILD_TYPE_LOWERCASE} STREQUAL "debug")
		MESSAGE(FATAL_ERROR "Unknown configuration ${CMAKE_BUILD_TYPE}")
	endif(${CMAKE_BUILD_TYPE_LOWERCASE} STREQUAL "debug")	
	
ELSEIF(TARGET_BUILD_PLATFORM STREQUAL "Mac")
	SET(GOOGLETEST_PLATFORM_INCLUDES		
	)
	
	SET(GOOGLETEST_COMPILE_DEFS 

		# Common to all configurations
		${PHYSX_MAC_COMPILE_DEFS};GTEST_HAS_RTTI=0;

		$<$<CONFIG:debug>:${PHYSX_MAC_DEBUG_COMPILE_DEFS};>
		$<$<CONFIG:checked>:${PHYSX_MAC_CHECKED_COMPILE_DEFS};>
		$<$<CONFIG:profile>:${PHYSX_MAC_PROFILE_COMPILE_DEFS};>
		$<$<CONFIG:release>:${PHYSX_MAC_RELEASE_COMPILE_DEFS};>
	)
	
	SET( CMAKE_CXX_FLAGS  "${CMAKE_CXX_FLAGS} -Wno-format-nonliteral -Wno-switch-enum" )
	
ELSEIF(TARGET_BUILD_PLATFORM STREQUAL "IOS")
	SET(GOOGLETEST_PLATFORM_INCLUDES		
	)
	
	SET(GOOGLETEST_COMPILE_DEFS 

		# Common to all configurations
		${PHYSX_IOS_COMPILE_DEFS};GTEST_HAS_RTTI=0;

		$<$<CONFIG:debug>:${PHYSX_IOS_DEBUG_COMPILE_DEFS};>
		$<$<CONFIG:checked>:${PHYSX_IOS_CHECKED_COMPILE_DEFS};>
		$<$<CONFIG:profile>:${PHYSX_IOS_PROFILE_COMPILE_DEFS};>
		$<$<CONFIG:release>:${PHYSX_IOS_RELEASE_COMPILE_DEFS};>
	)
ENDIF()


ADD_LIBRARY(GoogleTest STATIC 
	${GOOGLETEST_PLATFORM_SOURCE_FILES}
	
	${GOOGLETEST_SRC_DIR}/gtest.cpp
	${GOOGLETEST_SRC_DIR}/gtest_main.cpp
	${GOOGLETEST_SRC_DIR}/gtest-death-test.cpp
	${GOOGLETEST_SRC_DIR}/gtest-filepath.cpp
	${GOOGLETEST_SRC_DIR}/gtest-port.cpp
	${GOOGLETEST_SRC_DIR}/gtest-test-part.cpp
	${GOOGLETEST_SRC_DIR}/gtest-typed-test.cpp

)

# Target specific compile options


TARGET_INCLUDE_DIRECTORIES(GoogleTest 
	PRIVATE ${GOOGLETEST_PLATFORM_INCLUDES}

	PRIVATE ${GOOGLETEST_ROOT_DIR}
	PRIVATE ${GOOGLETEST_INCLUDE_DIR}
	PRIVATE ${GOOGLETEST_INCLUDE_DIR}/gtest/internal
)



TARGET_COMPILE_DEFINITIONS(GoogleTest 
	PRIVATE ${GOOGLETEST_COMPILE_DEFS}
)

# Do final direct sets after the target has been defined
IF(TARGET_BUILD_PLATFORM STREQUAL "Windows")
																	   
	SET_TARGET_PROPERTIES(GoogleTest PROPERTIES 
		CXX_FLAGS "/GS /W3 /Zc:wchar_t /Zi /Gm- /Od /fp:precise /WX- /Zc:forScope /Gd /EHsc"
		CXX_FLAGS_DEBUG   "${WINCRT_DEBUG}"
		CXX_FLAGS_CHECKED "${WINCRT_NDEBUG}"
		CXX_FLAGS_PROFILE "${WINCRT_NDEBUG}"
		CXX_FLAGS_RELEASE "${WINCRT_NDEBUG}"
		)
ELSEIF(TARGET_BUILD_PLATFORM STREQUAL "Mac")		
	
	SET_TARGET_PROPERTIES(GoogleTest PROPERTIES 
		CXX_FLAGS "-Wno-format-nonliteral -Wno-switch-enum"
		CXX_FLAGS_DEBUG   ""
		CXX_FLAGS_CHECKED ""
		CXX_FLAGS_PROFILE ""
		CXX_FLAGS_RELEASE ""
		)
	
	
ELSEIF(TARGET_BUILD_PLATFORM STREQUAL "PS4")


ELSEIF(TARGET_BUILD_PLATFORM STREQUAL "XboxOne")
	MESSAGE("XboxOne google test flags set here")

	SET_TARGET_PROPERTIES(GoogleTest PROPERTIES 
		CXX_FLAGS "/GS /W3 /Zc:wchar_t /Zi /Gm- /Od /fp:precise /WX- /Zc:forScope /Gd /EHsc /ZW"
		CXX_FLAGS_DEBUG   "/MTd"
		CXX_FLAGS_CHECKED "/MT"
		CXX_FLAGS_PROFILE "/MT"
		CXX_FLAGS_RELEASE "/MT"
		)
		
	target_compile_options(GoogleTest PRIVATE /GS /W3 /Zc:wchar_t /Zi /Gm- /Od /fp:precise /WX- /Zc:forScope /Gd /EHsc /ZW)

ELSEIF(TARGET_BUILD_PLATFORM STREQUAL "Unix")
ENDIF()


