Explorar o código

Check*.cmake: Expand imported targets in CMAKE_REQUIRED_LIBRARIES

Add the function cmake_expand_imported_targets() to expand imported
targets in a list of libraries into their on-disk file names for a
particular configuration.  Adapt the implementation from KDE's
HANDLE_IMPORTED_TARGETS_IN_CMAKE_REQUIRED_LIBRARIES which has been in
use for over 2 years.  Call the function from all the Check*.cmake
macros to handle imported targets named in CMAKE_REQUIRED_LIBRARIES.

Alex
Alex Neundorf %!s(int64=13) %!d(string=hai) anos
pai
achega
35c48e1270

+ 129 - 0
Modules/CMakeExpandImportedTargets.cmake

@@ -0,0 +1,129 @@
+# CMAKE_EXPAND_IMPORTED_TARGETS(<var> LIBRARIES lib1 lib2...libN
+#                                     [CONFIGURATION <config>] )
+#
+# CMAKE_EXPAND_IMPORTED_TARGETS() takes a list of libraries and replaces
+# all imported targets contained in this list with their actual file paths
+# of the referenced libraries on disk, including the libraries from their
+# link interfaces.
+# If a CONFIGURATION is given, it uses the respective configuration of the
+# imported targets if it exists. If no CONFIGURATION is given, it uses
+# the first configuration from ${CMAKE_CONFIGURATION_TYPES} if set, otherwise
+# ${CMAKE_BUILD_TYPE}.
+# This macro is used by all Check*.cmake files which use
+# TRY_COMPILE() or TRY_RUN() and support CMAKE_REQUIRED_LIBRARIES , so that
+# these checks support imported targets in CMAKE_REQUIRED_LIBRARIES:
+#    cmake_expand_imported_targets(expandedLibs LIBRARIES ${CMAKE_REQUIRED_LIBRARIES}
+#                                               CONFIGURATION "${CMAKE_TRY_COMPILE_CONFIGURATION}" )
+
+
+#=============================================================================
+# Copyright 2012 Kitware, Inc.
+# Copyright 2009-2012 Alexander Neundorf <[email protected]>
+#
+# Distributed under the OSI-approved BSD License (the "License");
+# see accompanying file Copyright.txt for details.
+#
+# This software is distributed WITHOUT ANY WARRANTY; without even the
+# implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
+# See the License for more information.
+#=============================================================================
+# (To distribute this file outside of CMake, substitute the full
+#  License text for the above reference.)
+
+include(CMakeParseArguments)
+
+function(CMAKE_EXPAND_IMPORTED_TARGETS _RESULT )
+
+   set(options )
+   set(oneValueArgs CONFIGURATION )
+   set(multiValueArgs LIBRARIES )
+
+   cmake_parse_arguments(CEIT "${options}" "${oneValueArgs}" "${multiValueArgs}"  ${ARGN})
+
+   if(CEIT_UNPARSED_ARGUMENTS)
+      message(FATAL_ERROR "Unknown keywords given to CMAKE_EXPAND_IMPORTED_TARGETS(): \"${CEIT_UNPARSED_ARGUMENTS}\"")
+   endif()
+
+   if(NOT CEIT_CONFIGURATION)
+      if(CMAKE_CONFIGURATION_TYPES)
+         list(GET CMAKE_CONFIGURATION_TYPES 0 CEIT_CONFIGURATION)
+      else()
+         set(CEIT_CONFIGURATION ${CMAKE_BUILD_TYPE})
+      endif()
+   endif()
+
+   # handle imported library targets
+
+   set(_CCSR_REQ_LIBS ${CEIT_LIBRARIES})
+
+   set(_CHECK_FOR_IMPORTED_TARGETS TRUE)
+   set(_CCSR_LOOP_COUNTER 0)
+   while(_CHECK_FOR_IMPORTED_TARGETS)
+      math(EXPR _CCSR_LOOP_COUNTER "${_CCSR_LOOP_COUNTER} + 1 ")
+      set(_CCSR_NEW_REQ_LIBS )
+      set(_CHECK_FOR_IMPORTED_TARGETS FALSE)
+      foreach(_CURRENT_LIB ${_CCSR_REQ_LIBS})
+         get_target_property(_importedConfigs "${_CURRENT_LIB}" IMPORTED_CONFIGURATIONS)
+         if (_importedConfigs)
+#            message(STATUS "Detected imported target ${_CURRENT_LIB}")
+            # Ok, so this is an imported target.
+            # First we get the imported configurations.
+            # Then we get the location of the actual library on disk of the first configuration.
+            # then we'll get its link interface libraries property,
+            # iterate through it and replace all imported targets we find there
+            # with there actual location.
+
+            # guard against infinite loop: abort after 100 iterations ( 100 is arbitrary chosen)
+            if ("${_CCSR_LOOP_COUNTER}" LESS 100)
+               set(_CHECK_FOR_IMPORTED_TARGETS TRUE)
+#                else ("${_CCSR_LOOP_COUNTER}" LESS 1)
+#                   message(STATUS "********* aborting loop, counter : ${_CCSR_LOOP_COUNTER}")
+            endif ("${_CCSR_LOOP_COUNTER}" LESS 100)
+
+            # if one of the imported configurations equals ${CMAKE_TRY_COMPILE_CONFIGURATION},
+            # use it, otherwise simply use the first one:
+            list(FIND _importedConfigs "${CEIT_CONFIGURATION}" _configIndexToUse)
+            if("${_configIndexToUse}" EQUAL -1)
+              set(_configIndexToUse 0)
+            endif("${_configIndexToUse}" EQUAL -1)
+            list(GET _importedConfigs ${_configIndexToUse} _importedConfigToUse)
+
+            get_target_property(_importedLocation "${_CURRENT_LIB}" IMPORTED_LOCATION_${_importedConfigToUse})
+            get_target_property(_linkInterfaceLibs "${_CURRENT_LIB}" IMPORTED_LINK_INTERFACE_LIBRARIES_${_importedConfigToUse} )
+
+            list(APPEND _CCSR_NEW_REQ_LIBS  "${_importedLocation}")
+#            message(STATUS "Appending lib ${_CURRENT_LIB} as ${_importedLocation}")
+            if(_linkInterfaceLibs)
+               foreach(_currentLinkInterfaceLib ${_linkInterfaceLibs})
+#                  message(STATUS "Appending link interface lib ${_currentLinkInterfaceLib}")
+                  if(_currentLinkInterfaceLib)
+                     list(APPEND _CCSR_NEW_REQ_LIBS "${_currentLinkInterfaceLib}" )
+                  endif(_currentLinkInterfaceLib)
+               endforeach(_currentLinkInterfaceLib "${_linkInterfaceLibs}")
+            endif(_linkInterfaceLibs)
+         else(_importedConfigs)
+            # "Normal" libraries are just used as they are.
+            list(APPEND _CCSR_NEW_REQ_LIBS "${_CURRENT_LIB}" )
+#            message(STATUS "Appending lib directly: ${_CURRENT_LIB}")
+         endif(_importedConfigs)
+      endforeach(_CURRENT_LIB ${_CCSR_REQ_LIBS})
+
+      set(_CCSR_REQ_LIBS ${_CCSR_NEW_REQ_LIBS} )
+   endwhile(_CHECK_FOR_IMPORTED_TARGETS)
+
+   # Finally we iterate once more over all libraries. This loop only removes
+   # all remaining imported target names (there shouldn't be any left anyway).
+   set(_CCSR_NEW_REQ_LIBS )
+   foreach(_CURRENT_LIB ${_CCSR_REQ_LIBS})
+      get_target_property(_importedConfigs "${_CURRENT_LIB}" IMPORTED_CONFIGURATIONS)
+      if (NOT _importedConfigs)
+         list(APPEND _CCSR_NEW_REQ_LIBS "${_CURRENT_LIB}" )
+#         message(STATUS "final: appending ${_CURRENT_LIB}")
+      else (NOT _importedConfigs)
+#             message(STATUS "final: skipping ${_CURRENT_LIB}")
+      endif (NOT _importedConfigs)
+   endforeach(_CURRENT_LIB ${_CCSR_REQ_LIBS})
+#   message(STATUS "setting -${_RESULT}- to -${_CCSR_NEW_REQ_LIBS}-")
+   set(${_RESULT} "${_CCSR_NEW_REQ_LIBS}" PARENT_SCOPE)
+
+endfunction()

