Skip to content

Commit 39b854d

Browse files
committed
Factor out the commit hash from the library version.
This PR captures the current commit hash in a different variable from the library version. There is no user-facing impact.
1 parent 82b63f6 commit 39b854d

File tree

6 files changed

+49
-39
lines changed

6 files changed

+49
-39
lines changed

Package.swift

Lines changed: 4 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -468,16 +468,12 @@ extension Array where Element == PackageDescription.CXXSetting {
468468
.define("SWT_NO_LIBDISPATCH", .whenEmbedded()),
469469
]
470470

471-
// Capture the testing library's version as a C++ string constant.
471+
// Capture the testing library's commit info as C++ constants.
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 & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88

99
set(CMAKE_CXX_SCAN_FOR_MODULES 0)
1010

11+
include(GitCommit)
1112
include(LibraryVersion)
1213
include(TargetTriple)
1314
add_library(_TestingInternals STATIC

Sources/_TestingInternals/Versions.cpp

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,19 @@ const char *swt_getTestingLibraryVersion(void) {
1919
#endif
2020
}
2121

22+
void swt_getTestingLibraryCommit(const char *_Nullable *_Nonnull outHash, bool *outModified) {
23+
#if defined(SWT_TESTING_LIBRARY_COMMIT_HASH)
24+
*outHash = SWT_TESTING_LIBRARY_COMMIT_HASH;
25+
#else
26+
*outHash = nullptr;
27+
#endif
28+
#if defined(SWT_TESTING_LIBRARY_COMMIT_MODIFIED)
29+
*outModified = (SWT_TESTING_LIBRARY_COMMIT_MODIFIED != 0);
30+
#else
31+
*outModified = false;
32+
#endif
33+
}
34+
2235
const char *swt_getTargetTriple(void) {
2336
#if defined(SWT_TARGET_TRIPLE)
2437
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/LibraryVersion.cmake

Lines changed: 0 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -10,34 +10,6 @@
1010
# remember to remove -dev.
1111
set(SWT_TESTING_LIBRARY_VERSION "6.3-dev")
1212

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!
4113
message(STATUS "Swift Testing version: ${SWT_TESTING_LIBRARY_VERSION}")
4214
add_compile_definitions(
4315
"$<$<COMPILE_LANGUAGE:CXX>:SWT_TESTING_LIBRARY_VERSION=\"${SWT_TESTING_LIBRARY_VERSION}\">")

0 commit comments

Comments
 (0)