macro(v4r_print_debug_message)
  if(PRINT_DEBUG)
    string(REPLACE ";" " " __msg "${ARGN}")
    message(STATUS "${__msg}")
  endif()
endmacro()


# Print all cache variables matching a given string
macro(v4r_print_cache_variables _string)
  if(NOT ${_string} STREQUAL "")
    message(STATUS "")
    message(STATUS "List of cache variables matching ${_string}")
    message(STATUS "")
    get_cmake_property(_vars VARIABLES)
    foreach(_var ${_vars})
      if(_var MATCHES ${_string})
        get_property(_help_string CACHE "${_var}" PROPERTY HELPSTRING)
        if(NOT ${_help_string} STREQUAL "")
          message(STATUS "# ${_help_string}")
        endif()
        message(STATUS "${_var}='${${_var}}'")
        message(STATUS "")
      endif()
    endforeach()
  endif()
endmacro()


# Clear all cache variables matching a given string
macro(v4r_clear_cache_variables _string)
  if(NOT ${_string} STREQUAL "")
    message(STATUS "")
    message(STATUS "Clearing cache variables matching ${_string}")
    message(STATUS "")
    get_cmake_property(_vars VARIABLES)
    foreach(_var ${_vars})
      if(_var MATCHES ${_string})
        message(STATUS "${_var}")
        v4r_clear_vars(${_var})
      endif()
    endforeach()
  endif()
endmacro()


# Get all propreties that CMake supports
execute_process(COMMAND ${CMAKE_COMMAND} --help-property-list OUTPUT_VARIABLE CMAKE_PROPERTY_LIST)
# Convert command output into a CMake list
string(REGEX REPLACE ";" "\\\\;" CMAKE_PROPERTY_LIST "${CMAKE_PROPERTY_LIST}")
string(REGEX REPLACE "\n" ";" CMAKE_PROPERTY_LIST "${CMAKE_PROPERTY_LIST}")
list(REMOVE_DUPLICATES CMAKE_PROPERTY_LIST)


function(v4r_print_target_properties _target)
    if(_target STREQUAL "")
      return()
    endif()
    if(NOT TARGET ${_target})
      message("There is no target named '${_target}'")
      return()
    endif()
    get_property(_value TARGET ${_target} PROPERTY IMPORTED_CONFIGURATIONS SET)
    if(_value)
      get_target_property(_configurations ${_target} IMPORTED_CONFIGURATIONS)
    endif()
    message(STATUS "")
    message(STATUS "List of target properties for \"${_target}\"")
    message(STATUS "")
    foreach(_prop ${CMAKE_PROPERTY_LIST})
      if(_prop MATCHES "LOCATION" AND NOT _prop MATCHES "IMPORTED_LOCATION")
        continue()
      endif()
      if(_prop MATCHES "<CONFIG>")
        foreach(_config ${_configurations})
          string(REPLACE "<CONFIG>" "${_config}" _prop_config ${_prop})
          get_property(_value TARGET ${_target} PROPERTY ${_prop_config} SET)
          if(_value)
            get_target_property(_value ${_target} ${_prop_config})
            message(STATUS "${_prop_config}='${_value}'")
            message(STATUS "")
          endif()
        endforeach()
      else()
        get_property(_value TARGET ${_target} PROPERTY ${_prop} SET)
        if(_value)
          get_target_property(_value ${_target} ${_prop})
          message(STATUS "${_prop}='${_value}'")
          message(STATUS "")
        endif()
      endif()
    endforeach()
endfunction()
