diff --git a/Package.swift b/Package.swift index 1a1ef2ba..65e12789 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,15 @@ let package = Package( .target(name: "Tracing") ] ), + + // ==== -------------------------------------------------------------------------------------------------------- + // MARK: Wasm Support + + // Provides C shims for compiling to wasm + .target( + name: "_CWASI", + dependencies: [] + ), ] ) diff --git a/Sources/Instrumentation/Locks.swift b/Sources/Instrumentation/Locks.swift index 0a3f866c..8f36ad9a 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/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 diff --git a/Sources/Tracing/TracingTime.swift b/Sources/Tracing/TracingTime.swift index c4887e5e..7f99c8e2 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 } @@ -84,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__