+ 6 - 1
Modules/CheckCSourceCompiles.cmake

@@ -24,6 +24,9 @@
 # (To distribute this file outside of CMake, substitute the full
 #  License text for the above reference.)
 
+INCLUDE("${CMAKE_CURRENT_LIST_DIR}/CMakeExpandImportedTargets.cmake")
+
+
 MACRO(CHECK_C_SOURCE_COMPILES SOURCE VAR)
   IF("${VAR}" MATCHES "^${VAR}$")
     SET(_FAIL_REGEX)
@@ -40,8 +43,10 @@ MACRO(CHECK_C_SOURCE_COMPILES SOURCE VAR)
     SET(MACRO_CHECK_FUNCTION_DEFINITIONS
       "-D${VAR} ${CMAKE_REQUIRED_FLAGS}")
     IF(CMAKE_REQUIRED_LIBRARIES)
+      # this one translates potentially used imported library targets to their files on disk
+      CMAKE_EXPAND_IMPORTED_TARGETS(_ADJUSTED_CMAKE_REQUIRED_LIBRARIES  LIBRARIES  ${CMAKE_REQUIRED_LIBRARIES} CONFIGURATION "${CMAKE_TRY_COMPILE_CONFIGURATION}")
       SET(CHECK_C_SOURCE_COMPILES_ADD_LIBRARIES
-        "-DLINK_LIBRARIES:STRING=${CMAKE_REQUIRED_LIBRARIES}")
+        "-DLINK_LIBRARIES:STRING=${_ADJUSTED_CMAKE_REQUIRED_LIBRARIES}")
     ELSE(CMAKE_REQUIRED_LIBRARIES)
       SET(CHECK_C_SOURCE_COMPILES_ADD_LIBRARIES)
     ENDIF(CMAKE_REQUIRED_LIBRARIES)

