CMakeExpandImportedTargets.cmake 6.1 KB

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