File indexing completed on 2021-02-14 13:29:02
0001
0002
0003
0004
0005
0006
0007
0008
0009
0010
0011
0012
0013
0014 #include <filesystem>
0015 #include <fstream>
0016 #include <functional>
0017 #include <set>
0018
0019
0020 #include "TInterpreter.h"
0021 #include "TVirtualMutex.h"
0022
0023
0024 #include "FWCore/PluginManager/interface/CacheParser.h"
0025 #include "FWCore/PluginManager/interface/PluginFactoryBase.h"
0026 #include "FWCore/PluginManager/interface/PluginFactoryManager.h"
0027 #include "FWCore/PluginManager/interface/PluginManager.h"
0028 #include "FWCore/PluginManager/interface/standard.h"
0029 #include "FWCore/Utilities/interface/Exception.h"
0030 #include "FWCore/Utilities/interface/thread_safety_macros.h"
0031
0032 namespace edmplugin {
0033
0034
0035
0036
0037
0038
0039
0040
0041 static bool readCacheFile(const std::filesystem::path& cacheFile,
0042 const std::filesystem::path& dir,
0043 PluginManager::CategoryToInfos& categoryToInfos) {
0044 if (exists(cacheFile)) {
0045 std::ifstream file(cacheFile.string().c_str());
0046 if (not file) {
0047 throw cms::Exception("PluginMangerCacheProblem")
0048 << "Unable to open the cache file '" << cacheFile.string() << "'. Please check permissions on file";
0049 }
0050 CacheParser::read(file, dir, categoryToInfos);
0051 return true;
0052 }
0053 return false;
0054 }
0055
0056
0057
0058 PluginManager::PluginManager(const PluginManager::Config& iConfig) : searchPath_(iConfig.searchPath()) {
0059 using std::placeholders::_1;
0060 const std::filesystem::path& kCacheFile(standard::cachefileName());
0061
0062
0063
0064 const std::filesystem::path& kPoisonedCacheFile(standard::poisonedCachefileName());
0065
0066 PluginFactoryManager* pfm = PluginFactoryManager::get();
0067 pfm->newFactory_.connect(std::bind(std::mem_fn(&PluginManager::newFactory), this, _1));
0068
0069
0070
0071
0072 for (PluginFactoryManager::const_iterator i = pfm->begin(), e = pfm->end(); i != e; ++i) {
0073 categoryToInfos_[(*i)->category()] = (*i)->available();
0074 }
0075
0076
0077
0078
0079 bool foundAtLeastOneCacheFile = false;
0080 std::set<std::string> alreadySeen;
0081 for (SearchPath::const_iterator itPath = searchPath_.begin(), itEnd = searchPath_.end(); itPath != itEnd;
0082 ++itPath) {
0083
0084 if (alreadySeen.find(*itPath) != alreadySeen.end()) {
0085 continue;
0086 }
0087 alreadySeen.insert(*itPath);
0088 std::filesystem::path dir(*itPath);
0089 if (exists(dir)) {
0090 if (not is_directory(dir)) {
0091 throw cms::Exception("PluginManagerBadPath")
0092 << "The path '" << dir.string() << "' for the PluginManager is not a directory";
0093 }
0094 std::filesystem::path cacheFile = dir / kCacheFile;
0095
0096 if (readCacheFile(cacheFile, dir, categoryToInfos_)) {
0097 foundAtLeastOneCacheFile = true;
0098 }
0099
0100
0101
0102 std::filesystem::path poisonedCacheFile = dir / kPoisonedCacheFile;
0103 readCacheFile(poisonedCacheFile, dir / "poisoned", categoryToInfos_);
0104 }
0105 }
0106 if (not foundAtLeastOneCacheFile and iConfig.mustHaveCache()) {
0107 auto ex = cms::Exception("PluginManagerNoCacheFile")
0108 << "No cache files named '" << standard::cachefileName() << "' were found in the directories \n";
0109 for (auto const& seen : alreadySeen) {
0110 ex << " '" << seen << "'\n";
0111 }
0112 throw ex;
0113 }
0114
0115 loadingLibraryNamed_() = "<loaded by another plugin system>";
0116 }
0117
0118
0119
0120
0121
0122
0123 PluginManager::~PluginManager() {}
0124
0125
0126
0127
0128
0129
0130
0131
0132
0133
0134
0135
0136
0137
0138
0139
0140 void PluginManager::newFactory(const PluginFactoryBase*) {}
0141
0142
0143
0144 namespace {
0145 struct PICompare {
0146 bool operator()(const PluginInfo& iLHS, const PluginInfo& iRHS) const { return iLHS.name_ < iRHS.name_; }
0147 };
0148 }
0149
0150 const std::filesystem::path& PluginManager::loadableFor(const std::string& iCategory, const std::string& iPlugin) {
0151 bool throwIfFail = true;
0152 return loadableFor_(iCategory, iPlugin, throwIfFail);
0153 }
0154
0155 const std::filesystem::path& PluginManager::loadableFor_(const std::string& iCategory,
0156 const std::string& iPlugin,
0157 bool& ioThrowIfFailElseSucceedStatus) {
0158 const bool throwIfFail = ioThrowIfFailElseSucceedStatus;
0159 ioThrowIfFailElseSucceedStatus = true;
0160 CategoryToInfos::iterator itFound = categoryToInfos_.find(iCategory);
0161 if (itFound == categoryToInfos_.end()) {
0162 if (throwIfFail) {
0163 throw cms::Exception("PluginNotFound") << "Unable to find plugin '" << iPlugin << "' because the category '"
0164 << iCategory << "' has no known plugins";
0165 } else {
0166 ioThrowIfFailElseSucceedStatus = false;
0167 static const std::filesystem::path s_path;
0168 return s_path;
0169 }
0170 }
0171
0172 PluginInfo i;
0173 i.name_ = iPlugin;
0174 typedef std::vector<PluginInfo>::iterator PIItr;
0175 std::pair<PIItr, PIItr> range = std::equal_range(itFound->second.begin(), itFound->second.end(), i, PICompare());
0176
0177 if (range.first == range.second) {
0178 if (throwIfFail) {
0179 throw cms::Exception("PluginNotFound") << "Unable to find plugin '" << iPlugin << "' in category '" << iCategory
0180 << "'. Please check spelling of name.";
0181 } else {
0182 ioThrowIfFailElseSucceedStatus = false;
0183 static const std::filesystem::path s_path;
0184 return s_path;
0185 }
0186 }
0187
0188 if (range.second - range.first > 1) {
0189
0190 if (range.first->loadable_.parent_path() == (range.first + 1)->loadable_.parent_path()) {
0191
0192 throw cms::Exception("MultiplePlugins")
0193 << "The plugin '" << iPlugin
0194 << "' is found in multiple files \n"
0195 " '"
0196 << range.first->loadable_.filename() << "'\n '" << (range.first + 1)->loadable_.filename()
0197 << "'\n"
0198 "in directory '"
0199 << range.first->loadable_.parent_path().string()
0200 << "'.\n"
0201 "The code must be changed so the plugin only appears in one plugin file. "
0202 "You will need to remove the macro which registers the plugin so it only appears in"
0203 " one of these files.\n"
0204 " If none of these files register such a plugin, "
0205 "then the problem originates in a library to which all these files link.\n"
0206 "The plugin registration must be removed from that library since plugins are not allowed in regular "
0207 "libraries.";
0208 }
0209 }
0210
0211 return range.first->loadable_;
0212 }
0213
0214 namespace {
0215 class Sentry {
0216 public:
0217 Sentry(std::string& iPath, const std::string& iNewPath) : path_(iPath), oldPath_(iPath) { path_ = iNewPath; }
0218 ~Sentry() { path_ = oldPath_; }
0219
0220 private:
0221 std::string& path_;
0222 std::string oldPath_;
0223 };
0224 }
0225
0226 const SharedLibrary& PluginManager::load(const std::string& iCategory, const std::string& iPlugin) {
0227 askedToLoadCategoryWithPlugin_(iCategory, iPlugin);
0228 const std::filesystem::path& p = loadableFor(iCategory, iPlugin);
0229
0230
0231 auto itLoaded = loadables_.find(p);
0232 if (itLoaded == loadables_.end()) {
0233
0234 std::lock_guard<std::recursive_mutex> guard(pluginLoadMutex());
0235
0236 itLoaded = loadables_.find(p);
0237 if (itLoaded == loadables_.end()) {
0238
0239 goingToLoad_(p);
0240 Sentry s(loadingLibraryNamed_(), p.string());
0241
0242 std::shared_ptr<SharedLibrary> ptr;
0243 {
0244
0245
0246 R__LOCKGUARD2(gInterpreterMutex);
0247 try {
0248 ptr = std::make_shared<SharedLibrary>(p);
0249 } catch (cms::Exception& e) {
0250 e.addContext("While attempting to load plugin " + iPlugin);
0251 throw;
0252 }
0253 }
0254 loadables_.emplace(p, ptr);
0255 justLoaded_(*ptr);
0256 return *ptr;
0257 }
0258 }
0259 return *(itLoaded->second);
0260 }
0261
0262 const SharedLibrary* PluginManager::tryToLoad(const std::string& iCategory, const std::string& iPlugin) {
0263 askedToLoadCategoryWithPlugin_(iCategory, iPlugin);
0264 bool ioThrowIfFailElseSucceedStatus = false;
0265 const std::filesystem::path& p = loadableFor_(iCategory, iPlugin, ioThrowIfFailElseSucceedStatus);
0266
0267 if (not ioThrowIfFailElseSucceedStatus) {
0268 return nullptr;
0269 }
0270
0271
0272 auto itLoaded = loadables_.find(p);
0273 if (itLoaded == loadables_.end()) {
0274
0275 std::lock_guard<std::recursive_mutex> guard(pluginLoadMutex());
0276
0277 itLoaded = loadables_.find(p);
0278 if (itLoaded == loadables_.end()) {
0279
0280 goingToLoad_(p);
0281 Sentry s(loadingLibraryNamed_(), p.string());
0282
0283 std::shared_ptr<SharedLibrary> ptr;
0284 {
0285
0286
0287 R__LOCKGUARD(gInterpreterMutex);
0288 try {
0289 ptr = std::make_shared<SharedLibrary>(p);
0290 } catch (cms::Exception& e) {
0291 e.addContext("While attempting to load plugin " + iPlugin);
0292 throw;
0293 }
0294 }
0295 loadables_[p] = ptr;
0296 justLoaded_(*ptr);
0297 return ptr.get();
0298 }
0299 }
0300 return (itLoaded->second).get();
0301 }
0302
0303
0304
0305
0306 PluginManager* PluginManager::get() {
0307 PluginManager* manager = singleton();
0308 if (nullptr == manager) {
0309 throw cms::Exception("PluginManagerNotConfigured")
0310 << "PluginManager::get() was called before PluginManager::configure.";
0311 }
0312 return manager;
0313 }
0314
0315 PluginManager& PluginManager::configure(const Config& iConfig) {
0316 PluginManager*& s = singleton();
0317 if (nullptr != s) {
0318 throw cms::Exception("PluginManagerReconfigured");
0319 }
0320
0321 const Config& realConfig = iConfig;
0322 if (realConfig.searchPath().empty()) {
0323 throw cms::Exception("PluginManagerEmptySearchPath");
0324 }
0325 s = new PluginManager(realConfig);
0326 return *s;
0327 }
0328
0329 const std::string& PluginManager::staticallyLinkedLoadingFileName() {
0330 static const std::string s_name("static");
0331 return s_name;
0332 }
0333
0334 std::string& PluginManager::loadingLibraryNamed_() {
0335
0336
0337 CMS_THREAD_SAFE static std::string s_name(staticallyLinkedLoadingFileName());
0338 return s_name;
0339 }
0340
0341 PluginManager*& PluginManager::singleton() {
0342 CMS_THREAD_SAFE static PluginManager* s_singleton = nullptr;
0343 return s_singleton;
0344 }
0345
0346 bool PluginManager::isAvailable() { return nullptr != singleton(); }
0347
0348 }