Skip to content

feat: Add Swift WebAssembly compilation support to swift-distributed-tracing #174

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 9 commits into from
Aug 8, 2025
Merged
20 changes: 18 additions & 2 deletions .github/workflows/pull_request.yml
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down
10 changes: 10 additions & 0 deletions Package.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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])),
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We seem to be getting an error on this line on 5.10

error: Target Tracing imports another target (_CWASI) in the package without declaring it a dependency.

Is this a bug in the package manager on 5.10? Can you take a look and if it is a bug we can just disable the strict dependency check on just 5.10

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@FranzBusch Indeed it does look like that is a bug. I tracked it down to FB14859516 metnioned in https://forums.swift.org/t/do-swiftpm-conditional-target-dependencies-work/74047.

I removed the dependency check for swift versions before Swift 6. See 386e324

]
),
.testTarget(
Expand All @@ -43,6 +44,15 @@ let package = Package(
.target(name: "Tracing")
]
),

// ==== --------------------------------------------------------------------------------------------------------
// MARK: Wasm Support

// Provides C shims for compiling to wasm
.target(
name: "_CWASI",
dependencies: []
),
]
)

Expand Down
5 changes: 5 additions & 0 deletions Sources/Instrumentation/Locks.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
1 change: 0 additions & 1 deletion Sources/Tracing/NoOpTracer.swift
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,6 @@
//
//===----------------------------------------------------------------------===//

import Dispatch
@_exported import Instrumentation
@_exported import ServiceContextModule

Expand Down
1 change: 0 additions & 1 deletion Sources/Tracing/Tracer.swift
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,6 @@
//
//===----------------------------------------------------------------------===//

import Dispatch
@_exported import Instrumentation
@_exported import ServiceContextModule

Expand Down
1 change: 0 additions & 1 deletion Sources/Tracing/TracerProtocol+Legacy.swift
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,6 @@
//
//===----------------------------------------------------------------------===//

import Dispatch
@_exported import Instrumentation
@_exported import ServiceContextModule

Expand Down
10 changes: 10 additions & 0 deletions Sources/Tracing/TracingTime.swift
Original file line number Diff line number Diff line change
Expand Up @@ -23,10 +23,16 @@ import Glibc
import Android
#elseif canImport(Musl)
import Musl
#elseif canImport(WASILibc)
import WASILibc
#else
#error("Unsupported runtime")
#endif

#if os(WASI)
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 }
Expand Down Expand Up @@ -84,7 +90,11 @@ public struct DefaultTracerClock {

public var now: Self.Instant {
var ts = timespec()
#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.
Expand Down
13 changes: 13 additions & 0 deletions Sources/_CWASI/_CWASI.c
Original file line number Diff line number Diff line change
@@ -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
//
//===----------------------------------------------------------------------===//
27 changes: 27 additions & 0 deletions Sources/_CWASI/include/_CWASI.h
Original file line number Diff line number Diff line change
@@ -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 <fcntl.h>
#include <time.h>

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__