The Gaudi Framework  v38r3 (c3fc9673)
listcomponents.cpp File Reference
#include <cstdlib>
#include <fstream>
#include <iostream>
#include <list>
#include <memory>
#include <set>
#include <string>
#include <dlfcn.h>
#include <getopt.h>
#include <Gaudi/PluginService.h>
#include <Gaudi/PluginServiceV1.h>
Include dependency graph for listcomponents.cpp:

Go to the source code of this file.

Macros

#define GAUDI_PLUGIN_SERVICE_V2
 

Functions

void help (std::string argv0)
 
void usage (std::string argv0)
 
int main (int argc, char *argv[])
 

Macro Definition Documentation

◆ GAUDI_PLUGIN_SERVICE_V2

#define GAUDI_PLUGIN_SERVICE_V2
Author
Marco Clemencic marco.nosp@m..cle.nosp@m.menci.nosp@m.c@ce.nosp@m.rn.ch

Definition at line 26 of file listcomponents.cpp.

Function Documentation

◆ help()

void help ( std::string  argv0)

Definition at line 29 of file listcomponents.cpp.

29  {
30  std::cout << "Usage: " << argv0
31  << " [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 }

◆ main()

int main ( int  argc,
char *  argv[] 
)

Definition at line 49 of file listcomponents.cpp.

49  {
50  auto& reg2 = Gaudi::PluginService::v2::Details::Registry::instance();
52 
53  using key_type = Gaudi::PluginService::v2::Details::Registry::KeyType;
54 
55  // cache to keep track of the loaded factories
57  // initialize the local cache
58  for ( const auto& name : reg2.loadedFactoryNames() ) loaded.emplace( name, "<preloaded>" );
59  for ( const auto& name : reg1.loadedFactoryNames() ) loaded.emplace( name, "<preloaded>" );
60 
61  // Parse command line
62  std::list<char*> libs;
63  std::string output_opt( "-" );
64  {
65  std::string argv0( argv[0] );
66  {
67  auto i = argv0.rfind( '/' );
68  if ( i != std::string::npos ) argv0 = argv0.substr( i + 1 );
69  }
70 
71  int i = 1;
72  while ( i < argc ) {
73  const std::string arg( argv[i] );
74  if ( arg == "-o" || arg == "--output" ) {
75  if ( ++i < argc ) {
76  output_opt = argv[i];
77  } else {
78  std::cerr << "ERROR: missing argument for option " << arg << std::endl;
79  std::cerr << "See `" << argv0 << " -h' for more details." << std::endl;
80  return EXIT_FAILURE;
81  }
82  } else if ( arg == "-h" || arg == "--help" ) {
83  help( argv0 );
84  return EXIT_SUCCESS;
85  } else {
86  libs.push_back( argv[i] );
87  }
88  ++i;
89  }
90  if ( libs.empty() ) {
91  usage( argv0 );
92  return EXIT_FAILURE;
93  }
94  }
95 
96  // handle output option
98  if ( output_opt != "-" ) { output_file.reset( new std::ofstream{ output_opt } ); }
99  std::ostream& output = ( output_file ? *output_file : std::cout );
100 
101  auto dump_from = [&output, &loaded]( const auto& reg, const char* lib, const char* prefix ) {
102  for ( const auto& factoryName : reg.loadedFactoryNames() ) {
103  // Skip if the component does not come from the same library we are processing
104  // (i.e. we found a symbol that is coming from a library loaded by the linker).
105  const auto& info = reg.factories().find( factoryName )->second;
106  if ( lib != info.library && !info.library.empty() && info.library != "unknown" ) {
107  std::cerr << "WARNING: library [" << lib << "] exposes factory [" << factoryName << "] which is declared in ["
108  << info.library << "] !!" << std::endl;
109  continue;
110  }
111  auto f = loaded.find( factoryName );
112  if ( f == loaded.end() ) {
113  output << prefix << "::" << lib << ":" << factoryName << std::endl;
114  loaded.emplace( factoryName, lib );
115  } else
116  std::cerr << "WARNING: factory '" << factoryName << "' already found in " << f->second << std::endl;
117  }
118  };
119 
120  // loop over the list of libraries passed on the command line
121  for ( const char* lib : libs ) {
122  if ( dlopen( lib, RTLD_LAZY | RTLD_LOCAL ) ) {
123  dump_from( reg2, lib, "v2" );
124  dump_from( reg1, lib, "v1" );
125  } else {
126  std::cerr << "ERROR: failed to load " << lib << ": " << dlerror() << std::endl;
127  return EXIT_FAILURE;
128  }
129  }
130  return EXIT_SUCCESS;
131 }

◆ usage()

void usage ( std::string  argv0)

Definition at line 41 of file listcomponents.cpp.

41  {
42  std::cout << "Usage: " << argv0
43  << " [option] library1 [library2 ...]\n"
44  "Try `"
45  << argv0 << " -h' for more information.\n"
46  << std::endl;
47 }
std::string
STL class.
std::list
STL class.
plotBacklogPyRoot.argc
argc
Definition: plotBacklogPyRoot.py:173
std::map::find
T find(T... args)
std::map::emplace
T emplace(T... args)
gaudirun.prefix
string prefix
Definition: gaudirun.py:361
gaudirun.output
output
Definition: gaudirun.py:521
help
void help(std::string argv0)
Definition: listcomponents.cpp:29
std::unique_ptr::reset
T reset(T... args)
Gaudi::PluginService::v1::Details::Registry::instance
static Registry & instance()
Retrieve the singleton instance of Registry.
Definition: PluginServiceV1.cpp:133
std::list::push_back
T push_back(T... args)
std::cout
std::ofstream
STL class.
std::map
STL class.
ConditionsStallTest.name
name
Definition: ConditionsStallTest.py:77
std::endl
T endl(T... args)
std::list::empty
T empty(T... args)
std::map::end
T end(T... args)
usage
void usage(std::string argv0)
Definition: listcomponents.cpp:41
std::unique_ptr
STL class.
gaudirun.argv
list argv
Definition: gaudirun.py:327