Chris Lattner | c1b5d09 | 2002-07-23 17:56:53 +0000 | [diff] [blame] | 1 | //===-- PluginLoader.cpp - Implement -load command line option ------------===// |
Misha Brukman | f976c85 | 2005-04-21 22:55:34 +0000 | [diff] [blame] | 2 | // |
John Criswell | b576c94 | 2003-10-20 19:43:21 +0000 | [diff] [blame] | 3 | // The LLVM Compiler Infrastructure |
| 4 | // |
Chris Lattner | 4ee451d | 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. |
Misha Brukman | f976c85 | 2005-04-21 22:55:34 +0000 | [diff] [blame] | 7 | // |
John Criswell | b576c94 | 2003-10-20 19:43:21 +0000 | [diff] [blame] | 8 | //===----------------------------------------------------------------------===// |
Chris Lattner | c1b5d09 | 2002-07-23 17:56:53 +0000 | [diff] [blame] | 9 | // |
Chris Lattner | 13a253a | 2004-07-11 01:04:33 +0000 | [diff] [blame] | 10 | // This file implements the -load <plugin> command line option handler. |
Chris Lattner | c1b5d09 | 2002-07-23 17:56:53 +0000 | [diff] [blame] | 11 | // |
| 12 | //===----------------------------------------------------------------------===// |
| 13 | |
Chris Lattner | 13a253a | 2004-07-11 01:04:33 +0000 | [diff] [blame] | 14 | #define DONT_GET_PLUGIN_LOADER_OPTION |
Julien Lerouge | e7d3af5 | 2008-10-22 16:30:41 +0000 | [diff] [blame] | 15 | #include "llvm/Support/ManagedStatic.h" |
Reid Spencer | 551ccae | 2004-09-01 22:55:40 +0000 | [diff] [blame] | 16 | #include "llvm/Support/PluginLoader.h" |
Bill Wendling | fe6b146 | 2006-11-26 10:52:51 +0000 | [diff] [blame] | 17 | #include "llvm/Support/Streams.h" |
Reid Spencer | 737459d | 2004-11-29 14:07:46 +0000 | [diff] [blame] | 18 | #include "llvm/System/DynamicLibrary.h" |
Owen Anderson | 61ffc0c | 2009-06-23 00:02:39 +0000 | [diff] [blame] | 19 | #include "llvm/System/Mutex.h" |
Bill Wendling | 1a097e3 | 2006-12-07 23:41:45 +0000 | [diff] [blame] | 20 | #include <ostream> |
Andrew Lenharth | 4b93476 | 2006-01-26 18:36:50 +0000 | [diff] [blame] | 21 | #include <vector> |
Chris Lattner | 2cdd21c | 2003-12-14 21:35:53 +0000 | [diff] [blame] | 22 | using namespace llvm; |
Brian Gaeke | d0fde30 | 2003-11-11 22:41:34 +0000 | [diff] [blame] | 23 | |
Julien Lerouge | e7d3af5 | 2008-10-22 16:30:41 +0000 | [diff] [blame] | 24 | static ManagedStatic<std::vector<std::string> > Plugins; |
Owen Anderson | 61ffc0c | 2009-06-23 00:02:39 +0000 | [diff] [blame] | 25 | static ManagedStatic<sys::SmartMutex<true> > PluginsLock; |
Andrew Lenharth | 4b93476 | 2006-01-26 18:36:50 +0000 | [diff] [blame] | 26 | |
Chris Lattner | 13a253a | 2004-07-11 01:04:33 +0000 | [diff] [blame] | 27 | void PluginLoader::operator=(const std::string &Filename) { |
Owen Anderson | 61ffc0c | 2009-06-23 00:02:39 +0000 | [diff] [blame] | 28 | sys::SmartScopedLock<true> Lock(&*PluginsLock); |
Chris Lattner | 17aa9d3 | 2006-07-07 17:14:04 +0000 | [diff] [blame] | 29 | std::string Error; |
| 30 | if (sys::DynamicLibrary::LoadLibraryPermanently(Filename.c_str(), &Error)) { |
Bill Wendling | e815619 | 2006-12-07 01:30:32 +0000 | [diff] [blame] | 31 | cerr << "Error opening '" << Filename << "': " << Error |
| 32 | << "\n -load request ignored.\n"; |
Chris Lattner | 17aa9d3 | 2006-07-07 17:14:04 +0000 | [diff] [blame] | 33 | } else { |
| 34 | Plugins->push_back(Filename); |
| 35 | } |
Chris Lattner | c1b5d09 | 2002-07-23 17:56:53 +0000 | [diff] [blame] | 36 | } |
Andrew Lenharth | 4b93476 | 2006-01-26 18:36:50 +0000 | [diff] [blame] | 37 | |
Chris Lattner | 17aa9d3 | 2006-07-07 17:14:04 +0000 | [diff] [blame] | 38 | unsigned PluginLoader::getNumPlugins() { |
Owen Anderson | 61ffc0c | 2009-06-23 00:02:39 +0000 | [diff] [blame] | 39 | sys::SmartScopedLock<true> Lock(&*PluginsLock); |
Julien Lerouge | e7d3af5 | 2008-10-22 16:30:41 +0000 | [diff] [blame] | 40 | return Plugins.isConstructed() ? Plugins->size() : 0; |
Andrew Lenharth | 4b93476 | 2006-01-26 18:36:50 +0000 | [diff] [blame] | 41 | } |
| 42 | |
Chris Lattner | 17aa9d3 | 2006-07-07 17:14:04 +0000 | [diff] [blame] | 43 | std::string &PluginLoader::getPlugin(unsigned num) { |
Owen Anderson | 61ffc0c | 2009-06-23 00:02:39 +0000 | [diff] [blame] | 44 | sys::SmartScopedLock<true> Lock(&*PluginsLock); |
Julien Lerouge | e7d3af5 | 2008-10-22 16:30:41 +0000 | [diff] [blame] | 45 | assert(Plugins.isConstructed() && num < Plugins->size() && |
| 46 | "Asking for an out of bounds plugin"); |
Chris Lattner | 17aa9d3 | 2006-07-07 17:14:04 +0000 | [diff] [blame] | 47 | return (*Plugins)[num]; |
Andrew Lenharth | 4b93476 | 2006-01-26 18:36:50 +0000 | [diff] [blame] | 48 | } |