| The Gaudi Framework v30r0 (c919700c)

- 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 <cstdlib>
15 #include <fstream>
16 #include <iostream>
17 #include <list>
18 #include <memory>
19 #include <set>
20 #include <string>
21
22 #include <dlfcn.h>
23 #include <getopt.h>
24
25 #include <Gaudi/PluginService.h>
26
27 void help( std::string argv0 )
28 {
29 std::cout << "Usage: " << argv0 << " [option] library1 [library2 ...]\n"
30 "\n list the component factories present in the given libraries\n\n"
31 "Options:\n\n"
32 " -h, --help show this help message and exit\n"
33 " -o OUTPUT, --output OUTPUT\n"
34 " write the list of factories on the file OUTPUT, use - for\n"
35 " standard output (default)\n"
36 << std::endl;
37 }
38
39 void usage( std::string argv0 )
40 {
41 std::cout << "Usage: " << argv0 << " [option] library1 [library2 ...]\n"
42 "Try `"
43 << argv0 << " -h' for more information.\n"
44 << std::endl;
45 }
46
47 int main( int argc, char* argv[] )
48 {
49 Gaudi::PluginService::Details::Registry& reg = Gaudi::PluginService::Details::Registry::instance();
50 typedef Gaudi::PluginService::Details::Registry::KeyType key_type;
51
52 // cache to keep track of the loaded factories
53 std::map<key_type, std::string> loaded;
54 // initialize the local cache
55 for ( const auto& name : reg.loadedFactoryNames() ) loaded.emplace( name, "<preloaded>" );
56
57 // Parse command line
58 std::list<char*> libs;
59 std::string output_opt( "-" );
60 {
61 std::string argv0( argv[0] );
62 {
63 auto i = argv0.rfind( '/' );
64 if ( i != std::string::npos ) argv0 = argv0.substr( i + 1 );
65 }
66
67 int i = 1;
68 while ( i < argc ) {
69 const std::string arg( argv[i] );
70 if ( arg == "-o" || arg == "--output" ) {
71 if ( ++i < argc ) {
72 output_opt = argv[i];
73 } else {
74 std::cerr << "ERROR: missing argument for option " << arg << std::endl;
75 std::cerr << "See `" << argv0 << " -h' for more details." << std::endl;
76 return EXIT_FAILURE;
77 }
78 } else if ( arg == "-h" || arg == "--help" ) {
79 help( argv0 );
80 return EXIT_SUCCESS;
81 } else {
82 libs.push_back( argv[i] );
83 }
84 ++i;
85 }
86 if ( libs.empty() ) {
87 usage( argv0 );
88 return EXIT_FAILURE;
89 }
90 }
91
92 // handle output option
93 std::unique_ptr<std::ostream> output_file;
94 if ( output_opt != "-" ) {
95 output_file.reset( new std::ofstream{output_opt} );
96 }
97 std::ostream& output = ( output_file ? *output_file : std::cout );
98
99 // loop over the list of libraries passed on the command line
100 for ( char* lib : libs ) {
101 if ( dlopen( lib, RTLD_LAZY | RTLD_LOCAL ) ) {
102 for ( const auto& factoryName : reg.loadedFactoryNames() ) {
103 auto f = loaded.find( factoryName );
104 if ( f == loaded.end() ) {
105 output << lib << ":" << factoryName << std::endl;
106 loaded.emplace( factoryName, lib );
107 } else
108 std::cerr << "WARNING: factory '" << factoryName << "' already found in " << f->second << std::endl;
109 }
110 } else {
111 std::cerr << "ERROR: failed to load " << lib << ": " << dlerror() << std::endl;
112 return EXIT_FAILURE;
113 }
114 }
115 return EXIT_SUCCESS;
116 }
std::list::empty
T empty(T...args)
gaudiComponentHelp.name
name
Definition: gaudiComponentHelp.py:32
gaudirun.output
output
Definition: gaudirun.py:404
gaudirun.argv
list argv
Definition: gaudirun.py:235
std::string::rfind
T rfind(T...args)
std::endl
T endl(T...args)
usage
void usage(std::string argv0)
Definition: listcomponents.cpp:39
std::map::end
T end(T...args)
std::map
STL class.
std::string
STL class.
main
int main(int argc, char *argv[])
Definition: listcomponents.cpp:47
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:113
std::list
STL class.
plotBacklogPyRoot.argc
argc
Definition: plotBacklogPyRoot.py:138
PluginService.h
std::map::find
T find(T...args)
std::unique_ptr
STL class.
Gaudi::PluginService::Details::Registry::instance
static Registry & instance()
Retrieve the singleton instance of Registry.
Definition: PluginService.cpp:148
std::map::emplace
T emplace(T...args)
Gaudi::PluginService::Details::Registry
In-memory database of the loaded factories.
Definition: PluginServiceDetails.h:110
std::string::substr
T substr(T...args)
Gaudi::PluginService::Details::Registry::loadedFactoryNames
std::set< KeyType > loadedFactoryNames() const
Return a list of all the known and loaded factories.
Definition: PluginService.cpp:320
std::cout