Skip to content

build: add support for nesting the Swift subdirectory #860

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

Closed
wants to merge 9 commits into from
Closed
19 changes: 8 additions & 11 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -118,7 +118,6 @@ include(DispatchAppleOptions)
include(DispatchSanitization)
include(DispatchCompilerWarnings)
include(DTrace)
include(SwiftSupport)

# 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
Expand Down Expand Up @@ -157,6 +156,14 @@ option(INSTALL_PRIVATE_HEADERS "installs private headers in the same location as
option(ENABLE_SWIFT "enable libdispatch swift overlay" OFF)
if(ENABLE_SWIFT)
enable_language(Swift)

include(PlatformInfo)

option(Dispatch_INSTALL_NESTED_SUBDIR "Install libraries under a platform and architecture subdirectory" NO)
set(Dispatch_INSTALL_LIBDIR "${CMAKE_INSTALL_LIBDIR}/swift$<$<NOT:$<BOOL:${BUILD_SHARED_LIBS}>>:_static>/${Dispatch_PLATFORM_SUBDIR}$<$<BOOL:${Dispatch_INSTALL_NESTED_SUBDIR}>:/${Dispatch_ARCH_SUBDIR}>")
set(Dispatch_INSTALL_SWIFTMODULEDIR "${CMAKE_INSTALL_LIBDIR}/swift$<$<NOT:$<BOOL:${BUILD_SHARED_LIBS}>>:_static>/${Dispatch_PLATFORM_SUBDIR}$<$<BOOL:${Dispatch_INSTALL_NESTED_SUBDIR}>:/${Dispatch_ARCH_SUBDIR}>")
else()
set(Dispatch_INSTALL_LIBDIR ${CMAKE_INSTALL_LIBDIR})
endif()

option(ENABLE_THREAD_LOCAL_STORAGE "enable usage of thread local storage via _Thread_local" ON)
Expand Down Expand Up @@ -307,20 +314,10 @@ add_compile_definitions($<$<COMPILE_LANGUAGE:C,CXX>:HAVE_CONFIG_H>)


if(ENABLE_SWIFT)
if(NOT SWIFT_SYSTEM_NAME)
if(APPLE)
set(SWIFT_SYSTEM_NAME macosx)
else()
set(SWIFT_SYSTEM_NAME "$<LOWER_CASE:${CMAKE_SYSTEM_NAME}>")
endif()
endif()

set(INSTALL_TARGET_DIR "${CMAKE_INSTALL_LIBDIR}/swift$<$<NOT:$<BOOL:${BUILD_SHARED_LIBS}>>:_static>/${SWIFT_SYSTEM_NAME}" CACHE PATH "Path where the libraries will be installed")
set(INSTALL_DISPATCH_HEADERS_DIR "${CMAKE_INSTALL_LIBDIR}/swift$<$<NOT:$<BOOL:${BUILD_SHARED_LIBS}>>:_static>/dispatch" CACHE PATH "Path where the headers will be installed for libdispatch")
set(INSTALL_BLOCK_HEADERS_DIR "${CMAKE_INSTALL_LIBDIR}/swift$<$<NOT:$<BOOL:${BUILD_SHARED_LIBS}>>:_static>/Block" CACHE PATH "Path where the headers will be installed for the blocks runtime")
set(INSTALL_OS_HEADERS_DIR "${CMAKE_INSTALL_LIBDIR}/swift$<$<NOT:$<BOOL:${BUILD_SHARED_LIBS}>>:_static>/os" CACHE PATH "Path where the os/ headers will be installed")
else()
set(INSTALL_TARGET_DIR "${CMAKE_INSTALL_LIBDIR}" CACHE PATH "Path where the libraries will be installed")
set(INSTALL_DISPATCH_HEADERS_DIR "include/dispatch" CACHE PATH "Path where the headers will be installed")
set(INSTALL_BLOCK_HEADERS_DIR "include" CACHE PATH "Path where the headers will be installed for the blocks runtime")
set(INSTALL_OS_HEADERS_DIR "include/os" CACHE PATH "Path where the headers will be installed")
Expand Down
48 changes: 48 additions & 0 deletions cmake/modules/PlatformInfo.cmake
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
# This source file is part of the Swift.org open source project
#
# Copyright (c) 2025 Apple Inc. and the Swift project authors
# Licensed under Apache License v2.0 with Runtime Library Exception
#
# See http://swift.org/LICENSE.txt for license information
# See http://swift.org/CONTRIBUTORS.txt for Swift project authors

set(print_target_info_invocation "${CMAKE_Swift_COMPILER}" -print-target-info)
if(CMAKE_Swift_COMPILER_TARGET)
list(APPEND print_target_info_invocation -target ${CMAKE_Swift_COMPILER_TARGET})
endif()
execute_process(COMMAND ${print_target_info_invocation} OUTPUT_VARIABLE target_info_json)
message(CONFIGURE_LOG "Swift Target Info: ${print_target_info_invocation}\n"
"${target_info_json}")

if(NOT Dispatch_MODULE_TRIPLE)
string(JSON module_triple GET "${target_info_json}" "target" "moduleTriple")
set(Dispatch_MODULE_TRIPLE "${module_triple}" CACHE STRING "Triple used for installed swift{doc,module,interface} files")
mark_as_advanced(Dispatch_MODULE_TRIPLE)

message(CONFIGURE_LOG "Swift Module Triple: ${module_triple}")
endif()

if(NOT Dispatch_PLATFORM_SUBDIR)
string(JSON platform GET "${target_info_json}" "target" "platform")
if(NOT platform)
if(NOT SWIFT_SYSTEM_NAME)
if(CMAKE_SYSTEM_NAME STREQUAL Darwin)
set(platform macosx)
else()
set(platform $<LOWER_CASE:${CMAKE_SYSTEM_NAME}>)
endif()
endif()
endif()
set(Dispatch_PLATFORM_SUBDIR "${platform}" CACHE STRING "Platform name used for installed swift{doc,module,interface} files")
mark_as_advanced(Dispatch_PLATFORM_SUBDIR)

message(CONFIGURE_LOG "Swift Platform: ${platform}")
endif()

if(NOT Dispatch_ARCH_SUBDIR)
string(JSON arch GET "${target_info_json}" "target" "arch")
set(Dispatch_ARCH_SUBDIR "${arch}" CACHE STRING "Architecture used for setting the architecture subdirectory")
mark_as_advanced(Dispatch_ARCH_SUBDIR)

message(CONFIGURE_LOG "Swift Architecture: ${arch}")
endif()
41 changes: 0 additions & 41 deletions cmake/modules/SwiftSupport.cmake

This file was deleted.

4 changes: 2 additions & 2 deletions src/BlocksRuntime/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,6 @@ endif()
set_property(GLOBAL APPEND PROPERTY DISPATCH_EXPORTS BlocksRuntime)
install(TARGETS BlocksRuntime
EXPORT dispatchExports
ARCHIVE DESTINATION ${INSTALL_TARGET_DIR}
LIBRARY DESTINATION ${INSTALL_TARGET_DIR}
ARCHIVE DESTINATION ${Dispatch_INSTALL_LIBDIR}
LIBRARY DESTINATION ${Dispatch_INSTALL_LIBDIR}
RUNTIME DESTINATION ${CMAKE_INSTALL_BINDIR})
4 changes: 2 additions & 2 deletions src/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -189,6 +189,6 @@ endif()
set_property(GLOBAL APPEND PROPERTY DISPATCH_EXPORTS dispatch)
install(TARGETS dispatch
EXPORT dispatchExports
ARCHIVE DESTINATION ${INSTALL_TARGET_DIR}
LIBRARY DESTINATION ${INSTALL_TARGET_DIR}
ARCHIVE DESTINATION ${Dispatch_INSTALL_LIBDIR}
LIBRARY DESTINATION ${Dispatch_INSTALL_LIBDIR}
RUNTIME DESTINATION ${CMAKE_INSTALL_BINDIR})
35 changes: 22 additions & 13 deletions src/swift/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -31,24 +31,33 @@ target_link_libraries(swiftDispatch PRIVATE
BlocksRuntime::BlocksRuntime)
target_link_libraries(swiftDispatch PUBLIC
dispatch)
if(NOT DARWIN AND NOT WIN32)
target_link_options(swiftDispatch PRIVATE "SHELL:-no-toolchain-stdlib-rpath")
set_target_properties(swiftDispatch PROPERTIES INSTALL_RPATH "$ORIGIN")
endif()

get_swift_host_arch(swift_arch)
install(FILES
${CMAKE_CURRENT_BINARY_DIR}/swift/Dispatch.swiftmodule
${CMAKE_CURRENT_BINARY_DIR}/swift/Dispatch.swiftdoc
DESTINATION ${INSTALL_TARGET_DIR}/${swift_arch})
set_property(GLOBAL APPEND PROPERTY DISPATCH_EXPORTS swiftDispatch)
install(TARGETS swiftDispatch
EXPORT dispatchExports
ARCHIVE DESTINATION ${INSTALL_TARGET_DIR}
LIBRARY DESTINATION ${INSTALL_TARGET_DIR}
RUNTIME DESTINATION ${CMAKE_INSTALL_BINDIR})
ARCHIVE DESTINATION "${Dispatch_INSTALL_LIBDIR}"
LIBRARY DESTINATION "${Dispatch_INSTALL_LIBDIR}"
RUNTIME DESTINATION "${CMAKE_INSTALL_BINDIR}")
INSTALL(FILES $<TARGET_PROPERTY:swiftDispatch,Swift_MODULE_DIRECTORY>/$<TARGET_PROPERTY:swiftDispatch,Swift_MODULE_NAME>.swiftdoc
DESTINATION ${Dispatch_INSTALL_SWIFTMODULEDIR}/$<TARGET_PROPERTY:swiftDispatch,Swift_MODULE_NAME>.swiftmodule
RENAME ${Dispatch_MODULE_TRIPLE}.swiftdoc)
# INSTALL(FILES $<TARGET_PROPERTY:swiftDispatch,Swift_MODULE_DIRECTORY>/$<TARGET_PROPERTY:swiftDispatch,Swift_MODULE_NAME>.swiftinterface
Copy link
Member

