swiftlang / swift-package-manager

The Package Manager for the Swift Programming Language
Apache License 2.0
9.65k stars 1.31k forks source link

Metal files *not* silently compiled #7716

Open rgov opened 4 days ago

rgov commented 4 days ago

Is it reproducible with SwiftPM command-line tools: swift build, swift test, swift package etc?

Description

5822 discusses that "[r]esources of known types are automatically handled, as per the resources proposal", such that "all .metal files in a package are compiled into a default.metallib file".

There's also a Apple Developer Forums post from 2020 that claims this behavior exists.

However, I do not see this occurring with my project. (Admittedly, this is my first time using SwiftPM.)

Expected behavior

The add.metal file is processed and default.metallib is produced, such that it can be loaded by MTLDevice.makeDefaultLibrary(bundle:).

Actual behavior

Library loading fails because default.metallib does not exist.

The add.metal file is simply copied to .build/arm64-apple-macosx/debug/example_tool.bundle/add.metal.

Steps to reproduce

Here's Package.swift:

let package = Package(
    name: "example",

    platforms: [
        .macOS(.v10_14),
    ],

    targets: [
        .executableTarget(
            name: "tool",
            resources: [
                .process("Kernels/add.metal"),
            ]
        ),
    ]
)

And Sources/main.swift could look something like:

import Foundation
import Metal

let device = MTLCopyAllDevices().first!
let library = try! device.makeDefaultLibrary(bundle: Bundle.module)

The command swift run should succeed.

Note

I am not able to locate any documentation about what process rules exist, and whether the behavior of compiling Metal is actually an officially-supported feature.

Swift Package Manager version/commit hash

Swift Package Manager - Swift 5.10.0-dev

Swift & OS version

swift-driver version: 1.90.11.1 Apple Swift version 5.10 (swiftlang-5.10.0.13 clang-1500.3.9.4)
Target: arm64-apple-macosx14.0
Darwin Midnight-One.local 23.5.0 Darwin Kernel Version 23.5.0: Wed May  1 20:12:58 PDT 2024; root:xnu-10063.121.3~5/RELEASE_ARM64_T6000 arm64
rgov commented 4 days ago

The default.metallib is produced only if I use Xcode to build the package, but not SwiftPM from the command line.

It's unclear to me as a novice user why these should behave differently, and what the full matrix of supported features between a "pure" SwiftPM project vs an Xcode project is.