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