From 21b29073eea834d8e47bcfa4ec1ea7fe9c597a13 Mon Sep 17 00:00:00 2001 From: Scott Marchant Date: Tue, 6 May 2025 11:27:31 -0600 Subject: [PATCH 1/8] chore: Remove unused Dispatch imports. --- Sources/Tracing/NoOpTracer.swift | 1 - Sources/Tracing/Tracer.swift | 1 - Sources/Tracing/TracerProtocol+Legacy.swift | 1 - 3 files changed, 3 deletions(-) diff --git a/Sources/Tracing/NoOpTracer.swift b/Sources/Tracing/NoOpTracer.swift index 244f4ef0..746a48c2 100644 --- a/Sources/Tracing/NoOpTracer.swift +++ b/Sources/Tracing/NoOpTracer.swift @@ -12,7 +12,6 @@ // //===----------------------------------------------------------------------===// -import Dispatch @_exported import Instrumentation @_exported import ServiceContextModule diff --git a/Sources/Tracing/Tracer.swift b/Sources/Tracing/Tracer.swift index e87c8c61..1fee0788 100644 --- a/Sources/Tracing/Tracer.swift +++ b/Sources/Tracing/Tracer.swift @@ -12,7 +12,6 @@ // //===----------------------------------------------------------------------===// -import Dispatch @_exported import Instrumentation @_exported import ServiceContextModule diff --git a/Sources/Tracing/TracerProtocol+Legacy.swift b/Sources/Tracing/TracerProtocol+Legacy.swift index 79b61b18..2ddab18e 100644 --- a/Sources/Tracing/TracerProtocol+Legacy.swift +++ b/Sources/Tracing/TracerProtocol+Legacy.swift @@ -12,7 +12,6 @@ // //===----------------------------------------------------------------------===// -import Dispatch @_exported import Instrumentation @_exported import ServiceContextModule From c8bf8bbd90b79f803e9e1377115234a79cb49636 Mon Sep 17 00:00:00 2001 From: Scott Marchant Date: Tue, 6 May 2025 11:28:03 -0600 Subject: [PATCH 2/8] chore: Adding missing wasilibc and pthread imports. --- Sources/Instrumentation/Locks.swift | 5 +++++ Sources/Tracing/TracingTime.swift | 6 ++++++ 2 files changed, 11 insertions(+) diff --git a/Sources/Instrumentation/Locks.swift b/Sources/Instrumentation/Locks.swift index 0a3f866c..b04ea62d 100644 --- a/Sources/Instrumentation/Locks.swift +++ b/Sources/Instrumentation/Locks.swift @@ -34,6 +34,11 @@ import Glibc import Android #elseif canImport(Musl) import Musl +#elseif canImport(WASILibc) +import WASILibc +#if canImport(wasi_pthread) +import wasi_pthread +#endif #else #error("Unsupported runtime") #endif diff --git a/Sources/Tracing/TracingTime.swift b/Sources/Tracing/TracingTime.swift index c4887e5e..16134273 100644 --- a/Sources/Tracing/TracingTime.swift +++ b/Sources/Tracing/TracingTime.swift @@ -23,10 +23,16 @@ import Glibc import Android #elseif canImport(Musl) import Musl +#elseif canImport(WASILibc) +import WASILibc #else #error("Unsupported runtime") #endif +#if canImport(_CWASI) +import _CWASI +#endif + public protocol TracerInstant: Comparable, Hashable, Sendable { /// Representation of this instant as the number of nanoseconds since UNIX Epoch (January 1st 1970) var nanosecondsSinceEpoch: UInt64 { get } From 9ad9a3380f8f6aabba9a7569852692230ab3ce65 Mon Sep 17 00:00:00 2001 From: Scott Marchant Date: Tue, 6 May 2025 11:29:18 -0600 Subject: [PATCH 3/8] feat: Add shim to allow wrapped access to CLOCK_REALTIME struct from WASILibc. --- Package.swift | 11 +++++++++++ Sources/Tracing/TracingTime.swift | 6 +++++- Sources/_CWASI/_CWASI.c | 13 +++++++++++++ Sources/_CWASI/include/_CWASI.h | 27 +++++++++++++++++++++++++++ 4 files changed, 56 insertions(+), 1 deletion(-) create mode 100644 Sources/_CWASI/_CWASI.c create mode 100644 Sources/_CWASI/include/_CWASI.h diff --git a/Package.swift b/Package.swift index 1a1ef2ba..99d5bb18 100644 --- a/Package.swift +++ b/Package.swift @@ -35,6 +35,7 @@ let package = Package( dependencies: [ .product(name: "ServiceContextModule", package: "swift-service-context"), .target(name: "Instrumentation"), + .target(name: "_CWASI", condition: .when(platforms: [.wasi])), ] ), .testTarget( @@ -43,6 +44,16 @@ let package = Package( .target(name: "Tracing") ] ), + + // ==== -------------------------------------------------------------------------------------------------------- + // MARK: Wasm Support + + // Provides C shims for compiling to wasm + .target( + name: "_CWASI", + dependencies: [] + ), + ] ) diff --git a/Sources/Tracing/TracingTime.swift b/Sources/Tracing/TracingTime.swift index 16134273..fcfcdc3b 100644 --- a/Sources/Tracing/TracingTime.swift +++ b/Sources/Tracing/TracingTime.swift @@ -90,7 +90,11 @@ public struct DefaultTracerClock { public var now: Self.Instant { var ts = timespec() - clock_gettime(CLOCK_REALTIME, &ts) + #if os(WASI) + CWASI_clock_gettime_realtime(&ts) + #else + clock_gettime(CLOCK_REALTIME, &ts) + #endif /// We use unsafe arithmetic here because `UInt64.max` nanoseconds is more than 580 years, /// and the odds that this code will still be running 530 years from now is very, very low, /// so as a practical matter this will never overflow. diff --git a/Sources/_CWASI/_CWASI.c b/Sources/_CWASI/_CWASI.c new file mode 100644 index 00000000..7d1b4a88 --- /dev/null +++ b/Sources/_CWASI/_CWASI.c @@ -0,0 +1,13 @@ +//===----------------------------------------------------------------------===// +// +// This source file is part of the Swift Distributed Tracing open source project +// +// Copyright (c) 2025 Apple Inc. and the Swift Distributed Tracing project authors +// Licensed under Apache License v2.0 +// +// See LICENSE.txt for license information +// See CONTRIBUTORS.txt for the list of Swift Distributed Tracing project authors +// +// SPDX-License-Identifier: Apache-2.0 +// +//===----------------------------------------------------------------------===// diff --git a/Sources/_CWASI/include/_CWASI.h b/Sources/_CWASI/include/_CWASI.h new file mode 100644 index 00000000..70553cf3 --- /dev/null +++ b/Sources/_CWASI/include/_CWASI.h @@ -0,0 +1,27 @@ +//===----------------------------------------------------------------------===// +// +// This source file is part of the Swift Distributed Tracing open source project +// +// Copyright (c) 2025 Apple Inc. and the Swift Distributed Tracing project authors +// Licensed under Apache License v2.0 +// +// See LICENSE.txt for license information +// See CONTRIBUTORS.txt for the list of Swift Distributed Tracing project authors +// +// SPDX-License-Identifier: Apache-2.0 +// +//===----------------------------------------------------------------------===// + +#pragma once + +#if __wasi__ + +#include +#include + +static inline void CWASI_clock_gettime_realtime(struct timespec *tv) { + // ClangImporter doesn't support `CLOCK_REALTIME` declaration in WASILibc, thus we have to define a bridge manually + clock_gettime(CLOCK_REALTIME, tv); +} + +#endif // __wasi__ From bfdfbadcf46264268ea74d614cf49b6549993518 Mon Sep 17 00:00:00 2001 From: Scott Marchant Date: Mon, 23 Jun 2025 15:21:22 -0600 Subject: [PATCH 4/8] chore: Address swiftformat lint in modified lines of code. --- Package.swift | 1 - Sources/Instrumentation/Locks.swift | 8 ++++---- Sources/Tracing/TracingTime.swift | 4 ++-- 3 files changed, 6 insertions(+), 7 deletions(-) diff --git a/Package.swift b/Package.swift index 99d5bb18..65e12789 100644 --- a/Package.swift +++ b/Package.swift @@ -53,7 +53,6 @@ let package = Package( name: "_CWASI", dependencies: [] ), - ] ) diff --git a/Sources/Instrumentation/Locks.swift b/Sources/Instrumentation/Locks.swift index b04ea62d..8f36ad9a 100644 --- a/Sources/Instrumentation/Locks.swift +++ b/Sources/Instrumentation/Locks.swift @@ -35,10 +35,10 @@ import Android #elseif canImport(Musl) import Musl #elseif canImport(WASILibc) -import WASILibc -#if canImport(wasi_pthread) -import wasi_pthread -#endif + import WASILibc + #if canImport(wasi_pthread) + import wasi_pthread + #endif #else #error("Unsupported runtime") #endif diff --git a/Sources/Tracing/TracingTime.swift b/Sources/Tracing/TracingTime.swift index fcfcdc3b..7f99c8e2 100644 --- a/Sources/Tracing/TracingTime.swift +++ b/Sources/Tracing/TracingTime.swift @@ -24,13 +24,13 @@ import Android #elseif canImport(Musl) import Musl #elseif canImport(WASILibc) -import WASILibc + import WASILibc #else #error("Unsupported runtime") #endif #if canImport(_CWASI) -import _CWASI + import _CWASI #endif public protocol TracerInstant: Comparable, Hashable, Sendable { From 9f4a29234495d07e74fe49a430cb36c2df039d07 Mon Sep 17 00:00:00 2001 From: Scott Marchant Date: Tue, 5 Aug 2025 10:41:48 -0600 Subject: [PATCH 5/8] chore: Clean up new lint flagged by swift-format in soundness check. --- Sources/Instrumentation/Locks.swift | 8 ++++---- Sources/Tracing/TracingTime.swift | 8 ++++---- 2 files changed, 8 insertions(+), 8 deletions(-) diff --git a/Sources/Instrumentation/Locks.swift b/Sources/Instrumentation/Locks.swift index 8f36ad9a..b04ea62d 100644 --- a/Sources/Instrumentation/Locks.swift +++ b/Sources/Instrumentation/Locks.swift @@ -35,10 +35,10 @@ import Android #elseif canImport(Musl) import Musl #elseif canImport(WASILibc) - import WASILibc - #if canImport(wasi_pthread) - import wasi_pthread - #endif +import WASILibc +#if canImport(wasi_pthread) +import wasi_pthread +#endif #else #error("Unsupported runtime") #endif diff --git a/Sources/Tracing/TracingTime.swift b/Sources/Tracing/TracingTime.swift index 7f99c8e2..684c022a 100644 --- a/Sources/Tracing/TracingTime.swift +++ b/Sources/Tracing/TracingTime.swift @@ -24,13 +24,13 @@ import Android #elseif canImport(Musl) import Musl #elseif canImport(WASILibc) - import WASILibc +import WASILibc #else #error("Unsupported runtime") #endif #if canImport(_CWASI) - import _CWASI +import _CWASI #endif public protocol TracerInstant: Comparable, Hashable, Sendable { @@ -91,9 +91,9 @@ public struct DefaultTracerClock { public var now: Self.Instant { var ts = timespec() #if os(WASI) - CWASI_clock_gettime_realtime(&ts) + CWASI_clock_gettime_realtime(&ts) #else - clock_gettime(CLOCK_REALTIME, &ts) + clock_gettime(CLOCK_REALTIME, &ts) #endif /// We use unsafe arithmetic here because `UInt64.max` nanoseconds is more than 580 years, /// and the odds that this code will still be running 530 years from now is very, very low, From a9691e9c71e8c91b1bba46a500e5a702cfd64f4f Mon Sep 17 00:00:00 2001 From: Scott Marchant Date: Tue, 5 Aug 2025 11:08:42 -0600 Subject: [PATCH 6/8] chore: Adjust import guard to os(WASI). --- Sources/Tracing/TracingTime.swift | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Sources/Tracing/TracingTime.swift b/Sources/Tracing/TracingTime.swift index 684c022a..328b5332 100644 --- a/Sources/Tracing/TracingTime.swift +++ b/Sources/Tracing/TracingTime.swift @@ -29,7 +29,7 @@ import WASILibc #error("Unsupported runtime") #endif -#if canImport(_CWASI) +#if os(WASI) import _CWASI #endif From 386e324dbb9b6f3d3fe3eb0244ca6d96ba6fec63 Mon Sep 17 00:00:00 2001 From: Scott Marchant Date: Tue, 5 Aug 2025 11:10:34 -0600 Subject: [PATCH 7/8] ci: Disable explicit target dependency import checks for swift versions before Swift 6, due to associated false positive errors. See FB14859516. --- .github/workflows/pull_request.yml | 20 ++++++++++++++++++-- 1 file changed, 18 insertions(+), 2 deletions(-) diff --git a/.github/workflows/pull_request.yml b/.github/workflows/pull_request.yml index 59e9d344..e05d5585 100644 --- a/.github/workflows/pull_request.yml +++ b/.github/workflows/pull_request.yml @@ -17,8 +17,24 @@ jobs: name: Unit tests uses: apple/swift-nio/.github/workflows/unit_tests.yml@main with: - linux_5_9_arguments_override: "--explicit-target-dependency-import-check error" - linux_5_10_arguments_override: "--explicit-target-dependency-import-check error" + + # NOTE: Aug 5, 2025. We've determined SPM has bugs for `condition: .when(platforms: [.somePlatform])` + # that incorrectly ignore the condition and import anyways for unintended platforms. + # + # See https://github.com/apple/swift-distributed-tracing/actions/runs/16578688393/job/46920025183?pr=174 + # for an example of the resulting build failure. + # + # This is documented as a bug in Swift 5.10, resolved in Swift 6. + # See FB14859516. + # See also https://forums.swift.org/t/do-swiftpm-conditional-target-dependencies-work/74047 + # + # Given this, the explicit target dependency check is disabled for Swift versions before Swift 6. + + # Intentionally disabled: + # + # linux_5_9_arguments_override: "--explicit-target-dependency-import-check error" + # linux_5_10_arguments_override: "--explicit-target-dependency-import-check error" + linux_6_0_arguments_override: "--explicit-target-dependency-import-check error -Xswiftc -require-explicit-sendable" linux_6_1_arguments_override: "--explicit-target-dependency-import-check error -Xswiftc -require-explicit-sendable" linux_nightly_6_0_arguments_override: "--explicit-target-dependency-import-check error -Xswiftc -require-explicit-sendable" From ddcb63b3086372b0d5adcc1b48993174bb83f3c4 Mon Sep 17 00:00:00 2001 From: Scott Marchant Date: Wed, 6 Aug 2025 10:24:50 -0600 Subject: [PATCH 8/8] chore: Clean up extra whitespace lint in pull_request.yml --- .github/workflows/pull_request.yml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/.github/workflows/pull_request.yml b/.github/workflows/pull_request.yml index e05d5585..af259c85 100644 --- a/.github/workflows/pull_request.yml +++ b/.github/workflows/pull_request.yml @@ -17,7 +17,7 @@ jobs: name: Unit tests uses: apple/swift-nio/.github/workflows/unit_tests.yml@main with: - + # NOTE: Aug 5, 2025. We've determined SPM has bugs for `condition: .when(platforms: [.somePlatform])` # that incorrectly ignore the condition and import anyways for unintended platforms. #