f6cbe0875435d550468fdf30589ed3fb40f3826f
[oota-llvm.git] / tools / llvmc2 / CompilationGraph.cpp
1 //===--- CompilationGraph.cpp - The LLVM Compiler Driver --------*- C++ -*-===//
2 //
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 //  Compilation graph - implementation.
11 //
12 //===----------------------------------------------------------------------===//
13
14 #include "CompilationGraph.h"
15
16 #include "llvm/Support/CommandLine.h"
17 #include "llvm/Support/DOTGraphTraits.h"
18 #include "llvm/Support/GraphWriter.h"
19
20 #include <stdexcept>
21
22 using namespace llvm;
23 using namespace llvmcc;
24
25 extern cl::list<std::string> InputFilenames;
26 extern cl::opt<std::string> OutputFilename;
27
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();
32        B != E; ++B) {
33     const Edge* E = (*B).getPtr();
34     if (E->isDefault())
35       if (!DefaultEdge)
36         DefaultEdge = E;
37       else
38         throw std::runtime_error("Node " + Name() +
39                                  ": multiple default edges found!"
40                                  "Most probably a specification error.");
41     if (E->isEnabled())
42       return E;
43   }
44
45   if (DefaultEdge)
46     return DefaultEdge;
47   else
48     throw std::runtime_error("Node " + Name() +
49                              ": no suitable edge found! "
50                              "Most probably a specification error.");
51 }
52
53 CompilationGraph::CompilationGraph() {
54   NodesMap["root"] = Node(this);
55 }
56
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");
61   return I->second;
62 }
63
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!");
68   return I->second;
69 }
70
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() + '!');
75   return Lang->second;
76 }
77
78 const CompilationGraph::tools_vector_type&
79 CompilationGraph::getToolsVector(const std::string& LangName) const
80 {
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
84                              + " found!");
85   return I->second;
86 }
87
88 void CompilationGraph::insertNode(Tool* V) {
89   if (!NodesMap.count(V->Name())) {
90     Node N;
91     N.OwningGraph = this;
92     N.ToolPtr = V;
93     NodesMap[V->Name()] = N;
94   }
95 }
96
97 void CompilationGraph::insertEdge(const std::string& A, Edge* E) {
98   if (A == "root") {
99     const Node& N = getNode(E->ToolName());
100     const std::string& InputLanguage = N.ToolPtr->InputLanguage();
101     ToolsMap[InputLanguage].push_back(E->ToolName());
102
103     // Needed to support iteration via GraphTraits.
104     NodesMap["root"].AddEdge(E);
105   }
106   else {
107     Node& N = getNode(A);
108     N.AddEdge(E);
109   }
110 }
111
112 // TOFIX: support more interesting graph topologies. We will need to
113 // do topological sorting to process multiple Join nodes correctly.
114 int CompilationGraph::Build (const sys::Path& tempDir) const {
115   PathVector JoinList;
116   const Tool* JoinTool = 0;
117   sys::Path In, Out;
118
119   // For each input file
120   for (cl::list<std::string>::const_iterator B = InputFilenames.begin(),
121         E = InputFilenames.end(); B != E; ++B) {
122     In = sys::Path(*B);
123
124     // Get to the head of the toolchain.
125     const tools_vector_type& TV = getToolsVector(getLanguage(In));
126     if (TV.empty())
127       throw std::runtime_error("Tool names vector is empty!");
128     const Node* N = &getNode(*TV.begin());
129
130     // Pass file through the chain until we bump into a Join node or a
131     // node that says that it is the last.
132     bool Last = false;
133     while(!Last) {
134       const Tool* CurTool = N->ToolPtr.getPtr();
135
136       if (CurTool->IsJoin()) {
137         JoinList.push_back(In);
138         JoinTool = CurTool;
139         break;
140       }
141
142       // Is this the last tool?
143       if (!N->HasChildren() || CurTool->IsLast()) {
144         // Check if the first tool is also the last
145         if (Out.empty())
146           Out.set(In.getBasename());
147         else
148           Out.appendComponent(In.getBasename());
149         Out.appendSuffix(CurTool->OutputSuffix());
150         Last = true;
151       }
152       else {
153         Out = tempDir;
154         Out.appendComponent(In.getBasename());
155         Out.appendSuffix(CurTool->OutputSuffix());
156         Out.makeUnique(true, NULL);
157         Out.eraseFromDisk();
158       }
159
160       if (CurTool->GenerateAction(In, Out).Execute() != 0)
161         throw std::runtime_error("Tool returned error code!");
162
163       N = &getNode(N->ChooseEdge()->ToolName());
164       In = Out; Out.clear();
165     }
166   }
167
168   if (JoinTool) {
169     // If the final output name is empty, set it to "a.out"
170     if (!OutputFilename.empty()) {
171       Out = sys::Path(OutputFilename);
172     }
173     else {
174       Out = sys::Path("a");
175       Out.appendSuffix(JoinTool->OutputSuffix());
176     }
177
178     if (JoinTool->GenerateAction(JoinList, Out).Execute() != 0)
179       throw std::runtime_error("Tool returned error code!");
180   }
181
182   return 0;
183 }
184
185 namespace llvm {
186   template <>
187   struct DOTGraphTraits<llvmcc::CompilationGraph*>
188     : public DefaultDOTGraphTraits
189   {
190
191   template<typename GraphType>
192   static std::string getNodeLabel(const Node* N, const GraphType&) {
193     if (N->ToolPtr)
194       return N->Name();
195     else
196       return "root";
197   }
198
199   };
200 }
201
202 void CompilationGraph::writeGraph() {
203   std::ofstream O("CompilationGraph.dot");
204
205   if (O.good()) {
206     llvm::WriteGraph(this, "CompilationGraph");
207     O.close();
208   }
209   else {
210     throw std::runtime_error("");
211   }
212 }
213
214 void CompilationGraph::viewGraph() {
215   llvm::ViewGraph(this, "compilation-graph");
216 }