Skip to content

Commit 68fabd4

Browse files
committed
build: support static builds for Swift
Adjust the paths for static runtime builds of Swift. Use the exported targets instead of reconstructing the paths.
1 parent edce1fe commit 68fabd4

File tree

3 files changed

+24
-30
lines changed

3 files changed

+24
-30
lines changed

CMakeLists.txt

Lines changed: 12 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -50,32 +50,21 @@ if(ENABLE_SWIFT)
5050
message(FATAL_ERROR "CMAKE_SWIFT_COMPILER must be defined to enable swift")
5151
endif()
5252

53-
get_filename_component(SWIFT_TOOLCHAIN ${CMAKE_SWIFT_COMPILER} DIRECTORY)
54-
get_filename_component(SWIFT_TOOLCHAIN ${SWIFT_TOOLCHAIN} DIRECTORY)
53+
find_package(Swift REQUIRED CONFIG)
5554

56-
string(TOLOWER ${CMAKE_SYSTEM_NAME} SWIFT_OS)
57-
get_swift_host_arch(SWIFT_HOST_ARCH)
55+
string(TOLOWER ${CMAKE_SYSTEM_NAME} swift_os)
56+
get_swift_host_arch(swift_arch)
5857

59-
set(SWIFT_RUNTIME_LIBDIR ${SWIFT_TOOLCHAIN}/${SWIFT_LIBDIR}/swift/${SWIFT_OS}/${SWIFT_HOST_ARCH})
60-
61-
add_library(swiftCore
62-
SHARED IMPORTED GLOBAL)
63-
set_target_properties(swiftCore
64-
PROPERTIES
65-
IMPORTED_LOCATION
66-
${SWIFT_RUNTIME_LIBDIR}/${CMAKE_SHARED_LIBRARY_PREFIX}swiftCore${CMAKE_SHARED_LIBRARY_SUFFIX})
67-
68-
add_library(swiftSwiftOnoneSupport
69-
SHARED IMPORTED GLOBAL)
70-
set_target_properties(swiftSwiftOnoneSupport
71-
PROPERTIES
72-
IMPORTED_LOCATION
73-
${SWIFT_RUNTIME_LIBDIR}/${CMAKE_SHARED_LIBRARY_PREFIX}swiftSwiftOnoneSupport${CMAKE_SHARED_LIBRARY_SUFFIX})
58+
if(BUILD_SHARED_LIBS)
59+
set(swift_dir swift)
60+
else()
61+
set(swift_dir swift_static)
62+
endif()
7463

75-
set(INSTALL_TARGET_DIR "${INSTALL_LIBDIR}/swift/${SWIFT_OS}" CACHE PATH "Path where the libraries will be installed")
76-
set(INSTALL_DISPATCH_HEADERS_DIR "${INSTALL_LIBDIR}/swift/dispatch" CACHE PATH "Path where the headers will be installed for libdispatch")
77-
set(INSTALL_BLOCK_HEADERS_DIR "${INSTALL_LIBDIR}/swift/Block" CACHE PATH "Path where the headers will be installed for the blocks runtime")
78-
set(INSTALL_OS_HEADERS_DIR "${INSTALL_LIBDIR}/swift/os" CACHE PATH "Path where the os/ headers will be installed")
64+
set(INSTALL_TARGET_DIR "${INSTALL_LIBDIR}/${swift_dir}/${swift_os}" CACHE PATH "Path where the libraries will be installed")
65+
set(INSTALL_DISPATCH_HEADERS_DIR "${INSTALL_LIBDIR}/${swift_dir}/dispatch" CACHE PATH "Path where the headers will be installed for libdispatch")
66+
set(INSTALL_BLOCK_HEADERS_DIR "${INSTALL_LIBDIR}/${swift_dir}/Block" CACHE PATH "Path where the headers will be installed for the blocks runtime")
67+
set(INSTALL_OS_HEADERS_DIR "${INSTALL_LIBDIR}/${swift_dir}/os" CACHE PATH "Path where the os/ headers will be installed")
7968
endif()
8069

8170
if(NOT ENABLE_SWIFT)

src/CMakeLists.txt

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -115,8 +115,10 @@ if(ENABLE_SWIFT)
115115
target_sources(dispatch
116116
PRIVATE
117117
swift/DispatchStubs.cc
118-
${CMAKE_CURRENT_BINARY_DIR}/swiftDispatch.o
119-
${SWIFT_RUNTIME_LIBDIR}/swiftrt.o)
118+
${CMAKE_CURRENT_BINARY_DIR}/swiftDispatch.o)
119+
set_target_properties(dispatch
120+
PROPRERTIES APPEND_STRING
121+
LINK_FLAGS "-Xlinker --whole-archive -Xlinker swiftImageInspectionShared-${swift_os}-${swift_arch} -Xlinker --no-whole-archive")
120122
if(CMAKE_BUILD_TYPE MATCHES Debug)
121123
target_link_libraries(dispatch
122124
PRIVATE

tests/CMakeLists.txt

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -59,8 +59,8 @@ endif()
5959
if(ENABLE_SWIFT)
6060
target_link_libraries(bsdtestharness
6161
PRIVATE
62-
swiftCore
63-
swiftSwiftOnoneSupport)
62+
swiftCore-${swift_os}-${swift_arch}
63+
swiftSwiftOnoneSupport-${swift_os}-${swift_arch})
6464
endif()
6565

6666
function(add_unit_test name)
@@ -83,7 +83,10 @@ function(add_unit_test name)
8383
# For testing in swift.org CI system; make deadlines lenient by default
8484
# to reduce probability of test failures due to machine load.
8585
target_compile_options(${name} PRIVATE -DLENIENT_DEADLINES=1)
86-
target_link_libraries(${name} PRIVATE swiftCore swiftSwiftOnoneSupport)
86+
target_link_libraries(${name}
87+
PRIVATE
88+
swiftCore-${swift_os}-${swift_arch}
89+
swiftSwiftOnoneSupport-${swift_os}-${swift_arch})
8790
endif()
8891
target_include_directories(${name}
8992
SYSTEM BEFORE PRIVATE
@@ -114,8 +117,8 @@ function(add_unit_test name)
114117
if(ENABLE_SWIFT)
115118
target_link_libraries(${name}
116119
PRIVATE
117-
swiftCore
118-
swiftSwiftOnoneSupport)
120+
swiftCore-${swift_os}-${swift_arch}
121+
swiftSwiftOnoneSupport-${swift_os}-${swift_arch})
119122
endif()
120123
target_link_libraries(${name} PRIVATE bsdtests)
121124
add_test(NAME ${name}

0 commit comments

Comments
 (0)