From 9fa62a61103c3d66e006bedb01b337634a1ae24f Mon Sep 17 00:00:00 2001 From: RKS Date: Tue, 6 Oct 2026 14:02:45 -0400 Subject: [PATCH 1/7] Add CMake library builds and package exports **Motivation:** Swift Service Context has no CMake build, preventing CMake-based consumers from building its libraries (#86). **Modifications:** Add explicit module targets, Swift package compilation settings, build-tree and relocatable install exports, and independent consumer tests with build documentation. Preserve SwiftPM and the runtime source. **Result:** CMake consumers can build, import, link, and run against static or shared libraries. Local macOS and Linux Swift 6.4 tests cover Ninja and Ninja Multi-Config and moved installation prefixes. --- CMakeLists.txt | 37 ++++++++++++++++++++++++ README.md | 4 +++ Tests/CMake/CMakeLists.txt | 13 +++++++++ Tests/CMake/main.swift | 23 +++++++++++++++ cmake/README.md | 27 +++++++++++++++++ cmake/SwiftLibrary.cmake | 28 ++++++++++++++++++ cmake/SwiftServiceContextConfig.cmake.in | 6 ++++ 7 files changed, 138 insertions(+) create mode 100644 CMakeLists.txt create mode 100644 Tests/CMake/CMakeLists.txt create mode 100644 Tests/CMake/main.swift create mode 100644 cmake/README.md create mode 100644 cmake/SwiftLibrary.cmake create mode 100644 cmake/SwiftServiceContextConfig.cmake.in diff --git a/CMakeLists.txt b/CMakeLists.txt new file mode 100644 index 0000000..45bf8e8 --- /dev/null +++ b/CMakeLists.txt @@ -0,0 +1,37 @@ +# SPDX-License-Identifier: Apache-2.0 + +cmake_minimum_required(VERSION 3.29) +project(SwiftServiceContext LANGUAGES C Swift) + +if(CMAKE_Swift_COMPILER_VERSION VERSION_LESS 6.2) + message(FATAL_ERROR "Swift 6.2 or newer is required") +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_library(ServiceContextModule) + +add_library(InstrumentationBaggage + Sources/InstrumentationBaggage/CompatibilityShims.swift +) +add_library(SwiftServiceContext::InstrumentationBaggage ALIAS InstrumentationBaggage) +swift_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") diff --git a/README.md b/README.md index 319d64b..6806dd4 100644 --- a/README.md +++ b/README.md @@ -75,3 +75,7 @@ targets: [ // ... ] ``` + +## Building with CMake + +See [the CMake build instructions](cmake/README.md) for library builds and consumer validation. diff --git a/Tests/CMake/CMakeLists.txt b/Tests/CMake/CMakeLists.txt new file mode 100644 index 0000000..cfd7f05 --- /dev/null +++ b/Tests/CMake/CMakeLists.txt @@ -0,0 +1,13 @@ +# SPDX-License-Identifier: Apache-2.0 + +cmake_minimum_required(VERSION 3.29) +project(CMakeConsumer LANGUAGES Swift) +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:$;PATH=path_list_prepend:$") +endif() diff --git a/Tests/CMake/main.swift b/Tests/CMake/main.swift new file mode 100644 index 0000000..1748800 --- /dev/null +++ b/Tests/CMake/main.swift @@ -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") diff --git a/cmake/README.md b/cmake/README.md new file mode 100644 index 0000000..5e91aec --- /dev/null +++ b/cmake/README.md @@ -0,0 +1,27 @@ +# 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. + +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. 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. diff --git a/cmake/SwiftLibrary.cmake b/cmake/SwiftLibrary.cmake new file mode 100644 index 0000000..5c00ce0 --- /dev/null +++ b/cmake/SwiftLibrary.cmake @@ -0,0 +1,28 @@ +# SPDX-License-Identifier: Apache-2.0 + +function(swift_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}/$") + 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) + target_compile_options(${target} PRIVATE + "$<$:SHELL:-package-name swift_service_context>" + "$<$:SHELL:-enable-experimental-feature StrictConcurrency=complete>") + target_include_directories(${target} PUBLIC + "$" + "$") + install(TARGETS ${target} EXPORT SwiftServiceContextTargets) + install(FILES + "${module_directory}/${target}.swiftmodule" + "${module_directory}/${target}.swiftdoc" + DESTINATION "${CMAKE_INSTALL_LIBDIR}/swift") +endfunction() diff --git a/cmake/SwiftServiceContextConfig.cmake.in b/cmake/SwiftServiceContextConfig.cmake.in new file mode 100644 index 0000000..832dc37 --- /dev/null +++ b/cmake/SwiftServiceContextConfig.cmake.in @@ -0,0 +1,6 @@ +# SPDX-License-Identifier: Apache-2.0 + +@PACKAGE_INIT@ + +include("${CMAKE_CURRENT_LIST_DIR}/SwiftServiceContextTargets.cmake") +check_required_components(SwiftServiceContext) From 23b283076bde1b4511d643695fb97701b23ede24 Mon Sep 17 00:00:00 2001 From: RKS Date: Tue, 6 Oct 2026 14:23:07 -0400 Subject: [PATCH 2/7] Use project license headers and scoped CMake helpers **Motivation:** The required license check expects the complete project header. Generic helper names can also collide in CMake superbuilds. **Modifications:** Add the required headers and scope helper names to their package. **Result:** The upstream license script passes locally, and static/shared Ninja and Ninja Multi-Config builds with build-tree and relocated consumers pass on macOS and Linux. --- CMakeLists.txt | 18 +++++++++++++++--- Tests/CMake/CMakeLists.txt | 14 +++++++++++++- cmake/SwiftLibrary.cmake | 16 ++++++++++++++-- cmake/SwiftServiceContextConfig.cmake.in | 14 +++++++++++++- 4 files changed, 55 insertions(+), 7 deletions(-) diff --git a/CMakeLists.txt b/CMakeLists.txt index 45bf8e8..a10c7bc 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -1,4 +1,16 @@ -# SPDX-License-Identifier: Apache-2.0 +##===----------------------------------------------------------------------===## +## +## 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) @@ -16,13 +28,13 @@ add_library(ServiceContextModule Sources/ServiceContextModule/ServiceContextKey.swift ) add_library(SwiftServiceContext::ServiceContextModule ALIAS ServiceContextModule) -swift_library(ServiceContextModule) +_swift_service_context_library(ServiceContextModule) add_library(InstrumentationBaggage Sources/InstrumentationBaggage/CompatibilityShims.swift ) add_library(SwiftServiceContext::InstrumentationBaggage ALIAS InstrumentationBaggage) -swift_library(InstrumentationBaggage) +_swift_service_context_library(InstrumentationBaggage) target_link_libraries(InstrumentationBaggage PUBLIC ServiceContextModule) set(package_directory "${CMAKE_INSTALL_LIBDIR}/cmake/SwiftServiceContext") diff --git a/Tests/CMake/CMakeLists.txt b/Tests/CMake/CMakeLists.txt index cfd7f05..0a643b3 100644 --- a/Tests/CMake/CMakeLists.txt +++ b/Tests/CMake/CMakeLists.txt @@ -1,4 +1,16 @@ -# SPDX-License-Identifier: Apache-2.0 +##===----------------------------------------------------------------------===## +## +## 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 Swift) diff --git a/cmake/SwiftLibrary.cmake b/cmake/SwiftLibrary.cmake index 5c00ce0..434d04f 100644 --- a/cmake/SwiftLibrary.cmake +++ b/cmake/SwiftLibrary.cmake @@ -1,6 +1,18 @@ -# SPDX-License-Identifier: Apache-2.0 +##===----------------------------------------------------------------------===## +## +## 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_library target) +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) diff --git a/cmake/SwiftServiceContextConfig.cmake.in b/cmake/SwiftServiceContextConfig.cmake.in index 832dc37..9e70fdd 100644 --- a/cmake/SwiftServiceContextConfig.cmake.in +++ b/cmake/SwiftServiceContextConfig.cmake.in @@ -1,4 +1,16 @@ -# SPDX-License-Identifier: Apache-2.0 +##===----------------------------------------------------------------------===## +## +## 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@ From 7454ca43f1eebdca4d8bc4f083d7f994896d712a Mon Sep 17 00:00:00 2001 From: RKS Date: Tue, 6 Oct 2026 14:35:04 -0400 Subject: [PATCH 3/7] Match Swift autolink library names on Windows **Motivation:** The Windows consumer test cannot link because Swift requests lib-prefixed libraries while CMake emits unprefixed MSVC library names. **Modifications:** Use the lib prefix for Windows libraries and import libraries. Document the Visual Studio developer environment required for standalone compiler/linker invocations. **Result:** CMake output names match Swift autolink requests. The existing Windows consumer is the regression check; its cloud rerun is pending. The macOS superbuild consumer and license checks pass locally. --- cmake/README.md | 3 +++ cmake/SwiftLibrary.cmake | 4 ++++ 2 files changed, 7 insertions(+) diff --git a/cmake/README.md b/cmake/README.md index 5e91aec..6106a8e 100644 --- a/cmake/README.md +++ b/cmake/README.md @@ -3,6 +3,9 @@ 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. + Configure, build, and install the libraries: ```sh diff --git a/cmake/SwiftLibrary.cmake b/cmake/SwiftLibrary.cmake index 434d04f..e8c30cf 100644 --- a/cmake/SwiftLibrary.cmake +++ b/cmake/SwiftLibrary.cmake @@ -26,6 +26,10 @@ function(_swift_service_context_library target) set_target_properties(${target} PROPERTIES Swift_MODULE_DIRECTORY "${module_directory}" POSITION_INDEPENDENT_CODE YES) + if(WIN32) + # Swift's autolinker uses the lib prefix for MSVC libraries too. + set_target_properties(${target} PROPERTIES PREFIX "lib" IMPORT_PREFIX "lib") + endif() target_compile_options(${target} PRIVATE "$<$:SHELL:-package-name swift_service_context>" "$<$:SHELL:-enable-experimental-feature StrictConcurrency=complete>") From 495be584b2dffc69f6565726313752469f6d079b Mon Sep 17 00:00:00 2001 From: RKS Date: Tue, 6 Oct 2026 15:07:26 -0400 Subject: [PATCH 4/7] Document the compatible Windows C driver **Motivation:** CMake's MSVC C backend initializes shared linker flags that Swift cannot consume directly. The real Windows shared-library build fails before linking its consumer. **Modifications:** Document Clang's GNU-style C driver for Windows configuration and report unsupported shared-driver combinations early. Give the consumer the same C/Swift platform initialization as the libraries and clarify its dependency prefix example. **Result:** Windows configuration uses Swift-compatible linker conventions without dropping linker checks. Static/shared macOS consumers pass; Windows shared validation is pending. --- CMakeLists.txt | 4 ++++ Tests/CMake/CMakeLists.txt | 2 +- cmake/README.md | 6 +++++- 3 files changed, 10 insertions(+), 2 deletions(-) diff --git a/CMakeLists.txt b/CMakeLists.txt index a10c7bc..7382a95 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -18,6 +18,10 @@ project(SwiftServiceContext LANGUAGES C Swift) 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) diff --git a/Tests/CMake/CMakeLists.txt b/Tests/CMake/CMakeLists.txt index 0a643b3..87ab0ab 100644 --- a/Tests/CMake/CMakeLists.txt +++ b/Tests/CMake/CMakeLists.txt @@ -13,7 +13,7 @@ ##===----------------------------------------------------------------------===## cmake_minimum_required(VERSION 3.29) -project(CMakeConsumer LANGUAGES Swift) +project(CMakeConsumer LANGUAGES C Swift) find_package(SwiftServiceContext CONFIG REQUIRED) add_executable(CMakeConsumer main.swift) target_link_libraries(CMakeConsumer PRIVATE SwiftServiceContext::ServiceContextModule SwiftServiceContext::InstrumentationBaggage) diff --git a/cmake/README.md b/cmake/README.md index 6106a8e..0131ff8 100644 --- a/cmake/README.md +++ b/cmake/README.md @@ -4,7 +4,11 @@ CMake 3.29 or newer, Ninja, and Swift 6.2 or newer are required. Ninja Multi-Con 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. +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. Configure, build, and install the libraries: From 7608878a36e0caf5914cdc1414ab5bbf4dade8b8 Mon Sep 17 00:00:00 2001 From: RKS Date: Tue, 6 Oct 2026 15:22:01 -0400 Subject: [PATCH 5/7] Keep Windows DLL import-library names unprefixed **Motivation:** The real shared-library test shows that Swift requests ServiceContextModule.lib for DLL imports, while static imports require libServiceContextModule.lib. Applying the prefix to both forms prevents the dependent DLL from linking. **Modifications:** Apply the lib prefix only to Windows static library targets. Keep native shared-library and import-library names. **Result:** Target names match the distinct Swift autolink conventions. Static Windows consumers already passed; shared Windows validation of this correction is pending. --- cmake/SwiftLibrary.cmake | 7 ++++--- 1 file changed, 4 insertions(+), 3 deletions(-) diff --git a/cmake/SwiftLibrary.cmake b/cmake/SwiftLibrary.cmake index e8c30cf..8ceefca 100644 --- a/cmake/SwiftLibrary.cmake +++ b/cmake/SwiftLibrary.cmake @@ -26,9 +26,10 @@ function(_swift_service_context_library target) set_target_properties(${target} PROPERTIES Swift_MODULE_DIRECTORY "${module_directory}" POSITION_INDEPENDENT_CODE YES) - if(WIN32) - # Swift's autolinker uses the lib prefix for MSVC libraries too. - set_target_properties(${target} PROPERTIES PREFIX "lib" IMPORT_PREFIX "lib") + 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 "$<$:SHELL:-package-name swift_service_context>" From d2e87c85f268a2a7134d87fc72045313eab2a6a9 Mon Sep 17 00:00:00 2001 From: RKS Date: Tue, 6 Oct 2026 15:40:07 -0400 Subject: [PATCH 6/7] Build the compatibility module statically on Windows --- CMakeLists.txt | 8 +++++++- cmake/README.md | 6 ++++-- 2 files changed, 11 insertions(+), 3 deletions(-) diff --git a/CMakeLists.txt b/CMakeLists.txt index 7382a95..17265fa 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -34,7 +34,13 @@ add_library(ServiceContextModule add_library(SwiftServiceContext::ServiceContextModule ALIAS ServiceContextModule) _swift_service_context_library(ServiceContextModule) -add_library(InstrumentationBaggage +# 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) diff --git a/cmake/README.md b/cmake/README.md index 0131ff8..f654ebc 100644 --- a/cmake/README.md +++ b/cmake/README.md @@ -18,8 +18,10 @@ cmake --build build cmake --install build ``` -Pass `-DBUILD_SHARED_LIBS=ON` to build shared libraries. An enclosing CMake project -can also use `add_subdirectory` and link the namespaced targets. The build directory +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: From 9a54f53f0d28fe16011705adc516b3e5155e8d2b Mon Sep 17 00:00:00 2001 From: RKS Date: Tue, 6 Oct 2026 15:50:40 -0400 Subject: [PATCH 7/7] Match the Windows Swift SDK runtime in CMake builds **Motivation:** Hosted consumer tests pass but report conflicting CRT defaults. CMake selects the debug CRT for Debug configurations while the Swift SDK uses the release DLL CRT. **Modifications:** Default Windows library and consumer targets to MultiThreadedDLL unless callers explicitly choose a runtime. Document the matching setting for independent consumers. **Result:** Debug and release configurations select the SDK-compatible CRT without suppressing linker diagnostics. The existing hosted consumer tests will validate the Windows correction; Unix configure/build checks pass. --- CMakeLists.txt | 5 +++++ Tests/CMake/CMakeLists.txt | 5 +++++ cmake/README.md | 3 ++- 3 files changed, 12 insertions(+), 1 deletion(-) diff --git a/CMakeLists.txt b/CMakeLists.txt index 17265fa..3dd1867 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -15,6 +15,11 @@ 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() diff --git a/Tests/CMake/CMakeLists.txt b/Tests/CMake/CMakeLists.txt index 87ab0ab..8be0d14 100644 --- a/Tests/CMake/CMakeLists.txt +++ b/Tests/CMake/CMakeLists.txt @@ -14,6 +14,11 @@ 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) diff --git a/cmake/README.md b/cmake/README.md index f654ebc..6b04bb3 100644 --- a/cmake/README.md +++ b/cmake/README.md @@ -8,7 +8,8 @@ linker and Windows SDK are available. Add `-DCMAKE_C_COMPILER=clang` to each con 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. +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: