Skip to content

[6.0.0][CMake] Explicitly link Testing to Foundation #715

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 4 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 7 additions & 0 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,13 @@ endif()
project(SwiftTesting
LANGUAGES CXX Swift)

if(NOT APPLE)
if(NOT CMAKE_SYSTEM_NAME STREQUAL WASI)
find_package(dispatch CONFIG)
endif()
find_package(Foundation CONFIG)
endif()

include(GNUInstallDirs)

list(APPEND CMAKE_MODULE_PATH
Expand Down
8 changes: 8 additions & 0 deletions Sources/Testing/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -94,6 +94,14 @@ add_library(Testing
Traits/Trait.swift)
target_link_libraries(Testing PRIVATE
_TestingInternals)
if(NOT APPLE)
if(NOT CMAKE_SYSTEM_NAME STREQUAL WASI)
target_link_libraries(Testing PUBLIC
dispatch)
endif()
target_link_libraries(Testing PUBLIC
Foundation)
endif()
add_dependencies(Testing
TestingMacros)
target_compile_options(Testing PRIVATE
Expand Down
2 changes: 1 addition & 1 deletion Sources/Testing/Support/Versions.swift
Original file line number Diff line number Diff line change
Expand Up @@ -125,7 +125,7 @@ let simulatorVersion: String = {
///
/// This value is not part of the public interface of the testing library.
var testingLibraryVersion: String {
SWT_TESTING_LIBRARY_VERSION
swt_getTestingLibraryVersion().flatMap(String.init(validatingCString:)) ?? "unknown"
}

/// A human-readable string describing the Swift Standard Library's version.
Expand Down
1 change: 1 addition & 0 deletions Sources/_TestingInternals/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ set(CMAKE_CXX_SCAN_FOR_MODULES 0)
include(LibraryVersion)
add_library(_TestingInternals STATIC
Discovery.cpp
Versions.cpp
WillThrow.cpp)
target_include_directories(_TestingInternals PUBLIC
${CMAKE_CURRENT_SOURCE_DIR}/include)
Expand Down
20 changes: 20 additions & 0 deletions Sources/_TestingInternals/Versions.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
//
// 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 https://swift.org/LICENSE.txt for license information
// See https://swift.org/CONTRIBUTORS.txt for Swift project authors
//

#include "Versions.h"

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
return nullptr;
#endif
}
11 changes: 0 additions & 11 deletions Sources/_TestingInternals/include/Defines.h
Original file line number Diff line number Diff line change
Expand Up @@ -32,15 +32,4 @@
/// An attribute that renames a C symbol in Swift.
#define SWT_SWIFT_NAME(name) __attribute__((swift_name(#name)))

/// The testing library version from the package manifest.
///
/// - Bug: The value provided to the compiler (`_SWT_TESTING_LIBRARY_VERSION`)
/// is not visible in Swift, so this second macro is needed.
/// ((#43521)[https://github.com/swiftlang/swift/issues/43521])
#if defined(_SWT_TESTING_LIBRARY_VERSION)
#define SWT_TESTING_LIBRARY_VERSION _SWT_TESTING_LIBRARY_VERSION
#else
#define SWT_TESTING_LIBRARY_VERSION "unknown"
#endif

#endif // SWT_DEFINES_H
28 changes: 28 additions & 0 deletions Sources/_TestingInternals/include/Versions.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
//
// 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 https://swift.org/LICENSE.txt for license information
// See https://swift.org/CONTRIBUTORS.txt for Swift project authors
//

#if !defined(SWT_VERSIONS_H)
#define SWT_VERSIONS_H

#include "Defines.h"

SWT_ASSUME_NONNULL_BEGIN

/// Get the human-readable version of the testing library.
///
/// - Returns: A human-readable string describing the version of the testing
/// library, or `nullptr` if no version information is available. This
/// string's value and format may vary between platforms, releases, or any
/// other conditions. Do not attempt to parse it.
SWT_EXTERN const char *_Nullable swt_getTestingLibraryVersion(void);

SWT_ASSUME_NONNULL_END

#endif
50 changes: 27 additions & 23 deletions cmake/modules/LibraryVersion.cmake
Original file line number Diff line number Diff line change
Expand Up @@ -6,34 +6,38 @@
# 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.0")

find_package(Git QUIET)
if(Git_FOUND)
# Get the commit hash corresponding to the current build. Limit length to 15
# to match `swift --version` output format.
execute_process(
COMMAND ${GIT_EXECUTABLE} describe --tags --exact-match
COMMAND ${GIT_EXECUTABLE} rev-parse --short=15 --verify HEAD
WORKING_DIRECTORY ${PROJECT_SOURCE_DIR}
OUTPUT_VARIABLE GIT_TAG
OUTPUT_VARIABLE GIT_VERSION
OUTPUT_STRIP_TRAILING_WHITESPACE
ERROR_QUIET)
if(GIT_TAG)
add_compile_definitions(
"$<$<COMPILE_LANGUAGE:CXX>:_SWT_TESTING_LIBRARY_VERSION=${GIT_TAG}>")
else()
execute_process(
COMMAND ${GIT_EXECUTABLE} rev-parse --verify HEAD
WORKING_DIRECTORY ${PROJECT_SOURCE_DIR}
OUTPUT_VARIABLE GIT_REVISION
OUTPUT_STRIP_TRAILING_WHITESPACE)
execute_process(
COMMAND ${GIT_EXECUTABLE} status -s
WORKING_DIRECTORY ${PROJECT_SOURCE_DIR}
OUTPUT_VARIABLE GIT_STATUS
OUTPUT_STRIP_TRAILING_WHITESPACE)
if(GIT_STATUS)
add_compile_definitions(
"$<$<COMPILE_LANGUAGE:CXX>:_SWT_TESTING_LIBRARY_VERSION=${GIT_REVISION} (modified)>")
else()
add_compile_definitions(
"$<$<COMPILE_LANGUAGE:CXX>:_SWT_TESTING_LIBRARY_VERSION=${GIT_REVISION}>")
endif()

# Check if there are local changes.
execute_process(
COMMAND ${GIT_EXECUTABLE} status -s
WORKING_DIRECTORY ${PROJECT_SOURCE_DIR}
OUTPUT_VARIABLE GIT_STATUS
OUTPUT_STRIP_TRAILING_WHITESPACE)
if(GIT_STATUS)
set(GIT_VERSION "${GIT_VERSION} - modified")
endif()
endif()

# Combine the hard-coded Swift version with available Git information.
if(GIT_VERSION)
set(SWT_TESTING_LIBRARY_VERSION "${SWT_TESTING_LIBRARY_VERSION} (${GIT_VERSION})")
endif()

# All done!
message(STATUS "Swift Testing version: ${SWT_TESTING_LIBRARY_VERSION}")
add_compile_definitions(
"$<$<COMPILE_LANGUAGE:CXX>:_SWT_TESTING_LIBRARY_VERSION=\"${SWT_TESTING_LIBRARY_VERSION}\">")