Skip to content

Commit 2753f4d

Browse files
committed
[CMake] Add an option to pass-in the plugin path
Add `SwiftTesting_MACRO` configuration option: * By default, the plugin is built using `SwiftTesting_MACRO_*` options * If it's explicitly "NO" (or other 'false' value), build Testing library without using the plugin * Otherwise, use the passed-in value as the plugin path Update macOS install location to 'usr/lib/swift/testing' for libraries, and 'usr/lib/host/plugins/testing' for plugin. Correct the install RPATH.
1 parent 5eee2cf commit 2753f4d

File tree

5 files changed

+133
-76
lines changed

5 files changed

+133
-76
lines changed

CMakeLists.txt

+2
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,8 @@ endif()
1515
project(SwiftTesting
1616
LANGUAGES CXX Swift)
1717

18+
include(GNUInstallDirs)
19+
1820
list(APPEND CMAKE_MODULE_PATH
1921
${PROJECT_SOURCE_DIR}/cmake/modules
2022
${PROJECT_SOURCE_DIR}/cmake/modules/shared)

Sources/CMakeLists.txt

+95-47
Original file line numberDiff line numberDiff line change
@@ -6,60 +6,108 @@
66
# See http://swift.org/LICENSE.txt for license information
77
# See http://swift.org/CONTRIBUTORS.txt for Swift project authors
88

9-
# Macros must be built for the build machine, not the host.
10-
include(ExternalProject)
11-
if(NOT SwiftTesting_MACRO_MAKE_PROGRAM)
12-
set(SwiftTesting_MACRO_MAKE_PROGRAM ${CMAKE_MAKE_PROGRAM})
13-
endif()
14-
if(NOT SwiftTesting_MACRO_Swift_COMPILER)
15-
set(SwiftTesting_MACRO_Swift_COMPILER ${CMAKE_Swift_COMPILER})
16-
endif()
17-
if(NOT SwiftTesting_MACRO_AR)
18-
set(SwiftTesting_MACRO_AR ${CMAKE_AR})
19-
endif()
20-
if(NOT SwiftTesting_MACRO_RANLIB)
21-
set(SwiftTesting_MACRO_RANLIB ${CMAKE_RANLIB})
22-
endif()
9+
set(SwiftTesting_MACRO "<auto>" CACHE STRING
10+
"Path to SwiftTesting macro plugin, or '<auto>' for automatically building it")
2311

24-
find_package(SwiftSyntax CONFIG GLOBAL)
25-
if(SwiftSyntax_FOUND)
26-
set(SwiftTesting_BuildMacrosAsExecutables NO)
27-
else()
28-
set(SwiftTesting_BuildMacrosAsExecutables YES)
29-
endif()
12+
if(SwiftTesting_MACRO STREQUAL "<auto>")
13+
# Macros must be built for the build machine, not the host.
14+
include(ExternalProject)
15+
if(NOT SwiftTesting_MACRO_MAKE_PROGRAM)
16+
set(SwiftTesting_MACRO_MAKE_PROGRAM ${CMAKE_MAKE_PROGRAM})
17+
endif()
18+
if(NOT SwiftTesting_MACRO_Swift_COMPILER)
19+
set(SwiftTesting_MACRO_Swift_COMPILER ${CMAKE_Swift_COMPILER})
20+
endif()
21+
if(NOT SwiftTesting_MACRO_Swift_FLAGS)
22+
set(SwiftTesting_MACRO_Swift_FLAGS ${CMAKE_Swift_FLAGS})
23+
set(SwiftTesting_MACRO_SWIFT_FLAGS_RELEASE ${CMAKE_Swift_FLAGS_RELEASE})
24+
set(SwiftTesting_MACRO_SWIFT_FLAGS_RELWITHDEBINFO ${CMAKE_Swift_FLAGS_RELWITHDEBINFO})
25+
endif()
26+
if(NOT SwiftTesting_MACRO_AR)
27+
set(SwiftTesting_MACRO_AR ${CMAKE_AR})
28+
endif()
29+
if(NOT SwiftTesting_MACRO_RANLIB)
30+
set(SwiftTesting_MACRO_RANLIB ${CMAKE_RANLIB})
31+
endif()
32+
if(NOT SwiftTesting_MACRO_BUILD_TYPE)
33+
set(SwiftTesting_MACRO_BUILD_TYPE ${CMAKE_BUILD_TYPE})
34+
endif()
35+
36+
find_package(SwiftSyntax CONFIG GLOBAL)
37+
if(SwiftSyntax_FOUND)
38+
set(SwiftTesting_BuildMacrosAsExecutables NO)
39+
else()
40+
set(SwiftTesting_BuildMacrosAsExecutables YES)
41+
endif()
3042

