Main.cpp revision c3437f05c638f8befda59170ae788873db24dc1c
1/* 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 <dlfcn.h> 21#include <stdlib.h> 22 23#include <llvm/ADT/OwningPtr.h> 24#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> 29#include <llvm/Support/MemoryBuffer.h> 30#include <llvm/Support/PluginLoader.h> 31#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> 38#include <bcc/ExecutionEngine/CompilerRTSymbolResolver.h> 39#include <bcc/ExecutionEngine/ObjectLoader.h> 40#include <bcc/ExecutionEngine/SymbolResolverProxy.h> 41#include <bcc/ExecutionEngine/SymbolResolvers.h> 42#include <bcc/Renderscript/RSCompilerDriver.h> 43#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> 49#include <bcc/Support/TargetCompilerConfigs.h> 50 51using namespace bcc; 52 53#define STR2(a) #a 54#define STR(a) STR2(a) 55 56//===----------------------------------------------------------------------===// 57// General Options 58//===----------------------------------------------------------------------===// 59namespace { 60 61llvm::cl::opt<std::string> 62OptInputFilename(llvm::cl::Positional, llvm::cl::ValueRequired, 63 llvm::cl::desc("<input bitcode file>")); 64 65llvm::cl::opt<std::string> 66OptOutputFilename("o", llvm::cl::desc("Specify the output filename"), 67 llvm::cl::value_desc("filename"), 68 llvm::cl::init("bcc_output")); 69 70llvm::cl::opt<std::string> 71OptBCLibFilename("bclib", llvm::cl::desc("Specify the bclib filename"), 72 llvm::cl::value_desc("bclib")); 73 74llvm::cl::opt<std::string> 75OptOutputPath("output_path", llvm::cl::desc("Specify the output path"), 76 llvm::cl::value_desc("output path"), 77 llvm::cl::init(".")); 78 79llvm::cl::opt<bool> 80OptEmitLLVM("emit-llvm", 81 llvm::cl::desc("Emit an LLVM-IR version of the generated program")); 82 83#ifdef TARGET_BUILD 84const std::string OptTargetTriple(DEFAULT_TARGET_TRIPLE_STRING); 85#else 86llvm::cl::opt<std::string> 87OptTargetTriple("mtriple", 88 llvm::cl::desc("Specify the target triple (default: " 89 DEFAULT_TARGET_TRIPLE_STRING ")"), 90 llvm::cl::init(DEFAULT_TARGET_TRIPLE_STRING), 91 llvm::cl::value_desc("triple")); 92 93llvm::cl::alias OptTargetTripleC("C", llvm::cl::NotHidden, 94 llvm::cl::desc("Alias for -mtriple"), 95 llvm::cl::aliasopt(OptTargetTriple)); 96#endif 97 98llvm::cl::opt<bool> 99OptRSDebugContext("rs-debug-ctx", 100 llvm::cl::desc("Enable build to work with a RenderScript debug context")); 101 102//===----------------------------------------------------------------------===// 103// Compiler Options 104//===----------------------------------------------------------------------===// 105 106// RenderScript uses -O3 by default 107llvm::cl::opt<char> 108OptOptLevel("O", llvm::cl::desc("Optimization level. [-O0, -O1, -O2, or -O3] " 109 "(default: -O3)"), 110 llvm::cl::Prefix, llvm::cl::ZeroOrMore, llvm::cl::init('3')); 111 112// Override "bcc -version" since the LLVM version information is not correct on 113// Android build. 114void BCCVersionPrinter() { 115 llvm::raw_ostream &os = llvm::outs(); 116 os << "libbcc (The Android Open Source Project, http://www.android.com/):\n" 117 << " Build time: " << BuildInfo::GetBuildTime() << "\n" 118 << " Build revision: " << BuildInfo::GetBuildRev() << "\n" 119 << " Build source blob: " << BuildInfo::GetBuildSourceBlob() << "\n" 120 << " Default target: " << DEFAULT_TARGET_TRIPLE_STRING << "\n"; 121 122 os << "\n"; 123 124 os << "LLVM (http://llvm.org/):\n" 125 << " Version: " << PACKAGE_VERSION << "\n"; 126 return; 127} 128 129} // end anonymous namespace 130 131static inline 132bool ConfigCompiler(RSCompilerDriver &pRSCD) { 133 RSCompiler *RSC = pRSCD.getCompiler(); 134 CompilerConfig *config = NULL; 135 136#ifdef TARGET_BUILD 137 config = new (std::nothrow) DefaultCompilerConfig(); 138#else 139 config = new (std::nothrow) CompilerConfig(OptTargetTriple); 140#endif 141 if (config == NULL) { 142 llvm::errs() << "Out of memory when create the compiler configuration!\n"; 143 return false; 144 } 145 146 switch (OptOptLevel) { 147 case '0': config->setOptimizationLevel(llvm::CodeGenOpt::None); break; 148 case '1': config->setOptimizationLevel(llvm::CodeGenOpt::Less); break; 149 case '2': config->setOptimizationLevel(llvm::CodeGenOpt::Default); break; 150 case '3': 151 default: { 152 config->setOptimizationLevel(llvm::CodeGenOpt::Aggressive); 153 break; 154 } 155 } 156 157 pRSCD.setConfig(config); 158 Compiler::ErrorCode result = RSC->config(*config); 159 160 if (OptRSDebugContext) { 161 pRSCD.setDebugContext(true); 162 } 163 164 if (result != Compiler::kSuccess) { 165 llvm::errs() << "Failed to configure the compiler! (detail: " 166 << Compiler::GetErrorString(result) << ")\n"; 167 return false; 168 } 169 170 return true; 171} 172 173int main(int argc, char **argv) { 174 llvm::cl::SetVersionPrinter(BCCVersionPrinter); 175 llvm::cl::ParseCommandLineOptions(argc, argv); 176 init::Initialize(); 177 178 BCCContext context; 179 RSCompilerDriver RSCD; 180 181 llvm::OwningPtr<llvm::MemoryBuffer> input_data; 182 183 llvm::error_code ec = 184 llvm::MemoryBuffer::getFile(OptInputFilename.c_str(), input_data); 185 if (ec != llvm::error_code::success()) { 186 ALOGE("Failed to load bitcode from path %s! (%s)", 187 OptInputFilename.c_str(), ec.message().c_str()); 188 return EXIT_FAILURE; 189 } 190 191 llvm::MemoryBuffer *input_memory = input_data.take(); 192 193 const char *bitcode = input_memory->getBufferStart(); 194 size_t bitcodeSize = input_memory->getBufferSize(); 195 196 if (!ConfigCompiler(RSCD)) { 197 ALOGE("Failed to configure compiler"); 198 return EXIT_FAILURE; 199 } 200 201 // Attempt to dynamically initialize the compiler driver if such a function 202 // is present. It is only present if passed via "-load libFOO.so". 203 RSCompilerDriverInit_t rscdi = (RSCompilerDriverInit_t) 204 dlsym(RTLD_DEFAULT, STR(RS_COMPILER_DRIVER_INIT_FN)); 205 if (rscdi != NULL) { 206 rscdi(&RSCD); 207 } 208 209 bool built = RSCD.build(context, OptOutputPath.c_str(), 210 OptOutputFilename.c_str(), bitcode, bitcodeSize, 211 OptBCLibFilename.c_str(), NULL, OptEmitLLVM); 212 213 if (!built) { 214 return EXIT_FAILURE; 215 } 216 217 return EXIT_SUCCESS; 218} 219