Skip to content
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
64 changes: 64 additions & 0 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
##===----------------------------------------------------------------------===##
##
## This source file is part of the Swift Service Context open source project
##
## Copyright (c) 2026 Apple Inc. and the Swift Service Context project authors
## Licensed under Apache License v2.0
##
## See LICENSE.txt for license information
## See CONTRIBUTORS.txt for the list of Swift Service Context project authors
##
## SPDX-License-Identifier: Apache-2.0
##
##===----------------------------------------------------------------------===##

cmake_minimum_required(VERSION 3.29)
project(SwiftServiceContext LANGUAGES C Swift)

# Match the DLL runtime used by the Windows Swift SDK, including Debug builds.
if(WIN32 AND NOT DEFINED CMAKE_MSVC_RUNTIME_LIBRARY)
set(CMAKE_MSVC_RUNTIME_LIBRARY MultiThreadedDLL)
endif()

if(CMAKE_Swift_COMPILER_VERSION VERSION_LESS 6.2)
message(FATAL_ERROR "Swift 6.2 or newer is required")
endif()
if(WIN32 AND BUILD_SHARED_LIBS AND CMAKE_C_COMPILER_FRONTEND_VARIANT STREQUAL "MSVC")
message(FATAL_ERROR
"Swift shared-library builds require Clang's GNU-style driver on Windows. Configure with -DCMAKE_C_COMPILER=clang.")
endif()
set(CMAKE_Swift_LANGUAGE_VERSION 6)
include(GNUInstallDirs)
include(CMakePackageConfigHelpers)
include(cmake/SwiftLibrary.cmake)

add_library(ServiceContextModule
Sources/ServiceContextModule/ServiceContext.swift
Sources/ServiceContextModule/ServiceContextKey.swift
)
add_library(SwiftServiceContext::ServiceContextModule ALIAS ServiceContextModule)
_swift_service_context_library(ServiceContextModule)

# This compatibility module has no runtime symbols, so Windows cannot create
# a DLL import library for it. Preserve the module in a static archive instead.
set(compatibility_library_type)
if(WIN32)
set(compatibility_library_type STATIC)
endif()
add_library(InstrumentationBaggage ${compatibility_library_type}
Sources/InstrumentationBaggage/CompatibilityShims.swift
)
add_library(SwiftServiceContext::InstrumentationBaggage ALIAS InstrumentationBaggage)
_swift_service_context_library(InstrumentationBaggage)
target_link_libraries(InstrumentationBaggage PUBLIC ServiceContextModule)

