Skip to content

Commit 2e05461

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 2e05461

File tree

3 files changed

+29
-35
lines changed

3 files changed

+29
-35
lines changed

CMakeLists.txt

Lines changed: 17 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -44,38 +44,32 @@ dispatch_common_warnings()
4444
option(ENABLE_DISPATCH_INIT_CONSTRUCTOR "enable libdispatch_init as a constructor" ON)
4545
set(USE_LIBDISPATCH_INIT_CONSTRUCTOR ${ENABLE_DISPATCH_INIT_CONSTRUCTOR})
4646

47+
# NOTE(abdulras) this is the CMake supported way to control whether we generate
48+
# shared or static libraries. This impacts the behaviour of `add_library` in
49+
# what type of library it generates.
50+
option(BUILD_SHARED_LIBS "build shared libraries" ON)
51+
4752
option(ENABLE_SWIFT "enable libdispatch swift overlay" OFF)
4853
if(ENABLE_SWIFT)
4954
if(NOT CMAKE_SWIFT_COMPILER)
5055
message(FATAL_ERROR "CMAKE_SWIFT_COMPILER must be defined to enable swift")
5156
endif()
5257

53-
get_filename_component(SWIFT_TOOLCHAIN ${CMAKE_SWIFT_COMPILER} DIRECTORY)
54-
get_filename_component(SWIFT_TOOLCHAIN ${SWIFT_TOOLCHAIN} DIRECTORY)
55-
56-
string(TOLOWER ${CMAKE_SYSTEM_NAME} SWIFT_OS)
57-
get_swift_host_arch(SWIFT_HOST_ARCH)
58+
find_package(Swift REQUIRED CONFIG)
5859

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})
60+
string(TOLOWER ${CMAKE_SYSTEM_NAME} swift_os)
61+
get_swift_host_arch(swift_arch)
6762

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})
63+
if(BUILD_SHARED_LIBS)
64+
set(swift_dir swift)
65+
else()
66+
set(swift_dir swift_static)
67+
endif()
7468

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")
69+
set(INSTALL_TARGET_DIR "${INSTALL_LIBDIR}/${swift_dir}/${swift_os}" CACHE PATH "Path where the libraries will be installed")
70+
set(INSTALL_DISPATCH_HEADERS_DIR "${INSTALL_LIBDIR}/${swift_dir}/dispatch" CACHE PATH "Path where the headers will be installed for libdispatch")
71+
set(INSTALL_BLOCK_HEADERS_DIR "${INSTALL_LIBDIR}/${swift_dir}/Block" CACHE PATH "Path where the headers will be installed for the blocks runtime")
72+
set(INSTALL_OS_HEADERS_DIR "${INSTALL_LIBDIR}/${swift_dir}/os" CACHE PATH "Path where the os/ headers will be installed")
7973
endif()
8074

8175
if(NOT ENABLE_SWIFT)
@@ -87,11 +81,6 @@ endif()
8781

8882
option(ENABLE_DTRACE "enable dtrace support" "")
8983

90-
# NOTE(abdulras) this is the CMake supported way to control whether we generate
91-
# shared or static libraries. This impacts the behaviour of `add_library` in
92-
# what type of library it generates.
93-
option(BUILD_SHARED_LIBS "build shared libraries" ON)
94-
9584
option(ENABLE_TESTING "build libdispatch tests" ON)
9685

9786
option(USE_LLD_LINKER "use the lld linker" FALSE)

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_property(TARGET dispatch APPEND_STRING
120+
PROPERTY LINK_FLAGS
121+
-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)