From d6325faacab4dd6a9c1208c3b9fa702601124ae7 Mon Sep 17 00:00:00 2001 From: Scott Roy Date: Tue, 25 Nov 2025 14:23:40 -0800 Subject: [PATCH] Make ExecuTorch CoreML asset/trash/databse folders configurable. Summary: The ExecuTorch CoreML backend uses various folder locations to store compiled assets, to trash old assets, and to store a databse. With this diff, all of these are configurable with the following compile-time flags: * EXECUTORCH_COREML_ASSETS_DIRECTORY_PATH (default value is [cache]/executorchcoreml) * EXECUTORCH_COREML_TRASH_DIRECTORY_PATH (default value is [tmp]/executorchcoreml) * EXECUTORCH_COREML_DATABASE_DIRECTORY_PATH (default value is [app support]/executorchcoreml) Differential Revision: D85064722 --- .../runtime/delegate/ETCoreMLStrings.mm | 65 +++++++++++-------- 1 file changed, 38 insertions(+), 27 deletions(-) diff --git a/backends/apple/coreml/runtime/delegate/ETCoreMLStrings.mm b/backends/apple/coreml/runtime/delegate/ETCoreMLStrings.mm index fb66f7b7c03..232e3297b76 100644 --- a/backends/apple/coreml/runtime/delegate/ETCoreMLStrings.mm +++ b/backends/apple/coreml/runtime/delegate/ETCoreMLStrings.mm @@ -101,39 +101,50 @@ + (NSString *)debugSymbolToHandlesKeyName { } + (nullable NSString *)assetsDirectoryPath { - static dispatch_once_t onceToken; - static NSString *result = nil; - dispatch_once(&onceToken, ^{ - NSArray *paths = NSSearchPathForDirectoriesInDomains(NSCachesDirectory, NSUserDomainMask, YES); - if (paths.count > 0) { - result = [paths.lastObject stringByAppendingPathComponent:self.productName]; - } - }); - - return result; + #if defined(EXECUTORCH_COREML_ASSETS_DIRECTORY_PATH) + return @(EXECUTORCH_COREML_ASSETS_DIRECTORY_PATH); + #else + static dispatch_once_t onceToken; + static NSString *result = nil; + dispatch_once(&onceToken, ^{ + NSArray *paths = NSSearchPathForDirectoriesInDomains(NSCachesDirectory, NSUserDomainMask, YES); + if (paths.count > 0) { + result = [paths.lastObject stringByAppendingPathComponent:self.productName]; + } + }); + + return result; + #endif } + (nullable NSString *)trashDirectoryPath { - static dispatch_once_t onceToken; - static NSString *result = nil; - dispatch_once(&onceToken, ^{ - result = [NSTemporaryDirectory() stringByAppendingPathComponent:self.productName]; - }); - - return result; + #if defined(EXECUTORCH_COREML_TRASH_DIRECTORY_PATH) + return @(EXECUTORCH_COREML_TRASH_DIRECTORY_PATH); + #else + static dispatch_once_t onceToken; + static NSString *result = nil; + dispatch_once(&onceToken, ^{ + result = [NSTemporaryDirectory() stringByAppendingPathComponent:self.productName]; + }); + + return result; + #endif } + (nullable NSString *)databaseDirectoryPath { - static dispatch_once_t onceToken; - static NSString *result = nil; - dispatch_once(&onceToken, ^{ - NSArray *paths = NSSearchPathForDirectoriesInDomains(NSApplicationSupportDirectory, NSUserDomainMask, YES); - if (paths.count > 0) { - result = [paths.lastObject stringByAppendingPathComponent:self.productName]; - } - }); - - return result; + #if defined(EXECUTORCH_COREML_DATABASE_DIRECTORY_PATH) + return @(EXECUTORCH_COREML_DATABASE_DIRECTORY_PATH); + #else + static dispatch_once_t onceToken; + static NSString *result = nil; + dispatch_once(&onceToken, ^{ + NSArray *paths = NSSearchPathForDirectoriesInDomains(NSApplicationSupportDirectory, NSUserDomainMask, YES); + if (paths.count > 0) { + result = [paths.lastObject stringByAppendingPathComponent:self.productName]; + } + }); + return result; + #endif }