@@ -18,6 +18,136 @@ function(add_sourcekit_default_compiler_flags target)
18
18
-fblocks )
19
19
endfunction ()
20
20
21
+ function (add_sourcekitd_swifrt_linking target path HAS_SWIFT_MODULES )
22
+ set (RPATH_LIST )
23
+ if (${SWIFT_HOST_VARIANT_SDK} IN_LIST SWIFT_DARWIN_PLATFORMS )
24
+
25
+ # Lists of rpaths that we are going to add to our executables.
26
+ #
27
+ # Please add each rpath separately below to the list, explaining why you are
28
+ # adding it.
29
+ set (sdk_dir "${SWIFT_SDK_${SWIFT_HOST_VARIANT_SDK} _ARCH_${SWIFT_HOST_VARIANT_ARCH} _PATH}/usr/lib/swift" )
30
+
31
+ # If we found a swift compiler and are going to use swift code in swift
32
+ # host side tools but link with clang, add the appropriate -L paths so we
33
+ # find all of the necessary swift libraries on Darwin.
34
+ if (HAS_SWIFT_MODULES AND BOOTSTRAPPING_MODE )
35
+
36
+ if (BOOTSTRAPPING_MODE STREQUAL "HOSTTOOLS" )
37
+ # Add in the toolchain directory so we can grab compatibility libraries
38
+ get_filename_component (TOOLCHAIN_BIN_DIR ${SWIFT_EXEC_FOR_SWIFT_MODULES} DIRECTORY )
39
+ get_filename_component (TOOLCHAIN_LIB_DIR "${TOOLCHAIN_BIN_DIR} /../lib/swift/macosx" ABSOLUTE )
40
+ target_link_directories (${target} PUBLIC ${TOOLCHAIN_LIB_DIR} )
41
+
42
+ # Add the SDK directory for the host platform.
43
+ target_link_directories (${target} PRIVATE "${sdk_dir} " )
44
+
45
+ # Include the abi stable system stdlib in our rpath.
46
+ list (APPEND RPATH_LIST "/usr/lib/swift" )
47
+
48
+ elseif (BOOTSTRAPPING_MODE STREQUAL "CROSSCOMPILE-WITH-HOSTLIBS" )
49
+
50
+ # Intentinally don't add the lib dir of the cross-compiled compiler, so that
51
+ # the stdlib is not picked up from there, but from the SDK.
52
+ # This requires to explicitly add all the needed compatibility libraries. We
53
+ # can take them from the current build.
54
+ set (vsuffix "-${SWIFT_SDK_${SWIFT_HOST_VARIANT_SDK} _LIB_SUBDIR}-${SWIFT_HOST_VARIANT_ARCH} " )
55
+ set (conctarget "swiftCompatibilityConcurrency${vsuffix} " )
56
+ target_link_libraries (${target} PUBLIC ${conctarget} )
57
+
58
+ # Add the SDK directory for the host platform.
59
+ target_link_directories (${target} PRIVATE "${sdk_dir} " )
60
+
61
+ # Include the abi stable system stdlib in our rpath.
62
+ list (APPEND RPATH_LIST "/usr/lib/swift" )
63
+
64
+ elseif (BOOTSTRAPPING_MODE STREQUAL "BOOTSTRAPPING-WITH-HOSTLIBS" )
65
+ # Add the SDK directory for the host platform.
66
+ target_link_directories (${target} PRIVATE "${sdk_dir} " )
67
+
68
+ # A backup in case the toolchain doesn't have one of the compatibility libraries.
69
+ target_link_directories (${target} PRIVATE
70
+ "${SWIFTLIB_DIR} /${SWIFT_SDK_${SWIFT_HOST_VARIANT_SDK} _LIB_SUBDIR}" )
71
+
72
+ # Include the abi stable system stdlib in our rpath.
73
+ list (APPEND RPATH_LIST "/usr/lib/swift" )
74
+
75
+ elseif (BOOTSTRAPPING_MODE STREQUAL "BOOTSTRAPPING" )
76
+ # At build time link against the built swift libraries from the
77
+ # previous bootstrapping stage.
78
+ get_bootstrapping_swift_lib_dir (bs_lib_dir "" )
79
+ target_link_directories (${target} PRIVATE ${bs_lib_dir} )
80
+
81
+ # Required to pick up the built libswiftCompatibility<n>.a libraries
82
+ target_link_directories (${target} PRIVATE
83
+ "${SWIFTLIB_DIR} /${SWIFT_SDK_${SWIFT_HOST_VARIANT_SDK} _LIB_SUBDIR}" )
84
+
85
+ # At runtime link against the built swift libraries from the current
86
+ # bootstrapping stage.
87
+ file (RELATIVE_PATH relative_rtlib_path "${path} " "${SWIFTLIB_DIR} /${SWIFT_SDK_${SWIFT_HOST_VARIANT_SDK} _LIB_SUBDIR}" )
88
+ list (APPEND RPATH_LIST "@loader_path/${relative_rtlib_path} " )
89
+ else ()
90
+ message (FATAL_ERROR "Unknown BOOTSTRAPPING_MODE '${BOOTSTRAPPING_MODE} '" )
91
+ endif ()
92
+
93
+ # Workaround to make lldb happy: we have to explicitly add all swift compiler modules
94
+ # to the linker command line.
95
+ set (swift_ast_path_flags "-Wl" )
96
+ get_property (modules GLOBAL PROPERTY swift_compiler_modules )
97
+ foreach (module ${modules} )
98
+ get_target_property (module_file "SwiftModule${module} " "module_file" )
99
+ string (APPEND swift_ast_path_flags ",-add_ast_path,${module_file} " )
100
+ endforeach ()
101
+
102
+ set_property (TARGET ${target} APPEND_STRING PROPERTY
103
+ LINK_FLAGS " ${swift_ast_path_flags} " )
104
+
105
+ # Workaround for a linker crash related to autolinking: rdar://77839981
106
+ set_property (TARGET ${target} APPEND_STRING PROPERTY
107
+ LINK_FLAGS " -lobjc " )
108
+
109
+ endif () # HAS_SWIFT_MODULES AND BOOTSTRAPPING_MODE
110
+
111
+ elseif (SWIFT_HOST_VARIANT_SDK MATCHES "LINUX|ANDROID|OPENBSD" AND HAS_SWIFT_MODULES AND BOOTSTRAPPING_MODE )
112
+ set (swiftrt "swiftImageRegistrationObject${SWIFT_SDK_${SWIFT_HOST_VARIANT_SDK} _OBJECT_FORMAT}-${SWIFT_SDK_${SWIFT_HOST_VARIANT_SDK} _LIB_SUBDIR}-${SWIFT_HOST_VARIANT_ARCH} " )
113
+ if (${BOOTSTRAPPING_MODE} MATCHES "HOSTTOOLS|CROSSCOMPILE" )
114
+ # At build time and and run time, link against the swift libraries in the
115
+ # installed host toolchain.
116
+ get_filename_component (swift_bin_dir ${SWIFT_EXEC_FOR_SWIFT_MODULES} DIRECTORY )
117
+ get_filename_component (swift_dir ${swift_bin_dir} DIRECTORY )
118
+ set (host_lib_dir "${swift_dir} /lib/swift/${SWIFT_SDK_${SWIFT_HOST_VARIANT_SDK} _LIB_SUBDIR}" )
119
+
120
+ target_link_libraries (${target} PRIVATE ${swiftrt} )
121
+ target_link_libraries (${target} PRIVATE "swiftCore" )
122
+
123
+ target_link_directories (${target} PRIVATE ${host_lib_dir} )
124
+ if (BOOTSTRAPPING_MODE STREQUAL "HOSTTOOLS" )
125
+ list (APPEND RPATH_LIST "${host_lib_dir} " )
126
+ else ()
127
+ file (RELATIVE_PATH relative_rtlib_path "${path} " "${SWIFTLIB_DIR} /${SWIFT_SDK_${SWIFT_HOST_VARIANT_SDK} _LIB_SUBDIR}" )
128
+ list (APPEND RPATH_LIST "$ORIGIN/${relative_rtlib_path} " )
129
+ endif ()
130
+
131
+ elseif (BOOTSTRAPPING_MODE STREQUAL "BOOTSTRAPPING" )
132
+ get_bootstrapping_swift_lib_dir (bs_lib_dir "" )
133
+ target_link_directories (${target} PRIVATE ${bs_lib_dir} )
134
+ target_link_libraries (${target} PRIVATE ${swiftrt} )
135
+ target_link_libraries (${target} PRIVATE "swiftCore" )
136
+
137
+ # At runtime link against the built swift libraries from the current
138
+ # bootstrapping stage.
139
+ file (RELATIVE_PATH relative_rtlib_path "${path} " "${SWIFTLIB_DIR} /${SWIFT_SDK_${SWIFT_HOST_VARIANT_SDK} _LIB_SUBDIR}" )
140
+ list (APPEND RPATH_LIST "$ORIGIN/${relative_rtlib_path} " )
141
+
142
+ elseif (BOOTSTRAPPING_MODE STREQUAL "BOOTSTRAPPING-WITH-HOSTLIBS" )
143
+ message (FATAL_ERROR "BOOTSTRAPPING_MODE 'BOOTSTRAPPING-WITH-HOSTLIBS' not supported on Linux" )
144
+ else ()
145
+ message (FATAL_ERROR "Unknown BOOTSTRAPPING_MODE '${BOOTSTRAPPING_MODE} '" )
146
+ endif ()
147
+ endif ()
148
+ set (RPATH_LIST ${RPATH_LIST} PARENT_SCOPE )
149
+ endfunction ()
150
+
21
151
# Add a new SourceKit library.
22
152
#
23
153
# Usage:
@@ -26,10 +156,11 @@ endfunction()
26
156
# [LLVM_LINK_COMPONENTS comp1 ...] # LLVM components this library depends on
27
157
# [INSTALL_IN_COMPONENT comp] # The Swift installation component that this library belongs to.
28
158
# [SHARED]
159
+ # [HAS_SWIFT_MODULES] # Whether to link SwiftCompilerModules
29
160
# source1 [source2 source3 ...]) # Sources to add into this library
30
161
macro (add_sourcekit_library name )
31
162
cmake_parse_arguments (SOURCEKITLIB
32
- "SHARED"
163
+ "SHARED;HAS_SWIFT_MODULES "
33
164
"INSTALL_IN_COMPONENT"
34
165
"HEADERS;DEPENDS;LLVM_LINK_COMPONENTS"
35
166
${ARGN} )
@@ -89,25 +220,16 @@ macro(add_sourcekit_library name)
89
220
add_llvm_symbol_exports (${name} ${EXPORTED_SYMBOL_FILE} )
90
221
endif ()
91
222
92
- if ("${CMAKE_SYSTEM_NAME} " STREQUAL "Darwin" )
93
- if (SOURCEKITLIB_SHARED )
94
- set_target_properties (${name} PROPERTIES BUILD_WITH_INSTALL_RPATH TRUE )
95
- set_target_properties (${name} PROPERTIES INSTALL_NAME_DIR "@rpath" )
96
- endif ()
97
- endif ()
98
223
99
- if ("${CMAKE_SYSTEM_NAME} " STREQUAL "Linux" )
100
- if (SOURCEKITLIB_SHARED )
101
- set_target_properties (${name} PROPERTIES BUILD_WITH_INSTALL_RPATH TRUE )
102
- set_target_properties (${name} PROPERTIES INSTALL_RPATH "$ORIGIN/../lib/swift/linux" )
103
- endif ()
104
- endif ()
224
+ if (SOURCEKITLIB_SHARED )
225
+ set (RPATH_LIST )
226
+ add_sourcekitd_swifrt_linking (${name} ${SOURCEKIT_LIBRARY_OUTPUT_INTDIR} ${SOURCEKITLIB_HAS_SWIFT_MODULES} )
105
227
106
- if ("${CMAKE_SYSTEM_NAME} " STREQUAL "Android" )
107
- if (SOURCEKITLIB_SHARED )
108
- set_target_properties (${name} PROPERTIES BUILD_WITH_INSTALL_RPATH TRUE )
109
- set_target_properties (${name} PROPERTIES INSTALL_RPATH "$ORIGIN/../lib/swift/android" )
228
+ if ("${CMAKE_SYSTEM_NAME} " STREQUAL "Darwin" )
229
+ set_target_properties (${name} PROPERTIES INSTALL_NAME_DIR "@rpath" )
110
230
endif ()
231
+ set_target_properties (${name} PROPERTIES BUILD_WITH_INSTALL_RPATH TRUE )
232
+ set_target_properties (${name} PROPERTIES INSTALL_RPATH "${RPATH_LIST} " )
111
233
endif ()
112
234
113
235
if ("${SOURCEKITLIB_INSTALL_IN_COMPONENT} " STREQUAL "" )
@@ -185,10 +307,11 @@ endmacro()
185
307
# [LLVM_LINK_COMPONENTS comp1 ...] # LLVM components this framework depends on
186
308
# [MODULEMAP modulemap] # Module map file for this framework
187
309
# [INSTALL_IN_COMPONENT comp] # The Swift installation component that this framework belongs to.
310
+ # [HAS_SWIFT_MODULES] # Whether to link SwiftCompilerModules
188
311
# source1 [source2 source3 ...]) # Sources to add into this framework
189
312
macro (add_sourcekit_framework name )
190
313
cmake_parse_arguments (SOURCEKITFW
191
- "" "MODULEMAP;INSTALL_IN_COMPONENT" "LLVM_LINK_COMPONENTS" ${ARGN} )
314
+ "HAS_SWIFT_MODULES " "MODULEMAP;INSTALL_IN_COMPONENT" "LLVM_LINK_COMPONENTS" ${ARGN} )
192
315
set (srcs ${SOURCEKITFW_UNPARSED_ARGUMENTS} )
193
316
194
317
set (lib_dir ${SOURCEKIT_LIBRARY_OUTPUT_INTDIR} )
@@ -255,11 +378,14 @@ macro(add_sourcekit_framework name)
255
378
set_output_directory (${name}
256
379
BINARY_DIR ${SOURCEKIT_RUNTIME_OUTPUT_INTDIR}
257
380
LIBRARY_DIR ${SOURCEKIT_LIBRARY_OUTPUT_INTDIR} )
381
+ set (RPATH_LIST )
382
+ add_sourcekitd_swifrt_linking (${name} "${SOURCEKIT_LIBRARY_OUTPUT_INTDIR} " ${SOURCEKITFW_HAS_SWIFT_MODULES} )
258
383
set_target_properties (${name} PROPERTIES
259
384
BUILD_WITH_INSTALL_RPATH TRUE
260
385
FOLDER "SourceKit frameworks"
261
386
FRAMEWORK TRUE
262
387
INSTALL_NAME_DIR "@rpath"
388
+ INSTALL_RPATH "${RPATH_LIST} "
263
389
MACOSX_FRAMEWORK_INFO_PLIST "${SOURCEKIT_SOURCE_DIR} /cmake/MacOSXFrameworkInfo.plist.in"
264
390
MACOSX_FRAMEWORK_IDENTIFIER "com.apple.${name} "
265
391
MACOSX_FRAMEWORK_SHORT_VERSION_STRING "1.0"
@@ -283,10 +409,13 @@ macro(add_sourcekit_framework name)
283
409
set_output_directory (${name}
284
410
BINARY_DIR ${framework_location}
285
411
LIBRARY_DIR ${framework_location} )
412
+ set (RPATH_LIST )
413
+ add_sourcekitd_swifrt_linking (${name} "${framework_location} " SOURCEKITFW_HAS_SWIFT_MODULES RPATH_LIST )
286
414
set_target_properties (${name} PROPERTIES
287
415
BUILD_WITH_INSTALL_RPATH TRUE
288
416
FOLDER "SourceKit frameworks"
289
417
INSTALL_NAME_DIR "@rpath/${name} .framework"
418
+ INSTALL_RPATH "${RPATH_LIST} "
290
419
PREFIX ""
291
420
SUFFIX "" )
292
421
swift_install_in_component (DIRECTORY ${framework_location}
@@ -316,9 +445,10 @@ endmacro(add_sourcekit_framework)
316
445
# Usage:
317
446
# add_sourcekit_xpc_service(name # Name of the XPC service
318
447
# [LLVM_LINK_COMPONENTS comp1 ...] # LLVM components this service depends on
448
+ # [HAS_SWIFT_MODULES] # Whether to link SwiftCompilerModules
319
449
# source1 [source2 source3 ...]) # Sources to add into this service
320
450
macro (add_sourcekit_xpc_service name framework_target )
321
- cmake_parse_arguments (SOURCEKITXPC "" "" "LLVM_LINK_COMPONENTS" ${ARGN} )
451
+ cmake_parse_arguments (SOURCEKITXPC "HAS_SWIFT_MODULES " "" "LLVM_LINK_COMPONENTS" ${ARGN} )
322
452
set (srcs ${SOURCEKITXPC_UNPARSED_ARGUMENTS} )
323
453
324
454
set (lib_dir ${SOURCEKIT_LIBRARY_OUTPUT_INTDIR} )
@@ -364,10 +494,17 @@ macro(add_sourcekit_xpc_service name framework_target)
364
494
365
495
add_dependencies (${framework_target} ${name} )
366
496
497
+ set (RPATH_LIST )
498
+ add_sourcekitd_swifrt_linking (${name} ${xpc_bin_dir} ${SOURCEKITXPC_HAS_SWIFT_MODULES} )
499
+
500
+ file (RELATIVE_PATH relative_lib_path "${xpc_bin_dir} " "${lib_dir} " )
501
+ list (APPEND RPATH_LIST "@loader_path/${relative_lib_path} " )
502
+
367
503
# Add rpath for sourcekitdInProc
504
+ # lib/${framework_taget}.framework/Versions/A/XPCServices/${name}.xpc/Contents/MacOS/${name}
368
505
set_target_properties (${name} PROPERTIES
369
506
BUILD_WITH_INSTALL_RPATH On
370
- INSTALL_RPATH "@loader_path/../../../../../../.. "
507
+ INSTALL_RPATH "${RPATH_LIST} "
371
508
INSTALL_NAME_DIR "@rpath" )
372
509
373
510
if (SOURCEKIT_DEPLOYMENT_OS MATCHES "^macosx" )
@@ -379,9 +516,8 @@ macro(add_sourcekit_xpc_service name framework_target)
379
516
# ASan does not play well with exported_symbol option. This should be fixed soon.
380
517
if (NOT SWIFT_ASAN_BUILD )
381
518
if ("${CMAKE_SYSTEM_NAME} " STREQUAL "Darwin" )
382
- set_target_properties (${name}
383
- PROPERTIES
384
- LINK_FLAGS "-Wl,-exported_symbol,_main" )
519
+ set_property (TARGET ${name} APPEND_STRING PROPERTY
520
+ LINK_FLAGS " -Wl,-exported_symbol,_main " )
385
521
endif ()
386
522
endif ()
387
523
add_sourcekit_default_compiler_flags ("${name} " )
0 commit comments