blob: d20e49bb01baf166f776e07afc46cf66d6285106 [file] [log] [blame]
Shih-wei Liaod577d112012-04-25 04:06:29 -07001/*
2 * Copyright 2012, The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17#include <string>
18#include <vector>
19
20#include <stdlib.h>
21
22#include <llvm/ADT/STLExtras.h>
23#include <llvm/ADT/SmallString.h>
24#include <llvm/Config/config.h>
25#include <llvm/Support/CommandLine.h>
26#include <llvm/Support/FileSystem.h>
Stephen Hines8be8dba2013-06-18 09:53:43 -070027#include <llvm/Support/MemoryBuffer.h>
Stephen Hines88f8c522013-06-13 00:35:03 -070028#include <llvm/Support/PathV1.h>
Shih-wei Liaod577d112012-04-25 04:06:29 -070029#include <llvm/Support/Path.h>
30#include <llvm/Support/raw_ostream.h>
31#include <llvm/Support/system_error.h>
32
33#include <bcc/BCCContext.h>
34#include <bcc/Compiler.h>
35#include <bcc/Config/BuildInfo.h>
36#include <bcc/Config/Config.h>
Shih-wei Liao09ca9542013-01-25 17:00:08 -080037#include <bcc/ExecutionEngine/CompilerRTSymbolResolver.h>
Shih-wei Liaod577d112012-04-25 04:06:29 -070038#include <bcc/ExecutionEngine/ObjectLoader.h>
39#include <bcc/ExecutionEngine/SymbolResolverProxy.h>
40#include <bcc/ExecutionEngine/SymbolResolvers.h>
Stephen Hines8be8dba2013-06-18 09:53:43 -070041#include <bcc/Renderscript/RSCompilerDriver.h>
Shih-wei Liaod577d112012-04-25 04:06:29 -070042#include <bcc/Script.h>
43#include <bcc/Source.h>
44#include <bcc/Support/CompilerConfig.h>
45#include <bcc/Support/Initialization.h>
46#include <bcc/Support/InputFile.h>
47#include <bcc/Support/OutputFile.h>
48#include <bcc/Support/TargetCompilerConfigs.h>
49
50using namespace bcc;
51
52//===----------------------------------------------------------------------===//
53// General Options
54//===----------------------------------------------------------------------===//
55namespace {
56
Stephen Hines8be8dba2013-06-18 09:53:43 -070057llvm::cl::opt<std::string>
58OptInputFilename(llvm::cl::Positional, llvm::cl::ValueRequired,
59 llvm::cl::desc("<input bitcode file>"));
Shih-wei Liaod577d112012-04-25 04:06:29 -070060
61llvm::cl::opt<std::string>
62OptOutputFilename("o", llvm::cl::desc("Specify the output filename"),
Stephen Hines8be8dba2013-06-18 09:53:43 -070063 llvm::cl::value_desc("filename"),
64 llvm::cl::init("bcc_output"));
65
66llvm::cl::opt<std::string>
67OptBCLibFilename("bclib", llvm::cl::desc("Specify the bclib filename"),
68 llvm::cl::value_desc("bclib"));
69
70llvm::cl::opt<std::string>
71OptOutputPath("output_path", llvm::cl::desc("Specify the output path"),
72 llvm::cl::value_desc("output path"),
73 llvm::cl::init("."));
Shih-wei Liaod577d112012-04-25 04:06:29 -070074
Tobias Grosser7b980e12013-06-20 10:12:13 -070075llvm::cl::opt<bool>
76OptEmitLLVM("emit-llvm",
77 llvm::cl::desc("Emit an LLVM-IR version of the generated program"));
78
Shih-wei Liaod577d112012-04-25 04:06:29 -070079#ifdef TARGET_BUILD
80const std::string OptTargetTriple(DEFAULT_TARGET_TRIPLE_STRING);
81#else
82llvm::cl::opt<std::string>
83OptTargetTriple("mtriple",
84 llvm::cl::desc("Specify the target triple (default: "
85 DEFAULT_TARGET_TRIPLE_STRING ")"),
86 llvm::cl::init(DEFAULT_TARGET_TRIPLE_STRING),
87 llvm::cl::value_desc("triple"));
88
89llvm::cl::alias OptTargetTripleC("C", llvm::cl::NotHidden,
90 llvm::cl::desc("Alias for -mtriple"),
91 llvm::cl::aliasopt(OptTargetTriple));
92#endif
93
94//===----------------------------------------------------------------------===//
95// Compiler Options
96//===----------------------------------------------------------------------===//
Shih-wei Liaod577d112012-04-25 04:06:29 -070097
98llvm::cl::opt<char>
99OptOptLevel("O", llvm::cl::desc("Optimization level. [-O0, -O1, -O2, or -O3] "
100 "(default: -O2)"),
101 llvm::cl::Prefix, llvm::cl::ZeroOrMore, llvm::cl::init('2'));
102
Shih-wei Liaod577d112012-04-25 04:06:29 -0700103// Override "bcc -version" since the LLVM version information is not correct on
104// Android build.
105void BCCVersionPrinter() {
106 llvm::raw_ostream &os = llvm::outs();
107 os << "libbcc (The Android Open Source Project, http://www.android.com/):\n"
108 << " Build time: " << BuildInfo::GetBuildTime() << "\n"
109 << " Build revision: " << BuildInfo::GetBuildRev() << "\n"
110 << " Build source blob: " << BuildInfo::GetBuildSourceBlob() << "\n"
111 << " Default target: " << DEFAULT_TARGET_TRIPLE_STRING << "\n";
112
113 os << "\n";
114
115 os << "LLVM (http://llvm.org/):\n"
116 << " Version: " << PACKAGE_VERSION << "\n";
117 return;
118}
119
120} // end anonymous namespace
121
122static inline
Shih-wei Liaod577d112012-04-25 04:06:29 -0700123bool ConfigCompiler(Compiler &pCompiler) {
124 CompilerConfig *config = NULL;
125
126#ifdef TARGET_BUILD
127 config = new (std::nothrow) DefaultCompilerConfig();
128#else
129 config = new (std::nothrow) CompilerConfig(OptTargetTriple);
130#endif
131 if (config == NULL) {
132 llvm::errs() << "Out of memory when create the compiler configuration!\n";
133 return false;
134 }
135
Shih-wei Liaod577d112012-04-25 04:06:29 -0700136 switch (OptOptLevel) {
137 case '0': config->setOptimizationLevel(llvm::CodeGenOpt::None); break;
138 case '1': config->setOptimizationLevel(llvm::CodeGenOpt::Less); break;
139 case '3': config->setOptimizationLevel(llvm::CodeGenOpt::Aggressive); break;
140 case '2':
141 default: {
142 config->setOptimizationLevel(llvm::CodeGenOpt::Default);
143 break;
144 }
145 }
146
147 Compiler::ErrorCode result = pCompiler.config(*config);
148
149 delete config;
150
151 if (result != Compiler::kSuccess) {
152 llvm::errs() << "Failed to configure the compiler! (detail: "
153 << Compiler::GetErrorString(result) << ")\n";
154 return false;
155 }
156
157 return true;
158}
159
Shih-wei Liaod577d112012-04-25 04:06:29 -0700160static inline
161bool CompileScript(Compiler &pCompiler, Script &pScript,
162 const std::string &pOutputPath) {
163 // Open the output file.
Shih-wei Liaoc02eae62012-07-22 16:23:32 -0700164 OutputFile output_file(pOutputPath, FileBase::kTruncate);
Shih-wei Liaod577d112012-04-25 04:06:29 -0700165
166 if (output_file.hasError()) {
167 llvm::errs() << "Failed to open the output file `" << pOutputPath
168 << "'! (detail: " << output_file.getErrorMessage() << ")\n";
169 return false;
170 }
171
172 // Run the compiler.
173 Compiler::ErrorCode result = pCompiler.compile(pScript, output_file);
174 if (result != Compiler::kSuccess) {
175 llvm::errs() << "Fatal error during compilation (detail: "
176 << Compiler::GetErrorString(result) << ".)\n";
177 return false;
178 }
179
180 return true;
181}
182
Shih-wei Liaod577d112012-04-25 04:06:29 -0700183int main(int argc, char **argv) {
184 llvm::cl::SetVersionPrinter(BCCVersionPrinter);
185 llvm::cl::ParseCommandLineOptions(argc, argv);
186 init::Initialize();
187
188 BCCContext context;
Stephen Hines8be8dba2013-06-18 09:53:43 -0700189 RSCompilerDriver RSCD;
Shih-wei Liaod577d112012-04-25 04:06:29 -0700190
Stephen Hines8be8dba2013-06-18 09:53:43 -0700191 llvm::OwningPtr<llvm::MemoryBuffer> input_data;
192
193 llvm::error_code ec =
194 llvm::MemoryBuffer::getFile(OptInputFilename.c_str(), input_data);
195 if (ec != llvm::error_code::success()) {
196 ALOGE("Failed to load bitcode from path %s! (%s)",
197 OptInputFilename.c_str(), ec.message().c_str());
Shih-wei Liaod577d112012-04-25 04:06:29 -0700198 return EXIT_FAILURE;
199 }
200
Stephen Hines8be8dba2013-06-18 09:53:43 -0700201 llvm::MemoryBuffer *input_memory = input_data.take();
Shih-wei Liaod577d112012-04-25 04:06:29 -0700202
Stephen Hines8be8dba2013-06-18 09:53:43 -0700203 const char *bitcode = input_memory->getBufferStart();
204 size_t bitcodeSize = input_memory->getBufferSize();
Shih-wei Liaod577d112012-04-25 04:06:29 -0700205
Stephen Hines8be8dba2013-06-18 09:53:43 -0700206 bool built = RSCD.build(context, OptOutputPath.c_str(),
207 OptOutputFilename.c_str(), bitcode, bitcodeSize,
Tobias Grosser7b980e12013-06-20 10:12:13 -0700208 OptBCLibFilename.c_str(), NULL, OptEmitLLVM);
Stephen Hines8be8dba2013-06-18 09:53:43 -0700209
210 if (!built) {
Shih-wei Liaod577d112012-04-25 04:06:29 -0700211 return EXIT_FAILURE;
212 }
213
Shih-wei Liaod577d112012-04-25 04:06:29 -0700214 return EXIT_SUCCESS;
215}