From 13c9cf926b1a7d27104e6093a77666f96258d6da Mon Sep 17 00:00:00 2001 From: Jonathan Grynspan Date: Thu, 29 Aug 2024 10:34:47 -0400 Subject: [PATCH 1/3] Expose the target triple as a separate string from the testing library version. --- Package.swift | 2 +- .../Event.HumanReadableOutputRecorder.swift | 3 ++ Sources/Testing/Support/Versions.swift | 7 ++++ Sources/_TestingInternals/CMakeLists.txt | 1 + Sources/_TestingInternals/Versions.cpp | 36 +++++++++++++++++-- Sources/_TestingInternals/include/Stubs.h | 18 ---------- Sources/_TestingInternals/include/Versions.h | 16 +++++++++ cmake/modules/LibraryVersion.cmake | 2 +- cmake/modules/TargetTriple.cmake | 22 ++++++++++++ 9 files changed, 84 insertions(+), 23 deletions(-) create mode 100644 cmake/modules/TargetTriple.cmake diff --git a/Package.swift b/Package.swift index a97b5e55f..135bb864e 100644 --- a/Package.swift +++ b/Package.swift @@ -170,7 +170,7 @@ extension Array where Element == PackageDescription.CXXSetting { } else { git.currentCommit } - result.append(.define("_SWT_TESTING_LIBRARY_VERSION", to: #""\#(testingLibraryVersion)""#)) + result.append(.define("SWT_TESTING_LIBRARY_VERSION", to: #""\#(testingLibraryVersion)""#)) } return result diff --git a/Sources/Testing/Events/Recorder/Event.HumanReadableOutputRecorder.swift b/Sources/Testing/Events/Recorder/Event.HumanReadableOutputRecorder.swift index 75444e629..cfc2c396e 100644 --- a/Sources/Testing/Events/Recorder/Event.HumanReadableOutputRecorder.swift +++ b/Sources/Testing/Events/Recorder/Event.HumanReadableOutputRecorder.swift @@ -310,6 +310,9 @@ extension Event.HumanReadableOutputRecorder { comments.append("Swift Version: \(swiftStandardLibraryVersion)") } comments.append("Testing Library Version: \(testingLibraryVersion)") + if let targetTriple { + comments.append("Target Platform: \(targetTriple)") + } if verbosity > 0 { #if targetEnvironment(simulator) comments.append("OS Version (Simulator): \(simulatorVersion)") diff --git a/Sources/Testing/Support/Versions.swift b/Sources/Testing/Support/Versions.swift index 238a03b8f..e14cc450f 100644 --- a/Sources/Testing/Support/Versions.swift +++ b/Sources/Testing/Support/Versions.swift @@ -132,6 +132,13 @@ var testingLibraryVersion: String { swt_getTestingLibraryVersion().flatMap(String.init(validatingCString:)) ?? "unknown" } +/// Get the LLVM target triple used to build the testing library, if available. +/// +/// This value is not part of the public interface of the testing library. +var targetTriple: String? { + swt_getTargetTriple().flatMap(String.init(validatingCString:)) +} + /// A human-readable string describing the Swift Standard Library's version. /// /// This value's format is platform-specific and is not meant to be diff --git a/Sources/_TestingInternals/CMakeLists.txt b/Sources/_TestingInternals/CMakeLists.txt index 923450b51..b0bde88ed 100644 --- a/Sources/_TestingInternals/CMakeLists.txt +++ b/Sources/_TestingInternals/CMakeLists.txt @@ -9,6 +9,7 @@ set(CMAKE_CXX_SCAN_FOR_MODULES 0) include(LibraryVersion) +include(TargetTriple) add_library(_TestingInternals STATIC Discovery.cpp Versions.cpp diff --git a/Sources/_TestingInternals/Versions.cpp b/Sources/_TestingInternals/Versions.cpp index e9f0db698..2f5e1959a 100644 --- a/Sources/_TestingInternals/Versions.cpp +++ b/Sources/_TestingInternals/Versions.cpp @@ -10,11 +10,41 @@ #include "Versions.h" -const char *swt_getTestingLibraryVersion(void) { #if defined(_SWT_TESTING_LIBRARY_VERSION) - return _SWT_TESTING_LIBRARY_VERSION; +#warning _SWT_TESTING_LIBRARY_VERSION is deprecated +#warning Define SWT_TESTING_LIBRARY_VERSION and optionally SWT_TARGET_TRIPLE instead +#endif + +const char *swt_getTestingLibraryVersion(void) { +#if defined(SWT_TESTING_LIBRARY_VERSION) + return SWT_TESTING_LIBRARY_VERSION; #else -#warning _SWT_TESTING_LIBRARY_VERSION not defined: testing library version is unavailable +#warning SWT_TESTING_LIBRARY_VERSION not defined: testing library version is unavailable return nullptr; #endif } + +const char *swt_getTargetTriple(void) { +#if defined(SWT_TARGET_TRIPLE) + return SWT_TARGET_TRIPLE; +#else + // If we're here, we're presumably building as a package. Swift Package + // Manager does not provide a way to get the target triple from within the + // package manifest. SEE: swift-package-manager-#7929 + // + // clang has __is_target_*() intrinsics, but we don't want to play a game of + // Twenty Questions in order to synthesize the triple (and still potentially + // get it wrong.) SEE: rdar://134933385 + return nullptr; +#endif +} + +#if defined(__wasi__) +const char *swt_getWASIVersion(void) { +#if defined(WASI_LIBC_VERSION) + return WASI_LIBC_VERSION; +#else + return nullptr; +#endif +} +#endif diff --git a/Sources/_TestingInternals/include/Stubs.h b/Sources/_TestingInternals/include/Stubs.h index 2378b205c..b02eb4b0c 100644 --- a/Sources/_TestingInternals/include/Stubs.h +++ b/Sources/_TestingInternals/include/Stubs.h @@ -134,24 +134,6 @@ static int swt_siginfo_t_si_status(const siginfo_t *siginfo) { #endif #endif -#if defined(__wasi__) -/// Get the version of the C standard library and runtime used by WASI, if -/// available. -/// -/// This function is provided because `WASI_LIBC_VERSION` may or may not be -/// defined and may or may not be a complex macro. -/// -/// For more information about the `WASI_LIBC_VERSION` macro, see -/// [wasi-libc-#490](https://github.com/WebAssembly/wasi-libc/issues/490). -static const char *_Nullable swt_getWASIVersion(void) { -#if defined(WASI_LIBC_VERSION) - return WASI_LIBC_VERSION; -#else - return 0; -#endif -} -#endif - SWT_ASSUME_NONNULL_END #endif diff --git a/Sources/_TestingInternals/include/Versions.h b/Sources/_TestingInternals/include/Versions.h index e30aec043..c2e7e7964 100644 --- a/Sources/_TestingInternals/include/Versions.h +++ b/Sources/_TestingInternals/include/Versions.h @@ -23,6 +23,22 @@ SWT_ASSUME_NONNULL_BEGIN /// other conditions. Do not attempt to parse it. SWT_EXTERN const char *_Nullable swt_getTestingLibraryVersion(void); +/// Get the LLVM target triple used to build the testing library. +/// +/// - Returns: A string containing the LLVM target triple used to build the +/// testing library, or `nullptr` if that information is not available. +SWT_EXTERN const char *_Nullable swt_getTargetTriple(void); + +/// Get the version of the C standard library and runtime used by WASI, if +/// available. +/// +/// This function is provided because `WASI_LIBC_VERSION` may or may not be +/// defined and may or may not be a complex macro. +/// +/// For more information about the `WASI_LIBC_VERSION` macro, see +/// [wasi-libc-#490](https://github.com/WebAssembly/wasi-libc/issues/490). +SWT_EXTERN const char *_Nullable swt_getWASIVersion(void); + SWT_ASSUME_NONNULL_END #endif diff --git a/cmake/modules/LibraryVersion.cmake b/cmake/modules/LibraryVersion.cmake index e4e7fe24e..720d2c85c 100644 --- a/cmake/modules/LibraryVersion.cmake +++ b/cmake/modules/LibraryVersion.cmake @@ -40,4 +40,4 @@ endif() # All done! message(STATUS "Swift Testing version: ${SWT_TESTING_LIBRARY_VERSION}") add_compile_definitions( - "$<$:_SWT_TESTING_LIBRARY_VERSION=\"${SWT_TESTING_LIBRARY_VERSION}\">") + "$<$:SWT_TESTING_LIBRARY_VERSION=\"${SWT_TESTING_LIBRARY_VERSION}\">") diff --git a/cmake/modules/TargetTriple.cmake b/cmake/modules/TargetTriple.cmake new file mode 100644 index 000000000..e087cc47c --- /dev/null +++ b/cmake/modules/TargetTriple.cmake @@ -0,0 +1,22 @@ +# This source file is part of the Swift.org open source project +# +# Copyright (c) 2024 Apple Inc. and the Swift project authors +# Licensed under Apache License v2.0 with Runtime Library Exception +# +# See http://swift.org/LICENSE.txt for license information +# See http://swift.org/CONTRIBUTORS.txt for Swift project authors + +# Ask the Swift compiler what target triple it will be compiling with today. +set(SWT_TARGET_INFO_COMMAND "${CMAKE_Swift_COMPILER}" -print-target-info) +if(CMAKE_Swift_COMPILER_TARGET) + list(APPEND SWT_TARGET_INFO_COMMAND -target ${CMAKE_Swift_COMPILER_TARGET}) +endif() +execute_process(COMMAND ${SWT_TARGET_INFO_COMMAND} OUTPUT_VARIABLE SWT_TARGET_INFO_JSON) +string(JSON SWT_TARGET_TRIPLE GET "${SWT_TARGET_INFO_JSON}" "target" "unversionedTriple") + +# All done! +message(STATUS "Swift Testing target triple: ${SWT_TARGET_TRIPLE}") +if(SWT_TARGET_TRIPLE) + add_compile_definitions( + "$<$:SWT_TARGET_TRIPLE=\"${SWT_TARGET_TRIPLE}\">") +endif() From 3231f37516f3e052eaeadd08c9e4ebdfe02c73ea Mon Sep 17 00:00:00 2001 From: Jonathan Grynspan Date: Mon, 9 Sep 2024 09:21:59 -0400 Subject: [PATCH 2/3] Switch from WASI_LIBC_VERSION to WASI_SDK_VERSION while I'm in here --- Sources/Testing/Support/Versions.swift | 4 ++-- Sources/_TestingInternals/Versions.cpp | 11 ++++++++--- Sources/_TestingInternals/include/Versions.h | 4 ++-- 3 files changed, 12 insertions(+), 7 deletions(-) diff --git a/Sources/Testing/Support/Versions.swift b/Sources/Testing/Support/Versions.swift index e14cc450f..7bb68ab25 100644 --- a/Sources/Testing/Support/Versions.swift +++ b/Sources/Testing/Support/Versions.swift @@ -88,8 +88,8 @@ let operatingSystemVersion: String = { } } #elseif os(WASI) - if let libcVersion = swt_getWASIVersion().flatMap(String.init(validatingCString:)), !libcVersion.isEmpty { - return "WASI with libc \(libcVersion)" + if let version = swt_getWASIVersion().flatMap(String.init(validatingCString:)) { + return version } #else #warning("Platform-specific implementation missing: OS version unavailable") diff --git a/Sources/_TestingInternals/Versions.cpp b/Sources/_TestingInternals/Versions.cpp index 2f5e1959a..076aa86fb 100644 --- a/Sources/_TestingInternals/Versions.cpp +++ b/Sources/_TestingInternals/Versions.cpp @@ -10,9 +10,14 @@ #include "Versions.h" -#if defined(_SWT_TESTING_LIBRARY_VERSION) +#if defined(__wasi__) && __has_include() +#include +#endif + +#if defined(_SWT_TESTING_LIBRARY_VERSION) && !defined(SWT_TESTING_LIBRARY_VERSION) #warning _SWT_TESTING_LIBRARY_VERSION is deprecated #warning Define SWT_TESTING_LIBRARY_VERSION and optionally SWT_TARGET_TRIPLE instead +#define SWT_TESTING_LIBRARY_VERSION _SWT_TESTING_LIBRARY_VERSION #endif const char *swt_getTestingLibraryVersion(void) { @@ -41,8 +46,8 @@ const char *swt_getTargetTriple(void) { #if defined(__wasi__) const char *swt_getWASIVersion(void) { -#if defined(WASI_LIBC_VERSION) - return WASI_LIBC_VERSION; +#if defined(WASI_SDK_VERSION) + return WASI_SDK_VERSION; #else return nullptr; #endif diff --git a/Sources/_TestingInternals/include/Versions.h b/Sources/_TestingInternals/include/Versions.h index c2e7e7964..0a5ed1cf2 100644 --- a/Sources/_TestingInternals/include/Versions.h +++ b/Sources/_TestingInternals/include/Versions.h @@ -32,10 +32,10 @@ SWT_EXTERN const char *_Nullable swt_getTargetTriple(void); /// Get the version of the C standard library and runtime used by WASI, if /// available. /// -/// This function is provided because `WASI_LIBC_VERSION` may or may not be +/// This function is provided because `WASI_SDK_VERSION` may or may not be /// defined and may or may not be a complex macro. /// -/// For more information about the `WASI_LIBC_VERSION` macro, see +/// For more information about the `WASI_SDK_VERSION` macro, see /// [wasi-libc-#490](https://github.com/WebAssembly/wasi-libc/issues/490). SWT_EXTERN const char *_Nullable swt_getWASIVersion(void); From ce75228d6e4d0e2529f22d60f66c0711a4136926 Mon Sep 17 00:00:00 2001 From: Jonathan Grynspan Date: Mon, 9 Sep 2024 09:32:10 -0400 Subject: [PATCH 3/3] Make swt_getWASIVersion() static again so it can be fully inlined --- Sources/_TestingInternals/Versions.cpp | 14 -------- Sources/_TestingInternals/include/Includes.h | 36 +++++++++++++------- Sources/_TestingInternals/include/Versions.h | 11 +++++- 3 files changed, 33 insertions(+), 28 deletions(-) diff --git a/Sources/_TestingInternals/Versions.cpp b/Sources/_TestingInternals/Versions.cpp index 076aa86fb..9af19c50c 100644 --- a/Sources/_TestingInternals/Versions.cpp +++ b/Sources/_TestingInternals/Versions.cpp @@ -10,10 +10,6 @@ #include "Versions.h" -#if defined(__wasi__) && __has_include() -#include -#endif - #if defined(_SWT_TESTING_LIBRARY_VERSION) && !defined(SWT_TESTING_LIBRARY_VERSION) #warning _SWT_TESTING_LIBRARY_VERSION is deprecated #warning Define SWT_TESTING_LIBRARY_VERSION and optionally SWT_TARGET_TRIPLE instead @@ -43,13 +39,3 @@ const char *swt_getTargetTriple(void) { return nullptr; #endif } - -#if defined(__wasi__) -const char *swt_getWASIVersion(void) { -#if defined(WASI_SDK_VERSION) - return WASI_SDK_VERSION; -#else - return nullptr; -#endif -} -#endif diff --git a/Sources/_TestingInternals/include/Includes.h b/Sources/_TestingInternals/include/Includes.h index b7009456b..afa4477f5 100644 --- a/Sources/_TestingInternals/include/Includes.h +++ b/Sources/_TestingInternals/include/Includes.h @@ -39,15 +39,6 @@ #include #include -#if defined(__APPLE__) && !SWT_NO_MACH_PORTS -#include -#include -#endif - -#if defined(__APPLE__) && !SWT_NO_LIBDISPATCH -#include -#endif - #if __has_include() #include #endif @@ -106,10 +97,6 @@ #include #endif -#if __has_include() -#include -#endif - #if __has_include() #include #endif @@ -122,6 +109,19 @@ #include #endif +// MARK: - Platform-specific includes + +#if defined(__APPLE__) +#if !SWT_NO_MACH_PORTS +#include +#include +#endif + +#if !SWT_NO_LIBDISPATCH +#include +#endif +#endif + #if defined(_WIN32) #define WIN32_LEAN_AND_MEAN #define NOMINMAX @@ -129,6 +129,16 @@ #include #endif +#if defined(__wasi__) +#if __has_include() +#include +#endif + +#if __has_include() +#include +#endif +#endif + #if defined(__ANDROID__) #pragma clang module import posix_filesystem.linux_stat #include diff --git a/Sources/_TestingInternals/include/Versions.h b/Sources/_TestingInternals/include/Versions.h index 0a5ed1cf2..1be02ba33 100644 --- a/Sources/_TestingInternals/include/Versions.h +++ b/Sources/_TestingInternals/include/Versions.h @@ -12,6 +12,7 @@ #define SWT_VERSIONS_H #include "Defines.h" +#include "Includes.h" SWT_ASSUME_NONNULL_BEGIN @@ -29,6 +30,7 @@ SWT_EXTERN const char *_Nullable swt_getTestingLibraryVersion(void); /// testing library, or `nullptr` if that information is not available. SWT_EXTERN const char *_Nullable swt_getTargetTriple(void); +#if defined(__wasi__) /// Get the version of the C standard library and runtime used by WASI, if /// available. /// @@ -37,7 +39,14 @@ SWT_EXTERN const char *_Nullable swt_getTargetTriple(void); /// /// For more information about the `WASI_SDK_VERSION` macro, see /// [wasi-libc-#490](https://github.com/WebAssembly/wasi-libc/issues/490). -SWT_EXTERN const char *_Nullable swt_getWASIVersion(void); +static const char *_Nullable swt_getWASIVersion(void) { +#if defined(WASI_SDK_VERSION) + return WASI_SDK_VERSION; +#else + return 0; +#endif +} +#endif SWT_ASSUME_NONNULL_END