blob: ba2d70a62031d3e79f33c9ba0e63bc9a566c39ce [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: Add a --keep-temps option.
36// TOFIX: Write a 'driver driver' (easier to do as a separate
37// executable that drives llvmc2 proper).
Mikhail Glushenkov0d08db02008-05-06 16:35:25 +000038cl::list<std::string> InputFilenames(cl::Positional, cl::desc("<input file>"),
Mikhail Glushenkovd7bb87a2008-05-06 17:44:16 +000039 cl::ZeroOrMore);
Anton Korobeynikovac67b7e2008-03-23 08:57:20 +000040cl::opt<std::string> OutputFilename("o", cl::desc("Output file name"),
41 cl::value_desc("file"));
Mikhail Glushenkov87416b42008-05-06 18:10:53 +000042cl::list<std::string> Languages("x",
43 cl::desc("Specify the language of the following input files"),
44 cl::ZeroOrMore);
Mikhail Glushenkov0d08db02008-05-06 16:35:25 +000045cl::opt<bool> VerboseMode("v",
46 cl::desc("Enable verbose mode"));
47cl::opt<bool> WriteGraph("write-graph",
Mikhail Glushenkov3d688222008-05-06 18:07:48 +000048 cl::desc("Write compilation-graph.dot file"),
Mikhail Glushenkov0d08db02008-05-06 16:35:25 +000049 cl::Hidden);
50cl::opt<bool> ViewGraph("view-graph",
51 cl::desc("Show compilation graph in GhostView"),
52 cl::Hidden);
Anton Korobeynikovac67b7e2008-03-23 08:57:20 +000053
54namespace {
Mikhail Glushenkov4561ab52008-05-07 21:50:19 +000055 /// BuildTargets - A small wrapper for CompilationGraph::Build.
Mikhail Glushenkov02606582008-05-06 18:07:14 +000056 int BuildTargets(CompilationGraph& graph) {
Anton Korobeynikovac67b7e2008-03-23 08:57:20 +000057 int ret;
58 sys::Path tempDir(sys::Path::GetTemporaryDirectory());
59
60 try {
61 ret = graph.Build(tempDir);
62 }
63 catch(...) {
64 tempDir.eraseFromDisk(true);
65 throw;
66 }
67
68 tempDir.eraseFromDisk(true);
69 return ret;
70 }
71}
72
73int main(int argc, char** argv) {
74 try {
75 CompilationGraph graph;
76
Mikhail Glushenkov4eaa3892008-05-30 06:11:45 +000077 cl::ParseCommandLineOptions
78 (argc, argv, "LLVM Compiler Driver (Work In Progress)", true);
Anton Korobeynikovac67b7e2008-03-23 08:57:20 +000079 PopulateCompilationGraph(graph);
Mikhail Glushenkov0d08db02008-05-06 16:35:25 +000080
Mikhail Glushenkovd7bb87a2008-05-06 17:44:16 +000081 if (WriteGraph) {
Mikhail Glushenkov0d08db02008-05-06 16:35:25 +000082 graph.writeGraph();
Mikhail Glushenkov3d688222008-05-06 18:07:48 +000083 if (!ViewGraph)
84 return 0;
Mikhail Glushenkovd7bb87a2008-05-06 17:44:16 +000085 }
86
87 if (ViewGraph) {
Mikhail Glushenkov0d08db02008-05-06 16:35:25 +000088 graph.viewGraph();
Mikhail Glushenkovd7bb87a2008-05-06 17:44:16 +000089 return 0;
90 }
91
92 if (InputFilenames.empty()) {
Mikhail Glushenkoved3ba402008-05-30 06:26:35 +000093 throw std::runtime_error("no input files");
Mikhail Glushenkovd7bb87a2008-05-06 17:44:16 +000094 }
Mikhail Glushenkov0d08db02008-05-06 16:35:25 +000095
Anton Korobeynikovac67b7e2008-03-23 08:57:20 +000096 return BuildTargets(graph);
97 }
Mikhail Glushenkova6730372008-05-12 16:31:42 +000098 catch(llvmc::error_code& ec) {
99 return ec.code();
100 }
Anton Korobeynikovac67b7e2008-03-23 08:57:20 +0000101 catch(const std::exception& ex) {
Mikhail Glushenkoved3ba402008-05-30 06:26:35 +0000102 std::cerr << argv[0] << ": " << ex.what() << '\n';
Anton Korobeynikovac67b7e2008-03-23 08:57:20 +0000103 }
104 catch(...) {
Mikhail Glushenkoved3ba402008-05-30 06:26:35 +0000105 std::cerr << argv[0] << ": unknown error!\n";
Anton Korobeynikovac67b7e2008-03-23 08:57:20 +0000106 }
Mikhail Glushenkovd8188782008-05-06 17:25:23 +0000107 return 1;
Anton Korobeynikovac67b7e2008-03-23 08:57:20 +0000108}