CMakePrintHelpers.cmake 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145
  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. function(CMAKE_PRINT_VARIABLES)
  41. set(msg "")
  42. foreach(var ${ARGN})
  43. if(msg)
  44. string(APPEND msg " ; ")
  45. endif()
  46. string(APPEND msg "${var}=\"${${var}}\"")
  47. endforeach()
  48. message(STATUS "${msg}")
  49. endfunction()
  50. function(CMAKE_PRINT_PROPERTIES )
  51. set(options )
  52. set(oneValueArgs )
  53. set(multiValueArgs TARGETS SOURCES TESTS DIRECTORIES CACHE_ENTRIES PROPERTIES )
  54. cmake_parse_arguments(CPP "${options}" "${oneValueArgs}" "${multiValueArgs}" ${ARGN})
  55. if(CPP_UNPARSED_ARGUMENTS)
  56. message(FATAL_ERROR "Unknown keywords given to cmake_print_properties(): \"${CPP_UNPARSED_ARGUMENTS}\"")
  57. return()
  58. endif()
  59. if(NOT CPP_PROPERTIES)
  60. message(FATAL_ERROR "Required argument PROPERTIES missing in cmake_print_properties() call")
  61. return()
  62. endif()
  63. set(mode)
  64. set(items)
  65. set(keyword)
  66. if(CPP_TARGETS)
  67. set(items ${CPP_TARGETS})
  68. set(mode ${mode} TARGETS)
  69. set(keyword TARGET)
  70. endif()
  71. if(CPP_SOURCES)
  72. set(items ${CPP_SOURCES})
  73. set(mode ${mode} SOURCES)
  74. set(keyword SOURCE)
  75. endif()
  76. if(CPP_TESTS)
  77. set(items ${CPP_TESTS})
  78. set(mode ${mode} TESTS)
  79. set(keyword TEST)
  80. endif()
  81. if(CPP_DIRECTORIES)
  82. set(items ${CPP_DIRECTORIES})
  83. set(mode ${mode} DIRECTORIES)
  84. set(keyword DIRECTORY)
  85. endif()
  86. if(CPP_CACHE_ENTRIES)
  87. set(items ${CPP_CACHE_ENTRIES})
  88. set(mode ${mode} CACHE_ENTRIES)
  89. set(keyword CACHE)
  90. endif()
  91. if(NOT mode)
  92. message(FATAL_ERROR "Mode keyword missing in cmake_print_properties() call, must be one of TARGETS SOURCES TESTS DIRECTORIES CACHE_ENTRIES PROPERTIES")
  93. return()
  94. endif()
  95. list(LENGTH mode modeLength)
  96. if("${modeLength}" GREATER 1)
  97. 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")
  98. return()
  99. endif()
  100. set(msg "\n")
  101. foreach(item ${items})
  102. set(itemExists TRUE)
  103. if(keyword STREQUAL "TARGET")
  104. if(NOT TARGET ${item})
  105. set(itemExists FALSE)
  106. string(APPEND msg "\n No such TARGET \"${item}\" !\n\n")
  107. endif()
  108. endif()
  109. if (itemExists)
  110. string(APPEND msg " Properties for ${keyword} ${item}:\n")
  111. foreach(prop ${CPP_PROPERTIES})
  112. get_property(propertySet ${keyword} ${item} PROPERTY "${prop}" SET)
  113. if(propertySet)
  114. get_property(property ${keyword} ${item} PROPERTY "${prop}")
  115. string(APPEND msg " ${item}.${prop} = \"${property}\"\n")
  116. else()
  117. string(APPEND msg " ${item}.${prop} = <NOTFOUND>\n")
  118. endif()
  119. endforeach()
  120. endif()
  121. endforeach()
  122. message(STATUS "${msg}")
  123. endfunction()