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/ADT/STLExtras.h"
23 extern cl::list<std::string> InputFilenames;
24 extern cl::opt<std::string> OutputFilename;
26 int llvmcc::CompilationGraph::Build (const sys::Path& tempDir) const {
27 sys::Path In(InputFilenames.at(0)), Out;
29 // Find out which language corresponds to the suffix of the first input file
30 LanguageMap::const_iterator Lang = ExtsToLangs.find(In.getSuffix());
31 if (Lang == ExtsToLangs.end())
32 throw std::runtime_error("Unknown suffix!");
34 // Find the toolchain corresponding to this language
35 ToolChainMap::const_iterator ToolsIt = ToolChains.find(Lang->second);
36 if (ToolsIt == ToolChains.end())
37 throw std::runtime_error("Unknown language!");
38 ToolChain Tools = ToolsIt->second;
42 for (cl::list<std::string>::const_iterator B = InputFilenames.begin(),
43 E = InputFilenames.end(); B != E; ++B) {
46 // Pass input file through the toolchain
47 for (ToolChain::const_iterator B = Tools.begin(), E = Tools.end();
50 const Tool* CurTool = B->getPtr();
52 // Is this the last step in the chain?
53 if (llvm::next(B) == E || CurTool->IsLast()) {
54 JoinList.push_back(In);
59 Out.appendComponent(In.getBasename());
60 Out.appendSuffix(CurTool->OutputSuffix());
61 Out.makeUnique(true, NULL);
65 if (CurTool->GenerateAction(In, Out).Execute() != 0)
66 throw std::runtime_error("Tool returned error code!");
68 In = Out; Out.clear();
72 // Pass .o files to linker
73 const Tool* JoinNode = (--Tools.end())->getPtr();
75 // If the final output name is empty, set it to "a.out"
76 if (!OutputFilename.empty()) {
77 Out = sys::Path(OutputFilename);
81 Out.appendSuffix(JoinNode->OutputSuffix());
84 if (JoinNode->GenerateAction(JoinList, Out).Execute() != 0)
85 throw std::runtime_error("Tool returned error code!");