Skip to content
Merged
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: 4 additions & 6 deletions .github/workflows/swift.yml
Original file line number Diff line number Diff line change
Expand Up @@ -12,10 +12,8 @@ jobs:

steps:
- uses: actions/checkout@v4
- uses: swift-actions/setup-swift@v1
- uses: swift-actions/setup-swift@v2
with:
swift-version: "5.9.2"
- name: Build
run: swift build -v
- name: Run tests
run: swift test -v
swift-version: "6.0.3"
- name: Build and Test
run: ./scripts/build-and-test-ios.sh
27 changes: 18 additions & 9 deletions .github/workflows/wasm.yml
Original file line number Diff line number Diff line change
Expand Up @@ -6,14 +6,23 @@ on:

jobs:
build:
name: Build on WebAssembly
runs-on: ubuntu-latest
container:
image: swift:6.0.2-focal
name: Build and Test on WebAssembly
runs-on: ubuntu-24.04
steps:
- uses: actions/checkout@v4
- run: swift sdk install https://github.com/swiftwasm/swift/releases/download/swift-wasm-6.0.2-RELEASE/swift-wasm-6.0.2-RELEASE-wasm32-unknown-wasi.artifactbundle.zip --checksum 6ffedb055cb9956395d9f435d03d53ebe9f6a8d45106b979d1b7f53358e1dcb4
- name: Build
run: swift build --swift-sdk wasm32-unknown-wasi
- name: Test
run: swift test --swift-sdk wasm32-unknown-wasi

- name: Install Swift
uses: swift-actions/setup-swift@v2
with:
swift-version: "6.0.3"

# Wasmtime is required because `swift test` doesn't work for WebAssembly targets.
# For WASM, we must build tests separately and run them with a WASM runtime.
# See: https://book.swiftwasm.org/getting-started/testing.html
- name: Install Wasmtime
run: |
curl https://wasmtime.dev/install.sh -sSf | bash
echo "$HOME/.wasmtime/bin" >> $GITHUB_PATH

- name: Build and Test
run: ./scripts/build-and-test-wasm.sh
33 changes: 20 additions & 13 deletions Package.resolved

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

17 changes: 7 additions & 10 deletions Package.swift
Original file line number Diff line number Diff line change
@@ -1,38 +1,35 @@
// swift-tools-version:5.1
// swift-tools-version:5.9
// The swift-tools-version declares the minimum version of Swift required to build this package.

import PackageDescription

