blob: 04b69425202ffd095a1645cb721c59a3bedeb58a [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
Tim Murrayc2074ca2014-04-08 15:39:08 -070022#include <llvm/ADT/OwningPtr.h>
Shih-wei Liaod577d112012-04-25 04:06:29 -070023#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>
Shih-wei Liaod577d112012-04-25 04:06:29 -070029#include <llvm/Support/raw_ostream.h>
30#include <llvm/Support/system_error.h>
31
32#include <bcc/BCCContext.h>
33#include <bcc/Compiler.h>
34#include <bcc/Config/BuildInfo.h>
35#include <bcc/Config/Config.h>
Shih-wei Liao09ca9542013-01-25 17:00:08 -080036#include <bcc/ExecutionEngine/CompilerRTSymbolResolver.h>
Shih-wei Liaod577d112012-04-25 04:06:29 -070037#include <bcc/ExecutionEngine/ObjectLoader.h>
38#include <bcc/ExecutionEngine/SymbolResolverProxy.h>
39#include <bcc/ExecutionEngine/SymbolResolvers.h>
Stephen Hines8be8dba2013-06-18 09:53:43 -070040#include <bcc/Renderscript/RSCompilerDriver.h>
Shih-wei Liaod577d112012-04-25 04:06:29 -070041#include <bcc/Script.h>
42#include <bcc/Source.h>
43#include <bcc/Support/CompilerConfig.h>
44#include <bcc/Support/Initialization.h>
45#include <bcc/Support/InputFile.h>
46#include <bcc/Support/OutputFile.h>
47#include <bcc/Support/TargetCompilerConfigs.h>
48
49using namespace bcc;
50
51//===----------------------------------------------------------------------===//
52// General Options
53//===----------------------------------------------------------------------===//
54namespace {
55
Stephen Hines8be8dba2013-06-18 09:53:43 -070056llvm::cl::opt<std::string>
57OptInputFilename(llvm::cl::Positional, llvm::cl::ValueRequired,
58 llvm::cl::desc("<input bitcode file>"));
Shih-wei Liaod577d112012-04-25 04:06:29 -070059
60llvm::cl::opt<std::string>
61OptOutputFilename("o", llvm::cl::desc("Specify the output filename"),
Stephen Hines8be8dba2013-06-18 09:53:43 -070062 llvm::cl::value_desc("filename"),
63 llvm::cl::init("bcc_output"));
64
65llvm::cl::opt<std::string>
66OptBCLibFilename("bclib", llvm::cl::desc("Specify the bclib filename"),
67 llvm::cl::value_desc("bclib"));
68
69llvm::cl::opt<std::string>
70OptOutputPath("output_path", llvm::cl::desc("Specify the output path"),
71 llvm::cl::value_desc("output path"),
72 llvm::cl::init("."));
Shih-wei Liaod577d112012-04-25 04:06:29 -070073
Tobias Grosser7b980e12013-06-20 10:12:13 -070074llvm::cl::opt<bool>
75OptEmitLLVM("emit-llvm",
76 llvm::cl::desc("Emit an LLVM-IR version of the generated program"));
77
Shih-wei Liaod577d112012-04-25 04:06:29 -070078#ifdef TARGET_BUILD
79const std::string OptTargetTriple(DEFAULT_TARGET_TRIPLE_STRING);
80#else
81llvm::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));
91#endif
92
93//===----------------------------------------------------------------------===//
94// Compiler Options
95//===----------------------------------------------------------------------===//
Shih-wei Liaod577d112012-04-25 04:06:29 -070096
Stephen Hines7e9c1852013-06-21 19:25:34 -070097// RenderScript uses -O3 by default
Shih-wei Liaod577d112012-04-25 04:06:29 -070098llvm::cl::opt<char>
99OptOptLevel("O", llvm::cl::desc("Optimization level. [-O0, -O1, -O2, or -O3] "
Stephen Hines7e9c1852013-06-21 19:25:34 -0700100 "(default: -O3)"),
101 llvm::cl::Prefix, llvm::cl::ZeroOrMore, llvm::cl::init('3'));
Shih-wei Liaod577d112012-04-25 04:06:29 -0700102
Shih-wei Liaod577d112012-04-25 04:06:29 -0700103// Override "bcc -version" since the LLVM version information is not correct on
104// Android build.
105void BCCVersionPrinter() {
106 llvm::raw_ostream &os = llvm::outs();
107 os << "libbcc (The Android Open Source Project, http://www.android.com/):\n"
108 << " Build time: " << BuildInfo::GetBuildTime() << "\n"
109 << " Build revision: " << BuildInfo::GetBuildRev() << "\n"
110 << " Build source blob: " << BuildInfo::GetBuildSourceBlob() << "\n"
111 << " Default target: " << DEFAULT_TARGET_TRIPLE_STRING << "\n";
112
113 os << "\n";
114
115 os << "LLVM (http://llvm.org/):\n"
116 << " Version: " << PACKAGE_VERSION << "\n";
117 return;
118}
119
120} // end anonymous namespace
121
122static inline
Stephen Hines7e9c1852013-06-21 19:25:34 -0700123bool ConfigCompiler(RSCompilerDriver &pRSCD) {
124 RSCompiler *RSC = pRSCD.getCompiler();
Shih-wei Liaod577d112012-04-25 04:06:29 -0700125 CompilerConfig *config = NULL;
126
127#ifdef TARGET_BUILD
128 config = new (std::nothrow) DefaultCompilerConfig();
129#else
130 config = new (std::nothrow) CompilerConfig(OptTargetTriple);
131#endif
132 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
151 if (result != Compiler::kSuccess) {
152 llvm::errs() << "Failed to configure the compiler! (detail: "
153 << Compiler::GetErrorString(result) << ")\n";
154 return false;
155 }
156
157 return true;
158}
159
Shih-wei Liaod577d112012-04-25 04:06:29 -0700160int main(int argc, char **argv) {
161 llvm::cl::SetVersionPrinter(BCCVersionPrinter);
162 llvm::cl::ParseCommandLineOptions(argc, argv);
163 init::Initialize();
164
165 BCCContext context;
Stephen Hines8be8dba2013-06-18 09:53:43 -0700166 RSCompilerDriver RSCD;
Shih-wei Liaod577d112012-04-25 04:06:29 -0700167
Stephen Hines8be8dba2013-06-18 09:53:43 -0700168 llvm::OwningPtr<llvm::MemoryBuffer> input_data;
169
170 llvm::error_code ec =
171 llvm::MemoryBuffer::getFile(OptInputFilename.c_str(), input_data);
172 if (ec != llvm::error_code::success()) {
173 ALOGE("Failed to load bitcode from path %s! (%s)",
174 OptInputFilename.c_str(), ec.message().c_str());
Shih-wei Liaod577d112012-04-25 04:06:29 -0700175 return EXIT_FAILURE;
176 }
177
Stephen Hines8be8dba2013-06-18 09:53:43 -0700178 llvm::MemoryBuffer *input_memory = input_data.take();
Shih-wei Liaod577d112012-04-25 04:06:29 -0700179
Stephen Hines8be8dba2013-06-18 09:53:43 -0700180 const char *bitcode = input_memory->getBufferStart();
181 size_t bitcodeSize = input_memory->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 Hines8be8dba2013-06-18 09:53:43 -0700187 bool built = RSCD.build(context, OptOutputPath.c_str(),
188 OptOutputFilename.c_str(), bitcode, bitcodeSize,
Tobias Grosser7b980e12013-06-20 10:12:13 -0700189 OptBCLibFilename.c_str(), NULL, OptEmitLLVM);
Stephen Hines8be8dba2013-06-18 09:53:43 -0700190
191 if (!built) {
Shih-wei Liaod577d112012-04-25 04:06:29 -0700192 return EXIT_FAILURE;
193 }
194
Shih-wei Liaod577d112012-04-25 04:06:29 -0700195 return EXIT_SUCCESS;
196}