Entropic 2.3.8
Local-first agentic inference engine
Loading...
Searching...
No Matches
data_dir.cpp
Go to the documentation of this file.
1// SPDX-License-Identifier: Apache-2.0
9#include <entropic/entropic_config.h>
11
12#include <cstdlib>
13#include <vector>
14
15#include <dlfcn.h>
16
17static auto s_log = entropic::log::get("config");
18
19namespace entropic::config {
20
37static std::filesystem::path share_dir_from_library()
38{
39 // Take the address of resolve_data_dir itself — guaranteed to be
40 // inside librentropic.so (we're in it). dladdr resolves this
41 // pointer back to the .so's filesystem path.
42 Dl_info info = {};
43 if (dladdr(reinterpret_cast<void*>(&share_dir_from_library), &info) == 0
44 || info.dli_fname == nullptr) {
45 return {};
46 }
47 auto lib_path = std::filesystem::path(info.dli_fname);
48 // dli_fname may be a relative path (e.g. just "librentropic.so")
49 // when the loader resolved via LD_LIBRARY_PATH. Make it absolute
50 // so ".." traversal is meaningful.
51 std::error_code ec;
52 auto abs_lib = std::filesystem::absolute(lib_path, ec);
53 if (ec) {
54 return {};
55 }
56 // <prefix>/lib/librentropic.so.2.0.5 → <prefix>/share/entropic
57 return abs_lib.parent_path().parent_path() / "share" / "entropic";
58}
59
81std::filesystem::path resolve_data_dir(const ParsedConfig& config)
82{
83 struct Candidate {
84 std::filesystem::path path;
85 const char* label;
86 };
87
88 std::vector<Candidate> candidates;
89
90 // Priority 1: ENTROPIC_DATA_DIR env var (explicit operator override)
91 if (const char* env = std::getenv("ENTROPIC_DATA_DIR"); env && *env) {
92 candidates.push_back({env, "ENTROPIC_DATA_DIR env"});
93 }
94
95 // Priority 2: config.config_dir / "data"
96 if (!config.config_dir.empty()) {
97 candidates.push_back({config.config_dir / "data", "config.config_dir"});
98 }
99
100 // Priority 3: binary-relative discovery (portable across prefixes)
101 if (auto from_lib = share_dir_from_library(); !from_lib.empty()) {
102 candidates.push_back({from_lib, "binary-relative (dladdr)"});
103 }
104
105 // Priority 4-5: compile-time install path, then source-tree / CWD fallbacks
106 candidates.push_back({CONFIG_ENTROPIC_DATA_DIR, "compile-time install path"});
107 candidates.push_back({CONFIG_ENTROPIC_SOURCE_DATA_DIR, "source tree (dev fallback)"});
108 candidates.push_back({"data", "CWD-relative (dev fallback)"});
109
110 std::filesystem::path result;
111 for (const auto& [path, label] : candidates) {
112 if (std::filesystem::is_directory(path)) {
113 s_log->info("Data dir from {}: {}", label, path.string());
114 result = path;
115 break;
116 }
117 }
118
119 if (result.empty()) {
120 s_log->warn("No data directory found — bundled files unavailable");
121 }
122 return result;
123}
124
125} // namespace entropic::config
static std::filesystem::path share_dir_from_library()
Locate librentropic.so on disk and derive its sibling share dir.
Definition data_dir.cpp:37
Config loader — YAML to C++ structs with validation.
ENTROPIC_EXPORT std::filesystem::path resolve_data_dir(const ParsedConfig &config)
Resolve the bundled data directory.
Definition data_dir.cpp:81
spdlog initialization and logger access.
ENTROPIC_EXPORT std::shared_ptr< spdlog::logger > get(const std::string &name)
Get or create a named logger.
Definition logging.cpp:211
Full parsed configuration.
Definition config.h:714
std::filesystem::path config_dir
Config dir — base for bundled data discovery.
Definition config.h:738