31-
ExternalProject_Add(TestingMacros
32-
PREFIX "tm"
33-
SOURCE_DIR "${CMAKE_CURRENT_SOURCE_DIR}/TestingMacros"
34-
CMAKE_ARGS
35-
-DCMAKE_Swift_COMPILER=${SwiftTesting_MACRO_Swift_COMPILER}
36-
-DCMAKE_AR=${SwiftTesting_MACRO_AR}
37-
-DCMAKE_RANLIB=${SwiftTesting_MACRO_RANLIB}
38-
-DCMAKE_MAKE_PROGRAM=${SwiftTesting_MACRO_MAKE_PROGRAM}
39-
-DSwiftTesting_BuildMacrosAsExecutables=${SwiftTesting_BuildMacrosAsExecutables}
40-
-DSwiftSyntax_DIR=${SwiftSyntax_DIR}
41-
-DCMAKE_INSTALL_PREFIX=<INSTALL_DIR>)
42-
ExternalProject_Get_Property(TestingMacros BINARY_DIR)
43-
ExternalProject_Get_Property(TestingMacros INSTALL_DIR)
43+
# Build and install the plugin into the current build directry.
44+
set(SwiftTesting_MACRO_INSTALL_PREFIX "${CMAKE_BINARY_DIR}/plugin")
4445

45-
include(AvailabilityDefinitions)
46-
include(CompilerSettings)
47-
add_subdirectory(_TestingInternals)
48-
add_subdirectory(Testing)
46+
ExternalProject_Add(TestingMacros
47+
PREFIX "tm"
48+
SOURCE_DIR "${CMAKE_CURRENT_SOURCE_DIR}/TestingMacros"
49+
BUILD_ALWAYS ON
50+
CMAKE_ARGS
51+
-DCMAKE_MAKE_PROGRAM=${SwiftTesting_MACRO_MAKE_PROGRAM}
52+
-DCMAKE_Swift_COMPILER=${SwiftTesting_MACRO_Swift_COMPILER}
53+
-DCMAKE_Swift_FLAGS=${SwiftTesting_MACRO_Swift_FLAGS}
54+
-DCMAKE_Swift_FLAGS_RELEASE=${SwiftTesting_MACRO_Swift_FLAGS_RELEASE}
55+
-DCMAKE_Swift_FLAGS_RELWITHDEBINFO=${SwiftTesting_MACRO_Swift_FLAGS_RELWITHDEBINFO}
56+
-DCMAKE_AR=${SwiftTesting_MACRO_AR}
57+
-DCMAKE_RANLIB=${SwiftTesting_MACRO_RANLIB}
58+
-DCMAKE_BUILD_TYPE=${CSwiftTesting_MACRO_BUILD_TYPE}
59+
-DSwiftTesting_BuildMacrosAsExecutables=${SwiftTesting_BuildMacrosAsExecutables}
60+
-DSwiftSyntax_DIR=${SwiftSyntax_DIR}
61+
-DCMAKE_INSTALL_PREFIX=${SwiftTesting_MACRO_INSTALL_PREFIX})
4962

