Skip to content

Commit 818b566

Browse files
authored
Merge pull request #3406 from gottesmm/add-support-for-module-library-imported-targets
[cmake] Add support for importing MODULE_LIBRARY targets and clean th…
2 parents 898aa3e + d004abc commit 818b566

File tree

1 file changed

+71
-31
lines changed

1 file changed

+71
-31
lines changed

cmake/modules/SwiftSharedCMakeConfig.cmake

Lines changed: 71 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -49,41 +49,81 @@ function(get_imported_library_prefix outvar target prefix)
4949
endif()
5050
endfunction()
5151

52-
# What this function does is go through each of the passed in imported targets
53-
# from LLVM/Clang and changes them to use the appropriate fully expanded paths.
54-
#
55-
# TODO: This function needs a better name.
56-
function(fix_imported_target_locations_for_xcode targets)
57-
foreach(t ${targets})
58-
if (NOT TARGET ${t})
59-
message(FATAL_ERROR "${t} is not a target?!")
60-
endif()
52+
function(check_imported_target_has_imported_configuration target config)
53+
get_target_property(IMPORTED_CONFIGS_LIST ${target} IMPORTED_CONFIGURATIONS)
54+
if ("${IMPORTED_CONFIGS_LIST}" STREQUAL "NOTFOUND")
55+
message(FATAL_ERROR "No import configuration of ${target} specified?!")
56+
endif()
6157

