blob: 07b09e7390b7971986a3c5c9e9f3c170e67f45a0 [file] [log] [blame]
Mikhail Glushenkovb90cd832008-05-06 16:34:12 +00001//===--- CompilationGraph.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//
Mikhail Glushenkovb90cd832008-05-06 16:34:12 +000010// Compilation graph - implementation.
Anton Korobeynikovac67b7e2008-03-23 08:57:20 +000011//
12//===----------------------------------------------------------------------===//
13
Mikhail Glushenkovb90cd832008-05-06 16:34:12 +000014#include "CompilationGraph.h"
Anton Korobeynikovac67b7e2008-03-23 08:57:20 +000015
Anton Korobeynikovac67b7e2008-03-23 08:57:20 +000016#include "llvm/Support/CommandLine.h"
Mikhail Glushenkov0d08db02008-05-06 16:35:25 +000017#include "llvm/Support/DOTGraphTraits.h"
18#include "llvm/Support/GraphWriter.h"
Anton Korobeynikovac67b7e2008-03-23 08:57:20 +000019
Anton Korobeynikovac67b7e2008-03-23 08:57:20 +000020#include <stdexcept>
21
22using namespace llvm;
Mikhail Glushenkov0d08db02008-05-06 16:35:25 +000023using namespace llvmcc;
Anton Korobeynikovac67b7e2008-03-23 08:57:20 +000024
25extern cl::list<std::string> InputFilenames;
26extern cl::opt<std::string> OutputFilename;
Anton Korobeynikovac67b7e2008-03-23 08:57:20 +000027
Mikhail Glushenkov0d08db02008-05-06 16:35:25 +000028CompilationGraph::CompilationGraph() {
29 NodesMap["root"] = Node(this);
30}
Anton Korobeynikovac67b7e2008-03-23 08:57:20 +000031
Mikhail Glushenkov0d08db02008-05-06 16:35:25 +000032Node& CompilationGraph::getNode(const std::string& ToolName) {
33 nodes_map_type::iterator I = NodesMap.find(ToolName);
34 if (I == NodesMap.end())
35 throw std::runtime_error("Node " + ToolName + " is not in graph");
36 return I->second;
37}
38
39const Node& CompilationGraph::getNode(const std::string& ToolName) const {
40 nodes_map_type::const_iterator I = NodesMap.find(ToolName);
41 if (I == NodesMap.end())
42 throw std::runtime_error("Node " + ToolName + " is not in graph!");
43 return I->second;
44}
45
46const std::string& CompilationGraph::getLanguage(const sys::Path& File) const {
47 LanguageMap::const_iterator Lang = ExtsToLangs.find(File.getSuffix());
Anton Korobeynikovac67b7e2008-03-23 08:57:20 +000048 if (Lang == ExtsToLangs.end())
Mikhail Glushenkov0d08db02008-05-06 16:35:25 +000049 throw std::runtime_error("Unknown suffix: " + File.getSuffix() + '!');
50 return Lang->second;
51}
Anton Korobeynikovac67b7e2008-03-23 08:57:20 +000052
Mikhail Glushenkov0d08db02008-05-06 16:35:25 +000053const CompilationGraph::tools_vector_type&
54CompilationGraph::getToolsVector(const std::string& LangName) const
55{
56 tools_map_type::const_iterator I = ToolsMap.find(LangName);
57 if (I == ToolsMap.end())
58 throw std::runtime_error("No tools corresponding to " + LangName
59 + " found!");
60 return I->second;
61}
Anton Korobeynikovac67b7e2008-03-23 08:57:20 +000062
Mikhail Glushenkov0a174932008-05-06 16:36:06 +000063void CompilationGraph::insertNode(Tool* V) {
Mikhail Glushenkov0d08db02008-05-06 16:35:25 +000064 if (!NodesMap.count(V->Name())) {
65 Node N;
66 N.OwningGraph = this;
67 N.ToolPtr = V;
68 NodesMap[V->Name()] = N;
69 }
70}
71
72void CompilationGraph::insertEdge(const std::string& A,
73 const std::string& B) {
74 // TOTHINK: check this at compile-time?
75 if (B == "root")
76 throw std::runtime_error("Edges back to the root are not allowed!"
77 "Compilation graph should be acyclic!");
78
79 if (A == "root") {
80 const Node& N = getNode(B);
81 const std::string& InputLanguage = N.ToolPtr->InputLanguage();
82 ToolsMap[InputLanguage].push_back(B);
83
84 // Needed to support iteration via GraphTraits.
Mikhail Glushenkov0a174932008-05-06 16:36:06 +000085 NodesMap["root"].AddEdge(new DefaultEdge(B));
Mikhail Glushenkov0d08db02008-05-06 16:35:25 +000086 }
87 else {
Mikhail Glushenkov0a174932008-05-06 16:36:06 +000088 Node& N = getNode(A);
Mikhail Glushenkov0d08db02008-05-06 16:35:25 +000089 // Check that there is a node at B.
90 getNode(B);
Mikhail Glushenkov0a174932008-05-06 16:36:06 +000091 N.AddEdge(new DefaultEdge(B));
Mikhail Glushenkov0d08db02008-05-06 16:35:25 +000092 }
93}
94
95// TOFIX: extend, add an ability to choose between different
96// toolchains, support more interesting graph topologies.
97int CompilationGraph::Build (const sys::Path& tempDir) const {
Anton Korobeynikovac67b7e2008-03-23 08:57:20 +000098 PathVector JoinList;
Mikhail Glushenkov0d08db02008-05-06 16:35:25 +000099 const Tool* JoinTool = 0;
100 sys::Path In, Out;
Anton Korobeynikovac67b7e2008-03-23 08:57:20 +0000101
Mikhail Glushenkov0d08db02008-05-06 16:35:25 +0000102 // For each input file
Anton Korobeynikovac67b7e2008-03-23 08:57:20 +0000103 for (cl::list<std::string>::const_iterator B = InputFilenames.begin(),
104 E = InputFilenames.end(); B != E; ++B) {
105 In = sys::Path(*B);
106
Mikhail Glushenkov0d08db02008-05-06 16:35:25 +0000107 // Get to the head of the toolchain.
108 const tools_vector_type& TV = getToolsVector(getLanguage(In));
109 if(TV.empty())
110 throw std::runtime_error("Tool names vector is empty!");
111 const Node* N = &getNode(*TV.begin());
Anton Korobeynikovac67b7e2008-03-23 08:57:20 +0000112
Mikhail Glushenkov0d08db02008-05-06 16:35:25 +0000113 // Pass it through the chain until we bump into a Join node or a
114 // node that says that it is the last.
115 bool Last = false;
116 while(!Last) {
117 const Tool* CurTool = N->ToolPtr.getPtr();
Anton Korobeynikovac67b7e2008-03-23 08:57:20 +0000118
Mikhail Glushenkov0d08db02008-05-06 16:35:25 +0000119 if(CurTool->IsJoin()) {
Anton Korobeynikovac67b7e2008-03-23 08:57:20 +0000120 JoinList.push_back(In);
Mikhail Glushenkov0d08db02008-05-06 16:35:25 +0000121 JoinTool = CurTool;
Anton Korobeynikovac67b7e2008-03-23 08:57:20 +0000122 break;
123 }
Mikhail Glushenkov0d08db02008-05-06 16:35:25 +0000124
125 // Is this the last tool?
Mikhail Glushenkov0a174932008-05-06 16:36:06 +0000126 if (!N->HasChildren() || CurTool->IsLast()) {
Mikhail Glushenkov0d08db02008-05-06 16:35:25 +0000127 Out.appendComponent(In.getBasename());
128 Out.appendSuffix(CurTool->OutputSuffix());
129 Last = true;
130 }
Anton Korobeynikovac67b7e2008-03-23 08:57:20 +0000131 else {
132 Out = tempDir;
133 Out.appendComponent(In.getBasename());
134 Out.appendSuffix(CurTool->OutputSuffix());
135 Out.makeUnique(true, NULL);
136 Out.eraseFromDisk();
137 }
138
139 if (CurTool->GenerateAction(In, Out).Execute() != 0)
140 throw std::runtime_error("Tool returned error code!");
141
Mikhail Glushenkov0a174932008-05-06 16:36:06 +0000142 N = &getNode((*N->EdgesBegin())->ToolName());
Anton Korobeynikovac67b7e2008-03-23 08:57:20 +0000143 In = Out; Out.clear();
144 }
145 }
146
Mikhail Glushenkov0d08db02008-05-06 16:35:25 +0000147 if(JoinTool) {
148 // If the final output name is empty, set it to "a.out"
149 if (!OutputFilename.empty()) {
150 Out = sys::Path(OutputFilename);
151 }
152 else {
153 Out = sys::Path("a");
154 Out.appendSuffix(JoinTool->OutputSuffix());
155 }
Anton Korobeynikovac67b7e2008-03-23 08:57:20 +0000156
Mikhail Glushenkov0d08db02008-05-06 16:35:25 +0000157 if (JoinTool->GenerateAction(JoinList, Out).Execute() != 0)
158 throw std::runtime_error("Tool returned error code!");
Anton Korobeynikovac67b7e2008-03-23 08:57:20 +0000159 }
Anton Korobeynikovac67b7e2008-03-23 08:57:20 +0000160
161 return 0;
162}
Mikhail Glushenkov0d08db02008-05-06 16:35:25 +0000163
164namespace llvm {
165 template <>
166 struct DOTGraphTraits<llvmcc::CompilationGraph*>
167 : public DefaultDOTGraphTraits
168 {
169
170 template<typename GraphType>
171 static std::string getNodeLabel(const Node* N, const GraphType&) {
172 if (N->ToolPtr)
173 return N->ToolPtr->Name();
174 else
175 return "root";
176 }
177
178 };
179}
180
181void CompilationGraph::writeGraph() {
182 std::ofstream O("CompilationGraph.dot");
183
184 if(O.good()) {
185 llvm::WriteGraph(this, "CompilationGraph");
186 O.close();
187 }
188 else {
189 throw std::runtime_error("");
190 }
191}
192
193void CompilationGraph::viewGraph() {
194 llvm::ViewGraph(this, "CompilationGraph");
195}