blob: 94cbec3a933327095047cc365a6d0c4f351fcbe1 [file] [log] [blame]
Chris Lattnerc1b5d092002-07-23 17:56:53 +00001//===-- PluginLoader.cpp - Implement -load command line option ------------===//
Misha Brukmanf976c852005-04-21 22:55:34 +00002//
John Criswellb576c942003-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 Brukmanf976c852005-04-21 22:55:34 +00007//
John Criswellb576c942003-10-20 19:43:21 +00008//===----------------------------------------------------------------------===//
Chris Lattnerc1b5d092002-07-23 17:56:53 +00009//
Chris Lattner13a253a2004-07-11 01:04:33 +000010// This file implements the -load <plugin> command line option handler.
Chris Lattnerc1b5d092002-07-23 17:56:53 +000011//
12//===----------------------------------------------------------------------===//
13
Chris Lattner13a253a2004-07-11 01:04:33 +000014#define DONT_GET_PLUGIN_LOADER_OPTION
Reid Spencer551ccae2004-09-01 22:55:40 +000015#include "llvm/Support/PluginLoader.h"
Reid Spencer737459d2004-11-29 14:07:46 +000016#include "llvm/System/DynamicLibrary.h"
Chris Lattner0c0edf82002-07-25 06:17:51 +000017#include <iostream>
Andrew Lenharth4b934762006-01-26 18:36:50 +000018#include <vector>
Reid Spencer737459d2004-11-29 14:07:46 +000019
Chris Lattner2cdd21c2003-12-14 21:35:53 +000020using namespace llvm;
Brian Gaeked0fde302003-11-11 22:41:34 +000021
Andrew Lenharth9d901442006-01-26 19:38:58 +000022static std::vector<std::string>* plugins;
Andrew Lenharth4b934762006-01-26 18:36:50 +000023
Chris Lattner13a253a2004-07-11 01:04:33 +000024void PluginLoader::operator=(const std::string &Filename) {
25 std::string ErrorMessage;
Andrew Lenharth9d901442006-01-26 19:38:58 +000026
27 if (!plugins)
28 plugins = new std::vector<std::string>();
29
Reid Spencer737459d2004-11-29 14:07:46 +000030 try {
31 sys::DynamicLibrary::LoadLibraryPermanently(Filename.c_str());
Andrew Lenharth9d901442006-01-26 19:38:58 +000032 plugins->push_back(Filename);
Reid Spencer737459d2004-11-29 14:07:46 +000033 } catch (const std::string& errmsg) {
34 if (errmsg.empty()) {
35 ErrorMessage = "Unknown";
36 } else {
37 ErrorMessage = errmsg;
38 }
39 }
40 if (!ErrorMessage.empty())
Chris Lattner13a253a2004-07-11 01:04:33 +000041 std::cerr << "Error opening '" << Filename << "': " << ErrorMessage
Misha Brukman3c944972005-04-22 04:08:30 +000042 << "\n -load request ignored.\n";
Chris Lattnerc1b5d092002-07-23 17:56:53 +000043}
Andrew Lenharth4b934762006-01-26 18:36:50 +000044
45unsigned PluginLoader::getNumPlugins()
46{
Andrew Lenharth9d901442006-01-26 19:38:58 +000047 if(plugins)
48 return plugins->size();
49 else
50 return 0;
Andrew Lenharth4b934762006-01-26 18:36:50 +000051}
52
53std::string& PluginLoader::getPlugin(unsigned num)
54{
Andrew Lenharth9d901442006-01-26 19:38:58 +000055 assert(plugins && num < plugins->size() && "Asking for an out of bounds plugin");
56 return (*plugins)[num];
Andrew Lenharth4b934762006-01-26 18:36:50 +000057}