1*9880d681SAndroid Build Coastguard Worker //===-- PluginLoader.cpp - Implement -load command line option ------------===// 2*9880d681SAndroid Build Coastguard Worker // 3*9880d681SAndroid Build Coastguard Worker // The LLVM Compiler Infrastructure 4*9880d681SAndroid Build Coastguard Worker // 5*9880d681SAndroid Build Coastguard Worker // This file is distributed under the University of Illinois Open Source 6*9880d681SAndroid Build Coastguard Worker // License. See LICENSE.TXT for details. 7*9880d681SAndroid Build Coastguard Worker // 8*9880d681SAndroid Build Coastguard Worker //===----------------------------------------------------------------------===// 9*9880d681SAndroid Build Coastguard Worker // 10*9880d681SAndroid Build Coastguard Worker // This file implements the -load <plugin> command line option handler. 11*9880d681SAndroid Build Coastguard Worker // 12*9880d681SAndroid Build Coastguard Worker //===----------------------------------------------------------------------===// 13*9880d681SAndroid Build Coastguard Worker 14*9880d681SAndroid Build Coastguard Worker #define DONT_GET_PLUGIN_LOADER_OPTION 15*9880d681SAndroid Build Coastguard Worker #include "llvm/Support/PluginLoader.h" 16*9880d681SAndroid Build Coastguard Worker #include "llvm/Support/DynamicLibrary.h" 17*9880d681SAndroid Build Coastguard Worker #include "llvm/Support/ManagedStatic.h" 18*9880d681SAndroid Build Coastguard Worker #include "llvm/Support/Mutex.h" 19*9880d681SAndroid Build Coastguard Worker #include "llvm/Support/raw_ostream.h" 20*9880d681SAndroid Build Coastguard Worker #include <vector> 21*9880d681SAndroid Build Coastguard Worker using namespace llvm; 22*9880d681SAndroid Build Coastguard Worker 23*9880d681SAndroid Build Coastguard Worker static ManagedStatic<std::vector<std::string> > Plugins; 24*9880d681SAndroid Build Coastguard Worker static ManagedStatic<sys::SmartMutex<true> > PluginsLock; 25*9880d681SAndroid Build Coastguard Worker operator =(const std::string & Filename)26*9880d681SAndroid Build Coastguard Workervoid PluginLoader::operator=(const std::string &Filename) { 27*9880d681SAndroid Build Coastguard Worker sys::SmartScopedLock<true> Lock(*PluginsLock); 28*9880d681SAndroid Build Coastguard Worker std::string Error; 29*9880d681SAndroid Build Coastguard Worker if (sys::DynamicLibrary::LoadLibraryPermanently(Filename.c_str(), &Error)) { 30*9880d681SAndroid Build Coastguard Worker errs() << "Error opening '" << Filename << "': " << Error 31*9880d681SAndroid Build Coastguard Worker << "\n -load request ignored.\n"; 32*9880d681SAndroid Build Coastguard Worker } else { 33*9880d681SAndroid Build Coastguard Worker Plugins->push_back(Filename); 34*9880d681SAndroid Build Coastguard Worker } 35*9880d681SAndroid Build Coastguard Worker } 36*9880d681SAndroid Build Coastguard Worker getNumPlugins()37*9880d681SAndroid Build Coastguard Workerunsigned PluginLoader::getNumPlugins() { 38*9880d681SAndroid Build Coastguard Worker sys::SmartScopedLock<true> Lock(*PluginsLock); 39*9880d681SAndroid Build Coastguard Worker return Plugins.isConstructed() ? Plugins->size() : 0; 40*9880d681SAndroid Build Coastguard Worker } 41*9880d681SAndroid Build Coastguard Worker getPlugin(unsigned num)42*9880d681SAndroid Build Coastguard Workerstd::string &PluginLoader::getPlugin(unsigned num) { 43*9880d681SAndroid Build Coastguard Worker sys::SmartScopedLock<true> Lock(*PluginsLock); 44*9880d681SAndroid Build Coastguard Worker assert(Plugins.isConstructed() && num < Plugins->size() && 45*9880d681SAndroid Build Coastguard Worker "Asking for an out of bounds plugin"); 46*9880d681SAndroid Build Coastguard Worker return (*Plugins)[num]; 47*9880d681SAndroid Build Coastguard Worker } 48