+ 6 - 1
Modules/CheckCSourceRuns.cmake

@@ -24,13 +24,18 @@
 # (To distribute this file outside of CMake, substitute the full
 #  License text for the above reference.)
 
+INCLUDE("${CMAKE_CURRENT_LIST_DIR}/CMakeExpandImportedTargets.cmake")
+
+
 MACRO(CHECK_C_SOURCE_RUNS SOURCE VAR)
   IF("${VAR}" MATCHES "^${VAR}$")
     SET(MACRO_CHECK_FUNCTION_DEFINITIONS
       "-D${VAR} ${CMAKE_REQUIRED_FLAGS}")
     IF(CMAKE_REQUIRED_LIBRARIES)
+      # this one translates potentially used imported library targets to their files on disk
+      CMAKE_EXPAND_IMPORTED_TARGETS(_ADJUSTED_CMAKE_REQUIRED_LIBRARIES  LIBRARIES  ${CMAKE_REQUIRED_LIBRARIES} CONFIGURATION "${CMAKE_TRY_COMPILE_CONFIGURATION}")
       SET(CHECK_C_SOURCE_COMPILES_ADD_LIBRARIES
-        "-DLINK_LIBRARIES:STRING=${CMAKE_REQUIRED_LIBRARIES}")
+        "-DLINK_LIBRARIES:STRING=${_ADJUSTED_CMAKE_REQUIRED_LIBRARIES}")
     ELSE(CMAKE_REQUIRED_LIBRARIES)
       SET(CHECK_C_SOURCE_COMPILES_ADD_LIBRARIES)
     ENDIF(CMAKE_REQUIRED_LIBRARIES)

+ 6 - 1
Modules/CheckCXXSourceCompiles.cmake

@@ -24,6 +24,9 @@
 # (To distribute this file outside of CMake, substitute the full
 #  License text for the above reference.)
 
+INCLUDE("${CMAKE_CURRENT_LIST_DIR}/CMakeExpandImportedTargets.cmake")
+
+
 MACRO(CHECK_CXX_SOURCE_COMPILES SOURCE VAR)
   IF("${VAR}" MATCHES "^${VAR}$")
     SET(_FAIL_REGEX)
