rules_apple icon indicating copy to clipboard operation
rules_apple copied to clipboard

Extending apple_metal_library rule

Open mxpv opened this issue 1 year ago • 0 comments

I've been playing with Bazel and apple_metal_library rule for a small project to compile MSL shaders. While it serves its purpose, I found it pretty challenging to generate symbols companion file for the metal library. These are pretty useful during development stages. So I'm curious if there are any suggestions or room for a PR :)

Internally apple_metal_library works in two phases:

  • generate air files (like xcrun -sdk macosx metal -o Shadow.ir -c Shadow.metal {COPTS})
  • and then combining set of air files into a library (like xcrun -sdk macosx metallib -o LightsAndShadow.metallib Lights.metalar Shadow.ir)

So to support debug symbols generation with multiple commands, we need:

  • Add -frecord-sources COPTS in step 1.
  • Add -frecord-sources to step 2 (so add anothe COPTS for linking stage?)
  • Separate the sources from the library and create a companion symbol file by running the metal-dsymutil command. (like % xcrun -sdk macosx metal-dsymutil -flat -remove-source LightsAndShadow.metallib)

This is quite complex.

Slightly easier option would be to simplify current implementation and reduce it to just one command:

For current scenario, the command would look like:

% xcrun -sdk macosx metal -o default.metallib Shadow.metal PointLights.metal DirectionalLight.metal

With debug symbols:

% xcrun -sdk macosx metal -o default.metallib -frecord-sources=flat Shadow.metal PointLights.metal DirectionalLight.metal

Which can be satisfied via existing COPTS param.

apple_metal_library(
    name = "metal_lib",
    copts = ["-w", "-frecord-sources=flat"], # or param symbols = True?
    hdrs = ["shared.h"],
    srcs = ["shader1.metal", "shader2.metal"],
    outs = ["test.metallib", "test.metallibsym"] 
)

cc: @aaronsky

mxpv avatar Jul 18 '24 16:07 mxpv