blob: fa80d95fd1dcff6a58d4388c284e9c7b4b59427a [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>
Stephen Hines88f8c522013-06-13 00:35:03 -070028#include <llvm/Support/PathV1.h>
Shih-wei Liaod577d112012-04-25 04:06:29 -070029#include <llvm/Support/Path.h>
30#include <llvm/Support/raw_ostream.h>
31#include <llvm/Support/system_error.h>
32
33#include <bcc/BCCContext.h>
34#include <bcc/Compiler.h>
35#include <bcc/Config/BuildInfo.h>
36#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>
48#include <bcc/Support/TargetCompilerConfigs.h>
49
50using namespace bcc;
51
52//===----------------------------------------------------------------------===//
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
75#ifdef TARGET_BUILD
76const std::string OptTargetTriple(DEFAULT_TARGET_TRIPLE_STRING);
77#else
78llvm::cl::opt<std::string>
79OptTargetTriple("mtriple",
80 llvm::cl::desc("Specify the target triple (default: "
81 DEFAULT_TARGET_TRIPLE_STRING ")"),
82 llvm::cl::init(DEFAULT_TARGET_TRIPLE_STRING),
83 llvm::cl::value_desc("triple"));
84
85llvm::cl::alias OptTargetTripleC("C", llvm::cl::NotHidden,
86 llvm::cl::desc("Alias for -mtriple"),
87 llvm::cl::aliasopt(OptTargetTriple));
88#endif
89
90//===----------------------------------------------------------------------===//
91// Compiler Options
92//===----------------------------------------------------------------------===//
Shih-wei Liaod577d112012-04-25 04:06:29 -070093
94llvm::cl::opt<char>
95OptOptLevel("O", llvm::cl::desc("Optimization level. [-O0, -O1, -O2, or -O3] "
96 "(default: -O2)"),
97 llvm::cl::Prefix, llvm::cl::ZeroOrMore, llvm::cl::init('2'));
98
Shih-wei Liaod577d112012-04-25 04:06:29 -070099// Override "bcc -version" since the LLVM version information is not correct on
100// Android build.
101void BCCVersionPrinter() {
102 llvm::raw_ostream &os = llvm::outs();
103 os << "libbcc (The Android Open Source Project, http://www.android.com/):\n"
104 << " Build time: " << BuildInfo::GetBuildTime() << "\n"
105 << " Build revision: " << BuildInfo::GetBuildRev() << "\n"
106 << " Build source blob: " << BuildInfo::GetBuildSourceBlob() << "\n"
107 << " Default target: " << DEFAULT_TARGET_TRIPLE_STRING << "\n";
108
109 os << "\n";
110
111 os << "LLVM (http://llvm.org/):\n"
112 << " Version: " << PACKAGE_VERSION << "\n";
113 return;
114}
115
116} // end anonymous namespace
117
118static inline
Shih-wei Liaod577d112012-04-25 04:06:29 -0700119bool ConfigCompiler(Compiler &pCompiler) {
120 CompilerConfig *config = NULL;
121
122#ifdef TARGET_BUILD
123 config = new (std::nothrow) DefaultCompilerConfig();
124#else
125 config = new (std::nothrow) CompilerConfig(OptTargetTriple);
126#endif
127 if (config == NULL) {
128 llvm::errs() << "Out of memory when create the compiler configuration!\n";
129 return false;
130 }
131
Shih-wei Liaod577d112012-04-25 04:06:29 -0700132 switch (OptOptLevel) {
133 case '0': config->setOptimizationLevel(llvm::CodeGenOpt::None); break;
134 case '1': config->setOptimizationLevel(llvm::CodeGenOpt::Less); break;
135 case '3': config->setOptimizationLevel(llvm::CodeGenOpt::Aggressive); break;
136 case '2':
137 default: {
138 config->setOptimizationLevel(llvm::CodeGenOpt::Default);
139 break;
140 }
141 }
142
143 Compiler::ErrorCode result = pCompiler.config(*config);
144
145 delete config;
146
147 if (result != Compiler::kSuccess) {
148 llvm::errs() << "Failed to configure the compiler! (detail: "
149 << Compiler::GetErrorString(result) << ")\n";
150 return false;
151 }
152
153 return true;
154}
155
Shih-wei Liaod577d112012-04-25 04:06:29 -0700156static inline
157bool CompileScript(Compiler &pCompiler, Script &pScript,
158 const std::string &pOutputPath) {
159 // Open the output file.
Shih-wei Liaoc02eae62012-07-22 16:23:32 -0700160 OutputFile output_file(pOutputPath, FileBase::kTruncate);
Shih-wei Liaod577d112012-04-25 04:06:29 -0700161
162 if (output_file.hasError()) {
163 llvm::errs() << "Failed to open the output file `" << pOutputPath
164 << "'! (detail: " << output_file.getErrorMessage() << ")\n";
165 return false;
166 }
167
168 // Run the compiler.
169 Compiler::ErrorCode result = pCompiler.compile(pScript, output_file);
170 if (result != Compiler::kSuccess) {
171 llvm::errs() << "Fatal error during compilation (detail: "
172 << Compiler::GetErrorString(result) << ".)\n";
173 return false;
174 }
175
176 return true;
177}
178
Shih-wei Liaod577d112012-04-25 04:06:29 -0700179int main(int argc, char **argv) {
180 llvm::cl::SetVersionPrinter(BCCVersionPrinter);
181 llvm::cl::ParseCommandLineOptions(argc, argv);
182 init::Initialize();
183
184 BCCContext context;
Stephen Hines8be8dba2013-06-18 09:53:43 -0700185 RSCompilerDriver RSCD;
Shih-wei Liaod577d112012-04-25 04:06:29 -0700186
Stephen Hines8be8dba2013-06-18 09:53:43 -0700187 llvm::OwningPtr<llvm::MemoryBuffer> input_data;
188
189 llvm::error_code ec =
190 llvm::MemoryBuffer::getFile(OptInputFilename.c_str(), input_data);
191 if (ec != llvm::error_code::success()) {
192 ALOGE("Failed to load bitcode from path %s! (%s)",
193 OptInputFilename.c_str(), ec.message().c_str());
Shih-wei Liaod577d112012-04-25 04:06:29 -0700194 return EXIT_FAILURE;
195 }
196
Stephen Hines8be8dba2013-06-18 09:53:43 -0700197 llvm::MemoryBuffer *input_memory = input_data.take();
Shih-wei Liaod577d112012-04-25 04:06:29 -0700198
Stephen Hines8be8dba2013-06-18 09:53:43 -0700199 const char *bitcode = input_memory->getBufferStart();
200 size_t bitcodeSize = input_memory->getBufferSize();
Shih-wei Liaod577d112012-04-25 04:06:29 -0700201
Stephen Hines8be8dba2013-06-18 09:53:43 -0700202 bool built = RSCD.build(context, OptOutputPath.c_str(),
203 OptOutputFilename.c_str(), bitcode, bitcodeSize,
204 OptBCLibFilename.c_str(), NULL);
205
206 if (!built) {
Shih-wei Liaod577d112012-04-25 04:06:29 -0700207 return EXIT_FAILURE;
208 }
209
Shih-wei Liaod577d112012-04-25 04:06:29 -0700210 return EXIT_SUCCESS;
211}