Skip to content

Commit

Permalink
[6.0.2] Fix WASI build with CMake (#687)
Browse files Browse the repository at this point in the history
- **Explanation**: When building the Testing library as a static library
(BUILD_SHARED_LIBS=FALSE), `_TestingInternals` is not included in the
`libTesting.a` archive by default. (when building as a shared library,
`libTesting.so` includes `_TestingInternals` objects). This causes the
linker to complain about missing symbols, so we need to ship
`lib_TestingInternals.a` and autolink it too.
- **Original PR**: #651
#672
- **Risk**: Low; NFC for non WASI platforms
- **Testing**: Tested in ci.swift.org
- **Reviewer**: @grynspan @briancroom
  • Loading branch information
kateinoigakukun authored Sep 24, 2024
1 parent 3755346 commit 981ef15
Show file tree
Hide file tree
Showing 4 changed files with 39 additions and 8 deletions.
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_testing_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

0 comments on commit 981ef15

Please sign in to comment.