blob: b965eb53e6bc3ee919bf953c4d370a4882bafe39 [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
Stephen Hines7e9c1852013-06-21 19:25:34 -070098// RenderScript uses -O3 by default
Shih-wei Liaod577d112012-04-25 04:06:29 -070099llvm::cl::opt<char>
100OptOptLevel("O", llvm::cl::desc("Optimization level. [-O0, -O1, -O2, or -O3] "
Stephen Hines7e9c1852013-06-21 19:25:34 -0700101 "(default: -O3)"),
102 llvm::cl::Prefix, llvm::cl::ZeroOrMore, llvm::cl::init('3'));
Shih-wei Liaod577d112012-04-25 04:06:29 -0700103
Shih-wei Liaod577d112012-04-25 04:06:29 -0700104// Override "bcc -version" since the LLVM version information is not correct on
105// Android build.
106void BCCVersionPrinter() {
107 llvm::raw_ostream &os = llvm::outs();
108 os << "libbcc (The Android Open Source Project, http://www.android.com/):\n"
109 << " Build time: " << BuildInfo::GetBuildTime() << "\n"
110 << " Build revision: " << BuildInfo::GetBuildRev() << "\n"
111 << " Build source blob: " << BuildInfo::GetBuildSourceBlob() << "\n"
112 << " Default target: " << DEFAULT_TARGET_TRIPLE_STRING << "\n";
113
114 os << "\n";
115
116 os << "LLVM (http://llvm.org/):\n"
117 << " Version: " << PACKAGE_VERSION << "\n";
118 return;
119}
120
121} // end anonymous namespace
122
123static inline
Stephen Hines7e9c1852013-06-21 19:25:34 -0700124bool ConfigCompiler(RSCompilerDriver &pRSCD) {
125 RSCompiler *RSC = pRSCD.getCompiler();
Shih-wei Liaod577d112012-04-25 04:06:29 -0700126 CompilerConfig *config = NULL;
127
128#ifdef TARGET_BUILD
129 config = new (std::nothrow) DefaultCompilerConfig();
130#else
131 config = new (std::nothrow) CompilerConfig(OptTargetTriple);
132#endif
133 if (config == NULL) {
134 llvm::errs() << "Out of memory when create the compiler configuration!\n";
135 return false;
136 }
137
Shih-wei Liaod577d112012-04-25 04:06:29 -0700138 switch (OptOptLevel) {
139 case '0': config->setOptimizationLevel(llvm::CodeGenOpt::None); break;
140 case '1': config->setOptimizationLevel(llvm::CodeGenOpt::Less); break;
Stephen Hines7e9c1852013-06-21 19:25:34 -0700141 case '2': config->setOptimizationLevel(llvm::CodeGenOpt::Default); break;
142 case '3':
Shih-wei Liaod577d112012-04-25 04:06:29 -0700143 default: {
Stephen Hines7e9c1852013-06-21 19:25:34 -0700144 config->setOptimizationLevel(llvm::CodeGenOpt::Aggressive);
Shih-wei Liaod577d112012-04-25 04:06:29 -0700145 break;
146 }
147 }
148
Stephen Hines7e9c1852013-06-21 19:25:34 -0700149 pRSCD.setConfig(config);
150 Compiler::ErrorCode result = RSC->config(*config);
Shih-wei Liaod577d112012-04-25 04:06:29 -0700151
152 if (result != Compiler::kSuccess) {
153 llvm::errs() << "Failed to configure the compiler! (detail: "
154 << Compiler::GetErrorString(result) << ")\n";
155 return false;
156 }
157
158 return true;
159}
160
Shih-wei Liaod577d112012-04-25 04:06:29 -0700161static inline
162bool CompileScript(Compiler &pCompiler, Script &pScript,
163 const std::string &pOutputPath) {
164 // Open the output file.
Shih-wei Liaoc02eae62012-07-22 16:23:32 -0700165 OutputFile output_file(pOutputPath, FileBase::kTruncate);
Shih-wei Liaod577d112012-04-25 04:06:29 -0700166
167 if (output_file.hasError()) {
168 llvm::errs() << "Failed to open the output file `" << pOutputPath
169 << "'! (detail: " << output_file.getErrorMessage() << ")\n";
170 return false;
171 }
172
173 // Run the compiler.
174 Compiler::ErrorCode result = pCompiler.compile(pScript, output_file);
175 if (result != Compiler::kSuccess) {
176 llvm::errs() << "Fatal error during compilation (detail: "
177 << Compiler::GetErrorString(result) << ".)\n";
178 return false;
179 }
180
181 return true;
182}
183
Shih-wei Liaod577d112012-04-25 04:06:29 -0700184int main(int argc, char **argv) {
185 llvm::cl::SetVersionPrinter(BCCVersionPrinter);
186 llvm::cl::ParseCommandLineOptions(argc, argv);
187 init::Initialize();
188
189 BCCContext context;
Stephen Hines8be8dba2013-06-18 09:53:43 -0700190 RSCompilerDriver RSCD;
Shih-wei Liaod577d112012-04-25 04:06:29 -0700191
Stephen Hines8be8dba2013-06-18 09:53:43 -0700192 llvm::OwningPtr<llvm::MemoryBuffer> input_data;
193
194 llvm::error_code ec =
195 llvm::MemoryBuffer::getFile(OptInputFilename.c_str(), input_data);
196 if (ec != llvm::error_code::success()) {
197 ALOGE("Failed to load bitcode from path %s! (%s)",
198 OptInputFilename.c_str(), ec.message().c_str());
Shih-wei Liaod577d112012-04-25 04:06:29 -0700199 return EXIT_FAILURE;
200 }
201
Stephen Hines8be8dba2013-06-18 09:53:43 -0700202 llvm::MemoryBuffer *input_memory = input_data.take();
Shih-wei Liaod577d112012-04-25 04:06:29 -0700203
Stephen Hines8be8dba2013-06-18 09:53:43 -0700204 const char *bitcode = input_memory->getBufferStart();
205 size_t bitcodeSize = input_memory->getBufferSize();
Shih-wei Liaod577d112012-04-25 04:06:29 -0700206
Stephen Hines7e9c1852013-06-21 19:25:34 -0700207 if (!ConfigCompiler(RSCD)) {
208 ALOGE("Failed to configure compiler");
209 return EXIT_FAILURE;
210 }
Stephen Hines8be8dba2013-06-18 09:53:43 -0700211 bool built = RSCD.build(context, OptOutputPath.c_str(),
212 OptOutputFilename.c_str(), bitcode, bitcodeSize,
Tobias Grosser7b980e12013-06-20 10:12:13 -0700213 OptBCLibFilename.c_str(), NULL, OptEmitLLVM);
Stephen Hines8be8dba2013-06-18 09:53:43 -0700214
215 if (!built) {
Shih-wei Liaod577d112012-04-25 04:06:29 -0700216 return EXIT_FAILURE;
217 }
218
Shih-wei Liaod577d112012-04-25 04:06:29 -0700219 return EXIT_SUCCESS;
220}