#* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
# this file is under MIT License.
# See https://opensource.org/licenses/MIT for license information.
# Copyright (c) 2020-2022 moe-org All rights reserved.
#* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
# 此文件负责管理编译选项

# 设置默认参数
if(MSVC)
	set(CMAKE_CXX_FLAGS "/utf-8")
	set(CMAKE_CXX_FLAGS_DEBUG "")
	set(CMAKE_CXX_FLAGS_RELEASE "")
	set(CMAKE_CXX_FLAGS_RELWITHDEBINFO "")
	set(CMAKE_CXX_FLAGS_MINSIZEREL "")
	set(CMAKE_C_FLAGS "/utf-8")
	set(CMAKE_C_FLAGS_DEBUG "")
	set(CMAKE_C_FLAGS_RELEASE "")
	set(CMAKE_C_FLAGS_RELWITHDEBINFO "")
	set(CMAKE_C_FLAGS_MINSIZEREL "")
else()
	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 "")
endif()

#================================================
# 参数模块化设置

# 优化 速度
function("zsc_opt_enable_optimization_fast")
	if(MSVC)
		target_compile_options(${ARGV} "/O2")
	else()
		target_compile_options(${ARGV} "-O3")
	endif()
endfunction("zsc_opt_enable_optimization_fast")

# 优化 大小
function("zsc_opt_enable_optimization_minal")
	if(MSVC)
		target_compile_options(${ARGV} "/O1")
	else()
		target_compile_options(${ARGV} "-Os")
	endif()
endfunction("zsc_opt_enable_optimization_minal")

# 关闭 优化
function("zsc_opt_disenable_optimization")
	if(MSVC)
		target_compile_options(${ARGV} "/Od")
	else()
		target_compile_options(${ARGV} "-O0")
	endif()
endfunction("zsc_opt_disenable_optimization")

# debug信息
function("zsc_opt_enable_debug_info")
	if(MSVC)
		target_compile_options(${ARGV} "/Z7")
		target_link_options(${ARGV} "/DEBUG:FULL")
	else()
		target_compile_options(${ARGV} "-g3" "-ggdb")
	endif()
endfunction("zsc_opt_enable_debug_info")

# 关闭debug信息
function("zsc_opt_disenable_debug_info")
	# NDEBUG
	target_compile_definitions(${ARGV} "NDEBUG")

	if(MSVC)
		target_link_options(${ARGV} "/DEBUG:NONE")
	else()
		target_compile_options(${ARGV} "-g0")
	endif()
endfunction("zsc_opt_disenable_debug_info")

# 通用选项
function("zsc_opt_common_options")
	if(MSVC)
		# UTF-8编码 && 警告等级 && 标准异常 && 符合标准的代码 && C++20
		target_compile_options(${ARGV} "/utf-8" "/W4" "/EHsc" "/permissive-" "/std:c++20")
		# 启用__cplusplus一致性
		target_compile_options(${ARGV} "/Zc:__cplusplus")
		# 视警告为错误
		target_link_options(${ARGV} "/WX")
	else()
		# 开启所有警告 && C++20
		target_compile_options(${ARGV} "-Wall" "-Wextra" "-std=c++20")
	endif()
endfunction("zsc_opt_common_options")
#================================================
function("zsc_standard_options")
	
	zsc_opt_common_options(${ARGV} PUBLIC)

	if(CMAKE_BUILD_TYPE STREQUAL "Debug")

		zsc_opt_enable_debug_info(${ARGV} PUBLIC)
		zsc_opt_disenable_optimization(${ARGV} PUBLIC)


	elseif(CMAKE_BUILD_TYPE STREQUAL "Release")

		zsc_opt_disenable_debug_info(${ARGV} PUBLIC)
		zsc_opt_enable_optimization_fast(${ARGV} PUBLIC)


	elseif(CMAKE_BUILD_TYPE STREQUAL "RelWithDebInfo")

		zsc_opt_enable_debug_info(${ARGV} PUBLIC)
		zsc_opt_enable_optimization_fast(${ARGV} PUBLIC)


	elseif(CMAKE_BUILD_TYPE STREQUAL "MinSizeRel")

		zsc_opt_disenable_debug_info(${ARGV} PUBLIC)
		zsc_opt_enable_optimization_minal(${ARGV} PUBLIC)

	endif()
endfunction("zsc_standard_options")

function("zsc_standard_windows_crt")
	if(MSVC)
		if(CMAKE_BUILD_TYPE STREQUAL "Debug")
			target_compile_options("${ARGV}" PUBLIC "/MDd")
			message(STATUS "use windows crt /MDd for ${ARGV}")
			
		elseif(CMAKE_BUILD_TYPE STREQUAL "Release")
			target_compile_options("${ARGV}" PUBLIC "/MD")
			message(STATUS "use windows crt /MD for ${ARGV}")

		elseif(CMAKE_BUILD_TYPE STREQUAL "RelWithDebInfo")
			target_compile_options("${ARGV}" PUBLIC "/MDd")
			message(STATUS "use windows crt /MDd for ${ARGV}")

		elseif(CMAKE_BUILD_TYPE STREQUAL "MinSizeRel")
			target_compile_options("${ARGV}" PUBLIC "/MD")
			message(STATUS "use windows crt /MD for ${ARGV}")
		endif()
	endif()
endfunction("zsc_standard_windows_crt")

function("zsc_standard_define")
	if(MSVC)
		target_compile_definitions(${ARGV} PUBLIC ZSCRIPT_ON_WIN)
	elseif(UNIX)
		target_compile_definitions(${ARGV} PUBLIC ZSCRIPT_ON_UNIX)
	endif()
endfunction("zsc_standard_define")
