Init.cpp 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145
  1. // Copyright (c) 2013-2017 Josh Blum
  2. // SPDX-License-Identifier: BSL-1.0
  3. #include <Pothos/Init.hpp>
  4. #include <Pothos/Exception.hpp>
  5. #include <Pothos/System/Paths.hpp>
  6. #include <Pothos/Plugin.hpp>
  7. #include <Poco/Path.h>
  8. #include <Poco/File.h>
  9. #include <Poco/Format.h>
  10. //from lib/Framework/ConfLoader.cpp
  11. std::vector<Pothos::PluginPath> Pothos_ConfLoader_loadConfFiles(void);
  12. /***********************************************************************
  13. * Singleton for initialization once per process
  14. **********************************************************************/
  15. namespace Pothos {
  16. class InitSingleton
  17. {
  18. public:
  19. InitSingleton(void);
  20. static InitSingleton &instance(void);
  21. void load(void);
  22. void unload(void);
  23. private:
  24. std::vector<Pothos::PluginModule> modules;
  25. std::vector<Pothos::PluginPath> confLoadedPaths;
  26. };
  27. } //namespace Pothos
  28. Pothos::InitSingleton &Pothos::InitSingleton::instance(void)
  29. {
  30. static Pothos::InitSingleton inst;
  31. return inst;
  32. }
  33. void Pothos::InitSingleton::load(void)
  34. {
  35. if (not modules.empty()) return;
  36. modules = PluginLoader::loadModules();
  37. confLoadedPaths = Pothos_ConfLoader_loadConfFiles();
  38. }
  39. void Pothos::InitSingleton::unload(void)
  40. {
  41. for (const auto &path : confLoadedPaths)
  42. {
  43. Pothos::PluginRegistry::remove(path);
  44. }
  45. confLoadedPaths.clear();
  46. modules.clear();
  47. }
  48. /***********************************************************************
  49. * Actual init implementation
  50. **********************************************************************/
  51. Pothos::InitSingleton::InitSingleton(void)
  52. {
  53. //check the root
  54. const Poco::File rootPath(System::getRootPath());
  55. if (not rootPath.exists() or not rootPath.isDirectory()) throw Exception(
  56. "Pothos::init()", Poco::format(
  57. "Pothos installation FAIL - \"%s\" does not exist",
  58. System::getRootPath()
  59. ));
  60. //check for the util executable
  61. const Poco::File utilExecutablePath(System::getPothosUtilExecutablePath());
  62. if (not utilExecutablePath.exists()) throw Exception(
  63. "Pothos::init()", Poco::format(
  64. "Pothos installation FAIL - \"%s\" does not exist",
  65. System::getPothosUtilExecutablePath()
  66. ));
  67. if (not utilExecutablePath.canExecute()) throw Exception(
  68. "Pothos::init()", Poco::format(
  69. "Pothos installation FAIL - \"%s\" not executable",
  70. System::getPothosUtilExecutablePath()
  71. ));
  72. //check for the runtime library
  73. const Poco::File runtimeLibraryPath(System::getPothosRuntimeLibraryPath());
  74. if (not runtimeLibraryPath.exists() or not runtimeLibraryPath.isFile()) throw Exception(
  75. "Pothos::init()", Poco::format(
  76. "Pothos installation FAIL - \"%s\" does not exist",
  77. System::getPothosRuntimeLibraryPath()
  78. ));
  79. //check the user data directory
  80. Poco::File userDataPath(System::getUserDataPath());
  81. if (not userDataPath.exists()) userDataPath.createDirectories();
  82. if (not userDataPath.exists() or not userDataPath.canWrite()) throw Exception(
  83. "Pothos::init()", Poco::format(
  84. "Pothos user directory FAIL - \"%s\" permissions error",
  85. System::getUserDataPath()
  86. ));
  87. //check the user config directory
  88. Poco::File userConfigPath(System::getUserConfigPath());
  89. if (not userConfigPath.exists()) userConfigPath.createDirectories();
  90. if (not userConfigPath.exists() or not userConfigPath.canWrite()) throw Exception(
  91. "Pothos::init()", Poco::format(
  92. "Pothos user directory FAIL - \"%s\" permissions error",
  93. System::getUserConfigPath()
  94. ));
  95. //check the development environment (includes)
  96. Poco::File devIncludePath(System::getPothosDevIncludePath());
  97. if (not devIncludePath.exists()) throw Exception(
  98. "Pothos::init()", Poco::format(
  99. "Pothos development include directory FAIL - \"%s\" does not exist",
  100. System::getPothosDevIncludePath()
  101. ));
  102. //check the development environment (libraries)
  103. Poco::File devLibraryPath(System::getPothosDevLibraryPath());
  104. if (not devLibraryPath.exists()) throw Exception(
  105. "Pothos::init()", Poco::format(
  106. "Pothos development library directory FAIL - \"%s\" does not exist",
  107. System::getPothosDevLibraryPath()
  108. ));
  109. }
  110. void Pothos::init(void)
  111. {
  112. Pothos::InitSingleton::instance().load();
  113. }
  114. void Pothos::deinit(void)
  115. {
  116. Pothos::InitSingleton::instance().unload();
  117. }
  118. Pothos::ScopedInit::ScopedInit(void)
  119. {
  120. Pothos::init();
  121. }
  122. Pothos::ScopedInit::~ScopedInit(void)
  123. {
  124. Pothos::deinit();
  125. }