blob: 4eeb627169bedae2d2155591f0d9cbfc63e7cb55 [file] [log] [blame]
Mikhail Glushenkovbe9d9a12008-05-06 18:08:59 +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"
18#include "Tool.h"
Anton Korobeynikovac67b7e2008-03-23 08:57:20 +000019
20#include "llvm/System/Path.h"
21#include "llvm/Support/CommandLine.h"
22
23#include <iostream>
24#include <stdexcept>
25#include <string>
26
Mikhail Glushenkovb90cd832008-05-06 16:34:12 +000027namespace cl = llvm::cl;
28namespace sys = llvm::sys;
Mikhail Glushenkovbe9d9a12008-05-06 18:08:59 +000029using namespace llvmc;
Anton Korobeynikovac67b7e2008-03-23 08:57:20 +000030
Mikhail Glushenkov0d08db02008-05-06 16:35:25 +000031// Built-in command-line options.
Mikhail Glushenkovb90cd832008-05-06 16:34:12 +000032// External linkage here is intentional.
Mikhail Glushenkov0d08db02008-05-06 16:35:25 +000033
34cl::list<std::string> InputFilenames(cl::Positional, cl::desc("<input file>"),
Mikhail Glushenkovd7bb87a2008-05-06 17:44:16 +000035 cl::ZeroOrMore);
Anton Korobeynikovac67b7e2008-03-23 08:57:20 +000036cl::opt<std::string> OutputFilename("o", cl::desc("Output file name"),
37 cl::value_desc("file"));
Mikhail Glushenkov87416b42008-05-06 18:10:53 +000038cl::list<std::string> Languages("x",
39 cl::desc("Specify the language of the following input files"),
40 cl::ZeroOrMore);
Mikhail Glushenkov0d08db02008-05-06 16:35:25 +000041cl::opt<bool> VerboseMode("v",
42 cl::desc("Enable verbose mode"));
43cl::opt<bool> WriteGraph("write-graph",
Mikhail Glushenkov3d688222008-05-06 18:07:48 +000044 cl::desc("Write compilation-graph.dot file"),
Mikhail Glushenkov0d08db02008-05-06 16:35:25 +000045 cl::Hidden);
46cl::opt<bool> ViewGraph("view-graph",
47 cl::desc("Show compilation graph in GhostView"),
48 cl::Hidden);
Anton Korobeynikovac67b7e2008-03-23 08:57:20 +000049
50namespace {
Mikhail Glushenkov4561ab52008-05-07 21:50:19 +000051 /// BuildTargets - A small wrapper for CompilationGraph::Build.
Mikhail Glushenkov02606582008-05-06 18:07:14 +000052 int BuildTargets(CompilationGraph& graph) {
Anton Korobeynikovac67b7e2008-03-23 08:57:20 +000053 int ret;
54 sys::Path tempDir(sys::Path::GetTemporaryDirectory());
55
56 try {
57 ret = graph.Build(tempDir);
58 }
59 catch(...) {
60 tempDir.eraseFromDisk(true);
61 throw;
62 }
63
64 tempDir.eraseFromDisk(true);
65 return ret;
66 }
67}
68
69int main(int argc, char** argv) {
70 try {
71 CompilationGraph graph;
72
73 cl::ParseCommandLineOptions(argc, argv,
Mikhail Glushenkov02606582008-05-06 18:07:14 +000074 "LLVM Compiler Driver (Work In Progress)");
Anton Korobeynikovac67b7e2008-03-23 08:57:20 +000075 PopulateCompilationGraph(graph);
Mikhail Glushenkov0d08db02008-05-06 16:35:25 +000076
Mikhail Glushenkovd7bb87a2008-05-06 17:44:16 +000077 if (WriteGraph) {
Mikhail Glushenkov0d08db02008-05-06 16:35:25 +000078 graph.writeGraph();
Mikhail Glushenkov3d688222008-05-06 18:07:48 +000079 if (!ViewGraph)
80 return 0;
Mikhail Glushenkovd7bb87a2008-05-06 17:44:16 +000081 }
82
83 if (ViewGraph) {
Mikhail Glushenkov0d08db02008-05-06 16:35:25 +000084 graph.viewGraph();
Mikhail Glushenkovd7bb87a2008-05-06 17:44:16 +000085 return 0;
86 }
87
88 if (InputFilenames.empty()) {
89 std::cerr << "No input files.\n";
90 return 1;
91 }
Mikhail Glushenkov0d08db02008-05-06 16:35:25 +000092
Anton Korobeynikovac67b7e2008-03-23 08:57:20 +000093 return BuildTargets(graph);
94 }
95 catch(const std::exception& ex) {
96 std::cerr << ex.what() << '\n';
97 }
98 catch(...) {
99 std::cerr << "Unknown error!\n";
100 }
Mikhail Glushenkovd8188782008-05-06 17:25:23 +0000101 return 1;
Anton Korobeynikovac67b7e2008-03-23 08:57:20 +0000102}