blob: 08a59890eb65cf9a8933584777f8bdaf09274036 [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
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 Glushenkov7ef36062008-05-30 18:48:52 +000042cl::opt<bool> DryRun("dry-run",
Mikhail Glushenkov74a0c282008-05-30 19:56:27 +000043 cl::desc("Only pretend to run commands"));
Mikhail Glushenkov0d08db02008-05-06 16:35:25 +000044cl::opt<bool> VerboseMode("v",
45 cl::desc("Enable verbose mode"));
46cl::opt<bool> WriteGraph("write-graph",
Mikhail Glushenkov3d688222008-05-06 18:07:48 +000047 cl::desc("Write compilation-graph.dot file"),
Mikhail Glushenkov0d08db02008-05-06 16:35:25 +000048 cl::Hidden);
49cl::opt<bool> ViewGraph("view-graph",
50 cl::desc("Show compilation graph in GhostView"),
51 cl::Hidden);
Mikhail Glushenkov73296102008-05-30 06:29:17 +000052cl::opt<bool> SaveTemps("save-temps",
53 cl::desc("Keep temporary files"),
54 cl::Hidden);
Anton Korobeynikovac67b7e2008-03-23 08:57:20 +000055
56namespace {
Mikhail Glushenkov4561ab52008-05-07 21:50:19 +000057 /// BuildTargets - A small wrapper for CompilationGraph::Build.
Mikhail Glushenkov11a353a2008-09-22 20:47:46 +000058 int BuildTargets(CompilationGraph& graph, const LanguageMap& langMap) {
Anton Korobeynikovac67b7e2008-03-23 08:57:20 +000059 int ret;
Mikhail Glushenkov73296102008-05-30 06:29:17 +000060 const sys::Path& tempDir = SaveTemps
61 ? sys::Path("")
62 : sys::Path(sys::Path::GetTemporaryDirectory());
Anton Korobeynikovac67b7e2008-03-23 08:57:20 +000063
64 try {
Mikhail Glushenkov11a353a2008-09-22 20:47:46 +000065 ret = graph.Build(tempDir, langMap);
Anton Korobeynikovac67b7e2008-03-23 08:57:20 +000066 }
67 catch(...) {
68 tempDir.eraseFromDisk(true);
69 throw;
70 }
71
Mikhail Glushenkov73296102008-05-30 06:29:17 +000072 if (!SaveTemps)
73 tempDir.eraseFromDisk(true);
Anton Korobeynikovac67b7e2008-03-23 08:57:20 +000074 return ret;
75 }
76}
77
78int main(int argc, char** argv) {
79 try {
Mikhail Glushenkov11a353a2008-09-22 20:47:46 +000080 LanguageMap langMap;
Anton Korobeynikovac67b7e2008-03-23 08:57:20 +000081 CompilationGraph graph;
82
Mikhail Glushenkov4eaa3892008-05-30 06:11:45 +000083 cl::ParseCommandLineOptions
84 (argc, argv, "LLVM Compiler Driver (Work In Progress)", true);
Mikhail Glushenkov11a353a2008-09-22 20:47:46 +000085
86 PopulateLanguageMap(langMap);
Anton Korobeynikovac67b7e2008-03-23 08:57:20 +000087 PopulateCompilationGraph(graph);
Mikhail Glushenkov0d08db02008-05-06 16:35:25 +000088
Mikhail Glushenkovd7bb87a2008-05-06 17:44:16 +000089 if (WriteGraph) {
Mikhail Glushenkov0d08db02008-05-06 16:35:25 +000090 graph.writeGraph();
Mikhail Glushenkov3d688222008-05-06 18:07:48 +000091 if (!ViewGraph)
92 return 0;
Mikhail Glushenkovd7bb87a2008-05-06 17:44:16 +000093 }
94
95 if (ViewGraph) {
Mikhail Glushenkov0d08db02008-05-06 16:35:25 +000096 graph.viewGraph();
Mikhail Glushenkovd7bb87a2008-05-06 17:44:16 +000097 return 0;
98 }
99
100 if (InputFilenames.empty()) {
Mikhail Glushenkoved3ba402008-05-30 06:26:35 +0000101 throw std::runtime_error("no input files");
Mikhail Glushenkovd7bb87a2008-05-06 17:44:16 +0000102 }
Mikhail Glushenkov0d08db02008-05-06 16:35:25 +0000103
Mikhail Glushenkov11a353a2008-09-22 20:47:46 +0000104 return BuildTargets(graph, langMap);
Anton Korobeynikovac67b7e2008-03-23 08:57:20 +0000105 }
Mikhail Glushenkova6730372008-05-12 16:31:42 +0000106 catch(llvmc::error_code& ec) {
107 return ec.code();
108 }
Anton Korobeynikovac67b7e2008-03-23 08:57:20 +0000109 catch(const std::exception& ex) {
Mikhail Glushenkoved3ba402008-05-30 06:26:35 +0000110 std::cerr << argv[0] << ": " << ex.what() << '\n';
Anton Korobeynikovac67b7e2008-03-23 08:57:20 +0000111 }
112 catch(...) {
Mikhail Glushenkoved3ba402008-05-30 06:26:35 +0000113 std::cerr << argv[0] << ": unknown error!\n";
Anton Korobeynikovac67b7e2008-03-23 08:57:20 +0000114 }
Mikhail Glushenkovd8188782008-05-06 17:25:23 +0000115 return 1;
Anton Korobeynikovac67b7e2008-03-23 08:57:20 +0000116}