12cbeb8abd92d5ad8a1bd415b51b3816213b15f3Thomas G. Lane//===-- PluginLoader.cpp - Implement -load command line option ------------===//
22cbeb8abd92d5ad8a1bd415b51b3816213b15f3Thomas G. Lane//
32cbeb8abd92d5ad8a1bd415b51b3816213b15f3Thomas G. Lane//                     The LLVM Compiler Infrastructure
4a73e870ad02de20c2b34cb3a5382c2846c2afbe3DRC//
55ead57a34a398aa798f35bd7a6abad19b2e453e2Thomas G. Lane// This file is distributed under the University of Illinois Open Source
65829cb23983cd241c48abd8ed70ff3627560c453Guido Vollbeding// License. See LICENSE.TXT for details.
7a6ef282a49f2d7d1b4d19cc89f63e81fd66b35b7DRC//
8cf763c0cd8116ac225bd2d36a71da33a3c7fcc46DRC//===----------------------------------------------------------------------===//
90e94025ac72a481ca822b9c876dcada6db977d8bDRC//
1078df2e6115b0e579432d01cb034132cd4402a1baDRC// This file implements the -load <plugin> command line option handler.
116eb7d3798b5a79347c62825fc4c16f7ce673bdd0Alex Naidis//
126eb7d3798b5a79347c62825fc4c16f7ce673bdd0Alex Naidis//===----------------------------------------------------------------------===//
132cbeb8abd92d5ad8a1bd415b51b3816213b15f3Thomas G. Lane
142cbeb8abd92d5ad8a1bd415b51b3816213b15f3Thomas G. Lane#define DONT_GET_PLUGIN_LOADER_OPTION
152cbeb8abd92d5ad8a1bd415b51b3816213b15f3Thomas G. Lane#include "llvm/Support/ManagedStatic.h"
162cbeb8abd92d5ad8a1bd415b51b3816213b15f3Thomas G. Lane#include "llvm/Support/PluginLoader.h"
1736a4ccccd33f5cc9df62949554af87129ced7f84Thomas G. Lane#include "llvm/Support/raw_ostream.h"
182cbeb8abd92d5ad8a1bd415b51b3816213b15f3Thomas G. Lane#include "llvm/Support/DynamicLibrary.h"
1936a4ccccd33f5cc9df62949554af87129ced7f84Thomas G. Lane#include "llvm/Support/Mutex.h"
2059a3938b2e3f6c49730a57935f389078219a8fabPierre Ossman#include <vector>
21ff6961f3d22060adc13cc3e60d42fc480de007e5DRCusing namespace llvm;
2236a4ccccd33f5cc9df62949554af87129ced7f84Thomas G. Lane
2336a4ccccd33f5cc9df62949554af87129ced7f84Thomas G. Lanestatic ManagedStatic<std::vector<std::string> > Plugins;
2436a4ccccd33f5cc9df62949554af87129ced7f84Thomas G. Lanestatic ManagedStatic<sys::SmartMutex<true> > PluginsLock;
2536a4ccccd33f5cc9df62949554af87129ced7f84Thomas G. Lane
2636a4ccccd33f5cc9df62949554af87129ced7f84Thomas G. Lanevoid PluginLoader::operator=(const std::string &Filename) {
2736a4ccccd33f5cc9df62949554af87129ced7f84Thomas G. Lane  sys::SmartScopedLock<true> Lock(*PluginsLock);
2836a4ccccd33f5cc9df62949554af87129ced7f84Thomas G. Lane  std::string Error;
2936a4ccccd33f5cc9df62949554af87129ced7f84Thomas G. Lane  if (sys::DynamicLibrary::LoadLibraryPermanently(Filename.c_str(), &Error)) {
306eb7d3798b5a79347c62825fc4c16f7ce673bdd0Alex Naidis    errs() << "Error opening '" << Filename << "': " << Error
316eb7d3798b5a79347c62825fc4c16f7ce673bdd0Alex Naidis           << "\n  -load request ignored.\n";
326eb7d3798b5a79347c62825fc4c16f7ce673bdd0Alex Naidis  } else {
336eb7d3798b5a79347c62825fc4c16f7ce673bdd0Alex Naidis    Plugins->push_back(Filename);
345829cb23983cd241c48abd8ed70ff3627560c453Guido Vollbeding  }
355829cb23983cd241c48abd8ed70ff3627560c453Guido Vollbeding}
366eb7d3798b5a79347c62825fc4c16f7ce673bdd0Alex Naidis
3736a4ccccd33f5cc9df62949554af87129ced7f84Thomas G. Laneunsigned PluginLoader::getNumPlugins() {
3836a4ccccd33f5cc9df62949554af87129ced7f84Thomas G. Lane  sys::SmartScopedLock<true> Lock(*PluginsLock);
396eb7d3798b5a79347c62825fc4c16f7ce673bdd0Alex Naidis  return Plugins.isConstructed() ? Plugins->size() : 0;
402cbeb8abd92d5ad8a1bd415b51b3816213b15f3Thomas G. Lane}
412cbeb8abd92d5ad8a1bd415b51b3816213b15f3Thomas G. Lane
424a6b7303643714d495b9d26742d8a156fd120936Thomas G. Lanestd::string &PluginLoader::getPlugin(unsigned num) {
435829cb23983cd241c48abd8ed70ff3627560c453Guido Vollbeding  sys::SmartScopedLock<true> Lock(*PluginsLock);
444a6b7303643714d495b9d26742d8a156fd120936Thomas G. Lane  assert(Plugins.isConstructed() && num < Plugins->size() &&
454a6b7303643714d495b9d26742d8a156fd120936Thomas G. Lane         "Asking for an out of bounds plugin");
464a6b7303643714d495b9d26742d8a156fd120936Thomas G. Lane  return (*Plugins)[num];
474a6b7303643714d495b9d26742d8a156fd120936Thomas G. Lane}
484a6b7303643714d495b9d26742d8a156fd120936Thomas G. Lane