@@ -41,8 +44,10 @@ MACRO(CHECK_CXX_SOURCE_COMPILES SOURCE VAR)
     SET(MACRO_CHECK_FUNCTION_DEFINITIONS
       "-D${VAR} ${CMAKE_REQUIRED_FLAGS}")
     IF(CMAKE_REQUIRED_LIBRARIES)
+      # this one translates potentially used imported library targets to their files on disk
+      CMAKE_EXPAND_IMPORTED_TARGETS(_ADJUSTED_CMAKE_REQUIRED_LIBRARIES  LIBRARIES  ${CMAKE_REQUIRED_LIBRARIES} CONFIGURATION "${CMAKE_TRY_COMPILE_CONFIGURATION}")
       SET(CHECK_CXX_SOURCE_COMPILES_ADD_LIBRARIES
-        "-DLINK_LIBRARIES:STRING=${CMAKE_REQUIRED_LIBRARIES}")
+        "-DLINK_LIBRARIES:STRING=${_ADJUSTED_CMAKE_REQUIRED_LIBRARIES}")
     ELSE(CMAKE_REQUIRED_LIBRARIES)
       SET(CHECK_CXX_SOURCE_COMPILES_ADD_LIBRARIES)
     ENDIF(CMAKE_REQUIRED_LIBRARIES)

+ 6 - 1
Modules/CheckCXXSourceRuns.cmake

@@ -24,13 +24,18 @@
 # (To distribute this file outside of CMake, substitute the full
 #  License text for the above reference.)
 
+INCLUDE("${CMAKE_CURRENT_LIST_DIR}/CMakeExpandImportedTargets.cmake")
+
+
 MACRO(CHECK_CXX_SOURCE_RUNS SOURCE VAR)
   IF("${VAR}" MATCHES "^${VAR}$")
     SET(MACRO_CHECK_FUNCTION_DEFINITIONS
       "-D${VAR} ${CMAKE_REQUIRED_FLAGS}")
     IF(CMAKE_REQUIRED_LIBRARIES)
+      # this one translates potentially used imported library targets to their files on disk
+      CMAKE_EXPAND_IMPORTED_TARGETS(_ADJUSTED_CMAKE_REQUIRED_LIBRARIES  LIBRARIES  ${CMAKE_REQUIRED_LIBRARIES} CONFIGURATION "${CMAKE_TRY_COMPILE_CONFIGURATION}")
       SET(CHECK_CXX_SOURCE_COMPILES_ADD_LIBRARIES
-        "-DLINK_LIBRARIES:STRING=${CMAKE_REQUIRED_LIBRARIES}")
+        "-DLINK_LIBRARIES:STRING=${_ADJUSTED_CMAKE_REQUIRED_LIBRARIES}")
     ELSE(CMAKE_REQUIRED_LIBRARIES)
       SET(CHECK_CXX_SOURCE_COMPILES_ADD_LIBRARIES)
     ENDIF(CMAKE_REQUIRED_LIBRARIES)

+ 6 - 1
Modules/CheckFortranFunctionExists.cmake

@@ -22,12 +22,17 @@
 # (To distribute this file outside of CMake, substitute the full
 #  License text for the above reference.)
 
+INCLUDE("${CMAKE_CURRENT_LIST_DIR}/CMakeExpandImportedTargets.cmake")
+
+
 macro(CHECK_FORTRAN_FUNCTION_EXISTS FUNCTION VARIABLE)
   if(NOT DEFINED ${VARIABLE})
     message(STATUS "Looking for Fortran ${FUNCTION}")
     if(CMAKE_REQUIRED_LIBRARIES)
+      # this one translates potentially used imported library targets to their files on disk
+      cmake_expand_imported_targets(_ADJUSTED_CMAKE_REQUIRED_LIBRARIES  LIBRARIES  ${CMAKE_REQUIRED_LIBRARIES} CONFIGURATION "${CMAKE_TRY_COMPILE_CONFIGURATION}")
       set(CHECK_FUNCTION_EXISTS_ADD_LIBRARIES
-        "-DLINK_LIBRARIES:STRING=${CMAKE_REQUIRED_LIBRARIES}")
+        "-DLINK_LIBRARIES:STRING=${_ADJUSTED_CMAKE_REQUIRED_LIBRARIES}")
     else(CMAKE_REQUIRED_LIBRARIES)
       set(CHECK_FUNCTION_EXISTS_ADD_LIBRARIES)
     endif(CMAKE_REQUIRED_LIBRARIES)

