Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 1 | //===-- PluginLoader.cpp - Implement -load command line option ------------===// |
| 2 | // |
| 3 | // The LLVM Compiler Infrastructure |
| 4 | // |
Chris Lattner | 081ce94 | 2007-12-29 20:36:04 +0000 | [diff] [blame] | 5 | // This file is distributed under the University of Illinois Open Source |
| 6 | // License. See LICENSE.TXT for details. |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 7 | // |
| 8 | //===----------------------------------------------------------------------===// |
| 9 | // |
| 10 | // This file implements the -load <plugin> command line option handler. |
| 11 | // |
| 12 | //===----------------------------------------------------------------------===// |
| 13 | |
| 14 | #define DONT_GET_PLUGIN_LOADER_OPTION |
Julien Lerouge | d231d18 | 2008-10-22 16:30:41 +0000 | [diff] [blame] | 15 | #include "llvm/Support/ManagedStatic.h" |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 16 | #include "llvm/Support/PluginLoader.h" |
Chris Lattner | 5febcae | 2009-08-23 08:43:55 +0000 | [diff] [blame] | 17 | #include "llvm/Support/raw_ostream.h" |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 18 | #include "llvm/System/DynamicLibrary.h" |
Owen Anderson | 8974bd6 | 2009-06-23 00:02:39 +0000 | [diff] [blame] | 19 | #include "llvm/System/Mutex.h" |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 20 | #include <vector> |
| 21 | using namespace llvm; |
| 22 | |
Julien Lerouge | d231d18 | 2008-10-22 16:30:41 +0000 | [diff] [blame] | 23 | static ManagedStatic<std::vector<std::string> > Plugins; |
Owen Anderson | 8974bd6 | 2009-06-23 00:02:39 +0000 | [diff] [blame] | 24 | static ManagedStatic<sys::SmartMutex<true> > PluginsLock; |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 25 | |
| 26 | void PluginLoader::operator=(const std::string &Filename) { |
Owen Anderson | be44bed | 2009-07-07 18:33:04 +0000 | [diff] [blame] | 27 | sys::SmartScopedLock<true> Lock(*PluginsLock); |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 28 | std::string Error; |
| 29 | if (sys::DynamicLibrary::LoadLibraryPermanently(Filename.c_str(), &Error)) { |
Chris Lattner | 5febcae | 2009-08-23 08:43:55 +0000 | [diff] [blame] | 30 | errs() << "Error opening '" << Filename << "': " << Error |
| 31 | << "\n -load request ignored.\n"; |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 32 | } else { |
| 33 | Plugins->push_back(Filename); |
| 34 | } |
| 35 | } |
| 36 | |
| 37 | unsigned PluginLoader::getNumPlugins() { |
Owen Anderson | be44bed | 2009-07-07 18:33:04 +0000 | [diff] [blame] | 38 | sys::SmartScopedLock<true> Lock(*PluginsLock); |
Julien Lerouge | d231d18 | 2008-10-22 16:30:41 +0000 | [diff] [blame] | 39 | return Plugins.isConstructed() ? Plugins->size() : 0; |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 40 | } |
| 41 | |
| 42 | std::string &PluginLoader::getPlugin(unsigned num) { |
Owen Anderson | be44bed | 2009-07-07 18:33:04 +0000 | [diff] [blame] | 43 | sys::SmartScopedLock<true> Lock(*PluginsLock); |
Julien Lerouge | d231d18 | 2008-10-22 16:30:41 +0000 | [diff] [blame] | 44 | assert(Plugins.isConstructed() && num < Plugins->size() && |
| 45 | "Asking for an out of bounds plugin"); |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 46 | return (*Plugins)[num]; |
| 47 | } |