2 * Copyright (C) 2009-2010 Joel Rosdahl
4 * This program is free software; you can redistribute it and/or modify it
5 * under the terms of the GNU General Public License as published by the Free
6 * Software Foundation; either version 3 of the License, or (at your option)
9 * This program is distributed in the hope that it will be useful, but WITHOUT
10 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
11 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
14 * You should have received a copy of the GNU General Public License along with
15 * this program; if not, write to the Free Software Foundation, Inc., 51
16 * Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
21 #include "murmurhashneutral2.h"
24 hash_from_string(void *str)
26 return murmurhashneutral2(str, strlen((const char *)str), 0);
32 return murmurhashneutral2(&i, sizeof(int), 0);
36 strings_equal(void *str1, void *str2)
38 return str_eq((const char *)str1, (const char *)str2);
42 file_hashes_equal(struct file_hash *fh1, struct file_hash *fh2)
44 return memcmp(fh1->hash, fh2->hash, 16) == 0
45 && fh1->size == fh2->size;
50 hashbuf[hashbuflen] = ch; \
52 if (hashbuflen == sizeof(hashbuf)) {\
53 hash_buffer(hash, hashbuf, sizeof(hashbuf)); \
59 * Hash a string ignoring comments. Returns a bitmask of HASH_SOURCE_CODE_*
63 hash_source_code_string(
64 struct mdfour *hash, const char *str, size_t len, const char *path)
69 size_t hashbuflen = 0;
70 int result = HASH_SOURCE_CODE_OK;
71 extern unsigned sloppiness;
80 /* Potential start of comment. */
87 HASH(' '); /* Don't paste tokens together when removing the comment. */
90 && (*p != '*' || *(p+1) != '/')) {
92 /* Keep line numbers. */
106 && (*p != '\n' || *(p-1) == '\\')) {
116 /* Start of string. */
120 while (p < end && (*p != '"' || *(p-1) == '\\')) {
129 /* Potential start of volatile macro. */
132 && p[1] == '_' && p[5] == 'E'
133 && p[6] == '_' && p[7] == '_') {
134 if (p[2] == 'D' && p[3] == 'A'
136 result |= HASH_SOURCE_CODE_FOUND_DATE;
137 } else if (p[2] == 'T' && p[3] == 'I'
139 result |= HASH_SOURCE_CODE_FOUND_TIME;
142 * Of course, we can't be sure that we have found a __{DATE,TIME}__
143 * that's actually used, but better safe than sorry. And if you do
146 * #define TIME __TI ## ME__
148 * in your code, you deserve to get a false cache hit.
162 hash_buffer(hash, hashbuf, hashbuflen);
164 if (sloppiness & SLOPPY_TIME_MACROS) {
167 if (result & HASH_SOURCE_CODE_FOUND_DATE) {
169 * Make sure that the hash sum changes if the (potential) expansion of
172 time_t t = time(NULL);
173 struct tm *now = localtime(&t);
174 cc_log("Found __DATE__ in %s", path);
175 hash_delimiter(hash, "date");
176 hash_buffer(hash, &now->tm_year, sizeof(now->tm_year));
177 hash_buffer(hash, &now->tm_mon, sizeof(now->tm_mon));
178 hash_buffer(hash, &now->tm_mday, sizeof(now->tm_mday));
180 if (result & HASH_SOURCE_CODE_FOUND_TIME) {
182 * We don't know for sure that the program actually uses the __TIME__
183 * macro, but we have to assume it anyway and hash the time stamp. However,
184 * that's not very useful since the chance that we get a cache hit later
185 * the same second should be quite slim... So, just signal back to the
186 * caller that __TIME__ has been found so that the direct mode can be
189 cc_log("Found __TIME__ in %s", path);
196 * Hash a file ignoring comments. Returns a bitmask of HASH_SOURCE_CODE_*
200 hash_source_code_file(struct mdfour *hash, const char *path)
206 if (is_precompiled_header(path)) {
207 if (hash_file(hash, path)) {
208 return HASH_SOURCE_CODE_OK;
210 return HASH_SOURCE_CODE_ERROR;
213 if (!read_file(path, 0, &data, &size)) {
214 return HASH_SOURCE_CODE_ERROR;
216 result = hash_source_code_string(hash, data, size, path);
223 hash_command_output(struct mdfour *hash, const char *command,
224 const char *compiler)
229 struct args *args = args_init_from_string(command);
231 for (i = 0; i < args->argc; i++) {
232 if (str_eq(args->argv[i], "%compiler%")) {
233 args_set(args, i, compiler);
236 cc_log_argv("Executing compiler check command ", args->argv);
238 if (pipe(pipefd) == -1) {
239 fatal("pipe failed");
243 fatal("fork failed");
252 _exit(execvp(args->argv[0], args->argv));
253 return false; /* Never reached. */
260 ok = hash_fd(hash, pipefd[0]);
262 cc_log("Error hashing compiler check command output: %s", strerror(errno));
263 stats_update(STATS_COMPCHECK);
266 if (waitpid(pid, &status, 0) != pid) {
267 cc_log("waitpid failed");
270 if (!WIFEXITED(status) || WEXITSTATUS(status) != 0) {
271 cc_log("Compiler check command returned %d", WEXITSTATUS(status));
272 stats_update(STATS_COMPCHECK);
280 hash_multicommand_output(struct mdfour *hash, const char *commands,
281 const char *compiler)
283 char *command_string, *command, *p, *saveptr = NULL;
286 command_string = x_strdup(commands);
288 while ((command = strtok_r(p, ";", &saveptr))) {
289 if (!hash_command_output(hash, command, compiler)) {
294 free(command_string);