1 //===--- Core.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 // Core driver abstractions.
12 //===----------------------------------------------------------------------===//
17 #include "llvm/ADT/STLExtras.h"
18 #include "llvm/ADT/StringExtras.h"
19 #include "llvm/Support/CommandLine.h"
26 using namespace llvmcc;
28 extern cl::list<std::string> InputFilenames;
29 extern cl::opt<std::string> OutputFilename;
30 extern cl::opt<bool> VerboseMode;
33 void print_string (const std::string& str) {
34 std::cerr << str << ' ';
38 int llvmcc::Action::Execute() {
40 std::cerr << Command_ << " ";
41 std::for_each(Args_.begin(), Args_.end(), print_string);
44 return ExecuteProgram(Command_, Args_);
47 int llvmcc::CompilationGraph::Build (const sys::Path& tempDir) const {
48 sys::Path In(InputFilenames.at(0)), Out;
50 // Find out which language corresponds to the suffix of the first input file
51 LanguageMap::const_iterator Lang = ExtsToLangs.find(In.getSuffix());
52 if (Lang == ExtsToLangs.end())
53 throw std::runtime_error("Unknown suffix!");
55 // Find the toolchain corresponding to this language
56 ToolChainMap::const_iterator ToolsIt = ToolChains.find(Lang->second);
57 if (ToolsIt == ToolChains.end())
58 throw std::runtime_error("Unknown language!");
59 ToolChain Tools = ToolsIt->second;
63 for (cl::list<std::string>::const_iterator B = InputFilenames.begin(),
64 E = InputFilenames.end(); B != E; ++B) {
67 // Pass input file through the toolchain
68 for (ToolChain::const_iterator B = Tools.begin(), E = Tools.end();
71 const Tool* CurTool = B->getPtr();
73 // Is this the last step in the chain?
74 if (llvm::next(B) == E || CurTool->IsLast()) {
75 JoinList.push_back(In);
80 Out.appendComponent(In.getBasename());
81 Out.appendSuffix(CurTool->OutputSuffix());
82 Out.makeUnique(true, NULL);
86 if (CurTool->GenerateAction(In, Out).Execute() != 0)
87 throw std::runtime_error("Tool returned error code!");
89 In = Out; Out.clear();
93 // Pass .o files to linker
94 const Tool* JoinNode = (--Tools.end())->getPtr();
96 // If the final output name is empty, set it to "a.out"
97 if (!OutputFilename.empty()) {
98 Out = sys::Path(OutputFilename);
101 Out = sys::Path("a");
102 Out.appendSuffix(JoinNode->OutputSuffix());
105 if (JoinNode->GenerateAction(JoinList, Out).Execute() != 0)
106 throw std::runtime_error("Tool returned error code!");
111 void llvmcc::Tool::UnpackValues (const std::string& from,
112 std::vector<std::string>& to) const {
113 SplitString(from, to, ",");