set(package_directory "${CMAKE_INSTALL_LIBDIR}/cmake/SwiftServiceContext")
configure_package_config_file(cmake/SwiftServiceContextConfig.cmake.in
"${CMAKE_CURRENT_BINARY_DIR}/SwiftServiceContextConfig.cmake"
INSTALL_DESTINATION "${package_directory}")
install(FILES "${CMAKE_CURRENT_BINARY_DIR}/SwiftServiceContextConfig.cmake"
DESTINATION "${package_directory}")
install(EXPORT SwiftServiceContextTargets NAMESPACE SwiftServiceContext::
DESTINATION "${package_directory}")
export(EXPORT SwiftServiceContextTargets NAMESPACE SwiftServiceContext::
FILE "${CMAKE_CURRENT_BINARY_DIR}/SwiftServiceContextTargets.cmake")
4 changes: 4 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -75,3 +75,7 @@ targets: [
// ...
]
```

## Building with CMake

See [the CMake build instructions](cmake/README.md) for library builds and consumer validation.
30 changes: 30 additions & 0 deletions Tests/CMake/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
##===----------------------------------------------------------------------===##
##
## This source file is part of the Swift Service Context open source project
##
## Copyright (c) 2026 Apple Inc. and the Swift Service Context project authors
## Licensed under Apache License v2.0
##
## See LICENSE.txt for license information
## See CONTRIBUTORS.txt for the list of Swift Service Context project authors
##
## SPDX-License-Identifier: Apache-2.0
##
##===----------------------------------------------------------------------===##

cmake_minimum_required(VERSION 3.29)
project(CMakeConsumer LANGUAGES C Swift)

# Match the DLL runtime used by the Windows Swift SDK, including Debug builds.
if(WIN32 AND NOT DEFINED CMAKE_MSVC_RUNTIME_LIBRARY)
set(CMAKE_MSVC_RUNTIME_LIBRARY MultiThreadedDLL)
endif()
find_package(SwiftServiceContext CONFIG REQUIRED)
add_executable(CMakeConsumer main.swift)
target_link_libraries(CMakeConsumer PRIVATE SwiftServiceContext::ServiceContextModule SwiftServiceContext::InstrumentationBaggage)
enable_testing()
add_test(NAME CMakeConsumer COMMAND CMakeConsumer)
if(WIN32)
set_tests_properties(CMakeConsumer PROPERTIES ENVIRONMENT_MODIFICATION
"PATH=path_list_prepend:$<TARGET_FILE_DIR:SwiftServiceContext::InstrumentationBaggage>;PATH=path_list_prepend:$<TARGET_FILE_DIR:SwiftServiceContext::ServiceContextModule>")
endif()
23 changes: 23 additions & 0 deletions Tests/CMake/main.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
//===----------------------------------------------------------------------===//
//
// This source file is part of the Swift Service Context open source project
//
// Copyright (c) 2026 Apple Inc. and the Swift Service Context project authors
// Licensed under Apache License v2.0
//
// See LICENSE.txt for license information
// See CONTRIBUTORS.txt for the list of Swift Service Context project authors
//
// SPDX-License-Identifier: Apache-2.0
//
//===----------------------------------------------------------------------===//

import InstrumentationBaggage
import ServiceContextModule

enum MessageKey: ServiceContextKey {
typealias Value = String
}
var context = ServiceContext.topLevel
context[MessageKey.self] = "CMake"
precondition(context[MessageKey.self] == "CMake")
37 changes: 37 additions & 0 deletions cmake/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
# Building with CMake

CMake 3.29 or newer, Ninja, and Swift 6.2 or newer are required. Ninja Multi-Config
requires CMake 4.0 or newer. SwiftPM remains the test runner for the Swift unit tests.

On Windows, run CMake from a Visual Studio developer command prompt so the MSVC
linker and Windows SDK are available. Add `-DCMAKE_C_COMPILER=clang` to each configure
command to use the Clang driver supplied with Swift. Shared builds require this
GNU-style driver because CMake's MSVC C driver initializes linker flags that Swift
cannot consume directly. Use the same build configuration for libraries and their
consumer. The default Windows runtime is `MultiThreadedDLL` to match Swift's SDK;
set `-DCMAKE_MSVC_RUNTIME_LIBRARY=MultiThreadedDLL` in independent consumers too.

Configure, build, and install the libraries:

```sh
cmake -S . -B build -G Ninja -DCMAKE_INSTALL_PREFIX=/path/to/install
cmake --build build
cmake --install build
```

Pass `-DBUILD_SHARED_LIBS=ON` to build shared libraries. On Windows, the
`InstrumentationBaggage` compatibility module remains static because its
re-exports and type aliases have no runtime symbols for a DLL import library.
An enclosing CMake project can also use `add_subdirectory` and link the namespaced targets. The build directory
and install prefix both provide a `SwiftServiceContext` package configuration.

The independent consumer verifies that public modules can be imported and linked:

```sh
cmake -S Tests/CMake -B consumer -G Ninja -DCMAKE_PREFIX_PATH=/path/to/install
cmake --build consumer
ctest --test-dir consumer --output-on-failure
```

Test relocated installations by moving the installation prefix before configuring the consumer.
For Ninja Multi-Config, pass `--config Debug` to build/install and `-C Debug` to CTest.
45 changes: 45 additions & 0 deletions cmake/SwiftLibrary.cmake
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
##===----------------------------------------------------------------------===##
##
## This source file is part of the Swift Service Context open source project
##
## Copyright (c) 2026 Apple Inc. and the Swift Service Context project authors
## Licensed under Apache License v2.0
##
## See LICENSE.txt for license information
## See CONTRIBUTORS.txt for the list of Swift Service Context project authors
##
## SPDX-License-Identifier: Apache-2.0
##
##===----------------------------------------------------------------------===##

function(_swift_service_context_library target)
set(module_directory "${CMAKE_CURRENT_BINARY_DIR}/swift")
if(CMAKE_CONFIGURATION_TYPES)
foreach(configuration IN LISTS CMAKE_CONFIGURATION_TYPES)
file(MAKE_DIRECTORY "${module_directory}/${configuration}")
endforeach()
set(module_directory "${module_directory}/$<CONFIG>")
if(CMAKE_VERSION VERSION_LESS 4.0)
message(FATAL_ERROR "Swift multi-configuration builds require CMake 4.0 or newer")
endif()
endif()
set_target_properties(${target} PROPERTIES
Swift_MODULE_DIRECTORY "${module_directory}"
POSITION_INDEPENDENT_CODE YES)
get_target_property(library_type ${target} TYPE)
if(WIN32 AND library_type STREQUAL "STATIC_LIBRARY")
# Swift autolinks lib-prefixed static archives, but unprefixed DLL import libraries.
set_target_properties(${target} PROPERTIES PREFIX "lib")
endif()
target_compile_options(${target} PRIVATE
"$<$<COMPILE_LANGUAGE:Swift>:SHELL:-package-name swift_service_context>"
"$<$<COMPILE_LANGUAGE:Swift>:SHELL:-enable-experimental-feature StrictConcurrency=complete>")
target_include_directories(${target} PUBLIC
"$<BUILD_INTERFACE:${module_directory}>"
"$<INSTALL_INTERFACE:${CMAKE_INSTALL_LIBDIR}/swift>")
install(TARGETS ${target} EXPORT SwiftServiceContextTargets)
install(FILES
"${module_directory}/${target}.swiftmodule"
"${module_directory}/${target}.swiftdoc"
DESTINATION "${CMAKE_INSTALL_LIBDIR}/swift")
endfunction()
18 changes: 18 additions & 0 deletions cmake/SwiftServiceContextConfig.cmake.in
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
##===----------------------------------------------------------------------===##
##
## This source file is part of the Swift Service Context open source project
##
## Copyright (c) 2026 Apple Inc. and the Swift Service Context project authors
## Licensed under Apache License v2.0
##
## See LICENSE.txt for license information
## See CONTRIBUTORS.txt for the list of Swift Service Context project authors
##
## SPDX-License-Identifier: Apache-2.0
##
##===----------------------------------------------------------------------===##

@PACKAGE_INIT@

include("${CMAKE_CURRENT_LIST_DIR}/SwiftServiceContextTargets.cmake")
check_required_components(SwiftServiceContext)