Skip to content
Merged
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
43 changes: 43 additions & 0 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
# This source file is part of the Swift open source project
#
# Copyright (c) 2025 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
# =============================================================================
# This file configure testing that each project configures and builds with
# ctest.
# It is not meant for configuring and building each project.

cmake_minimum_required(VERSION 3.22)
project(SwiftCMakeExamples LANGUAGES NONE)

include(CTest)

function(add_cmake_test name source_dir)
cmake_parse_arguments(PARSE_ARGV 2 ARG "" "CMAKE_VERSION" "" )
if(NOT ARG_CMAKE_VERSION)
set(ARG_CMAKE_VERSION 3.22)
endif()

if(${CMAKE_VERSION} VERSION_LESS ${ARG_CMAKE_VERSION})
message(STATUS "Skipping ${name} -- CMake version too old: ${CMAKE_VERSION} < ${ARG_CMAKE_VERSION}")
return()
endif()

add_test(NAME "${name}-configure"
COMMAND ${CMAKE_COMMAND}
-G ${CMAKE_GENERATOR}
-B "${name}-build"
-S "${CMAKE_CURRENT_SOURCE_DIR}/${source_dir}"
-DCMAKE_BUILD_TYPE=${CMAKE_BUILD_TYPE})
add_test(NAME "${name}-build" COMMAND
${CMAKE_COMMAND} --build "${name}-build")
set_tests_properties("${name}-build" PROPERTIES DEPENDS "${name}-configure")
endfunction()

add_cmake_test(SingleExecutable 1_single_executable)
add_cmake_test(ExecutableLibrary 2_executable_library)
add_cmake_test(BidirectionalCxxInterop 3_bidirectional_cxx_interop
CMAKE_VERSION 3.26)
add_cmake_test(SwiftMacros 4_swift_macros)
19 changes: 19 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -57,3 +57,22 @@ using the Swift macro support introduced in Swift 5.9.
Requires:

- Swift 5.9 (macOS: Swift 5.9.0, Windows and Linux: Swift 5.9.1)

# Testing

Tests are run with `ctest`, configured with the CMakeLists at the top-level.

```sh
cmake -G Ninja -B build -S .
cd build
ctest -j --output-on-failure
```

When you add a test, add it to the top-level CMakeLists file and re-run CMake
from the test directory to ensure that the CTest files are updated
appropriately before trying to run the tests.

```sh
cmake .
ctest -j --output-on-failure
```