
macro(DEFINE_DEFAULT_DEFINITIONS)
  if(WIN32)
		add_definitions(
			-DNOMINMAX # do not define min() and max()
			-D_CRT_SECURE_NO_WARNINGS
			-D_CRT_NONSTDC_NO_WARNINGS
			-D__STDC_CONSTANT_MACROS
			-DWIN32_LEAN_AND_MEAN # remove obsolete things from windows headers
			-DUNICODE -D_UNICODE
		)
	endif(WIN32)

	add_definitions(
		-DBRAND_FULLNAME="${BRAND_FULLNAME}"
		-DBRAND_COMPANYNAME="${BRAND_COMPANYNAME}"

		-DBRAND_FULLNAME_W=L"${BRAND_FULLNAME}"
		-DBRAND_COMPANYNAME_W=L"${BRAND_COMPANYNAME}"
	)

	if(WIN32)
		set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -Zi /std:c++17")
		add_definitions(-D_WIN32_WINNT=0x0601) # Windows 7
	endif(WIN32)


	if(OFF)
	# support WindowsXP
	if(MSVC OR MSVC_IDE)
		if(MSVC_VERSION EQUAL 1700)  # VC11/VS2012
			add_definitions(-D_USING_V110_SDK71_)
			set(CMAKE_GENERATOR_TOOLSET "v110_xp" CACHE STRING "Platform Toolset" FORCE)
		elseif(MSVC_VERSION EQUAL 1800) # VC12/VS2013
			add_definitions(-D_USING_V120_SDK71_)
			set(CMAKE_GENERATOR_TOOLSET "v120_xp" CACHE STRING "Platform Toolset" FORCE)
		elseif(MSVC_VERSION EQUAL 1900) # VC14/VS2015
			add_definitions(-D_USING_V140_SDK71_)
			set(CMAKE_GENERATOR_TOOLSET "v140_xp" CACHE STRING "Platform Toolset" FORCE)
		elseif(MSVC_VERSION EQUAL 1911) # VC14.1/VS2017
			add_definitions(-D_USING_V141_SDK71_)
			set(CMAKE_GENERATOR_TOOLSET "v141_xp" CACHE STRING "Platform Toolset" FORCE)
		endif()
	endif(MSVC OR MSVC_IDE)
	endif(OFF)
endmacro(DEFINE_DEFAULT_DEFINITIONS)

macro(SETUP_COMPILER_SETTINGS IS_DYNAMIC)
	set(IS_DYNAMIC ${IS_DYNAMIC})

	string(REPLACE ";" " " cmake_cl_release_init_str "${ADDITIONAL_CL_OPTIMIZATION_OPTIONS} /D NDEBUG /EHsc /W4 /Ox /Ot /GL /GF /Gy /Qpar /GR /FR ")
