blob: ea191ad739fe37ccd1465267b482d1eb0796634e [file] [log] [blame]
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001//===-- PluginLoader.cpp - Implement -load command line option ------------===//
2//
3// The LLVM Compiler Infrastructure
4//
Chris Lattner081ce942007-12-29 20:36:04 +00005// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
Dan Gohmanf17a25c2007-07-18 16:29:46 +00007//
8//===----------------------------------------------------------------------===//
9//
10// This file implements the -load <plugin> command line option handler.
11//
12//===----------------------------------------------------------------------===//
13
14#define DONT_GET_PLUGIN_LOADER_OPTION
Julien Lerouged231d182008-10-22 16:30:41 +000015#include "llvm/Support/ManagedStatic.h"
Dan Gohmanf17a25c2007-07-18 16:29:46 +000016#include "llvm/Support/PluginLoader.h"
17#include "llvm/Support/Streams.h"
18#include "llvm/System/DynamicLibrary.h"
Owen Anderson8974bd62009-06-23 00:02:39 +000019#include "llvm/System/Mutex.h"
Dan Gohmanf17a25c2007-07-18 16:29:46 +000020#include <ostream>
21#include <vector>
22using namespace llvm;
23
Julien Lerouged231d182008-10-22 16:30:41 +000024static ManagedStatic<std::vector<std::string> > Plugins;
Owen Anderson8974bd62009-06-23 00:02:39 +000025static ManagedStatic<sys::SmartMutex<true> > PluginsLock;
Dan Gohmanf17a25c2007-07-18 16:29:46 +000026
27void PluginLoader::operator=(const std::string &Filename) {
Owen Andersonbe44bed2009-07-07 18:33:04 +000028 sys::SmartScopedLock<true> Lock(*PluginsLock);
Dan Gohmanf17a25c2007-07-18 16:29:46 +000029 std::string Error;
30 if (sys::DynamicLibrary::LoadLibraryPermanently(Filename.c_str(), &Error)) {
31 cerr << "Error opening '" << Filename << "': " << Error
32 << "\n -load request ignored.\n";
33 } else {
34 Plugins->push_back(Filename);
35 }
36}
37
38unsigned PluginLoader::getNumPlugins() {
Owen Andersonbe44bed2009-07-07 18:33:04 +000039 sys::SmartScopedLock<true> Lock(*PluginsLock);
Julien Lerouged231d182008-10-22 16:30:41 +000040 return Plugins.isConstructed() ? Plugins->size() : 0;
Dan Gohmanf17a25c2007-07-18 16:29:46 +000041}
42
43std::string &PluginLoader::getPlugin(unsigned num) {
Owen Andersonbe44bed2009-07-07 18:33:04 +000044 sys::SmartScopedLock<true> Lock(*PluginsLock);
Julien Lerouged231d182008-10-22 16:30:41 +000045 assert(Plugins.isConstructed() && num < Plugins->size() &&
46 "Asking for an out of bounds plugin");
Dan Gohmanf17a25c2007-07-18 16:29:46 +000047 return (*Plugins)[num];
48}