CMakeExpandImportedTargets.cmake 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146
  1. #.rst:
  2. # CMakeExpandImportedTargets
  3. # --------------------------
  4. #
  5. # ::
  6. #
  7. # CMAKE_EXPAND_IMPORTED_TARGETS(<var> LIBRARIES lib1 lib2...libN
  8. # [CONFIGURATION <config>])
  9. #
  10. # CMAKE_EXPAND_IMPORTED_TARGETS() takes a list of libraries and replaces
  11. # all imported targets contained in this list with their actual file
  12. # paths of the referenced libraries on disk, including the libraries
  13. # from their link interfaces. If a CONFIGURATION is given, it uses the
  14. # respective configuration of the imported targets if it exists. If no
  15. # CONFIGURATION is given, it uses the first configuration from
  16. # ${CMAKE_CONFIGURATION_TYPES} if set, otherwise ${CMAKE_BUILD_TYPE}.
  17. # This macro is used by all Check*.cmake files which use try_compile()
  18. # or try_run() and support CMAKE_REQUIRED_LIBRARIES , so that these
  19. # checks support imported targets in CMAKE_REQUIRED_LIBRARIES:
  20. #
  21. # ::
  22. #
  23. # cmake_expand_imported_targets(expandedLibs
  24. # LIBRARIES ${CMAKE_REQUIRED_LIBRARIES}
  25. # CONFIGURATION "${CMAKE_TRY_COMPILE_CONFIGURATION}" )
  26. #=============================================================================
  27. # Copyright 2012 Kitware, Inc.
  28. # Copyright 2009-2012 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(${CMAKE_CURRENT_LIST_DIR}/CMakeParseArguments.cmake)
  40. function(CMAKE_EXPAND_IMPORTED_TARGETS _RESULT )
  41. set(options )
  42. set(oneValueArgs CONFIGURATION )
  43. set(multiValueArgs LIBRARIES )
  44. cmake_parse_arguments(CEIT "${options}" "${oneValueArgs}" "${multiValueArgs}" ${ARGN})
  45. if(CEIT_UNPARSED_ARGUMENTS)
  46. message(FATAL_ERROR "Unknown keywords given to CMAKE_EXPAND_IMPORTED_TARGETS(): \"${CEIT_UNPARSED_ARGUMENTS}\"")
  47. endif()
  48. if(NOT CEIT_CONFIGURATION)
  49. if(CMAKE_CONFIGURATION_TYPES)
  50. list(GET CMAKE_CONFIGURATION_TYPES 0 CEIT_CONFIGURATION)
  51. else()
  52. set(CEIT_CONFIGURATION ${CMAKE_BUILD_TYPE})
  53. endif()
  54. endif()
  55. # handle imported library targets
  56. set(_CCSR_REQ_LIBS ${CEIT_LIBRARIES})
  57. set(_CHECK_FOR_IMPORTED_TARGETS TRUE)
  58. set(_CCSR_LOOP_COUNTER 0)
  59. while(_CHECK_FOR_IMPORTED_TARGETS)
  60. math(EXPR _CCSR_LOOP_COUNTER "${_CCSR_LOOP_COUNTER} + 1 ")
  61. set(_CCSR_NEW_REQ_LIBS )
  62. set(_CHECK_FOR_IMPORTED_TARGETS FALSE)
  63. foreach(_CURRENT_LIB ${_CCSR_REQ_LIBS})
  64. if(TARGET "${_CURRENT_LIB}")
  65. get_target_property(_importedConfigs "${_CURRENT_LIB}" IMPORTED_CONFIGURATIONS)
  66. else()
  67. set(_importedConfigs "")
  68. endif()
  69. if (_importedConfigs)
  70. # message(STATUS "Detected imported target ${_CURRENT_LIB}")
  71. # Ok, so this is an imported target.
  72. # First we get the imported configurations.
  73. # Then we get the location of the actual library on disk of the first configuration.
  74. # then we'll get its link interface libraries property,
  75. # iterate through it and replace all imported targets we find there
  76. # with there actual location.
  77. # guard against infinite loop: abort after 100 iterations ( 100 is arbitrary chosen)
  78. if ("${_CCSR_LOOP_COUNTER}" LESS 100)
  79. set(_CHECK_FOR_IMPORTED_TARGETS TRUE)
  80. # else ()
  81. # message(STATUS "********* aborting loop, counter : ${_CCSR_LOOP_COUNTER}")
  82. endif ()
  83. # if one of the imported configurations equals ${CMAKE_TRY_COMPILE_CONFIGURATION},
  84. # use it, otherwise simply use the first one:
  85. list(FIND _importedConfigs "${CEIT_CONFIGURATION}" _configIndexToUse)
  86. if("${_configIndexToUse}" EQUAL -1)
  87. set(_configIndexToUse 0)
  88. endif()
  89. list(GET _importedConfigs ${_configIndexToUse} _importedConfigToUse)
  90. get_target_property(_importedLocation "${_CURRENT_LIB}" IMPORTED_LOCATION_${_importedConfigToUse})
  91. get_target_property(_linkInterfaceLibs "${_CURRENT_LIB}" IMPORTED_LINK_INTERFACE_LIBRARIES_${_importedConfigToUse} )
  92. list(APPEND _CCSR_NEW_REQ_LIBS "${_importedLocation}")
  93. # message(STATUS "Appending lib ${_CURRENT_LIB} as ${_importedLocation}")
  94. if(_linkInterfaceLibs)
  95. foreach(_currentLinkInterfaceLib ${_linkInterfaceLibs})
  96. # message(STATUS "Appending link interface lib ${_currentLinkInterfaceLib}")
  97. if(_currentLinkInterfaceLib)
  98. list(APPEND _CCSR_NEW_REQ_LIBS "${_currentLinkInterfaceLib}" )
  99. endif()
  100. endforeach()
  101. endif()
  102. else()
  103. # "Normal" libraries are just used as they are.
  104. list(APPEND _CCSR_NEW_REQ_LIBS "${_CURRENT_LIB}" )
  105. # message(STATUS "Appending lib directly: ${_CURRENT_LIB}")
  106. endif()
  107. endforeach()
  108. set(_CCSR_REQ_LIBS ${_CCSR_NEW_REQ_LIBS} )
  109. endwhile()
  110. # Finally we iterate once more over all libraries. This loop only removes
  111. # all remaining imported target names (there shouldn't be any left anyway).
  112. set(_CCSR_NEW_REQ_LIBS )
  113. foreach(_CURRENT_LIB ${_CCSR_REQ_LIBS})
  114. if(TARGET "${_CURRENT_LIB}")
  115. get_target_property(_importedConfigs "${_CURRENT_LIB}" IMPORTED_CONFIGURATIONS)
  116. else()
  117. set(_importedConfigs "")
  118. endif()
  119. if (NOT _importedConfigs)
  120. list(APPEND _CCSR_NEW_REQ_LIBS "${_CURRENT_LIB}" )
  121. # message(STATUS "final: appending ${_CURRENT_LIB}")
  122. else ()
  123. # message(STATUS "final: skipping ${_CURRENT_LIB}")
  124. endif ()
  125. endforeach()
  126. # message(STATUS "setting -${_RESULT}- to -${_CCSR_NEW_REQ_LIBS}-")
  127. set(${_RESULT} "${_CCSR_NEW_REQ_LIBS}" PARENT_SCOPE)
  128. endfunction()