Skip to content

Commit 981ef15

Browse files
[6.0.2] Fix WASI build with CMake (#687)
- **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
1 parent 3755346 commit 981ef15

File tree

4 files changed

+39
-8
lines changed

4 files changed

+39
-8
lines changed

CMakeLists.txt

+1
Original file line numberDiff line numberDiff line change
@@ -38,4 +38,5 @@ if(NOT SWIFT_SYSTEM_NAME)
3838
endif()
3939
endif()
4040

41+
include(SwiftModuleInstallation)
4142
add_subdirectory(Sources)

Sources/Testing/CMakeLists.txt

+6-1
Original file line numberDiff line numberDiff line change
@@ -94,11 +94,16 @@ add_library(Testing
9494
Traits/Trait.swift)
9595
target_link_libraries(Testing PRIVATE
9696
_TestingInternals)
97+
if(NOT BUILD_SHARED_LIBS)
98+
# When building a static library, tell clients to autolink the internal
99+
# library.
100+
target_compile_options(Testing PRIVATE
101+
"SHELL:-Xfrontend -public-autolink-library -Xfrontend _TestingInternals")
102+
endif()
97103
add_dependencies(Testing
98104
TestingMacros)
99105
target_compile_options(Testing PRIVATE
100106
-enable-library-evolution
101107
-emit-module-interface -emit-module-interface-path $<TARGET_PROPERTY:Testing,Swift_MODULE_DIRECTORY>/Testing.swiftinterface)
102108

103-
include(SwiftModuleInstallation)
104109
_swift_testing_install_target(Testing)

Sources/_TestingInternals/CMakeLists.txt

+9
Original file line numberDiff line numberDiff line change
@@ -17,3 +17,12 @@ target_include_directories(_TestingInternals PUBLIC
1717
${CMAKE_CURRENT_SOURCE_DIR}/include)
1818
target_compile_options(_TestingInternals PRIVATE
1919
-fno-exceptions)
20+
21+
if(NOT BUILD_SHARED_LIBS)
22+
# When building a static library, install the internal library archive
23+
# alongside the main library. In shared library builds, the internal library
24+
# is linked into the main library and does not need to be installed separately.
25+
get_swift_testing_install_lib_dir(STATIC_LIBRARY lib_destination_dir)
26+
install(TARGETS _TestingInternals
27+
ARCHIVE DESTINATION ${lib_destination_dir})
28+
endif()

cmake/modules/SwiftModuleInstallation.cmake

+23-7
Original file line numberDiff line numberDiff line change
@@ -18,28 +18,44 @@ function(get_swift_host_os result_var_name)
1818
set(${result_var_name} ${SWIFT_SYSTEM_NAME} PARENT_SCOPE)
1919
endfunction()
2020

21-
function(_swift_testing_install_target module)
21+
# Returns the path to the Swift Testing library installation directory
22+
#
23+
# Usage:
24+
# get_swift_testing_install_lib_dir(type result_var_name)
25+
#
26+
# Arguments:
27+
# type: The type of the library (STATIC_LIBRARY, SHARED_LIBRARY, or EXECUTABLE).
28+
# Typically, the value of the TYPE target property.
29+
# result_var_name: The name of the variable to set
30+
function(get_swift_testing_install_lib_dir type result_var_name)
2231
get_swift_host_os(swift_os)
23-
get_target_property(type ${module} TYPE)
24-
2532
if(type STREQUAL STATIC_LIBRARY)
2633
set(swift swift_static)
2734
else()
2835
set(swift swift)
2936
endif()
3037

38+
if(APPLE)
39+
set(${result_var_name} "lib/${swift}/${swift_os}/testing" PARENT_SCOPE)
40+
else()
41+
set(${result_var_name} "lib/${swift}/${swift_os}" PARENT_SCOPE)
42+
endif()
43+
endfunction()
44+
45+
function(_swift_testing_install_target module)
3146
target_compile_options(Testing PRIVATE "-no-toolchain-stdlib-rpath")
3247

33-
if(CMAKE_SYSTEM_NAME STREQUAL "Darwin")
34-
set(lib_destination_dir "lib/${swift}/${swift_os}/testing")
48+
if(APPLE)
3549
set_property(TARGET ${module} PROPERTY
3650
INSTALL_RPATH "@loader_path/..")
3751
else()
38-
set(lib_destination_dir "lib/${swift}/${swift_os}")
3952
set_property(TARGET ${module} PROPERTY
4053
INSTALL_RPATH "$ORIGIN")
4154
endif()
4255

56+
get_target_property(type ${module} TYPE)
57+
get_swift_testing_install_lib_dir(${type} lib_destination_dir)
58+
4359
install(TARGETS ${module}
4460
ARCHIVE DESTINATION "${lib_destination_dir}"
4561
LIBRARY DESTINATION "${lib_destination_dir}"
@@ -71,7 +87,7 @@ function(_swift_testing_install_target module)
7187
install(FILES $<TARGET_PROPERTY:${module},Swift_MODULE_DIRECTORY>/${module_name}.swiftmodule
7288
DESTINATION "${module_dir}"
7389
RENAME ${SwiftTesting_MODULE_TRIPLE}.swiftmodule)
74-
if(CMAKE_SYSTEM_NAME STREQUAL "Darwin")
90+
if(APPLE)
7591
# Only Darwin has stable ABI.
7692
install(FILES $<TARGET_PROPERTY:${module},Swift_MODULE_DIRECTORY>/${module_name}.swiftinterface
7793
DESTINATION "${module_dir}"

0 commit comments

Comments
 (0)