CMakePrintHelpers.cmake 4.0 KB

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