# Various macros to assit in unit testing CMake.

macro(CPM_PostModuleExecCallback)
  set(CPM_LAST_MODULE_UNID_NO_VER ${__CPM_PATH_UNID} PARENT_SCOPE)
  set(CPM_LAST_MODULE_FULL_UNID ${__CPM_FULL_UNID} PARENT_SCOPE)
  set(CPM_LAST_MODULE_DECLARED_NAME ${CPM_LAST_MODULE_NAME} PARENT_SCOPE)
endmacro()

macro(CPM_PrintVariables)
  message("")
  message("== Module: ${CPM_LAST_MODULE_DECLARED_NAME}")
  message("")
  message("Non version UNID for module: ${CPM_LAST_MODULE_UNID_NO_VER}")
  message("Non version UNID keyed variables: ")
  message("+ Version map: ${CPM_KV_MOD_VERSION_MAP_${CPM_LAST_MODULE_UNID_NO_VER}}")
  message("Full UNID for module: ${CPM_LAST_MODULE_FULL_UNID}")
  message("Full UNID keyed variables:")
  message("+ Last name declared by module: ${CPM_LAST_MODULE_DECLARED_NAME}")
  message("+ Source added: ${CPM_KV_SOURCE_ADDED_MAP_${CPM_LAST_MODULE_FULL_UNID}}")
  message("+ Preproc: ${CPM_KV_PREPROC_NS_MAP_${CPM_LAST_MODULE_FULL_UNID}}")
  message("+ Include: ${CPM_KV_INCLUDE_MAP_${CPM_LAST_MODULE_FULL_UNID}}")
  message("+ Definition: ${CPM_KV_DEFINITION_MAP_${CPM_LAST_MODULE_FULL_UNID}}")
  message("+ Export: ${CPM_KV_EXPORT_MAP_${CPM_LAST_MODULE_FULL_UNID}}")
  message("+ Forward DECL: ${CPM_KV_FORWARD_DECL_MAP_${CPM_LAST_MODULE_FULL_UNID}}")
  message("Lists:")
  message("+ Version map: ${CPM_KV_LIST_MOD_VERSION_MAP}")
  message("+ Source added: ${CPM_KV_LIST_SOURCE_ADDED_MAP}")
  message("+ Prepoc list: ${CPM_KV_LIST_PREPROC_NS_MAP}")
  message("+ Include list: ${CPM_KV_LIST_INCLUDE_MAP}")
  message("+ Def list: ${CPM_KV_LIST_DEFINITION_MAP}")
  message("+ Export list: ${CPM_KV_LIST_EXPORT_MAP}")
  message("Full definitions and includes")
  message("+ Definitions: ${CPM_DEFINITIONS}")
  message("+ Includes: ${CPM_INCLUDE_DIRS}")
endmacro()

macro(CPM_ListContains var value)
  set(${var})
  foreach (value2 ${ARGN})
    if ("${value}" STREQUAL "${value2}")
      set(${var} TRUE)
    endif ()
  endforeach ()
endmacro(CPM_ListContains)

macro(CPM_VerifyListSubset list1 list2 errorPrefix)
  foreach (val IN LISTS ${list1})
    CPM_ListContains(contains ${val} ${${list2}})
    if (NOT contains)
      message(FATAL_ERROR "${errorPrefix} Cannot find: ${val}")
    endif ()
  endforeach()
endmacro()

macro(CPM_VerifyListEquality list1 list2 errorPrefix)
  CPM_VerifyListSubset(${list1} ${list2} ${errorPrefix})
  list(LENGTH ${list2} listlen1)
  list(LENGTH ${list1} listlen2)
  if (NOT listlen1 EQUAL listlen2)
    message(FATAL_ERROR "${errorPrefix} List lengths not equal!")
  endif()
endmacro()

