# - Dependency
# To manage the dependency tree
#
SET(myindent "${myindent}\t")
message(STATUS "${myindent}#	\"Dependency\" included!")

if(__Dependency_INCLUDED)
  return()
endif()
set(__Dependency_INCLUDED TRUE)

#-------------------------------------------------------------------------------
# icedust_depends_on(subdir1 [subdir2 ...])
#
# The presence of this function in a CMakeLists.txt is used by icedust_sort_subdirectories
# to get the dependencies from the subdirectories before actually adding them.
#
# The fuction performs those operations that are not needed if there is no
# dependency declared.
#
# The arguments are actually ignored, so there is a check to execute it only once.
#-------------------------------------------------------------------------------
function(icedust_depends_on)
	message(STATUS "${myindent}==>function(icedust_depends_on) ${ARGN}")
	SET(myindent "${myindent}\t")
#	# avoid multiple calls (note that the
#	if(NOT icedust_depends_on_called)
#		message(STATUS "${myindent}	First call!")
#		# get direct and indirect dependencies
#		string(REGEX MATCH ".*/([0-9a-zA-Z_.]*)/([0-9a-zA-Z_.]*)/([0-9a-zA-Z_.]*)" package_name ${CMAKE_CURRENT_LIST_FILE})
#		set(package_name ${CMAKE_MATCH_1})
#		message(STATUS "${myindent}	package_name=${package_name}")
#		set(deps)
#		icedust_list_dependencies(deps ${package_name})
#		message(STATUS "${myindent}	initial deps!"${deps})
#
#		# find the list of targets that generate headers in the packages we depend on.
#		icedust_get_genheader_targets(required_genheader_targets ${deps})
#		message(STATUS "${myindent}	got deps!"${deps})
#		#message(STATUS "${myindent}	required_genheader_targets: ${required_genheader_targets}")
#		set(required_genheader_targets ${required_genheader_targets} PARENT_SCOPE)
#		message(STATUS "${myindent}	required_genheader_targets?"${required_genheader_targets})
#
#		# add the the directories that provide headers to the include_directories
#		foreach(subdir ${deps})
#			message(STATUS "${myindent}	#in "${subdir})
#			if(IS_DIRECTORY ${CMAKE_SOURCE_DIR}/${subdir}/src)
#				message(STATUS "${myindent}		It's a directory!")
#				get_property(local_headers DIRECTORY ${CMAKE_SOURCE_DIR}/${subdir} PROPERTY INSTALLS_LOCAL_HEADERS SET)
#				if(${local_headers} STREQUAL "")
#					message(STATUS "${myindent}			No It does not have headers...")
#				else()
#					message(STATUS "${myindent}			It has headers!")
#					message(STATUS ${local_headers})
#					include_directories(${CMAKE_SOURCE_DIR}/${subdir}/src)
#					include_directories(${CMAKE_SOURCE_DIR}/${subdir}/Linux-x86_64/include)
#				endif()
#			else()
#				message(STATUS "${myindent}		No It's not a directory...")
#			endif()
#		endforeach()
#		# prevent multiple executions
#		set(icedust_depends_on_called TRUE PARENT_SCOPE)
#	else()
#		message(STATUS "${myindent}	Called before?")
#	endif()

	# add the dependencies lines to the DOT dependency graph
#	foreach(d ${ARGN})
#		file(APPEND ${CMAKE_BINARY_DIR}/subdirs_deps.dot "\"${subdir_name}\" -> \"${d}\";\n")
#	endforeach()
	string(REGEX REPLACE "(\t*)\t" "\\1" myindent "${myindent}")
	message(STATUS "${myindent}<==function(icedust_depends_on) ${ARGN}")
endfunction()

##-------------------------------------------------------------------------------
## icedust_list_dependencies(<variable> subdir)
##
## Add the subdirectories we depend on, (directly and indirectly) to the variable
## passed.
##-------------------------------------------------------------------------------
#macro(icedust_list_dependencies variable subdir)
#	message(STATUS "${myindent}==>macro(icedust_list_dependencies ${variable} ${subdir})")
#	SET(myindent "${myindent}\t")
#	# recurse for the direct dependencies
#	foreach(other ${${subdir}_DEPENDENCIES})
#		list(FIND ${variable} ${other} other_idx)
#		message(STATUS "${myindent}	other_idx=${other_idx}, variable=${variable}, other=${other}")
#		if(other_idx LESS 0) # recurse only if the entry is not yet in the list
#			icedust_list_dependencies(${variable} ${other})
#			list(APPEND ${variable} ${other})
#		endif()
#	endforeach()
#	#message(STATUS "${myindent}	 --> ${${variable}}")
#	message(STATUS "${myindent}<==macro(icedust_list_dependencies ${variable} ${subdir})")
#	string(REGEX REPLACE "(\t*)\t" "\\1" myindent "${myindent}")
#endmacro()

#-------------------------------------------------------------------------------
# icedust_get_genheader_targets(<variable> [subdir1 ...])
#
# Collect the targets that are used to generate the headers in the
# subdirectories specified in the arguments and store the list in the variable.
#-------------------------------------------------------------------------------
function(icedust_get_genheader_targets variable)
	message(STATUS "${myindent}==>function(icedust_get_genheader_targets variable)")
	SET(myindent "${myindent}\t")
	set(targets)
	foreach(subdir ${ARGN})
		if(EXISTS ${CMAKE_SOURCE_DIR}/${subdir})
#			get_property(tmp DIRECTORY ${CMAKE_SOURCE_DIR}/${subdir} PROPERTY GENERATED_HEADERS_TARGETS)
			set(targets ${targets} ${tmp})
		endif()
	endforeach()
	if(targets)
		list(REMOVE_DUPLICATES targets)
	endif()
	set(${variable} ${targets} PARENT_SCOPE)
	string(REGEX REPLACE "(\t*)\t" "\\1" myindent "${myindent}")
	message(STATUS "${myindent}<==function(icedust_get_genheader_targets variable)")
endfunction()

string(REGEX REPLACE "(\t*)\t" "\\1" myindent "${myindent}")