50-
if(NOT SwiftTesting_BuildMacrosAsExecutables)
51-
# Hardcode the known library names based on system name as a workaround since
63+
# Hardcode the known file names based on system name as a workaround since
5264
# TestingMacros uses `ExternalProject` and we cannot directly query the
5365
# properties of its targets here.
54-
if("${CMAKE_SYSTEM_NAME}" STREQUAL "Darwin")
55-
set(SwiftTesting_TestingMacrosLibraryName "libTestingMacros.dylib")
56-
elseif("${CMAKE_SYSTEM_NAME}" STREQUAL "Linux")
57-
set(SwiftTesting_TestingMacrosLibraryName "libTestingMacros.so")
66+
if(NOT SwiftTesting_BuildMacrosAsExecutables)
67+
if(CMAKE_HOST_SYSTEM_NAME STREQUAL "Darwin")
68+
set(SwiftTesting_MACRO_PATH "${SwiftTesting_MACRO_INSTALL_PREFIX}/lib/libTestingMacros.dylib")
69+
install(PROGRAMS "${SwiftTesting_MACRO_PATH}"
70+
DESTINATION lib/swift/host/plugins/testing)
71+
elseif(CMAKE_HOST_SYSTEM_NAME STREQUAL "Linux")
72+
set(SwiftTesting_MACRO_PATH "${SwiftTesting_MACRO_INSTALL_PREFIX}/lib/libTestingMacros.so")
73+
install(PROGRAMS "${SwiftTesting_MACRO_PATH}"
74+
DESTINATION lib/swift/host/plugins)
75+
elseif(CMAKE_HOST_SYSTEM_NAME STREQUAL "Windows")
76+
set(SwiftTesting_MACRO_PATH "${SwiftTesting_MACRO_INSTALL_PREFIX}/bin/TestingMacros.dll")
77+
install(PROGRAMS "${SwiftTesting_MACRO_PATH}"
78+
DESTINATION bin)
79+
else()
80+
message(FATAL_ERROR "Unable to determine the library name for TestingMacros based on system name: ${CMAKE_HOST_SYSTEM_NAME}")
81+
endif()
5882
else()
59-
message(FATAL_ERROR "Unable to determine the library name for TestingMacros based on system name: ${CMAKE_SYSTEM_NAME}")
83+
if(CMAKE_HOST_SYSTEM_NAME STREQUAL "Windows")
84+
set(SwiftTesting_MACRO_PATH "${SwiftTesting_MACRO_INSTALL_PREFIX}/bin/TestingMacros.exe")
85+
else()
86+
set(SwiftTesting_MACRO_PATH "${SwiftTesting_MACRO_INSTALL_PREFIX}/bin/TestingMacros")
87+
endif()
6088
endif()
89+
elseif(SwiftTesting_MACRO)
90+
# Use the passed-in plugin path.
91+
set(SwiftTesting_MACRO_PATH "${SwiftTesting_MACRO}")
92+
add_custom_target(TestingMacros DEPENDS "${SwiftTesting_MACRO_PATH}")
93+
else()
94+
# If it's explicitly "NO", do not compile the library with macros.
95+
add_custom_target(TestingMacros)
96+
endif()
6197

62-
install(PROGRAMS "${INSTALL_DIR}/lib/${SwiftTesting_TestingMacrosLibraryName}"
63-
# TODO: Finalize the install path
64-
DESTINATION lib/swift/host/plugins)
98+
if(NOT SwiftTesting_MACRO_PATH)
99+
message(STATUS "TestingMacros: (none)")
100+
elseif(SwiftTesting_MACRO_PATH)
101+
if(SwiftTesting_MACRO_PATH MATCHES [[\.(dylib|so|dll)$]])
102+
message(STATUS "TestingMacros: ${SwiftTesting_MACRO_PATH} (shared library)")
103+
add_compile_options("$<$<COMPILE_LANGUAGE:Swift>:SHELL:-load-plugin-library ${SwiftTesting_MACRO_PATH}>")
104+
else()
105+
message(STATUS "TestingMacros: ${SwiftTesting_MACRO_PATH} (executable)")
106+
add_compile_options("$<$<COMPILE_LANGUAGE:Swift>:SHELL:-load-plugin-exectuable ${SwiftTesting_MACRO_PATH}#TestingMacros>")
107+
endif()
65108
endif()
109+
110+
include(AvailabilityDefinitions)
111+
include(CompilerSettings)
112+
add_subdirectory(_TestingInternals)
113+
add_subdirectory(Testing)

Sources/Testing/CMakeLists.txt

-16
Original file line numberDiff line numberDiff line change
@@ -100,21 +100,5 @@ target_compile_options(Testing PRIVATE
100100
-enable-library-evolution
101101
-emit-module-interface -emit-module-interface-path $<TARGET_PROPERTY:Testing,Swift_MODULE_DIRECTORY>/Testing.swiftinterface)
102102

103-
if(SwiftTesting_BuildMacrosAsExecutables)
104-
if(CMAKE_HOST_WIN32)
105-
set(_TestingMacros_ExecutableSuffix ".exe")
106-
endif()
107-
108-
target_compile_options(Testing PUBLIC
109-
"SHELL:$<$<COMPILE_LANGUAGE:Swift>:-load-plugin-executable ${BINARY_DIR}/TestingMacros${_TestingMacros_ExecutableSuffix}#TestingMacros>")
110-
else()
111-
target_compile_options(Testing PUBLIC
112-
"SHELL:$<$<COMPILE_LANGUAGE:Swift>:-plugin-path ${BINARY_DIR}>")
113-
endif()
114-
115-
# TODO: @rpath-relative install name
116-
# This may or may not involve the `-(no-)toolchain-stdlib-rpath` Swift flag.
117-
118103
include(SwiftModuleInstallation)
119-
# TODO: Finalize the install path
120104
_swift_testing_install_target(Testing)

