Skip to content

Commit d3e5fdb

Browse files
committed
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.
1 parent 82b63f6 commit d3e5fdb

File tree

8 files changed

+117
-48
lines changed

8 files changed

+117
-48
lines changed

Package.swift

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

471471
// Capture the testing library's version as a C++ string constant.
472472
if let git {
473-
let testingLibraryVersion = if let tag = git.currentTag {
474-
tag
475-
} else if git.hasUncommittedChanges {
476-
"\(git.currentCommit) (modified)"
477-
} else {
478-
git.currentCommit
473+
result.append(.define("SWT_TESTING_LIBRARY_COMMIT_HASH", to: #""\#(git.currentCommit)""#))
474+
if git.hasUncommittedChanges {
475+
result.append(.define("SWT_TESTING_LIBRARY_COMMIT_MODIFIED", to: "1"))
479476
}
480-
result.append(.define("SWT_TESTING_LIBRARY_VERSION", to: #""\#(testingLibraryVersion)""#))
481477
}
482478

483479
return result

Sources/Testing/Support/Versions.swift

Lines changed: 22 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -129,9 +129,28 @@ let simulatorVersion: String = {
129129
/// an event writer.
130130
///
131131
/// This value is not part of the public interface of the testing library.
132-
var testingLibraryVersion: String {
133-
swt_getTestingLibraryVersion().flatMap(String.init(validatingCString:)) ?? "unknown"
134-
}
132+
let testingLibraryVersion: String = {
133+
guard var result = swt_getTestingLibraryVersion().flatMap(String.init(validatingCString:)) else {
134+
return "unknown"
135+
}
136+
137+
// Get details of the git commit used when compiling the testing library.
138+
var commitHash: UnsafePointer<CChar>?
139+
var commitModified = CBool(false)
140+
swt_getTestingLibraryCommit(&commitHash, &commitModified)
141+
142+
if let commitHash = commitHash.flatMap(String.init(validatingCString:)) {
143+
// Truncate to 15 characters of the hash to match `swift --version`.
144+
let commitHash = commitHash.prefix(15)
145+
if commitModified {
146+
result = "\(result) (\(commitHash) - modified)"
147+
} else {
148+
result = "\(result) (\(commitHash))"
149+
}
150+
}
151+
152+
return result
153+
}()
135154

136155
/// Get the LLVM target triple used to build the testing library, if available.
137156
///

Sources/_TestingInternals/CMakeLists.txt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88

99
set(CMAKE_CXX_SCAN_FOR_MODULES 0)
1010

11-
include(LibraryVersion)
11+
include(GitCommit)
1212
include(TargetTriple)
1313
add_library(_TestingInternals STATIC
1414
Discovery.cpp

Sources/_TestingInternals/Versions.cpp

Lines changed: 36 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,15 +10,50 @@
1010

1111
#include "Versions.h"
1212

13+
#include <mutex>
14+
1315
const char *swt_getTestingLibraryVersion(void) {
1416
#if defined(SWT_TESTING_LIBRARY_VERSION)
17+
// The current environment explicitly specifies a version string to return.
1518
return SWT_TESTING_LIBRARY_VERSION;
19+
#elif __has_embed("../../version.txt")
20+
// Read the version from version.txt at the root of the package's repository.
21+
static char version[] = {
22+
#pragma clang diagnostic push
23+
#pragma clang diagnostic ignored "-Wc23-extensions"
24+
#embed "../../version.txt" suffix(, '\0')
25+
#pragma clang diagnostic pop
26+
};
27+
28+
// Trim any trailing whitespace.
29+
static std::once_flag once;
30+
std::call_once(once, [] {
31+
auto i = std::find_if(std::begin(version), std::end(version), std::isspace);
32+
if (i != std::end(version)) {
33+
*i = '\0';
34+
}
35+
});
36+
37+
return version;
1638
#else
17-
#warning SWT_TESTING_LIBRARY_VERSION not defined: testing library version is unavailable
39+
#warning SWT_TESTING_LIBRARY_VERSION not defined and version.txt not found: testing library version is unavailable
1840
return nullptr;
1941
#endif
2042
}
2143

44+
void swt_getTestingLibraryCommit(const char *_Nullable *_Nonnull outHash, bool *outModified) {
45+
#if defined(SWT_TESTING_LIBRARY_COMMIT_HASH)
46+
*outHash = SWT_TESTING_LIBRARY_COMMIT_HASH;
47+
#else
48+
*outHash = nullptr;
49+
#endif
50+
#if defined(SWT_TESTING_LIBRARY_COMMIT_MODIFIED)
51+
*outModified = (SWT_TESTING_LIBRARY_COMMIT_MODIFIED != 0);
52+
#else
53+
*outModified = false;
54+
#endif
55+
}
56+
2257
const char *swt_getTargetTriple(void) {
2358
#if defined(SWT_TARGET_TRIPLE)
2459
return SWT_TARGET_TRIPLE;

Sources/_TestingInternals/include/Versions.h

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,15 @@ static inline uint64_t swt_getSwiftCompilerVersion(void) {
3838
/// other conditions. Do not attempt to parse it.
3939
SWT_EXTERN const char *_Nullable swt_getTestingLibraryVersion(void);
4040

41+
/// Get details of the source control (git) commit from which the testing
42+
/// library was built.
43+
///
44+
/// - Parameters:
45+
/// - outHash: On return, set to a pointer to a string containing the commit
46+
/// hash from which the testing library was built.
47+
/// - outModified: On return, whether or not there were uncommitted changes.
48+
SWT_EXTERN void swt_getTestingLibraryCommit(const char *_Nullable *_Nonnull outHash, bool *outModified);
49+
4150
/// Get the LLVM target triple used to build the testing library.
4251
///
4352
/// - Returns: A string containing the LLVM target triple used to build the

cmake/modules/GitCommit.cmake

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
# This source file is part of the Swift.org open source project
2+
#
3+
# Copyright (c) 2024–2025 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+
find_package(Git QUIET)
10+
if(Git_FOUND)
11+
# Get the commit hash corresponding to the current build.
12+
execute_process(
13+
COMMAND ${GIT_EXECUTABLE} rev-parse --verify HEAD
14+
WORKING_DIRECTORY ${PROJECT_SOURCE_DIR}
15+
OUTPUT_VARIABLE SWT_TESTING_LIBRARY_COMMIT_HASH
16+
OUTPUT_STRIP_TRAILING_WHITESPACE
17+
ERROR_QUIET)
18+
19+
# Check if there are local changes.
20+
execute_process(
21+
COMMAND ${GIT_EXECUTABLE} status -s
22+
WORKING_DIRECTORY ${PROJECT_SOURCE_DIR}
23+
OUTPUT_VARIABLE SWT_TESTING_LIBRARY_COMMIT_MODIFIED
24+
OUTPUT_STRIP_TRAILING_WHITESPACE)
25+
endif()
26+
27+
if(SWT_TESTING_LIBRARY_COMMIT_HASH)
28+
add_compile_definitions(
29+
"$<$<COMPILE_LANGUAGE:CXX>:SWT_TESTING_LIBRARY_COMMIT_HASH=\"${SWT_TESTING_LIBRARY_COMMIT_HASH}\">")
30+
if(SWT_TESTING_LIBRARY_COMMIT_MODIFIED)
31+
add_compile_definitions(
32+
"$<$<COMPILE_LANGUAGE:CXX>:SWT_TESTING_LIBRARY_COMMIT_MODIFIED=1>")
33+
endif()
34+
endif()

cmake/modules/LibraryVersion.cmake

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

9-
# The current version of the Swift Testing release. For release branches,
10-
# remember to remove -dev.
11-
set(SWT_TESTING_LIBRARY_VERSION "6.3-dev")
12-
13-
find_package(Git QUIET)
14-
if(Git_FOUND)
15-
# Get the commit hash corresponding to the current build. Limit length to 15
16-
# to match `swift --version` output format.
17-
execute_process(
18-
COMMAND ${GIT_EXECUTABLE} rev-parse --short=15 --verify HEAD
19-
WORKING_DIRECTORY ${PROJECT_SOURCE_DIR}
20-
OUTPUT_VARIABLE GIT_VERSION
21-
OUTPUT_STRIP_TRAILING_WHITESPACE
22-
ERROR_QUIET)
23-
24-
# Check if there are local changes.
25-
execute_process(
26-
COMMAND ${GIT_EXECUTABLE} status -s
27-
WORKING_DIRECTORY ${PROJECT_SOURCE_DIR}
28-
OUTPUT_VARIABLE GIT_STATUS
29-
OUTPUT_STRIP_TRAILING_WHITESPACE)
30-
if(GIT_STATUS)
31-
set(GIT_VERSION "${GIT_VERSION} - modified")
32-
endif()
33-
endif()
34-
35-
# Combine the hard-coded Swift version with available Git information.
36-
if(GIT_VERSION)
37-
set(SWT_TESTING_LIBRARY_VERSION "${SWT_TESTING_LIBRARY_VERSION} (${GIT_VERSION})")
38-
endif()
39-
40-
# All done!
41-
message(STATUS "Swift Testing version: ${SWT_TESTING_LIBRARY_VERSION}")
42-
add_compile_definitions(
43-
"$<$<COMPILE_LANGUAGE:CXX>:SWT_TESTING_LIBRARY_VERSION=\"${SWT_TESTING_LIBRARY_VERSION}\">")
9+
# The library version is now tracked in version.txt at the root directory of the
10+
# repository. This file will be removed in a future commit.

version.txt

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
6.3-dev
2+
3+
# This source file is part of the Swift.org open source project
4+
#
5+
# Copyright (c) 2025 Apple Inc. and the Swift project authors
6+
# Licensed under Apache License v2.0 with Runtime Library Exception
7+
#
8+
# See http://swift.org/LICENSE.txt for license information
9+
# See http://swift.org/CONTRIBUTORS.txt for Swift project authors

0 commit comments

Comments
 (0)