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