Main.cpp revision 331310e1f3f86a795f78e42b3f03558a43829f09
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 <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> 27#include <llvm/Support/Path.h> 28#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> 35#include <bcc/ExecutionEngine/BCCRuntimeSymbolResolver.h> 36#include <bcc/ExecutionEngine/ObjectLoader.h> 37#include <bcc/ExecutionEngine/SymbolResolverProxy.h> 38#include <bcc/ExecutionEngine/SymbolResolvers.h> 39#include <bcc/Renderscript/RSCompilerDriver.h> 40#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 55llvm::cl::list<std::string> 56OptInputFilenames(llvm::cl::Positional, llvm::cl::OneOrMore, 57 llvm::cl::desc("<input bitcode files>")); 58 59llvm::cl::opt<std::string> 60OptOutputFilename("o", llvm::cl::desc("Specify the output filename"), 61 llvm::cl::value_desc("filename")); 62 63llvm::cl::opt<std::string> 64OptRuntimePath("rt-path", llvm::cl::desc("Specify the runtime library path"), 65 llvm::cl::value_desc("path")); 66 67#ifdef TARGET_BUILD 68const std::string OptTargetTriple(DEFAULT_TARGET_TRIPLE_STRING); 69#else 70llvm::cl::opt<std::string> 71OptTargetTriple("mtriple", 72 llvm::cl::desc("Specify the target triple (default: " 73 DEFAULT_TARGET_TRIPLE_STRING ")"), 74 llvm::cl::init(DEFAULT_TARGET_TRIPLE_STRING), 75 llvm::cl::value_desc("triple")); 76 77llvm::cl::alias OptTargetTripleC("C", llvm::cl::NotHidden, 78 llvm::cl::desc("Alias for -mtriple"), 79 llvm::cl::aliasopt(OptTargetTriple)); 80#endif 81 82//===----------------------------------------------------------------------===// 83// Compiler Options 84//===----------------------------------------------------------------------===// 85llvm::cl::opt<bool> 86OptPIC("fPIC", llvm::cl::desc("Generate fully relocatable, position independent" 87 " code")); 88 89llvm::cl::opt<char> 90OptOptLevel("O", llvm::cl::desc("Optimization level. [-O0, -O1, -O2, or -O3] " 91 "(default: -O2)"), 92 llvm::cl::Prefix, llvm::cl::ZeroOrMore, llvm::cl::init('2')); 93 94llvm::cl::opt<bool> 95OptC("c", llvm::cl::desc("Compile and assemble, but do not link.")); 96 97//===----------------------------------------------------------------------===// 98// Linker Options 99//===----------------------------------------------------------------------===// 100// FIXME: this option will be removed in the future when MCLinker is capable 101// of generating shared library directly from given bitcode. It only 102// takes effect when -shared is supplied. 103llvm::cl::opt<std::string> 104OptImmObjectOutput("or", llvm::cl::desc("Specify the filename for output the " 105 "intermediate relocatable when linking " 106 "the input bitcode to the shared " 107 "library"), llvm::cl::ValueRequired); 108 109llvm::cl::opt<bool> 110OptShared("shared", llvm::cl::desc("Create a shared library from input bitcode " 111 "files")); 112 113 114// Override "bcc -version" since the LLVM version information is not correct on 115// Android build. 116void BCCVersionPrinter() { 117 llvm::raw_ostream &os = llvm::outs(); 118 os << "libbcc (The Android Open Source Project, http://www.android.com/):\n" 119 << " Build time: " << BuildInfo::GetBuildTime() << "\n" 120 << " Build revision: " << BuildInfo::GetBuildRev() << "\n" 121 << " Build source blob: " << BuildInfo::GetBuildSourceBlob() << "\n" 122 << " Default target: " << DEFAULT_TARGET_TRIPLE_STRING << "\n"; 123 124 os << "\n"; 125 126 os << "LLVM (http://llvm.org/):\n" 127 << " Version: " << PACKAGE_VERSION << "\n"; 128 return; 129} 130 131} // end anonymous namespace 132 133RSScript *PrepareRSScript(BCCContext &pContext, 134 const llvm::cl::list<std::string> &pBitcodeFiles) { 135 RSScript *result = NULL; 136 137 for (unsigned i = 0; i < pBitcodeFiles.size(); i++) { 138 const std::string &input_bitcode = pBitcodeFiles[i]; 139 Source *source = Source::CreateFromFile(pContext, input_bitcode); 140 if (source == NULL) { 141 llvm::errs() << "Failed to load llvm module from file `" << input_bitcode 142 << "'!\n"; 143 return NULL; 144 } 145 146 if (result != NULL) { 147 if (!result->mergeSource(*source, /* pPreserveSource */false)) { 148 llvm::errs() << "Failed to merge the llvm module `" << input_bitcode 149 << "' to compile!\n"; 150 delete source; 151 return NULL; 152 } 153 } else { 154 result = new (std::nothrow) RSScript(*source); 155 if (result == NULL) { 156 llvm::errs() << "Out of memory when create script for file `" 157 << input_bitcode << "'!\n"; 158 delete source; 159 return NULL; 160 } 161 } 162 } 163 164 return result; 165} 166 167static inline 168bool ConfigCompiler(RSCompilerDriver &pCompilerDriver) { 169 RSCompiler *compiler = pCompilerDriver.getCompiler(); 170 CompilerConfig *config = NULL; 171 172#ifdef TARGET_BUILD 173 config = new (std::nothrow) DefaultCompilerConfig(); 174#else 175 config = new (std::nothrow) CompilerConfig(OptTargetTriple); 176#endif 177 if (config == NULL) { 178 llvm::errs() << "Out of memory when create the compiler configuration!\n"; 179 return false; 180 } 181 182 // Setup the config according to the value of command line option. 183 if (OptPIC) { 184 config->setRelocationModel(llvm::Reloc::PIC_); 185 } 186 switch (OptOptLevel) { 187 case '0': config->setOptimizationLevel(llvm::CodeGenOpt::None); break; 188 case '1': config->setOptimizationLevel(llvm::CodeGenOpt::Less); break; 189 case '3': config->setOptimizationLevel(llvm::CodeGenOpt::Aggressive); break; 190 case '2': 191 default: { 192 config->setOptimizationLevel(llvm::CodeGenOpt::Default); 193 break; 194 } 195 } 196 197 pCompilerDriver.setConfig(config); 198 Compiler::ErrorCode result = compiler->config(*config); 199 200 if (result != Compiler::kSuccess) { 201 llvm::errs() << "Failed to configure the compiler! (detail: " 202 << Compiler::GetErrorString(result) << ")\n"; 203 return false; 204 } 205 206 return true; 207} 208 209#define DEFAULT_OUTPUT_PATH "/sdcard/a.out" 210static inline 211std::string DetermineOutputFilename(const std::string &pOutputPath) { 212 if (!pOutputPath.empty()) { 213 return pOutputPath; 214 } 215 216 // User doesn't specify the value to -o. 217 if (OptInputFilenames.size() > 1) { 218 llvm::errs() << "Use " DEFAULT_OUTPUT_PATH " for output file!\n"; 219 return DEFAULT_OUTPUT_PATH; 220 } 221 222 // There's only one input bitcode file. 223 const std::string &input_path = OptInputFilenames[0]; 224 llvm::SmallString<200> output_path(input_path); 225 226 llvm::error_code err = llvm::sys::fs::make_absolute(output_path); 227 if (err != llvm::errc::success) { 228 llvm::errs() << "Failed to determine the absolute path of `" << input_path 229 << "'! (detail: " << err.message() << ")\n"; 230 return ""; 231 } 232 233 if (OptC) { 234 // -c was specified. Replace the extension to .o. 235 llvm::sys::path::replace_extension(output_path, "o"); 236 } else { 237 // Use a.out under current working directory when compile executable or 238 // shared library. 239 llvm::sys::path::remove_filename(output_path); 240 llvm::sys::path::append(output_path, "a.out"); 241 } 242 243 return output_path.c_str(); 244} 245 246int main(int argc, char **argv) { 247 llvm::cl::SetVersionPrinter(BCCVersionPrinter); 248 llvm::cl::ParseCommandLineOptions(argc, argv); 249 init::Initialize(); 250 251 if (OptRuntimePath.empty()) { 252 fprintf(stderr, "You must set \"-rt-path </path/to/libclcore.bc>\" with " 253 "this tool\n"); 254 return EXIT_FAILURE; 255 } 256 257 BCCContext context; 258 RSCompilerDriver rscd; 259 Compiler compiler; 260 261 if (!ConfigCompiler(rscd)) { 262 return EXIT_FAILURE; 263 } 264 265 std::string OutputFilename = DetermineOutputFilename(OptOutputFilename); 266 if (OutputFilename.empty()) { 267 return EXIT_FAILURE; 268 } 269 270 RSScript *s = NULL; 271 s = PrepareRSScript(context, OptInputFilenames); 272 rscd.build(*s, OutputFilename.c_str(), OptRuntimePath.c_str()); 273 274 return EXIT_SUCCESS; 275} 276