PluginLoader.cpp revision 61ffc0c7fd9a23dc423305f144948fbae9956bf6
1c1b5d092a0f89db5356ae79d8cc4213118f230ddChris Lattner//===-- PluginLoader.cpp - Implement -load command line option ------------===//
2f976c856fcc5055f3fc7d9f070d72c2d027c1d9dMisha Brukman//
3b576c94c15af9a440f69d9d03c2afead7971118cJohn Criswell//                     The LLVM Compiler Infrastructure
4b576c94c15af9a440f69d9d03c2afead7971118cJohn Criswell//
54ee451de366474b9c228b4e5fa573795a715216dChris Lattner// This file is distributed under the University of Illinois Open Source
64ee451de366474b9c228b4e5fa573795a715216dChris Lattner// License. See LICENSE.TXT for details.
7f976c856fcc5055f3fc7d9f070d72c2d027c1d9dMisha Brukman//
8b576c94c15af9a440f69d9d03c2afead7971118cJohn Criswell//===----------------------------------------------------------------------===//
9c1b5d092a0f89db5356ae79d8cc4213118f230ddChris Lattner//
1013a253aae77594bb3fd804417e4aa3d4ffe0229bChris Lattner// This file implements the -load <plugin> command line option handler.
11c1b5d092a0f89db5356ae79d8cc4213118f230ddChris Lattner//
12c1b5d092a0f89db5356ae79d8cc4213118f230ddChris Lattner//===----------------------------------------------------------------------===//
13c1b5d092a0f89db5356ae79d8cc4213118f230ddChris Lattner
1413a253aae77594bb3fd804417e4aa3d4ffe0229bChris Lattner#define DONT_GET_PLUGIN_LOADER_OPTION
15e7d3af54737b7787c92ea03588dd4301b29d0899Julien Lerouge#include "llvm/Support/ManagedStatic.h"
16551ccae044b0ff658fe629dd67edd5ffe75d10e8Reid Spencer#include "llvm/Support/PluginLoader.h"
17fe6b146dcd09f488b12c7ff888ead9cedc92c2a6Bill Wendling#include "llvm/Support/Streams.h"
18737459df795e9a83835d1c69bd6e2ec4389b1534Reid Spencer#include "llvm/System/DynamicLibrary.h"
1961ffc0c7fd9a23dc423305f144948fbae9956bf6Owen Anderson#include "llvm/System/Mutex.h"
201a097e30d39e60303ae2b19f7a56e813f3e3c18eBill Wendling#include <ostream>
214b934766bde8989b4eeb3f4a1cc222327e262379Andrew Lenharth#include <vector>
222cdd21c2e4d855500dfb53f77aa74da53ccf9de6Chris Lattnerusing namespace llvm;
23d0fde30ce850b78371fd1386338350591f9ff494Brian Gaeke
24e7d3af54737b7787c92ea03588dd4301b29d0899Julien Lerougestatic ManagedStatic<std::vector<std::string> > Plugins;
2561ffc0c7fd9a23dc423305f144948fbae9956bf6Owen Andersonstatic ManagedStatic<sys::SmartMutex<true> > PluginsLock;
264b934766bde8989b4eeb3f4a1cc222327e262379Andrew Lenharth
2713a253aae77594bb3fd804417e4aa3d4ffe0229bChris Lattnervoid PluginLoader::operator=(const std::string &Filename) {
2861ffc0c7fd9a23dc423305f144948fbae9956bf6Owen Anderson  sys::SmartScopedLock<true> Lock(&*PluginsLock);
2917aa9d3f53d0afe6a5188fd5f76f0738cb7e6a07Chris Lattner  std::string Error;
3017aa9d3f53d0afe6a5188fd5f76f0738cb7e6a07Chris Lattner  if (sys::DynamicLibrary::LoadLibraryPermanently(Filename.c_str(), &Error)) {
31e81561909d128c6e2d8033cb5465a49b2596b26aBill Wendling    cerr << "Error opening '" << Filename << "': " << Error
32e81561909d128c6e2d8033cb5465a49b2596b26aBill Wendling         << "\n  -load request ignored.\n";
3317aa9d3f53d0afe6a5188fd5f76f0738cb7e6a07Chris Lattner  } else {
3417aa9d3f53d0afe6a5188fd5f76f0738cb7e6a07Chris Lattner    Plugins->push_back(Filename);
3517aa9d3f53d0afe6a5188fd5f76f0738cb7e6a07Chris Lattner  }
36c1b5d092a0f89db5356ae79d8cc4213118f230ddChris Lattner}
374b934766bde8989b4eeb3f4a1cc222327e262379Andrew Lenharth
3817aa9d3f53d0afe6a5188fd5f76f0738cb7e6a07Chris Lattnerunsigned PluginLoader::getNumPlugins() {
3961ffc0c7fd9a23dc423305f144948fbae9956bf6Owen Anderson  sys::SmartScopedLock<true> Lock(&*PluginsLock);
40e7d3af54737b7787c92ea03588dd4301b29d0899Julien Lerouge  return Plugins.isConstructed() ? Plugins->size() : 0;
414b934766bde8989b4eeb3f4a1cc222327e262379Andrew Lenharth}
424b934766bde8989b4eeb3f4a1cc222327e262379Andrew Lenharth
4317aa9d3f53d0afe6a5188fd5f76f0738cb7e6a07Chris Lattnerstd::string &PluginLoader::getPlugin(unsigned num) {
4461ffc0c7fd9a23dc423305f144948fbae9956bf6Owen Anderson  sys::SmartScopedLock<true> Lock(&*PluginsLock);
45e7d3af54737b7787c92ea03588dd4301b29d0899Julien Lerouge  assert(Plugins.isConstructed() && num < Plugins->size() &&
46e7d3af54737b7787c92ea03588dd4301b29d0899Julien Lerouge         "Asking for an out of bounds plugin");
4717aa9d3f53d0afe6a5188fd5f76f0738cb7e6a07Chris Lattner  return (*Plugins)[num];
484b934766bde8989b4eeb3f4a1cc222327e262379Andrew Lenharth}
49