# This file is part of Seir.
# Copyright (C) Sergei Blagodarin.
# SPDX-License-Identifier: Apache-2.0

include(${CMAKE_CURRENT_LIST_DIR}/SeirUtils.cmake)

set(SEIR_3RDPARTY_DIR ${CMAKE_BINARY_DIR}/_SeirPackages CACHE PATH "Directory for provided third-party packages")
set(SEIR_3RDPARTY_SKIP "" CACHE STRING "List of third-party packages to skip providing")
mark_as_advanced(SEIR_3RDPARTY_DIR SEIR_3RDPARTY_SKIP)

function(_seir_cmake _source_dir _build_dir _install_dir)
	cmake_parse_arguments(_arg "" "TARGET" "MSVC_WARNINGS;OPTIONS" ${ARGN})
	file(REMOVE_RECURSE ${_build_dir})
	file(MAKE_DIRECTORY ${_build_dir})
	file(REMOVE_RECURSE ${_install_dir})
	set(_options ${_source_dir} -G ${CMAKE_GENERATOR})
	if(CMAKE_GENERATOR_PLATFORM)
		list(APPEND _options -A ${CMAKE_GENERATOR_PLATFORM})
	endif()
	if(CMAKE_GENERATOR_TOOLSET)
		list(APPEND _options -T ${CMAKE_GENERATOR_TOOLSET})
	endif()
	list(APPEND _options
		-DCMAKE_INSTALL_PREFIX=${_install_dir}
		-DCMAKE_POSITION_INDEPENDENT_CODE=OFF
		)
	get_property(_is_multi_config GLOBAL PROPERTY GENERATOR_IS_MULTI_CONFIG)
	if(_is_multi_config)
		list(APPEND _options -DCMAKE_DEBUG_POSTFIX=d)
	elseif(CMAKE_BUILD_TYPE)
		list(APPEND _options -DCMAKE_BUILD_TYPE=${CMAKE_BUILD_TYPE})
	endif()
	if(_arg_TARGET)
		set(_target ${_arg_TARGET})
		if(_is_multi_config)
			list(APPEND _options -DCMAKE_ARCHIVE_OUTPUT_DIRECTORY_DEBUG=${_install_dir}/lib -DCMAKE_ARCHIVE_OUTPUT_DIRECTORY_RELEASE=${_install_dir}/lib)
		else()
			list(APPEND _options -DCMAKE_ARCHIVE_OUTPUT_DIRECTORY=${_install_dir}/lib)
		endif()
	else()
		set(_target "install")
	endif()
	if(MSVC AND _arg_MSVC_WARNINGS)
		set(_msvc_warnings)
		foreach(_warning ${_arg_MSVC_WARNINGS})
			set(_msvc_warnings "${_msvc_warnings} /wd${_warning}")
		endforeach()
		set(_env "CFLAGS=$ENV{CFLAGS}${_msvc_warnings}")
	endif()
	execute_process(COMMAND ${CMAKE_COMMAND} -E env RCFLAGS=/nologo ${_env} ${CMAKE_COMMAND} ${_options} ${_arg_OPTIONS} WORKING_DIRECTORY ${_build_dir} COMMAND_ERROR_IS_FATAL ANY)
	if(CMAKE_GENERATOR MATCHES "^Visual Studio")
		set(_build_options -- /nologo)
	else()
		set(_build_options "")
	endif()
	if(_is_multi_config)
		execute_process(COMMAND ${CMAKE_COMMAND} --build ${_build_dir} --target ${_target} --parallel --config Debug ${_build_options} COMMAND_ERROR_IS_FATAL ANY)
		execute_process(COMMAND ${CMAKE_COMMAND} --build ${_build_dir} --target ${_target} --parallel --config Release ${_build_options} COMMAND_ERROR_IS_FATAL ANY)
	else()
		execute_process(COMMAND ${CMAKE_COMMAND} --build ${_build_dir} --target ${_target} --parallel ${_build_options} COMMAND_ERROR_IS_FATAL ANY)
	endif()
endfunction()

macro(_seir_provide_begin name)
	if(${name} IN_LIST SEIR_3RDPARTY_SKIP)
		if(arg_FLAG)
			set(${result} "" PARENT_SCOPE)
		else()
			unset(${result} PARENT_SCOPE)
		endif()
		return()
	endif()
	if(arg_SET_UPDATED)
		set(${arg_SET_UPDATED} OFF PARENT_SCOPE)
	endif()
endmacro()

macro(_seir_provide_end_library name)
	seir_select(${result} ${arg_FLAG} "-D${name}_ROOT=${install_dir}" ${install_dir} PARENT_SCOPE)
endmacro()

include(${CMAKE_CURRENT_LIST_DIR}/SeirPackages/benchmark.cmake)
include(${CMAKE_CURRENT_LIST_DIR}/SeirPackages/doctest.cmake)
include(${CMAKE_CURRENT_LIST_DIR}/SeirPackages/fmt.cmake)
include(${CMAKE_CURRENT_LIST_DIR}/SeirPackages/freetype.cmake)
include(${CMAKE_CURRENT_LIST_DIR}/SeirPackages/jpeg.cmake)
include(${CMAKE_CURRENT_LIST_DIR}/SeirPackages/nasm.cmake)
include(${CMAKE_CURRENT_LIST_DIR}/SeirPackages/ogg.cmake)
include(${CMAKE_CURRENT_LIST_DIR}/SeirPackages/plf_colony.cmake)
include(${CMAKE_CURRENT_LIST_DIR}/SeirPackages/vorbis.cmake)
include(${CMAKE_CURRENT_LIST_DIR}/SeirPackages/zlib.cmake)
include(${CMAKE_CURRENT_LIST_DIR}/SeirPackages/zstd.cmake)
