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 | |
| 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> |
Stephen Hines | 8be8dba | 2013-06-18 09:53:43 -0700 | [diff] [blame] | 27 | #include <llvm/Support/MemoryBuffer.h> |
Shih-wei Liao | d577d11 | 2012-04-25 04:06:29 -0700 | [diff] [blame] | 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> |
Shih-wei Liao | 09ca954 | 2013-01-25 17:00:08 -0800 | [diff] [blame] | 35 | #include <bcc/ExecutionEngine/CompilerRTSymbolResolver.h> |
Shih-wei Liao | d577d11 | 2012-04-25 04:06:29 -0700 | [diff] [blame] | 36 | #include <bcc/ExecutionEngine/ObjectLoader.h> |
| 37 | #include <bcc/ExecutionEngine/SymbolResolverProxy.h> |
| 38 | #include <bcc/ExecutionEngine/SymbolResolvers.h> |
Stephen Hines | 8be8dba | 2013-06-18 09:53:43 -0700 | [diff] [blame] | 39 | #include <bcc/Renderscript/RSCompilerDriver.h> |
Shih-wei Liao | d577d11 | 2012-04-25 04:06:29 -0700 | [diff] [blame] | 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 | |
| 48 | using namespace bcc; |
| 49 | |
| 50 | //===----------------------------------------------------------------------===// |
| 51 | // General Options |
| 52 | //===----------------------------------------------------------------------===// |
| 53 | namespace { |
| 54 | |
Stephen Hines | 8be8dba | 2013-06-18 09:53:43 -0700 | [diff] [blame] | 55 | llvm::cl::opt<std::string> |
| 56 | OptInputFilename(llvm::cl::Positional, llvm::cl::ValueRequired, |
| 57 | llvm::cl::desc("<input bitcode file>")); |
Shih-wei Liao | d577d11 | 2012-04-25 04:06:29 -0700 | [diff] [blame] | 58 | |
| 59 | llvm::cl::opt<std::string> |
| 60 | OptOutputFilename("o", llvm::cl::desc("Specify the output filename"), |
Stephen Hines | 8be8dba | 2013-06-18 09:53:43 -0700 | [diff] [blame] | 61 | llvm::cl::value_desc("filename"), |
| 62 | llvm::cl::init("bcc_output")); |
| 63 | |
| 64 | llvm::cl::opt<std::string> |
| 65 | OptBCLibFilename("bclib", llvm::cl::desc("Specify the bclib filename"), |
| 66 | llvm::cl::value_desc("bclib")); |
| 67 | |
| 68 | llvm::cl::opt<std::string> |
| 69 | OptOutputPath("output_path", llvm::cl::desc("Specify the output path"), |
| 70 | llvm::cl::value_desc("output path"), |
| 71 | llvm::cl::init(".")); |
Shih-wei Liao | d577d11 | 2012-04-25 04:06:29 -0700 | [diff] [blame] | 72 | |
Tobias Grosser | 7b980e1 | 2013-06-20 10:12:13 -0700 | [diff] [blame] | 73 | llvm::cl::opt<bool> |
| 74 | OptEmitLLVM("emit-llvm", |
| 75 | llvm::cl::desc("Emit an LLVM-IR version of the generated program")); |
| 76 | |
Shih-wei Liao | d577d11 | 2012-04-25 04:06:29 -0700 | [diff] [blame] | 77 | #ifdef TARGET_BUILD |
| 78 | const std::string OptTargetTriple(DEFAULT_TARGET_TRIPLE_STRING); |
| 79 | #else |
| 80 | llvm::cl::opt<std::string> |
| 81 | OptTargetTriple("mtriple", |
| 82 | llvm::cl::desc("Specify the target triple (default: " |
| 83 | DEFAULT_TARGET_TRIPLE_STRING ")"), |
| 84 | llvm::cl::init(DEFAULT_TARGET_TRIPLE_STRING), |
| 85 | llvm::cl::value_desc("triple")); |
| 86 | |
| 87 | llvm::cl::alias OptTargetTripleC("C", llvm::cl::NotHidden, |
| 88 | llvm::cl::desc("Alias for -mtriple"), |
| 89 | llvm::cl::aliasopt(OptTargetTriple)); |
| 90 | #endif |
| 91 | |
| 92 | //===----------------------------------------------------------------------===// |
| 93 | // Compiler Options |
| 94 | //===----------------------------------------------------------------------===// |
Shih-wei Liao | d577d11 | 2012-04-25 04:06:29 -0700 | [diff] [blame] | 95 | |
Stephen Hines | 7e9c185 | 2013-06-21 19:25:34 -0700 | [diff] [blame] | 96 | // RenderScript uses -O3 by default |
Shih-wei Liao | d577d11 | 2012-04-25 04:06:29 -0700 | [diff] [blame] | 97 | llvm::cl::opt<char> |
| 98 | OptOptLevel("O", llvm::cl::desc("Optimization level. [-O0, -O1, -O2, or -O3] " |
Stephen Hines | 7e9c185 | 2013-06-21 19:25:34 -0700 | [diff] [blame] | 99 | "(default: -O3)"), |
| 100 | llvm::cl::Prefix, llvm::cl::ZeroOrMore, llvm::cl::init('3')); |
Shih-wei Liao | d577d11 | 2012-04-25 04:06:29 -0700 | [diff] [blame] | 101 | |
Shih-wei Liao | d577d11 | 2012-04-25 04:06:29 -0700 | [diff] [blame] | 102 | // Override "bcc -version" since the LLVM version information is not correct on |
| 103 | // Android build. |
| 104 | void BCCVersionPrinter() { |
| 105 | llvm::raw_ostream &os = llvm::outs(); |
| 106 | os << "libbcc (The Android Open Source Project, http://www.android.com/):\n" |
| 107 | << " Build time: " << BuildInfo::GetBuildTime() << "\n" |
| 108 | << " Build revision: " << BuildInfo::GetBuildRev() << "\n" |
| 109 | << " Build source blob: " << BuildInfo::GetBuildSourceBlob() << "\n" |
| 110 | << " Default target: " << DEFAULT_TARGET_TRIPLE_STRING << "\n"; |
| 111 | |
| 112 | os << "\n"; |
| 113 | |
| 114 | os << "LLVM (http://llvm.org/):\n" |
| 115 | << " Version: " << PACKAGE_VERSION << "\n"; |
| 116 | return; |
| 117 | } |
| 118 | |
| 119 | } // end anonymous namespace |
| 120 | |
| 121 | static inline |
Stephen Hines | 7e9c185 | 2013-06-21 19:25:34 -0700 | [diff] [blame] | 122 | bool ConfigCompiler(RSCompilerDriver &pRSCD) { |
| 123 | RSCompiler *RSC = pRSCD.getCompiler(); |
Shih-wei Liao | d577d11 | 2012-04-25 04:06:29 -0700 | [diff] [blame] | 124 | CompilerConfig *config = NULL; |
| 125 | |
| 126 | #ifdef TARGET_BUILD |
| 127 | config = new (std::nothrow) DefaultCompilerConfig(); |
| 128 | #else |
| 129 | config = new (std::nothrow) CompilerConfig(OptTargetTriple); |
| 130 | #endif |
| 131 | if (config == NULL) { |
| 132 | llvm::errs() << "Out of memory when create the compiler configuration!\n"; |
| 133 | return false; |
| 134 | } |
| 135 | |
Shih-wei Liao | d577d11 | 2012-04-25 04:06:29 -0700 | [diff] [blame] | 136 | switch (OptOptLevel) { |
| 137 | case '0': config->setOptimizationLevel(llvm::CodeGenOpt::None); break; |
| 138 | case '1': config->setOptimizationLevel(llvm::CodeGenOpt::Less); break; |
Stephen Hines | 7e9c185 | 2013-06-21 19:25:34 -0700 | [diff] [blame] | 139 | case '2': config->setOptimizationLevel(llvm::CodeGenOpt::Default); break; |
| 140 | case '3': |
Shih-wei Liao | d577d11 | 2012-04-25 04:06:29 -0700 | [diff] [blame] | 141 | default: { |
Stephen Hines | 7e9c185 | 2013-06-21 19:25:34 -0700 | [diff] [blame] | 142 | config->setOptimizationLevel(llvm::CodeGenOpt::Aggressive); |
Shih-wei Liao | d577d11 | 2012-04-25 04:06:29 -0700 | [diff] [blame] | 143 | break; |
| 144 | } |
| 145 | } |
| 146 | |
Stephen Hines | 7e9c185 | 2013-06-21 19:25:34 -0700 | [diff] [blame] | 147 | pRSCD.setConfig(config); |
| 148 | Compiler::ErrorCode result = RSC->config(*config); |
Shih-wei Liao | d577d11 | 2012-04-25 04:06:29 -0700 | [diff] [blame] | 149 | |
| 150 | if (result != Compiler::kSuccess) { |
| 151 | llvm::errs() << "Failed to configure the compiler! (detail: " |
| 152 | << Compiler::GetErrorString(result) << ")\n"; |
| 153 | return false; |
| 154 | } |
| 155 | |
| 156 | return true; |
| 157 | } |
| 158 | |
Shih-wei Liao | d577d11 | 2012-04-25 04:06:29 -0700 | [diff] [blame] | 159 | int main(int argc, char **argv) { |
| 160 | llvm::cl::SetVersionPrinter(BCCVersionPrinter); |
| 161 | llvm::cl::ParseCommandLineOptions(argc, argv); |
| 162 | init::Initialize(); |
| 163 | |
| 164 | BCCContext context; |
Stephen Hines | 8be8dba | 2013-06-18 09:53:43 -0700 | [diff] [blame] | 165 | RSCompilerDriver RSCD; |
Shih-wei Liao | d577d11 | 2012-04-25 04:06:29 -0700 | [diff] [blame] | 166 | |
Stephen Hines | 8be8dba | 2013-06-18 09:53:43 -0700 | [diff] [blame] | 167 | llvm::OwningPtr<llvm::MemoryBuffer> input_data; |
| 168 | |
| 169 | llvm::error_code ec = |
| 170 | llvm::MemoryBuffer::getFile(OptInputFilename.c_str(), input_data); |
| 171 | if (ec != llvm::error_code::success()) { |
| 172 | ALOGE("Failed to load bitcode from path %s! (%s)", |
| 173 | OptInputFilename.c_str(), ec.message().c_str()); |
Shih-wei Liao | d577d11 | 2012-04-25 04:06:29 -0700 | [diff] [blame] | 174 | return EXIT_FAILURE; |
| 175 | } |
| 176 | |
Stephen Hines | 8be8dba | 2013-06-18 09:53:43 -0700 | [diff] [blame] | 177 | llvm::MemoryBuffer *input_memory = input_data.take(); |
Shih-wei Liao | d577d11 | 2012-04-25 04:06:29 -0700 | [diff] [blame] | 178 | |
Stephen Hines | 8be8dba | 2013-06-18 09:53:43 -0700 | [diff] [blame] | 179 | const char *bitcode = input_memory->getBufferStart(); |
| 180 | size_t bitcodeSize = input_memory->getBufferSize(); |
Shih-wei Liao | d577d11 | 2012-04-25 04:06:29 -0700 | [diff] [blame] | 181 | |
Stephen Hines | 7e9c185 | 2013-06-21 19:25:34 -0700 | [diff] [blame] | 182 | if (!ConfigCompiler(RSCD)) { |
| 183 | ALOGE("Failed to configure compiler"); |
| 184 | return EXIT_FAILURE; |
| 185 | } |
Stephen Hines | 8be8dba | 2013-06-18 09:53:43 -0700 | [diff] [blame] | 186 | bool built = RSCD.build(context, OptOutputPath.c_str(), |
| 187 | OptOutputFilename.c_str(), bitcode, bitcodeSize, |
Tobias Grosser | 7b980e1 | 2013-06-20 10:12:13 -0700 | [diff] [blame] | 188 | OptBCLibFilename.c_str(), NULL, OptEmitLLVM); |
Stephen Hines | 8be8dba | 2013-06-18 09:53:43 -0700 | [diff] [blame] | 189 | |
| 190 | if (!built) { |
Shih-wei Liao | d577d11 | 2012-04-25 04:06:29 -0700 | [diff] [blame] | 191 | return EXIT_FAILURE; |
| 192 | } |
| 193 | |
Shih-wei Liao | d577d11 | 2012-04-25 04:06:29 -0700 | [diff] [blame] | 194 | return EXIT_SUCCESS; |
| 195 | } |