+ 6 - 1
Modules/CheckFunctionExists.cmake

@@ -27,14 +27,19 @@
 # (To distribute this file outside of CMake, substitute the full
 #  License text for the above reference.)
 
+INCLUDE("${CMAKE_CURRENT_LIST_DIR}/CMakeExpandImportedTargets.cmake")
+
+
 MACRO(CHECK_FUNCTION_EXISTS FUNCTION VARIABLE)
   IF("${VARIABLE}" MATCHES "^${VARIABLE}$")
     SET(MACRO_CHECK_FUNCTION_DEFINITIONS
       "-DCHECK_FUNCTION_EXISTS=${FUNCTION} ${CMAKE_REQUIRED_FLAGS}")
     MESSAGE(STATUS "Looking for ${FUNCTION}")
     IF(CMAKE_REQUIRED_LIBRARIES)
+      # this one translates potentially used imported library targets to their files on disk
+      CMAKE_EXPAND_IMPORTED_TARGETS(_ADJUSTED_CMAKE_REQUIRED_LIBRARIES  LIBRARIES  ${CMAKE_REQUIRED_LIBRARIES} CONFIGURATION "${CMAKE_TRY_COMPILE_CONFIGURATION}")
       SET(CHECK_FUNCTION_EXISTS_ADD_LIBRARIES
-        "-DLINK_LIBRARIES:STRING=${CMAKE_REQUIRED_LIBRARIES}")
+        "-DLINK_LIBRARIES:STRING=${_ADJUSTED_CMAKE_REQUIRED_LIBRARIES}")
     ELSE(CMAKE_REQUIRED_LIBRARIES)
       SET(CHECK_FUNCTION_EXISTS_ADD_LIBRARIES)
     ENDIF(CMAKE_REQUIRED_LIBRARIES)

+ 6 - 1
Modules/CheckLibraryExists.cmake

@@ -26,6 +26,9 @@
 # (To distribute this file outside of CMake, substitute the full
 #  License text for the above reference.)
 
