CMakePrintHelpers.cmake 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147
  1. # Distributed under the OSI-approved BSD 3-Clause License. See accompanying
  2. # file Copyright.txt or https://cmake.org/licensing for details.
  3. #.rst:
  4. # CMakePrintHelpers
  5. # -----------------
  6. #
  7. # Convenience macros for printing properties and variables, useful e.g. for debugging.
  8. #
  9. # ::
  10. #
  11. # CMAKE_PRINT_PROPERTIES([TARGETS target1 .. targetN]
  12. # [SOURCES source1 .. sourceN]
  13. # [DIRECTORIES dir1 .. dirN]
  14. # [TESTS test1 .. testN]
  15. # [CACHE_ENTRIES entry1 .. entryN]
  16. # PROPERTIES prop1 .. propN )
  17. #
  18. # This macro 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. Example::
  21. #
  22. # cmake_print_properties(TARGETS foo bar PROPERTIES
  23. # LOCATION INTERFACE_INCLUDE_DIRS)
  24. #
  25. # This will print the LOCATION and INTERFACE_INCLUDE_DIRS properties for
  26. # both targets foo and bar.
  27. #
  28. #
  29. #
  30. # CMAKE_PRINT_VARIABLES(var1 var2 .. varN)
  31. #
  32. # This macro will print the name of each variable followed by its value.
  33. # Example::
  34. #
  35. # cmake_print_variables(CMAKE_C_COMPILER CMAKE_MAJOR_VERSION DOES_NOT_EXIST)
  36. #
  37. # Gives::
  38. #
  39. # -- CMAKE_C_COMPILER="/usr/bin/gcc" ; CMAKE_MAJOR_VERSION="2" ; DOES_NOT_EXIST=""
  40. include(CMakeParseArguments)
  41. function(CMAKE_PRINT_VARIABLES)
  42. set(msg "")
  43. foreach(var ${ARGN})
  44. if(msg)
  45. string(APPEND msg " ; ")
  46. endif()
  47. string(APPEND msg "${var}=\"${${var}}\"")
  48. endforeach()
  49. message(STATUS "${msg}")
  50. endfunction()
  51. function(CMAKE_PRINT_PROPERTIES )
  52. set(options )
  53. set(oneValueArgs )
  54. set(multiValueArgs TARGETS SOURCES TESTS DIRECTORIES CACHE_ENTRIES PROPERTIES )
  55. cmake_parse_arguments(CPP "${options}" "${oneValueArgs}" "${multiValueArgs}" ${ARGN})
  56. if(CPP_UNPARSED_ARGUMENTS)
  57. message(FATAL_ERROR "Unknown keywords given to cmake_print_properties(): \"${CPP_UNPARSED_ARGUMENTS}\"")
  58. return()
  59. endif()
  60. if(NOT CPP_PROPERTIES)
  61. message(FATAL_ERROR "Required argument PROPERTIES missing in cmake_print_properties() call")
  62. return()
  63. endif()
  64. set(mode)
  65. set(items)
  66. set(keyword)
  67. if(CPP_TARGETS)
  68. set(items ${CPP_TARGETS})
  69. set(mode ${mode} TARGETS)
  70. set(keyword TARGET)
  71. endif()
  72. if(CPP_SOURCES)
  73. set(items ${CPP_SOURCES})
  74. set(mode ${mode} SOURCES)
  75. set(keyword SOURCE)
  76. endif()
  77. if(CPP_TESTS)
  78. set(items ${CPP_TESTS})
  79. set(mode ${mode} TESTS)
  80. set(keyword TEST)
  81. endif()
  82. if(CPP_DIRECTORIES)
  83. set(items ${CPP_DIRECTORIES})
  84. set(mode ${mode} DIRECTORIES)
  85. set(keyword DIRECTORY)
  86. endif()
  87. if(CPP_CACHE_ENTRIES)
  88. set(items ${CPP_CACHE_ENTRIES})
  89. set(mode ${mode} CACHE_ENTRIES)
  90. set(keyword CACHE)
  91. endif()
  92. if(NOT mode)
  93. message(FATAL_ERROR "Mode keyword missing in cmake_print_properties() call, must be one of TARGETS SOURCES TESTS DIRECTORIES CACHE_ENTRIES PROPERTIES")
  94. return()
  95. endif()
  96. list(LENGTH mode modeLength)
  97. if("${modeLength}" GREATER 1)
  98. message(FATAL_ERROR "Multiple mode keyword used in cmake_print_properties() call, it must be exactly one of TARGETS SOURCES TESTS DIRECTORIES CACHE_ENTRIES PROPERTIES")
  99. return()
  100. endif()
  101. set(msg "\n")
  102. foreach(item ${items})
  103. set(itemExists TRUE)
  104. if(keyword STREQUAL "TARGET")
  105. if(NOT TARGET ${item})
  106. set(itemExists FALSE)
  107. string(APPEND msg "\n No such TARGET \"${item}\" !\n\n")
  108. endif()
  109. endif()
  110. if (itemExists)
  111. string(APPEND msg " Properties for ${keyword} ${item}:\n")
  112. foreach(prop ${CPP_PROPERTIES})
  113. get_property(propertySet ${keyword} ${item} PROPERTY "${prop}" SET)
  114. if(propertySet)
  115. get_property(property ${keyword} ${item} PROPERTY "${prop}")
  116. string(APPEND msg " ${item}.${prop} = \"${property}\"\n")
  117. else()
  118. string(APPEND msg " ${item}.${prop} = <NOTFOUND>\n")
  119. endif()
  120. endforeach()
  121. endif()
  122. endforeach()
  123. message(STATUS "${msg}")
  124. endfunction()