Skip to content
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
2 changes: 1 addition & 1 deletion Examples/Sources/Factorial/Factorial.swift
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ struct Example {
static func main() throws {
// Convert a WAT file to a Wasm binary, then parse it.
let module = try parseWasm(
bytes: try wat2wasm(String(contentsOfFile: "wasm/factorial.wat"))
bytes: try wat2wasm(String(contentsOfFile: "wasm/factorial.wat", encoding: .utf8))
)

// Create a module instance from the parsed module.
Expand Down
266 changes: 266 additions & 0 deletions Package@swift-6.2.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,266 @@
// swift-tools-version:6.2

import PackageDescription

import class Foundation.ProcessInfo

let DarwinPlatforms: [Platform] = [.macOS, .iOS, .watchOS, .tvOS, .visionOS]

let cliCommandsTarget = Target.target(
name: "CLICommands",
dependencies: [
"WAT",
"WasmKit",
"WasmKitWASI",
.product(name: "ArgumentParser", package: "swift-argument-parser"),
.product(name: "SystemPackage", package: "swift-system"),
],
exclude: ["CMakeLists.txt"]
)

let swiftSettings: [SwiftSetting] = [
.treatAllWarnings(as: .error, .when(platforms: DarwinPlatforms + [.linux, .wasi, .android, .openbsd])),
]

let package = Package(
name: "WasmKit",
platforms: [.macOS(.v14), .iOS(.v13)],
products: [
.executable(name: "wasmkit-cli", targets: ["CLI"]),
.library(name: "WasmKit", targets: ["WasmKit"]),
.library(name: "WasmKitWASI", targets: ["WasmKitWASI"]),
.library(name: "WASI", targets: ["WASI"]),
.library(name: "WasmParser", targets: ["WasmParser"]),
.library(name: "WAT", targets: ["WAT"]),
.library(name: "WIT", targets: ["WIT"]),
.library(name: "_CabiShims", targets: ["_CabiShims"]),
],
traits: [
.default(enabledTraits: []),
"WasmDebuggingSupport",
],
targets: [
cliCommandsTarget,
.executableTarget(
name: "CLI",
dependencies: ["CLICommands"],
exclude: ["CMakeLists.txt"],
swiftSettings: swiftSettings
),
.target(
name: "WasmKit",
dependencies: [
"_CWasmKit",
"WasmParser",
"WasmTypes",
"SystemExtras",
.product(name: "SystemPackage", package: "swift-system"),
],
exclude: ["CMakeLists.txt"],
swiftSettings: swiftSettings
),
.target(name: "_CWasmKit"),
.target(
name: "WasmKitFuzzing",
dependencies: ["WasmKit"],
path: "FuzzTesting/Sources/WasmKitFuzzing",
swiftSettings: swiftSettings
),
.testTarget(
name: "WasmKitTests",
dependencies: ["WasmKit", "WAT", "WasmKitFuzzing"],
exclude: ["ExtraSuite", "CMakeLists.txt"],
swiftSettings: swiftSettings
),

.target(
name: "WAT",
dependencies: ["WasmParser"],
exclude: ["CMakeLists.txt"],
swiftSettings: swiftSettings
),
.testTarget(
name: "WATTests",
dependencies: ["WAT"],
swiftSettings: swiftSettings
),

.target(
name: "WasmParser",
dependencies: [
"WasmTypes",
.product(name: "SystemPackage", package: "swift-system"),
],
exclude: ["CMakeLists.txt"],
swiftSettings: swiftSettings
),
.testTarget(
name: "WasmParserTests",
dependencies: ["WasmParser"],
swiftSettings: swiftSettings
),

.target(
name: "WasmTypes",
exclude: ["CMakeLists.txt"],
swiftSettings: swiftSettings
),

.target(
name: "WasmKitWASI",
dependencies: ["WasmKit", "WASI"],
exclude: ["CMakeLists.txt"],
swiftSettings: swiftSettings
),
.target(
name: "WASI",
dependencies: ["WasmTypes", "SystemExtras"],
exclude: ["CMakeLists.txt"],
swiftSettings: swiftSettings
),
.testTarget(
name: "WASITests",
dependencies: ["WASI", "WasmKitWASI"],
swiftSettings: swiftSettings
),

.target(
name: "SystemExtras",
dependencies: [
.product(name: "SystemPackage", package: "swift-system"),
.target(name: "CSystemExtras", condition: .when(platforms: [.wasi])),
],
exclude: ["CMakeLists.txt"],
swiftSettings: swiftSettings + [
.define("SYSTEM_PACKAGE_DARWIN", .when(platforms: DarwinPlatforms))
]
),

.target(name: "CSystemExtras"),

.executableTarget(
name: "WITTool",
dependencies: [
"WIT",
"WITOverlayGenerator",
"WITExtractor",
.product(name: "ArgumentParser", package: "swift-argument-parser"),
],
swiftSettings: swiftSettings
),

.target(
name: "WIT",
swiftSettings: swiftSettings
),
.testTarget(
name: "WITTests",
dependencies: ["WIT"],
swiftSettings: swiftSettings
),

.target(
name: "WITOverlayGenerator",
dependencies: ["WIT"],
swiftSettings: swiftSettings
),
.target(name: "_CabiShims"),

.target(
name: "WITExtractor",
swiftSettings: swiftSettings
),
.testTarget(
name: "WITExtractorTests",
dependencies: ["WITExtractor", "WIT"],
swiftSettings: swiftSettings
),

.target(
name: "GDBRemoteProtocol",
dependencies: [
.product(name: "Logging", package: "swift-log"),
.product(name: "NIOCore", package: "swift-nio"),
],
exclude: ["LICENSE.txt"],
swiftSettings: swiftSettings
),
.testTarget(
name: "GDBRemoteProtocolTests",
dependencies: ["GDBRemoteProtocol"],
exclude: ["LICENSE.txt"],
swiftSettings: swiftSettings
),
]
)

if ProcessInfo.processInfo.environment["SWIFTCI_USE_LOCAL_DEPS"] == nil {
package.dependencies += [
.package(url: "https://github.com/apple/swift-argument-parser", from: "1.5.1"),
.package(url: "https://github.com/apple/swift-system", from: "1.5.0"),
.package(url: "https://github.com/apple/swift-nio", from: "2.90.0"),
.package(url: "https://github.com/apple/swift-log", from: "1.7.1"),
]
} else {
package.dependencies += [
.package(path: "../swift-argument-parser"),
.package(path: "../swift-system"),
.package(path: "../swift-nio"),
.package(path: "../swift-log"),
]
}

#if !os(Windows)
// Add build tool plugins only for non-Windows platforms
package.products.append(contentsOf: [
.plugin(name: "WITOverlayPlugin", targets: ["WITOverlayPlugin"]),
.plugin(name: "WITExtractorPlugin", targets: ["WITExtractorPlugin"]),
])

package.targets.append(contentsOf: [
.plugin(name: "WITOverlayPlugin", capability: .buildTool(), dependencies: ["WITTool"]),
.plugin(name: "GenerateOverlayForTesting", capability: .buildTool(), dependencies: ["WITTool"]),
.testTarget(
name: "WITOverlayGeneratorTests",
dependencies: ["WITOverlayGenerator", "WasmKit", "WasmKitWASI"],
exclude: ["Fixtures", "Compiled", "Generated", "EmbeddedSupport"],
swiftSettings: swiftSettings,
plugins: [.plugin(name: "GenerateOverlayForTesting")]
),
.plugin(
name: "WITExtractorPlugin",
capability: .command(
intent: .custom(verb: "extract-wit", description: "Extract WIT definition from Swift module"),
permissions: []
),
dependencies: ["WITTool"]
),
.testTarget(
name: "WITExtractorPluginTests",
exclude: ["Fixtures"],
swiftSettings: swiftSettings
),

.target(
name: "WasmKitGDBHandler",
dependencies: [
.product(name: "_NIOFileSystem", package: "swift-nio"),
.product(name: "NIOCore", package: "swift-nio"),
.product(name: "SystemPackage", package: "swift-system"),
"WasmKit",
"WasmKitWASI",
"GDBRemoteProtocol",
],
exclude: ["LICENSE.txt"],
swiftSettings: swiftSettings
),
])

cliCommandsTarget.dependencies.append(contentsOf: [
.product(name: "Logging", package: "swift-log", condition: .when(traits: ["WasmDebuggingSupport"])),
.product(name: "NIOCore", package: "swift-nio", condition: .when(traits: ["WasmDebuggingSupport"])),
.product(name: "NIOPosix", package: "swift-nio", condition: .when(traits: ["WasmDebuggingSupport"])),
.target(name: "GDBRemoteProtocol", condition: .when(traits: ["WasmDebuggingSupport"])),
.target(name: "WasmKitGDBHandler", condition: .when(traits: ["WasmDebuggingSupport"])),
])
#endif
8 changes: 5 additions & 3 deletions Tests/WASITests/TestSupport.swift
Original file line number Diff line number Diff line change
Expand Up @@ -137,7 +137,7 @@ enum TestSupport {
init() throws {
let tempdir = URL(fileURLWithPath: NSTemporaryDirectory())
let templatePath = tempdir.appendingPathComponent("WasmKit.XXXXXX")
var template = [UInt8](templatePath.path.utf8).map({ Int8($0) }) + [Int8(0)]
var template = [UInt8](templatePath.path.utf8).map({ UInt8($0) }) + [UInt8(0)]

#if os(Windows)
if _mktemp_s(&template, template.count) != 0 {
Expand All @@ -156,7 +156,7 @@ enum TestSupport {
}
#endif

self.path = String(cString: template)
self.path = String(decoding: template.dropLast(), as: UTF8.self)
}

func createDir(at relativePath: String) throws {
Expand All @@ -167,7 +167,9 @@ enum TestSupport {
func createFile(at relativePath: String, contents: String) throws {
let fileURL = url.appendingPathComponent(relativePath)
guard let data = contents.data(using: .utf8) else { return }
FileManager.default.createFile(atPath: fileURL.path, contents: data, attributes: nil)
guard FileManager.default.createFile(atPath: fileURL.path, contents: data, attributes: nil) else {
throw Error(description: "Couldn't create file at \(relativePath)")
}
}

func createSymlink(at relativePath: String, to target: String) throws {
Expand Down
2 changes: 1 addition & 1 deletion Tests/WATTests/EncoderTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,7 @@ struct EncoderTests {
}

var parser = WastParser(
try String(contentsOf: wast),
try String(contentsOf: wast, encoding: .utf8),
features: Spectest.deriveFeatureSet(wast: wast)
)
var watModules: [ModuleDirective] = []
Expand Down
2 changes: 1 addition & 1 deletion Tests/WATTests/LexerTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -96,7 +96,7 @@ struct LexerTests {
arguments: Spectest.wastFiles(include: [])
)
func lexSpectest(wastFile: URL) throws {
let source = try String(contentsOf: wastFile)
let source = try String(contentsOf: wastFile, encoding: .utf8)
_ = try collectToken(source)
}
}
2 changes: 1 addition & 1 deletion Tests/WATTests/ParserTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -189,7 +189,7 @@ struct ParserTests {
arguments: Spectest.wastFiles(include: [])
)
func parseSpectest(wastFile: URL) throws {
let source = try String(contentsOf: wastFile)
let source = try String(contentsOf: wastFile, encoding: .utf8)
_ = try parseWast(source, features: Spectest.deriveFeatureSet(wast: wastFile))
}
}
4 changes: 2 additions & 2 deletions Tests/WATTests/TestSupport.swift
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ enum TestSupport {
) throws -> Result {
let tempdir = URL(fileURLWithPath: NSTemporaryDirectory())
let templatePath = tempdir.appendingPathComponent("WasmKit.XXXXXX")
var template = [UInt8](templatePath.path.utf8).map({ Int8($0) }) + [Int8(0)]
var template = [UInt8](templatePath.path.utf8).map({ UInt8($0) }) + [UInt8(0)]

#if os(Windows)
if _mktemp_s(&template, template.count) != 0 {
Expand All @@ -37,7 +37,7 @@ enum TestSupport {
}
#endif

let path = String(cString: template)
let path = String(decoding: template.dropLast(), as: UTF8.self)
var shouldRetain = false
defer {
if !shouldRetain {
Expand Down
6 changes: 3 additions & 3 deletions Tests/WITExtractorPluginTests/TestSupport.swift
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ struct TestSupport {
) throws -> Result {
let tempdir = URL(fileURLWithPath: NSTemporaryDirectory())
let templatePath = tempdir.appendingPathComponent("WasmKit.XXXXXX")
var template = [UInt8](templatePath.path.utf8).map({ Int8($0) }) + [Int8(0)]
var template = [UInt8](templatePath.path.utf8).map({ UInt8($0) }) + [UInt8(0)]

if mkdtemp(&template) == nil {
#if os(Android)
Expand All @@ -31,7 +31,7 @@ struct TestSupport {
#endif
}

let path = String(cString: template)
let path = String(decoding: template.dropLast(), as: UTF8.self)
defer { _ = try? FileManager.default.removeItem(atPath: path) }
return try body(path)
}
Expand Down Expand Up @@ -124,7 +124,7 @@ func assertSwiftPackage(fixturePackage: String, _ trailingArguments: [String]) t
"""
)
}
return try String(contentsOfFile: jsonOutput.witOutputPath)
return try String(contentsOfFile: jsonOutput.witOutputPath, encoding: .utf8)
}
#endif
}
Loading
Loading