+INCLUDE("${CMAKE_CURRENT_LIST_DIR}/CMakeExpandImportedTargets.cmake")
+
+
 MACRO(CHECK_LIBRARY_EXISTS LIBRARY FUNCTION LOCATION VARIABLE)
   IF("${VARIABLE}" MATCHES "^${VARIABLE}$")
     SET(MACRO_CHECK_LIBRARY_EXISTS_DEFINITION
@@ -33,8 +36,10 @@ MACRO(CHECK_LIBRARY_EXISTS LIBRARY FUNCTION LOCATION VARIABLE)
     MESSAGE(STATUS "Looking for ${FUNCTION} in ${LIBRARY}")
     SET(CHECK_LIBRARY_EXISTS_LIBRARIES ${LIBRARY})
     IF(CMAKE_REQUIRED_LIBRARIES)
+      # this one translates potentially used imported library targets to their files on disk
+      CMAKE_EXPAND_IMPORTED_TARGETS(_ADJUSTED_CMAKE_REQUIRED_LIBRARIES  LIBRARIES  ${CMAKE_REQUIRED_LIBRARIES} CONFIGURATION "${CMAKE_TRY_COMPILE_CONFIGURATION}")
       SET(CHECK_LIBRARY_EXISTS_LIBRARIES
-        ${CHECK_LIBRARY_EXISTS_LIBRARIES} ${CMAKE_REQUIRED_LIBRARIES})
+        ${CHECK_LIBRARY_EXISTS_LIBRARIES} ${_ADJUSTED_CMAKE_REQUIRED_LIBRARIES})
     ENDIF(CMAKE_REQUIRED_LIBRARIES)
     TRY_COMPILE(${VARIABLE}
       ${CMAKE_BINARY_DIR}

+ 6 - 1
Modules/CheckPrototypeDefinition.cmake

@@ -34,8 +34,11 @@
 #  License text for the above reference.)
 #
 
+include("${CMAKE_CURRENT_LIST_DIR}/CMakeExpandImportedTargets.cmake")
+
 get_filename_component(__check_proto_def_dir "${CMAKE_CURRENT_LIST_FILE}" PATH)
 
+
 function(CHECK_PROTOTYPE_DEFINITION _FUNCTION _PROTOTYPE _RETURN _HEADER _VARIABLE)
 
   if ("${_VARIABLE}" MATCHES "^${_VARIABLE}$")
@@ -43,8 +46,10 @@ function(CHECK_PROTOTYPE_DEFINITION _FUNCTION _PROTOTYPE _RETURN _HEADER _VARIAB
 
     set(CHECK_PROTOTYPE_DEFINITION_FLAGS ${CMAKE_REQUIRED_FLAGS})
     if (CMAKE_REQUIRED_LIBRARIES)
+      # this one translates potentially used imported library targets to their files on disk
+      cmake_expand_imported_targets(_ADJUSTED_CMAKE_REQUIRED_LIBRARIES  LIBRARIES  ${CMAKE_REQUIRED_LIBRARIES} CONFIGURATION "${CMAKE_TRY_COMPILE_CONFIGURATION}")
       set(CHECK_PROTOTYPE_DEFINITION_LIBS
-        "-DLINK_LIBRARIES:STRING=${CMAKE_REQUIRED_LIBRARIES}")
+        "-DLINK_LIBRARIES:STRING=${_ADJUSTED_CMAKE_REQUIRED_LIBRARIES}")
     else(CMAKE_REQUIRED_LIBRARIES)
       set(CHECK_PROTOTYPE_DEFINITION_LIBS)
     endif(CMAKE_REQUIRED_LIBRARIES)

+ 6 - 1
Modules/CheckSymbolExists.cmake

@@ -35,6 +35,9 @@
 # (To distribute this file outside of CMake, substitute the full
 #  License text for the above reference.)
 
+INCLUDE("${CMAKE_CURRENT_LIST_DIR}/CMakeExpandImportedTargets.cmake")
+
+
 MACRO(CHECK_SYMBOL_EXISTS SYMBOL FILES VARIABLE)
   _CHECK_SYMBOL_EXISTS("${CMAKE_BINARY_DIR}${CMAKE_FILES_DIRECTORY}/CMakeTmp/CheckSymbolExists.c" "${SYMBOL}" "${FILES}" "${VARIABLE}" )
 ENDMACRO(CHECK_SYMBOL_EXISTS)
@@ -44,8 +47,10 @@ MACRO(_CHECK_SYMBOL_EXISTS SOURCEFILE SYMBOL FILES VARIABLE)
     SET(CMAKE_CONFIGURABLE_FILE_CONTENT "/* */\n")
     SET(MACRO_CHECK_SYMBOL_EXISTS_FLAGS ${CMAKE_REQUIRED_FLAGS})
     IF(CMAKE_REQUIRED_LIBRARIES)
+      # this one translates potentially used imported library targets to their files on disk
+      CMAKE_EXPAND_IMPORTED_TARGETS(_ADJUSTED_CMAKE_REQUIRED_LIBRARIES  LIBRARIES  ${CMAKE_REQUIRED_LIBRARIES} CONFIGURATION "${CMAKE_TRY_COMPILE_CONFIGURATION}")
       SET(CHECK_SYMBOL_EXISTS_LIBS
-        "-DLINK_LIBRARIES:STRING=${CMAKE_REQUIRED_LIBRARIES}")
+        "-DLINK_LIBRARIES:STRING=${_ADJUSTED_CMAKE_REQUIRED_LIBRARIES}")
     ELSE(CMAKE_REQUIRED_LIBRARIES)
       SET(CHECK_SYMBOL_EXISTS_LIBS)
     ENDIF(CMAKE_REQUIRED_LIBRARIES)

+ 6 - 1
Modules/CheckTypeSize.cmake

@@ -47,6 +47,7 @@
 #  License text for the above reference.)
 
 include(CheckIncludeFile)
+include("${CMAKE_CURRENT_LIST_DIR}/CMakeExpandImportedTargets.cmake")
 
 cmake_policy(PUSH)
 cmake_minimum_required(VERSION 2.6 FATAL_ERROR)
@@ -76,6 +77,10 @@ function(__check_type_size_impl type var map builtin)
   endforeach()
 
   # Perform the check.
