Add first proof-of-concept universal compiler driver framework based
[oota-llvm.git] / tools / llvmc2 / Core.cpp
1 //===--- Core.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 //  Core driver abstractions.
11 //
12 //===----------------------------------------------------------------------===//
13
14 #include "Core.h"
15 #include "Utility.h"
16
17 #include "llvm/ADT/STLExtras.h"
18 #include "llvm/ADT/StringExtras.h"
19 #include "llvm/Support/CommandLine.h"
20
21 #include <algorithm>
22 #include <iostream>
23 #include <stdexcept>
24
25 using namespace llvm;
26 using namespace llvmcc;
27
28 extern cl::list<std::string> InputFilenames;
29 extern cl::opt<std::string> OutputFilename;
30 extern cl::opt<bool> VerboseMode;
31
32 namespace {
33   void print_string (const std::string& str) {
34     std::cerr << str << ' ';
35   }
36 }
37
38 int llvmcc::Action::Execute() {
39   if (VerboseMode) {
40     std::cerr << Command_ << " ";
41     std::for_each(Args_.begin(), Args_.end(), print_string);
42     std::cerr << '\n';
43   }
44   return ExecuteProgram(Command_, Args_);
45 }
46
47 int llvmcc::CompilationGraph::Build (const sys::Path& tempDir) const {
48   sys::Path In(InputFilenames.at(0)), Out;
49
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!");
54
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;
60
61   PathVector JoinList;
62
63   for (cl::list<std::string>::const_iterator B = InputFilenames.begin(),
64         E = InputFilenames.end(); B != E; ++B) {
65     In = sys::Path(*B);
66
67     // Pass input file through the toolchain
68     for (ToolChain::const_iterator B = Tools.begin(), E = Tools.end();
69          B != E; ++B) {
70
71       const Tool* CurTool = B->getPtr();
72
73       // Is this the last step in the chain?
74       if (llvm::next(B) == E || CurTool->IsLast()) {
75         JoinList.push_back(In);
76         break;
77       }
78       else {
79         Out = tempDir;
80         Out.appendComponent(In.getBasename());
81         Out.appendSuffix(CurTool->OutputSuffix());
82         Out.makeUnique(true, NULL);
83         Out.eraseFromDisk();
84       }
85
86       if (CurTool->GenerateAction(In, Out).Execute() != 0)
87         throw std::runtime_error("Tool returned error code!");
88
89       In = Out; Out.clear();
90     }
91   }
92
93   // Pass .o files to linker
94   const Tool* JoinNode = (--Tools.end())->getPtr();
95
96   // If the final output name is empty, set it to "a.out"
97   if (!OutputFilename.empty()) {
98     Out = sys::Path(OutputFilename);
99   }
100   else {
101     Out = sys::Path("a");
102     Out.appendSuffix(JoinNode->OutputSuffix());
103   }
104
105   if (JoinNode->GenerateAction(JoinList, Out).Execute() != 0)
106     throw std::runtime_error("Tool returned error code!");
107
108   return 0;
109 }
110
111 void llvmcc::Tool::UnpackValues (const std::string& from,
112                                  std::vector<std::string>& to) const {
113   SplitString(from, to, ",");
114 }
115