Skip to content

CMake: Install _TestingInternals in static library builds #651

New issue

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

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

Already on GitHub? # to your account

Merged
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
1 change: 1 addition & 0 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -38,4 +38,5 @@ if(NOT SWIFT_SYSTEM_NAME)
endif()
endif()

include(SwiftModuleInstallation)
add_subdirectory(Sources)
7 changes: 6 additions & 1 deletion Sources/Testing/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -94,11 +94,16 @@ add_library(Testing
Traits/Trait.swift)
target_link_libraries(Testing PRIVATE
_TestingInternals)
if(NOT BUILD_SHARED_LIBS)
# When building a static library, tell clients to autolink the internal
# library.
target_compile_options(Testing PRIVATE
"SHELL:-Xfrontend -public-autolink-library -Xfrontend _TestingInternals")
endif()
add_dependencies(Testing
TestingMacros)
target_compile_options(Testing PRIVATE
-enable-library-evolution
-emit-module-interface -emit-module-interface-path $<TARGET_PROPERTY:Testing,Swift_MODULE_DIRECTORY>/Testing.swiftinterface)

include(SwiftModuleInstallation)
_swift_testing_install_target(Testing)
9 changes: 9 additions & 0 deletions Sources/_TestingInternals/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -17,3 +17,12 @@ target_include_directories(_TestingInternals PUBLIC
${CMAKE_CURRENT_SOURCE_DIR}/include)
target_compile_options(_TestingInternals PRIVATE
-fno-exceptions)

if(NOT BUILD_SHARED_LIBS)
# When building a static library, install the internal library archive
# alongside the main library. In shared library builds, the internal library
# is linked into the main library and does not need to be installed separately.
get_swift_install_lib_dir(STATIC_LIBRARY lib_destination_dir)
install(TARGETS _TestingInternals
ARCHIVE DESTINATION ${lib_destination_dir})
endif()
30 changes: 23 additions & 7 deletions cmake/modules/SwiftModuleInstallation.cmake
Original file line number Diff line number Diff line change
Expand Up @@ -18,28 +18,44 @@ function(get_swift_host_os result_var_name)
set(${result_var_name} ${SWIFT_SYSTEM_NAME} PARENT_SCOPE)
endfunction()

function(_swift_testing_install_target module)
# Returns the path to the Swift Testing library installation directory
#
# Usage:
# get_swift_testing_install_lib_dir(type result_var_name)
#
# Arguments:
# type: The type of the library (STATIC_LIBRARY, SHARED_LIBRARY, or EXECUTABLE).
# Typically, the value of the TYPE target property.
# result_var_name: The name of the variable to set
function(get_swift_testing_install_lib_dir type result_var_name)
get_swift_host_os(swift_os)
get_target_property(type ${module} TYPE)

if(type STREQUAL STATIC_LIBRARY)
set(swift swift_static)
else()
set(swift swift)
endif()

if(APPLE)
set(${result_var_name} "lib/${swift}/${swift_os}/testing" PARENT_SCOPE)
else()
set(${result_var_name} "lib/${swift}/${swift_os}" PARENT_SCOPE)
endif()
endfunction()

function(_swift_testing_install_target module)
target_compile_options(Testing PRIVATE "-no-toolchain-stdlib-rpath")

if(CMAKE_SYSTEM_NAME STREQUAL "Darwin")
set(lib_destination_dir "lib/${swift}/${swift_os}/testing")
if(APPLE)
set_property(TARGET ${module} PROPERTY
INSTALL_RPATH "@loader_path/..")
else()
set(lib_destination_dir "lib/${swift}/${swift_os}")
set_property(TARGET ${module} PROPERTY
INSTALL_RPATH "$ORIGIN")
endif()

get_target_property(type ${module} TYPE)
get_swift_testing_install_lib_dir(${type} lib_destination_dir)

install(TARGETS ${module}
ARCHIVE DESTINATION "${lib_destination_dir}"
LIBRARY DESTINATION "${lib_destination_dir}"
Expand Down Expand Up @@ -71,7 +87,7 @@ function(_swift_testing_install_target module)
install(FILES $<TARGET_PROPERTY:${module},Swift_MODULE_DIRECTORY>/${module_name}.swiftmodule
DESTINATION "${module_dir}"
RENAME ${SwiftTesting_MODULE_TRIPLE}.swiftmodule)
if(CMAKE_SYSTEM_NAME STREQUAL "Darwin")
if(APPLE)
# Only Darwin has stable ABI.
install(FILES $<TARGET_PROPERTY:${module},Swift_MODULE_DIRECTORY>/${module_name}.swiftinterface
DESTINATION "${module_dir}"
Expand Down