CMakePrintHelpers.cmake 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200
  1. # Distributed under the OSI-approved BSD 3-Clause License. See accompanying
  2. # file LICENSE.rst or https://cmake.org/licensing for details.
  3. #[=======================================================================[.rst:
  4. CMakePrintHelpers
  5. -----------------
  6. Convenience functions for printing properties and variables, useful
  7. e.g. for debugging.
  8. Commands
  9. ^^^^^^^^
  10. .. command:: cmake_print_properties
  11. .. code-block:: cmake
  12. cmake_print_properties(<TARGETS [<target1> ...] |
  13. SOURCES [<source1> ...] |
  14. DIRECTORIES [<dir1> ...] |
  15. TESTS [<test1> ...] |
  16. CACHE_ENTRIES [<entry1> ...] >
  17. PROPERTIES [<prop1> ...])
  18. This function prints the values of the properties of the given targets,
  19. source files, directories, tests or cache entries. Exactly one of the
  20. scope keywords must be used. The scope keyword and its arguments must
  21. come before the ``PROPERTIES`` keyword in the arguments list.
  22. .. command:: cmake_print_variables
  23. .. code-block:: cmake
  24. cmake_print_variables([var1 [var2 ... [varN]]])
  25. This function prints the name of each variable followed by its value.
  26. Examples
  27. ^^^^^^^^
  28. Printing the ``LOCATION`` and ``INTERFACE_INCLUDE_DIRECTORIES`` properties for
  29. both targets ``foo`` and ``bar``:
  30. .. code-block:: cmake
  31. include(CMakePrintHelpers)
  32. cmake_print_properties(
  33. TARGETS foo bar
  34. PROPERTIES LOCATION INTERFACE_INCLUDE_DIRECTORIES
  35. )
  36. Gives::
  37. --
  38. Properties for TARGET foo:
  39. foo.LOCATION = "/usr/lib/libfoo.so"
  40. foo.INTERFACE_INCLUDE_DIRECTORIES = "/usr/include;/usr/include/foo"
  41. Properties for TARGET bar:
  42. bar.LOCATION = "/usr/lib/libbar.so"
  43. bar.INTERFACE_INCLUDE_DIRECTORIES = "/usr/include;/usr/include/bar"
  44. Printing given variables:
  45. .. code-block:: cmake
  46. include(CMakePrintHelpers)
  47. cmake_print_variables(CMAKE_C_COMPILER CMAKE_MAJOR_VERSION NOT_EXISTS)
  48. Gives::
  49. -- CMAKE_C_COMPILER="/usr/bin/cc" ; CMAKE_MAJOR_VERSION="3" ; NOT_EXISTS=""
  50. #]=======================================================================]
  51. function(cmake_print_variables)
  52. set(msg "")
  53. foreach(var ${ARGN})
  54. if(msg)
  55. string(APPEND msg " ; ")
  56. endif()
  57. string(APPEND msg "${var}=\"${${var}}\"")
  58. endforeach()
  59. message(STATUS "${msg}")
  60. endfunction()
  61. function(cmake_print_properties)
  62. set(options )
  63. set(oneValueArgs )
  64. set(cpp_multiValueArgs PROPERTIES )
  65. set(cppmode_multiValueArgs TARGETS SOURCES TESTS DIRECTORIES CACHE_ENTRIES )
  66. string(JOIN " " _mode_names ${cppmode_multiValueArgs})
  67. set(_missing_mode_message
  68. "Mode keyword missing in cmake_print_properties() call, there must be exactly one of ${_mode_names}")
  69. cmake_parse_arguments(
  70. CPP "${options}" "${oneValueArgs}" "${cpp_multiValueArgs}" ${ARGN})
  71. if(NOT CPP_PROPERTIES)
  72. message(FATAL_ERROR
  73. "Required argument PROPERTIES missing in cmake_print_properties() call")
  74. return()
  75. endif()
  76. if(NOT CPP_UNPARSED_ARGUMENTS)
  77. message(FATAL_ERROR "${_missing_mode_message}")
  78. return()
  79. endif()
  80. cmake_parse_arguments(
  81. CPPMODE "${options}" "${oneValueArgs}" "${cppmode_multiValueArgs}"
  82. ${CPP_UNPARSED_ARGUMENTS})
  83. if(CPPMODE_UNPARSED_ARGUMENTS)
  84. message(FATAL_ERROR
  85. "Unknown keywords given to cmake_print_properties(): \"${CPPMODE_UNPARSED_ARGUMENTS}\"")
  86. return()
  87. endif()
  88. set(mode)
  89. set(items)
  90. set(keyword)
  91. if(CPPMODE_TARGETS)
  92. set(items ${CPPMODE_TARGETS})
  93. set(mode ${mode} TARGETS)
  94. set(keyword TARGET)
  95. endif()
  96. if(CPPMODE_SOURCES)
  97. set(items ${CPPMODE_SOURCES})
  98. set(mode ${mode} SOURCES)
  99. set(keyword SOURCE)
  100. endif()
  101. if(CPPMODE_TESTS)
  102. set(items ${CPPMODE_TESTS})
  103. set(mode ${mode} TESTS)
  104. set(keyword TEST)
  105. endif()
  106. if(CPPMODE_DIRECTORIES)
  107. set(items ${CPPMODE_DIRECTORIES})
  108. set(mode ${mode} DIRECTORIES)
  109. set(keyword DIRECTORY)
  110. endif()
  111. if(CPPMODE_CACHE_ENTRIES)
  112. set(items ${CPPMODE_CACHE_ENTRIES})
  113. set(mode ${mode} CACHE_ENTRIES)
  114. # This is a workaround for the fact that passing `CACHE` as an argument to
  115. # set() causes a cache variable to be set.
  116. set(keyword "")
  117. string(APPEND keyword CACHE)
  118. endif()
  119. if(NOT mode)
  120. message(FATAL_ERROR "${_missing_mode_message}")
  121. return()
  122. endif()
  123. list(LENGTH mode modeLength)
  124. if("${modeLength}" GREATER 1)
  125. message(FATAL_ERROR
  126. "Multiple mode keywords used in cmake_print_properties() call, there must be exactly one of ${_mode_names}.")
  127. return()
  128. endif()
  129. set(msg "\n")
  130. foreach(item ${items})
  131. set(itemExists TRUE)
  132. if(keyword STREQUAL "TARGET")
  133. if(NOT TARGET ${item})
  134. set(itemExists FALSE)
  135. string(APPEND msg "\n No such TARGET \"${item}\" !\n\n")
  136. endif()
  137. endif()
  138. if (itemExists)
  139. string(APPEND msg " Properties for ${keyword} ${item}:\n")
  140. foreach(prop ${CPP_PROPERTIES})
  141. get_property(propertySet ${keyword} ${item} PROPERTY "${prop}" SET)
  142. if(propertySet)
  143. get_property(property ${keyword} ${item} PROPERTY "${prop}")
  144. string(APPEND msg " ${item}.${prop} = \"${property}\"\n")
  145. else()
  146. string(APPEND msg " ${item}.${prop} = <NOTFOUND>\n")
  147. endif()
  148. endforeach()
  149. endif()
  150. endforeach()
  151. message(STATUS "${msg}")
  152. endfunction()