blob: bd5ea2b73f4026b7604552d23ca2bf9db879150b [file] [log] [blame]
Mikhail Glushenkov2d3327f2008-05-30 06:20:54 +00001//===--- llvmc.cpp - The LLVM Compiler Driver -------------------*- C++ -*-===//
Anton Korobeynikove9ffb5b2008-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 Glushenkovafbeae92008-05-06 16:34:12 +000017#include "CompilationGraph.h"
Mikhail Glushenkovd61ed932008-05-12 16:31:42 +000018#include "Error.h"
Mikhail Glushenkov945522f2008-09-22 20:49:34 +000019#include "Plugin.h"
Anton Korobeynikove9ffb5b2008-03-23 08:57:20 +000020
21#include "llvm/System/Path.h"
22#include "llvm/Support/CommandLine.h"
Mikhail Glushenkov945522f2008-09-22 20:49:34 +000023#include "llvm/Support/PluginLoader.h"
Anton Korobeynikove9ffb5b2008-03-23 08:57:20 +000024
25#include <iostream>
26#include <stdexcept>
27#include <string>
28
Mikhail Glushenkovafbeae92008-05-06 16:34:12 +000029namespace cl = llvm::cl;
30namespace sys = llvm::sys;
Mikhail Glushenkov34307a92008-05-06 18:08:59 +000031using namespace llvmc;
Anton Korobeynikove9ffb5b2008-03-23 08:57:20 +000032
Mikhail Glushenkov2cfd2232008-05-06 16:35:25 +000033// Built-in command-line options.
Mikhail Glushenkovafbeae92008-05-06 16:34:12 +000034// External linkage here is intentional.
Mikhail Glushenkov2cfd2232008-05-06 16:35:25 +000035
36cl::list<std::string> InputFilenames(cl::Positional, cl::desc("<input file>"),
Mikhail Glushenkova5b33932008-05-06 17:44:16 +000037 cl::ZeroOrMore);
Anton Korobeynikove9ffb5b2008-03-23 08:57:20 +000038cl::opt<std::string> OutputFilename("o", cl::desc("Output file name"),
39 cl::value_desc("file"));
Mikhail Glushenkovedd7ff92008-05-06 18:10:53 +000040cl::list<std::string> Languages("x",
41 cl::desc("Specify the language of the following input files"),
42 cl::ZeroOrMore);
Mikhail Glushenkov0bd4dba2008-05-30 18:48:52 +000043cl::opt<bool> DryRun("dry-run",
Mikhail Glushenkov3134d842008-05-30 19:56:27 +000044 cl::desc("Only pretend to run commands"));
Mikhail Glushenkov2cfd2232008-05-06 16:35:25 +000045cl::opt<bool> VerboseMode("v",
46 cl::desc("Enable verbose mode"));
47cl::opt<bool> WriteGraph("write-graph",
Mikhail Glushenkovdfdb36a2008-05-06 18:07:48 +000048 cl::desc("Write compilation-graph.dot file"),
Mikhail Glushenkov2cfd2232008-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);
Mikhail Glushenkova5bdf6e2008-05-30 06:29:17 +000053cl::opt<bool> SaveTemps("save-temps",
54 cl::desc("Keep temporary files"),
55 cl::Hidden);
Anton Korobeynikove9ffb5b2008-03-23 08:57:20 +000056
57namespace {
Mikhail Glushenkovbe46ae12008-05-07 21:50:19 +000058 /// BuildTargets - A small wrapper for CompilationGraph::Build.
Mikhail Glushenkovcdbfa1a2008-09-22 20:47:46 +000059 int BuildTargets(CompilationGraph& graph, const LanguageMap& langMap) {
Anton Korobeynikove9ffb5b2008-03-23 08:57:20 +000060 int ret;
Mikhail Glushenkova5bdf6e2008-05-30 06:29:17 +000061 const sys::Path& tempDir = SaveTemps
62 ? sys::Path("")
63 : sys::Path(sys::Path::GetTemporaryDirectory());
Anton Korobeynikove9ffb5b2008-03-23 08:57:20 +000064
65 try {
Mikhail Glushenkovcdbfa1a2008-09-22 20:47:46 +000066 ret = graph.Build(tempDir, langMap);
Anton Korobeynikove9ffb5b2008-03-23 08:57:20 +000067 }
68 catch(...) {
69 tempDir.eraseFromDisk(true);
70 throw;
71 }
72
Mikhail Glushenkova5bdf6e2008-05-30 06:29:17 +000073 if (!SaveTemps)
74 tempDir.eraseFromDisk(true);
Anton Korobeynikove9ffb5b2008-03-23 08:57:20 +000075 return ret;
76 }
77}
78
79int main(int argc, char** argv) {
80 try {
Mikhail Glushenkovcdbfa1a2008-09-22 20:47:46 +000081 LanguageMap langMap;
Anton Korobeynikove9ffb5b2008-03-23 08:57:20 +000082 CompilationGraph graph;
83
Mikhail Glushenkov1a03d942008-05-30 06:11:45 +000084 cl::ParseCommandLineOptions
85 (argc, argv, "LLVM Compiler Driver (Work In Progress)", true);
Mikhail Glushenkovcdbfa1a2008-09-22 20:47:46 +000086
87 PopulateLanguageMap(langMap);
Anton Korobeynikove9ffb5b2008-03-23 08:57:20 +000088 PopulateCompilationGraph(graph);
Mikhail Glushenkov2cfd2232008-05-06 16:35:25 +000089
Mikhail Glushenkova5b33932008-05-06 17:44:16 +000090 if (WriteGraph) {
Mikhail Glushenkov2cfd2232008-05-06 16:35:25 +000091 graph.writeGraph();
Mikhail Glushenkovdfdb36a2008-05-06 18:07:48 +000092 if (!ViewGraph)
93 return 0;
Mikhail Glushenkova5b33932008-05-06 17:44:16 +000094 }
95
96 if (ViewGraph) {
Mikhail Glushenkov2cfd2232008-05-06 16:35:25 +000097 graph.viewGraph();
Mikhail Glushenkova5b33932008-05-06 17:44:16 +000098 return 0;
99 }
100
101 if (InputFilenames.empty()) {
Mikhail Glushenkovd29cc3a2008-05-30 06:26:35 +0000102 throw std::runtime_error("no input files");
Mikhail Glushenkova5b33932008-05-06 17:44:16 +0000103 }
Mikhail Glushenkov2cfd2232008-05-06 16:35:25 +0000104
Mikhail Glushenkovcdbfa1a2008-09-22 20:47:46 +0000105 return BuildTargets(graph, langMap);
Anton Korobeynikove9ffb5b2008-03-23 08:57:20 +0000106 }
Mikhail Glushenkovd61ed932008-05-12 16:31:42 +0000107 catch(llvmc::error_code& ec) {
108 return ec.code();
109 }
Anton Korobeynikove9ffb5b2008-03-23 08:57:20 +0000110 catch(const std::exception& ex) {
Mikhail Glushenkovd29cc3a2008-05-30 06:26:35 +0000111 std::cerr << argv[0] << ": " << ex.what() << '\n';
Anton Korobeynikove9ffb5b2008-03-23 08:57:20 +0000112 }
113 catch(...) {
Mikhail Glushenkovd29cc3a2008-05-30 06:26:35 +0000114 std::cerr << argv[0] << ": unknown error!\n";
Anton Korobeynikove9ffb5b2008-03-23 08:57:20 +0000115 }
Mikhail Glushenkov86b2c502008-05-06 17:25:23 +0000116 return 1;
Anton Korobeynikove9ffb5b2008-03-23 08:57:20 +0000117}