+
+  # this one translates potentially used imported library targets to their files on disk
+  cmake_expand_imported_targets(_ADJUSTED_CMAKE_REQUIRED_LIBRARIES  LIBRARIES  ${CMAKE_REQUIRED_LIBRARIES} CONFIGURATION "${CMAKE_TRY_COMPILE_CONFIGURATION}")
+
   set(src ${CMAKE_BINARY_DIR}${CMAKE_FILES_DIRECTORY}/CheckTypeSize/${var}.c)
   set(bin ${CMAKE_BINARY_DIR}${CMAKE_FILES_DIRECTORY}/CheckTypeSize/${var}.bin)
   configure_file(${__check_type_size_dir}/CheckTypeSize.c.in ${src} @ONLY)
@@ -84,7 +89,7 @@ function(__check_type_size_impl type var map builtin)
     CMAKE_FLAGS
       "-DCOMPILE_DEFINITIONS:STRING=${CMAKE_REQUIRED_FLAGS}"
       "-DINCLUDE_DIRECTORIES:STRING=${CMAKE_REQUIRED_INCLUDES}"
-      "-DLINK_LIBRARIES:STRING=${CMAKE_REQUIRED_LIBRARIES}"
+      "-DLINK_LIBRARIES:STRING=${_ADJUSTED_CMAKE_REQUIRED_LIBRARIES}"
     OUTPUT_VARIABLE output
     COPY_FILE ${bin}
     )

+ 6 - 1
Modules/CheckVariableExists.cmake

@@ -26,14 +26,19 @@
 # (To distribute this file outside of CMake, substitute the full
 #  License text for the above reference.)
 
+INCLUDE("${CMAKE_CURRENT_LIST_DIR}/CMakeExpandImportedTargets.cmake")
+
+
 MACRO(CHECK_VARIABLE_EXISTS VAR VARIABLE)
   IF("${VARIABLE}" MATCHES "^${VARIABLE}$")
     SET(MACRO_CHECK_VARIABLE_DEFINITIONS
       "-DCHECK_VARIABLE_EXISTS=${VAR} ${CMAKE_REQUIRED_FLAGS}")
     MESSAGE(STATUS "Looking for ${VAR}")
     IF(CMAKE_REQUIRED_LIBRARIES)
+      # this one translates potentially used imported library targets to their files on disk
+      CMAKE_EXPAND_IMPORTED_TARGETS(_ADJUSTED_CMAKE_REQUIRED_LIBRARIES  LIBRARIES  ${CMAKE_REQUIRED_LIBRARIES} CONFIGURATION "${CMAKE_TRY_COMPILE_CONFIGURATION}")
       SET(CHECK_VARIABLE_EXISTS_ADD_LIBRARIES
-        "-DLINK_LIBRARIES:STRING=${CMAKE_REQUIRED_LIBRARIES}")
+        "-DLINK_LIBRARIES:STRING=${_ADJUSTED_CMAKE_REQUIRED_LIBRARIES}")
     ELSE(CMAKE_REQUIRED_LIBRARIES)
       SET(CHECK_VARIABLE_EXISTS_ADD_LIBRARIES)
     ENDIF(CMAKE_REQUIRED_LIBRARIES)

+ 11 - 0
Tests/ExportImport/Import/A/CMakeLists.txt

@@ -137,3 +137,14 @@ add_library(imp_lib1 STATIC imp_lib1.c)
 target_link_libraries(imp_lib1 exp_testLib2)
 add_library(imp_lib1b STATIC imp_lib1.c)
 target_link_libraries(imp_lib1b bld_testLib2)
+
+#-----------------------------------------------------------------------------
+# Test that handling imported targets, including transitive dependencies,
+# works in CheckFunctionExists (...and hopefully all other try_compile() checks
+include(CheckFunctionExists)
+unset(HAVE_TESTLIB1_FUNCTION CACHE)
+set(CMAKE_REQUIRED_LIBRARIES exp_testLib2)
+check_function_exists(testLib1 HAVE_TESTLIB1_FUNCTION)
+if (NOT HAVE_TESTLIB1_FUNCTION)
+  message(SEND_ERROR "Using imported target testLib2 in check_function_exists() failed !")
+endif()