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 the 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 the 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, Edge* E) {
74 const Node& N = getNode(E->ToolName());
75 const std::string& InputLanguage = N.ToolPtr->InputLanguage();
76 ToolsMap[InputLanguage].push_back(E->ToolName());
78 // Needed to support iteration via GraphTraits.
79 NodesMap["root"].AddEdge(E);
87 // TOFIX: support edge properties.
88 // TOFIX: support more interesting graph topologies.
89 int CompilationGraph::Build (const sys::Path& tempDir) const {
91 const Tool* JoinTool = 0;
94 // For each input file
95 for (cl::list<std::string>::const_iterator B = InputFilenames.begin(),
96 E = InputFilenames.end(); B != E; ++B) {
99 // Get to the head of the toolchain.
100 const tools_vector_type& TV = getToolsVector(getLanguage(In));
102 throw std::runtime_error("Tool names vector is empty!");
103 const Node* N = &getNode(*TV.begin());
105 // Pass it through the chain until we bump into a Join node or a
106 // node that says that it is the last.
109 const Tool* CurTool = N->ToolPtr.getPtr();
111 if (CurTool->IsJoin()) {
112 JoinList.push_back(In);
117 // Is this the last tool?
118 if (!N->HasChildren() || CurTool->IsLast()) {
119 // Check if the first tool is also the last
121 Out.set(In.getBasename());
123 Out.appendComponent(In.getBasename());
124 Out.appendSuffix(CurTool->OutputSuffix());
129 Out.appendComponent(In.getBasename());
130 Out.appendSuffix(CurTool->OutputSuffix());
131 Out.makeUnique(true, NULL);
135 if (CurTool->GenerateAction(In, Out).Execute() != 0)
136 throw std::runtime_error("Tool returned error code!");
138 N = &getNode((*N->EdgesBegin())->ToolName());
139 In = Out; Out.clear();
144 // If the final output name is empty, set it to "a.out"
145 if (!OutputFilename.empty()) {
146 Out = sys::Path(OutputFilename);
149 Out = sys::Path("a");
150 Out.appendSuffix(JoinTool->OutputSuffix());
153 if (JoinTool->GenerateAction(JoinList, Out).Execute() != 0)
154 throw std::runtime_error("Tool returned error code!");
162 struct DOTGraphTraits<llvmcc::CompilationGraph*>
163 : public DefaultDOTGraphTraits
166 template<typename GraphType>
167 static std::string getNodeLabel(const Node* N, const GraphType&) {
169 return N->ToolPtr->Name();
177 void CompilationGraph::writeGraph() {
178 std::ofstream O("CompilationGraph.dot");
181 llvm::WriteGraph(this, "CompilationGraph");
185 throw std::runtime_error("");
189 void CompilationGraph::viewGraph() {
190 llvm::ViewGraph(this, "compilation-graph");