Skip to content
Draft
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
7 changes: 7 additions & 0 deletions .bsp/xtool.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
{
"name": "xtool",
"version": "1.0",
"bspVersion": "2.2.0",
"languages": ["c", "cpp", "objective-c", "objective-cpp", "swift"],
"argv": ["/usr/bin/env", "xtool", "dev", "build-server", "--triple", "arm64-apple-macosx"]
}
10 changes: 10 additions & 0 deletions .swiftlint.yml
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,15 @@ custom_rules:
- identifier
message: "Use d.out.print instead of print or Swift.print."
severity: error
prefer_xtool_subprocess_output:
name: "Subprocess Dependency Output"
included: '/Sources/'
excluded: '/(Tests|Examples)/'
regex: '(?<=\.)\s*currentStandard(?:Output|Error)\b'
match_kinds:
- identifier
message: "Use .xtoolOutput() or .xtoolError() instead of .currentStandardOutput or .currentStandardError, respectively."
severity: error
spaces_not_tabs:
name: "Spaces not Tabs"
regex: ^\t
Expand All @@ -73,3 +82,4 @@ included:
- macOS/XToolMac
excluded:
- Sources/DeveloperAPI/Generated
- Sources/XPIF
1 change: 1 addition & 0 deletions Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ RUN apt-get update \
curl libcurl4-openssl-dev \
zip unzip \
liblzma-dev zlib1g-dev \
libsqlite3-dev libncurses-dev \
&& rm -rf /var/lib/apt/lists/*


Expand Down
22 changes: 22 additions & 0 deletions Examples/DemoAudioUnit/Package.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
// swift-tools-version: 6.0

import PackageDescription

let package = Package(
name: "DemoAudioUnit",
platforms: [
.iOS(.v17),
.macOS(.v14),
],
products: [
.library(name: "DemoAudioUnit", targets: ["DemoAudioUnit"]),
.library(name: "DemoAudioUnitExtension", targets: ["DemoAudioUnitExtension"]),
],
dependencies: [
.package(path: "../ExampleSupport"),
],
targets: [
.target(name: "DemoAudioUnit", dependencies: ["ExampleSupport"]),
.target(name: "DemoAudioUnitExtension"),
]
)
5 changes: 5 additions & 0 deletions Examples/DemoAudioUnit/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
# DemoAudioUnit

This is an end-to-end test of building and running an app with an app extension (AudioUnit Extension).

We verify that the app can communicate back and forth with the extension, and that they can use a shared app group.
78 changes: 78 additions & 0 deletions Examples/DemoAudioUnit/Sources/DemoAudioUnit/App.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,78 @@
import AudioToolbox
import AVFAudio
import ExampleSupport
import SwiftUI

@main struct DemoApp: App {
var body: some Scene {
WindowGroup {
Text("Testing Audio Unit extension...")
.padding()
.task {
let watchdog = Task { @MainActor in
try await Task.sleep(for: .seconds(15))
print("Test failed: Audio Unit extension timed out")
exit(1)
}
defer { watchdog.cancel() }
do {
try await Task { @concurrent in try await runTest() }.value
Example.pass()
} catch {
print("Test failed: \(error)")
exit(1)
}
}
}
}
}

private func runTest() async throws {
let description = AudioComponentDescription(
componentType: kAudioUnitType_Generator,
componentSubType: 0x70696E67, // ping
componentManufacturer: 0x58746F6F, // Xtoo
componentFlags: 0,
componentFlagsMask: 0
)
let audioUnit = try await AUAudioUnit.instantiate(with: description, options: .loadOutOfProcess)
let channel = audioUnit.messageChannel(for: "xtool.test")
guard let reply = channel.callAudioUnit?(["message": "ping"]),
reply["message"] as? String == "pong" else {
throw StringError("Audio Unit extension did not reply with pong")
}
guard let pid = reply["pid"] as? Int,
pid > 0, pid != Int(ProcessInfo.processInfo.processIdentifier) else {
throw StringError("Audio Unit did not execute in a separate process")
}
print("Audio Unit extension replied pong from PID \(pid)")
try testAppGroup(channel: channel)
}

private func testAppGroup(channel: any AUMessageChannel) throws {
// xtool prefixes both the bundle ID and App Group when signing for a device.
let groupID = "group.\(Bundle.main.bundleIdentifier!).data"
guard FileManager.default.containerURL(forSecurityApplicationGroupIdentifier: groupID) != nil,
let defaults = UserDefaults(suiteName: groupID) else {
throw StringError("Host could not access the App Group")
}
// fresh keys ensure a previous run cannot satisfy this test.
let token = UUID().uuidString
let requestKey = "request.\(token)"
let responseKey = "response.\(token)"
defer {
defaults.removeObject(forKey: requestKey)
defaults.removeObject(forKey: responseKey)
}
defaults.set("ping", forKey: requestKey)
let reply = channel.callAudioUnit?([
"message": "test-app-group", "groupID": groupID, "token": token,
])
guard reply?["message"] as? String == "ok" else {
throw StringError("App Group extension test failed: \(reply, default: "nil")")
}
guard defaults.string(forKey: responseKey) == "pong" else {
throw StringError("Host did not read pong from App Group defaults")
}
print("App Group defaults exchanged ping/pong between host and extension")
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,75 @@
import AudioToolbox
import AVFAudio
import Foundation

@objc(DemoAudioUnitFactory)
final class AudioUnitFactory: NSObject, AUAudioUnitFactory {
func beginRequest(with context: NSExtensionContext) {}

func createAudioUnit(with description: AudioComponentDescription) throws -> AUAudioUnit {
try DemoAudioUnit(componentDescription: description)
}
}

private final class DemoAudioUnit: AUAudioUnit {
private var outputs: AUAudioUnitBusArray!

override init(
componentDescription: AudioComponentDescription,
options: AudioComponentInstantiationOptions = []
) throws {
try super.init(componentDescription: componentDescription, options: options)
let format = AVAudioFormat(standardFormatWithSampleRate: 44_100, channels: 2)!
let bus = try AUAudioUnitBus(format: format)
self.outputs = AUAudioUnitBusArray(audioUnit: self, busType: .output, busses: [bus])
}

override var outputBusses: AUAudioUnitBusArray { outputs }

// A silent generator keeps this a valid Audio Unit, although the execution
// test only instantiates it and exchanges messages; it never starts audio.
override var internalRenderBlock: AUInternalRenderBlock {
{ _, _, _, _, output, _, _ in
for buffer in UnsafeMutableAudioBufferListPointer(output) {
if let data = buffer.mData {
memset(data, 0, Int(buffer.mDataByteSize))
}
}
return noErr
}
}

override func messageChannel(for channelName: String) -> any AUMessageChannel {
TestMessageChannel()
}
}

private final class TestMessageChannel: NSObject, AUMessageChannel {
// AudioToolbox installs this callback when connecting the remote channel,
// even though this test only sends requests from the host.
var callHostBlock: CallHostBlock?

func callAudioUnit(_ message: [AnyHashable: Any]) -> [AnyHashable: Any] {
if message["message"] as? String == "test-app-group" {
return ["message": testAppGroup(message)]
}
return [
"message": message["message"] as? String == "ping" ? "pong" : "unknown message",
"pid": Int(ProcessInfo.processInfo.processIdentifier),
]
}

private func testAppGroup(_ message: [AnyHashable: Any]) -> String {
guard let groupID = message["groupID"] as? String,
let token = message["token"] as? String,
FileManager.default.containerURL(forSecurityApplicationGroupIdentifier: groupID) != nil,
let defaults = UserDefaults(suiteName: groupID) else {
return "Extension could not access the App Group"
}
guard defaults.string(forKey: "request.\(token)") == "ping" else {
return "Extension did not read ping from App Group defaults"
}
defaults.set("pong", forKey: "response.\(token)")
return "ok"
}
}
12 changes: 12 additions & 0 deletions Examples/DemoAudioUnit/Support/DemoAudioUnit.entitlements
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
<plist version="1.0">
<dict>
<key>com.apple.security.application-groups</key>
<array>
<string>group.sh.xtool.examples.DemoAudioUnit.data</string>
</array>
<key>inter-app-audio</key>
<true/>
</dict>
</plist>
39 changes: 39 additions & 0 deletions Examples/DemoAudioUnit/Support/DemoAudioUnitExtension-Info.plist
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
<plist version="1.0">
<dict>
<key>NSExtension</key>
<dict>
<key>NSExtensionPointIdentifier</key>
<string>com.apple.AudioUnit</string>
<key>NSExtensionPrincipalClass</key>
<string>DemoAudioUnitFactory</string>
<key>NSExtensionAttributes</key>
<dict>
<key>AudioComponents</key>
<array>
<dict>
<key>name</key>
<string>xtool: DemoAudioUnit</string>
<key>description</key>
<string>xtool extension execution test</string>
<key>manufacturer</key>
<string>Xtoo</string>
<key>type</key>
<string>augn</string>
<key>subtype</key>
<string>ping</string>
<key>version</key>
<integer>1</integer>
<key>sandboxSafe</key>
<true/>
<key>tags</key>
<array>
<string>Generator</string>
</array>
</dict>
</array>
</dict>
</dict>
</dict>
</plist>
10 changes: 10 additions & 0 deletions Examples/DemoAudioUnit/Support/DemoAudioUnitExtension.entitlements
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
<plist version="1.0">
<dict>
<key>com.apple.security.application-groups</key>
<array>
<string>group.sh.xtool.examples.DemoAudioUnit.data</string>
</array>
</dict>
</plist>
8 changes: 8 additions & 0 deletions Examples/DemoAudioUnit/xtool.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
version: 1
bundleID: sh.xtool.examples.DemoAudioUnit
product: DemoAudioUnit
entitlementsPath: Support/DemoAudioUnit.entitlements
extensions:
- product: DemoAudioUnitExtension
infoPath: Support/DemoAudioUnitExtension-Info.plist
entitlementsPath: Support/DemoAudioUnitExtension.entitlements
15 changes: 15 additions & 0 deletions Examples/DemoBinary/Package.resolved

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

30 changes: 30 additions & 0 deletions Examples/DemoBinary/Package.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
// swift-tools-version: 6.0

import PackageDescription

let package = Package(
name: "DemoBinary",
platforms: [
.iOS(.v17),
.macOS(.v14),
],
products: [
.library(
name: "DemoBinary",
targets: ["DemoBinary"]
),
],
dependencies: [
.package(path: "../ExampleSupport"),
.package(url: "https://github.com/krzyzanowskim/OpenSSL", from: "3.0.0"),
],
targets: [
.target(
name: "DemoBinary",
dependencies: [
"ExampleSupport",
"OpenSSL",
]
),
]
)
5 changes: 5 additions & 0 deletions Examples/DemoBinary/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
# DemoBinary

This test validates that we can link and embed a binary xcframework, and then use it at runtime.

We perform the test with [krzyzanowskim/OpenSSL](https://github.com/krzyzanowskim/OpenSSL).
36 changes: 36 additions & 0 deletions Examples/DemoBinary/Sources/DemoBinary/App.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
import ExampleSupport
import SwiftUI
import OpenSSL

@main struct DemoApp: App {
public var body: some Scene {
WindowGroup {
ContentView()
}
}
}

private struct ContentView: View {
@State var counter = 0

var body: some View {
Text("Hello, world!")
.padding()
.task {
do {
try await verify()
Example.pass()
} catch {
print("Test failed: \(error)")
exit(1)
}
}
}
}

private func verify() async throws {
let shaSize = EVP_MD_get_size(EVP_sha256()) * 8
guard shaSize == 256 else {
throw StringError("OpenSSL sanity check failed: it claims that SHA-256 is \(shaSize) bits (!= 256)")
}
}
2 changes: 2 additions & 0 deletions Examples/DemoBinary/xtool.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
version: 1
bundleID: sh.xtool.examples.DemoBinary
Loading
Loading