Skip to content

build: support static builds for Swift #397

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Oct 18, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
45 changes: 17 additions & 28 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -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
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Consider linking straight to the reference: https://cmake.org/cmake/help/v3.4/variable/BUILD_SHARED_LIBS.html

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I do like that better; I think Im going to do that in a follow up (to get this merged first). This was just moving the option around.

# 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)
Expand All @@ -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)
Expand Down
7 changes: 6 additions & 1 deletion src/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
13 changes: 8 additions & 5 deletions tests/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand All @@ -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
Expand Down Expand Up @@ -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}
Expand Down