Skip to content

feat: Add swift wasm 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

Open
wants to merge 4 commits into
base: main
Choose a base branch
from
Open
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
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])),
]
),
.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
12 changes: 11 additions & 1 deletion Sources/Tracing/TracingTime.swift
Original file line number Diff line number Diff line change
Expand Up @@ -23,10 +23,16 @@ import Glibc
import Android
Copy link
Author

Choose a reason for hiding this comment

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

Note, there is a lot of existing swiftformat lint in this repository, including the existing indentation on many of these import blocks. I fixed the lint on the lines of code I'm changing in this PR. But I didn't fix it for everything else, to avoid making the diff far too large.

#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 }
Expand Down Expand Up @@ -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.
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__