blob: 959f9e6133a9707455c7a8ce4f80aa0c68a08ccc [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
Stephen Hinesc3437f02014-01-30 17:57:21 -080020#include <dlfcn.h>
Shih-wei Liaod577d112012-04-25 04:06:29 -070021#include <stdlib.h>
22
23#include <llvm/ADT/STLExtras.h>
24#include <llvm/ADT/SmallString.h>
25#include <llvm/Config/config.h>
26#include <llvm/Support/CommandLine.h>
27#include <llvm/Support/FileSystem.h>
Stephen Hines8be8dba2013-06-18 09:53:43 -070028#include <llvm/Support/MemoryBuffer.h>
Stephen Hinesc3437f02014-01-30 17:57:21 -080029#include <llvm/Support/PluginLoader.h>
Shih-wei Liaod577d112012-04-25 04:06:29 -070030#include <llvm/Support/raw_ostream.h>
Shih-wei Liaod577d112012-04-25 04:06:29 -070031
32#include <bcc/BCCContext.h>
33#include <bcc/Compiler.h>
Shih-wei Liaod577d112012-04-25 04:06:29 -070034#include <bcc/Config/Config.h>
Shih-wei Liao09ca9542013-01-25 17:00:08 -080035#include <bcc/ExecutionEngine/CompilerRTSymbolResolver.h>
Shih-wei Liaod577d112012-04-25 04:06:29 -070036#include <bcc/ExecutionEngine/ObjectLoader.h>
37#include <bcc/ExecutionEngine/SymbolResolverProxy.h>
38#include <bcc/ExecutionEngine/SymbolResolvers.h>
Stephen Hines8be8dba2013-06-18 09:53:43 -070039#include <bcc/Renderscript/RSCompilerDriver.h>
Shih-wei Liaod577d112012-04-25 04:06:29 -070040#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>
Shih-wei Liaod577d112012-04-25 04:06:29 -070046
47using namespace bcc;
48
Stephen Hinesc3437f02014-01-30 17:57:21 -080049#define STR2(a) #a
50#define STR(a) STR2(a)
51
Shih-wei Liaod577d112012-04-25 04:06:29 -070052//===----------------------------------------------------------------------===//
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 -070079llvm::cl::opt<std::string>
80OptTargetTriple("mtriple",
81 llvm::cl::desc("Specify the target triple (default: "
82 DEFAULT_TARGET_TRIPLE_STRING ")"),
83 llvm::cl::init(DEFAULT_TARGET_TRIPLE_STRING),
84 llvm::cl::value_desc("triple"));
85
86llvm::cl::alias OptTargetTripleC("C", llvm::cl::NotHidden,
87 llvm::cl::desc("Alias for -mtriple"),
88 llvm::cl::aliasopt(OptTargetTriple));
Shih-wei Liaod577d112012-04-25 04:06:29 -070089
Stephen Hinesc3437f02014-01-30 17:57:21 -080090llvm::cl::opt<bool>
91OptRSDebugContext("rs-debug-ctx",
92 llvm::cl::desc("Enable build to work with a RenderScript debug context"));
93
Shih-wei Liaod577d112012-04-25 04:06:29 -070094//===----------------------------------------------------------------------===//
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"
Jean-Luc Brouilletc5e607a2014-06-18 18:14:02 -0700109 << " Default target: " << DEFAULT_TARGET_TRIPLE_STRING << "\n\n"
110 << "LLVM (http://llvm.org/):\n"
Shih-wei Liaod577d112012-04-25 04:06:29 -0700111 << " Version: " << PACKAGE_VERSION << "\n";
112 return;
113}
114
115} // end anonymous namespace
116
117static inline
Stephen Hines7e9c1852013-06-21 19:25:34 -0700118bool ConfigCompiler(RSCompilerDriver &pRSCD) {
119 RSCompiler *RSC = pRSCD.getCompiler();
Chris Wailes900c6c12014-08-13 15:40:00 -0700120 CompilerConfig *config = nullptr;
Shih-wei Liaod577d112012-04-25 04:06:29 -0700121
Shih-wei Liaod577d112012-04-25 04:06:29 -0700122 config = new (std::nothrow) CompilerConfig(OptTargetTriple);
Chris Wailes900c6c12014-08-13 15:40:00 -0700123 if (config == nullptr) {
Shih-wei Liaod577d112012-04-25 04:06:29 -0700124 llvm::errs() << "Out of memory when create the compiler configuration!\n";
125 return false;
126 }
127
Shih-wei Liaod577d112012-04-25 04:06:29 -0700128 switch (OptOptLevel) {
129 case '0': config->setOptimizationLevel(llvm::CodeGenOpt::None); break;
130 case '1': config->setOptimizationLevel(llvm::CodeGenOpt::Less); break;
Stephen Hines7e9c1852013-06-21 19:25:34 -0700131 case '2': config->setOptimizationLevel(llvm::CodeGenOpt::Default); break;
132 case '3':
Shih-wei Liaod577d112012-04-25 04:06:29 -0700133 default: {
Stephen Hines7e9c1852013-06-21 19:25:34 -0700134 config->setOptimizationLevel(llvm::CodeGenOpt::Aggressive);
Shih-wei Liaod577d112012-04-25 04:06:29 -0700135 break;
136 }
137 }
138
Stephen Hines7e9c1852013-06-21 19:25:34 -0700139 pRSCD.setConfig(config);
140 Compiler::ErrorCode result = RSC->config(*config);
Shih-wei Liaod577d112012-04-25 04:06:29 -0700141
Stephen Hinesc3437f02014-01-30 17:57:21 -0800142 if (OptRSDebugContext) {
143 pRSCD.setDebugContext(true);
144 }
145
Shih-wei Liaod577d112012-04-25 04:06:29 -0700146 if (result != Compiler::kSuccess) {
147 llvm::errs() << "Failed to configure the compiler! (detail: "
148 << Compiler::GetErrorString(result) << ")\n";
149 return false;
150 }
151
152 return true;
153}
154
Shih-wei Liaod577d112012-04-25 04:06:29 -0700155int main(int argc, char **argv) {
156 llvm::cl::SetVersionPrinter(BCCVersionPrinter);
157 llvm::cl::ParseCommandLineOptions(argc, argv);
Jean-Luc Brouilletf2ac3172014-06-25 18:21:36 -0700158 std::string commandLine = bcc::getCommandLine(argc, argv);
Shih-wei Liaod577d112012-04-25 04:06:29 -0700159 init::Initialize();
160
161 BCCContext context;
Stephen Hines8be8dba2013-06-18 09:53:43 -0700162 RSCompilerDriver RSCD;
Shih-wei Liaod577d112012-04-25 04:06:29 -0700163
Jean-Luc Brouilletc5e607a2014-06-18 18:14:02 -0700164 if (OptBCLibFilename.empty()) {
165 ALOGE("Failed to compile bit code, -bclib was not specified");
166 return EXIT_FAILURE;
167 }
168
Stephen Hinesd0993af2014-07-15 16:49:25 -0700169 llvm::ErrorOr<std::unique_ptr<llvm::MemoryBuffer>> mb_or_error =
170 llvm::MemoryBuffer::getFile(OptInputFilename.c_str());
171 if (mb_or_error.getError()) {
Stephen Hines8be8dba2013-06-18 09:53:43 -0700172 ALOGE("Failed to load bitcode from path %s! (%s)",
Stephen Hinesd0993af2014-07-15 16:49:25 -0700173 OptInputFilename.c_str(), mb_or_error.getError().message().c_str());
Shih-wei Liaod577d112012-04-25 04:06:29 -0700174 return EXIT_FAILURE;
175 }
Stephen Hinesd0993af2014-07-15 16:49:25 -0700176 std::unique_ptr<llvm::MemoryBuffer> input_data = std::move(mb_or_error.get());
Shih-wei Liaod577d112012-04-25 04:06:29 -0700177
Stephen Hines1ae3fd62014-05-15 12:16:26 -0700178 const char *bitcode = input_data->getBufferStart();
179 size_t bitcodeSize = input_data->getBufferSize();
Shih-wei Liaod577d112012-04-25 04:06:29 -0700180
Stephen Hines7e9c1852013-06-21 19:25:34 -0700181 if (!ConfigCompiler(RSCD)) {
182 ALOGE("Failed to configure compiler");
183 return EXIT_FAILURE;
184 }
Stephen Hinesc3437f02014-01-30 17:57:21 -0800185
186 // Attempt to dynamically initialize the compiler driver if such a function
187 // is present. It is only present if passed via "-load libFOO.so".
188 RSCompilerDriverInit_t rscdi = (RSCompilerDriverInit_t)
189 dlsym(RTLD_DEFAULT, STR(RS_COMPILER_DRIVER_INIT_FN));
Chris Wailes900c6c12014-08-13 15:40:00 -0700190 if (rscdi != nullptr) {
Stephen Hinesc3437f02014-01-30 17:57:21 -0800191 rscdi(&RSCD);
192 }
193
Jean-Luc Brouilletf2ac3172014-06-25 18:21:36 -0700194 bool built = RSCD.build(context, OptOutputPath.c_str(), OptOutputFilename.c_str(), bitcode,
Chris Wailes900c6c12014-08-13 15:40:00 -0700195 bitcodeSize, commandLine.c_str(), OptBCLibFilename.c_str(), nullptr,
Jean-Luc Brouilletf2ac3172014-06-25 18:21:36 -0700196 OptEmitLLVM);
Stephen Hines8be8dba2013-06-18 09:53:43 -0700197
198 if (!built) {
Shih-wei Liaod577d112012-04-25 04:06:29 -0700199 return EXIT_FAILURE;
200 }
201
Shih-wei Liaod577d112012-04-25 04:06:29 -0700202 return EXIT_SUCCESS;
203}