xref: /openbsd-src/gnu/llvm/llvm/tools/llvm-driver/llvm-driver.cpp (revision d415bd752c734aee168c4ee86ff32e8cc249eb16)
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 
printHelpMessage()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 
findTool(int Argc,char ** Argv)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 IsImpl = [=](StringRef Stem) {
53       auto I = Stem.rfind_insensitive(Tool);
54       return I != StringRef::npos && (I + Tool.size() == Stem.size() ||
55                                       !llvm::isAlnum(Stem[I + Tool.size()]));
56     };
57     for (StringRef S : {Stem, sys::path::filename(ToolName)})
58       if (IsImpl(S))
59         return true;
60     return false;
61   };
62 
63 #define LLVM_DRIVER_TOOL(tool, entry)                                          \
64   if (Is(tool))                                                                \
65     return entry##_main(Argc, Argv);
66 #include "LLVMDriverTools.def"
67 
68   if (Is("llvm"))
69     return findTool(Argc - 1, Argv + 1);
70 
71   printHelpMessage();
72   return 1;
73 }
74 
75 extern bool IsLLVMDriver;
76 
main(int Argc,char ** Argv)77 int main(int Argc, char **Argv) {
78   IsLLVMDriver = true;
79   return findTool(Argc, Argv);
80 }
81