1 //===--- CompilationGraph.cpp - The LLVM Compiler Driver --------*- C++ -*-===//
3 // The LLVM Compiler Infrastructure
5 // This file is distributed under the University of Illinois Open
6 // Source License. See LICENSE.TXT for details.
8 //===----------------------------------------------------------------------===//
10 // Compilation graph - implementation.
12 //===----------------------------------------------------------------------===//
14 #include "CompilationGraph.h"
16 #include "llvm/Support/CommandLine.h"
17 #include "llvm/Support/DOTGraphTraits.h"
18 #include "llvm/Support/GraphWriter.h"
23 using namespace llvmcc;
25 extern cl::list<std::string> InputFilenames;
26 extern cl::opt<std::string> OutputFilename;
28 // Choose one of the edges based on command-line options.
29 const Edge* Node::ChooseEdge() const {
30 const Edge* DefaultEdge = 0;
31 for (const_iterator B = EdgesBegin(), E = EdgesEnd();
33 const Edge* E = (*B).getPtr();
38 throw std::runtime_error("Node " + Name() +
39 ": multiple default edges found!"
40 "Most probably a specification error.");
48 throw std::runtime_error("Node " + Name() +
49 ": no suitable edge found! "
50 "Most probably a specification error.");
53 CompilationGraph::CompilationGraph() {
54 NodesMap["root"] = Node(this);
57 Node& CompilationGraph::getNode(const std::string& ToolName) {
58 nodes_map_type::iterator I = NodesMap.find(ToolName);
59 if (I == NodesMap.end())
60 throw std::runtime_error("Node " + ToolName + " is not in the graph");
64 const Node& CompilationGraph::getNode(const std::string& ToolName) const {
65 nodes_map_type::const_iterator I = NodesMap.find(ToolName);
66 if (I == NodesMap.end())
67 throw std::runtime_error("Node " + ToolName + " is not in the graph!");
71 const std::string& CompilationGraph::getLanguage(const sys::Path& File) const {
72 LanguageMap::const_iterator Lang = ExtsToLangs.find(File.getSuffix());
73 if (Lang == ExtsToLangs.end())
74 throw std::runtime_error("Unknown suffix: " + File.getSuffix() + '!');
78 const CompilationGraph::tools_vector_type&
79 CompilationGraph::getToolsVector(const std::string& LangName) const
81 tools_map_type::const_iterator I = ToolsMap.find(LangName);
82 if (I == ToolsMap.end())
83 throw std::runtime_error("No tools corresponding to " + LangName
88 void CompilationGraph::insertNode(Tool* V) {
89 if (NodesMap.count(V->Name()) == 0) {
93 NodesMap[V->Name()] = N;
97 void CompilationGraph::insertEdge(const std::string& A, Edge* E) {
98 Node& B = getNode(E->ToolName());
100 const std::string& InputLanguage = B.ToolPtr->InputLanguage();
101 ToolsMap[InputLanguage].push_back(E->ToolName());
102 NodesMap["root"].AddEdge(E);
105 Node& N = getNode(A);
108 // Increase the inward edge counter.
112 // Pass input file through the chain until we bump into a Join node or
113 // a node that says that it is the last.
115 CompilationGraph::PassThroughGraph (sys::Path& In, sys::Path Out,
116 const sys::Path& TempDir) const {
120 // Get to the head of the toolchain.
121 const tools_vector_type& TV = getToolsVector(getLanguage(In));
123 throw std::runtime_error("Tool names vector is empty!");
124 const Node* N = &getNode(*TV.begin());
127 Tool* CurTool = N->ToolPtr.getPtr();
129 if (CurTool->IsJoin()) {
130 ret = &dynamic_cast<JoinTool&>(*CurTool);
131 ret->AddToJoinList(In);
135 // Is this the last tool?
136 if (!N->HasChildren() || CurTool->IsLast()) {
137 // Check if the first tool is also the last
139 Out.set(In.getBasename());
141 Out.appendComponent(In.getBasename());
142 Out.appendSuffix(CurTool->OutputSuffix());
147 Out.appendComponent(In.getBasename());
148 Out.appendSuffix(CurTool->OutputSuffix());
149 Out.makeUnique(true, NULL);
153 if (CurTool->GenerateAction(In, Out).Execute() != 0)
154 throw std::runtime_error("Tool returned error code!");
156 N = &getNode(N->ChooseEdge()->ToolName());
157 In = Out; Out.clear();
163 int CompilationGraph::Build (const sys::Path& TempDir) const {
164 const JoinTool* JT = 0;
167 // For each input file
168 for (cl::list<std::string>::const_iterator B = InputFilenames.begin(),
169 E = InputFilenames.end(); B != E; ++B) {
172 const JoinTool* NewJoin = PassThroughGraph(In, Out, TempDir);
173 if (JT && NewJoin && JT != NewJoin)
174 throw std::runtime_error("Graphs with multiple Join nodes"
175 "are not yet supported!");
181 // If the final output name is empty, set it to "a.out"
182 if (!OutputFilename.empty()) {
183 Out = sys::Path(OutputFilename);
186 Out = sys::Path("a");
187 Out.appendSuffix(JT->OutputSuffix());
190 if (JT->GenerateAction(Out).Execute() != 0)
191 throw std::runtime_error("Tool returned error code!");
199 struct DOTGraphTraits<llvmcc::CompilationGraph*>
200 : public DefaultDOTGraphTraits
203 template<typename GraphType>
204 static std::string getNodeLabel(const Node* N, const GraphType&)
207 if (N->ToolPtr->IsJoin())
208 return N->Name() + "\n (join" +
209 (N->HasChildren() ? ")"
210 : std::string(": ") + N->ToolPtr->OutputLanguage() + ')');
217 template<typename EdgeIter>
218 static std::string getEdgeSourceLabel(const Node* N, EdgeIter I) {
220 return N->ToolPtr->OutputLanguage();
222 return I->ToolPtr->InputLanguage();
228 void CompilationGraph::writeGraph() {
229 std::ofstream O("CompilationGraph.dot");
232 llvm::WriteGraph(this, "CompilationGraph");
236 throw std::runtime_error("");
240 void CompilationGraph::viewGraph() {
241 llvm::ViewGraph(this, "compilation-graph");