llvm-project
86 строк · 2.7 Кб
1//===-- llvm-driver.cpp ---------------------------------------------------===//
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 "llvm/ADT/StringExtras.h"
10#include "llvm/ADT/StringRef.h"
11#include "llvm/Support/CommandLine.h"
12#include "llvm/Support/ErrorHandling.h"
13#include "llvm/Support/InitLLVM.h"
14#include "llvm/Support/LLVMDriver.h"
15#include "llvm/Support/Path.h"
16#include "llvm/Support/WithColor.h"
17
18using namespace llvm;
19
20#define LLVM_DRIVER_TOOL(tool, entry) \
21int entry##_main(int argc, char **argv, const llvm::ToolContext &);
22#include "LLVMDriverTools.def"
23
24constexpr char subcommands[] =
25#define LLVM_DRIVER_TOOL(tool, entry) " " tool "\n"
26#include "LLVMDriverTools.def"
27;
28
29static void printHelpMessage() {
30llvm::outs() << "OVERVIEW: llvm toolchain driver\n\n"
31<< "USAGE: llvm [subcommand] [options]\n\n"
32<< "SUBCOMMANDS:\n\n"
33<< subcommands
34<< "\n Type \"llvm <subcommand> --help\" to get more help on a "
35"specific subcommand\n\n"
36<< "OPTIONS:\n\n --help - Display this message";
37}
38
39static int findTool(int Argc, char **Argv, const char *Argv0) {
40if (!Argc) {
41printHelpMessage();
42return 1;
43}
44
45StringRef ToolName = Argv[0];
46
47if (ToolName == "--help") {
48printHelpMessage();
49return 0;
50}
51
52StringRef Stem = sys::path::stem(ToolName);
53auto Is = [=](StringRef Tool) {
54auto IsImpl = [=](StringRef Stem) {
55auto I = Stem.rfind_insensitive(Tool);
56return I != StringRef::npos && (I + Tool.size() == Stem.size() ||
57!llvm::isAlnum(Stem[I + Tool.size()]));
58};
59for (StringRef S : {Stem, sys::path::filename(ToolName)})
60if (IsImpl(S))
61return true;
62return false;
63};
64
65auto MakeDriverArgs = [=]() -> llvm::ToolContext {
66if (ToolName != Argv0)
67return {Argv0, ToolName.data(), true};
68return {Argv0, sys::path::filename(Argv0).data(), false};
69};
70
71#define LLVM_DRIVER_TOOL(tool, entry) \
72if (Is(tool)) \
73return entry##_main(Argc, Argv, MakeDriverArgs());
74#include "LLVMDriverTools.def"
75
76if (Is("llvm") || Argv0 == Argv[0])
77return findTool(Argc - 1, Argv + 1, Argv0);
78
79printHelpMessage();
80return 1;
81}
82
83int main(int Argc, char **Argv) {
84llvm::InitLLVM X(Argc, Argv);
85return findTool(Argc, Argv, Argv[0]);
86}
87