CMakeExpandImportedTargets.cmake 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156
  1. #.rst:
  2. # CMakeExpandImportedTargets
  3. # --------------------------
  4. #
  5. # Deprecated. Do not use.
  6. #
  7. # This module was once needed to expand imported targets to the underlying
  8. # libraries they reference on disk for use with the :command:`try_compile`
  9. # and :command:`try_run` commands. These commands now support imported
  10. # libraries in their ``LINK_LIBRARIES`` options (since CMake 2.8.11
  11. # for :command:`try_compile` and since CMake 3.2 for :command:`try_run`).
  12. #
  13. # This module does not support the policy :policy:`CMP0022` ``NEW``
  14. # behavior or use of the :prop_tgt:`INTERFACE_LINK_LIBRARIES` property
  15. # because :manual:`generator expressions <cmake-generator-expressions(7)>`
  16. # cannot be evaluated during configuration.
  17. #
  18. # ::
  19. #
  20. # CMAKE_EXPAND_IMPORTED_TARGETS(<var> LIBRARIES lib1 lib2...libN
  21. # [CONFIGURATION <config>])
  22. #
  23. # CMAKE_EXPAND_IMPORTED_TARGETS() takes a list of libraries and replaces
  24. # all imported targets contained in this list with their actual file
  25. # paths of the referenced libraries on disk, including the libraries
  26. # from their link interfaces. If a CONFIGURATION is given, it uses the
  27. # respective configuration of the imported targets if it exists. If no
  28. # CONFIGURATION is given, it uses the first configuration from
  29. # ${CMAKE_CONFIGURATION_TYPES} if set, otherwise ${CMAKE_BUILD_TYPE}.
  30. #
  31. # ::
  32. #
  33. # cmake_expand_imported_targets(expandedLibs
  34. # LIBRARIES ${CMAKE_REQUIRED_LIBRARIES}
  35. # CONFIGURATION "${CMAKE_TRY_COMPILE_CONFIGURATION}" )
  36. #=============================================================================
  37. # Copyright 2012 Kitware, Inc.
  38. # Copyright 2009-2012 Alexander Neundorf <neundorf@kde.org>
  39. #
  40. # Distributed under the OSI-approved BSD License (the "License");
  41. # see accompanying file Copyright.txt for details.
  42. #
  43. # This software is distributed WITHOUT ANY WARRANTY; without even the
  44. # implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
  45. # See the License for more information.
  46. #=============================================================================
  47. # (To distribute this file outside of CMake, substitute the full
  48. # License text for the above reference.)
  49. include(${CMAKE_CURRENT_LIST_DIR}/CMakeParseArguments.cmake)
  50. function(CMAKE_EXPAND_IMPORTED_TARGETS _RESULT )
  51. set(options )
  52. set(oneValueArgs CONFIGURATION )
  53. set(multiValueArgs LIBRARIES )
  54. cmake_parse_arguments(CEIT "${options}" "${oneValueArgs}" "${multiValueArgs}" ${ARGN})
  55. if(CEIT_UNPARSED_ARGUMENTS)
  56. message(FATAL_ERROR "Unknown keywords given to CMAKE_EXPAND_IMPORTED_TARGETS(): \"${CEIT_UNPARSED_ARGUMENTS}\"")
  57. endif()
  58. if(NOT CEIT_CONFIGURATION)
  59. if(CMAKE_CONFIGURATION_TYPES)
  60. list(GET CMAKE_CONFIGURATION_TYPES 0 CEIT_CONFIGURATION)
  61. else()
  62. set(CEIT_CONFIGURATION ${CMAKE_BUILD_TYPE})
  63. endif()
  64. endif()
  65. # handle imported library targets
  66. set(_CCSR_REQ_LIBS ${CEIT_LIBRARIES})
  67. set(_CHECK_FOR_IMPORTED_TARGETS TRUE)
  68. set(_CCSR_LOOP_COUNTER 0)
  69. while(_CHECK_FOR_IMPORTED_TARGETS)
  70. math(EXPR _CCSR_LOOP_COUNTER "${_CCSR_LOOP_COUNTER} + 1 ")
  71. set(_CCSR_NEW_REQ_LIBS )
  72. set(_CHECK_FOR_IMPORTED_TARGETS FALSE)
  73. foreach(_CURRENT_LIB ${_CCSR_REQ_LIBS})
  74. if(TARGET "${_CURRENT_LIB}")
  75. get_target_property(_importedConfigs "${_CURRENT_LIB}" IMPORTED_CONFIGURATIONS)
  76. else()
  77. set(_importedConfigs "")
  78. endif()
  79. if (_importedConfigs)
  80. # message(STATUS "Detected imported target ${_CURRENT_LIB}")
  81. # Ok, so this is an imported target.
  82. # First we get the imported configurations.
  83. # Then we get the location of the actual library on disk of the first configuration.
  84. # then we'll get its link interface libraries property,
  85. # iterate through it and replace all imported targets we find there
  86. # with there actual location.
  87. # guard against infinite loop: abort after 100 iterations ( 100 is arbitrary chosen)
  88. if ("${_CCSR_LOOP_COUNTER}" LESS 100)
  89. set(_CHECK_FOR_IMPORTED_TARGETS TRUE)
  90. # else ()
  91. # message(STATUS "********* aborting loop, counter : ${_CCSR_LOOP_COUNTER}")
  92. endif ()
  93. # if one of the imported configurations equals ${CMAKE_TRY_COMPILE_CONFIGURATION},
  94. # use it, otherwise simply use the first one:
  95. list(FIND _importedConfigs "${CEIT_CONFIGURATION}" _configIndexToUse)
  96. if("${_configIndexToUse}" EQUAL -1)
  97. set(_configIndexToUse 0)
  98. endif()
  99. list(GET _importedConfigs ${_configIndexToUse} _importedConfigToUse)
  100. get_target_property(_importedLocation "${_CURRENT_LIB}" IMPORTED_LOCATION_${_importedConfigToUse})
  101. get_target_property(_linkInterfaceLibs "${_CURRENT_LIB}" IMPORTED_LINK_INTERFACE_LIBRARIES_${_importedConfigToUse} )
  102. list(APPEND _CCSR_NEW_REQ_LIBS "${_importedLocation}")
  103. # message(STATUS "Appending lib ${_CURRENT_LIB} as ${_importedLocation}")
  104. if(_linkInterfaceLibs)
  105. foreach(_currentLinkInterfaceLib ${_linkInterfaceLibs})
  106. # message(STATUS "Appending link interface lib ${_currentLinkInterfaceLib}")
  107. if(_currentLinkInterfaceLib)
  108. list(APPEND _CCSR_NEW_REQ_LIBS "${_currentLinkInterfaceLib}" )
  109. endif()
  110. endforeach()
  111. endif()
  112. else()
  113. # "Normal" libraries are just used as they are.
  114. list(APPEND _CCSR_NEW_REQ_LIBS "${_CURRENT_LIB}" )
  115. # message(STATUS "Appending lib directly: ${_CURRENT_LIB}")
  116. endif()
  117. endforeach()
  118. set(_CCSR_REQ_LIBS ${_CCSR_NEW_REQ_LIBS} )
  119. endwhile()
  120. # Finally we iterate once more over all libraries. This loop only removes
  121. # all remaining imported target names (there shouldn't be any left anyway).
  122. set(_CCSR_NEW_REQ_LIBS )
  123. foreach(_CURRENT_LIB ${_CCSR_REQ_LIBS})
  124. if(TARGET "${_CURRENT_LIB}")
  125. get_target_property(_importedConfigs "${_CURRENT_LIB}" IMPORTED_CONFIGURATIONS)
  126. else()
  127. set(_importedConfigs "")
  128. endif()
  129. if (NOT _importedConfigs)
  130. list(APPEND _CCSR_NEW_REQ_LIBS "${_CURRENT_LIB}" )
  131. # message(STATUS "final: appending ${_CURRENT_LIB}")
  132. else ()
  133. # message(STATUS "final: skipping ${_CURRENT_LIB}")
  134. endif ()
  135. endforeach()
  136. # message(STATUS "setting -${_RESULT}- to -${_CCSR_NEW_REQ_LIBS}-")
  137. set(${_RESULT} "${_CCSR_NEW_REQ_LIBS}" PARENT_SCOPE)
  138. endfunction()