blob: 560ee98173a4386783cad40004bb8e44529ac5eb [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 Glushenkovb90cd832008-05-06 16:34:12 +000017#include "CompilationGraph.h"
Mikhail Glushenkova6730372008-05-12 16:31:42 +000018#include "Error.h"
Mikhail Glushenkovb90cd832008-05-06 16:34:12 +000019#include "Tool.h"
Anton Korobeynikovac67b7e2008-03-23 08:57:20 +000020
21#include "llvm/System/Path.h"
22#include "llvm/Support/CommandLine.h"
23
24#include <iostream>
25#include <stdexcept>
26#include <string>
27
Mikhail Glushenkovb90cd832008-05-06 16:34:12 +000028namespace cl = llvm::cl;
29namespace sys = llvm::sys;
Mikhail Glushenkovbe9d9a12008-05-06 18:08:59 +000030using namespace llvmc;
Anton Korobeynikovac67b7e2008-03-23 08:57:20 +000031
Mikhail Glushenkov0d08db02008-05-06 16:35:25 +000032// Built-in command-line options.
Mikhail Glushenkovb90cd832008-05-06 16:34:12 +000033// External linkage here is intentional.
Mikhail Glushenkov0d08db02008-05-06 16:35:25 +000034
Mikhail Glushenkova7d0ae32008-05-30 06:28:37 +000035// TOFIX: Write a 'driver driver' (easier to do as a separate
36// executable that drives llvmc2 proper).
Mikhail Glushenkov0d08db02008-05-06 16:35:25 +000037cl::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"),
40 cl::value_desc("file"));
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 Glushenkov18aac1c2008-05-30 18:53:09 +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"));
48cl::opt<bool> WriteGraph("write-graph",
Mikhail Glushenkov3d688222008-05-06 18:07:48 +000049 cl::desc("Write compilation-graph.dot file"),
Mikhail Glushenkov0d08db02008-05-06 16:35:25 +000050 cl::Hidden);
51cl::opt<bool> ViewGraph("view-graph",
52 cl::desc("Show compilation graph in GhostView"),
53 cl::Hidden);
Mikhail Glushenkov73296102008-05-30 06:29:17 +000054cl::opt<bool> SaveTemps("save-temps",
55 cl::desc("Keep temporary files"),
56 cl::Hidden);
Anton Korobeynikovac67b7e2008-03-23 08:57:20 +000057
58namespace {
Mikhail Glushenkov4561ab52008-05-07 21:50:19 +000059 /// BuildTargets - A small wrapper for CompilationGraph::Build.
Mikhail Glushenkov02606582008-05-06 18:07:14 +000060 int BuildTargets(CompilationGraph& graph) {
Anton Korobeynikovac67b7e2008-03-23 08:57:20 +000061 int ret;
Mikhail Glushenkov73296102008-05-30 06:29:17 +000062 const sys::Path& tempDir = SaveTemps
63 ? sys::Path("")
64 : sys::Path(sys::Path::GetTemporaryDirectory());
Anton Korobeynikovac67b7e2008-03-23 08:57:20 +000065
66 try {
67 ret = graph.Build(tempDir);
68 }
69 catch(...) {
70 tempDir.eraseFromDisk(true);
71 throw;
72 }
73
Mikhail Glushenkov73296102008-05-30 06:29:17 +000074 if (!SaveTemps)
75 tempDir.eraseFromDisk(true);
Anton Korobeynikovac67b7e2008-03-23 08:57:20 +000076 return ret;
77 }
78}
79
80int main(int argc, char** argv) {
81 try {
82 CompilationGraph graph;
83
Mikhail Glushenkov4eaa3892008-05-30 06:11:45 +000084 cl::ParseCommandLineOptions
85 (argc, argv, "LLVM Compiler Driver (Work In Progress)", true);
Anton Korobeynikovac67b7e2008-03-23 08:57:20 +000086 PopulateCompilationGraph(graph);
Mikhail Glushenkov0d08db02008-05-06 16:35:25 +000087
Mikhail Glushenkovd7bb87a2008-05-06 17:44:16 +000088 if (WriteGraph) {
Mikhail Glushenkov0d08db02008-05-06 16:35:25 +000089 graph.writeGraph();
Mikhail Glushenkov3d688222008-05-06 18:07:48 +000090 if (!ViewGraph)
91 return 0;
Mikhail Glushenkovd7bb87a2008-05-06 17:44:16 +000092 }
93
94 if (ViewGraph) {
Mikhail Glushenkov0d08db02008-05-06 16:35:25 +000095 graph.viewGraph();
Mikhail Glushenkovd7bb87a2008-05-06 17:44:16 +000096 return 0;
97 }
98
99 if (InputFilenames.empty()) {
Mikhail Glushenkoved3ba402008-05-30 06:26:35 +0000100 throw std::runtime_error("no input files");
Mikhail Glushenkovd7bb87a2008-05-06 17:44:16 +0000101 }
Mikhail Glushenkov0d08db02008-05-06 16:35:25 +0000102
Anton Korobeynikovac67b7e2008-03-23 08:57:20 +0000103 return BuildTargets(graph);
104 }
Mikhail Glushenkova6730372008-05-12 16:31:42 +0000105 catch(llvmc::error_code& ec) {
106 return ec.code();
107 }
Anton Korobeynikovac67b7e2008-03-23 08:57:20 +0000108 catch(const std::exception& ex) {
Mikhail Glushenkoved3ba402008-05-30 06:26:35 +0000109 std::cerr << argv[0] << ": " << ex.what() << '\n';
Anton Korobeynikovac67b7e2008-03-23 08:57:20 +0000110 }
111 catch(...) {
Mikhail Glushenkoved3ba402008-05-30 06:26:35 +0000112 std::cerr << argv[0] << ": unknown error!\n";
Anton Korobeynikovac67b7e2008-03-23 08:57:20 +0000113 }
Mikhail Glushenkovd8188782008-05-06 17:25:23 +0000114 return 1;
Anton Korobeynikovac67b7e2008-03-23 08:57:20 +0000115}