Skip to content

Commit a0c8fc6

Browse files
authored
Expose the target triple as a separate string from the testing library version. (#652)
This PR adds a script to our CMake build to capture the target triple being used to build Swift Testing so that we can include it in our diagnostic output. Apple's fork of Swift Testing (as included with Xcode 16) also includes this information, and it is very useful to have when reviewing bug reports as it can tell us if a problem is specific to a given architecture or OS variant. ### Checklist: - [x] Code and documentation should follow the style of the [Style Guide](https://github.com/apple/swift-testing/blob/main/Documentation/StyleGuide.md). - [x] If public symbols are renamed or modified, DocC references should be updated.
1 parent f6af3d8 commit a0c8fc6

File tree

10 files changed

+109
-38
lines changed

10 files changed

+109
-38
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: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -88,8 +88,8 @@ let operatingSystemVersion: String = {
8888
}
8989
}
9090
#elseif os(WASI)
91-
if let libcVersion = swt_getWASIVersion().flatMap(String.init(validatingCString:)), !libcVersion.isEmpty {
92-
return "WASI with libc \(libcVersion)"
91+
if let version = swt_getWASIVersion().flatMap(String.init(validatingCString:)) {
92+
return version
9393
}
9494
#else
9595
#warning("Platform-specific implementation missing: OS version unavailable")
@@ -132,6 +132,13 @@ var testingLibraryVersion: String {
132132
swt_getTestingLibraryVersion().flatMap(String.init(validatingCString:)) ?? "unknown"
133133
}
134134

135+
/// Get the LLVM target triple used to build the testing library, if available.
136+
///
137+
/// This value is not part of the public interface of the testing library.
138+
var targetTriple: String? {
139+
swt_getTargetTriple().flatMap(String.init(validatingCString:))
140+
}
141+
135142
/// A human-readable string describing the Swift Standard Library's version.
136143
///
137144
/// 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: 24 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -10,11 +10,32 @@
1010

1111
#include "Versions.h"
1212

13+
#if defined(_SWT_TESTING_LIBRARY_VERSION) && !defined(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+
#define SWT_TESTING_LIBRARY_VERSION _SWT_TESTING_LIBRARY_VERSION
17+
#endif
18+
1319
const char *swt_getTestingLibraryVersion(void) {
14-
#if defined(_SWT_TESTING_LIBRARY_VERSION)
15-
return _SWT_TESTING_LIBRARY_VERSION;
20+
#if defined(SWT_TESTING_LIBRARY_VERSION)
21+
return SWT_TESTING_LIBRARY_VERSION;
22+
#else
23+
#warning SWT_TESTING_LIBRARY_VERSION not defined: testing library version is unavailable
24+
return nullptr;
25+
#endif
26+
}
27+
28+
const char *swt_getTargetTriple(void) {
29+
#if defined(SWT_TARGET_TRIPLE)
30+
return SWT_TARGET_TRIPLE;
1631
#else
17-
#warning _SWT_TESTING_LIBRARY_VERSION not defined: testing library version is unavailable
32+
// If we're here, we're presumably building as a package. Swift Package
33+
// Manager does not provide a way to get the target triple from within the
34+
// package manifest. SEE: swift-package-manager-#7929
35+
//
36+
// clang has __is_target_*() intrinsics, but we don't want to play a game of
37+
// Twenty Questions in order to synthesize the triple (and still potentially
38+
// get it wrong.) SEE: rdar://134933385
1839
return nullptr;
1940
#endif
2041
}

Sources/_TestingInternals/include/Includes.h

Lines changed: 23 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -39,15 +39,6 @@
3939
#include <string.h>
4040
#include <time.h>
4141

42-
#if defined(__APPLE__) && !SWT_NO_MACH_PORTS
43-
#include <mach/mach_init.h>
44-
#include <mach/task.h>
45-
#endif
46-
47-
#if defined(__APPLE__) && !SWT_NO_LIBDISPATCH
48-
#include <dispatch/dispatch.h>
49-
#endif
50-
5142
#if __has_include(<unistd.h>)
5243
#include <unistd.h>
5344
#endif
@@ -106,10 +97,6 @@
10697
#include <crt_externs.h>
10798
#endif
10899

109-
#if __has_include(<wasi/libc-environ.h>)
110-
#include <wasi/libc-environ.h>
111-
#endif
112-
113100
#if __has_include(<libgen.h>)
114101
#include <libgen.h>
115102
#endif
@@ -122,13 +109,36 @@
122109
#include <sysexits.h>
123110
#endif
124111

112+
// MARK: - Platform-specific includes
113+
114+
#if defined(__APPLE__)
115+
#if !SWT_NO_MACH_PORTS
116+
#include <mach/mach_init.h>
117+
#include <mach/task.h>
118+
#endif
119+
120+
#if !SWT_NO_LIBDISPATCH
121+
#include <dispatch/dispatch.h>
122+
#endif
123+
#endif
124+
125125
#if defined(_WIN32)
126126
#define WIN32_LEAN_AND_MEAN
127127
#define NOMINMAX
128128
#include <Windows.h>
129129
#include <Psapi.h>
130130
#endif
131131

132+
#if defined(__wasi__)
133+
#if __has_include(<wasi/libc-environ.h>)
134+
#include <wasi/libc-environ.h>
135+
#endif
136+
137+
#if __has_include(<wasi/version.h>)
138+
#include <wasi/version.h>
139+
#endif
140+
#endif
141+
132142
#if defined(__ANDROID__)
133143
#pragma clang module import posix_filesystem.linux_stat
134144
#include <sys/system_properties.h>

Sources/_TestingInternals/include/Stubs.h

Lines changed: 0 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -134,24 +134,6 @@ static int swt_siginfo_t_si_status(const siginfo_t *siginfo) {
134134
#endif
135135
#endif
136136

137-
#if defined(__wasi__)
138-
/// Get the version of the C standard library and runtime used by WASI, if
139-
/// available.
140-
///
141-
/// This function is provided because `WASI_LIBC_VERSION` may or may not be
142-
/// defined and may or may not be a complex macro.
143-
///
144-
/// For more information about the `WASI_LIBC_VERSION` macro, see
145-
/// [wasi-libc-#490](https://github.com/WebAssembly/wasi-libc/issues/490).
146-
static const char *_Nullable swt_getWASIVersion(void) {
147-
#if defined(WASI_LIBC_VERSION)
148-
return WASI_LIBC_VERSION;
149-
#else
150-
return 0;
151-
#endif
152-
}
153-
#endif
154-
155137
SWT_ASSUME_NONNULL_END
156138

157139
#endif

Sources/_TestingInternals/include/Versions.h

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212
#define SWT_VERSIONS_H
1313

1414
#include "Defines.h"
15+
#include "Includes.h"
1516

1617
SWT_ASSUME_NONNULL_BEGIN
1718

@@ -23,6 +24,30 @@ SWT_ASSUME_NONNULL_BEGIN
2324
/// other conditions. Do not attempt to parse it.
2425
SWT_EXTERN const char *_Nullable swt_getTestingLibraryVersion(void);
2526

27+
/// Get the LLVM target triple used to build the testing library.
28+
///
29+
/// - Returns: A string containing the LLVM target triple used to build the
30+
/// testing library, or `nullptr` if that information is not available.
31+
SWT_EXTERN const char *_Nullable swt_getTargetTriple(void);
32+
33+
#if defined(__wasi__)
34+
/// Get the version of the C standard library and runtime used by WASI, if
35+
/// available.
36+
///
37+
/// This function is provided because `WASI_SDK_VERSION` may or may not be
38+
/// defined and may or may not be a complex macro.
39+
///
40+
/// For more information about the `WASI_SDK_VERSION` macro, see
41+
/// [wasi-libc-#490](https://github.com/WebAssembly/wasi-libc/issues/490).
42+
static const char *_Nullable swt_getWASIVersion(void) {
43+
#if defined(WASI_SDK_VERSION)
44+
return WASI_SDK_VERSION;
45+
#else
46+
return 0;
47+
#endif
48+
}
49+
#endif
50+
2651
SWT_ASSUME_NONNULL_END
2752

2853
#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)