diff --git a/CMakeLists.txt b/CMakeLists.txt index 6b0a23e92..f0632afb8 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -44,38 +44,32 @@ dispatch_common_warnings() option(ENABLE_DISPATCH_INIT_CONSTRUCTOR "enable libdispatch_init as a constructor" ON) set(USE_LIBDISPATCH_INIT_CONSTRUCTOR ${ENABLE_DISPATCH_INIT_CONSTRUCTOR}) +# NOTE(abdulras) this is the CMake supported way to control whether we generate +# shared or static libraries. This impacts the behaviour of `add_library` in +# what type of library it generates. +option(BUILD_SHARED_LIBS "build shared libraries" ON) + option(ENABLE_SWIFT "enable libdispatch swift overlay" OFF) if(ENABLE_SWIFT) if(NOT CMAKE_SWIFT_COMPILER) message(FATAL_ERROR "CMAKE_SWIFT_COMPILER must be defined to enable swift") endif() - get_filename_component(SWIFT_TOOLCHAIN ${CMAKE_SWIFT_COMPILER} DIRECTORY) - get_filename_component(SWIFT_TOOLCHAIN ${SWIFT_TOOLCHAIN} DIRECTORY) - - string(TOLOWER ${CMAKE_SYSTEM_NAME} SWIFT_OS) - get_swift_host_arch(SWIFT_HOST_ARCH) + find_package(Swift REQUIRED CONFIG) - set(SWIFT_RUNTIME_LIBDIR ${SWIFT_TOOLCHAIN}/${SWIFT_LIBDIR}/swift/${SWIFT_OS}/${SWIFT_HOST_ARCH}) - - add_library(swiftCore - SHARED IMPORTED GLOBAL) - set_target_properties(swiftCore - PROPERTIES - IMPORTED_LOCATION - ${SWIFT_RUNTIME_LIBDIR}/${CMAKE_SHARED_LIBRARY_PREFIX}swiftCore${CMAKE_SHARED_LIBRARY_SUFFIX}) + string(TOLOWER ${CMAKE_SYSTEM_NAME} swift_os) + get_swift_host_arch(swift_arch) - add_library(swiftSwiftOnoneSupport - SHARED IMPORTED GLOBAL) - set_target_properties(swiftSwiftOnoneSupport - PROPERTIES - IMPORTED_LOCATION - ${SWIFT_RUNTIME_LIBDIR}/${CMAKE_SHARED_LIBRARY_PREFIX}swiftSwiftOnoneSupport${CMAKE_SHARED_LIBRARY_SUFFIX}) + if(BUILD_SHARED_LIBS) + set(swift_dir swift) + else() + set(swift_dir swift_static) + endif() - set(INSTALL_TARGET_DIR "${INSTALL_LIBDIR}/swift/${SWIFT_OS}" CACHE PATH "Path where the libraries will be installed") - set(INSTALL_DISPATCH_HEADERS_DIR "${INSTALL_LIBDIR}/swift/dispatch" CACHE PATH "Path where the headers will be installed for libdispatch") - set(INSTALL_BLOCK_HEADERS_DIR "${INSTALL_LIBDIR}/swift/Block" CACHE PATH "Path where the headers will be installed for the blocks runtime") - set(INSTALL_OS_HEADERS_DIR "${INSTALL_LIBDIR}/swift/os" CACHE PATH "Path where the os/ headers will be installed") + set(INSTALL_TARGET_DIR "${INSTALL_LIBDIR}/${swift_dir}/${swift_os}" CACHE PATH "Path where the libraries will be installed") + set(INSTALL_DISPATCH_HEADERS_DIR "${INSTALL_LIBDIR}/${swift_dir}/dispatch" CACHE PATH "Path where the headers will be installed for libdispatch") + set(INSTALL_BLOCK_HEADERS_DIR "${INSTALL_LIBDIR}/${swift_dir}/Block" CACHE PATH "Path where the headers will be installed for the blocks runtime") + set(INSTALL_OS_HEADERS_DIR "${INSTALL_LIBDIR}/${swift_dir}/os" CACHE PATH "Path where the os/ headers will be installed") endif() if(NOT ENABLE_SWIFT) @@ -87,11 +81,6 @@ endif() option(ENABLE_DTRACE "enable dtrace support" "") -# NOTE(abdulras) this is the CMake supported way to control whether we generate -# shared or static libraries. This impacts the behaviour of `add_library` in -# what type of library it generates. -option(BUILD_SHARED_LIBS "build shared libraries" ON) - option(ENABLE_TESTING "build libdispatch tests" ON) option(USE_LLD_LINKER "use the lld linker" FALSE) diff --git a/src/CMakeLists.txt b/src/CMakeLists.txt index 9782fed83..efe3aed2f 100644 --- a/src/CMakeLists.txt +++ b/src/CMakeLists.txt @@ -112,11 +112,16 @@ if(ENABLE_SWIFT) ${swift_optimization_flags} DEPENDS ${PROJECT_SOURCE_DIR}/dispatch/module.modulemap) + + get_filename_component(swift_toolchain ${CMAKE_SWIFT_COMPILER} DIRECTORY) + get_filename_component(swift_toolchain ${swift_toolchain} DIRECTORY) + set(swift_runtime_libdir ${swift_toolchain}/lib/${swift_dir}/${swift_os}/${swift_arch}) + target_sources(dispatch PRIVATE swift/DispatchStubs.cc ${CMAKE_CURRENT_BINARY_DIR}/swiftDispatch.o - ${SWIFT_RUNTIME_LIBDIR}/swiftrt.o) + ${swift_runtime_libdir}/swiftrt.o) if(CMAKE_BUILD_TYPE MATCHES Debug) target_link_libraries(dispatch PRIVATE diff --git a/tests/CMakeLists.txt b/tests/CMakeLists.txt index 3d0ccdd4b..eca431cef 100644 --- a/tests/CMakeLists.txt +++ b/tests/CMakeLists.txt @@ -59,8 +59,8 @@ endif() if(ENABLE_SWIFT) target_link_libraries(bsdtestharness PRIVATE - swiftCore - swiftSwiftOnoneSupport) + swiftCore-${swift_os}-${swift_arch} + swiftSwiftOnoneSupport-${swift_os}-${swift_arch}) endif() function(add_unit_test name) @@ -83,7 +83,10 @@ function(add_unit_test name) # For testing in swift.org CI system; make deadlines lenient by default # to reduce probability of test failures due to machine load. target_compile_options(${name} PRIVATE -DLENIENT_DEADLINES=1) - target_link_libraries(${name} PRIVATE swiftCore swiftSwiftOnoneSupport) + target_link_libraries(${name} + PRIVATE + swiftCore-${swift_os}-${swift_arch} + swiftSwiftOnoneSupport-${swift_os}-${swift_arch}) endif() target_include_directories(${name} SYSTEM BEFORE PRIVATE @@ -114,8 +117,8 @@ function(add_unit_test name) if(ENABLE_SWIFT) target_link_libraries(${name} PRIVATE - swiftCore - swiftSwiftOnoneSupport) + swiftCore-${swift_os}-${swift_arch} + swiftSwiftOnoneSupport-${swift_os}-${swift_arch}) endif() target_link_libraries(${name} PRIVATE bsdtests) add_test(NAME ${name}