|
15 | 15 | /// |
16 | 16 | //===----------------------------------------------------------------------===// |
17 | 17 |
|
| 18 | +#include "llvm/ADT/StringRef.h" |
18 | 19 | #include "llvm/Debuginfod/Debuginfod.h" |
19 | 20 | #include "llvm/Debuginfod/HTTPClient.h" |
| 21 | +#include "llvm/Option/ArgList.h" |
| 22 | +#include "llvm/Option/Option.h" |
20 | 23 | #include "llvm/Support/CommandLine.h" |
21 | 24 | #include "llvm/Support/InitLLVM.h" |
22 | 25 | #include "llvm/Support/ThreadPool.h" |
23 | 26 |
|
24 | 27 | using namespace llvm; |
25 | 28 |
|
26 | | -cl::OptionCategory DebuginfodCategory("llvm-debuginfod Options"); |
27 | | - |
28 | | -static cl::list<std::string> ScanPaths(cl::Positional, |
29 | | - cl::desc("<Directories to scan>"), |
30 | | - cl::cat(DebuginfodCategory)); |
31 | | - |
32 | | -static cl::opt<unsigned> |
33 | | - Port("p", cl::init(0), |
34 | | - cl::desc("Port to listen on. Set to 0 to bind to any available port."), |
35 | | - cl::cat(DebuginfodCategory)); |
36 | | - |
37 | | -static cl::opt<std::string> |
38 | | - HostInterface("i", cl::init("0.0.0.0"), |
39 | | - cl::desc("Host interface to bind to."), |
40 | | - cl::cat(DebuginfodCategory)); |
41 | | - |
42 | | -static cl::opt<int> |
43 | | - ScanInterval("t", cl::init(300), |
44 | | - cl::desc("Number of seconds to wait between subsequent " |
45 | | - "automated scans of the filesystem."), |
46 | | - cl::cat(DebuginfodCategory)); |
47 | | - |
48 | | -static cl::opt<double> MinInterval( |
49 | | - "m", cl::init(10), |
50 | | - cl::desc( |
51 | | - "Minimum number of seconds to wait before an on-demand update can be " |
52 | | - "triggered by a request for a buildid which is not in the collection."), |
53 | | - cl::cat(DebuginfodCategory)); |
54 | | - |
55 | | -static cl::opt<size_t> |
56 | | - MaxConcurrency("c", cl::init(0), |
57 | | - cl::desc("Maximum number of files to scan concurrently. If " |
58 | | - "0, use the hardware concurrency."), |
59 | | - cl::cat(DebuginfodCategory)); |
60 | | - |
61 | | -static cl::opt<bool> VerboseLogging("v", cl::init(false), |
62 | | - cl::desc("Enable verbose logging."), |
63 | | - cl::cat(DebuginfodCategory)); |
| 29 | +// Command-line option boilerplate. |
| 30 | +namespace { |
| 31 | +enum ID { |
| 32 | + OPT_INVALID = 0, // This is not an option ID. |
| 33 | +#define OPTION(PREFIX, NAME, ID, KIND, GROUP, ALIAS, ALIASARGS, FLAGS, PARAM, \ |
| 34 | + HELPTEXT, METAVAR, VALUES) \ |
| 35 | + OPT_##ID, |
| 36 | +#include "Opts.inc" |
| 37 | +#undef OPTION |
| 38 | +}; |
| 39 | + |
| 40 | +#define PREFIX(NAME, VALUE) \ |
| 41 | + static constexpr StringLiteral NAME##_init[] = VALUE; \ |
| 42 | + static constexpr ArrayRef<StringLiteral> NAME(NAME##_init, \ |
| 43 | + std::size(NAME##_init) - 1); |
| 44 | +#include "Opts.inc" |
| 45 | +#undef PREFIX |
| 46 | + |
| 47 | +static constexpr opt::OptTable::Info InfoTable[] = { |
| 48 | +#define OPTION(PREFIX, NAME, ID, KIND, GROUP, ALIAS, ALIASARGS, FLAGS, PARAM, \ |
| 49 | + HELPTEXT, METAVAR, VALUES) \ |
| 50 | + { \ |
| 51 | + PREFIX, NAME, HELPTEXT, \ |
| 52 | + METAVAR, OPT_##ID, opt::Option::KIND##Class, \ |
| 53 | + PARAM, FLAGS, OPT_##GROUP, \ |
| 54 | + OPT_##ALIAS, ALIASARGS, VALUES}, |
| 55 | +#include "Opts.inc" |
| 56 | +#undef OPTION |
| 57 | +}; |
| 58 | + |
| 59 | +class DebuginfodOptTable : public opt::GenericOptTable { |
| 60 | +public: |
| 61 | + DebuginfodOptTable() : GenericOptTable(InfoTable) {} |
| 62 | +}; |
| 63 | +} // end anonymous namespace |
| 64 | + |
| 65 | +// Options |
| 66 | +static unsigned Port; |
| 67 | +static std::string HostInterface; |
| 68 | +static int ScanInterval; |
| 69 | +static double MinInterval; |
| 70 | +static size_t MaxConcurrency; |
| 71 | +static bool VerboseLogging; |
| 72 | +static std::vector<std::string> ScanPaths; |
64 | 73 |
|
65 | 74 | ExitOnError ExitOnErr; |
66 | 75 |
|
| 76 | +template <typename T> |
| 77 | +static void parseIntArg(const opt::InputArgList &Args, int ID, T &Value, |
| 78 | + T Default) { |
| 79 | + if (const opt::Arg *A = Args.getLastArg(ID)) { |
| 80 | + StringRef V(A->getValue()); |
| 81 | + if (!llvm::to_integer(V, Value, 0)) { |
| 82 | + errs() << A->getSpelling() + ": expected an integer, but got '" + V + "'"; |
| 83 | + exit(1); |
| 84 | + } |
| 85 | + } else { |
| 86 | + Value = Default; |
| 87 | + } |
| 88 | +} |
| 89 | + |
| 90 | +static void parseArgs(int argc, char **argv) { |
| 91 | + DebuginfodOptTable Tbl; |
| 92 | + llvm::StringRef ToolName = argv[0]; |
| 93 | + llvm::BumpPtrAllocator A; |
| 94 | + llvm::StringSaver Saver{A}; |
| 95 | + opt::InputArgList Args = |
| 96 | + Tbl.parseArgs(argc, argv, OPT_UNKNOWN, Saver, [&](StringRef Msg) { |
| 97 | + llvm::errs() << Msg << '\n'; |
| 98 | + std::exit(1); |
| 99 | + }); |
| 100 | + |
| 101 | + if (Args.hasArg(OPT_help)) { |
| 102 | + Tbl.printHelp(llvm::outs(), |
| 103 | + "llvm-debuginfod [options] <Directories to scan>", |
| 104 | + ToolName.str().c_str()); |
| 105 | + std::exit(0); |
| 106 | + } |
| 107 | + |
| 108 | + VerboseLogging = Args.hasArg(OPT_verbose_logging); |
| 109 | + ScanPaths = Args.getAllArgValues(OPT_INPUT); |
| 110 | + |
| 111 | + parseIntArg(Args, OPT_port, Port, 0u); |
| 112 | + parseIntArg(Args, OPT_scan_interval, ScanInterval, 300); |
| 113 | + parseIntArg(Args, OPT_max_concurrency, MaxConcurrency, 0ul); |
| 114 | + |
| 115 | + if (const opt::Arg *A = Args.getLastArg(OPT_min_interval)) { |
| 116 | + StringRef V(A->getValue()); |
| 117 | + if (!llvm::to_float(V, MinInterval)) { |
| 118 | + errs() << A->getSpelling() + ": expected a number, but got '" + V + "'"; |
| 119 | + exit(1); |
| 120 | + } |
| 121 | + } else { |
| 122 | + MinInterval = 10.0; |
| 123 | + } |
| 124 | + |
| 125 | + HostInterface = Args.getLastArgValue(OPT_host_interface, "0.0.0.0"); |
| 126 | +} |
| 127 | + |
67 | 128 | int main(int argc, char **argv) { |
68 | 129 | InitLLVM X(argc, argv); |
69 | 130 | HTTPClient::initialize(); |
70 | | - cl::HideUnrelatedOptions({&DebuginfodCategory}); |
71 | | - cl::ParseCommandLineOptions(argc, argv); |
| 131 | + parseArgs(argc, argv); |
72 | 132 |
|
73 | 133 | SmallVector<StringRef, 1> Paths; |
74 | 134 | for (const std::string &Path : ScanPaths) |
|
0 commit comments