DD4hep  1.30.0
Detector Description Toolkit for High Energy Physics
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
27 #include <Gaudi/PluginServiceV1.h>
28 
29 void help( std::string argv0 ) {
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 }
40 
41 void usage( std::string argv0 ) {
42  std::cout << "Usage: " << argv0
43  << " [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  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
56  std::map<key_type, std::string> loaded;
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( std::move(argv0) );
92  return EXIT_FAILURE;
93  }
94  }
95 
96  // handle output option
97  std::unique_ptr<std::ostream> output_file;
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]( auto& reg, const char* lib, const char* prefix ) {
102  for ( const auto& factoryName : reg.loadedFactoryNames() ) {
103  auto f = loaded.find( factoryName );
104  if ( f == loaded.end() ) {
105  output << prefix << "::" << 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  };
111 
112  // loop over the list of libraries passed on the command line
113  for ( const char* aLib : libs ) {
114  if ( dlopen( aLib, RTLD_LAZY | RTLD_LOCAL ) ) {
115  dump_from( reg2, aLib, "v2" );
116  dump_from( reg1, aLib, "v1" );
117  } else {
118  std::cerr << "ERROR: failed to load " << aLib << ": " << dlerror() << std::endl;
119  return EXIT_FAILURE;
120  }
121  }
122  return EXIT_SUCCESS;
123 }
PluginServiceV1.h
help
void help(std::string argv0)
Definition: listcomponents.cpp:29
PluginService.h
main
int main(int argc, char *argv[])
Definition: listcomponents.cpp:49
usage
void usage(std::string argv0)
Definition: listcomponents.cpp:41
Gaudi::PluginService::v1::Details::Registry::instance
static Registry & instance()
Retrieve the singleton instance of Registry.
Definition: PluginServiceV1.cpp:136