|
3 | 3 | #include "gemc_options.h" |
4 | 4 | #include "gemcConventions.h" |
5 | 5 |
|
| 6 | +// yaml-cpp — needed by collectPluginOptions for the bootstrap YAML scan |
| 7 | +#include "yaml-cpp/yaml.h" |
| 8 | + |
| 9 | +// POSIX dynamic loading |
| 10 | +#include <dlfcn.h> |
| 11 | + |
| 12 | +// c++ |
| 13 | +#include <filesystem> |
| 14 | +#include <set> |
| 15 | +#include <sstream> |
| 16 | + |
6 | 17 | // options definitions |
7 | 18 | #include "dbselect_options.h" |
8 | 19 | #include "gstreamer_options.h" |
@@ -83,4 +94,103 @@ namespace gemc { |
83 | 94 | } |
84 | 95 |
|
85 | 96 |
|
| 97 | + GOptions collectPluginOptions(int argc, char* argv[]) { |
| 98 | + GOptions merged; |
| 99 | + |
| 100 | + // Build the plugin search path from three sources, checked in priority order: |
| 101 | + // 1. -plugin_path=... on the command line (not yet parsed) |
| 102 | + // 2. plugin_path: value in any YAML file listed in argv |
| 103 | + // 3. GEMC_PLUGIN_PATH environment variable |
| 104 | + // Source 1 is extracted first; source 2 is accumulated while scanning YAML files |
| 105 | + // below; source 3 is appended after both. |
| 106 | + std::string search_path; |
| 107 | + for (int i = 1; i < argc; ++i) { |
| 108 | + std::string_view arg = argv[i]; |
| 109 | + if (arg.size() > 13 && arg.substr(0, 13) == "-plugin_path=") { |
| 110 | + const auto val = std::string(arg.substr(13)); |
| 111 | + if (!search_path.empty()) search_path += ':'; |
| 112 | + search_path += val; |
| 113 | + } |
| 114 | + } |
| 115 | + |
| 116 | + // Scan every YAML file present in argv. For each file: |
| 117 | + // - collect any plugin_path value declared there |
| 118 | + // - collect gsystem names to probe for a definePluginOptions symbol |
| 119 | + std::set<std::string> seen_names; // deduplicate across multiple YAML files |
| 120 | + for (int i = 1; i < argc; ++i) { |
| 121 | + const std::string arg = argv[i]; |
| 122 | + if (!std::filesystem::exists(arg)) continue; |
| 123 | + |
| 124 | + YAML::Node root; |
| 125 | + try { root = YAML::LoadFile(arg); } catch (...) { continue; } |
| 126 | + |
| 127 | + // Pick up plugin_path declared in this YAML file (prepend so YAML paths |
| 128 | + // take priority over the env var but not over command-line paths). |
| 129 | + if (root["plugin_path"]) { |
| 130 | + try { |
| 131 | + const auto yaml_pp = root["plugin_path"].as<std::string>(); |
| 132 | + if (!yaml_pp.empty()) { |
| 133 | + search_path = search_path.empty() |
| 134 | + ? yaml_pp |
| 135 | + : search_path + ':' + yaml_pp; |
| 136 | + } |
| 137 | + } catch (...) {} |
| 138 | + } |
| 139 | + |
| 140 | + if (!root["gsystem"]) continue; |
| 141 | + for (const auto& entry : root["gsystem"]) { |
| 142 | + if (!entry["name"]) continue; |
| 143 | + std::string name; |
| 144 | + try { name = entry["name"].as<std::string>(); } catch (...) { continue; } |
| 145 | + if (name.empty() || !seen_names.insert(name).second) continue; |
| 146 | + |
| 147 | + // Resolve <name>.gplugin using the accumulated search path plus |
| 148 | + // GEMC_PLUGIN_PATH and, as a last resort, the current directory. |
| 149 | + std::string full_search = search_path; |
| 150 | + if (const char* env = std::getenv("GEMC_PLUGIN_PATH")) { |
| 151 | + if (!full_search.empty()) full_search += ':'; |
| 152 | + full_search += env; |
| 153 | + } |
| 154 | + |
| 155 | + const std::string basename = name + ".gplugin"; |
| 156 | + std::string lib_path; |
| 157 | + |
| 158 | + if (!full_search.empty()) { |
| 159 | + std::istringstream ss(full_search); |
| 160 | + std::string dir; |
| 161 | + while (std::getline(ss, dir, ':')) { |
| 162 | + if (dir.empty()) continue; |
| 163 | + const auto candidate = dir + "/" + basename; |
| 164 | + if (std::filesystem::exists(candidate)) { lib_path = candidate; break; } |
| 165 | + } |
| 166 | + } |
| 167 | + if (lib_path.empty() && std::filesystem::exists(basename)) lib_path = basename; |
| 168 | + if (lib_path.empty()) continue; // no plugin for this system — normal |
| 169 | + |
| 170 | + // Open the library. On Linux, RTLD_NODELETE keeps it resident so that the |
| 171 | + // later GManager dlopen gets the exact same handle without re-executing |
| 172 | + // static initialisers. On macOS the reference count achieves the same effect. |
| 173 | +#if defined(__linux__) && defined(RTLD_NODELETE) |
| 174 | + const auto h = dlopen(lib_path.c_str(), RTLD_NOW | RTLD_NODELETE); |
| 175 | +#else |
| 176 | + const auto h = dlopen(lib_path.c_str(), RTLD_NOW); |
| 177 | +#endif |
| 178 | + if (!h) continue; // library not loadable — skip silently |
| 179 | + |
| 180 | + // The definePluginOptions symbol is optional. Plugins that need no custom |
| 181 | + // options simply omit it and are silently skipped here. |
| 182 | + // The symbol returns GOptions* (heap-allocated) so that extern "C" linkage |
| 183 | + // is not applied to a by-value C++ return type. |
| 184 | + using opt_fptr = GOptions* (*)(); |
| 185 | + const auto sym = reinterpret_cast<opt_fptr>(dlsym(h, "definePluginOptions")); |
| 186 | + if (!sym) continue; |
| 187 | + |
| 188 | + std::unique_ptr<GOptions> plugin_opts(sym()); |
| 189 | + merged += *plugin_opts; |
| 190 | + } |
| 191 | + } |
| 192 | + |
| 193 | + return merged; |
| 194 | + } |
| 195 | + |
86 | 196 | } |
0 commit comments