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