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