From 897ea5d480c9456b7944f02a9ef80dd3404af2a9 Mon Sep 17 00:00:00 2001 From: Jonathan Grynspan Date: Tue, 9 Sep 2025 15:39:57 -0400 Subject: [PATCH 01/12] Move the package version to version.txt. This change moves the definition of Swift Testing's version to a new file, version.txt. This file doesn't represent a standard, _per se_, but it is common for open-source projects to list their version info in such a file. And by putting our version info in this file, we can share it between package builds and CMake builds. --- Package.swift | 6 ------ Sources/_TestingInternals/CMakeLists.txt | 1 - Sources/_TestingInternals/Versions.cpp | 24 +++++++++++++++++++++++- cmake/modules/LibraryVersion.cmake | 11 +++-------- version.txt | 9 +++++++++ 5 files changed, 35 insertions(+), 16 deletions(-) create mode 100644 version.txt diff --git a/Package.swift b/Package.swift index 840541887..b5cfd234c 100644 --- a/Package.swift +++ b/Package.swift @@ -470,12 +470,6 @@ extension Array where Element == PackageDescription.CXXSetting { // Capture the testing library's commit info as C++ constants. if let git { - if let tag = git.currentTag { - result.append(.define("SWT_TESTING_LIBRARY_VERSION", to: #""\#(tag)""#)) - } else { - result.append(.define("SWT_TESTING_LIBRARY_VERSION", to: "0")) - } - result.append(.define("SWT_TESTING_LIBRARY_COMMIT_HASH", to: #""\#(git.currentCommit)""#)) if git.hasUncommittedChanges { result.append(.define("SWT_TESTING_LIBRARY_COMMIT_MODIFIED", to: "1")) diff --git a/Sources/_TestingInternals/CMakeLists.txt b/Sources/_TestingInternals/CMakeLists.txt index 2b385cb1b..b8c1117c7 100644 --- a/Sources/_TestingInternals/CMakeLists.txt +++ b/Sources/_TestingInternals/CMakeLists.txt @@ -9,7 +9,6 @@ set(CMAKE_CXX_SCAN_FOR_MODULES 0) include(GitCommit) -include(LibraryVersion) include(TargetTriple) add_library(_TestingInternals STATIC Discovery.cpp diff --git a/Sources/_TestingInternals/Versions.cpp b/Sources/_TestingInternals/Versions.cpp index 8e85f3d06..bbd748b99 100644 --- a/Sources/_TestingInternals/Versions.cpp +++ b/Sources/_TestingInternals/Versions.cpp @@ -10,11 +10,33 @@ #include "Versions.h" +#include + const char *swt_getTestingLibraryVersion(void) { #if defined(SWT_TESTING_LIBRARY_VERSION) + // The current environment explicitly specifies a version string to return. return SWT_TESTING_LIBRARY_VERSION; +#elif __has_embed("../../version.txt") + // Read the version from version.txt at the root of the package's repository. + static char version[] = { +#pragma clang diagnostic push +#pragma clang diagnostic ignored "-Wc23-extensions" +#embed "../../version.txt" suffix(, '\0') +#pragma clang diagnostic pop + }; + + // Trim any trailing whitespace. + static std::once_flag once; + std::call_once(once, [] { + auto i = std::find_if(std::begin(version), std::end(version), std::isspace); + if (i != std::end(version)) { + *i = '\0'; + } + }); + + return version; #else -#warning SWT_TESTING_LIBRARY_VERSION not defined: testing library version is unavailable +#warning SWT_TESTING_LIBRARY_VERSION not defined and version.txt not found: testing library version is unavailable return nullptr; #endif } diff --git a/cmake/modules/LibraryVersion.cmake b/cmake/modules/LibraryVersion.cmake index 32901365a..d9a9441f8 100644 --- a/cmake/modules/LibraryVersion.cmake +++ b/cmake/modules/LibraryVersion.cmake @@ -1,15 +1,10 @@ # This source file is part of the Swift.org open source project # -# Copyright (c) 2024 Apple Inc. and the Swift project authors +# Copyright (c) 2024–2025 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 -# The current version of the Swift Testing release. For release branches, -# remember to remove -dev. -set(SWT_TESTING_LIBRARY_VERSION "6.3-dev") - -message(STATUS "Swift Testing version: ${SWT_TESTING_LIBRARY_VERSION}") -add_compile_definitions( - "$<$:SWT_TESTING_LIBRARY_VERSION=\"${SWT_TESTING_LIBRARY_VERSION}\">") +# The library version is now tracked in version.txt at the root directory of the +# repository. This file will be removed in a future commit. diff --git a/version.txt b/version.txt new file mode 100644 index 000000000..b01513f51 --- /dev/null +++ b/version.txt @@ -0,0 +1,9 @@ +6.3-dev + +# This source file is part of the Swift.org open source project +# +# Copyright (c) 2025 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 From f568f5bd189dfaa32cf4ff8b8dcfa9629870a3fe Mon Sep 17 00:00:00 2001 From: Jonathan Grynspan Date: Tue, 9 Sep 2025 15:52:38 -0400 Subject: [PATCH 02/12] Missing include --- Sources/_TestingInternals/Versions.cpp | 1 + 1 file changed, 1 insertion(+) diff --git a/Sources/_TestingInternals/Versions.cpp b/Sources/_TestingInternals/Versions.cpp index bbd748b99..774edc5f7 100644 --- a/Sources/_TestingInternals/Versions.cpp +++ b/Sources/_TestingInternals/Versions.cpp @@ -10,6 +10,7 @@ #include "Versions.h" +#include #include const char *swt_getTestingLibraryVersion(void) { From 77abcb6df89198325369c0983874246ac19d077f Mon Sep 17 00:00:00 2001 From: Jonathan Grynspan Date: Tue, 9 Sep 2025 15:55:22 -0400 Subject: [PATCH 03/12] Missing include --- Sources/_TestingInternals/Versions.cpp | 1 + 1 file changed, 1 insertion(+) diff --git a/Sources/_TestingInternals/Versions.cpp b/Sources/_TestingInternals/Versions.cpp index 774edc5f7..5c359958f 100644 --- a/Sources/_TestingInternals/Versions.cpp +++ b/Sources/_TestingInternals/Versions.cpp @@ -11,6 +11,7 @@ #include "Versions.h" #include +#include #include const char *swt_getTestingLibraryVersion(void) { From 74bf395a98f3d5da499b0fb03e54ae6d3bfab99d Mon Sep 17 00:00:00 2001 From: Jonathan Grynspan Date: Tue, 9 Sep 2025 16:05:01 -0400 Subject: [PATCH 04/12] GCC headers don't define std::isspace() without a second argument and rely on it being a macro instead, I guess --- Sources/_TestingInternals/Versions.cpp | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/Sources/_TestingInternals/Versions.cpp b/Sources/_TestingInternals/Versions.cpp index 5c359958f..6f4b3b5fb 100644 --- a/Sources/_TestingInternals/Versions.cpp +++ b/Sources/_TestingInternals/Versions.cpp @@ -12,6 +12,7 @@ #include #include +#include #include const char *swt_getTestingLibraryVersion(void) { @@ -30,7 +31,7 @@ const char *swt_getTestingLibraryVersion(void) { // Trim any trailing whitespace. static std::once_flag once; std::call_once(once, [] { - auto i = std::find_if(std::begin(version), std::end(version), std::isspace); + auto i = std::find_if(std::begin(version), std::end(version), isspace); if (i != std::end(version)) { *i = '\0'; } From bc14da6d2728465fe3e909f93146c4efd497b2c0 Mon Sep 17 00:00:00 2001 From: Jonathan Grynspan Date: Thu, 11 Sep 2025 13:15:56 -0400 Subject: [PATCH 05/12] Zero-fill the embedded file --- Sources/_TestingInternals/Versions.cpp | 6 ++---- 1 file changed, 2 insertions(+), 4 deletions(-) diff --git a/Sources/_TestingInternals/Versions.cpp b/Sources/_TestingInternals/Versions.cpp index 6f4b3b5fb..f5933a13b 100644 --- a/Sources/_TestingInternals/Versions.cpp +++ b/Sources/_TestingInternals/Versions.cpp @@ -28,13 +28,11 @@ const char *swt_getTestingLibraryVersion(void) { #pragma clang diagnostic pop }; - // Trim any trailing whitespace. + // Zero any trailing characters (e.g. the copyright block.) static std::once_flag once; std::call_once(once, [] { auto i = std::find_if(std::begin(version), std::end(version), isspace); - if (i != std::end(version)) { - *i = '\0'; - } + std::fill(i, std::end(version), '\0'); }); return version; From afa1d9c32b154e46f842e1bed26ea2898b34ae45 Mon Sep 17 00:00:00 2001 From: Jonathan Grynspan Date: Thu, 11 Sep 2025 14:28:59 -0400 Subject: [PATCH 06/12] Don't need this constant anymore --- Package.swift | 1 - 1 file changed, 1 deletion(-) diff --git a/Package.swift b/Package.swift index b5cfd234c..e387f1453 100644 --- a/Package.swift +++ b/Package.swift @@ -477,7 +477,6 @@ extension Array where Element == PackageDescription.CXXSetting { } else if let gitHubSHA = Context.environment["GITHUB_SHA"] { // When building in GitHub Actions, the git command may fail to get us the // commit hash, so check if GitHub shared it with us instead. - result.append(.define("SWT_TESTING_LIBRARY_VERSION", to: "0")) result.append(.define("SWT_TESTING_LIBRARY_COMMIT_HASH", to: #""\#(gitHubSHA)""#)) } From be7d423f9d4d45e4ad601cb397ec3b929ad68f61 Mon Sep 17 00:00:00 2001 From: Jonathan Grynspan Date: Thu, 11 Sep 2025 15:12:52 -0400 Subject: [PATCH 07/12] Constant-fold the whole version.txt file --- Sources/_TestingInternals/Versions.cpp | 27 +++++++++++++------------- 1 file changed, 14 insertions(+), 13 deletions(-) diff --git a/Sources/_TestingInternals/Versions.cpp b/Sources/_TestingInternals/Versions.cpp index f5933a13b..600f2b142 100644 --- a/Sources/_TestingInternals/Versions.cpp +++ b/Sources/_TestingInternals/Versions.cpp @@ -10,32 +10,33 @@ #include "Versions.h" +#include #include -#include #include -#include const char *swt_getTestingLibraryVersion(void) { #if defined(SWT_TESTING_LIBRARY_VERSION) // The current environment explicitly specifies a version string to return. return SWT_TESTING_LIBRARY_VERSION; #elif __has_embed("../../version.txt") - // Read the version from version.txt at the root of the package's repository. - static char version[] = { + static constinit auto version = [] () constexpr { + // Read the version from version.txt at the root of the package's repo. + char version[] = { #pragma clang diagnostic push #pragma clang diagnostic ignored "-Wc23-extensions" -#embed "../../version.txt" suffix(, '\0') +#embed "../../version.txt" #pragma clang diagnostic pop - }; + }; - // Zero any trailing characters (e.g. the copyright block.) - static std::once_flag once; - std::call_once(once, [] { - auto i = std::find_if(std::begin(version), std::end(version), isspace); - std::fill(i, std::end(version), '\0'); - }); + // Copy the first line from the C string into a C array so that we can + // return it from this closure. + std::array result {}; + auto i = std::find(std::begin(version), std::end(version), '\n'); + std::copy(std::begin(version), i, result.begin()); + return result; + }(); - return version; + return version.data(); #else #warning SWT_TESTING_LIBRARY_VERSION not defined and version.txt not found: testing library version is unavailable return nullptr; From c4b6d0e785ded76f08b945145888580ad5cdd206 Mon Sep 17 00:00:00 2001 From: Jonathan Grynspan Date: Thu, 11 Sep 2025 15:34:17 -0400 Subject: [PATCH 08/12] Windows inserts a \r character, it seems --- Sources/_TestingInternals/Versions.cpp | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/Sources/_TestingInternals/Versions.cpp b/Sources/_TestingInternals/Versions.cpp index 600f2b142..dc76e9f12 100644 --- a/Sources/_TestingInternals/Versions.cpp +++ b/Sources/_TestingInternals/Versions.cpp @@ -31,7 +31,9 @@ const char *swt_getTestingLibraryVersion(void) { // Copy the first line from the C string into a C array so that we can // return it from this closure. std::array result {}; - auto i = std::find(std::begin(version), std::end(version), '\n'); + auto i = std::find_if(std::begin(version), std::end(version), [] (char c) { + return c == '\r' || c == '\n'; + }); std::copy(std::begin(version), i, result.begin()); return result; }(); From 30854f781aa41aaf965c500f463b00e362187ab5 Mon Sep 17 00:00:00 2001 From: Jonathan Grynspan Date: Thu, 11 Sep 2025 16:13:53 -0400 Subject: [PATCH 09/12] Remove comment from file as atypical --- VERSION.txt | 1 + version.txt | 9 --------- 2 files changed, 1 insertion(+), 9 deletions(-) create mode 100644 VERSION.txt delete mode 100644 version.txt diff --git a/VERSION.txt b/VERSION.txt new file mode 100644 index 000000000..a2b88412f --- /dev/null +++ b/VERSION.txt @@ -0,0 +1 @@ +6.3-dev diff --git a/version.txt b/version.txt deleted file mode 100644 index b01513f51..000000000 --- a/version.txt +++ /dev/null @@ -1,9 +0,0 @@ -6.3-dev - -# This source file is part of the Swift.org open source project -# -# Copyright (c) 2025 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 From 62d4bb86e8303d05937a22b34a67597b6e98af04 Mon Sep 17 00:00:00 2001 From: Jonathan Grynspan Date: Sun, 14 Sep 2025 09:03:33 -0400 Subject: [PATCH 10/12] Capitalize filename --- Sources/_TestingInternals/Versions.cpp | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/Sources/_TestingInternals/Versions.cpp b/Sources/_TestingInternals/Versions.cpp index dc76e9f12..eb0d59252 100644 --- a/Sources/_TestingInternals/Versions.cpp +++ b/Sources/_TestingInternals/Versions.cpp @@ -18,13 +18,13 @@ const char *swt_getTestingLibraryVersion(void) { #if defined(SWT_TESTING_LIBRARY_VERSION) // The current environment explicitly specifies a version string to return. return SWT_TESTING_LIBRARY_VERSION; -#elif __has_embed("../../version.txt") +#elif __has_embed("../../VERSION.txt") static constinit auto version = [] () constexpr { // Read the version from version.txt at the root of the package's repo. char version[] = { #pragma clang diagnostic push #pragma clang diagnostic ignored "-Wc23-extensions" -#embed "../../version.txt" +#embed "../../VERSION.txt" #pragma clang diagnostic pop }; From a5498fa6697debc69aaf150810b8cac21dec418d Mon Sep 17 00:00:00 2001 From: Jonathan Grynspan Date: Sun, 14 Sep 2025 09:05:33 -0400 Subject: [PATCH 11/12] Capitalize filename --- Sources/_TestingInternals/Versions.cpp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Sources/_TestingInternals/Versions.cpp b/Sources/_TestingInternals/Versions.cpp index eb0d59252..bd8f2314a 100644 --- a/Sources/_TestingInternals/Versions.cpp +++ b/Sources/_TestingInternals/Versions.cpp @@ -40,7 +40,7 @@ const char *swt_getTestingLibraryVersion(void) { return version.data(); #else -#warning SWT_TESTING_LIBRARY_VERSION not defined and version.txt not found: testing library version is unavailable +#warning SWT_TESTING_LIBRARY_VERSION not defined and VERSION.txt not found: testing library version is unavailable return nullptr; #endif } From da803bf63573c569e4eaaabbd8ecb309ca583e32 Mon Sep 17 00:00:00 2001 From: Jonathan Grynspan Date: Sun, 14 Sep 2025 09:06:41 -0400 Subject: [PATCH 12/12] Capitalize filename --- cmake/modules/LibraryVersion.cmake | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/cmake/modules/LibraryVersion.cmake b/cmake/modules/LibraryVersion.cmake index d9a9441f8..175c554fb 100644 --- a/cmake/modules/LibraryVersion.cmake +++ b/cmake/modules/LibraryVersion.cmake @@ -6,5 +6,5 @@ # See http://swift.org/LICENSE.txt for license information # See http://swift.org/CONTRIBUTORS.txt for Swift project authors -# The library version is now tracked in version.txt at the root directory of the +# The library version is now tracked in VERSION.txt at the root directory of the # repository. This file will be removed in a future commit.