// Where the Metal engine finds the files it reads at run time: the .metal kernel sources it compiles when it starts, // strata-quant-common.h (formerly ggml-common.h), and the default expert-profile / draft-vocabulary tables. // // A build used from its own source tree finds them through the paths compiled in (STRATA_*_DEFAULT). A component package // (strata_control/engine_component.py) is moved away from the tree that built it, so the engine also looks beside its // own executable first: // // /metal/kernels/*.metal the kernel sources (metal/kernels) // /metal/engine-msl/*.metal the engine's own kernels (metal/src/engine/msl) // /metal/strata-quant-common.h (a package of an earlier release has it as /metal/ggml-common.h: read as the second choice) // /metal/data/expert-profile.bin draft_vocab.bin // // Order: the environment variable (STRATA_METAL_KERNELS, ...), then that folder when it exists, then the compiled-in // path. Nothing here touches a build run from its source tree: the folder does not exist there. #pragma once #include #include #include #include #include #include namespace strata_metal { inline std::string exe_dir() { char raw[PATH_MAX * 2]; uint32_t n = sizeof(raw); if (_NSGetExecutablePath(raw, &n) != 0) return ""; char real[PATH_MAX]; if (!realpath(raw, real)) return ""; std::string s(real); const size_t slash = s.rfind('/'); return slash == std::string::npos ? "" : s.substr(0, slash); } inline bool exists(const std::string& path) { struct stat st; return stat(path.c_str(), &st) == 0; } // `sub` is relative to /metal/, `compiled` is the path compiled into this build. inline std::string resource(const char* env, const char* sub, const char* compiled) { if (env) { if (const char* e = std::getenv(env)) return e; } const std::string dir = exe_dir(); if (!dir.empty()) { const std::string beside = dir + "/metal/" + sub; if (exists(beside)) return beside; } return compiled; } // The same for a file that has two names (own-stack phase 1B: strata-quant-common.h, formerly ggml-common.h): the environment variable, then the new name // beside the executable, then the old name beside the executable (a package of an earlier release), then the compiled-in path. inline std::string resource2(const char* env, const char* sub, const char* old_sub, const char* compiled) { if (env) { if (const char* e = std::getenv(env)) return e; } const std::string dir = exe_dir(); if (!dir.empty()) { const std::string beside = dir + "/metal/" + sub; if (exists(beside)) return beside; const std::string old_beside = dir + "/metal/" + old_sub; if (exists(old_beside)) return old_beside; } return compiled; } } // namespace strata_metal