CMakeExpandImportedTargets.cmake 6.0 KB

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