Choose a reason for hiding this comment

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

Is this commented out for a reason? Do we want a TODO here to enable installing the interface once interfaces work without stability, or should this code be deleted?

Copy link
Member Author

Choose a reason for hiding this comment

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

Yeah, this is something that we want to enable in the future. It requires adding new options, and I was trying to keep the flags the same as they were, just changing the install location.

Copy link
Member

Choose a reason for hiding this comment

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

Okay, maybe leave a FIXME/TODO comment so that we know what was intended.

# DESTINATION ${Dispatch_INSTALL_SWIFTMODULEDIR}/$<TARGET_PROPERTY:swiftDispatch,Swift_MODULE_NAME>.swiftmodule
# RENAME ${Dispach_MODULE_TRIPLE}.swiftinterface)
INSTALL(FILES $<TARGET_PROPERTY:swiftDispatch,Swift_MODULE_DIRECTORY>/$<TARGET_PROPERTY:swiftDispatch,Swift_MODULE_NAME>.swiftmodule
DESTINATION ${Dispatch_INSTALL_SWIFTMODULEDIR}/$<TARGET_PROPERTY:swiftDispatch,Swift_MODULE_NAME>.swiftmodule
RENAME ${Dispatch_MODULE_TRIPLE}.swiftmodule)
INSTALL(FILES $<TARGET_PROPERTY:swiftDispatch,Swift_MODULE_DIRECTORY>/$<TARGET_PROPERTY:swiftDispatch,Swift_MODULE_NAME>.swiftsourceinfo
DESTINATION ${Dispatch_INSTALL_SWIFTMODULEDIR}/$<TARGET_PROPERTY:swiftDispatch,Swift_MODULE_NAME>.swiftmodule
RENAME ${Dispatch_MODULE_TRIPLE}.swiftsourceinfo)
if(NOT BUILD_SHARED_LIBS)
set_property(GLOBAL APPEND PROPERTY DISPATCH_EXPORTS DispatchStubs)
install(TARGETS DispatchStubs
EXPORT dispatchExports
DESTINATION ${INSTALL_TARGET_DIR})
elseif(NOT DARWIN AND NOT WIN32)
target_link_options(swiftDispatch PRIVATE "SHELL:-no-toolchain-stdlib-rpath")
set_target_properties(swiftDispatch PROPERTIES INSTALL_RPATH "$ORIGIN")
DESTINATION ${Dispatch_INSTALL_LIBDIR})
endif()

set_property(GLOBAL APPEND PROPERTY DISPATCH_EXPORTS swiftDispatch)