1*0b57cec5SDimitry Andric //===-- PluginLoader.cpp - Implement -load command line option ------------===//
2*0b57cec5SDimitry Andric //
3*0b57cec5SDimitry Andric // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4*0b57cec5SDimitry Andric // See https://llvm.org/LICENSE.txt for license information.
5*0b57cec5SDimitry Andric // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6*0b57cec5SDimitry Andric //
7*0b57cec5SDimitry Andric //===----------------------------------------------------------------------===//
8*0b57cec5SDimitry Andric //
9*0b57cec5SDimitry Andric // This file implements the -load <plugin> command line option handler.
10*0b57cec5SDimitry Andric //
11*0b57cec5SDimitry Andric //===----------------------------------------------------------------------===//
12*0b57cec5SDimitry Andric 
13*0b57cec5SDimitry Andric #define DONT_GET_PLUGIN_LOADER_OPTION
14*0b57cec5SDimitry Andric #include "llvm/Support/PluginLoader.h"
15*0b57cec5SDimitry Andric #include "llvm/Support/DynamicLibrary.h"
16*0b57cec5SDimitry Andric #include "llvm/Support/Mutex.h"
17*0b57cec5SDimitry Andric #include "llvm/Support/raw_ostream.h"
18*0b57cec5SDimitry Andric #include <vector>
19*0b57cec5SDimitry Andric using namespace llvm;
20*0b57cec5SDimitry Andric 
21*0b57cec5SDimitry Andric namespace {
22*0b57cec5SDimitry Andric 
23*0b57cec5SDimitry Andric struct Plugins {
24*0b57cec5SDimitry Andric   sys::SmartMutex<true> Lock;
25*0b57cec5SDimitry Andric   std::vector<std::string> List;
26*0b57cec5SDimitry Andric };
27*0b57cec5SDimitry Andric 
getPlugins()28*0b57cec5SDimitry Andric Plugins &getPlugins() {
29*0b57cec5SDimitry Andric   static Plugins P;
30*0b57cec5SDimitry Andric   return P;
31*0b57cec5SDimitry Andric }
32*0b57cec5SDimitry Andric 
33*0b57cec5SDimitry Andric } // anonymous namespace
34*0b57cec5SDimitry Andric 
operator =(const std::string & Filename)35*0b57cec5SDimitry Andric void PluginLoader::operator=(const std::string &Filename) {
36*0b57cec5SDimitry Andric   auto &P = getPlugins();
37*0b57cec5SDimitry Andric   sys::SmartScopedLock<true> Lock(P.Lock);
38*0b57cec5SDimitry Andric   std::string Error;
39*0b57cec5SDimitry Andric   if (sys::DynamicLibrary::LoadLibraryPermanently(Filename.c_str(), &Error)) {
40*0b57cec5SDimitry Andric     errs() << "Error opening '" << Filename << "': " << Error
41*0b57cec5SDimitry Andric            << "\n  -load request ignored.\n";
42*0b57cec5SDimitry Andric   } else {
43*0b57cec5SDimitry Andric     P.List.push_back(Filename);
44*0b57cec5SDimitry Andric   }
45*0b57cec5SDimitry Andric }
46*0b57cec5SDimitry Andric 
getNumPlugins()47 unsigned PluginLoader::getNumPlugins() {
48   auto &P = getPlugins();
49   sys::SmartScopedLock<true> Lock(P.Lock);
50   return P.List.size();
51 }
52 
getPlugin(unsigned num)53 std::string &PluginLoader::getPlugin(unsigned num) {
54   auto &P = getPlugins();
55   sys::SmartScopedLock<true> Lock(P.Lock);
56   assert(num < P.List.size() && "Asking for an out of bounds plugin");
57   return P.List[num];
58 }
59