blob: c06ea59ed9a40869171847e18198765859ebb67f [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"
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
35cl::list<std::string> InputFilenames(cl::Positional, cl::desc("<input file>"),
Mikhail Glushenkovd7bb87a2008-05-06 17:44:16 +000036 cl::ZeroOrMore);
Anton Korobeynikovac67b7e2008-03-23 08:57:20 +000037cl::opt<std::string> OutputFilename("o", cl::desc("Output file name"),
38 cl::value_desc("file"));
Mikhail Glushenkov87416b42008-05-06 18:10:53 +000039cl::list<std::string> Languages("x",
40 cl::desc("Specify the language of the following input files"),
41 cl::ZeroOrMore);
Mikhail Glushenkov0d08db02008-05-06 16:35:25 +000042cl::opt<bool> VerboseMode("v",
43 cl::desc("Enable verbose mode"));
44cl::opt<bool> WriteGraph("write-graph",
Mikhail Glushenkov3d688222008-05-06 18:07:48 +000045 cl::desc("Write compilation-graph.dot file"),
Mikhail Glushenkov0d08db02008-05-06 16:35:25 +000046 cl::Hidden);
47cl::opt<bool> ViewGraph("view-graph",
48 cl::desc("Show compilation graph in GhostView"),
49 cl::Hidden);
Anton Korobeynikovac67b7e2008-03-23 08:57:20 +000050
51namespace {
Mikhail Glushenkov4561ab52008-05-07 21:50:19 +000052 /// BuildTargets - A small wrapper for CompilationGraph::Build.
Mikhail Glushenkov02606582008-05-06 18:07:14 +000053 int BuildTargets(CompilationGraph& graph) {
Anton Korobeynikovac67b7e2008-03-23 08:57:20 +000054 int ret;
55 sys::Path tempDir(sys::Path::GetTemporaryDirectory());
56
57 try {
58 ret = graph.Build(tempDir);
59 }
60 catch(...) {
61 tempDir.eraseFromDisk(true);
62 throw;
63 }
64
65 tempDir.eraseFromDisk(true);
66 return ret;
67 }
68}
69
70int main(int argc, char** argv) {
71 try {
72 CompilationGraph graph;
73
Mikhail Glushenkov4eaa3892008-05-30 06:11:45 +000074 cl::ParseCommandLineOptions
75 (argc, argv, "LLVM Compiler Driver (Work In Progress)", true);
Anton Korobeynikovac67b7e2008-03-23 08:57:20 +000076 PopulateCompilationGraph(graph);
Mikhail Glushenkov0d08db02008-05-06 16:35:25 +000077
Mikhail Glushenkovd7bb87a2008-05-06 17:44:16 +000078 if (WriteGraph) {
Mikhail Glushenkov0d08db02008-05-06 16:35:25 +000079 graph.writeGraph();
Mikhail Glushenkov3d688222008-05-06 18:07:48 +000080 if (!ViewGraph)
81 return 0;
Mikhail Glushenkovd7bb87a2008-05-06 17:44:16 +000082 }
83
84 if (ViewGraph) {
Mikhail Glushenkov0d08db02008-05-06 16:35:25 +000085 graph.viewGraph();
Mikhail Glushenkovd7bb87a2008-05-06 17:44:16 +000086 return 0;
87 }
88
89 if (InputFilenames.empty()) {
90 std::cerr << "No input files.\n";
91 return 1;
92 }
Mikhail Glushenkov0d08db02008-05-06 16:35:25 +000093
Anton Korobeynikovac67b7e2008-03-23 08:57:20 +000094 return BuildTargets(graph);
95 }
Mikhail Glushenkova6730372008-05-12 16:31:42 +000096 catch(llvmc::error_code& ec) {
97 return ec.code();
98 }
Anton Korobeynikovac67b7e2008-03-23 08:57:20 +000099 catch(const std::exception& ex) {
100 std::cerr << ex.what() << '\n';
101 }
102 catch(...) {
103 std::cerr << "Unknown error!\n";
104 }
Mikhail Glushenkovd8188782008-05-06 17:25:23 +0000105 return 1;
Anton Korobeynikovac67b7e2008-03-23 08:57:20 +0000106}