let package = Package(
name: "Matft",
products: [
// Products define the executables and libraries produced by a package, and make them visible to other packages.
.library(
name: "Matft",
targets: ["Matft"]),
//.library(name: "ReleaseTest", targets: ["Matft "])
],
dependencies: [
// Dependencies declare other packages that this package depends on.
// .package(url: /* package url */, from: "1.0.0"),
.package(url: "https://github.com/apple/swift-collections", from: "1.0.0"),

.package(url: "https://github.com/GoodNotes/CLAPACK", branch: "eigen-support"),
],
targets: [
// Targets are the basic building blocks of a package. A target can define a module or a test suite.
// Targets can depend on other targets in this package, and on products in packages which this package depends on.
.target(
name: "pocketFFT"
),
.target(
name: "Matft",
dependencies: ["Collections", "pocketFFT"]),
dependencies: [
.product(name: "Collections", package: "swift-collections"),
"pocketFFT",
.product(name: "CLAPACK", package: "CLAPACK", condition: .when(platforms: [.wasi])),
]),
.testTarget(
name: "MatftTests",
dependencies: ["Matft"]),
.testTarget(
name: "PerformanceTests",
dependencies: ["Matft"]),
]
//cxxLanguageStandard: .gnucxx1z
)
40 changes: 40 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,9 @@ INFO: Support Complex!!
+ [SwiftPM](#swiftpm)
+ [Carthage](#carthage)
+ [CocoaPods](#cocoapods)
* [Build Scripts](#-build-scripts)
+ [iOS/macOS Build & Test](#-iosmacos-build--test)
+ [WebAssembly Build & Test](#-webassembly-build--test)
* [Contact](#contact)

<strike>
Expand Down Expand Up @@ -858,6 +861,43 @@ So, a pull request is very welcome!!
pod install
```

## 🛠️ Build Scripts

Matft provides convenient bash scripts for building and testing the project locally.

### 🍎 iOS/macOS Build & Test

To build and test Matft for iOS/macOS platforms:

```bash
./scripts/build-and-test-ios.sh
```

This script will:
- Build the project using `swift build`
- Run all tests using `swift test`

### 🌐 WebAssembly Build & Test

To build and test Matft for WebAssembly:

```bash
./scripts/build-and-test-wasm.sh
```

This script will:
- 📦 Check and install the Swift WASM SDK if needed
- 🔧 Check and install wasmtime runtime if needed
- 🔨 Build the project for WebAssembly
- 🧪 Build and run tests using wasmtime

**Note:** The WASM script automatically handles SDK and runtime installation, so you can run it on a fresh machine without any prior setup!

### 📋 Requirements

- **iOS/macOS:** Swift 6.0.3 or later
- **WebAssembly:** Swift 6.0.3 or later (SDK will be automatically installed)

## Contact

Feel free to ask this project or anything via <junnosuke.kado.git@gmail.com>
22 changes: 22 additions & 0 deletions scripts/build-and-test-ios.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
#!/bin/bash
set -e

echo "🍎 =========================================="
echo "🍎 Building and Testing Matft for iOS/macOS"
echo "🍎 =========================================="
echo ""

echo "🔨 Building project..."
swift build -v

echo ""
echo "✅ Build completed successfully!"
echo ""

echo "🧪 Running tests..."
swift test -v

echo ""
echo "🎉 =========================================="
echo "🎉 All tests passed successfully!"
echo "🎉 =========================================="
164 changes: 164 additions & 0 deletions scripts/build-and-test-wasm.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,164 @@
#!/bin/bash
set -e

echo "🌐 =========================================="
echo "🌐 Building and Testing Matft for WebAssembly"
echo "🌐 =========================================="
echo ""

# Configuration for CI (GitHub Actions)
SWIFT_WASM_SDK_URL="https://github.com/swiftwasm/swift/releases/download/swift-wasm-6.0.3-RELEASE/swift-wasm-6.0.3-RELEASE-wasm32-unknown-wasi.artifactbundle.zip"
SWIFT_WASM_SDK_CHECKSUM="31d3585b06dd92de390bacc18527801480163188cd7473f492956b5e213a8618"
CI_SDK_NAME="wasm32-unknown-wasi"

# Swift command to use (may be overridden if toolchain detected)
SWIFT_CMD="swift"

# Detect development snapshot toolchain with WASM support
detect_toolchain() {
echo "🔍 Detecting Swift toolchain..."

# Check for development snapshot toolchains in the standard location
TOOLCHAIN_DIR="$HOME/Library/Developer/Toolchains"

if [ -d "$TOOLCHAIN_DIR" ]; then
# Find the most recent development snapshot toolchain
TOOLCHAIN=$(ls -1 "$TOOLCHAIN_DIR" 2>/dev/null | grep -E "swift-DEVELOPMENT-SNAPSHOT.*\.xctoolchain" | sort -r | head -1)

if [ -n "$TOOLCHAIN" ]; then
TOOLCHAIN_SWIFT="$TOOLCHAIN_DIR/$TOOLCHAIN/usr/bin/swift"
if [ -x "$TOOLCHAIN_SWIFT" ]; then
SWIFT_CMD="$TOOLCHAIN_SWIFT"
echo "✅ Found development toolchain: $TOOLCHAIN"
return 0
fi
fi
fi

echo "ℹ️ Using system Swift"
return 0
}

# Detect available WASM SDK
detect_wasm_sdk() {
echo "📦 Detecting WASM SDK..."

# Get list of installed SDKs
INSTALLED_SDKS=$($SWIFT_CMD sdk list 2>/dev/null || echo "")

# First, check if the CI SDK is available (for GitHub Actions)
if echo "$INSTALLED_SDKS" | grep -q "^${CI_SDK_NAME}$"; then
SWIFT_SDK_NAME="$CI_SDK_NAME"
echo "✅ Found SDK: $SWIFT_SDK_NAME"
return 0
fi

# For local development with development toolchains, prefer matching SDKs
# Extract toolchain version if using a development toolchain
if echo "$SWIFT_CMD" | grep -q "DEVELOPMENT-SNAPSHOT"; then
TOOLCHAIN_VERSION=$(echo "$SWIFT_CMD" | grep -oE "DEVELOPMENT-SNAPSHOT-[0-9]{4}-[0-9]{2}-[0-9]{2}-a")
if [ -n "$TOOLCHAIN_VERSION" ]; then
SDK_MATCH=$(echo "$INSTALLED_SDKS" | grep "$TOOLCHAIN_VERSION" | grep -v "embedded" | head -1)
if [ -n "$SDK_MATCH" ]; then
SWIFT_SDK_NAME="$SDK_MATCH"
echo "✅ Found matching SDK: $SWIFT_SDK_NAME"
return 0
fi
fi
fi

# Try to find any WASM SDK (prefer development snapshots with threads for local dev)
for pattern in "DEVELOPMENT-SNAPSHOT.*wasm32-unknown-wasip1-threads$" "swift-.*-RELEASE_wasm$" "wasm32-unknown-wasi" "wasm"; do
SDK_MATCH=$(echo "$INSTALLED_SDKS" | grep -E "$pattern" | grep -v "embedded" | head -1)
if [ -n "$SDK_MATCH" ]; then
SWIFT_SDK_NAME="$SDK_MATCH"
echo "✅ Found SDK: $SWIFT_SDK_NAME"
return 0
fi
done

return 1
}

# Install Swift WASM SDK if not found
install_swift_wasm_sdk() {
if detect_wasm_sdk; then
echo ""
return 0
fi

echo "⬇️ No WASM SDK found. Installing Swift WASM SDK..."
$SWIFT_CMD sdk install "$SWIFT_WASM_SDK_URL" --checksum "$SWIFT_WASM_SDK_CHECKSUM"
SWIFT_SDK_NAME="$CI_SDK_NAME"
echo "✅ Swift WASM SDK installed successfully"
echo ""
}

# Check if wasmtime is installed
install_wasmtime() {
echo "🔧 Checking wasmtime..."
if command -v wasmtime &> /dev/null; then
echo "✅ wasmtime already installed: $(wasmtime --version)"
else
echo "⬇️ Installing wasmtime..."
curl https://wasmtime.dev/install.sh -sSf | bash
export PATH="$HOME/.wasmtime/bin:$PATH"
echo "✅ wasmtime installed successfully"
fi
echo ""
}

# Build the project
build_project() {
echo "🔨 Building project for WebAssembly..."
echo " Swift: $SWIFT_CMD"
echo " SDK: $SWIFT_SDK_NAME"
$SWIFT_CMD build --swift-sdk "$SWIFT_SDK_NAME"
echo "✅ Build completed successfully!"
echo ""
}

# Build and run tests
run_tests() {
echo "🧪 Building tests for WebAssembly..."
$SWIFT_CMD build --build-tests --swift-sdk "$SWIFT_SDK_NAME"
echo "✅ Test build completed!"
echo ""

echo "🚀 Running tests with wasmtime..."
# Note: We use wasmtime instead of 'swift test' because swift test doesn't
# work directly for WebAssembly targets. For WASM, we must build tests
# separately and run them with a WASM runtime.
# See: https://book.swiftwasm.org/getting-started/testing.html

# Find the test binary in any wasm build directory
TEST_BINARY=$(find .build -path "*/debug/MatftPackageTests.wasm" -o -path "*/debug/MatftPackageTests.xctest" 2>/dev/null | grep -E "wasm|wasi" | head -1)

if [ -z "$TEST_BINARY" ]; then
echo "❌ Error: Could not find test binary"
echo " Searched in .build/*/debug/ for MatftPackageTests.wasm or .xctest"
exit 1
fi

echo "📍 Test binary: $TEST_BINARY"

# Determine wasmtime flags based on SDK type
WASMTIME_FLAGS="--dir ."
if echo "$SWIFT_SDK_NAME" | grep -q "threads"; then
WASMTIME_FLAGS="$WASMTIME_FLAGS --wasm threads=y --wasi threads=y"
fi

wasmtime run $WASMTIME_FLAGS "$TEST_BINARY"
echo ""
}

# Main execution
detect_toolchain
install_swift_wasm_sdk
install_wasmtime
build_project
run_tests

echo "🎉 =========================================="
echo "🎉 All WASM tests passed successfully!"
echo "🎉 =========================================="
Loading