1 //===-- PluginLoader.cpp - Implement -load command line option ------------===//
3 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4 // See https://llvm.org/LICENSE.txt for license information.
5 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
7 //===----------------------------------------------------------------------===//
9 // This file implements the -load <plugin> command line option handler.
11 //===----------------------------------------------------------------------===//
13 #define DONT_GET_PLUGIN_LOADER_OPTION
14 #include "llvm/Support/PluginLoader.h"
15 #include "llvm/Support/DynamicLibrary.h"
16 #include "llvm/Support/Mutex.h"
17 #include "llvm/Support/raw_ostream.h"
24 sys::SmartMutex
<true> Lock
;
25 std::vector
<std::string
> List
;
28 Plugins
&getPlugins() {
33 } // anonymous namespace
35 void PluginLoader::operator=(const std::string
&Filename
) {
36 auto &P
= getPlugins();
37 sys::SmartScopedLock
<true> Lock(P
.Lock
);
39 if (sys::DynamicLibrary::LoadLibraryPermanently(Filename
.c_str(), &Error
)) {
40 errs() << "Error opening '" << Filename
<< "': " << Error
41 << "\n -load request ignored.\n";
43 P
.List
.push_back(Filename
);
47 unsigned PluginLoader::getNumPlugins() {
48 auto &P
= getPlugins();
49 sys::SmartScopedLock
<true> Lock(P
.Lock
);
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");