PluginLoader.cpp 1.6 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758
  1. //===-- PluginLoader.cpp - Implement -load command line option ------------===//
  2. //
  3. // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
  4. // See https://llvm.org/LICENSE.txt for license information.
  5. // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
  6. //
  7. //===----------------------------------------------------------------------===//
  8. //
  9. // This file implements the -load <plugin> command line option handler.
  10. //
  11. //===----------------------------------------------------------------------===//
  12. #define DONT_GET_PLUGIN_LOADER_OPTION
  13. #include "llvm/Support/PluginLoader.h"
  14. #include "llvm/Support/DynamicLibrary.h"
  15. #include "llvm/Support/Mutex.h"
  16. #include "llvm/Support/raw_ostream.h"
  17. #include <vector>
  18. using namespace llvm;
  19. namespace {
  20. struct Plugins {
  21. sys::SmartMutex<true> Lock;
  22. std::vector<std::string> List;
  23. };
  24. Plugins &getPlugins() {
  25. static Plugins P;
  26. return P;
  27. }
  28. } // anonymous namespace
  29. void PluginLoader::operator=(const std::string &Filename) {
  30. auto &P = getPlugins();
  31. sys::SmartScopedLock<true> Lock(P.Lock);
  32. std::string Error;
  33. if (sys::DynamicLibrary::LoadLibraryPermanently(Filename.c_str(), &Error)) {
  34. errs() << "Error opening '" << Filename << "': " << Error
  35. << "\n -load request ignored.\n";
  36. } else {
  37. P.List.push_back(Filename);
  38. }
  39. }
  40. unsigned PluginLoader::getNumPlugins() {
  41. auto &P = getPlugins();
  42. sys::SmartScopedLock<true> Lock(P.Lock);
  43. return P.List.size();
  44. }
  45. std::string &PluginLoader::getPlugin(unsigned num) {
  46. auto &P = getPlugins();
  47. sys::SmartScopedLock<true> Lock(P.Lock);
  48. assert(num < P.List.size() && "Asking for an out of bounds plugin");
  49. return P.List[num];
  50. }