| The Gaudi Framework v28r2p1 (f1a77ff4)

- Main Page
- Related Pages
- Modules
- Namespaces
- Classes
- Files

- File List
- File Members

listcomponents.cpp
Go to the documentation of this file.
1 /*****************************************************************************\
2 * (c) Copyright 2013 CERN *
3 * *
4 * This software is distributed under the terms of the GNU General Public *
5 * Licence version 3 (GPL Version 3), copied verbatim in the file "LICENCE". *
6 * *
7 * In applying this licence, CERN does not waive the privileges and immunities *
8 * granted to it by virtue of its status as an Intergovernmental Organization *
9 * or submit itself to any jurisdiction. *
10 \*****************************************************************************/
11
13
14 #include <iostream>
15 #include <fstream>
16 #include <string>
17 #include <set>
18 #include <list>
19 #include <memory>
20 #include <cstdlib>
21
22 #include <getopt.h>
23 #include <dlfcn.h>
24
25 #include <Gaudi/PluginService.h>
26
27 void help(std::string argv0) {
28 std::cout << "Usage: " << argv0 << " [option] library1 [library2 ...]\n"
29 "\n list the component factories present in the given libraries\n\n"
30 "Options:\n\n"
31 " -h, --help show this help message and exit\n"
32 " -o OUTPUT, --output OUTPUT\n"
33 " write the list of factories on the file OUTPUT, use - for\n"
34 " standard output (default)\n"
35 << std::endl;
36 }
37
38 void usage(std::string argv0) {
39 std::cout << "Usage: " << argv0 << " [option] library1 [library2 ...]\n"
40 "Try `" << argv0 << " -h' for more information.\n"
41 << std::endl;
42 }
43
44 int main(int argc, char* argv[]) {
45 Gaudi::PluginService::Details::Registry &reg =
46 Gaudi::PluginService::Details::Registry::instance();
47 typedef Gaudi::PluginService::Details::Registry::KeyType key_type;
48
49 // cache to keep track of the loaded factories
50 std::map<key_type, std::string> loaded;
51 // initialize the local cache
52 for (const auto & elem : reg.loadedFactories()) loaded.emplace(elem, "<preloaded>");
53
54 // Parse command line
55 std::list<char*> libs;
56 std::string output_opt("-");
57 {
58 std::string argv0(argv[0]);
59 {
60 auto i = argv0.rfind('/');
61 if (i != std::string::npos)
62 argv0 = argv0.substr(i+1);
63 }
64
65 int i = 1;
66 while (i < argc) {
67 const std::string arg(argv[i]);
68 if (arg == "-o" || arg == "--output") {
69 if (++i < argc) {
70 output_opt = argv[i];
71 } else {
72 std::cerr << "ERROR: missing argument for option " << arg << std::endl;
73 std::cerr << "See `" << argv0 << " -h' for more details." << std::endl;
74 return EXIT_FAILURE;
75 }
76 } else if (arg == "-h" || arg == "--help") {
77 help(argv0);
78 return EXIT_SUCCESS;
79 } else {
80 libs.push_back(argv[i]);
81 }
82 ++i;
83 }
84 if (libs.empty()) {
85 usage(argv0);
86 return EXIT_FAILURE;
87 }
88 }
89
90 // handle output option
91 std::unique_ptr<std::ostream> output_file;
92 if (output_opt != "-") {
93 output_file.reset( new std::ofstream{output_opt} );
94 }
95 std::ostream &output = (output_file ? *output_file : std::cout);
96
97 // loop over the list of libraries passed on the command line
98 for (char* lib: libs) {
99 if (dlopen(lib, RTLD_LAZY | RTLD_LOCAL)) {
100 for (const auto & factory : reg.loadedFactories()) {
101 auto f = loaded.find(factory);
102 if ( f == loaded.end()) {
103 output << lib << ":" << factory << std::endl;
104 loaded.emplace(factory,lib);
105 } else
106 std::cerr << "WARNING: factory '" << factory
107 << "' already found in " << f->second
108 << std::endl;
109 }
110 } else {
111 std::cerr << "ERROR: failed to load " << lib
112 << ": " << dlerror() << std::endl;
113 return EXIT_FAILURE;
114 }
115 }
116 return EXIT_SUCCESS;
117 }
std::list::empty
T empty(T...args)
gaudirun.output
output
Definition: gaudirun.py:388
gaudirun.argv
list argv
Definition: gaudirun.py:227
std::string::rfind
T rfind(T...args)
std::endl
T endl(T...args)
usage
void usage(std::string argv0)
Definition: listcomponents.cpp:38
std::map::end
T end(T...args)
std::map
STL class.
Gaudi::PluginService::Details::Registry::loadedFactories
std::set< KeyType > loadedFactories() const
Return a list of all the known and loaded factories.
Definition: PluginService.cpp:322
std::string
STL class.
main
int main(int argc, char *argv[])
Definition: listcomponents.cpp:44
std::list::push_back
T push_back(T...args)
std::ofstream
STL class.
help
void help(std::string argv0)
Definition: listcomponents.cpp:27
std::unique_ptr::reset
T reset(T...args)
Gaudi::PluginService::Details::Registry::KeyType
std::string KeyType
Definition: PluginServiceDetails.h:101
std::list
STL class.
plotBacklogPyRoot.argc
argc
Definition: plotBacklogPyRoot.py:128
PluginService.h
std::map::find
T find(T...args)
std::unique_ptr< std::ostream >
Gaudi::PluginService::Details::Registry::instance
static Registry & instance()
Retrieve the singleton instance of Registry.
Definition: PluginService.cpp:145
std::map::emplace
T emplace(T...args)
Gaudi::PluginService::Details::Registry
In-memory database of the loaded factories.
Definition: PluginServiceDetails.h:99
std::string::substr
T substr(T...args)
std::cout