All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros Groups Pages
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 <iostream>
15 #include <fstream>
16 #include <string>
17 #include <set>
18 #include <list>
19 #include <memory>
20 #include <cstdlib>
21 
22 #include <getopt.h>
23 #include <dlfcn.h>
24 
25 #include <Gaudi/PluginService.h>
26 
27 void help(std::string argv0) {
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 }
37 
38 void usage(std::string argv0) {
39  std::cout << "Usage: " << argv0 << " [option] library1 [library2 ...]\n"
40  "Try `" << argv0 << " -h' for more information.\n"
41  << std::endl;
42 }
43 
44 int main(int argc, char* argv[]) {
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.
int main(int argc, char *argv[])
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