#	string(REPLACE ";" " " cmake_linker_release_init_str "${ADDITIONAL_LINKER_OPTIMIZATION_OPTIONS} /opt:ref /OPT:ICF /DEBUG")
	string(REPLACE ";" " " cmake_linker_release_init_str "${ADDITIONAL_LINKER_OPTIMIZATION_OPTIONS} /opt:ref /DEBUG /INCREMENTAL:NO /LTCG")
	string(REPLACE ";" " " cmake_librarian_release_init_str "${ADDITIONAL_LINKER_OPTIMIZATION_OPTIONS} /LTCG")

	if(IS_DYNAMIC)
		if(WIN32)
			set(CMAKE_CXX_FLAGS_DEBUG "/MDd /D_DEBUG /Zi /Ob0 /Od /RTC1")
			set(CMAKE_CXX_FLAGS_RELEASE "/MD /O2 ${cmake_cl_release_init_str}")
			set(CMAKE_CXX_FLAGS_MINSIZEREL "/MD /O1 ${cmake_cl_release_init_str}")
			set(CMAKE_CXX_FLAGS_RELWITHDEBINFO "/MD /O2 /Zi ${cmake_cl_release_init_str}")

			set(CMAKE_C_FLAGS "/DWIN32 /D_WINDOWS /W3")
			set(CMAKE_C_FLAGS_DEBUG "/MDd /Zi /Ob0 /Od /RTC1")
			set(CMAKE_C_FLAGS_RELEASE "/MD /O2 /Ob2 /DNDEBUG")
			set(CMAKE_C_FLAGS_MINSIZEREL "/MD /O1 /Ob1 /DNDEBUG")
			set(CMAKE_C_FLAGS_RELWITHDEBINFO "/MD /Zi /O2 /Ob1 /DNDEBUG")

			set(CMAKE_EXE_LINKER_FLAGS_RELEASE "${cmake_linker_release_init_str}")
			set(CMAKE_EXE_LINKER_FLAGS_MINSIZEREL "${cmake_linker_release_init_str}")
			set(CMAKE_EXE_LINKER_FLAGS_RELWITHDEBINFO "${cmake_linker_release_init_str}")

			set(CMAKE_STATIC_LINKER_FLAGS_RELEASE "${cmake_librarian_release_init_str}")
			set(CMAKE_STATIC_LINKER_FLAGS_MINSIZEREL "${cmake_librarian_release_init_str}")
			set(CMAKE_STATIC_LINKER_FLAGS_RELWITHDEBINFO "${cmake_librarian_release_init_str}")
		endif(WIN32)

		message(STATUS "Using setting for DYNAMIC run-time")

	else(IS_DYNAMIC)
		if(WIN32)
			set(CMAKE_CXX_FLAGS_DEBUG "/MTd /D_DEBUG /Zi /Ob0 /Od /RTC1")
			set(CMAKE_CXX_FLAGS_RELEASE "/MT /O2 ${cmake_cl_release_init_str}")
			set(CMAKE_CXX_FLAGS_MINSIZEREL "/MT /O1 ${cmake_cl_release_init_str}")
			set(CMAKE_CXX_FLAGS_RELWITHDEBINFO "/MT /O2 /Zi ${cmake_cl_release_init_str}")

			set(CMAKE_C_FLAGS "/DWIN32 /D_WINDOWS /W3")
			set(CMAKE_C_FLAGS_DEBUG "/MTd /Zi /Ob0 /Od /RTC1")
			set(CMAKE_C_FLAGS_RELEASE "/MT /O2 /Ob2 /DNDEBUG")
			set(CMAKE_C_FLAGS_MINSIZEREL "/MT /O1 /Ob1 /DNDEBUG")
			set(CMAKE_C_FLAGS_RELWITHDEBINFO "/MT /Zi /O2 /Ob1 /DNDEBUG")

			set(CMAKE_EXE_LINKER_FLAGS_RELEASE "${cmake_linker_release_init_str}")
			set(CMAKE_EXE_LINKER_FLAGS_MINSIZEREL "${cmake_linker_release_init_str}")
			set(CMAKE_EXE_LINKER_FLAGS_RELWITHDEBINFO "${cmake_linker_release_init_str}")

			set(CMAKE_STATIC_LINKER_FLAGS_RELEASE "${cmake_librarian_release_init_str}")
			set(CMAKE_STATIC_LINKER_FLAGS_MINSIZEREL "${cmake_librarian_release_init_str}")
			set(CMAKE_STATIC_LINKER_FLAGS_RELWITHDEBINFO "${cmake_librarian_release_init_str}")
		endif(WIN32)

		message(STATUS "Using setting for STATIC run-time")
	endif(IS_DYNAMIC)

	if(APPLE)
		#set(LIBCXX_DIR	${CMAKE_CURRENT_SOURCE_DIR}/imports/libc++10.7)
		#set(CMAKE_OSX_ARCHITECTURES i386)
		#set(CMAKE_OSX_ARCHITECTURES_DEBUG i368)
		#set(CMAKE_CXX_FLAGS "-arch i386")
		#set(LINK_FLAGS "-arch i386")
		#set(CMAKE_XCODE_ATTRIBUTE_GCC_VERSION "com.apple.compilers.llvm.clang.1_0")
		#set(CMAKE_XCODE_ATTRIBUTE_CLANG_CXX_LANGUAGE_STANDARD "c++11")
		#set(CMAKE_XCODE_ATTRIBUTE_CLANG_CXX_LIBRARY "libc++")
		#set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -std=c++11 -stdlib=libc++ -nostdinc++ -I${LIBCXX_DIR}/include -g -Wall")
		#set(LINK_FLAGS "${LINK_FLAGS} -L${LIBCXX_DIR}/lib -arch i386")

		# Detection target arch automaticly
		set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -std=c++11 -stdlib=libc++ -g -Wall")
		set(CMAKE_XCODE_ATTRIBUTE_GCC_VERSION "com.apple.compilers.llvm.clang.1_0")
		set(CMAKE_XCODE_ATTRIBUTE_CLANG_CXX_LANGUAGE_STANDARD "c++11")
		set(CMAKE_XCODE_ATTRIBUTE_CLANG_CXX_LIBRARY "libc++")
	elseif(UNIX)
		set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -std=c++17 -lstdc++fs -Wall")
	endif()

	if(MSVC)
		set(DEVELOPER_NUM_BUILD_PROCS 3 CACHE STRING "Parameter for /MP option")
		if(${DEVELOPER_NUM_BUILD_PROCS} GREATER 1)
			string(REGEX REPLACE "/MP([0-9]+)" "" CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS}")
			set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} /MP${DEVELOPER_NUM_BUILD_PROCS}")
		endif(${DEVELOPER_NUM_BUILD_PROCS} GREATER 1)
	endif(MSVC)
