include(CheckCXXCompilerFlag)

function(check_for_linker_availability _linker_name_var)
	if (UNIX AND (CMAKE_CXX_COMPILER_ID MATCHES "GNU|AppleClang|Clang"))
		check_cxx_compiler_flag(-fuse-ld=lld ld_lld)
		if (${ld_lld})
			set(${_linker_name_var} lld PARENT_SCOPE)
			return()
		else ()
			check_cxx_compiler_flag(-fuse-ld=gold ld_gold)
		endif()
		if (${ld_gold})
			set(${_linker_name_var} gold PARENT_SCOPE)
			return()
		else()
			if ((CMAKE_CXX_COMPILER_ID MATCHES "AppleClang|Clang"))
				check_cxx_compiler_flag(-fuse-ld=bfd ld_bfd)
				set(${_linker_name_var} bfd PARENT_SCOPE)
				return()
			endif()
		endif()
	endif()
endfunction(check_for_linker_availability)

if (UNIX AND CMAKE_CXX_COMPILER_ID MATCHES "GNU|AppleClang|Clang")
	check_for_linker_availability(_linker)
	add_library(options::best_linker INTERFACE IMPORTED)
	set_target_properties(options::best_linker PROPERTIES
		INTERFACE_LINK_LIBRARIES -fuse-ld=${_linker}
	)
else()
	add_library(options::best_linker INTERFACE IMPORTED)
endif()

function(lto _target)
	cotire_get_target_compile_flags(DEBUG CXX ${_target} _compile_flags)
	# cotire_get_target_compile_definitions(DEBUG CXX ${_target} _compile_defines)
	# cotire_get_target_include_directories(DEBUG CXX ${_target} _compile_includes _compile_sys_includes)
	get_property(sources TARGET ${_target} PROPERTY SOURCES)
	set (_source_files "")
	set (_excluded_sources "")
	set (_cotired_sources "")
	cotire_filter_language_source_files(CXX ${_target} _source_files _excluded_sources _cotired_sources ${sources})

	foreach(_file ${_source_files})

	endforeach()
endfunction(lto)
