Skip to content

Commit 1be6b35

Browse files
authored
Move the package version to VERSION.txt. (#1304)
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. Currently using C23's `#embed` which is supported in clang as a non-standard extension. C++26 will add support for `#embed`. ### 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 77f84d3 commit 1be6b35

File tree

5 files changed

+31
-17
lines changed

5 files changed

+31
-17
lines changed

Package.swift

Lines changed: 0 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -470,20 +470,13 @@ extension Array where Element == PackageDescription.CXXSetting {
470470

471471
// Capture the testing library's commit info as C++ constants.
472472
if let git {
473-
if let tag = git.currentTag {
474-
result.append(.define("SWT_TESTING_LIBRARY_VERSION", to: #""\#(tag)""#))
475-
} else {
476-
result.append(.define("SWT_TESTING_LIBRARY_VERSION", to: "0"))
477-
}
478-
479473
result.append(.define("SWT_TESTING_LIBRARY_COMMIT_HASH", to: #""\#(git.currentCommit)""#))
480474
if git.hasUncommittedChanges {
481475
result.append(.define("SWT_TESTING_LIBRARY_COMMIT_MODIFIED", to: "1"))
482476
}
483477
} else if let gitHubSHA = Context.environment["GITHUB_SHA"] {
484478
// When building in GitHub Actions, the git command may fail to get us the
485479
// commit hash, so check if GitHub shared it with us instead.
486-
result.append(.define("SWT_TESTING_LIBRARY_VERSION", to: "0"))
487480
result.append(.define("SWT_TESTING_LIBRARY_COMMIT_HASH", to: #""\#(gitHubSHA)""#))
488481
}
489482

Sources/_TestingInternals/CMakeLists.txt

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

1111
include(GitCommit)
12-
include(LibraryVersion)
1312
include(TargetTriple)
1413
add_library(_TestingInternals STATIC
1514
Discovery.cpp

Sources/_TestingInternals/Versions.cpp

Lines changed: 27 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,11 +10,37 @@
1010

1111
#include "Versions.h"
1212

13+
#include <array>
14+
#include <algorithm>
15+
#include <iterator>
16+
1317
const char *swt_getTestingLibraryVersion(void) {
1418
#if defined(SWT_TESTING_LIBRARY_VERSION)
19+
// The current environment explicitly specifies a version string to return.
1520
return SWT_TESTING_LIBRARY_VERSION;
21+
#elif __has_embed("../../VERSION.txt")
22+
static constinit auto version = [] () constexpr {
23+
// Read the version from version.txt at the root of the package's repo.
24+
char version[] = {
25+
#pragma clang diagnostic push
26+
#pragma clang diagnostic ignored "-Wc23-extensions"
27+
#embed "../../VERSION.txt"
28+
#pragma clang diagnostic pop
29+
};
30+
31+
// Copy the first line from the C string into a C array so that we can
32+
// return it from this closure.
33+
std::array<char, std::size(version) + 1> result {};
34+
auto i = std::find_if(std::begin(version), std::end(version), [] (char c) {
35+
return c == '\r' || c == '\n';
36+
});
37+
std::copy(std::begin(version), i, result.begin());
38+
return result;
39+
}();
40+
41+
return version.data();
1642
#else
17-
#warning SWT_TESTING_LIBRARY_VERSION not defined: testing library version is unavailable
43+
#warning SWT_TESTING_LIBRARY_VERSION not defined and VERSION.txt not found: testing library version is unavailable
1844
return nullptr;
1945
#endif
2046
}

VERSION.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
6.3-dev

cmake/modules/LibraryVersion.cmake

Lines changed: 3 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,12 @@
11
##
22
## This source file is part of the Swift.org open source project
33
##
4-
## Copyright (c) 2024 Apple Inc. and the Swift project authors
4+
## Copyright (c) 2024–2025 Apple Inc. and the Swift project authors
55
## Licensed under Apache License v2.0 with Runtime Library Exception
66
##
77
## See https://swift.org/LICENSE.txt for license information
88
## See https://swift.org/CONTRIBUTORS.txt for Swift project authors
99
##
1010

11-
# The current version of the Swift Testing release. For release branches,
12-
# remember to remove -dev.
13-
set(SWT_TESTING_LIBRARY_VERSION "6.3-dev")
14-
15-
message(STATUS "Swift Testing version: ${SWT_TESTING_LIBRARY_VERSION}")
16-
add_compile_definitions(
17-
"$<$<COMPILE_LANGUAGE:CXX>:SWT_TESTING_LIBRARY_VERSION=\"${SWT_TESTING_LIBRARY_VERSION}\">")
11+
# The library version is now tracked in VERSION.txt at the root directory of the
12+
# repository. This file will be removed in a future commit.

0 commit comments

Comments
 (0)