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/ADT/StringSwitch.h"
12 #include "llvm/Support/CommandLine.h"
13 #include "llvm/Support/ErrorHandling.h"
14 #include "llvm/Support/Path.h"
15 #include "llvm/Support/WithColor.h"
16 
17 using namespace llvm;
18 
19 #define LLVM_DRIVER_TOOL(tool, entry) int entry##_main(int argc, char **argv);
20 #include "LLVMDriverTools.def"
21 
22 constexpr char subcommands[] =
23 #define LLVM_DRIVER_TOOL(tool, entry) "  " tool "\n"
24 #include "LLVMDriverTools.def"
25     ;
26 
27 static void printHelpMessage() {
28   llvm::outs() << "OVERVIEW: llvm toolchain driver\n\n"
29                << "USAGE: llvm [subcommand] [options]\n\n"
30                << "SUBCOMMANDS:\n\n"
31                << subcommands
32                << "\n  Type \"llvm <subcommand> --help\" to get more help on a "
33                   "specific subcommand\n\n"
34                << "OPTIONS:\n\n  --help - Display this message";
35 }
36 
37 static int findTool(int Argc, char **Argv) {
38   if (!Argc) {
39     printHelpMessage();
40     return 1;
41   }
42 
43   StringRef ToolName = Argv[0];
44 
45   if (ToolName == "--help") {
46     printHelpMessage();
47     return 0;
48   }
49 
50   StringRef Stem = sys::path::stem(ToolName);
51   auto Is = [=](StringRef Tool) {
52     auto I = Stem.rfind_insensitive(Tool);
53     return I != StringRef::npos && (I + Tool.size() == Stem.size() ||
54                                     !llvm::isAlnum(Stem[I + Tool.size()]));
55   };
56 
57 #define LLVM_DRIVER_TOOL(tool, entry)                                          \
58   if (Is(tool))                                                                \
59     return entry##_main(Argc, Argv);
60 #include "LLVMDriverTools.def"
61 
62   if (Is("llvm"))
63     return findTool(Argc - 1, Argv + 1);
64 
65   printHelpMessage();
66   return 1;
67 }
68 
69 extern bool IsLLVMDriver;
70 
71 int main(int Argc, char **Argv) {
72   IsLLVMDriver = true;
73   return findTool(Argc, Argv);
74 }
75