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
15551ccae044b0ff658fe629dd67edd5ffe75d10e8Reid Spencer#include "llvm/Support/PluginLoader.h"
161f6efa3996dd1929fbc129203ce5009b620e6969Michael J. Spencer#include "llvm/Support/DynamicLibrary.h"
17d04a8d4b33ff316ca4cf961e06c9e312eff8e64fChandler Carruth#include "llvm/Support/ManagedStatic.h"
181f6efa3996dd1929fbc129203ce5009b620e6969Michael J. Spencer#include "llvm/Support/Mutex.h"
19d04a8d4b33ff316ca4cf961e06c9e312eff8e64fChandler Carruth#include "llvm/Support/raw_ostream.h"
204b934766bde8989b4eeb3f4a1cc222327e262379Andrew Lenharth#include <vector>
212cdd21c2e4d855500dfb53f77aa74da53ccf9de6Chris Lattnerusing namespace llvm;
22d0fde30ce850b78371fd1386338350591f9ff494Brian Gaeke
23e7d3af54737b7787c92ea03588dd4301b29d0899Julien Lerougestatic ManagedStatic<std::vector<std::string> > Plugins;
2461ffc0c7fd9a23dc423305f144948fbae9956bf6Owen Andersonstatic ManagedStatic<sys::SmartMutex<true> > PluginsLock;
254b934766bde8989b4eeb3f4a1cc222327e262379Andrew Lenharth
2613a253aae77594bb3fd804417e4aa3d4ffe0229bChris Lattnervoid PluginLoader::operator=(const std::string &Filename) {
27a9d1f2c559ef4b2549e29288fe6944e68913ba0fOwen Anderson  sys::SmartScopedLock<true> Lock(*PluginsLock);
2817aa9d3f53d0afe6a5188fd5f76f0738cb7e6a07Chris Lattner  std::string Error;
2917aa9d3f53d0afe6a5188fd5f76f0738cb7e6a07Chris Lattner  if (sys::DynamicLibrary::LoadLibraryPermanently(Filename.c_str(), &Error)) {
30d9ea85ab01fb0f2929ed50223d3758dceea8bcbdChris Lattner    errs() << "Error opening '" << Filename << "': " << Error
31d9ea85ab01fb0f2929ed50223d3758dceea8bcbdChris Lattner           << "\n  -load request ignored.\n";
3217aa9d3f53d0afe6a5188fd5f76f0738cb7e6a07Chris Lattner  } else {
3317aa9d3f53d0afe6a5188fd5f76f0738cb7e6a07Chris Lattner    Plugins->push_back(Filename);
3417aa9d3f53d0afe6a5188fd5f76f0738cb7e6a07Chris Lattner  }
35c1b5d092a0f89db5356ae79d8cc4213118f230ddChris Lattner}
364b934766bde8989b4eeb3f4a1cc222327e262379Andrew Lenharth
3717aa9d3f53d0afe6a5188fd5f76f0738cb7e6a07Chris Lattnerunsigned PluginLoader::getNumPlugins() {
38a9d1f2c559ef4b2549e29288fe6944e68913ba0fOwen Anderson  sys::SmartScopedLock<true> Lock(*PluginsLock);
39e7d3af54737b7787c92ea03588dd4301b29d0899Julien Lerouge  return Plugins.isConstructed() ? Plugins->size() : 0;
404b934766bde8989b4eeb3f4a1cc222327e262379Andrew Lenharth}
414b934766bde8989b4eeb3f4a1cc222327e262379Andrew Lenharth
4217aa9d3f53d0afe6a5188fd5f76f0738cb7e6a07Chris Lattnerstd::string &PluginLoader::getPlugin(unsigned num) {
43a9d1f2c559ef4b2549e29288fe6944e68913ba0fOwen Anderson  sys::SmartScopedLock<true> Lock(*PluginsLock);
44e7d3af54737b7787c92ea03588dd4301b29d0899Julien Lerouge  assert(Plugins.isConstructed() && num < Plugins->size() &&
45e7d3af54737b7787c92ea03588dd4301b29d0899Julien Lerouge         "Asking for an out of bounds plugin");
4617aa9d3f53d0afe6a5188fd5f76f0738cb7e6a07Chris Lattner  return (*Plugins)[num];
474b934766bde8989b4eeb3f4a1cc222327e262379Andrew Lenharth}
48