PluginLoader.cpp revision 4b934766bde8989b4eeb3f4a1cc222327e262379
1c1b5d092a0f89db5356ae79d8cc4213118f230ddChris Lattner//===-- PluginLoader.cpp - Implement -load command line option ------------===// 2f976c856fcc5055f3fc7d9f070d72c2d027c1d9dMisha Brukman// 3b576c94c15af9a440f69d9d03c2afead7971118cJohn Criswell// The LLVM Compiler Infrastructure 4b576c94c15af9a440f69d9d03c2afead7971118cJohn Criswell// 5b576c94c15af9a440f69d9d03c2afead7971118cJohn Criswell// This file was developed by the LLVM research group and is distributed under 6b576c94c15af9a440f69d9d03c2afead7971118cJohn Criswell// the University of Illinois Open Source 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" 16737459df795e9a83835d1c69bd6e2ec4389b1534Reid Spencer#include "llvm/System/DynamicLibrary.h" 170c0edf8afc35a42b15a24ebb5fa5f3fc674290aeChris Lattner#include <iostream> 184b934766bde8989b4eeb3f4a1cc222327e262379Andrew Lenharth#include <vector> 19737459df795e9a83835d1c69bd6e2ec4389b1534Reid Spencer 202cdd21c2e4d855500dfb53f77aa74da53ccf9de6Chris Lattnerusing namespace llvm; 21d0fde30ce850b78371fd1386338350591f9ff494Brian Gaeke 224b934766bde8989b4eeb3f4a1cc222327e262379Andrew Lenharthstd::vector<std::string> plugins; 234b934766bde8989b4eeb3f4a1cc222327e262379Andrew Lenharth 2413a253aae77594bb3fd804417e4aa3d4ffe0229bChris Lattnervoid PluginLoader::operator=(const std::string &Filename) { 2513a253aae77594bb3fd804417e4aa3d4ffe0229bChris Lattner std::string ErrorMessage; 26737459df795e9a83835d1c69bd6e2ec4389b1534Reid Spencer try { 27737459df795e9a83835d1c69bd6e2ec4389b1534Reid Spencer sys::DynamicLibrary::LoadLibraryPermanently(Filename.c_str()); 284b934766bde8989b4eeb3f4a1cc222327e262379Andrew Lenharth plugins.push_back(Filename); 29737459df795e9a83835d1c69bd6e2ec4389b1534Reid Spencer } catch (const std::string& errmsg) { 30737459df795e9a83835d1c69bd6e2ec4389b1534Reid Spencer if (errmsg.empty()) { 31737459df795e9a83835d1c69bd6e2ec4389b1534Reid Spencer ErrorMessage = "Unknown"; 32737459df795e9a83835d1c69bd6e2ec4389b1534Reid Spencer } else { 33737459df795e9a83835d1c69bd6e2ec4389b1534Reid Spencer ErrorMessage = errmsg; 34737459df795e9a83835d1c69bd6e2ec4389b1534Reid Spencer } 35737459df795e9a83835d1c69bd6e2ec4389b1534Reid Spencer } 36737459df795e9a83835d1c69bd6e2ec4389b1534Reid Spencer if (!ErrorMessage.empty()) 3713a253aae77594bb3fd804417e4aa3d4ffe0229bChris Lattner std::cerr << "Error opening '" << Filename << "': " << ErrorMessage 383c94497ec7852eccd68c1bc1663e8ac2a7bb1ab9Misha Brukman << "\n -load request ignored.\n"; 39c1b5d092a0f89db5356ae79d8cc4213118f230ddChris Lattner} 404b934766bde8989b4eeb3f4a1cc222327e262379Andrew Lenharth 414b934766bde8989b4eeb3f4a1cc222327e262379Andrew Lenharthunsigned PluginLoader::getNumPlugins() 424b934766bde8989b4eeb3f4a1cc222327e262379Andrew Lenharth{ 434b934766bde8989b4eeb3f4a1cc222327e262379Andrew Lenharth return plugins.size(); 444b934766bde8989b4eeb3f4a1cc222327e262379Andrew Lenharth} 454b934766bde8989b4eeb3f4a1cc222327e262379Andrew Lenharth 464b934766bde8989b4eeb3f4a1cc222327e262379Andrew Lenharthstd::string& PluginLoader::getPlugin(unsigned num) 474b934766bde8989b4eeb3f4a1cc222327e262379Andrew Lenharth{ 484b934766bde8989b4eeb3f4a1cc222327e262379Andrew Lenharth assert(num < plugins.size() && "Asking for an out of bounds plugin"); 494b934766bde8989b4eeb3f4a1cc222327e262379Andrew Lenharth return plugins[num]; 504b934766bde8989b4eeb3f4a1cc222327e262379Andrew Lenharth} 51