#* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
# 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(NOT DEFINED ZSC_CMAKE_PATH)
	set(ZSC_CMAKE_PATH cmake)
endif()

set(ZSC_LLVM_SOURCE_DIR		"${ZSC_LIBRARIES_DIR}/llvm/llvm")
set(ZSC_LLVM_BUILD_DIR		"${CMAKE_CURRENT_BINARY_DIR}/llvm/build")
set(ZSC_LLVM_INSTALL_DIR	"${CMAKE_CURRENT_BINARY_DIR}/llvm/installed/${CMAKE_BUILD_TYPE}")

# 安装完成后将设置此文件，防止重复安装
set(ZSC_LLVM_INSTALL_MARK_FILE	"${ZSC_LLVM_INSTALL_DIR}/ZSC_LLVM_INSTALLED.MARK")

message(STATUS "LLVM generate options:${ZSC_LLVM_GENERATE_OPTIONS}")
message(STATUS "LLVM build options:${ZSC_LLVM_BUILD_OPTIONS}")
message(STATUS "LLVM install options:${ZSC_LLVM_INSTALL_OPTIONS}")

# 开始
if(NOT EXISTS ${ZSC_LLVM_INSTALL_MARK_FILE})

	# 设置处理器个数
	if(NOT DEFINED ZSC_LLVM_BUILD_COUNT)
		include(ProcessorCount)
		ProcessorCount(ZSC_LLVM_BUILD_COUNT)
	endif()

	message(STATUS "use ${ZSC_LLVM_BUILD_COUNT} processors to build llvm")

	# 创建目录
	if(NOT EXISTS ${ZSC_LLVM_INSTALL_DIR})
		file(MAKE_DIRECTORY ${ZSC_LLVM_INSTALL_DIR})
	endif()
	if(NOT EXISTS ${ZSC_LLVM_BUILD_DIR})
		file(MAKE_DIRECTORY ${ZSC_LLVM_BUILD_DIR})
	endif()

	# LLVM-生成
	execute_process(
		COMMAND 
		${ZSC_CMAKE_PATH}
		# 设置源文件和输出目录
		"-S" "${ZSC_LLVM_SOURCE_DIR}"
		"-B" "${ZSC_LLVM_BUILD_DIR}"
		# 设置构建基本信息
		"-DCMAKE_BUILD_TYPE=${CMAKE_BUILD_TYPE}"
		"-DCMAKE_CXX_STANDARD=14"
		# 我们只启用LLVM.清空此项
		"-DLLVM_ENABLE_PROJECTS="
		# 不构建一些额外的东西
		"-DLLVM_BUILD_TESTS=False"
		"-DLLVM_BUILD_EXAMPLES=False"
		"-DLLVM_BUILD_BENCHMARKS=False"
		"-DLLVM_BUILD_DOCS=False"
		"-DLLVM_INCLUDE_TESTS=False"
		"-DLLVM_INCLUDE_BENCHMARKS=False"
		"-DLLVM_INCLUDE_EXAMPLES=False"
		"-DLLVM_ENABLE_BINDINGS=False"
		# 构建静态库
		"-DBUILD_SHARED_LIBS=False"
		# 设置安装路径
		"-DCMAKE_INSTALL_PREFIX=${ZSC_LLVM_INSTALL_DIR}"
		${ZSC_LLVM_GENERATE_OPTIONS}

		# 其他执行设置
		WORKING_DIRECTORY "${ZSC_LLVM_BUILD_DIR}"
		COMMAND_ECHO STDOUT
		COMMAND_ERROR_IS_FATAL ANY
	)
	
	# LLVM-构建
	execute_process(
		COMMAND 
		${ZSC_CMAKE_PATH} 
		"--build" "${ZSC_LLVM_BUILD_DIR}"
	       	"-j${ZSC_LLVM_BUILD_COUNT}" 
		${ZSC_LLVM_BUILD_OPTIONS}
		
		# 其他执行设置
		WORKING_DIRECTORY "${ZSC_LLVM_BUILD_DIR}"
		COMMAND_ECHO STDOUT
		COMMAND_ERROR_IS_FATAL ANY
	)

	# LLVM-安装
	execute_process(
		COMMAND 
		${ZSC_CMAKE_PATH} 
		"--install" "${ZSC_LLVM_BUILD_DIR}"
		"--prefix" "${ZSC_LLVM_INSTALL_DIR}"
		"--config" "${CMAKE_BUILD_TYPE}"
		${ZSC_LLVM_INSTALL_OPTIONS}
	
		# 其他执行设置
		WORKING_DIRECTORY "${ZSC_LLVM_BUILD_DIR}"
		COMMAND_ECHO STDOUT
		COMMAND_ERROR_IS_FATAL ANY
	)

	# 创建标记文件
	file(TOUCH ${ZSC_LLVM_INSTALL_MARK_FILE})

else()
	message(STATUS "LLVM build and installed in ${ZSC_LLVM_BUILD_DIR} and ${ZSC_LLVM_INSTALL_DIR}")
	message(STATUS "if you want to update llvm build delete the file:${ZSC_LLVM_INSTALL_MARK_FILE}")
endif()

# 查找llvm
set(ZSC_LLVM_CMAKE_MODULE_DIR "${ZSC_LLVM_INSTALL_DIR}/lib/cmake/llvm")
set(LLVM_DIR ${ZSC_LLVM_CMAKE_MODULE_DIR})

message(STATUS "search LLVM in ${ZSC_LLVM_CMAKE_MODULE_DIR}")

find_package(
	LLVM
	REQUIRED
	CONFIG
	)

message(STATUS "LLVM include dir:${LLVM_INCLUDE_DIRS}")
message(STATUS "LLVM definitions:${LLVM_DEFINITIONS}")

separate_arguments(LLVM_DEFINITIONS_LIST NATIVE_COMMAND ${LLVM_DEFINITIONS})
