Shih-wei Liao | d577d11 | 2012-04-25 04:06:29 -0700 | [diff] [blame] | 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 | |
Tim Murray | c2074ca | 2014-04-08 15:39:08 -0700 | [diff] [blame^] | 22 | #include <llvm/ADT/OwningPtr.h> |
Shih-wei Liao | d577d11 | 2012-04-25 04:06:29 -0700 | [diff] [blame] | 23 | #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 Hines | 8be8dba | 2013-06-18 09:53:43 -0700 | [diff] [blame] | 28 | #include <llvm/Support/MemoryBuffer.h> |
Shih-wei Liao | d577d11 | 2012-04-25 04:06:29 -0700 | [diff] [blame] | 29 | #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 Liao | 09ca954 | 2013-01-25 17:00:08 -0800 | [diff] [blame] | 36 | #include <bcc/ExecutionEngine/CompilerRTSymbolResolver.h> |
Shih-wei Liao | d577d11 | 2012-04-25 04:06:29 -0700 | [diff] [blame] | 37 | #include <bcc/ExecutionEngine/ObjectLoader.h> |
| 38 | #include <bcc/ExecutionEngine/SymbolResolverProxy.h> |
| 39 | #include <bcc/ExecutionEngine/SymbolResolvers.h> |
Stephen Hines | 8be8dba | 2013-06-18 09:53:43 -0700 | [diff] [blame] | 40 | #include <bcc/Renderscript/RSCompilerDriver.h> |
Shih-wei Liao | d577d11 | 2012-04-25 04:06:29 -0700 | [diff] [blame] | 41 | #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 | |
| 49 | using namespace bcc; |
| 50 | |
| 51 | //===----------------------------------------------------------------------===// |
| 52 | // General Options |
| 53 | //===----------------------------------------------------------------------===// |
| 54 | namespace { |
| 55 | |
Stephen Hines | 8be8dba | 2013-06-18 09:53:43 -0700 | [diff] [blame] | 56 | llvm::cl::opt<std::string> |
| 57 | OptInputFilename(llvm::cl::Positional, llvm::cl::ValueRequired, |
| 58 | llvm::cl::desc("<input bitcode file>")); |
Shih-wei Liao | d577d11 | 2012-04-25 04:06:29 -0700 | [diff] [blame] | 59 | |
| 60 | llvm::cl::opt<std::string> |
| 61 | OptOutputFilename("o", llvm::cl::desc("Specify the output filename"), |
Stephen Hines | 8be8dba | 2013-06-18 09:53:43 -0700 | [diff] [blame] | 62 | llvm::cl::value_desc("filename"), |
| 63 | llvm::cl::init("bcc_output")); |
| 64 | |
| 65 | llvm::cl::opt<std::string> |
| 66 | OptBCLibFilename("bclib", llvm::cl::desc("Specify the bclib filename"), |
| 67 | llvm::cl::value_desc("bclib")); |
| 68 | |
| 69 | llvm::cl::opt<std::string> |
| 70 | OptOutputPath("output_path", llvm::cl::desc("Specify the output path"), |
| 71 | llvm::cl::value_desc("output path"), |
| 72 | llvm::cl::init(".")); |
Shih-wei Liao | d577d11 | 2012-04-25 04:06:29 -0700 | [diff] [blame] | 73 | |
Tobias Grosser | 7b980e1 | 2013-06-20 10:12:13 -0700 | [diff] [blame] | 74 | llvm::cl::opt<bool> |
| 75 | OptEmitLLVM("emit-llvm", |
| 76 | llvm::cl::desc("Emit an LLVM-IR version of the generated program")); |
| 77 | |
Shih-wei Liao | d577d11 | 2012-04-25 04:06:29 -0700 | [diff] [blame] | 78 | #ifdef TARGET_BUILD |
| 79 | const std::string OptTargetTriple(DEFAULT_TARGET_TRIPLE_STRING); |
| 80 | #else |
| 81 | llvm::cl::opt<std::string> |
| 82 | OptTargetTriple("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 | |
| 88 | llvm::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 Liao | d577d11 | 2012-04-25 04:06:29 -0700 | [diff] [blame] | 96 | |
Stephen Hines | 7e9c185 | 2013-06-21 19:25:34 -0700 | [diff] [blame] | 97 | // RenderScript uses -O3 by default |
Shih-wei Liao | d577d11 | 2012-04-25 04:06:29 -0700 | [diff] [blame] | 98 | llvm::cl::opt<char> |
| 99 | OptOptLevel("O", llvm::cl::desc("Optimization level. [-O0, -O1, -O2, or -O3] " |
Stephen Hines | 7e9c185 | 2013-06-21 19:25:34 -0700 | [diff] [blame] | 100 | "(default: -O3)"), |
| 101 | llvm::cl::Prefix, llvm::cl::ZeroOrMore, llvm::cl::init('3')); |
Shih-wei Liao | d577d11 | 2012-04-25 04:06:29 -0700 | [diff] [blame] | 102 | |
Shih-wei Liao | d577d11 | 2012-04-25 04:06:29 -0700 | [diff] [blame] | 103 | // Override "bcc -version" since the LLVM version information is not correct on |
| 104 | // Android build. |
| 105 | void 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 | |
| 122 | static inline |
Stephen Hines | 7e9c185 | 2013-06-21 19:25:34 -0700 | [diff] [blame] | 123 | bool ConfigCompiler(RSCompilerDriver &pRSCD) { |
| 124 | RSCompiler *RSC = pRSCD.getCompiler(); |
Shih-wei Liao | d577d11 | 2012-04-25 04:06:29 -0700 | [diff] [blame] | 125 | 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 Liao | d577d11 | 2012-04-25 04:06:29 -0700 | [diff] [blame] | 137 | switch (OptOptLevel) { |
| 138 | case '0': config->setOptimizationLevel(llvm::CodeGenOpt::None); break; |
| 139 | case '1': config->setOptimizationLevel(llvm::CodeGenOpt::Less); break; |
Stephen Hines | 7e9c185 | 2013-06-21 19:25:34 -0700 | [diff] [blame] | 140 | case '2': config->setOptimizationLevel(llvm::CodeGenOpt::Default); break; |
| 141 | case '3': |
Shih-wei Liao | d577d11 | 2012-04-25 04:06:29 -0700 | [diff] [blame] | 142 | default: { |
Stephen Hines | 7e9c185 | 2013-06-21 19:25:34 -0700 | [diff] [blame] | 143 | config->setOptimizationLevel(llvm::CodeGenOpt::Aggressive); |
Shih-wei Liao | d577d11 | 2012-04-25 04:06:29 -0700 | [diff] [blame] | 144 | break; |
| 145 | } |
| 146 | } |
| 147 | |
Stephen Hines | 7e9c185 | 2013-06-21 19:25:34 -0700 | [diff] [blame] | 148 | pRSCD.setConfig(config); |
| 149 | Compiler::ErrorCode result = RSC->config(*config); |
Shih-wei Liao | d577d11 | 2012-04-25 04:06:29 -0700 | [diff] [blame] | 150 | |
| 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 Liao | d577d11 | 2012-04-25 04:06:29 -0700 | [diff] [blame] | 160 | int main(int argc, char **argv) { |
| 161 | llvm::cl::SetVersionPrinter(BCCVersionPrinter); |
| 162 | llvm::cl::ParseCommandLineOptions(argc, argv); |
| 163 | init::Initialize(); |
| 164 | |
| 165 | BCCContext context; |
Stephen Hines | 8be8dba | 2013-06-18 09:53:43 -0700 | [diff] [blame] | 166 | RSCompilerDriver RSCD; |
Shih-wei Liao | d577d11 | 2012-04-25 04:06:29 -0700 | [diff] [blame] | 167 | |
Stephen Hines | 8be8dba | 2013-06-18 09:53:43 -0700 | [diff] [blame] | 168 | 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 Liao | d577d11 | 2012-04-25 04:06:29 -0700 | [diff] [blame] | 175 | return EXIT_FAILURE; |
| 176 | } |
| 177 | |
Stephen Hines | 8be8dba | 2013-06-18 09:53:43 -0700 | [diff] [blame] | 178 | llvm::MemoryBuffer *input_memory = input_data.take(); |
Shih-wei Liao | d577d11 | 2012-04-25 04:06:29 -0700 | [diff] [blame] | 179 | |
Stephen Hines | 8be8dba | 2013-06-18 09:53:43 -0700 | [diff] [blame] | 180 | const char *bitcode = input_memory->getBufferStart(); |
| 181 | size_t bitcodeSize = input_memory->getBufferSize(); |
Shih-wei Liao | d577d11 | 2012-04-25 04:06:29 -0700 | [diff] [blame] | 182 | |
Stephen Hines | 7e9c185 | 2013-06-21 19:25:34 -0700 | [diff] [blame] | 183 | if (!ConfigCompiler(RSCD)) { |
| 184 | ALOGE("Failed to configure compiler"); |
| 185 | return EXIT_FAILURE; |
| 186 | } |
Stephen Hines | 8be8dba | 2013-06-18 09:53:43 -0700 | [diff] [blame] | 187 | bool built = RSCD.build(context, OptOutputPath.c_str(), |
| 188 | OptOutputFilename.c_str(), bitcode, bitcodeSize, |
Tobias Grosser | 7b980e1 | 2013-06-20 10:12:13 -0700 | [diff] [blame] | 189 | OptBCLibFilename.c_str(), NULL, OptEmitLLVM); |
Stephen Hines | 8be8dba | 2013-06-18 09:53:43 -0700 | [diff] [blame] | 190 | |
| 191 | if (!built) { |
Shih-wei Liao | d577d11 | 2012-04-25 04:06:29 -0700 | [diff] [blame] | 192 | return EXIT_FAILURE; |
| 193 | } |
| 194 | |
Shih-wei Liao | d577d11 | 2012-04-25 04:06:29 -0700 | [diff] [blame] | 195 | return EXIT_SUCCESS; |
| 196 | } |