| Anton Korobeynikov | ac67b7e | 2008-03-23 08:57:20 +0000 | [diff] [blame] | 1 | //===--- llvmcc.cpp - The LLVM Compiler Driver ------------------*- C++ -*-===// | 
|  | 2 | // | 
|  | 3 | //                     The LLVM Compiler Infrastructure | 
|  | 4 | // | 
|  | 5 | // This file is distributed under the University of Illinois Open | 
|  | 6 | // Source License. See LICENSE.TXT for details. | 
|  | 7 | // | 
|  | 8 | //===----------------------------------------------------------------------===// | 
|  | 9 | // | 
|  | 10 | //  This tool provides a single point of access to the LLVM | 
|  | 11 | //  compilation tools.  It has many options. To discover the options | 
|  | 12 | //  supported please refer to the tools' manual page or run the tool | 
|  | 13 | //  with the --help option. | 
|  | 14 | // | 
|  | 15 | //===----------------------------------------------------------------------===// | 
|  | 16 |  | 
| Mikhail Glushenkov | b90cd83 | 2008-05-06 16:34:12 +0000 | [diff] [blame] | 17 | #include "CompilationGraph.h" | 
|  | 18 | #include "Tool.h" | 
| Anton Korobeynikov | ac67b7e | 2008-03-23 08:57:20 +0000 | [diff] [blame] | 19 |  | 
|  | 20 | #include "llvm/System/Path.h" | 
|  | 21 | #include "llvm/Support/CommandLine.h" | 
|  | 22 |  | 
|  | 23 | #include <iostream> | 
|  | 24 | #include <stdexcept> | 
|  | 25 | #include <string> | 
|  | 26 |  | 
| Mikhail Glushenkov | b90cd83 | 2008-05-06 16:34:12 +0000 | [diff] [blame] | 27 | namespace cl = llvm::cl; | 
|  | 28 | namespace sys = llvm::sys; | 
| Anton Korobeynikov | ac67b7e | 2008-03-23 08:57:20 +0000 | [diff] [blame] | 29 | using namespace llvmcc; | 
|  | 30 |  | 
| Mikhail Glushenkov | 0d08db0 | 2008-05-06 16:35:25 +0000 | [diff] [blame] | 31 | // Built-in command-line options. | 
| Mikhail Glushenkov | b90cd83 | 2008-05-06 16:34:12 +0000 | [diff] [blame] | 32 | // External linkage here is intentional. | 
| Mikhail Glushenkov | 0d08db0 | 2008-05-06 16:35:25 +0000 | [diff] [blame] | 33 |  | 
|  | 34 | cl::list<std::string> InputFilenames(cl::Positional, cl::desc("<input file>"), | 
| Mikhail Glushenkov | d7bb87a | 2008-05-06 17:44:16 +0000 | [diff] [blame] | 35 | cl::ZeroOrMore); | 
| Anton Korobeynikov | ac67b7e | 2008-03-23 08:57:20 +0000 | [diff] [blame] | 36 | cl::opt<std::string> OutputFilename("o", cl::desc("Output file name"), | 
|  | 37 | cl::value_desc("file")); | 
| Mikhail Glushenkov | 0d08db0 | 2008-05-06 16:35:25 +0000 | [diff] [blame] | 38 | cl::opt<bool> VerboseMode("v", | 
|  | 39 | cl::desc("Enable verbose mode")); | 
|  | 40 | cl::opt<bool> WriteGraph("write-graph", | 
| Mikhail Glushenkov | 3d68822 | 2008-05-06 18:07:48 +0000 | [diff] [blame^] | 41 | cl::desc("Write compilation-graph.dot file"), | 
| Mikhail Glushenkov | 0d08db0 | 2008-05-06 16:35:25 +0000 | [diff] [blame] | 42 | cl::Hidden); | 
|  | 43 | cl::opt<bool> ViewGraph("view-graph", | 
|  | 44 | cl::desc("Show compilation graph in GhostView"), | 
|  | 45 | cl::Hidden); | 
| Anton Korobeynikov | ac67b7e | 2008-03-23 08:57:20 +0000 | [diff] [blame] | 46 |  | 
|  | 47 | namespace { | 
| Mikhail Glushenkov | 0260658 | 2008-05-06 18:07:14 +0000 | [diff] [blame] | 48 | int BuildTargets(CompilationGraph& graph) { | 
| Anton Korobeynikov | ac67b7e | 2008-03-23 08:57:20 +0000 | [diff] [blame] | 49 | int ret; | 
|  | 50 | sys::Path tempDir(sys::Path::GetTemporaryDirectory()); | 
|  | 51 |  | 
|  | 52 | try { | 
|  | 53 | ret = graph.Build(tempDir); | 
|  | 54 | } | 
|  | 55 | catch(...) { | 
|  | 56 | tempDir.eraseFromDisk(true); | 
|  | 57 | throw; | 
|  | 58 | } | 
|  | 59 |  | 
|  | 60 | tempDir.eraseFromDisk(true); | 
|  | 61 | return ret; | 
|  | 62 | } | 
|  | 63 | } | 
|  | 64 |  | 
|  | 65 | int main(int argc, char** argv) { | 
|  | 66 | try { | 
|  | 67 | CompilationGraph graph; | 
|  | 68 |  | 
|  | 69 | cl::ParseCommandLineOptions(argc, argv, | 
| Mikhail Glushenkov | 0260658 | 2008-05-06 18:07:14 +0000 | [diff] [blame] | 70 | "LLVM Compiler Driver (Work In Progress)"); | 
| Anton Korobeynikov | ac67b7e | 2008-03-23 08:57:20 +0000 | [diff] [blame] | 71 | PopulateCompilationGraph(graph); | 
| Mikhail Glushenkov | 0d08db0 | 2008-05-06 16:35:25 +0000 | [diff] [blame] | 72 |  | 
| Mikhail Glushenkov | d7bb87a | 2008-05-06 17:44:16 +0000 | [diff] [blame] | 73 | if (WriteGraph) { | 
| Mikhail Glushenkov | 0d08db0 | 2008-05-06 16:35:25 +0000 | [diff] [blame] | 74 | graph.writeGraph(); | 
| Mikhail Glushenkov | 3d68822 | 2008-05-06 18:07:48 +0000 | [diff] [blame^] | 75 | if (!ViewGraph) | 
|  | 76 | return 0; | 
| Mikhail Glushenkov | d7bb87a | 2008-05-06 17:44:16 +0000 | [diff] [blame] | 77 | } | 
|  | 78 |  | 
|  | 79 | if (ViewGraph) { | 
| Mikhail Glushenkov | 0d08db0 | 2008-05-06 16:35:25 +0000 | [diff] [blame] | 80 | graph.viewGraph(); | 
| Mikhail Glushenkov | d7bb87a | 2008-05-06 17:44:16 +0000 | [diff] [blame] | 81 | return 0; | 
|  | 82 | } | 
|  | 83 |  | 
|  | 84 | if (InputFilenames.empty()) { | 
|  | 85 | std::cerr << "No input files.\n"; | 
|  | 86 | return 1; | 
|  | 87 | } | 
| Mikhail Glushenkov | 0d08db0 | 2008-05-06 16:35:25 +0000 | [diff] [blame] | 88 |  | 
| Anton Korobeynikov | ac67b7e | 2008-03-23 08:57:20 +0000 | [diff] [blame] | 89 | return BuildTargets(graph); | 
|  | 90 | } | 
|  | 91 | catch(const std::exception& ex) { | 
|  | 92 | std::cerr << ex.what() << '\n'; | 
|  | 93 | } | 
|  | 94 | catch(...) { | 
|  | 95 | std::cerr << "Unknown error!\n"; | 
|  | 96 | } | 
| Mikhail Glushenkov | d818878 | 2008-05-06 17:25:23 +0000 | [diff] [blame] | 97 | return 1; | 
| Anton Korobeynikov | ac67b7e | 2008-03-23 08:57:20 +0000 | [diff] [blame] | 98 | } |