Skip to content

Commit d13d520

Browse files
committed
Expose the target triple as a separate string from the testing library version.
1 parent d00d469 commit d13d520

File tree

9 files changed

+84
-23
lines changed

9 files changed

+84
-23
lines changed

Package.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -170,7 +170,7 @@ extension Array where Element == PackageDescription.CXXSetting {
170170
} else {
171171
git.currentCommit
172172
}
173-
result.append(.define("_SWT_TESTING_LIBRARY_VERSION", to: #""\#(testingLibraryVersion)""#))
173+
result.append(.define("SWT_TESTING_LIBRARY_VERSION", to: #""\#(testingLibraryVersion)""#))
174174
}
175175

176176
return result

Sources/Testing/Events/Recorder/Event.HumanReadableOutputRecorder.swift

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -310,6 +310,9 @@ extension Event.HumanReadableOutputRecorder {
310310
comments.append("Swift Version: \(swiftStandardLibraryVersion)")
311311
}
312312
comments.append("Testing Library Version: \(testingLibraryVersion)")
313+
if let targetTriple {
314+
comments.append("Target Platform: \(targetTriple)")
315+
}
313316
if verbosity > 0 {
314317
#if targetEnvironment(simulator)
315318
comments.append("OS Version (Simulator): \(simulatorVersion)")

Sources/Testing/Support/Versions.swift

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -128,6 +128,13 @@ var testingLibraryVersion: String {
128128
swt_getTestingLibraryVersion().flatMap(String.init(validatingCString:)) ?? "unknown"
129129
}
130130

131+
/// Get the LLVM target triple used to build the testing library, if available.
132+
///
133+
/// This value is not part of the public interface of the testing library.
134+
var targetTriple: String? {
135+
swt_getTargetTriple().flatMap(String.init(validatingCString:))
136+
}
137+
131138
/// A human-readable string describing the Swift Standard Library's version.
132139
///
133140
/// This value's format is platform-specific and is not meant to be

Sources/_TestingInternals/CMakeLists.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
set(CMAKE_CXX_SCAN_FOR_MODULES 0)
1010

1111
include(LibraryVersion)
12+
include(TargetTriple)
1213
add_library(_TestingInternals STATIC
1314
Discovery.cpp
1415
Versions.cpp

Sources/_TestingInternals/Versions.cpp

Lines changed: 33 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -10,11 +10,41 @@
1010

1111
#include "Versions.h"
1212

13-
const char *swt_getTestingLibraryVersion(void) {
1413
#if defined(_SWT_TESTING_LIBRARY_VERSION)
15-
return _SWT_TESTING_LIBRARY_VERSION;
14+
#warning _SWT_TESTING_LIBRARY_VERSION is deprecated
15+
#warning Define SWT_TESTING_LIBRARY_VERSION and optionally SWT_TARGET_TRIPLE instead
16+
#endif
17+
18+
const char *swt_getTestingLibraryVersion(void) {
19+
#if defined(SWT_TESTING_LIBRARY_VERSION)
20+
return SWT_TESTING_LIBRARY_VERSION;
1621
#else
17-
#warning _SWT_TESTING_LIBRARY_VERSION not defined: testing library version is unavailable
22+
#warning SWT_TESTING_LIBRARY_VERSION not defined: testing library version is unavailable
1823
return nullptr;
1924
#endif
2025
}
26+
27+
const char *swt_getTargetTriple(void) {
28+
#if defined(SWT_TARGET_TRIPLE)
29+
return SWT_TARGET_TRIPLE;
30+
#else
31+
// If we're here, we're presumably building as a package. Swift Package
32+
// Manager does not provide a way to get the target triple from within the
33+
// package manifest. SEE: swift-package-manager-#7929
34+
//
35+
// clang has __is_target_*() intrinsics, but we don't want to play a game of
36+
// Twenty Questions in order to synthesize the triple (and still potentially
37+
// get it wrong.) SEE: rdar://134933385
38+
return nullptr;
39+
#endif
40+
}
41+
42+
#if defined(__wasi__)
43+
const char *swt_getWASIVersion(void) {
44+
#if defined(WASI_LIBC_VERSION)
45+
return WASI_LIBC_VERSION;
46+
#else
47+
return nullptr;
48+
#endif
49+
}
50+
#endif

Sources/_TestingInternals/include/Stubs.h

Lines changed: 0 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -121,24 +121,6 @@ static int swt_siginfo_t_si_status(const siginfo_t *siginfo) {
121121
}
122122
#endif
123123

124-
#if defined(__wasi__)
125-
/// Get the version of the C standard library and runtime used by WASI, if
126-
/// available.
127-
///
128-
/// This function is provided because `WASI_LIBC_VERSION` may or may not be
129-
/// defined and may or may not be a complex macro.
130-
///
131-
/// For more information about the `WASI_LIBC_VERSION` macro, see
132-
/// [wasi-libc-#490](https://github.com/WebAssembly/wasi-libc/issues/490).
133-
static const char *_Nullable swt_getWASIVersion(void) {
134-
#if defined(WASI_LIBC_VERSION)
135-
return WASI_LIBC_VERSION;
136-
#else
137-
return 0;
138-
#endif
139-
}
140-
#endif
141-
142124
SWT_ASSUME_NONNULL_END
143125

144126
#endif

Sources/_TestingInternals/include/Versions.h

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,22 @@ SWT_ASSUME_NONNULL_BEGIN
2323
/// other conditions. Do not attempt to parse it.
2424
SWT_EXTERN const char *_Nullable swt_getTestingLibraryVersion(void);
2525

26+
/// Get the LLVM target triple used to build the testing library.
27+
///
28+
/// - Returns: A string containing the LLVM target triple used to build the
29+
/// testing library, or `nullptr` if that information is not available.
30+
SWT_EXTERN const char *_Nullable swt_getTargetTriple(void);
31+
32+
/// Get the version of the C standard library and runtime used by WASI, if
33+
/// available.
34+
///
35+
/// This function is provided because `WASI_LIBC_VERSION` may or may not be
36+
/// defined and may or may not be a complex macro.
37+
///
38+
/// For more information about the `WASI_LIBC_VERSION` macro, see
39+
/// [wasi-libc-#490](https://github.com/WebAssembly/wasi-libc/issues/490).
40+
SWT_EXTERN const char *_Nullable swt_getWASIVersion(void);
41+
2642
SWT_ASSUME_NONNULL_END
2743

2844
#endif

cmake/modules/LibraryVersion.cmake

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -40,4 +40,4 @@ endif()
4040
# All done!
4141
message(STATUS "Swift Testing version: ${SWT_TESTING_LIBRARY_VERSION}")
4242
add_compile_definitions(
43-
"$<$<COMPILE_LANGUAGE:CXX>:_SWT_TESTING_LIBRARY_VERSION=\"${SWT_TESTING_LIBRARY_VERSION}\">")
43+
"$<$<COMPILE_LANGUAGE:CXX>:SWT_TESTING_LIBRARY_VERSION=\"${SWT_TESTING_LIBRARY_VERSION}\">")

cmake/modules/TargetTriple.cmake

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
# This source file is part of the Swift.org open source project
2+
#
3+
# Copyright (c) 2024 Apple Inc. and the Swift project authors
4+
# Licensed under Apache License v2.0 with Runtime Library Exception
5+
#
6+
# See http://swift.org/LICENSE.txt for license information
7+
# See http://swift.org/CONTRIBUTORS.txt for Swift project authors
8+
9+
# Ask the Swift compiler what target triple it will be compiling with today.
10+
set(SWT_TARGET_INFO_COMMAND "${CMAKE_Swift_COMPILER}" -print-target-info)
11+
if(CMAKE_Swift_COMPILER_TARGET)
12+
list(APPEND SWT_TARGET_INFO_COMMAND -target ${CMAKE_Swift_COMPILER_TARGET})
13+
endif()
14+
execute_process(COMMAND ${SWT_TARGET_INFO_COMMAND} OUTPUT_VARIABLE SWT_TARGET_INFO_JSON)
15+
string(JSON SWT_TARGET_TRIPLE GET "${SWT_TARGET_INFO_JSON}" "target" "unversionedTriple")
16+
17+
# All done!
18+
message(STATUS "Swift Testing target triple: ${SWT_TARGET_TRIPLE}")
19+
if(SWT_TARGET_TRIPLE)
20+
add_compile_definitions(
21+
"$<$<COMPILE_LANGUAGE:CXX>:SWT_TARGET_TRIPLE=\"${SWT_TARGET_TRIPLE}\">")
22+
endif()

0 commit comments

Comments
 (0)