﻿include_guard()
include(${CMAKE_CURRENT_LIST_DIR}/CommonOptions.cmake)

get_filename_component(XRAD_private_Root "../.." ABSOLUTE BASE_DIR "${CMAKE_CURRENT_LIST_DIR}")
#message(XRAD_private_Root=${XRAD_private_Root})

if(CMAKE_CXX_COMPILER_ID STREQUAL "MSVC")
	include(${CMAKE_CURRENT_LIST_DIR}/DetectThirdParty.cmake)
	set(XRAD_IncludeDir "${XRAD_private_Root}" "${XRAD_ThirdParty}/vld/include")
	if(NOT DEFINED XRAD_private_BuildDir)
		set(XRAD_BuildDir "${XRAD_BuildDir}" CACHE PATH "XRAD library build directory." FORCE)
		if("${XRAD_BuildDir}" STREQUAL "")
			message(FATAL_ERROR "Please, specify XRAD library build directory (XRAD_BuildDir).")
		endif()
		set(XRAD_private_BuildDir "${XRAD_BuildDir}")
	endif()
	set(XRAD_Vld_Libraries "${XRAD_ThirdParty}/vld/lib/${XRAD_Platform}/vld.lib")

	#set(XRAD_MSVCRelativeBuildDir "out/build/${XRAD_Platform}-${XRAD_Configuration}/")
	set(XRAD_MSVCRelativeBuildDir "")

	# Записать в переменную var_name имя файла библиотеки package_name/library_name с путем
	function(xrad_compose_library_name var_name package_name)
		if(ARGC GREATER_EQUAL 3)
			set(library_name ${ARGV2})
		else()
			set(library_name ${package_name})
		endif()
		set(${var_name} "${XRAD_private_BuildDir}/${package_name}/${XRAD_MSVCRelativeBuildDir}${library_name}.lib" PARENT_SCOPE)
	endfunction()
elseif(CMAKE_CXX_COMPILER_ID STREQUAL "GNU")
	set(XRAD_IncludeDir "${XRAD_private_Root}")
	if(NOT DEFINED XRAD_private_BuildDir)
		set(XRAD_BuildDir "${XRAD_BuildDir}" CACHE PATH "XRAD library build directory." FORCE)
		if("${XRAD_BuildDir}" STREQUAL "")
			message(FATAL_ERROR "Please, specify XRAD library build directory (XRAD_BuildDir).")
		endif()
		set(XRAD_private_BuildDir "${XRAD_BuildDir}")
	endif()

	# Записать в переменную var_name имя файла библиотеки package_name/library_name с путем
	function(xrad_compose_library_name var_name package_name)
		if(ARGC GREATER_EQUAL 3)
			set(library_name ${ARGV2})
		else()
			set(library_name ${package_name})
		endif()
		set(${var_name} "${XRAD_private_BuildDir}/${package_name}/lib${library_name}.a" PARENT_SCOPE)
	endfunction()
else()
	message(FATAL_ERROR "Unsupported CMAKE_CXX_COMPILER_ID: \"${CMAKE_CXX_COMPILER_ID}\".")
endif()

function(xrad_setup_package package_name)
	set(XRAD_Require_${package_name} TRUE CACHE INTERNAL "XRAD_Require_${package_name}" FORCE)
endfunction()

# Установить зависимости для пакета (библиотеки) package_name
# package_name: название библиотеки (самого проекта, а не бинарного файла)
# dependencies: разделенный ";" список пакетов, от которых зависит package_name
function(xrad_set_dependencies package_name dependencies)
	if(XRAD_CollectDependencies)
		set(XRAD_${package_name}_Dependencies ${dependencies} CACHE INTERNAL "XRAD dependencies for ${package_name}" FORCE)
	endif()
endfunction()

# Добавить зависимости пакета (проекта) package_name от dependencies
# package_name: название проекта (не бинарного файла)
# dependencies: разделенный ";" список пакетов, от которых зависит package_name
function(xrad_add_dependencies package_name dependencies)
	#message("xrad_add_dependencies(${package_name} ${dependencies})")
	if(XRAD_CollectDependencies)
		foreach(dep_package_name ${dependencies})
			#message("dep_package_name=${dep_package_name}")
			add_dependencies(${package_name} ${dep_package_name})
			message(STATUS "Add dependency ${package_name}(${dep_package_name})")
			#message("dep_package_name_dependencies=" "${XRAD_${dep_package_name}_Dependencies}")
			if(DEFINED XRAD_${dep_package_name}_Dependencies)
				xrad_add_dependencies(${package_name} "${XRAD_${dep_package_name}_Dependencies}")
			endif()
		endforeach()
	endif()
endfunction()
