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 13 #define DONT_GET_PLUGIN_LOADER_OPTION 14 #include "llvm/Support/PluginLoader.h" 15 #include "llvm/Support/DynamicLibrary.h" 16 #include "llvm/Support/Mutex.h" 17 #include "llvm/Support/raw_ostream.h" 18 #include <vector> 19 using namespace llvm; 20 21 namespace { 22 23 struct Plugins { 24 sys::SmartMutex<true> Lock; 25 std::vector<std::string> List; 26 }; 27 28 Plugins &getPlugins() { 29 static Plugins P; 30 return P; 31 } 32 33 } // anonymous namespace 34 35 void PluginLoader::operator=(const std::string &Filename) { 36 auto &P = getPlugins(); 37 sys::SmartScopedLock<true> Lock(P.Lock); 38 std::string Error; 39 if (sys::DynamicLibrary::LoadLibraryPermanently(Filename.c_str(), &Error)) { 40 errs() << "Error opening '" << Filename << "': " << Error 41 << "\n -load request ignored.\n"; 42 } else { 43 P.List.push_back(Filename); 44 } 45 } 46 47 unsigned PluginLoader::getNumPlugins() { 48 auto &P = getPlugins(); 49 sys::SmartScopedLock<true> Lock(P.Lock); 50 return P.List.size(); 51 } 52 53 std::string &PluginLoader::getPlugin(unsigned num) { 54 auto &P = getPlugins(); 55 sys::SmartScopedLock<true> Lock(P.Lock); 56 assert(num < P.List.size() && "Asking for an out of bounds plugin"); 57 return P.List[num]; 58 } 59