The Gaudi Framework  v30r3 (a5ef0a68)
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 #define GAUDI_PLUGIN_SERVICE_V2
26 #include <Gaudi/PluginService.h>
27 #include <Gaudi/PluginServiceV1.h>
28 
29 void help( std::string argv0 )
30 {
31  std::cout << "Usage: " << argv0 << " [option] library1 [library2 ...]\n"
32  "\n list the component factories present in the given libraries\n\n"
33  "Options:\n\n"
34  " -h, --help show this help message and exit\n"
35  " -o OUTPUT, --output OUTPUT\n"
36  " write the list of factories on the file OUTPUT, use - for\n"
37  " standard output (default)\n"
38  << std::endl;
39 }
40 
41 void usage( std::string argv0 )
42 {
43  std::cout << "Usage: " << argv0 << " [option] library1 [library2 ...]\n"
44  "Try `"
45  << argv0 << " -h' for more information.\n"
46  << std::endl;
47 }
48 
49 int main( int argc, char* argv[] )
50 {
51  auto& reg2 = Gaudi::PluginService::v2::Details::Registry::instance();
53 
54  using key_type = Gaudi::PluginService::v2::Details::Registry::KeyType;
55 
56  // cache to keep track of the loaded factories
58  // initialize the local cache
59  for ( const auto& name : reg2.loadedFactoryNames() ) loaded.emplace( name, "<preloaded>" );
60  for ( const auto& name : reg1.loadedFactoryNames() ) loaded.emplace( name, "<preloaded>" );
61 
62  // Parse command line
63  std::list<char*> libs;
64  std::string output_opt( "-" );
65  {
66  std::string argv0( argv[0] );
67  {
68  auto i = argv0.rfind( '/' );
69  if ( i != std::string::npos ) argv0 = argv0.substr( i + 1 );
70  }
71 
72  int i = 1;
73  while ( i < argc ) {
74  const std::string arg( argv[i] );
75  if ( arg == "-o" || arg == "--output" ) {
76  if ( ++i < argc ) {
77  output_opt = argv[i];
78  } else {
79  std::cerr << "ERROR: missing argument for option " << arg << std::endl;
80  std::cerr << "See `" << argv0 << " -h' for more details." << std::endl;
81  return EXIT_FAILURE;
82  }
83  } else if ( arg == "-h" || arg == "--help" ) {
84  help( argv0 );
85  return EXIT_SUCCESS;
86  } else {
87  libs.push_back( argv[i] );
88  }
89  ++i;
90  }
91  if ( libs.empty() ) {
92  usage( argv0 );
93  return EXIT_FAILURE;
94  }
95  }
96 
97  // handle output option
99  if ( output_opt != "-" ) {
100  output_file.reset( new std::ofstream{output_opt} );
101  }
102  std::ostream& output = ( output_file ? *output_file : std::cout );
103 
104  auto dump_from = [&output, &loaded]( auto& reg, const char* lib, const char* prefix ) {
105  for ( const auto& factoryName : reg.loadedFactoryNames() ) {
106  auto f = loaded.find( factoryName );
107  if ( f == loaded.end() ) {
108  output << prefix << "::" << lib << ":" << factoryName << std::endl;
109  loaded.emplace( factoryName, lib );
110  } else
111  std::cerr << "WARNING: factory '" << factoryName << "' already found in " << f->second << std::endl;
112  }
113  };
114 
115  // loop over the list of libraries passed on the command line
116  for ( const char* lib : libs ) {
117  if ( dlopen( lib, RTLD_LAZY | RTLD_LOCAL ) ) {
118  dump_from( reg2, lib, "v2" );
119  dump_from( reg1, lib, "v1" );
120  } else {
121  std::cerr << "ERROR: failed to load " << lib << ": " << dlerror() << std::endl;
122  return EXIT_FAILURE;
123  }
124  }
125  return EXIT_SUCCESS;
126 }
T empty(T...args)
static Registry & instance()
Retrieve the singleton instance of Registry.
list argv
Definition: gaudirun.py:235
T rfind(T...args)
T endl(T...args)
void usage(std::string argv0)
T end(T...args)
STL class.
STL class.
int main(int argc, char *argv[])
T push_back(T...args)
STL class.
void help(std::string argv0)
string prefix
Definition: gaudirun.py:268
T reset(T...args)
STL class.
T find(T...args)
STL class.
T emplace(T...args)
T substr(T...args)