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