blob: 2ac317084931f325bb9a5111362a5aa215a6e474 [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>
36#include <bcc/Config/BuildInfo.h>
37#include <bcc/Config/Config.h>
Shih-wei Liao09ca9542013-01-25 17:00:08 -080038#include <bcc/ExecutionEngine/CompilerRTSymbolResolver.h>
Shih-wei Liaod577d112012-04-25 04:06:29 -070039#include <bcc/ExecutionEngine/ObjectLoader.h>
40#include <bcc/ExecutionEngine/SymbolResolverProxy.h>
41#include <bcc/ExecutionEngine/SymbolResolvers.h>
Stephen Hines8be8dba2013-06-18 09:53:43 -070042#include <bcc/Renderscript/RSCompilerDriver.h>
Shih-wei Liaod577d112012-04-25 04:06:29 -070043#include <bcc/Script.h>
44#include <bcc/Source.h>
45#include <bcc/Support/CompilerConfig.h>
46#include <bcc/Support/Initialization.h>
47#include <bcc/Support/InputFile.h>
48#include <bcc/Support/OutputFile.h>
Shih-wei Liaod577d112012-04-25 04:06:29 -070049
50using namespace bcc;
51
Stephen Hinesc3437f02014-01-30 17:57:21 -080052#define STR2(a) #a
53#define STR(a) STR2(a)
54
Shih-wei Liaod577d112012-04-25 04:06:29 -070055//===----------------------------------------------------------------------===//
56// General Options
57//===----------------------------------------------------------------------===//
58namespace {
59
Stephen Hines8be8dba2013-06-18 09:53:43 -070060llvm::cl::opt<std::string>
61OptInputFilename(llvm::cl::Positional, llvm::cl::ValueRequired,
62 llvm::cl::desc("<input bitcode file>"));
Shih-wei Liaod577d112012-04-25 04:06:29 -070063
64llvm::cl::opt<std::string>
65OptOutputFilename("o", llvm::cl::desc("Specify the output filename"),
Stephen Hines8be8dba2013-06-18 09:53:43 -070066 llvm::cl::value_desc("filename"),
67 llvm::cl::init("bcc_output"));
68
69llvm::cl::opt<std::string>
70OptBCLibFilename("bclib", llvm::cl::desc("Specify the bclib filename"),
71 llvm::cl::value_desc("bclib"));
72
73llvm::cl::opt<std::string>
74OptOutputPath("output_path", llvm::cl::desc("Specify the output path"),
75 llvm::cl::value_desc("output path"),
76 llvm::cl::init("."));
Shih-wei Liaod577d112012-04-25 04:06:29 -070077
Tobias Grosser7b980e12013-06-20 10:12:13 -070078llvm::cl::opt<bool>
79OptEmitLLVM("emit-llvm",
80 llvm::cl::desc("Emit an LLVM-IR version of the generated program"));
81
Shih-wei Liaod577d112012-04-25 04:06:29 -070082llvm::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));
Shih-wei Liaod577d112012-04-25 04:06:29 -070092
Stephen Hinesc3437f02014-01-30 17:57:21 -080093llvm::cl::opt<bool>
94OptRSDebugContext("rs-debug-ctx",
95 llvm::cl::desc("Enable build to work with a RenderScript debug context"));
96
Shih-wei Liaod577d112012-04-25 04:06:29 -070097//===----------------------------------------------------------------------===//
98// Compiler Options
99//===----------------------------------------------------------------------===//
Shih-wei Liaod577d112012-04-25 04:06:29 -0700100
Stephen Hines7e9c1852013-06-21 19:25:34 -0700101// RenderScript uses -O3 by default
Shih-wei Liaod577d112012-04-25 04:06:29 -0700102llvm::cl::opt<char>
103OptOptLevel("O", llvm::cl::desc("Optimization level. [-O0, -O1, -O2, or -O3] "
Stephen Hines7e9c1852013-06-21 19:25:34 -0700104 "(default: -O3)"),
105 llvm::cl::Prefix, llvm::cl::ZeroOrMore, llvm::cl::init('3'));
Shih-wei Liaod577d112012-04-25 04:06:29 -0700106
Shih-wei Liaod577d112012-04-25 04:06:29 -0700107// Override "bcc -version" since the LLVM version information is not correct on
108// Android build.
109void BCCVersionPrinter() {
110 llvm::raw_ostream &os = llvm::outs();
111 os << "libbcc (The Android Open Source Project, http://www.android.com/):\n"
112 << " Build time: " << BuildInfo::GetBuildTime() << "\n"
113 << " Build revision: " << BuildInfo::GetBuildRev() << "\n"
114 << " Build source blob: " << BuildInfo::GetBuildSourceBlob() << "\n"
115 << " Default target: " << DEFAULT_TARGET_TRIPLE_STRING << "\n";
116
117 os << "\n";
118
119 os << "LLVM (http://llvm.org/):\n"
120 << " Version: " << PACKAGE_VERSION << "\n";
121 return;
122}
123
124} // end anonymous namespace
125
126static inline
Stephen Hines7e9c1852013-06-21 19:25:34 -0700127bool ConfigCompiler(RSCompilerDriver &pRSCD) {
128 RSCompiler *RSC = pRSCD.getCompiler();
Shih-wei Liaod577d112012-04-25 04:06:29 -0700129 CompilerConfig *config = NULL;
130
Shih-wei Liaod577d112012-04-25 04:06:29 -0700131 config = new (std::nothrow) CompilerConfig(OptTargetTriple);
Shih-wei Liaod577d112012-04-25 04:06:29 -0700132 if (config == NULL) {
133 llvm::errs() << "Out of memory when create the compiler configuration!\n";
134 return false;
135 }
136
Shih-wei Liaod577d112012-04-25 04:06:29 -0700137 switch (OptOptLevel) {
138 case '0': config->setOptimizationLevel(llvm::CodeGenOpt::None); break;
139 case '1': config->setOptimizationLevel(llvm::CodeGenOpt::Less); break;
Stephen Hines7e9c1852013-06-21 19:25:34 -0700140 case '2': config->setOptimizationLevel(llvm::CodeGenOpt::Default); break;
141 case '3':
Shih-wei Liaod577d112012-04-25 04:06:29 -0700142 default: {
Stephen Hines7e9c1852013-06-21 19:25:34 -0700143 config->setOptimizationLevel(llvm::CodeGenOpt::Aggressive);
Shih-wei Liaod577d112012-04-25 04:06:29 -0700144 break;
145 }
146 }
147
Stephen Hines7e9c1852013-06-21 19:25:34 -0700148 pRSCD.setConfig(config);
149 Compiler::ErrorCode result = RSC->config(*config);
Shih-wei Liaod577d112012-04-25 04:06:29 -0700150
Stephen Hinesc3437f02014-01-30 17:57:21 -0800151 if (OptRSDebugContext) {
152 pRSCD.setDebugContext(true);
153 }
154
Shih-wei Liaod577d112012-04-25 04:06:29 -0700155 if (result != Compiler::kSuccess) {
156 llvm::errs() << "Failed to configure the compiler! (detail: "
157 << Compiler::GetErrorString(result) << ")\n";
158 return false;
159 }
160
161 return true;
162}
163
Shih-wei Liaod577d112012-04-25 04:06:29 -0700164int main(int argc, char **argv) {
165 llvm::cl::SetVersionPrinter(BCCVersionPrinter);
166 llvm::cl::ParseCommandLineOptions(argc, argv);
167 init::Initialize();
168
169 BCCContext context;
Stephen Hines8be8dba2013-06-18 09:53:43 -0700170 RSCompilerDriver RSCD;
Shih-wei Liaod577d112012-04-25 04:06:29 -0700171
Stephen Hinesa6300782014-05-28 15:17:19 -0700172 std::unique_ptr<llvm::MemoryBuffer> input_data;
Stephen Hines8be8dba2013-06-18 09:53:43 -0700173
174 llvm::error_code ec =
175 llvm::MemoryBuffer::getFile(OptInputFilename.c_str(), input_data);
176 if (ec != llvm::error_code::success()) {
177 ALOGE("Failed to load bitcode from path %s! (%s)",
178 OptInputFilename.c_str(), ec.message().c_str());
Shih-wei Liaod577d112012-04-25 04:06:29 -0700179 return EXIT_FAILURE;
180 }
181
Stephen Hines1ae3fd62014-05-15 12:16:26 -0700182 const char *bitcode = input_data->getBufferStart();
183 size_t bitcodeSize = input_data->getBufferSize();
Shih-wei Liaod577d112012-04-25 04:06:29 -0700184
Stephen Hines7e9c1852013-06-21 19:25:34 -0700185 if (!ConfigCompiler(RSCD)) {
186 ALOGE("Failed to configure compiler");
187 return EXIT_FAILURE;
188 }
Stephen Hinesc3437f02014-01-30 17:57:21 -0800189
190 // Attempt to dynamically initialize the compiler driver if such a function
191 // is present. It is only present if passed via "-load libFOO.so".
192 RSCompilerDriverInit_t rscdi = (RSCompilerDriverInit_t)
193 dlsym(RTLD_DEFAULT, STR(RS_COMPILER_DRIVER_INIT_FN));
194 if (rscdi != NULL) {
195 rscdi(&RSCD);
196 }
197
Stephen Hines8be8dba2013-06-18 09:53:43 -0700198 bool built = RSCD.build(context, OptOutputPath.c_str(),
199 OptOutputFilename.c_str(), bitcode, bitcodeSize,
Tobias Grosser7b980e12013-06-20 10:12:13 -0700200 OptBCLibFilename.c_str(), NULL, OptEmitLLVM);
Stephen Hines8be8dba2013-06-18 09:53:43 -0700201
202 if (!built) {
Shih-wei Liaod577d112012-04-25 04:06:29 -0700203 return EXIT_FAILURE;
204 }
205
Shih-wei Liaod577d112012-04-25 04:06:29 -0700206 return EXIT_SUCCESS;
207}