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