Logan | 474cbd2 | 2011-01-31 01:47:44 +0800 | [diff] [blame] | 1 | /* |
| 2 | * Copyright 2010, 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 | |
Logan | 474cbd2 | 2011-01-31 01:47:44 +0800 | [diff] [blame] | 17 | #include "SourceInfo.h" |
| 18 | |
Shih-wei Liao | 5e3e0ce | 2011-06-17 13:59:46 -0700 | [diff] [blame] | 19 | #include "MCCacheWriter.h" |
| 20 | #include "MCCacheReader.h" |
Logan | 474cbd2 | 2011-01-31 01:47:44 +0800 | [diff] [blame] | 21 | |
Logan | 4dcd679 | 2011-02-28 05:12:00 +0800 | [diff] [blame] | 22 | #include "DebugHelper.h" |
Logan | 474cbd2 | 2011-01-31 01:47:44 +0800 | [diff] [blame] | 23 | #include "ScriptCompiled.h" |
| 24 | #include "Sha1Helper.h" |
| 25 | |
| 26 | #include <bcc/bcc.h> |
Logan | 474cbd2 | 2011-01-31 01:47:44 +0800 | [diff] [blame] | 27 | |
Zonr Chang | df3fee4 | 2012-01-10 15:58:36 +0800 | [diff] [blame] | 28 | #include <llvm/Bitcode/ReaderWriter.h> |
| 29 | #include <llvm/Module.h> |
| 30 | #include <llvm/LLVMContext.h> |
Logan | 474cbd2 | 2011-01-31 01:47:44 +0800 | [diff] [blame] | 31 | #include <llvm/ADT/OwningPtr.h> |
| 32 | #include <llvm/ADT/StringRef.h> |
| 33 | #include <llvm/Support/MemoryBuffer.h> |
Logan Chien | c4ea07f | 2011-03-09 17:27:50 +0800 | [diff] [blame] | 34 | #include <llvm/Support/system_error.h> |
Logan | 474cbd2 | 2011-01-31 01:47:44 +0800 | [diff] [blame] | 35 | |
| 36 | #include <stddef.h> |
| 37 | #include <string.h> |
| 38 | |
| 39 | namespace bcc { |
| 40 | |
| 41 | |
| 42 | SourceInfo *SourceInfo::createFromBuffer(char const *resName, |
| 43 | char const *bitcode, |
| 44 | size_t bitcodeSize, |
| 45 | unsigned long flags) { |
| 46 | SourceInfo *result = new SourceInfo(); |
| 47 | |
| 48 | if (!result) { |
| 49 | return NULL; |
| 50 | } |
| 51 | |
| 52 | result->type = SourceKind::Buffer; |
| 53 | result->buffer.resName = resName; |
| 54 | result->buffer.bitcode = bitcode; |
| 55 | result->buffer.bitcodeSize = bitcodeSize; |
| 56 | result->flags = flags; |
| 57 | |
Logan | 474cbd2 | 2011-01-31 01:47:44 +0800 | [diff] [blame] | 58 | if (!resName && !(flags & BCC_SKIP_DEP_SHA1)) { |
| 59 | result->flags |= BCC_SKIP_DEP_SHA1; |
| 60 | |
Steve Block | 10c5145 | 2012-01-05 23:23:07 +0000 | [diff] [blame] | 61 | ALOGW("It is required to give resName for sha1 dependency check.\n"); |
| 62 | ALOGW("Sha1sum dependency check will be skipped.\n"); |
| 63 | ALOGW("Set BCC_SKIP_DEP_SHA1 for flags to surpress this warning.\n"); |
Logan | 474cbd2 | 2011-01-31 01:47:44 +0800 | [diff] [blame] | 64 | } |
| 65 | |
| 66 | if (result->flags & BCC_SKIP_DEP_SHA1) { |
| 67 | memset(result->sha1, '\0', 20); |
| 68 | } else { |
| 69 | calcSHA1(result->sha1, bitcode, bitcodeSize); |
| 70 | } |
Logan | 474cbd2 | 2011-01-31 01:47:44 +0800 | [diff] [blame] | 71 | |
| 72 | return result; |
| 73 | } |
| 74 | |
| 75 | |
| 76 | SourceInfo *SourceInfo::createFromFile(char const *path, |
| 77 | unsigned long flags) { |
| 78 | SourceInfo *result = new SourceInfo(); |
| 79 | |
| 80 | if (!result) { |
| 81 | return NULL; |
| 82 | } |
| 83 | |
| 84 | result->type = SourceKind::File; |
| 85 | result->file.path = path; |
| 86 | result->flags = flags; |
| 87 | |
Logan | 474cbd2 | 2011-01-31 01:47:44 +0800 | [diff] [blame] | 88 | memset(result->sha1, '\0', 20); |
| 89 | |
| 90 | if (!(result->flags & BCC_SKIP_DEP_SHA1)) { |
| 91 | calcFileSHA1(result->sha1, path); |
| 92 | } |
Logan | 474cbd2 | 2011-01-31 01:47:44 +0800 | [diff] [blame] | 93 | |
| 94 | return result; |
| 95 | } |
| 96 | |
| 97 | |
| 98 | SourceInfo *SourceInfo::createFromModule(llvm::Module *module, |
| 99 | unsigned long flags) { |
| 100 | SourceInfo *result = new SourceInfo(); |
| 101 | |
| 102 | if (!result) { |
| 103 | return NULL; |
| 104 | } |
| 105 | |
| 106 | result->type = SourceKind::Module; |
Zonr Chang | df3fee4 | 2012-01-10 15:58:36 +0800 | [diff] [blame] | 107 | result->module = module; |
Logan | 474cbd2 | 2011-01-31 01:47:44 +0800 | [diff] [blame] | 108 | result->flags = flags; |
| 109 | |
Logan | 474cbd2 | 2011-01-31 01:47:44 +0800 | [diff] [blame] | 110 | if (! (flags & BCC_SKIP_DEP_SHA1)) { |
| 111 | result->flags |= BCC_SKIP_DEP_SHA1; |
| 112 | |
Steve Block | 10c5145 | 2012-01-05 23:23:07 +0000 | [diff] [blame] | 113 | ALOGW("Unable to calculate sha1sum for llvm::Module.\n"); |
| 114 | ALOGW("Sha1sum dependency check will be skipped.\n"); |
| 115 | ALOGW("Set BCC_SKIP_DEP_SHA1 for flags to surpress this warning.\n"); |
Logan | 474cbd2 | 2011-01-31 01:47:44 +0800 | [diff] [blame] | 116 | } |
| 117 | |
| 118 | memset(result->sha1, '\0', 20); |
Logan | 474cbd2 | 2011-01-31 01:47:44 +0800 | [diff] [blame] | 119 | |
| 120 | return result; |
| 121 | } |
| 122 | |
| 123 | |
Zonr Chang | df3fee4 | 2012-01-10 15:58:36 +0800 | [diff] [blame] | 124 | int SourceInfo::prepareModule(llvm::LLVMContext *context) { |
| 125 | if (module) |
| 126 | return 0; |
| 127 | |
| 128 | llvm::OwningPtr<llvm::MemoryBuffer> mem; |
| 129 | std::string errmsg; |
| 130 | |
Logan | 474cbd2 | 2011-01-31 01:47:44 +0800 | [diff] [blame] | 131 | switch (type) { |
| 132 | case SourceKind::Buffer: |
| 133 | { |
Zonr Chang | df3fee4 | 2012-01-10 15:58:36 +0800 | [diff] [blame] | 134 | mem.reset(llvm::MemoryBuffer::getMemBuffer( |
Logan | 474cbd2 | 2011-01-31 01:47:44 +0800 | [diff] [blame] | 135 | llvm::StringRef(buffer.bitcode, buffer.bitcodeSize))); |
| 136 | |
Zonr Chang | df3fee4 | 2012-01-10 15:58:36 +0800 | [diff] [blame] | 137 | if (!mem.get()) { |
Steve Block | 10c1412 | 2012-01-08 10:15:06 +0000 | [diff] [blame] | 138 | ALOGE("Unable to MemoryBuffer::getMemBuffer(addr=%p, size=%lu)\n", |
Zonr Chang | df3fee4 | 2012-01-10 15:58:36 +0800 | [diff] [blame] | 139 | buffer.bitcode, (unsigned long)buffer.bitcodeSize); |
Logan | 474cbd2 | 2011-01-31 01:47:44 +0800 | [diff] [blame] | 140 | return 1; |
| 141 | } |
Logan | 474cbd2 | 2011-01-31 01:47:44 +0800 | [diff] [blame] | 142 | } |
| 143 | break; |
| 144 | |
| 145 | case SourceKind::File: |
| 146 | { |
Zonr Chang | df3fee4 | 2012-01-10 15:58:36 +0800 | [diff] [blame] | 147 | if (llvm::error_code ec = llvm::MemoryBuffer::getFile(file.path, mem)) { |
| 148 | ALOGE("Unable to MemoryBuffer::getFile(path=%s, %s)\n", |
| 149 | file.path, ec.message().c_str()); |
Logan | 474cbd2 | 2011-01-31 01:47:44 +0800 | [diff] [blame] | 150 | return 1; |
| 151 | } |
Logan | 474cbd2 | 2011-01-31 01:47:44 +0800 | [diff] [blame] | 152 | } |
| 153 | break; |
| 154 | |
| 155 | default: |
Zonr Chang | df3fee4 | 2012-01-10 15:58:36 +0800 | [diff] [blame] | 156 | return 0; |
Logan | 474cbd2 | 2011-01-31 01:47:44 +0800 | [diff] [blame] | 157 | break; |
| 158 | } |
| 159 | |
Zonr Chang | df3fee4 | 2012-01-10 15:58:36 +0800 | [diff] [blame] | 160 | if (context) |
| 161 | shared_context = true; |
| 162 | else |
| 163 | context = new llvm::LLVMContext(); |
| 164 | |
| 165 | module = llvm::ParseBitcodeFile(mem.get(), *context, &errmsg); |
| 166 | if (module == NULL) { |
| 167 | ALOGE("Unable to ParseBitcodeFile: %s\n", errmsg.c_str()); |
| 168 | if (!shared_context) |
| 169 | delete context; |
| 170 | } |
| 171 | |
| 172 | return (module == NULL); |
Logan | 474cbd2 | 2011-01-31 01:47:44 +0800 | [diff] [blame] | 173 | } |
| 174 | |
Zonr Chang | df3fee4 | 2012-01-10 15:58:36 +0800 | [diff] [blame] | 175 | SourceInfo::~SourceInfo() { |
Zonr Chang | 4adbcb2 | 2012-02-07 11:19:21 +0800 | [diff] [blame] | 176 | if (module != NULL) { |
| 177 | llvm::LLVMContext *context = &module->getContext(); |
| 178 | delete module; |
| 179 | if (!shared_context) |
| 180 | delete context; |
| 181 | } |
Zonr Chang | df3fee4 | 2012-01-10 15:58:36 +0800 | [diff] [blame] | 182 | } |
Logan | 474cbd2 | 2011-01-31 01:47:44 +0800 | [diff] [blame] | 183 | |
Logan | 474cbd2 | 2011-01-31 01:47:44 +0800 | [diff] [blame] | 184 | template <typename T> void SourceInfo::introDependency(T &checker) { |
| 185 | if (flags & BCC_SKIP_DEP_SHA1) { |
| 186 | return; |
| 187 | } |
| 188 | |
| 189 | switch (type) { |
| 190 | case SourceKind::Buffer: |
| 191 | checker.addDependency(BCC_APK_RESOURCE, buffer.resName, sha1); |
| 192 | break; |
| 193 | |
| 194 | case SourceKind::File: |
| 195 | checker.addDependency(BCC_FILE_RESOURCE, file.path, sha1); |
| 196 | break; |
| 197 | |
| 198 | default: |
| 199 | break; |
| 200 | } |
| 201 | } |
| 202 | |
Shih-wei Liao | 5e3e0ce | 2011-06-17 13:59:46 -0700 | [diff] [blame] | 203 | template void SourceInfo::introDependency<MCCacheWriter>(MCCacheWriter &); |
| 204 | template void SourceInfo::introDependency<MCCacheReader>(MCCacheReader &); |
Logan | 474cbd2 | 2011-01-31 01:47:44 +0800 | [diff] [blame] | 205 | |
| 206 | |
| 207 | } // namespace bcc |