Xwift

Getting started

Build, test and run the proof-of-concept package natively and as WebAssembly.

This guide reproduces how the proof of concept was built and tested. The proof of concept used the stock Swift 6.4.1 toolchain from swift.org. To install the Xwift 6.4.1 toolchain or stock Swift 6.4.1, see Install.

The proof-of-concept repository is not public. The code on this page is the complete source of the package's public API and executable.

The package

Package.swift
// swift-tools-version:6.0
import PackageDescription

let package = Package(
    name: "Xwift",
    products: [
        .library(name: "XwiftCore", targets: ["XwiftCore"]),
        .executable(name: "xwift-hello", targets: ["xwift-hello"]),
    ],
    targets: [
        .target(name: "XwiftCore"),
        .executableTarget(name: "xwift-hello", dependencies: ["XwiftCore"]),
        .testTarget(name: "XwiftCoreTests", dependencies: ["XwiftCore"]),
    ],
    swiftLanguageModes: [.v6]
)
Sources/xwift-hello/main.swift
import XwiftCore

print(greeting())

PlatformInfo describes the platform the binary was compiled for, resolved with conditional compilation:

Sources/XwiftCore/PlatformInfo.swift
/// Compile-time description of the platform Xwift was built for.
public struct PlatformInfo: Sendable, Equatable, CustomStringConvertible {
    public enum OperatingSystem: String, Sendable, CaseIterable {
        case macOS, iOS, linux, windows, wasi, android, unknown
    }

    public enum Architecture: String, Sendable, CaseIterable {
        case x86_64, arm64, wasm32, unknown
    }

    public let os: OperatingSystem
    public let architecture: Architecture
    public let pointerBitWidth: Int

    public init(os: OperatingSystem, architecture: Architecture, pointerBitWidth: Int) {
        self.os = os
        self.architecture = architecture
        self.pointerBitWidth = pointerBitWidth
    }

    /// The platform this binary was compiled for.
    public static let current = PlatformInfo(
        os: currentOS,
        architecture: currentArchitecture,
        pointerBitWidth: MemoryLayout<Int>.size * 8
    )

    public var description: String {
        "\(os.rawValue)-\(architecture.rawValue) (\(pointerBitWidth)-bit)"
    }

    private static var currentOS: OperatingSystem {
        #if os(macOS)
        return .macOS
        #elseif os(iOS)
        return .iOS
        #elseif os(Android)
        return .android
        #elseif os(Linux)
        return .linux
        #elseif os(Windows)
        return .windows
        #elseif os(WASI)
        return .wasi
        #else
        return .unknown
        #endif
    }

    private static var currentArchitecture: Architecture {
        #if arch(x86_64)
        return .x86_64
        #elseif arch(arm64)
        return .arm64
        #elseif arch(wasm32)
        return .wasm32
        #else
        return .unknown
        #endif
    }
}

/// Greeting used by the `xwift-hello` executable.
public func greeting(for platform: PlatformInfo = .current) -> String {
    "Hello from Xwift on \(platform)!"
}

PlatformInfo already has iOS and android cases, but those targets are not built or tested yet. They are on the roadmap.

Build, test and run natively

Works the same on macOS, Linux and Windows:

Build

swift build

Test

Runs both the XCTest and swift-testing suites:

swift test

Run

swift run xwift-hello

On a 64-bit Linux x86_64 machine this prints:

Hello from Xwift on linux-x86_64 (64-bit)!

The proof of concept was built in release mode, with the same commands and -c release:

swift build -c release
swift test --parallel
swift run -c release xwift-hello

Build and run as WebAssembly

Requires the Swift SDK for WebAssembly and wasmtime from Install.

Build for wasm32-wasi

swift build -c release --swift-sdk swift-6.4.1-RELEASE_wasm --product xwift-hello

Run with wasmtime

wasmtime .build/release/xwift-hello.wasm

The output contains wasi-wasm32, which confirms the WebAssembly build:

Hello from Xwift on wasi-wasm32 (32-bit)!

Tests

Tests/XwiftCoreTests/PlatformInfoTests.swift (excerpt)
import Testing
@testable import XwiftCore

@Suite struct PlatformInfoTests {
    @Test func pointerWidthMatchesInt() {
        #expect(PlatformInfo.current.pointerBitWidth == Int.bitWidth)
    }

    @Test(arguments: PlatformInfo.OperatingSystem.allCases)
    func descriptionContainsOS(_ os: PlatformInfo.OperatingSystem) {
        let p = PlatformInfo(os: os, architecture: .wasm32, pointerBitWidth: 32)
        #expect(p.description == "\(os.rawValue)-wasm32 (32-bit)")
    }
}

The WebAssembly build runs the executable. It does not run the test suite under WASI.

On this page