blob: 059e840eeb6fc98af6481136dbbd91c2fe3ec192 [file] [log] [blame]
Mikhail Glushenkovfb37f392008-05-30 06:20:54 +00001//===--- llvmc.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 Glushenkova6730372008-05-12 16:31:42 +000017#include "Error.h"
Mikhail Glushenkov4a1a77c2008-09-22 20:50:40 +000018
19#include "llvm/CompilerDriver/CompilationGraph.h"
20#include "llvm/CompilerDriver/Plugin.h"
Anton Korobeynikovac67b7e2008-03-23 08:57:20 +000021
22#include "llvm/System/Path.h"
23#include "llvm/Support/CommandLine.h"
Mikhail Glushenkovc82ce4a2008-09-22 20:49:34 +000024#include "llvm/Support/PluginLoader.h"
Anton Korobeynikovac67b7e2008-03-23 08:57:20 +000025
26#include <iostream>
27#include <stdexcept>
28#include <string>
29
Mikhail Glushenkovb90cd832008-05-06 16:34:12 +000030namespace cl = llvm::cl;
31namespace sys = llvm::sys;
Mikhail Glushenkovbe9d9a12008-05-06 18:08:59 +000032using namespace llvmc;
Anton Korobeynikovac67b7e2008-03-23 08:57:20 +000033
Mikhail Glushenkov0d08db02008-05-06 16:35:25 +000034// Built-in command-line options.
Mikhail Glushenkovb90cd832008-05-06 16:34:12 +000035// External linkage here is intentional.
Mikhail Glushenkov0d08db02008-05-06 16:35:25 +000036
37cl::list<std::string> InputFilenames(cl::Positional, cl::desc("<input file>"),
Mikhail Glushenkovd7bb87a2008-05-06 17:44:16 +000038 cl::ZeroOrMore);
Anton Korobeynikovac67b7e2008-03-23 08:57:20 +000039cl::opt<std::string> OutputFilename("o", cl::desc("Output file name"),
Mikhail Glushenkov15b064d2009-01-14 02:02:16 +000040 cl::value_desc("file"), cl::Prefix);
Mikhail Glushenkov87416b42008-05-06 18:10:53 +000041cl::list<std::string> Languages("x",
42 cl::desc("Specify the language of the following input files"),
43 cl::ZeroOrMore);
Mikhail Glushenkov7ef36062008-05-30 18:48:52 +000044cl::opt<bool> DryRun("dry-run",
Mikhail Glushenkov74a0c282008-05-30 19:56:27 +000045 cl::desc("Only pretend to run commands"));
Mikhail Glushenkov0d08db02008-05-06 16:35:25 +000046cl::opt<bool> VerboseMode("v",
47 cl::desc("Enable verbose mode"));
Mikhail Glushenkovf8c430b2009-01-09 16:16:27 +000048
49cl::opt<bool> CheckGraph("check-graph",
50 cl::desc("Check the compilation graph for errors"),
51 cl::Hidden);
Mikhail Glushenkov0d08db02008-05-06 16:35:25 +000052cl::opt<bool> WriteGraph("write-graph",
Mikhail Glushenkov3d688222008-05-06 18:07:48 +000053 cl::desc("Write compilation-graph.dot file"),
Mikhail Glushenkov0d08db02008-05-06 16:35:25 +000054 cl::Hidden);
55cl::opt<bool> ViewGraph("view-graph",
56 cl::desc("Show compilation graph in GhostView"),
57 cl::Hidden);
Mikhail Glushenkov73296102008-05-30 06:29:17 +000058cl::opt<bool> SaveTemps("save-temps",
59 cl::desc("Keep temporary files"),
60 cl::Hidden);
Anton Korobeynikovac67b7e2008-03-23 08:57:20 +000061
62namespace {
Mikhail Glushenkov4561ab52008-05-07 21:50:19 +000063 /// BuildTargets - A small wrapper for CompilationGraph::Build.
Mikhail Glushenkov11a353a2008-09-22 20:47:46 +000064 int BuildTargets(CompilationGraph& graph, const LanguageMap& langMap) {
Anton Korobeynikovac67b7e2008-03-23 08:57:20 +000065 int ret;
Mikhail Glushenkov73296102008-05-30 06:29:17 +000066 const sys::Path& tempDir = SaveTemps
67 ? sys::Path("")
68 : sys::Path(sys::Path::GetTemporaryDirectory());
Anton Korobeynikovac67b7e2008-03-23 08:57:20 +000069
70 try {
Mikhail Glushenkov11a353a2008-09-22 20:47:46 +000071 ret = graph.Build(tempDir, langMap);
Anton Korobeynikovac67b7e2008-03-23 08:57:20 +000072 }
73 catch(...) {
74 tempDir.eraseFromDisk(true);
75 throw;
76 }
77
Mikhail Glushenkov73296102008-05-30 06:29:17 +000078 if (!SaveTemps)
79 tempDir.eraseFromDisk(true);
Anton Korobeynikovac67b7e2008-03-23 08:57:20 +000080 return ret;
81 }
82}
83
84int main(int argc, char** argv) {
85 try {
Mikhail Glushenkov11a353a2008-09-22 20:47:46 +000086 LanguageMap langMap;
Anton Korobeynikovac67b7e2008-03-23 08:57:20 +000087 CompilationGraph graph;
88
Mikhail Glushenkov4eaa3892008-05-30 06:11:45 +000089 cl::ParseCommandLineOptions
90 (argc, argv, "LLVM Compiler Driver (Work In Progress)", true);
Mikhail Glushenkov11a353a2008-09-22 20:47:46 +000091
Mikhail Glushenkov14ef0592008-09-22 20:51:19 +000092 PluginLoader Plugins;
93 Plugins.PopulateLanguageMap(langMap);
94 Plugins.PopulateCompilationGraph(graph);
Mikhail Glushenkov0d08db02008-05-06 16:35:25 +000095
Mikhail Glushenkovf8c430b2009-01-09 16:16:27 +000096 if (CheckGraph) {
97 return graph.Check();
Mikhail Glushenkovd7bb87a2008-05-06 17:44:16 +000098 }
99
100 if (ViewGraph) {
Mikhail Glushenkov0d08db02008-05-06 16:35:25 +0000101 graph.viewGraph();
Mikhail Glushenkovf8c430b2009-01-09 16:16:27 +0000102 if (!WriteGraph)
103 return 0;
104 }
105
106 if (WriteGraph) {
107 graph.writeGraph();
Mikhail Glushenkovd7bb87a2008-05-06 17:44:16 +0000108 return 0;
109 }
110
111 if (InputFilenames.empty()) {
Mikhail Glushenkoved3ba402008-05-30 06:26:35 +0000112 throw std::runtime_error("no input files");
Mikhail Glushenkovd7bb87a2008-05-06 17:44:16 +0000113 }
Mikhail Glushenkov0d08db02008-05-06 16:35:25 +0000114
Mikhail Glushenkov11a353a2008-09-22 20:47:46 +0000115 return BuildTargets(graph, langMap);
Anton Korobeynikovac67b7e2008-03-23 08:57:20 +0000116 }
Mikhail Glushenkova6730372008-05-12 16:31:42 +0000117 catch(llvmc::error_code& ec) {
118 return ec.code();
119 }
Anton Korobeynikovac67b7e2008-03-23 08:57:20 +0000120 catch(const std::exception& ex) {
Mikhail Glushenkoved3ba402008-05-30 06:26:35 +0000121 std::cerr << argv[0] << ": " << ex.what() << '\n';
Anton Korobeynikovac67b7e2008-03-23 08:57:20 +0000122 }
123 catch(...) {
Mikhail Glushenkoved3ba402008-05-30 06:26:35 +0000124 std::cerr << argv[0] << ": unknown error!\n";
Anton Korobeynikovac67b7e2008-03-23 08:57:20 +0000125 }
Mikhail Glushenkovd8188782008-05-06 17:25:23 +0000126 return 1;
Anton Korobeynikovac67b7e2008-03-23 08:57:20 +0000127}