blob: 131cf7576ad0aa980a70dcc8663e57f75686c96b [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>
Shih-wei Liaod577d112012-04-25 04:06:29 -070028#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>
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>
46#include <bcc/Support/TargetCompilerConfigs.h>
47
48using namespace bcc;
49
50//===----------------------------------------------------------------------===//
51// General Options
52//===----------------------------------------------------------------------===//
53namespace {
54
Stephen Hines8be8dba2013-06-18 09:53:43 -070055llvm::cl::opt<std::string>
56OptInputFilename(llvm::cl::Positional, llvm::cl::ValueRequired,
57 llvm::cl::desc("<input bitcode file>"));
Shih-wei Liaod577d112012-04-25 04:06:29 -070058
59llvm::cl::opt<std::string>
60OptOutputFilename("o", llvm::cl::desc("Specify the output filename"),
Stephen Hines8be8dba2013-06-18 09:53:43 -070061 llvm::cl::value_desc("filename"),
62 llvm::cl::init("bcc_output"));
63
64llvm::cl::opt<std::string>
65OptBCLibFilename("bclib", llvm::cl::desc("Specify the bclib filename"),
66 llvm::cl::value_desc("bclib"));
67
68llvm::cl::opt<std::string>
69OptOutputPath("output_path", llvm::cl::desc("Specify the output path"),
70 llvm::cl::value_desc("output path"),
71 llvm::cl::init("."));
Shih-wei Liaod577d112012-04-25 04:06:29 -070072
Tobias Grosser7b980e12013-06-20 10:12:13 -070073llvm::cl::opt<bool>
74OptEmitLLVM("emit-llvm",
75 llvm::cl::desc("Emit an LLVM-IR version of the generated program"));
76
Shih-wei Liaod577d112012-04-25 04:06:29 -070077#ifdef TARGET_BUILD
78const std::string OptTargetTriple(DEFAULT_TARGET_TRIPLE_STRING);
79#else
80llvm::cl::opt<std::string>
81OptTargetTriple("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
87llvm::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//===----------------------------------------------------------------------===//
Shih-wei Liaod577d112012-04-25 04:06:29 -070095
Stephen Hines7e9c1852013-06-21 19:25:34 -070096// RenderScript uses -O3 by default
Shih-wei Liaod577d112012-04-25 04:06:29 -070097llvm::cl::opt<char>
98OptOptLevel("O", llvm::cl::desc("Optimization level. [-O0, -O1, -O2, or -O3] "
Stephen Hines7e9c1852013-06-21 19:25:34 -070099 "(default: -O3)"),
100 llvm::cl::Prefix, llvm::cl::ZeroOrMore, llvm::cl::init('3'));
Shih-wei Liaod577d112012-04-25 04:06:29 -0700101
Shih-wei Liaod577d112012-04-25 04:06:29 -0700102// Override "bcc -version" since the LLVM version information is not correct on
103// Android build.
104void 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
121static inline
Stephen Hines7e9c1852013-06-21 19:25:34 -0700122bool ConfigCompiler(RSCompilerDriver &pRSCD) {
123 RSCompiler *RSC = pRSCD.getCompiler();
Shih-wei Liaod577d112012-04-25 04:06:29 -0700124 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
Shih-wei Liaod577d112012-04-25 04:06:29 -0700136 switch (OptOptLevel) {
137 case '0': config->setOptimizationLevel(llvm::CodeGenOpt::None); break;
138 case '1': config->setOptimizationLevel(llvm::CodeGenOpt::Less); break;
Stephen Hines7e9c1852013-06-21 19:25:34 -0700139 case '2': config->setOptimizationLevel(llvm::CodeGenOpt::Default); break;
140 case '3':
Shih-wei Liaod577d112012-04-25 04:06:29 -0700141 default: {
Stephen Hines7e9c1852013-06-21 19:25:34 -0700142 config->setOptimizationLevel(llvm::CodeGenOpt::Aggressive);
Shih-wei Liaod577d112012-04-25 04:06:29 -0700143 break;
144 }
145 }
146
Stephen Hines7e9c1852013-06-21 19:25:34 -0700147 pRSCD.setConfig(config);
148 Compiler::ErrorCode result = RSC->config(*config);
Shih-wei Liaod577d112012-04-25 04:06:29 -0700149
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
Shih-wei Liaod577d112012-04-25 04:06:29 -0700159int main(int argc, char **argv) {
160 llvm::cl::SetVersionPrinter(BCCVersionPrinter);
161 llvm::cl::ParseCommandLineOptions(argc, argv);
162 init::Initialize();
163
164 BCCContext context;
Stephen Hines8be8dba2013-06-18 09:53:43 -0700165 RSCompilerDriver RSCD;
Shih-wei Liaod577d112012-04-25 04:06:29 -0700166
Stephen Hines8be8dba2013-06-18 09:53:43 -0700167 llvm::OwningPtr<llvm::MemoryBuffer> input_data;
168
169 llvm::error_code ec =
170 llvm::MemoryBuffer::getFile(OptInputFilename.c_str(), input_data);
171 if (ec != llvm::error_code::success()) {
172 ALOGE("Failed to load bitcode from path %s! (%s)",
173 OptInputFilename.c_str(), ec.message().c_str());
Shih-wei Liaod577d112012-04-25 04:06:29 -0700174 return EXIT_FAILURE;
175 }
176
Stephen Hines8be8dba2013-06-18 09:53:43 -0700177 llvm::MemoryBuffer *input_memory = input_data.take();
Shih-wei Liaod577d112012-04-25 04:06:29 -0700178
Stephen Hines8be8dba2013-06-18 09:53:43 -0700179 const char *bitcode = input_memory->getBufferStart();
180 size_t bitcodeSize = input_memory->getBufferSize();
Shih-wei Liaod577d112012-04-25 04:06:29 -0700181
Stephen Hines7e9c1852013-06-21 19:25:34 -0700182 if (!ConfigCompiler(RSCD)) {
183 ALOGE("Failed to configure compiler");
184 return EXIT_FAILURE;
185 }
Stephen Hines8be8dba2013-06-18 09:53:43 -0700186 bool built = RSCD.build(context, OptOutputPath.c_str(),
187 OptOutputFilename.c_str(), bitcode, bitcodeSize,
Tobias Grosser7b980e12013-06-20 10:12:13 -0700188 OptBCLibFilename.c_str(), NULL, OptEmitLLVM);
Stephen Hines8be8dba2013-06-18 09:53:43 -0700189
190 if (!built) {
Shih-wei Liaod577d112012-04-25 04:06:29 -0700191 return EXIT_FAILURE;
192 }
193
Shih-wei Liaod577d112012-04-25 04:06:29 -0700194 return EXIT_SUCCESS;
195}