1 /*
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>
27 #include <llvm/Support/MemoryBuffer.h>
28 #include <llvm/Support/raw_ostream.h>
29 #include <llvm/Support/system_error.h>
30
31 #include <bcc/BCCContext.h>
32 #include <bcc/Compiler.h>
33 #include <bcc/Config/BuildInfo.h>
34 #include <bcc/Config/Config.h>
35 #include <bcc/ExecutionEngine/CompilerRTSymbolResolver.h>
36 #include <bcc/ExecutionEngine/ObjectLoader.h>
37 #include <bcc/ExecutionEngine/SymbolResolverProxy.h>
38 #include <bcc/ExecutionEngine/SymbolResolvers.h>
39 #include <bcc/Renderscript/RSCompilerDriver.h>
40 #include <bcc/Script.h>
41 #include <bcc/Source.h>
42 #include <bcc/Support/CompilerConfig.h>
43 #include <bcc/Support/Initialization.h>
44 #include <bcc/Support/InputFile.h>
45 #include <bcc/Support/OutputFile.h>
46 #include <bcc/Support/TargetCompilerConfigs.h>
47
48 using namespace bcc;
49
50 //===----------------------------------------------------------------------===//
51 // General Options
52 //===----------------------------------------------------------------------===//
53 namespace {
54
55 llvm::cl::opt<std::string>
56 OptInputFilename(llvm::cl::Positional, llvm::cl::ValueRequired,
57 llvm::cl::desc("<input bitcode file>"));
58
59 llvm::cl::opt<std::string>
60 OptOutputFilename("o", llvm::cl::desc("Specify the output filename"),
61 llvm::cl::value_desc("filename"),
62 llvm::cl::init("bcc_output"));
63
64 llvm::cl::opt<std::string>
65 OptBCLibFilename("bclib", llvm::cl::desc("Specify the bclib filename"),
66 llvm::cl::value_desc("bclib"));
67
68 llvm::cl::opt<std::string>
69 OptOutputPath("output_path", llvm::cl::desc("Specify the output path"),
70 llvm::cl::value_desc("output path"),
71 llvm::cl::init("."));
72
73 llvm::cl::opt<bool>
74 OptEmitLLVM("emit-llvm",
75 llvm::cl::desc("Emit an LLVM-IR version of the generated program"));
76
77 #ifdef TARGET_BUILD
78 const std::string OptTargetTriple(DEFAULT_TARGET_TRIPLE_STRING);
79 #else
80 llvm::cl::opt<std::string>
81 OptTargetTriple("mtriple",
82 llvm::cl::desc("Specify the target triple (default: "
83 DEFAULT_TARGET_TRIPLE_STRING ")"),
84 llvm::cl::init(DEFAULT_TARGET_TRIPLE_STRING),
85 llvm::cl::value_desc("triple"));
86
87 llvm::cl::alias OptTargetTripleC("C", llvm::cl::NotHidden,
88 llvm::cl::desc("Alias for -mtriple"),
89 llvm::cl::aliasopt(OptTargetTriple));
90 #endif
91
92 //===----------------------------------------------------------------------===//
93 // Compiler Options
94 //===----------------------------------------------------------------------===//
95
96 // RenderScript uses -O3 by default
97 llvm::cl::opt<char>
98 OptOptLevel("O", llvm::cl::desc("Optimization level. [-O0, -O1, -O2, or -O3] "
99 "(default: -O3)"),
100 llvm::cl::Prefix, llvm::cl::ZeroOrMore, llvm::cl::init('3'));
101
102 // Override "bcc -version" since the LLVM version information is not correct on
103 // Android build.
BCCVersionPrinter()104 void BCCVersionPrinter() {
105 llvm::raw_ostream &os = llvm::outs();
106 os << "libbcc (The Android Open Source Project, http://www.android.com/):\n"
107 << " Build time: " << BuildInfo::GetBuildTime() << "\n"
108 << " Build revision: " << BuildInfo::GetBuildRev() << "\n"
109 << " Build source blob: " << BuildInfo::GetBuildSourceBlob() << "\n"
110 << " Default target: " << DEFAULT_TARGET_TRIPLE_STRING << "\n";
111
112 os << "\n";
113
114 os << "LLVM (http://llvm.org/):\n"
115 << " Version: " << PACKAGE_VERSION << "\n";
116 return;
117 }
118
119 } // end anonymous namespace
120
121 static inline
ConfigCompiler(RSCompilerDriver & pRSCD)122 bool ConfigCompiler(RSCompilerDriver &pRSCD) {
123 RSCompiler *RSC = pRSCD.getCompiler();
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
136 switch (OptOptLevel) {
137 case '0': config->setOptimizationLevel(llvm::CodeGenOpt::None); break;
138 case '1': config->setOptimizationLevel(llvm::CodeGenOpt::Less); break;
139 case '2': config->setOptimizationLevel(llvm::CodeGenOpt::Default); break;
140 case '3':
141 default: {
142 config->setOptimizationLevel(llvm::CodeGenOpt::Aggressive);
143 break;
144 }
145 }
146
147 pRSCD.setConfig(config);
148 Compiler::ErrorCode result = RSC->config(*config);
149
150 if (result != Compiler::kSuccess) {
151 llvm::errs() << "Failed to configure the compiler! (detail: "
152 << Compiler::GetErrorString(result) << ")\n";
153 return false;
154 }
155
156 return true;
157 }
158
159 static inline
CompileScript(Compiler & pCompiler,Script & pScript,const std::string & pOutputPath)160 bool CompileScript(Compiler &pCompiler, Script &pScript,
161 const std::string &pOutputPath) {
162 // Open the output file.
163 OutputFile output_file(pOutputPath, FileBase::kTruncate);
164
165 if (output_file.hasError()) {
166 llvm::errs() << "Failed to open the output file `" << pOutputPath
167 << "'! (detail: " << output_file.getErrorMessage() << ")\n";
168 return false;
169 }
170
171 // Run the compiler.
172 Compiler::ErrorCode result = pCompiler.compile(pScript, output_file);
173 if (result != Compiler::kSuccess) {
174 llvm::errs() << "Fatal error during compilation (detail: "
175 << Compiler::GetErrorString(result) << ".)\n";
176 return false;
177 }
178
179 return true;
180 }
181
main(int argc,char ** argv)182 int main(int argc, char **argv) {
183 llvm::cl::SetVersionPrinter(BCCVersionPrinter);
184 llvm::cl::ParseCommandLineOptions(argc, argv);
185 init::Initialize();
186
187 BCCContext context;
188 RSCompilerDriver RSCD;
189
190 llvm::OwningPtr<llvm::MemoryBuffer> input_data;
191
192 llvm::error_code ec =
193 llvm::MemoryBuffer::getFile(OptInputFilename.c_str(), input_data);
194 if (ec != llvm::error_code::success()) {
195 ALOGE("Failed to load bitcode from path %s! (%s)",
196 OptInputFilename.c_str(), ec.message().c_str());
197 return EXIT_FAILURE;
198 }
199
200 llvm::MemoryBuffer *input_memory = input_data.take();
201
202 const char *bitcode = input_memory->getBufferStart();
203 size_t bitcodeSize = input_memory->getBufferSize();
204
205 if (!ConfigCompiler(RSCD)) {
206 ALOGE("Failed to configure compiler");
207 return EXIT_FAILURE;
208 }
209 bool built = RSCD.build(context, OptOutputPath.c_str(),
210 OptOutputFilename.c_str(), bitcode, bitcodeSize,
211 OptBCLibFilename.c_str(), NULL, OptEmitLLVM);
212
213 if (!built) {
214 return EXIT_FAILURE;
215 }
216
217 return EXIT_SUCCESS;
218 }
219