-
Notifications
You must be signed in to change notification settings - Fork 112
[cmake] Build swift-testing with CMake. #387
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
Merged
Merged
Changes from all commits
Commits
Show all changes
14 commits
Select commit
Hold shift + click to select a range
a48014f
[cmake] Build swift-testing with CMake.
jeffdav 581d281
Use CMake define to toggle TestingInternals import mechanism.
jeffdav ee183a9
Pin swift-syntax hash.
jeffdav a69f0eb
Add CMake.md documentation.
jeffdav 8eb95b6
Address PR feedback on documentation; add a bit about -enable-testing.
jeffdav 6a39d96
Hit save on the bit about -enable-testing.
jeffdav 9ff2f9a
Update CMakeLists to referemce _TestingInternals; enable library evol…
jeffdav 4bee9ee
Make min CMake versions match.
jeffdav 9a62a24
Provide range arg to cmake_minimum_required().
jeffdav 88a51a4
PR feedback on cmake include locations.
jeffdav 506390f
Remove duplicate settings.
jeffdav 200b967
Add -fno-exceptions for _TestingInternals.
jeffdav 07d0dcb
Fixes for latest changes from main.
jeffdav 31fafd5
Add flags for InferSendableFromCaptures upcoming feature.
jeffdav File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,5 +1,6 @@ | ||
.DS_Store | ||
/.build | ||
/build | ||
/Packages | ||
/*.xcodeproj | ||
xcuserdata/ | ||
|
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
# 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 http://swift.org/LICENSE.txt for license information | ||
# See http://swift.org/CONTRIBUTORS.txt for Swift project authors | ||
|
||
cmake_minimum_required(VERSION 3.19.6...3.29) | ||
|
||
if(POLICY CMP0157) | ||
cmake_policy(SET CMP0157 NEW) | ||
endif() | ||
|
||
project(SwiftTesting | ||
LANGUAGES CXX Swift) | ||
|
||
list(APPEND CMAKE_MODULE_PATH | ||
${PROJECT_SOURCE_DIR}/cmake/modules | ||
${PROJECT_SOURCE_DIR}/cmake/modules/shared) | ||
|
||
set(CMAKE_ARCHIVE_OUTPUT_DIRECTORY ${CMAKE_BINARY_DIR}/lib) | ||
set(CMAKE_LIBRARY_OUTPUT_DIRECTORY ${CMAKE_BINARY_DIR}/lib) | ||
set(CMAKE_RUNTIME_OUTPUT_DIRECTORY ${CMAKE_BINARY_DIR}/bin) | ||
|
||
set(CMAKE_MSVC_RUNTIME_LIBRARY MultiThreadedDLL) | ||
set(CMAKE_CXX_STANDARD 20) | ||
set(CMAKE_Swift_MODULE_DIRECTORY ${CMAKE_BINARY_DIR}/swift) | ||
|
||
add_subdirectory(Sources) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,77 @@ | ||
# Building with CMake | ||
|
||
## Add `swift-testing` to Your Project | ||
|
||
Add `swift-testing` with to your project using the standard `FetchContent` or `find_package` mechanism, as appropriate for your project. For example: | ||
|
||
```cmake | ||
include(FetchContent) | ||
FetchContent_Declare(SwiftTesting | ||
GIT_REPOSITORY https://github.com/apple/swift-testing.git | ||
GIT_TAG main) | ||
FetchContent_MakeAvailable(SwiftTesting) | ||
``` | ||
|
||
## Define Your Test Executable | ||
|
||
To build a test executable using `swift-testing`, define an executable target | ||
of the form `[YOURPROJECT]PackageTests`, set the executable suffix to be | ||
`.swift-testing`, and link to your project targets with `Testing`. | ||
|
||
The following | ||
example shows what this might look like for a hypothetical project called | ||
`Example`: | ||
|
||
```cmake | ||
add_executable(ExamplePackageTests | ||
ExampleTests.swift | ||
...) | ||
set_target_properties(ExamplePackageTests PROPERTIES | ||
SUFFIX .swift-testing) | ||
target_link_libraries(ExamplePackageTests PRIVATE | ||
Example | ||
Testing | ||
...) | ||
``` | ||
|
||
When building the test executable, the code you're testing will need to be built | ||
with `-enable-testing`. This should only be enabled for testing, for example: | ||
|
||
```cmake | ||
include(CTest) | ||
if(BUILD_TESTING) | ||
add_compile_options($<$<COMPILE_LANGUAGE:Swift>:-enable-testing>) | ||
endif() | ||
``` | ||
|
||
## Add an Entry Point | ||
|
||
You must define a custom source file with a `@main` entry point. This should be | ||
a separate source file that is included in your test executable's `SOURCES` | ||
list. | ||
|
||
The following example uses the SwiftPM entry point: | ||
|
||
```swift | ||
import Testing | ||
|
||
@main struct Runner { | ||
static func main() async { | ||
await Testing.__swiftPMEntryPoint() as Never | ||
} | ||
} | ||
``` | ||
> [!WARNING] | ||
> The entry point is expected to change to an entry point designed for other | ||
> build systems prior to `swift-testing` v1. | ||
|
||
|
||
## Integrate with CTest | ||
|
||
To run your test using CTest, add the test using the appropriate command line. | ||
|
||
```cmake | ||
include(CTest) | ||
add_test(NAME ExamplePackageTests | ||
COMMAND ExamplePackageTests) | ||
``` |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
# 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 http://swift.org/LICENSE.txt for license information | ||
# See http://swift.org/CONTRIBUTORS.txt for Swift project authors | ||
|
||
# Macros must be built for the build machine, not the host. | ||
include(ExternalProject) | ||
ExternalProject_Add(TestingMacros | ||
PREFIX "tm" | ||
SOURCE_DIR "${CMAKE_CURRENT_SOURCE_DIR}/TestingMacros" | ||
INSTALL_COMMAND "") | ||
ExternalProject_Get_Property(TestingMacros BINARY_DIR) | ||
if(CMAKE_HOST_WIN32) | ||
set(TestingMacrosPath "${BINARY_DIR}/TestingMacros.exe#TestingMacros") | ||
else() | ||
set(TestingMacrosPath "${BINARY_DIR}/TestingMacros#TestingMacros") | ||
endif() | ||
|
||
include(AvailabilityDefinitions) | ||
include(CompilerSettings) | ||
add_subdirectory(_TestingInternals) | ||
add_subdirectory(Testing) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,105 @@ | ||
# 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 http://swift.org/LICENSE.txt for license information | ||
# See http://swift.org/CONTRIBUTORS.txt for Swift project authors | ||
|
||
add_library(Testing | ||
EntryPoints/ABIEntryPoint.swift | ||
EntryPoints/ABIv0/ABIv0.Record.swift | ||
EntryPoints/ABIv0/ABIv0.Record+Streaming.swift | ||
EntryPoints/ABIv0/ABIv0.swift | ||
EntryPoints/ABIv0/Encoded/ABIv0.EncodedEvent.swift | ||
EntryPoints/ABIv0/Encoded/ABIv0.EncodedInstant.swift | ||
EntryPoints/ABIv0/Encoded/ABIv0.EncodedIssue.swift | ||
EntryPoints/ABIv0/Encoded/ABIv0.EncodedMessage.swift | ||
EntryPoints/ABIv0/Encoded/ABIv0.EncodedTest.swift | ||
EntryPoints/EntryPoint.swift | ||
EntryPoints/SwiftPMEntryPoint.swift | ||
EntryPoints/XCTestScaffold.swift | ||
Events/Clock.swift | ||
Events/Event.swift | ||
Events/Recorder/Event.ConsoleOutputRecorder.swift | ||
Events/Recorder/Event.HumanReadableOutputRecorder.swift | ||
Events/Recorder/Event.JUnitXMLRecorder.swift | ||
Events/Recorder/Event.Symbol.swift | ||
Events/TimeValue.swift | ||
ExitTests/ExitCondition.swift | ||
ExitTests/ExitTest.swift | ||
ExitTests/WaitFor.swift | ||
Expectations/Expectation.swift | ||
Expectations/Expectation+Macro.swift | ||
Expectations/ExpectationChecking+Macro.swift | ||
Issues/Confirmation.swift | ||
Issues/ErrorSnapshot.swift | ||
Issues/Issue.swift | ||
Issues/Issue+Recording.swift | ||
Issues/KnownIssue.swift | ||
Parameterization/CustomTestArgumentEncodable.swift | ||
Parameterization/Test.Case.Generator.swift | ||
Parameterization/Test.Case.ID.swift | ||
Parameterization/Test.Case.swift | ||
Parameterization/TypeInfo.swift | ||
Running/Configuration.swift | ||
Running/Configuration.TestFilter.swift | ||
Running/Configuration+EventHandling.swift | ||
Running/Runner.Plan.swift | ||
Running/Runner.Plan+Dumping.swift | ||
Running/Runner.RuntimeState.swift | ||
Running/Runner.swift | ||
Running/SkipInfo.swift | ||
SourceAttribution/Backtrace.swift | ||
SourceAttribution/CustomTestStringConvertible.swift | ||
SourceAttribution/Expression.swift | ||
SourceAttribution/Expression+Macro.swift | ||
SourceAttribution/SourceContext.swift | ||
SourceAttribution/SourceLocation.swift | ||
Support/Additions/ArrayAdditions.swift | ||
Support/Additions/CollectionDifferenceAdditions.swift | ||
Support/Additions/CommandLineAdditions.swift | ||
Support/Additions/NumericAdditions.swift | ||
Support/Additions/ResultAdditions.swift | ||
Support/Additions/StringAdditions.swift | ||
Support/CartesianProduct.swift | ||
Support/CError.swift | ||
Support/Environment.swift | ||
Support/FileHandle.swift | ||
Support/Graph.swift | ||
Support/JSON.swift | ||
Support/Locked.swift | ||
Support/SystemError.swift | ||
Support/UncheckedSendable.swift | ||
Support/Versions.swift | ||
Test.ID.Selection.swift | ||
Test.ID.swift | ||
Test.swift | ||
Test+Discovery.swift | ||
Test+Macro.swift | ||
Traits/Bug.swift | ||
Traits/Comment.swift | ||
Traits/Comment+Macro.swift | ||
Traits/ConditionTrait.swift | ||
Traits/ConditionTrait+Macro.swift | ||
Traits/HiddenTrait.swift | ||
Traits/ParallelizationTrait.swift | ||
Traits/SPIAwareTrait.swift | ||
Traits/Tags/Tag.Color.swift | ||
Traits/Tags/Tag.Color+Loading.swift | ||
Traits/Tags/Tag.List.swift | ||
Traits/Tags/Tag.swift | ||
Traits/Tags/Tag+Macro.swift | ||
Traits/Tags/Tag+Predefined.swift | ||
Traits/TimeLimitTrait.swift | ||
Traits/Trait.swift) | ||
target_link_libraries(Testing PRIVATE | ||
_TestingInternals) | ||
add_dependencies(Testing | ||
TestingMacros) | ||
target_compile_definitions(Testing PRIVATE | ||
SWT_BUILDING_WITH_CMAKE) | ||
target_compile_options(Testing PUBLIC | ||
-load-plugin-executable "${TestingMacrosPath}") | ||
target_compile_options(Testing PRIVATE | ||
jeffdav marked this conversation as resolved.
Show resolved
Hide resolved
|
||
-enable-library-evolution) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.