All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros Groups Pages
listcomponents.cpp File Reference
#include <iostream>
#include <fstream>
#include <string>
#include <set>
#include <list>
#include <memory>
#include <cstdlib>
#include <getopt.h>
#include <dlfcn.h>
#include <Gaudi/PluginService.h>
Include dependency graph for listcomponents.cpp:

Go to the source code of this file.

Functions

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

Function Documentation

void help ( std::string  argv0)
Author
Marco Clemencic marco.nosp@m..cle.nosp@m.menci.nosp@m.c@ce.nosp@m.rn.ch

Definition at line 27 of file listcomponents.cpp.

27  {
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 }
int main ( int  argc,
char *  argv[] 
)

Definition at line 44 of file listcomponents.cpp.

44  {
48 
49  // cache to keep track of the loaded factories
50  std::map<key_type, std::string> loaded;
51  {
52  // initialize the local cache
53  std::set<key_type> base = reg.loadedFactories();
54  for (std::set<key_type>::const_iterator f = base.begin(); f != base.end(); ++f)
55  {
56  loaded.insert(std::make_pair(*f, std::string("<preloaded>")));
57  }
58  }
59 
60  // Parse command line
61  std::list<char*> libs;
62  std::string output_opt("-");
63  {
64  std::string argv0(argv[0]);
65  {
66  auto i = argv0.rfind('/');
67  if (i != std::string::npos)
68  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
97  std::unique_ptr<std::ostream> output_file;
98  if (output_opt != "-") {
99  output_file = std::unique_ptr<std::ostream>(new std::ofstream(output_opt.c_str()));
100  }
101  std::ostream &output = (output_file ? *output_file : std::cout);
102 
103  // loop over the list of libraries passed on the command line
104  for (char* lib: libs) {
105 
106  if (dlopen(lib, RTLD_LAZY | RTLD_LOCAL)) {
107 
108  std::set<key_type> factories = reg.loadedFactories();
109  std::set<key_type>::const_iterator f;
110  for (f = factories.begin(); f != factories.end(); ++f) {
111  if (loaded.find(*f) == loaded.end())
112  {
113  output << lib << ":" << *f << std::endl;
114  loaded[*f] = lib;
115  }
116  else
117  std::cerr << "WARNING: factory '" << *f
118  << "' already found in " << loaded[*f]
119  << std::endl;
120  }
121 
122  } else {
123  std::cerr << "ERROR: failed to load " << lib << std::endl;
124  return EXIT_FAILURE;
125  }
126  }
127 
128  return EXIT_SUCCESS;
129 }
GAUDI_API long argc()
Number of arguments passed to the commandline (==numCmdLineArgs()); just to match argv call...
Definition: System.cpp:526
list argv
Definition: gaudirun.py:192
void usage(std::string argv0)
std::set< KeyType > loadedFactories() const
Return a list of all the known and loaded factories.
void help(std::string argv0)
static Registry & instance()
Retrieve the singleton instance of Registry.
In-memory database of the loaded factories.
list i
Definition: ana.py:128
void usage ( std::string  argv0)

Definition at line 38 of file listcomponents.cpp.

38  {
39  std::cout << "Usage: " << argv0 << " [option] library1 [library2 ...]\n"
40  "Try `" << argv0 << " -h' for more information.\n"
41  << std::endl;
42 }