#=================================================================
# file:         options.cmake
# Copyright (c) 2020-2021 moe-org 保留所有权利
#=================================================================

# 清除默认参数
set(CMAKE_CXX_FLAGS "")
set(CMAKE_CXX_FLAGS_DEBUG "")
set(CMAKE_CXX_FLAGS_RELEASE "")
set(CMAKE_CXX_FLAGS_RELWITHDEBINFO "")
set(CMAKE_CXX_FLAGS_MINSIZEREL "")
set(CMAKE_C_FLAGS "")
set(CMAKE_C_FLAGS_DEBUG "")
set(CMAKE_C_FLAGS_RELEASE "")
set(CMAKE_C_FLAGS_RELWITHDEBINFO "")
set(CMAKE_C_FLAGS_MINSIZEREL "")

# 启用c++20
target_compile_features(${U_CORE_LIBRARY} PUBLIC "cxx_std_20")
target_compile_features(${U_CLIENT_EXECUTABLE} PUBLIC "cxx_std_20")
target_compile_features(${U_SERVER_EXECUTABLE} PUBLIC "cxx_std_20")
target_compile_features(${U_CORE_LIBRARY_TEST} PUBLIC "cxx_std_20")
target_compile_features(${U_CLIENT_EXECUTABLE_TEST} PUBLIC "cxx_std_20")
target_compile_features(${U_SERVER_EXECUTABLE_TEST} PUBLIC "cxx_std_20")

# help functions
function("utopia_compiler_options")
	target_compile_options(${U_CORE_LIBRARY} PUBLIC ${ARGV})
	target_compile_options(${U_CLIENT_EXECUTABLE} PUBLIC ${ARGV})
	target_compile_options(${U_SERVER_EXECUTABLE} PUBLIC ${ARGV})
	target_compile_options(${U_CORE_LIBRARY_TEST} PUBLIC ${ARGV})
	target_compile_options(${U_CLIENT_EXECUTABLE_TEST} PUBLIC ${ARGV})
	target_compile_options(${U_SERVER_EXECUTABLE_TEST} PUBLIC ${ARGV})
endfunction("utopia_compiler_options")


function("utopia_linker_options")
	target_link_options(${U_CORE_LIBRARY} PUBLIC ${ARGV})
	target_link_options(${U_CLIENT_EXECUTABLE} PUBLIC ${ARGV})
	target_link_options(${U_SERVER_EXECUTABLE} PUBLIC ${ARGV})
	target_link_options(${U_CORE_LIBRARY_TEST} PUBLIC ${ARGV})
	target_link_options(${U_CLIENT_EXECUTABLE_TEST} PUBLIC ${ARGV})
	target_link_options(${U_SERVER_EXECUTABLE_TEST} PUBLIC ${ARGV})
endfunction("utopia_linker_options")

function("utopia_defines")
	target_compile_definitions(${U_CORE_LIBRARY} PUBLIC ${ARGV})
	target_compile_definitions(${U_CLIENT_EXECUTABLE} PUBLIC ${ARGV})
	target_compile_definitions(${U_SERVER_EXECUTABLE} PUBLIC ${ARGV})
	target_compile_definitions(${U_CORE_LIBRARY_TEST} PUBLIC ${ARGV})
	target_compile_definitions(${U_CLIENT_EXECUTABLE_TEST} PUBLIC ${ARGV})
	target_compile_definitions(${U_SERVER_EXECUTABLE_TEST} PUBLIC ${ARGV})
endfunction("utopia_defines")


#===============================================
# msvc设置
if(MSVC)
	# UTF-8编码 && 警告等级 && 标准异常 && 符合标准的代码 && C++20
	utopia_compiler_options("/utf-8" "/W4" "/EHsc" "/permissive-" "/std:c++20")

	# 启用__cplusplus一致性
	utopia_compiler_options("/Zc:__cplusplus")

	
	# 视警告为错误
	utopia_linker_options("/WX")
	
	#===============================================
	#==================msvc Debug设置
	if(CMAKE_BUILD_TYPE STREQUAL "Debug")

		# 禁止优化 && CRT Debug Version && 生成调试信息
		utopia_compiler_options("/Od" "/MDd" "/Z7")

		# 生成调试信息
		utopia_linker_options("/DEBUG:FULL")

	#===============================================
	#==================msvc Release设置
	elseif(CMAKE_BUILD_TYPE STREQUAL "Release")

		# 启动优化 && CRT && 不生成调试信息
		utopia_compiler_options("/O2" "/MD" "/Zi")
		
		# 不生成调试信息
		utopia_linker_options("/DEBUG:NONE")
		
	#===============================================
	else()
		message(FAIL "Unknown build type:${CMAKE_BUILD_TYPE}")
	endif()

else()
#===============================================
# unix compiler设置

	# 开启所有警告 && C++20
	utopia_compiler_options("-Wall" "-Wextra" "-std=c++20")


	#===============================================
	#==================unix Debug设置
	if(CMAKE_BUILD_TYPE STREQUAL "Debug")

		# 禁止优化 && 生成调试信息
		utopia_compiler_options("-O0" "-g3" "-gdwarf")


	#===============================================
	#==================unix Release设置
	elseif(CMAKE_BUILD_TYPE STREQUAL "Release")

		# 启动优化 && 基本调试信息
		utopia_compiler_options("-O3" "-g1")

		# NDEBUG
		utopia_defines("NDEBUG")
		
	#===============================================
	else()
		message(FATAL_ERROR "Unknown build type:${CMAKE_BUILD_TYPE}")
	endif()

endif()
