Loader.in.cpp 2.0 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768
  1. // Copyright (c) 2013-2016 Josh Blum
  2. // SPDX-License-Identifier: BSL-1.0
  3. #include <Pothos/Plugin/Loader.hpp>
  4. #include <Pothos/System.hpp>
  5. #include <Pothos/Plugin.hpp>
  6. #include <Poco/StringTokenizer.h>
  7. #include <Poco/SharedLibrary.h>
  8. #include <Poco/Environment.h>
  9. #include <Poco/Logger.h>
  10. #include <Poco/Path.h>
  11. #include <Poco/File.h>
  12. #include <future>
  13. static std::vector<Poco::Path> getModulePaths(const Poco::Path &path)
  14. {
  15. poco_debug(Poco::Logger::get("Pothos.PluginLoader.load"), path.toString());
  16. std::vector<Poco::Path> paths;
  17. const Poco::File file(path);
  18. if (not file.exists()) return paths;
  19. else if (file.isFile() and (path.getExtension() == "@MODULE_EXT@"))
  20. {
  21. paths.push_back(path);
  22. }
  23. else if (file.isDirectory())
  24. {
  25. std::vector<std::string> files; file.list(files);
  26. for (size_t i = 0; i < files.size(); i++)
  27. {
  28. auto subpaths = getModulePaths(Poco::Path(path, files[i]).absolute());
  29. paths.insert(paths.end(), subpaths.begin(), subpaths.end());
  30. }
  31. }
  32. return paths;
  33. }
  34. std::vector<Pothos::PluginModule> Pothos::PluginLoader::loadModules(void)
  35. {
  36. const auto searchPaths = Pothos::System::getPothosModuleSearchPaths();
  37. //traverse the search paths and spawn futures
  38. std::vector<std::future<Pothos::PluginModule>> futures;
  39. for (const auto &searchPath : searchPaths)
  40. {
  41. for (const auto &path : getModulePaths(searchPath))
  42. {
  43. futures.push_back(std::async(std::launch::async, &Pothos::PluginModule::safeLoad, path.toString()));
  44. }
  45. }
  46. //wait for completion of future module load
  47. std::vector<PluginModule> modules;
  48. for (auto &future : futures)
  49. {
  50. POTHOS_EXCEPTION_TRY
  51. {
  52. modules.push_back(future.get());
  53. }
  54. POTHOS_EXCEPTION_CATCH (const Pothos::Exception &ex)
  55. {
  56. poco_error(Poco::Logger::get("Pothos.PluginLoader.load"), ex.displayText());
  57. }
  58. }
  59. return modules;
  60. }