endmacro(SETUP_COMPILER_SETTINGS IS_DYNAMIC)


macro(INSTALL_RUNTIME_LIBRARIES)
	# Install CRT
	if(WIN32)
		set(CMAKE_INSTALL_SYSTEM_RUNTIME_DESTINATION .)
		include (InstallRequiredSystemLibraries)
	endif(WIN32)

# TODO: add code for other platforms here

endmacro(INSTALL_RUNTIME_LIBRARIES)


macro(INSTALL_DEBUG_INFO_FILE)

if(WIN32)
	#install pbd files
	foreach(buildCfg ${CMAKE_CONFIGURATION_TYPES})
		install(
			FILES ${CMAKE_CURRENT_BINARY_DIR}/${buildCfg}/${PROJECT_NAME}.pdb
			DESTINATION .
			CONFIGURATIONS ${buildCfg})
	endforeach(buildCfg ${CMAKE_CONFIGURATION_TYPES})
endif(WIN32)
# TODO: add code for other platforms here

endmacro(INSTALL_DEBUG_INFO_FILE)

macro(PRINT_ALL_CMAKE_SETTINGS)
	message(STATUS "CMAKE_CONFIGURATION_TYPES: " ${CMAKE_CONFIGURATION_TYPES})
	message(STATUS "CMAKE_CXX_FLAGS: " ${CMAKE_CXX_FLAGS})
	message(STATUS "CMAKE_CXX_FLAGS_DEBUG: " ${CMAKE_CXX_FLAGS_DEBUG})
	message(STATUS "CMAKE_CXX_FLAGS_MINSIZEREL: " ${CMAKE_CXX_FLAGS_MINSIZEREL})
	message(STATUS "CMAKE_CXX_FLAGS_RELEASE: " ${CMAKE_CXX_FLAGS_RELEASE})
	message(STATUS "CMAKE_CXX_FLAGS_RELWITHDEBINFO: " ${CMAKE_CXX_FLAGS_RELWITHDEBINFO})
	message(STATUS "CMAKE_CXX_STANDARD_LIBRARIES: " ${CMAKE_CXX_STANDARD_LIBRARIES})
	message(STATUS "CMAKE_C_FLAGS: " ${CMAKE_C_FLAGS})
	message(STATUS "CMAKE_C_FLAGS_DEBUG: " ${CMAKE_C_FLAGS_DEBUG})
	message(STATUS "CMAKE_C_FLAGS_MINSIZEREL: " ${CMAKE_C_FLAGS_MINSIZEREL})
	message(STATUS "CMAKE_C_FLAGS_RELEASE: " ${CMAKE_C_FLAGS_RELEASE})
	message(STATUS "CMAKE_C_FLAGS_RELWITHDEBINFO: " ${CMAKE_C_FLAGS_RELWITHDEBINFO})
	message(STATUS "CMAKE_C_STANDARD_LIBRARIES: " ${CMAKE_C_STANDARD_LIBRARIES})
	message(STATUS "CMAKE_EXE_LINKER_FLAGS: " ${CMAKE_EXE_LINKER_FLAGS})
	message(STATUS "CMAKE_EXE_LINKER_FLAGS_DEBUG: " ${CMAKE_EXE_LINKER_FLAGS_DEBUG})
	message(STATUS "CMAKE_EXE_LINKER_FLAGS_MINSIZEREL: " ${CMAKE_EXE_LINKER_FLAGS_MINSIZEREL})
	message(STATUS "CMAKE_EXE_LINKER_FLAGS_RELEASE: " ${CMAKE_EXE_LINKER_FLAGS_RELEASE})
	message(STATUS "CMAKE_EXE_LINKER_FLAGS_RELWITHDEBINFO: " ${CMAKE_EXE_LINKER_FLAGS_RELWITHDEBINFO})
	message(STATUS "CMAKE_INSTALL_PREFIX: " ${CMAKE_INSTALL_PREFIX})
	message(STATUS "CMAKE_LINKER: " ${CMAKE_LINKER})
	message(STATUS "CMAKE_MODULE_LINKER_FLAGS: " ${CMAKE_MODULE_LINKER_FLAGS})
	message(STATUS "CMAKE_MODULE_LINKER_FLAGS_DEBUG: " ${CMAKE_MODULE_LINKER_FLAGS_DEBUG})
	message(STATUS "CMAKE_MODULE_LINKER_FLAGS_MINSIZEREL: " ${CMAKE_MODULE_LINKER_FLAGS_MINSIZEREL})
	message(STATUS "CMAKE_MODULE_LINKER_FLAGS_RELEASE: " ${CMAKE_MODULE_LINKER_FLAGS_RELEASE})
	message(STATUS "CMAKE_MODULE_LINKER_FLAGS_RELWITHDEBINFO: " ${CMAKE_MODULE_LINKER_FLAGS_RELWITHDEBINFO})
	message(STATUS "CMAKE_RC_COMPILER: " ${CMAKE_RC_COMPILER})
	message(STATUS "CMAKE_RC_FLAGS: " ${CMAKE_RC_FLAGS})
	message(STATUS "CMAKE_RC_FLAGS_DEBUG: " ${CMAKE_RC_FLAGS_DEBUG})
	message(STATUS "CMAKE_RC_FLAGS_MINSIZEREL: " ${CMAKE_RC_FLAGS_MINSIZEREL})
	message(STATUS "CMAKE_RC_FLAGS_RELEASE: " ${CMAKE_RC_FLAGS_RELEASE})
	message(STATUS "CMAKE_RC_FLAGS_RELWITHDEBINFO: " ${CMAKE_RC_FLAGS_RELWITHDEBINFO})
	message(STATUS "CMAKE_SHARED_LINKER_FLAGS: " ${CMAKE_SHARED_LINKER_FLAGS})
	message(STATUS "CMAKE_SHARED_LINKER_FLAGS_DEBUG: " ${CMAKE_SHARED_LINKER_FLAGS_DEBUG})
	message(STATUS "CMAKE_SHARED_LINKER_FLAGS_MINSIZEREL: " ${CMAKE_SHARED_LINKER_FLAGS_MINSIZEREL})
	message(STATUS "CMAKE_SHARED_LINKER_FLAGS_RELEASE: " ${CMAKE_SHARED_LINKER_FLAGS_RELEASE})
	message(STATUS "CMAKE_SHARED_LINKER_FLAGS_RELWITHDEBINFO: " ${CMAKE_SHARED_LINKER_FLAGS_RELWITHDEBINFO})
	message(STATUS "CMAKE_SKIP_INSTALL_RPATH: " ${CMAKE_SKIP_INSTALL_RPATH})
	message(STATUS "CMAKE_SKIP_RPATH: " ${CMAKE_SKIP_RPATH})
	message(STATUS "CMAKE_STATIC_LINKER_FLAGS: " ${CMAKE_STATIC_LINKER_FLAGS})
	message(STATUS "CMAKE_STATIC_LINKER_FLAGS_DEBUG: " ${CMAKE_STATIC_LINKER_FLAGS_DEBUG})
	message(STATUS "CMAKE_STATIC_LINKER_FLAGS_MINSIZEREL: " ${CMAKE_STATIC_LINKER_FLAGS_MINSIZEREL})
	message(STATUS "CMAKE_STATIC_LINKER_FLAGS_RELEASE: " ${CMAKE_STATIC_LINKER_FLAGS_RELEASE})
	message(STATUS "CMAKE_STATIC_LINKER_FLAGS_RELWITHDEBINFO: " ${CMAKE_STATIC_LINKER_FLAGS_RELWITHDEBINFO})
	message(STATUS "CMAKE_VERBOSE_MAKEFILE: " ${CMAKE_VERBOSE_MAKEFILE})
endmacro(PRINT_ALL_CMAKE_SETTINGS)
