Skip to content

Commit 1610627

Browse files
Andrés Villegasmysterymath
authored andcommitted
[llvm-debuginfod][NFC] Switch to OptTable
Reviewed By: mysterymath Differential Revision: https://reviews.llvm.org/D151273
1 parent a2684ac commit 1610627

4 files changed

Lines changed: 136 additions & 40 deletions

File tree

llvm/tools/llvm-debuginfod/CMakeLists.txt

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,16 @@
11
set(LLVM_LINK_COMPONENTS
2+
Option
23
Support
34
)
5+
set(LLVM_TARGET_DEFINITIONS Opts.td)
6+
tablegen(LLVM Opts.inc -gen-opt-parser-defs)
7+
add_public_tablegen_target(DebugInfodOptsTableGen)
8+
49
add_llvm_tool(llvm-debuginfod
510
llvm-debuginfod.cpp
11+
12+
DEPENDS
13+
DebugInfodOptsTableGen
614
)
715
target_link_libraries(llvm-debuginfod PRIVATE LLVMDebuginfod)
816
if(LLVM_INSTALL_BINUTILS_SYMLINKS)

llvm/tools/llvm-debuginfod/Opts.td

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
include "llvm/Option/OptParser.td"
2+
3+
class F<string name, string help> : Flag<["-"], name>, HelpText<help>;
4+
class FF<string name, string help>: Flag<["--"], name>, HelpText<help>;
5+
class S<string name, string meta, string help>: Separate<["-"], name>, HelpText<help>, MetaVarName<meta>;
6+
7+
def help : FF<"help", "Display available options">;
8+
def : F<"h", "Alias for --help">, Alias<help>;
9+
def max_concurrency :
10+
S<"c", "<ulong>", "Maximum number of files to scan concurrently. "
11+
"If 0, use the hardware concurrency.">;
12+
def host_interface : S<"i", "<string>", "Host interface to bind to.">;
13+
def min_interval :
14+
S<"m", "<number>", "Minimum number of seconds to wait before an on-demand update can be"
15+
"triggered by a request for a buildid which is not in the collection.">;
16+
def port : S<"p", "<uint>", "Port to listen on. Set to 0 to bind to any available port.">;
17+
def scan_interval :
18+
S<"t", "<int>", "Number of seconds to wait between subsequent "
19+
"automated scans of the filesystem.">;
20+
def verbose_logging : F<"v", "Enable verbose logging.">;

llvm/tools/llvm-debuginfod/llvm-debuginfod.cpp

Lines changed: 100 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -15,60 +15,120 @@
1515
///
1616
//===----------------------------------------------------------------------===//
1717

18+
#include "llvm/ADT/StringRef.h"
1819
#include "llvm/Debuginfod/Debuginfod.h"
1920
#include "llvm/Debuginfod/HTTPClient.h"
21+
#include "llvm/Option/ArgList.h"
22+
#include "llvm/Option/Option.h"
2023
#include "llvm/Support/CommandLine.h"
2124
#include "llvm/Support/InitLLVM.h"
2225
#include "llvm/Support/ThreadPool.h"
2326

2427
using namespace llvm;
2528

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;
6473

6574
ExitOnError ExitOnErr;
6675

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+
67128
int main(int argc, char **argv) {
68129
InitLLVM X(argc, argv);
69130
HTTPClient::initialize();
70-
cl::HideUnrelatedOptions({&DebuginfodCategory});
71-
cl::ParseCommandLineOptions(argc, argv);
131+
parseArgs(argc, argv);
72132

73133
SmallVector<StringRef, 1> Paths;
74134
for (const std::string &Path : ScanPaths)

llvm/utils/gn/secondary/llvm/tools/llvm-debuginfod/BUILD.gn

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,12 @@
11
import("//llvm/tools/binutils_symlinks.gni")
2+
import("//llvm/utils/TableGen/tablegen.gni")
23
import("//llvm/utils/gn/build/symlink_or_copy.gni")
34

5+
tablegen("Opts") {
6+
visibility = [ ":llvm-debuginfod" ]
7+
args = [ "-gen-opt-parser-defs" ]
8+
}
9+
410
if (llvm_install_binutils_symlinks) {
511
symlink_or_copy("debuginfod") {
612
deps = [ ":llvm-debuginfod" ]
@@ -19,7 +25,9 @@ group("symlinks") {
1925

2026
executable("llvm-debuginfod") {
2127
deps = [
28+
":Opts",
2229
"//llvm/lib/Debuginfod",
30+
"//llvm/lib/Option",
2331
"//llvm/lib/Support",
2432
]
2533
sources = [ "llvm-debuginfod.cpp" ]

0 commit comments

Comments
 (0)