blob: 02860fecfe20305d9eca67716b9863c6c47e6802 [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
Tim Murrayc2074ca2014-04-08 15:39:08 -070023#include <llvm/ADT/OwningPtr.h>
Shih-wei Liaod577d112012-04-25 04:06:29 -070024#include <llvm/ADT/STLExtras.h>
25#include <llvm/ADT/SmallString.h>
26#include <llvm/Config/config.h>
27#include <llvm/Support/CommandLine.h>
28#include <llvm/Support/FileSystem.h>
Stephen Hines8be8dba2013-06-18 09:53:43 -070029#include <llvm/Support/MemoryBuffer.h>
Stephen Hinesc3437f02014-01-30 17:57:21 -080030#include <llvm/Support/PluginLoader.h>
Shih-wei Liaod577d112012-04-25 04:06:29 -070031#include <llvm/Support/raw_ostream.h>
32#include <llvm/Support/system_error.h>
33
34#include <bcc/BCCContext.h>
35#include <bcc/Compiler.h>
Shih-wei Liaod577d112012-04-25 04:06:29 -070036#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>
Shih-wei Liaod577d112012-04-25 04:06:29 -070048
49using namespace bcc;
50
Stephen Hinesc3437f02014-01-30 17:57:21 -080051#define STR2(a) #a
52#define STR(a) STR2(a)
53
Shih-wei Liaod577d112012-04-25 04:06:29 -070054//===----------------------------------------------------------------------===//
55// General Options
56//===----------------------------------------------------------------------===//
57namespace {
58
Stephen Hines8be8dba2013-06-18 09:53:43 -070059llvm::cl::opt<std::string>
60OptInputFilename(llvm::cl::Positional, llvm::cl::ValueRequired,
61 llvm::cl::desc("<input bitcode file>"));
Shih-wei Liaod577d112012-04-25 04:06:29 -070062
63llvm::cl::opt<std::string>
64OptOutputFilename("o", llvm::cl::desc("Specify the output filename"),
Stephen Hines8be8dba2013-06-18 09:53:43 -070065 llvm::cl::value_desc("filename"),
66 llvm::cl::init("bcc_output"));
67
68llvm::cl::opt<std::string>
69OptBCLibFilename("bclib", llvm::cl::desc("Specify the bclib filename"),
70 llvm::cl::value_desc("bclib"));
71
72llvm::cl::opt<std::string>
73OptOutputPath("output_path", llvm::cl::desc("Specify the output path"),
74 llvm::cl::value_desc("output path"),
75 llvm::cl::init("."));
Shih-wei Liaod577d112012-04-25 04:06:29 -070076
Tobias Grosser7b980e12013-06-20 10:12:13 -070077llvm::cl::opt<bool>
78OptEmitLLVM("emit-llvm",
79 llvm::cl::desc("Emit an LLVM-IR version of the generated program"));
80
Shih-wei Liaod577d112012-04-25 04:06:29 -070081llvm::cl::opt<std::string>
82OptTargetTriple("mtriple",
83 llvm::cl::desc("Specify the target triple (default: "
84 DEFAULT_TARGET_TRIPLE_STRING ")"),
85 llvm::cl::init(DEFAULT_TARGET_TRIPLE_STRING),
86 llvm::cl::value_desc("triple"));
87
88llvm::cl::alias OptTargetTripleC("C", llvm::cl::NotHidden,
89 llvm::cl::desc("Alias for -mtriple"),
90 llvm::cl::aliasopt(OptTargetTriple));
Shih-wei Liaod577d112012-04-25 04:06:29 -070091
Stephen Hinesc3437f02014-01-30 17:57:21 -080092llvm::cl::opt<bool>
93OptRSDebugContext("rs-debug-ctx",
94 llvm::cl::desc("Enable build to work with a RenderScript debug context"));
95
Shih-wei Liaod577d112012-04-25 04:06:29 -070096//===----------------------------------------------------------------------===//
97// Compiler Options
98//===----------------------------------------------------------------------===//
Shih-wei Liaod577d112012-04-25 04:06:29 -070099
Stephen Hines7e9c1852013-06-21 19:25:34 -0700100// RenderScript uses -O3 by default
Shih-wei Liaod577d112012-04-25 04:06:29 -0700101llvm::cl::opt<char>
102OptOptLevel("O", llvm::cl::desc("Optimization level. [-O0, -O1, -O2, or -O3] "
Stephen Hines7e9c1852013-06-21 19:25:34 -0700103 "(default: -O3)"),
104 llvm::cl::Prefix, llvm::cl::ZeroOrMore, llvm::cl::init('3'));
Shih-wei Liaod577d112012-04-25 04:06:29 -0700105
Shih-wei Liaod577d112012-04-25 04:06:29 -0700106// Override "bcc -version" since the LLVM version information is not correct on
107// Android build.
108void BCCVersionPrinter() {
109 llvm::raw_ostream &os = llvm::outs();
110 os << "libbcc (The Android Open Source Project, http://www.android.com/):\n"
Jean-Luc Brouilletc5e607a2014-06-18 18:14:02 -0700111 << " Default target: " << DEFAULT_TARGET_TRIPLE_STRING << "\n\n"
112 << "LLVM (http://llvm.org/):\n"
Shih-wei Liaod577d112012-04-25 04:06:29 -0700113 << " Version: " << PACKAGE_VERSION << "\n";
114 return;
115}
116
117} // end anonymous namespace
118
119static inline
Stephen Hines7e9c1852013-06-21 19:25:34 -0700120bool ConfigCompiler(RSCompilerDriver &pRSCD) {
121 RSCompiler *RSC = pRSCD.getCompiler();
Shih-wei Liaod577d112012-04-25 04:06:29 -0700122 CompilerConfig *config = NULL;
123
Shih-wei Liaod577d112012-04-25 04:06:29 -0700124 config = new (std::nothrow) CompilerConfig(OptTargetTriple);
Shih-wei Liaod577d112012-04-25 04:06:29 -0700125 if (config == NULL) {
126 llvm::errs() << "Out of memory when create the compiler configuration!\n";
127 return false;
128 }
129
Shih-wei Liaod577d112012-04-25 04:06:29 -0700130 switch (OptOptLevel) {
131 case '0': config->setOptimizationLevel(llvm::CodeGenOpt::None); break;
132 case '1': config->setOptimizationLevel(llvm::CodeGenOpt::Less); break;
Stephen Hines7e9c1852013-06-21 19:25:34 -0700133 case '2': config->setOptimizationLevel(llvm::CodeGenOpt::Default); break;
134 case '3':
Shih-wei Liaod577d112012-04-25 04:06:29 -0700135 default: {
Stephen Hines7e9c1852013-06-21 19:25:34 -0700136 config->setOptimizationLevel(llvm::CodeGenOpt::Aggressive);
Shih-wei Liaod577d112012-04-25 04:06:29 -0700137 break;
138 }
139 }
140
Stephen Hines7e9c1852013-06-21 19:25:34 -0700141 pRSCD.setConfig(config);
142 Compiler::ErrorCode result = RSC->config(*config);
Shih-wei Liaod577d112012-04-25 04:06:29 -0700143
Stephen Hinesc3437f02014-01-30 17:57:21 -0800144 if (OptRSDebugContext) {
145 pRSCD.setDebugContext(true);
146 }
147
Shih-wei Liaod577d112012-04-25 04:06:29 -0700148 if (result != Compiler::kSuccess) {
149 llvm::errs() << "Failed to configure the compiler! (detail: "
150 << Compiler::GetErrorString(result) << ")\n";
151 return false;
152 }
153
154 return true;
155}
156
Shih-wei Liaod577d112012-04-25 04:06:29 -0700157int main(int argc, char **argv) {
158 llvm::cl::SetVersionPrinter(BCCVersionPrinter);
159 llvm::cl::ParseCommandLineOptions(argc, argv);
160 init::Initialize();
161
162 BCCContext context;
Stephen Hines8be8dba2013-06-18 09:53:43 -0700163 RSCompilerDriver RSCD;
Shih-wei Liaod577d112012-04-25 04:06:29 -0700164
Stephen Hinesa6300782014-05-28 15:17:19 -0700165 std::unique_ptr<llvm::MemoryBuffer> input_data;
Stephen Hines8be8dba2013-06-18 09:53:43 -0700166
Jean-Luc Brouilletc5e607a2014-06-18 18:14:02 -0700167 if (OptBCLibFilename.empty()) {
168 ALOGE("Failed to compile bit code, -bclib was not specified");
169 return EXIT_FAILURE;
170 }
171
Stephen Hines8be8dba2013-06-18 09:53:43 -0700172 llvm::error_code ec =
173 llvm::MemoryBuffer::getFile(OptInputFilename.c_str(), input_data);
174 if (ec != llvm::error_code::success()) {
175 ALOGE("Failed to load bitcode from path %s! (%s)",
176 OptInputFilename.c_str(), ec.message().c_str());
Shih-wei Liaod577d112012-04-25 04:06:29 -0700177 return EXIT_FAILURE;
178 }
179
Stephen Hines1ae3fd62014-05-15 12:16:26 -0700180 const char *bitcode = input_data->getBufferStart();
181 size_t bitcodeSize = input_data->getBufferSize();
Shih-wei Liaod577d112012-04-25 04:06:29 -0700182
Stephen Hines7e9c1852013-06-21 19:25:34 -0700183 if (!ConfigCompiler(RSCD)) {
184 ALOGE("Failed to configure compiler");
185 return EXIT_FAILURE;
186 }
Stephen Hinesc3437f02014-01-30 17:57:21 -0800187
188 // Attempt to dynamically initialize the compiler driver if such a function
189 // is present. It is only present if passed via "-load libFOO.so".
190 RSCompilerDriverInit_t rscdi = (RSCompilerDriverInit_t)
191 dlsym(RTLD_DEFAULT, STR(RS_COMPILER_DRIVER_INIT_FN));
192 if (rscdi != NULL) {
193 rscdi(&RSCD);
194 }
195
Stephen Hines8be8dba2013-06-18 09:53:43 -0700196 bool built = RSCD.build(context, OptOutputPath.c_str(),
197 OptOutputFilename.c_str(), bitcode, bitcodeSize,
Tobias Grosser7b980e12013-06-20 10:12:13 -0700198 OptBCLibFilename.c_str(), NULL, OptEmitLLVM);
Stephen Hines8be8dba2013-06-18 09:53:43 -0700199
200 if (!built) {
Shih-wei Liaod577d112012-04-25 04:06:29 -0700201 return EXIT_FAILURE;
202 }
203
Shih-wei Liaod577d112012-04-25 04:06:29 -0700204 return EXIT_SUCCESS;
205}