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