xref: /src/contrib/llvm-project/llvm/lib/Support/PluginLoader.cpp (revision bdd1243df58e60e85101c09001d9812a789b6bc4)
1009b1c42SEd Schouten //===-- PluginLoader.cpp - Implement -load command line option ------------===//
2009b1c42SEd Schouten //
3e6d15924SDimitry Andric // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4e6d15924SDimitry Andric // See https://llvm.org/LICENSE.txt for license information.
5e6d15924SDimitry Andric // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6009b1c42SEd Schouten //
7009b1c42SEd Schouten //===----------------------------------------------------------------------===//
8009b1c42SEd Schouten //
9009b1c42SEd Schouten // This file implements the -load <plugin> command line option handler.
10009b1c42SEd Schouten //
11009b1c42SEd Schouten //===----------------------------------------------------------------------===//
12009b1c42SEd Schouten 
13009b1c42SEd Schouten #define DONT_GET_PLUGIN_LOADER_OPTION
14009b1c42SEd Schouten #include "llvm/Support/PluginLoader.h"
15cf099d11SDimitry Andric #include "llvm/Support/DynamicLibrary.h"
16cf099d11SDimitry Andric #include "llvm/Support/Mutex.h"
174a16efa3SDimitry Andric #include "llvm/Support/raw_ostream.h"
18009b1c42SEd Schouten #include <vector>
19009b1c42SEd Schouten using namespace llvm;
20009b1c42SEd Schouten 
21e3b55780SDimitry Andric namespace {
22e3b55780SDimitry Andric 
23e3b55780SDimitry Andric struct Plugins {
24e3b55780SDimitry Andric   sys::SmartMutex<true> Lock;
25e3b55780SDimitry Andric   std::vector<std::string> List;
26e3b55780SDimitry Andric };
27e3b55780SDimitry Andric 
getPlugins()28e3b55780SDimitry Andric Plugins &getPlugins() {
29e3b55780SDimitry Andric   static Plugins P;
30e3b55780SDimitry Andric   return P;
31e3b55780SDimitry Andric }
32e3b55780SDimitry Andric 
33e3b55780SDimitry Andric } // anonymous namespace
34009b1c42SEd Schouten 
operator =(const std::string & Filename)35009b1c42SEd Schouten void PluginLoader::operator=(const std::string &Filename) {
36e3b55780SDimitry Andric   auto &P = getPlugins();
37e3b55780SDimitry Andric   sys::SmartScopedLock<true> Lock(P.Lock);
38009b1c42SEd Schouten   std::string Error;
39009b1c42SEd Schouten   if (sys::DynamicLibrary::LoadLibraryPermanently(Filename.c_str(), &Error)) {
4059850d08SRoman Divacky     errs() << "Error opening '" << Filename << "': " << Error
41009b1c42SEd Schouten            << "\n  -load request ignored.\n";
42009b1c42SEd Schouten   } else {
43e3b55780SDimitry Andric     P.List.push_back(Filename);
44009b1c42SEd Schouten   }
45009b1c42SEd Schouten }
46009b1c42SEd Schouten 
getNumPlugins()47009b1c42SEd Schouten unsigned PluginLoader::getNumPlugins() {
48e3b55780SDimitry Andric   auto &P = getPlugins();
49e3b55780SDimitry Andric   sys::SmartScopedLock<true> Lock(P.Lock);
50e3b55780SDimitry Andric   return P.List.size();
51009b1c42SEd Schouten }
52009b1c42SEd Schouten 
getPlugin(unsigned num)53009b1c42SEd Schouten std::string &PluginLoader::getPlugin(unsigned num) {
54e3b55780SDimitry Andric   auto &P = getPlugins();
55e3b55780SDimitry Andric   sys::SmartScopedLock<true> Lock(P.Lock);
56e3b55780SDimitry Andric   assert(num < P.List.size() && "Asking for an out of bounds plugin");
57e3b55780SDimitry Andric   return P.List[num];
58009b1c42SEd Schouten }
59