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
3 changes: 3 additions & 0 deletions .github/workflows/pull_request.yml
Original file line number Diff line number Diff line change
Expand Up @@ -83,6 +83,9 @@ jobs:
shell: bash
run: xcodebuild -showComponent MetalToolchain

- name: Verify SwiftPM manifest CUDA containment
run: scripts/verify-spm-manifest.py

- name: Setup cmake
shell: sh
run: |
Expand Down
54 changes: 33 additions & 21 deletions Package.swift
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,35 @@

import PackageDescription

#if os(Linux)
let cudaBuildPlugins: [Target.PluginUsage] = [
.plugin(name: "CudaBuild")
]
let cudaPackageDependencies: [Package.Dependency] = [
.package(url: "https://github.com/apple/swift-argument-parser", from: "1.0.0")
]
let cudaTargets: [Target] = [
.executableTarget(
name: "encuda",
dependencies: [
.product(name: "ArgumentParser", package: "swift-argument-parser")
],
path: "Source/Encuda",
),
.plugin(
name: "CudaBuild",
capability: .buildTool(),
dependencies: [
.target(name: "encuda")
],
),
]
#else
let cudaBuildPlugins: [Target.PluginUsage] = []
let cudaPackageDependencies: [Package.Dependency] = []
let cudaTargets: [Target] = []
#endif

let noMetalCmlxExcludes = [
// Exclude Metal backend files, but keep no_metal.cpp for stubs
// "mlx/mlx/backend/metal/no_metal.cpp",
Expand Down Expand Up @@ -289,9 +318,7 @@ let cmlx = Target.target(
.define("MLX_VERSION", to: "\"0.31.1\""),
],
linkerSettings: linkerSettings,
plugins: [
.plugin(name: "CudaBuild")
],
plugins: cudaBuildPlugins,
)

let package = Package(
Expand All @@ -316,9 +343,8 @@ let package = Package(
],
dependencies: [
// for Complex type
.package(url: "https://github.com/apple/swift-numerics", from: "1.0.0"),
.package(url: "https://github.com/apple/swift-argument-parser", from: "1.0.0"),
],
.package(url: "https://github.com/apple/swift-numerics", from: "1.0.0")
] + cudaPackageDependencies,
targets: [
cmlx,
.testTarget(
Expand Down Expand Up @@ -414,21 +440,7 @@ let package = Package(
path: "Source/Examples",
sources: ["CustomFunctionExampleSimple.swift"]
),
.executableTarget(
name: "encuda",
dependencies: [
.product(name: "ArgumentParser", package: "swift-argument-parser")
],
path: "Source/Encuda",
),
.plugin(
name: "CudaBuild",
capability: .buildTool(),
dependencies: [
.target(name: "encuda")
],
),
],
] + cudaTargets,
cxxLanguageStandard: .gnucxx20
)

Expand Down
58 changes: 58 additions & 0 deletions scripts/verify-spm-manifest.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
#!/usr/bin/env python3

import json
import platform
import subprocess


def require(condition: bool, message: str) -> None:
if not condition:
raise SystemExit(message)


manifest = json.loads(
subprocess.run(
["swift", "package", "dump-package"],
check=True,
capture_output=True,
text=True,
).stdout
)

targets = {target["name"]: target for target in manifest["targets"]}
dependencies = {
dependency["sourceControl"][0]["identity"]
for dependency in manifest["dependencies"]
if "sourceControl" in dependency
}

cuda_targets = {"CudaBuild", "encuda"}
cuda_plugin_usages = targets["Cmlx"].get("pluginUsages") or []

if platform.system() == "Linux":
require(
cuda_targets <= targets.keys(), "Linux manifest is missing CUDA build targets"
)
require(
"swift-argument-parser" in dependencies,
"Linux manifest is missing the CUDA helper dependency",
)
require(
cuda_plugin_usages,
"Linux Cmlx target is missing the CUDA build plugin",
)
else:
require(
cuda_targets.isdisjoint(targets),
"non-Linux manifest unexpectedly contains CUDA build targets",
)
require(
"swift-argument-parser" not in dependencies,
"non-Linux manifest unexpectedly contains the CUDA helper dependency",
)
require(
not cuda_plugin_usages,
"non-Linux Cmlx target unexpectedly uses the CUDA build plugin",
)

print(f"SwiftPM manifest CUDA containment passed for {platform.system()}.")
Loading