1//===-- "main" function of libc-hdrgen ------------------------------------===//
2//
3// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4// See https://llvm.org/LICENSE.txt for license information.
5// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6//
7//===----------------------------------------------------------------------===//
8
9#include "Generator.h"
10
11#include "llvm/ADT/StringRef.h"
12#include "llvm/Support/CommandLine.h"
13#include "llvm/TableGen/Main.h"
14
15#include <string>
16#include <unordered_map>
17
18namespace {
19
20llvm::cl::opt<std::string>
21 HeaderDefFile("def", llvm::cl::desc("Path to the .h.def file."),
22 llvm::cl::value_desc("<filename>"), llvm::cl::Required);
23llvm::cl::opt<std::string> StandardHeader(
24 "header",
25 llvm::cl::desc("The standard header file which is to be generated."),
26 llvm::cl::value_desc("<header file>"));
27llvm::cl::list<std::string> EntrypointNamesOption(
28 "e", llvm::cl::value_desc("<list of entrypoints>"),
29 llvm::cl::desc(
30 "Each --e is one entrypoint (generated from entrypoints.txt)"),
31 llvm::cl::OneOrMore);
32llvm::cl::list<std::string> ReplacementValues(
33 "args", llvm::cl::desc("Command separated <argument name>=<value> pairs."),
34 llvm::cl::value_desc("<name=value>[,name=value]"));
35llvm::cl::opt<bool> ExportDecls(
36 "export-decls",
37 llvm::cl::desc("Output a new header containing only the entrypoints."));
38
39void ParseArgValuePairs(std::unordered_map<std::string, std::string> &Map) {
40 for (std::string &R : ReplacementValues) {
41 auto Pair = llvm::StringRef(R).split('=');
42 Map[std::string(Pair.first)] = std::string(Pair.second);
43 }
44}
45
46} // anonymous namespace
47
48namespace llvm_libc {
49
50bool HeaderGeneratorMain(llvm::raw_ostream &OS, llvm::RecordKeeper &Records) {
51 std::unordered_map<std::string, std::string> ArgMap;
52 ParseArgValuePairs(Map&: ArgMap);
53 Generator G(HeaderDefFile, EntrypointNamesOption, StandardHeader, ArgMap);
54 if (ExportDecls)
55 G.generateDecls(OS, Records);
56 else
57 G.generate(OS, Records);
58
59 return false;
60}
61
62} // namespace llvm_libc
63
64int main(int argc, char *argv[]) {
65 llvm::cl::ParseCommandLineOptions(argc, argv);
66 return TableGenMain(argv[0], &llvm_libc::HeaderGeneratorMain);
67}
68

source code of libc/utils/HdrGen/Main.cpp