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 CompilationGraph::CompilationGraph() {
29 NodesMap["root"] = Node(this);
32 Node& 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");
39 const 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!");
46 const std::string& CompilationGraph::getLanguage(const sys::Path& File) const {
47 LanguageMap::const_iterator Lang = ExtsToLangs.find(File.getSuffix());
48 if (Lang == ExtsToLangs.end())
49 throw std::runtime_error("Unknown suffix: " + File.getSuffix() + '!');
53 const CompilationGraph::tools_vector_type&
54 CompilationGraph::getToolsVector(const std::string& LangName) const
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
63 void CompilationGraph::insertNode(Tool* V) {
64 if (!NodesMap.count(V->Name())) {
68 NodesMap[V->Name()] = N;
72 void CompilationGraph::insertEdge(const std::string& A,
73 const std::string& B) {
74 // TOTHINK: check this at compile-time?
76 throw std::runtime_error("Edges back to the root are not allowed!"
77 "Compilation graph should be acyclic!");
80 const Node& N = getNode(B);
81 const std::string& InputLanguage = N.ToolPtr->InputLanguage();
82 ToolsMap[InputLanguage].push_back(B);
84 // Needed to support iteration via GraphTraits.
85 NodesMap["root"].AddEdge(new DefaultEdge(B));
89 // Check that there is a node at B.
91 N.AddEdge(new DefaultEdge(B));
95 // TOFIX: extend, add an ability to choose between different
96 // toolchains, support more interesting graph topologies.
97 int CompilationGraph::Build (const sys::Path& tempDir) const {
99 const Tool* JoinTool = 0;
102 // For each input file
103 for (cl::list<std::string>::const_iterator B = InputFilenames.begin(),
104 E = InputFilenames.end(); B != E; ++B) {
107 // Get to the head of the toolchain.
108 const tools_vector_type& TV = getToolsVector(getLanguage(In));
110 throw std::runtime_error("Tool names vector is empty!");
111 const Node* N = &getNode(*TV.begin());
113 // Pass it through the chain until we bump into a Join node or a
114 // node that says that it is the last.
117 const Tool* CurTool = N->ToolPtr.getPtr();
119 if(CurTool->IsJoin()) {
120 JoinList.push_back(In);
125 // Is this the last tool?
126 if (!N->HasChildren() || CurTool->IsLast()) {
127 Out.appendComponent(In.getBasename());
128 Out.appendSuffix(CurTool->OutputSuffix());
133 Out.appendComponent(In.getBasename());
134 Out.appendSuffix(CurTool->OutputSuffix());
135 Out.makeUnique(true, NULL);
139 if (CurTool->GenerateAction(In, Out).Execute() != 0)
140 throw std::runtime_error("Tool returned error code!");
142 N = &getNode((*N->EdgesBegin())->ToolName());
143 In = Out; Out.clear();
148 // If the final output name is empty, set it to "a.out"
149 if (!OutputFilename.empty()) {
150 Out = sys::Path(OutputFilename);
153 Out = sys::Path("a");
154 Out.appendSuffix(JoinTool->OutputSuffix());
157 if (JoinTool->GenerateAction(JoinList, Out).Execute() != 0)
158 throw std::runtime_error("Tool returned error code!");
166 struct DOTGraphTraits<llvmcc::CompilationGraph*>
167 : public DefaultDOTGraphTraits
170 template<typename GraphType>
171 static std::string getNodeLabel(const Node* N, const GraphType&) {
173 return N->ToolPtr->Name();
181 void CompilationGraph::writeGraph() {
182 std::ofstream O("CompilationGraph.dot");
185 llvm::WriteGraph(this, "CompilationGraph");
189 throw std::runtime_error("");
193 void CompilationGraph::viewGraph() {
194 llvm::ViewGraph(this, "CompilationGraph");