blob: 77eb52852dee6d0a46c5770f9544d12313e5a8c8 [file] [log] [blame]
Chris Lattner2c54a0d2002-07-23 17:56:53 +00001//===-- PluginLoader.cpp - Implement -load command line option ------------===//
Misha Brukman10468d82005-04-21 22:55:34 +00002//
John Criswell482202a2003-10-20 19:43:21 +00003// The LLVM Compiler Infrastructure
4//
5// This file was developed by the LLVM research group and is distributed under
6// the University of Illinois Open Source License. See LICENSE.TXT for details.
Misha Brukman10468d82005-04-21 22:55:34 +00007//
John Criswell482202a2003-10-20 19:43:21 +00008//===----------------------------------------------------------------------===//
Chris Lattner2c54a0d2002-07-23 17:56:53 +00009//
Chris Lattner99dcad42004-07-11 01:04:33 +000010// This file implements the -load <plugin> command line option handler.
Chris Lattner2c54a0d2002-07-23 17:56:53 +000011//
12//===----------------------------------------------------------------------===//
13
Chris Lattner99dcad42004-07-11 01:04:33 +000014#define DONT_GET_PLUGIN_LOADER_OPTION
Reid Spencer7c16caa2004-09-01 22:55:40 +000015#include "llvm/Support/PluginLoader.h"
Reid Spencer9ec27612004-11-29 14:07:46 +000016#include "llvm/System/DynamicLibrary.h"
Chris Lattner10073a92002-07-25 06:17:51 +000017#include <iostream>
Andrew Lenharthbb4c9c02006-01-26 18:36:50 +000018#include <vector>
Reid Spencer9ec27612004-11-29 14:07:46 +000019
Chris Lattnerc9499b62003-12-14 21:35:53 +000020using namespace llvm;
Brian Gaeke960707c2003-11-11 22:41:34 +000021
Andrew Lenharthbb4c9c02006-01-26 18:36:50 +000022std::vector<std::string> plugins;
23
Chris Lattner99dcad42004-07-11 01:04:33 +000024void PluginLoader::operator=(const std::string &Filename) {
25 std::string ErrorMessage;
Reid Spencer9ec27612004-11-29 14:07:46 +000026 try {
27 sys::DynamicLibrary::LoadLibraryPermanently(Filename.c_str());
Andrew Lenharthbb4c9c02006-01-26 18:36:50 +000028 plugins.push_back(Filename);
Reid Spencer9ec27612004-11-29 14:07:46 +000029 } catch (const std::string& errmsg) {
30 if (errmsg.empty()) {
31 ErrorMessage = "Unknown";
32 } else {
33 ErrorMessage = errmsg;
34 }
35 }
36 if (!ErrorMessage.empty())
Chris Lattner99dcad42004-07-11 01:04:33 +000037 std::cerr << "Error opening '" << Filename << "': " << ErrorMessage
Misha Brukman5191b4b2005-04-22 04:08:30 +000038 << "\n -load request ignored.\n";
Chris Lattner2c54a0d2002-07-23 17:56:53 +000039}
Andrew Lenharthbb4c9c02006-01-26 18:36:50 +000040
41unsigned PluginLoader::getNumPlugins()
42{
43 return plugins.size();
44}
45
46std::string& PluginLoader::getPlugin(unsigned num)
47{
48 assert(num < plugins.size() && "Asking for an out of bounds plugin");
49 return plugins[num];
50}