62-
get_target_property(TARGET_TYPE ${t} TYPE)
58+
list(FIND "${IMPORTED_CONFIGS_LIST}" "${config}" FOUND_CONFIG)
59+
if (NOT FOUND_CONFIG)
60+
message(FATAL_ERROR "${target} does not have imported config '${config}'?! \
61+
Instead: ${IMPORTED_CONFIGS_LIST}")
62+
endif()
63+
endfunction()
6364

64-
if (NOT "${TARGET_TYPE}" STREQUAL "STATIC_LIBRARY" AND
65-
NOT "${TARGET_TYPE}" STREQUAL "SHARED_LIBRARY" AND
66-
NOT "${TARGET_TYPE}" STREQUAL "EXECUTABLE")
67-
message(FATAL_ERROR "Unsupported imported target type ${TARGET_TYPE}")
68-
endif()
65+
function(fixup_imported_target_property_for_xcode target property real_build_type)
66+
set(FULL_PROP_NAME "${property}_${real_build_type}")
67+
68+
# First try to lookup the value associated with the "real build type".
69+
get_target_property(PROP_VALUE ${target} ${FULL_PROP_NAME})
6970

70-
# Ok, so at this point, we know that we have an executable, static library,
71-
# or shared library.
72-
#
73-
# First we handle the executables.
74-
if ("${TARGET_TYPE}" STREQUAL "EXECUTABLE")
75-
set_target_properties(${t} PROPERTIES
76-
IMPORTED_LOCATION_DEBUG "${LLVM_BINARY_OUTPUT_INTDIR}/${t}"
77-
IMPORTED_LOCATION_RELEASE "${LLVM_BINARY_OUTPUT_INTDIR}/${t}")
71+
# If the property is unspecified, return.
72+
if ("${PROP_VALUE}" STREQUAL "NOTFOUND")
73+
return()
74+
endif()
75+
76+
# Otherwise for each cmake configuration that is not real_build_type, hardcode
77+
# its value to be PROP_VALUE.
78+
foreach(c ${CMAKE_CONFIGURATION_TYPES})
79+
if ("${c}" STREQUAL "${real_build_type}")
7880
continue()
7981
endif()
82+
set_target_properties(${target} PROPERTIES ${FULL_PROP_NAME} "${PROP_VALUE}")
83+
endforeach()
84+
endfunction()
85+
86+
# When building with Xcode, we only support compiling against the LLVM
87+
# configuration that was specified by build-script. This becomes a problem since
88+
# if we compile LLVM-Release and Swift-Debug, Swift is going to look in the
89+
# Debug, not the Release folder for LLVM's code and thus will be compiling
90+
# against an unintended set of libraries, if those libraries exist at all.
91+
#
92+
# Luckily, via LLVMConfig.cmake, we know the configuration that LLVM was
93+
# compiled in, so we can grab the imported location for that configuration and
94+
# splat it across the other configurations as well.
95+
function(fix_imported_targets_for_xcode imported_targets)
96+
# This is the set of configuration specific cmake properties that are
97+
# supported for imported targets in cmake 3.4.3. Sadly, beyond hacks, it seems
98+
# that there is no way to dynamically query the list of set properties of a
99+
# target.
100+
#
101+
# *NOTE* In fixup_imported_target_property_for_xcode, we add the _${CONFIG}
102+
# *suffix.
103+
set(imported_target_properties
104+
IMPORTED_IMPLIB
105+
IMPORTED_LINK_DEPENDENT_LIBRARIES
106+
IMPORTED_LINK_INTERFACE_LANGUAGES
107+
IMPORTED_LINK_INTERFACE_LIBRARIES
108+
IMPORTED_LINK_INTERFACE_MULTIPLICITY
109+
IMPORTED_LOCATION
110+
IMPORTED_NO_SONAME
111+
IMPORTED_SONAME)
112+
113+
foreach(target ${imported_targets})
114+
if (NOT TARGET ${target})
115+
message(FATAL_ERROR "${target} is not a target?!")
116+
endif()
117+
118+
# First check that we actually imported the configuration that LLVM said
119+
# that we did. This is just a sanity check.
120+
check_imported_target_has_imported_configuration(${target} ${LLVM_BUILD_TYPE})
80121

81-
# Otherwise, we have libraries. Since libclang has an extra lib prefix in
82-
# its target name, we have to handle it specially.
83-
get_imported_library_prefix(PREFIX "${t}" "${CMAKE_${TARGET_TYPE}_PREFIX}")
84-
set_target_properties(${t} PROPERTIES
85-
IMPORTED_LOCATION_DEBUG "${LLVM_LIBRARY_OUTPUT_INTDIR}/${PREFIX}${t}.${CMAKE_${TARGET_TYPE}_SUFFIX}"
86-
IMPORTED_LOCATION_RELEASE "${LLVM_LIBRARY_OUTPUT_INTDIR}/${PREFIX}${t}.${CMAKE_${TARGET_TYPE}_SUFFIX}")
122+
# Then loop through all of the imported properties and translate.
123+
foreach(property ${imported_properties})
124+
fixup_imported_target_property_for_xcode(
125+
${target} ${property} ${LLVM_BUILD_TYPE})
126+
endforeach()
87127
endforeach()
88128
endfunction()
89129

@@ -147,7 +187,7 @@ macro(swift_common_standalone_build_config_llvm product is_cross_compiling)
147187
set(LLVM_LIBRARY_OUTPUT_INTDIR "${LLVM_LIBRARY_DIR}")
148188

149189
if (XCODE)
150-
fix_imported_target_locations_for_xcode("${LLVM_EXPORTED_TARGETS}")
190+
fix_imported_targets_for_xcode("${LLVM_EXPORTED_TARGETS}")
151191
endif()
152192

153193
if(NOT ${is_cross_compiling})
@@ -240,7 +280,7 @@ macro(swift_common_standalone_build_config_clang product is_cross_compiling)
240280
set(CLANG_MAIN_INCLUDE_DIR "${CLANG_MAIN_SRC_DIR}/include")
241281

242282
if (XCODE)
243-
fix_imported_target_locations_for_xcode("${CLANG_EXPORTED_TARGETS}")
283+
fix_imported_targets_for_xcode("${CLANG_EXPORTED_TARGETS}")
244284
endif()
245285

246286
include_directories("${CLANG_BUILD_INCLUDE_DIR}"

0 commit comments

Comments
 (0)