Skip to content

Commit 5d2152a

Browse files
committed
fix: support SPRUX_METALLIB_PATH env var for runtime metallib discovery
The compiled-in SPRUX_METAL_LIBRARY_PATH points to the build directory, which doesn't exist when Sprux is embedded via FetchContent in pip packages. Add SPRUX_METALLIB_PATH env var override (highest priority) so embedders can point to the installed metallib location. Priority: env var > compiled-in path > default library. Co-developed-by: Claude Code v2.1.81 (claude-opus-4-6)
1 parent 7b5533d commit 5d2152a

1 file changed

Lines changed: 24 additions & 11 deletions

File tree

sprux/sprux/MetalDefs.mm

Lines changed: 24 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -43,20 +43,33 @@
4343
asyncQueue = [device newCommandQueue];
4444
mtlCHECK(asyncQueue != nil, "Failed to create Metal async command queue");
4545

46-
// Load the compiled shader library
47-
#ifdef SPRUX_METAL_LIBRARY_PATH
46+
// Load the compiled shader library.
47+
// Priority: SPRUX_METALLIB_PATH env var > compiled-in path > default library.
4848
NSError* error = nil;
49-
NSString* libraryPath = @SPRUX_METAL_LIBRARY_PATH;
50-
NSURL* libraryURL = [NSURL fileURLWithPath:libraryPath];
51-
library = [device newLibraryWithURL:libraryURL error:&error];
52-
if (error != nil) {
53-
NSLog(@"Failed to load Metal library from %@: %@", libraryPath, error);
54-
// Try loading default library as fallback
55-
library = [device newDefaultLibrary];
49+
const char* envPath = getenv("SPRUX_METALLIB_PATH");
50+
if (envPath) {
51+
NSString* libraryPath = [NSString stringWithUTF8String:envPath];
52+
NSURL* libraryURL = [NSURL fileURLWithPath:libraryPath];
53+
library = [device newLibraryWithURL:libraryURL error:&error];
54+
if (error != nil) {
55+
NSLog(@"Failed to load Metal library from env SPRUX_METALLIB_PATH=%@: %@",
56+
libraryPath, error);
57+
}
58+
}
59+
#ifdef SPRUX_METAL_LIBRARY_PATH
60+
if (library == nil) {
61+
error = nil;
62+
NSString* libraryPath = @SPRUX_METAL_LIBRARY_PATH;
63+
NSURL* libraryURL = [NSURL fileURLWithPath:libraryPath];
64+
library = [device newLibraryWithURL:libraryURL error:&error];
65+
if (error != nil) {
66+
NSLog(@"Failed to load Metal library from %@: %@", libraryPath, error);
67+
}
5668
}
57-
#else
58-
library = [device newDefaultLibrary];
5969
#endif
70+
if (library == nil) {
71+
library = [device newDefaultLibrary];
72+
}
6073
mtlCHECK(library != nil, "Failed to load Metal shader library");
6174

6275
NSLog(@"Metal initialized: %@", device.name);

0 commit comments

Comments
 (0)