Sources/TestingMacros/CMakeLists.txt

+13-6
Original file line numberDiff line numberDiff line change
@@ -59,12 +59,19 @@ if(SwiftTesting_BuildMacrosAsExecutables)
5959
else()
6060
add_library(TestingMacros SHARED)
6161

62-
# Install the macro plugin to the default location.
63-
#
64-
# This is not the plugin's actual, final install location -- it's only
65-
# relative to this external project's install dir. The final location is
66-
# determined by the "outer" project.
67-
install(TARGETS TestingMacros)
62+
target_link_options(TestingMacros PRIVATE "-no-toolchain-stdlib-rpath")
63+
# Not setting RPATH means it requires all the dependencies are already loaded
64+
# in the process, because 'plugin' directory wouldn't contain any dependencies.
65+
set_property(TARGET TestingMacros PROPERTY INSTALL_RPATH)
66+
set_property(TARGET TestingMacros PROPERTY BUILD_WITH_INSTALL_RPATH YES)
67+
68+
if(CMAKE_SYSTEM_NAME STREQUAL "Windows")
69+
install(TARGETS TestingMacros
70+
RUNTIME DESTINATION bin)
71+
else()
72+
# Other platforms, TestingMacros is installed by the parent project.
73+
install(TARGETS TestingMacros)
74+
endif()
6875
endif()
6976

7077
target_sources(TestingMacros PRIVATE

cmake/modules/SwiftModuleInstallation.cmake

+23-7
Original file line numberDiff line numberDiff line change
@@ -28,9 +28,21 @@ function(_swift_testing_install_target module)
2828
set(swift swift)
2929
endif()
3030

31+
target_compile_options(Testing PRIVATE "-no-toolchain-stdlib-rpath")
32+
33+
if(CMAKE_SYSTEM_NAME STREQUAL "Darwin")
34+
set(lib_destination_dir "lib/${swift}/${swift_os}/testing")
35+
set_property(TARGET ${module} PROPERTY
36+
INSTALL_RPATH "@loader_path/..")
37+
else()
38+
set(lib_destination_dir "lib/${swift}/${swift_os}")
39+
set_property(TARGET ${module} PROPERTY
40+
INSTALL_RPATH "$ORIGIN")
41+
endif()
42+
3143
install(TARGETS ${module}
32-
ARCHIVE DESTINATION lib/${swift}/${swift_os}
33-
LIBRARY DESTINATION lib/${swift}/${swift_os}
44+
ARCHIVE DESTINATION "${lib_destination_dir}"
45+
LIBRARY DESTINATION "${lib_destination_dir}"
3446
RUNTIME DESTINATION ${CMAKE_INSTALL_BINDIR})
3547
if(type STREQUAL EXECUTABLE)
3648
return()
@@ -52,13 +64,17 @@ function(_swift_testing_install_target module)
5264
mark_as_advanced(SwiftTesting_MODULE_TRIPLE)
5365
endif()
5466

67+
set(module_dir "${lib_destination_dir}/${module_name}.swiftmodule")
5568
install(FILES $<TARGET_PROPERTY:${module},Swift_MODULE_DIRECTORY>/${module_name}.swiftdoc
56-
DESTINATION lib/${swift}/${swift_os}/${module_name}.swiftmodule
69+
DESTINATION "${module_dir}"
5770
RENAME ${SwiftTesting_MODULE_TRIPLE}.swiftdoc)
5871
install(FILES $<TARGET_PROPERTY:${module},Swift_MODULE_DIRECTORY>/${module_name}.swiftmodule
59-
DESTINATION lib/${swift}/${swift_os}/${module_name}.swiftmodule
72+
DESTINATION "${module_dir}"
6073
RENAME ${SwiftTesting_MODULE_TRIPLE}.swiftmodule)
61-
install(FILES $<TARGET_PROPERTY:${module},Swift_MODULE_DIRECTORY>/${module_name}.swiftinterface
62-
DESTINATION lib/${swift}/${swift_os}/${module_name}.swiftmodule
63-
RENAME ${SwiftTesting_MODULE_TRIPLE}.swiftinterface)
74+
if(CMAKE_SYSTEM_NAME STREQUAL "Darwin")
75+
# Only Darwin has stable ABI.
76+
install(FILES $<TARGET_PROPERTY:${module},Swift_MODULE_DIRECTORY>/${module_name}.swiftinterface
77+
DESTINATION "${module_dir}"
78+
RENAME ${SwiftTesting_MODULE_TRIPLE}.swiftinterface)
79+
endif()
6480
endfunction()

0 commit comments

Comments
 (0)