CMakePrintHelpers.cmake 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146
  1. # - Convenience macros for printing properties and variables, useful e.g. for debugging.
  2. #
  3. #
  4. # CMAKE_PRINT_PROPERTIES([TARGETS target1 .. targetN]
  5. # [SOURCES source1 .. sourceN]
  6. # [DIRECTORIES dir1 .. dirN]
  7. # [TESTS test1 .. testN]
  8. # [CACHE_ENTRIES entry1 .. entryN]
  9. # PROPERTIES prop1 .. propN )
  10. #
  11. # This macro prints the values of the properties of the given targets,
  12. # source files, directories, tests or cache entries. Exactly one of the
  13. # scope keywords must be used.
  14. # Example:
  15. # cmake_print_properties(TARGETS foo bar PROPERTIES LOCATION INTERFACE_INCLUDE_DIRS)
  16. # This will print the LOCATION and INTERFACE_INCLUDE_DIRS properties for both
  17. # targets foo and bar.
  18. #
  19. #
  20. # CMAKE_PRINT_VARIABLES(var1 var2 .. varN)
  21. #
  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 THIS_ONE_DOES_NOT_EXIST)
  25. # Gives:
  26. # -- CMAKE_C_COMPILER="/usr/bin/gcc" ; CMAKE_MAJOR_VERSION="2" ; THIS_ONE_DOES_NOT_EXIST=""
  27. #=============================================================================
  28. # Copyright 2013 Alexander Neundorf, <[email protected]>
  29. #
  30. # Distributed under the OSI-approved BSD License (the "License");
  31. # see accompanying file Copyright.txt for details.
  32. #
  33. # This software is distributed WITHOUT ANY WARRANTY; without even the
  34. # implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
  35. # See the License for more information.
  36. #=============================================================================
  37. # (To distribute this file outside of CMake, substitute the full
  38. # License text for the above reference.)
  39. include(CMakeParseArguments)
  40. function(CMAKE_PRINT_VARIABLES)
  41. set(msg "")
  42. foreach(var ${ARGN})
  43. if(msg)
  44. set(msg "${msg} ; ")
  45. endif()
  46. set(msg "${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. set(msg "${msg}\n No such TARGET \"${item}\" !\n\n")
  107. endif()
  108. endif()
  109. if (itemExists)
  110. set(msg "${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. set(msg "${msg} ${item}.${prop} = \"${property}\"\n")
  116. else()
  117. set(msg "${msg} ${item}.${prop} = <NOTFOUND>\n")
  118. endif()
  119. endforeach()
  120. endif()
  121. endforeach()
  122. message(STATUS "${msg}")
  123. endfunction()