@@ -509,30 +509,31 @@ @implementation GGMLMetalClass
509509        NSString  * path_lib = [bundle pathForResource: @" default" ofType: @" metallib" 
510510        if  (path_lib == nil ) {
511511            //  Try to find the resource in the directory where the current binary located.
512-             NSString  *currentBinary = [[NSProcessInfo  processInfo ] arguments ][0 ];
513-             NSString  *binDir = [currentBinary stringByDeletingLastPathComponent ];
514-             NSString  *defaultMetalibPath = [NSString  pathWithComponents: @[binDir, @" default.metallib" 
515-             NSDictionary  *atts = [[NSFileManager  defaultManager ] attributesOfItemAtPath: defaultMetalibPath error: &error];
516-             if  (error) {
517-                 GGML_METAL_LOG_ERROR (" %s : error: %s \n " description ] UTF8String ]);
518-                 return  NULL ;
519-             }
520-             if  (!atts) {
521-                 GGML_METAL_LOG_ERROR (" %s : error: src path attribute is NULL\n " 
522-                 return  NULL ;
523-             }
524-             if  (atts[NSFileType ] == NSFileTypeSymbolicLink ) {
525-                 //  Optionally, if this is a symlink, try to resolve it.
526-                 defaultMetalibPath = [[NSFileManager  defaultManager ] destinationOfSymbolicLinkAtPath: defaultMetalibPath error: &error];
527-                 if  (error) {
528-                     GGML_METAL_LOG_ERROR (" %s : error: %s \n " description ] UTF8String ]);
529-                     return  NULL ;
530-                 }
531-                 if  (defaultMetalibPath && [defaultMetalibPath length ] > 0  && ![[defaultMetalibPath substringToIndex: 1 ] isEqualToString: @" /" 
532-                     defaultMetalibPath = [NSString  pathWithComponents: @[binDir, defaultMetalibPath]];
512+             NSString  *current_binary = [[NSProcessInfo  processInfo ] arguments ][0 ];
513+             NSString  *bin_dir = [current_binary stringByDeletingLastPathComponent ];
514+             NSString  *default_metallib_path = [NSString  pathWithComponents: @[bin_dir, @" default.metallib" 
515+             if  ([[NSFileManager  defaultManager ] isReadableFileAtPath: default_metallib_path]) {
516+                 GGML_LOG_INFO (" %s : found '%s '\n " UTF8String ]);
517+                 NSDictionary  *atts = [[NSFileManager  defaultManager ] attributesOfItemAtPath: default_metallib_path error: &error];
518+                 if  (atts && atts[NSFileType ] == NSFileTypeSymbolicLink ) {
519+                     //  Optionally, if this is a symlink, try to resolve it.
520+                     default_metallib_path = [[NSFileManager  defaultManager ] destinationOfSymbolicLinkAtPath: default_metallib_path error: &error];
521+                     if  (default_metallib_path && [default_metallib_path length ] > 0  && ![[default_metallib_path substringToIndex: 1 ] isEqualToString: @" /" 
522+                         //  It is a relative path, adding the binary directory as directory prefix.
523+                         default_metallib_path = [NSString  pathWithComponents: @[bin_dir, default_metallib_path]];
524+                     }
525+                     if  (!default_metallib_path || ![[NSFileManager  defaultManager ] isReadableFileAtPath: default_metallib_path]) {
526+                         //  Link to the resource could not be resolved.
527+                         default_metallib_path = nil ;
528+                     } else  {
529+                         GGML_LOG_INFO (" %s : symlink resolved '%s '\n " UTF8String ]);
530+                     }
533531                }
532+             } else  {
533+                 //  The resource couldn't be found in the binary's directory.
534+                 default_metallib_path = nil ;
534535            }
535-             path_lib = defaultMetalibPath ;
536+             path_lib = default_metallib_path ;
536537        }
537538
538539        if  (try_metallib && path_lib != nil ) {
0 commit comments