blob: 16b91a87c91be79de816362820f4e3ccc828e266 [file] [log] [blame]
Mikhail Glushenkovf1881782009-03-02 09:01:14 +00001//===--- Main.cpp - The LLVM Compiler Driver -------------------*- C++ -*-===//
Anton Korobeynikovac67b7e2008-03-23 08:57:20 +00002//
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 Glushenkov4a1a77c2008-09-22 20:50:40 +000017#include "llvm/CompilerDriver/CompilationGraph.h"
Mikhail Glushenkovf1881782009-03-02 09:01:14 +000018#include "llvm/CompilerDriver/Error.h"
Mikhail Glushenkov4a1a77c2008-09-22 20:50:40 +000019#include "llvm/CompilerDriver/Plugin.h"
Anton Korobeynikovac67b7e2008-03-23 08:57:20 +000020
21#include "llvm/System/Path.h"
22#include "llvm/Support/CommandLine.h"
Mikhail Glushenkovc82ce4a2008-09-22 20:49:34 +000023#include "llvm/Support/PluginLoader.h"
Anton Korobeynikovac67b7e2008-03-23 08:57:20 +000024
25#include <iostream>
26#include <stdexcept>
27#include <string>
28
Mikhail Glushenkovb90cd832008-05-06 16:34:12 +000029namespace cl = llvm::cl;
30namespace sys = llvm::sys;
Mikhail Glushenkovbe9d9a12008-05-06 18:08:59 +000031using namespace llvmc;
Anton Korobeynikovac67b7e2008-03-23 08:57:20 +000032
Mikhail Glushenkov0d08db02008-05-06 16:35:25 +000033// Built-in command-line options.
Mikhail Glushenkovb90cd832008-05-06 16:34:12 +000034// External linkage here is intentional.
Mikhail Glushenkov0d08db02008-05-06 16:35:25 +000035
36cl::list<std::string> InputFilenames(cl::Positional, cl::desc("<input file>"),
Mikhail Glushenkovd7bb87a2008-05-06 17:44:16 +000037 cl::ZeroOrMore);
Anton Korobeynikovac67b7e2008-03-23 08:57:20 +000038cl::opt<std::string> OutputFilename("o", cl::desc("Output file name"),
Mikhail Glushenkov15b064d2009-01-14 02:02:16 +000039 cl::value_desc("file"), cl::Prefix);
Mikhail Glushenkov87416b42008-05-06 18:10:53 +000040cl::list<std::string> Languages("x",
41 cl::desc("Specify the language of the following input files"),
42 cl::ZeroOrMore);
Mikhail Glushenkov7ef36062008-05-30 18:48:52 +000043cl::opt<bool> DryRun("dry-run",
Mikhail Glushenkov74a0c282008-05-30 19:56:27 +000044 cl::desc("Only pretend to run commands"));
Mikhail Glushenkov0d08db02008-05-06 16:35:25 +000045cl::opt<bool> VerboseMode("v",
46 cl::desc("Enable verbose mode"));
Mikhail Glushenkovf8c430b2009-01-09 16:16:27 +000047
48cl::opt<bool> CheckGraph("check-graph",
49 cl::desc("Check the compilation graph for errors"),
50 cl::Hidden);
Mikhail Glushenkov0d08db02008-05-06 16:35:25 +000051cl::opt<bool> WriteGraph("write-graph",
Mikhail Glushenkov3d688222008-05-06 18:07:48 +000052 cl::desc("Write compilation-graph.dot file"),
Mikhail Glushenkov0d08db02008-05-06 16:35:25 +000053 cl::Hidden);
54cl::opt<bool> ViewGraph("view-graph",
55 cl::desc("Show compilation graph in GhostView"),
56 cl::Hidden);
Mikhail Glushenkov73296102008-05-30 06:29:17 +000057cl::opt<bool> SaveTemps("save-temps",
58 cl::desc("Keep temporary files"),
59 cl::Hidden);
Anton Korobeynikovac67b7e2008-03-23 08:57:20 +000060
61namespace {
Mikhail Glushenkov4561ab52008-05-07 21:50:19 +000062 /// BuildTargets - A small wrapper for CompilationGraph::Build.
Mikhail Glushenkov11a353a2008-09-22 20:47:46 +000063 int BuildTargets(CompilationGraph& graph, const LanguageMap& langMap) {
Anton Korobeynikovac67b7e2008-03-23 08:57:20 +000064 int ret;
Mikhail Glushenkov73296102008-05-30 06:29:17 +000065 const sys::Path& tempDir = SaveTemps
66 ? sys::Path("")
67 : sys::Path(sys::Path::GetTemporaryDirectory());
Anton Korobeynikovac67b7e2008-03-23 08:57:20 +000068
69 try {
Mikhail Glushenkov11a353a2008-09-22 20:47:46 +000070 ret = graph.Build(tempDir, langMap);
Anton Korobeynikovac67b7e2008-03-23 08:57:20 +000071 }
72 catch(...) {
73 tempDir.eraseFromDisk(true);
74 throw;
75 }
76
Mikhail Glushenkov73296102008-05-30 06:29:17 +000077 if (!SaveTemps)
78 tempDir.eraseFromDisk(true);
Anton Korobeynikovac67b7e2008-03-23 08:57:20 +000079 return ret;
80 }
81}
82
83int main(int argc, char** argv) {
84 try {
Mikhail Glushenkov11a353a2008-09-22 20:47:46 +000085 LanguageMap langMap;
Anton Korobeynikovac67b7e2008-03-23 08:57:20 +000086 CompilationGraph graph;
87
Mikhail Glushenkov4eaa3892008-05-30 06:11:45 +000088 cl::ParseCommandLineOptions
89 (argc, argv, "LLVM Compiler Driver (Work In Progress)", true);
Mikhail Glushenkov11a353a2008-09-22 20:47:46 +000090
Mikhail Glushenkov14ef0592008-09-22 20:51:19 +000091 PluginLoader Plugins;
92 Plugins.PopulateLanguageMap(langMap);
93 Plugins.PopulateCompilationGraph(graph);
Mikhail Glushenkov0d08db02008-05-06 16:35:25 +000094
Mikhail Glushenkovf8c430b2009-01-09 16:16:27 +000095 if (CheckGraph) {
96 return graph.Check();
Mikhail Glushenkovd7bb87a2008-05-06 17:44:16 +000097 }
98
99 if (ViewGraph) {
Mikhail Glushenkov0d08db02008-05-06 16:35:25 +0000100 graph.viewGraph();
Mikhail Glushenkovf8c430b2009-01-09 16:16:27 +0000101 if (!WriteGraph)
102 return 0;
103 }
104
105 if (WriteGraph) {
106 graph.writeGraph();
Mikhail Glushenkovd7bb87a2008-05-06 17:44:16 +0000107 return 0;
108 }
109
110 if (InputFilenames.empty()) {
Mikhail Glushenkoved3ba402008-05-30 06:26:35 +0000111 throw std::runtime_error("no input files");
Mikhail Glushenkovd7bb87a2008-05-06 17:44:16 +0000112 }
Mikhail Glushenkov0d08db02008-05-06 16:35:25 +0000113
Mikhail Glushenkov11a353a2008-09-22 20:47:46 +0000114 return BuildTargets(graph, langMap);
Anton Korobeynikovac67b7e2008-03-23 08:57:20 +0000115 }
Mikhail Glushenkova6730372008-05-12 16:31:42 +0000116 catch(llvmc::error_code& ec) {
117 return ec.code();
118 }
Anton Korobeynikovac67b7e2008-03-23 08:57:20 +0000119 catch(const std::exception& ex) {
Mikhail Glushenkoved3ba402008-05-30 06:26:35 +0000120 std::cerr << argv[0] << ": " << ex.what() << '\n';
Anton Korobeynikovac67b7e2008-03-23 08:57:20 +0000121 }
122 catch(...) {
Mikhail Glushenkoved3ba402008-05-30 06:26:35 +0000123 std::cerr << argv[0] << ": unknown error!\n";
Anton Korobeynikovac67b7e2008-03-23 08:57:20 +0000124 }
Mikhail Glushenkovd8188782008-05-06 17:25:23 +0000125 return 1;
Anton Korobeynikovac67b7e2008-03-23 08:57:20 +0000126}