From 03297c1a7c3cb364609665d12b81ddcf2d6cd6a1 Mon Sep 17 00:00:00 2001 From: Marc Date: Tue, 15 Aug 2023 16:35:06 +0200 Subject: [PATCH] simply code and allow to use the directory containing the file as a valid value as intented in the first place --- ggml-metal.m | 38 ++++++++++++++++++++++++-------------- 1 file changed, 24 insertions(+), 14 deletions(-) diff --git a/ggml-metal.m b/ggml-metal.m index b043d7424..7962e8dfd 100644 --- a/ggml-metal.m +++ b/ggml-metal.m @@ -1,4 +1,5 @@ #import "ggml-metal.h" +#include #import "ggml.h" @@ -132,27 +133,36 @@ struct ggml_metal_context * ggml_metal_init(int n_cb) { //NSString * path = [[NSBundle mainBundle] pathForResource:@"../../examples/metal/metal" ofType:@"metal"]; const char* v = getenv("GGML_METAL_PATH"); NSString * path; + BOOL doLegacyCode = false; if (v) { path = [NSString stringWithCString: v encoding: NSUTF8StringEncoding]; NSFileManager *fileManager = [NSFileManager defaultManager]; - if ([fileManager fileExistsAtPath:path]) { - fprintf(stderr, "%s: loading '%s'\n", __func__, [path UTF8String]); - } else { - path = [path stringByAppendingString: @"/ggml-metal.metal"]; - if ([fileManager fileExistsAtPath:path]) { - fprintf(stderr, "%s: loading '%s'\n", __func__, [path UTF8String]); + BOOL isDir; + if ([fileManager fileExistsAtPath:path isDirectory: &isDir]) { + if (isDir) { + path = [path stringByAppendingString: @"/ggml-metal.metal"]; + if ([fileManager fileExistsAtPath:path]) { + fprintf(stderr, "%s: loading '%s'\n", __func__, [path UTF8String]); + } else { + // Use original code + doLegacyCode = true; + } } else { - // Use original code - NSBundle * bundle = [NSBundle bundleForClass:[GGMLMetalClass class]]; - path = [bundle pathForResource:@"ggml-metal" ofType:@"metal"]; - fprintf(stderr, "%s: loading '%s'\n", __func__, [path UTF8String]); + fprintf(stderr, "%s: loading '%s'\n", __func__, [path UTF8String]); } + } else { + // Use original code + doLegacyCode = true; } - } else { - NSBundle * bundle = [NSBundle bundleForClass:[GGMLMetalClass class]]; - path = [bundle pathForResource:@"ggml-metal" ofType:@"metal"]; - fprintf(stderr, "%s: loading '%s'\n", __func__, [path UTF8String]); + // Use original code + doLegacyCode = true; + } + + if (doLegacyCode) { + NSBundle * bundle = [NSBundle bundleForClass:[GGMLMetalClass class]]; + path = [bundle pathForResource:@"ggml-metal" ofType:@"metal"]; + fprintf(stderr, "%s: loading '%s'\n", __func__, [path UTF8String]); } NSString * src = [NSString stringWithContentsOfFile:path encoding:NSUTF8StringEncoding error:&error];