Skip to content

Commit 0b8c39b

Browse files
committed
[cmake] When compiling swift libraries that are not pure swift on macOS be sure to set the linker language to cxx.
I already did this for executables. The reason why we must do this is that right now there are bugs in cmake's swift support that makes it so that one can not use swiftc to link targets with mixed c/c++/swift content even if the swift content is indirectly added via linking. To work around this we must also ensure that all libraries with mixed c/c++/swift objects link using clang. As an additional side-effect of this, we must pass to the clang linker -L flags that swiftc would normally provide for the linker. The two cases where I needed to do this were: 1. -L path for the compatibility libraries. This path points into the toolchain where these live. This is important since otherwise we will fail to link given the minimum deployment target that swift (both host/stdlib) target today (10.9). 2. -L path to the SDK. This path points to the SDKs lib/swift/${HOST_PLATFORM} for swiftCore and friends. That being said, we still want people to be able to link pure swift libraries using swiftc, so we introduce an option called PURE_SWIFT to add_host_library that preserves this and is used to also set IGNORE_LLVM_UPDATE_COMPILE_FLAGS (which arguably should have been called PURE_SWIFT).
1 parent bae7115 commit 0b8c39b

File tree

1 file changed

+45
-10
lines changed

1 file changed

+45
-10
lines changed

cmake/modules/AddSwift.cmake

Lines changed: 45 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -390,7 +390,7 @@ endfunction()
390390
# [SHARED]
391391
# [STATIC]
392392
# [OBJECT]
393-
# [IGNORE_LLVM_UPDATE_COMPILE_FLAGS]
393+
# [PURE_SWIFT]
394394
# [LLVM_LINK_COMPONENTS comp1 ...]
395395
# source1 [source2 source3 ...])
396396
#
@@ -409,10 +409,9 @@ endfunction()
409409
# LLVM_LINK_COMPONENTS
410410
# LLVM components this library depends on.
411411
#
412-
# IGNORE_LLVM_UPDATE_COMPILE_FLAGS
413-
# If set do not use llvm_update_compile_flags to generate cflags/etc. This is
414-
# generally used when compiling a mixed c/c++/swift library and one wants to
415-
# disable this for the swift part.
412+
# PURE_SWIFT
413+
# This has two effects if set: we do not use llvm_update_compile_flags to
414+
# generate cflags/etc and we leave the linking mode of the library as swift.
416415
#
417416
# source1 ...
418417
# Sources to add into this library.
@@ -421,7 +420,7 @@ function(add_swift_host_library name)
421420
SHARED
422421
STATIC
423422
OBJECT
424-
IGNORE_LLVM_UPDATE_COMPILE_FLAGS)
423+
PURE_SWIFT)
425424
set(single_parameter_options)
426425
set(multiple_parameter_options
427426
LLVM_LINK_COMPONENTS)
@@ -467,7 +466,7 @@ function(add_swift_host_library name)
467466
468467
add_library(${name} ${libkind} ${ASHL_SOURCES})
469468
add_dependencies(${name} ${LLVM_COMMON_DEPENDS})
470-
if (NOT ASHL_IGNORE_LLVM_UPDATE_COMPILE_FLAGS)
469+
if (NOT ASHL_PURE_SWIFT)
471470
llvm_update_compile_flags(${name})
472471
endif()
473472
swift_common_llvm_config(${name} ${ASHL_LLVM_LINK_COMPONENTS})
@@ -528,6 +527,12 @@ function(add_swift_host_library name)
528527
NO_SONAME YES)
529528
endif()
530529
530+
# Always link as CXX even if we have swift content unless we only contain
531+
# swift content signaled via us being marked "PURE_SWIFT".
532+
if (NOT ASHL_PURE_SWIFT)
533+
set_target_properties(${name} PROPERTIES LINKER_LANGUAGE CXX)
534+
endif()
535+
531536
if(${SWIFT_HOST_VARIANT_SDK} IN_LIST SWIFT_APPLE_PLATFORMS)
532537
target_link_options(${name} PRIVATE
533538
"LINKER:-compatibility_version,1")
@@ -536,8 +541,24 @@ function(add_swift_host_library name)
536541
"LINKER:-current_version,${SWIFT_COMPILER_VERSION}")
537542
endif()
538543
539-
# For now turn off in swift targets, debug info if we are compiling a static
540-
# library.
544+
# If we found a swift compiler and are going to use swift code in swift
545+
# host side tools but link with clang, add the appropriate -L paths so we
546+
# find all of the necessary swift libraries on Darwin.
547+
if (NOT ASHL_PURE_SWIFT)
548+
if (CMAKE_Swift_COMPILER)
549+
# Add in the SDK directory for the host platform and add an rpath.
550+
target_link_directories(${name} PRIVATE
551+
${SWIFT_SDK_${SWIFT_HOST_VARIANT_SDK}_ARCH_${SWIFT_HOST_VARIANT_ARCH}_PATH}/usr/lib/swift)
552+
# Add in the toolchain directory so we can grab compatibility libraries
553+
get_filename_component(TOOLCHAIN_BIN_DIR ${CMAKE_Swift_COMPILER} DIRECTORY)
554+
get_filename_component(TOOLCHAIN_LIB_DIR "${TOOLCHAIN_BIN_DIR}/../lib/swift/macosx" ABSOLUTE)
555+
target_link_directories(${name} PUBLIC ${TOOLCHAIN_LIB_DIR})
556+
endif()
557+
endif()
558+
559+
# For now turn off on Darwin swift targets, debug info if we are compiling a static
560+
# library and set up an rpath so that if someone works around this by using
561+
# shared libraries that in the short term we can find shared libraries.
541562
if (ASHL_STATIC)
542563
target_compile_options(${name} PRIVATE $<$<COMPILE_LANGUAGE:Swift>:-gnone>)
543564
endif()
@@ -605,9 +626,23 @@ function(add_swift_host_tool executable)
605626
JOB_POOL_LINK swift_link_job_pool)
606627
endif()
607628
if(${SWIFT_HOST_VARIANT_SDK} IN_LIST SWIFT_APPLE_PLATFORMS)
629+
# Lists of rpaths that we are going to add to our executables.
630+
#
631+
# Please add each rpath separately below to the list, explaining why you are
632+
# adding it.
633+
set(RPATH_LIST)
634+
635+
# We also want to be able to find libraries from the base toolchain
636+
# directory. This is so swiftc can rely on its own host side dylibs that may
637+
# contain swift content.
638+
list(APPEND RPATH_LIST "@executable_path/../lib")
639+
640+
# Also include the swift specific resource dir in our rpath.
641+
list(APPEND RPATH_LIST "@executable_path/../lib/swift/${SWIFT_SDK_${SWIFT_HOST_VARIANT_SDK}_LIB_SUBDIR}")
642+
608643
set_target_properties(${executable} PROPERTIES
609644
BUILD_WITH_INSTALL_RPATH YES
610-
INSTALL_RPATH "@executable_path/../lib/swift/${SWIFT_SDK_${SWIFT_HOST_VARIANT_SDK}_LIB_SUBDIR}")
645+
INSTALL_RPATH "${RPATH_LIST}")
611646
endif()
612647
613648
llvm_update_compile_flags(${executable})

0 commit comments

Comments
 (0)