1 | /* $Id: kObjCache.c 1047 2007-06-09 23:23:16Z bird $ */
|
---|
2 | /** @file
|
---|
3 | *
|
---|
4 | * kObjCache - Object Cache.
|
---|
5 | *
|
---|
6 | * Copyright (c) 2007 knut st. osmundsen <[email protected]>
|
---|
7 | *
|
---|
8 | *
|
---|
9 | * This file is part of kBuild.
|
---|
10 | *
|
---|
11 | * kBuild is free software; you can redistribute it and/or modify
|
---|
12 | * it under the terms of the GNU General Public License as published by
|
---|
13 | * the Free Software Foundation; either version 2 of the License, or
|
---|
14 | * (at your option) any later version.
|
---|
15 | *
|
---|
16 | * kBuild is distributed in the hope that it will be useful,
|
---|
17 | * but WITHOUT ANY WARRANTY; without even the implied warranty of
|
---|
18 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
---|
19 | * GNU General Public License for more details.
|
---|
20 | *
|
---|
21 | * You should have received a copy of the GNU General Public License
|
---|
22 | * along with kBuild; if not, write to the Free Software
|
---|
23 | * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
|
---|
24 | *
|
---|
25 | */
|
---|
26 |
|
---|
27 |
|
---|
28 | /*******************************************************************************
|
---|
29 | * Header Files *
|
---|
30 | *******************************************************************************/
|
---|
31 | #if 0
|
---|
32 | # define ELECTRIC_HEAP
|
---|
33 | # include "../kmk/electric.h"
|
---|
34 | #endif
|
---|
35 | #include <string.h>
|
---|
36 | #include <stdlib.h>
|
---|
37 | #include <stdarg.h>
|
---|
38 | #include <stdio.h>
|
---|
39 | #include <errno.h>
|
---|
40 | #include <assert.h>
|
---|
41 | #include <sys/stat.h>
|
---|
42 | #include <fcntl.h>
|
---|
43 | #include <limits.h>
|
---|
44 | #include <ctype.h>
|
---|
45 | #ifndef PATH_MAX
|
---|
46 | # define PATH_MAX _MAX_PATH /* windows */
|
---|
47 | #endif
|
---|
48 | #if defined(__OS2__) || defined(__WIN__)
|
---|
49 | # include <process.h>
|
---|
50 | # include <io.h>
|
---|
51 | # ifdef __OS2__
|
---|
52 | # include <unistd.h>
|
---|
53 | # endif
|
---|
54 | # if defined(_MSC_VER)
|
---|
55 | typedef intptr_t pid_t;
|
---|
56 | # endif
|
---|
57 | #else
|
---|
58 | # include <unistd.h>
|
---|
59 | # include <sys/wait.h>
|
---|
60 | # ifndef O_BINARY
|
---|
61 | # define O_BINARY 0
|
---|
62 | # endif
|
---|
63 | #endif
|
---|
64 | #if defined(__WIN__)
|
---|
65 | # include <Windows.h>
|
---|
66 | #endif
|
---|
67 |
|
---|
68 | #include "crc32.h"
|
---|
69 | #include "md5.h"
|
---|
70 |
|
---|
71 |
|
---|
72 | /*******************************************************************************
|
---|
73 | * Defined Constants And Macros *
|
---|
74 | *******************************************************************************/
|
---|
75 | /** The max line length in a cache file. */
|
---|
76 | #define KOBJCACHE_MAX_LINE_LEN 16384
|
---|
77 | #if defined(__WIN__)
|
---|
78 | # define PATH_SLASH '\\'
|
---|
79 | #else
|
---|
80 | # define PATH_SLASH '/'
|
---|
81 | #endif
|
---|
82 | #if defined(__OS2__) || defined(__WIN__)
|
---|
83 | # define IS_SLASH(ch) ((ch) == '/' || (ch) == '\\')
|
---|
84 | # define IS_SLASH_DRV(ch) ((ch) == '/' || (ch) == '\\' || (ch) == ':')
|
---|
85 | #else
|
---|
86 | # define IS_SLASH(ch) ((ch) == '/')
|
---|
87 | # define IS_SLASH_DRV(ch) ((ch) == '/')
|
---|
88 | #endif
|
---|
89 |
|
---|
90 | #ifndef STDIN_FILENO
|
---|
91 | # define STDIN_FILENO 0
|
---|
92 | #endif
|
---|
93 | #ifndef STDOUT_FILENO
|
---|
94 | # define STDOUT_FILENO 1
|
---|
95 | #endif
|
---|
96 | #ifndef STDERR_FILENO
|
---|
97 | # define STDERR_FILENO 2
|
---|
98 | #endif
|
---|
99 |
|
---|
100 |
|
---|
101 | /*******************************************************************************
|
---|
102 | * Global Variables *
|
---|
103 | *******************************************************************************/
|
---|
104 | /** Whether verbose output is enabled. */
|
---|
105 | static unsigned g_cVerbosityLevel = 0;
|
---|
106 | /** What to prefix the errors with. */
|
---|
107 | static char g_szErrorPrefix[128];
|
---|
108 |
|
---|
109 | /** Read buffer shared by the cache components. */
|
---|
110 | static char g_szLine[KOBJCACHE_MAX_LINE_LEN + 16];
|
---|
111 |
|
---|
112 |
|
---|
113 | /*******************************************************************************
|
---|
114 | * Internal Functions *
|
---|
115 | *******************************************************************************/
|
---|
116 | static char *MakePathFromDirAndFile(const char *pszName, const char *pszDir);
|
---|
117 | static char *CalcRelativeName(const char *pszPath, const char *pszDir);
|
---|
118 | static FILE *FOpenFileInDir(const char *pszName, const char *pszDir, const char *pszMode);
|
---|
119 | static int UnlinkFileInDir(const char *pszName, const char *pszDir);
|
---|
120 | static int RenameFileInDir(const char *pszOldName, const char *pszNewName, const char *pszDir);
|
---|
121 | static int DoesFileInDirExist(const char *pszName, const char *pszDir);
|
---|
122 | static void *ReadFileInDir(const char *pszName, const char *pszDir, size_t *pcbFile);
|
---|
123 |
|
---|
124 |
|
---|
125 | void FatalMsg(const char *pszFormat, ...)
|
---|
126 | {
|
---|
127 | va_list va;
|
---|
128 |
|
---|
129 | if (g_szErrorPrefix[0])
|
---|
130 | fprintf(stderr, "%s - fatal error: ", g_szErrorPrefix);
|
---|
131 | else
|
---|
132 | fprintf(stderr, "fatal error: ");
|
---|
133 |
|
---|
134 | va_start(va, pszFormat);
|
---|
135 | vfprintf(stderr, pszFormat, va);
|
---|
136 | va_end(va);
|
---|
137 | }
|
---|
138 |
|
---|
139 |
|
---|
140 | void FatalDie(const char *pszFormat, ...)
|
---|
141 | {
|
---|
142 | va_list va;
|
---|
143 |
|
---|
144 | if (g_szErrorPrefix[0])
|
---|
145 | fprintf(stderr, "%s - fatal error: ", g_szErrorPrefix);
|
---|
146 | else
|
---|
147 | fprintf(stderr, "fatal error: ");
|
---|
148 |
|
---|
149 | va_start(va, pszFormat);
|
---|
150 | vfprintf(stderr, pszFormat, va);
|
---|
151 | va_end(va);
|
---|
152 |
|
---|
153 | exit(1);
|
---|
154 | }
|
---|
155 |
|
---|
156 |
|
---|
157 | static void ErrorMsg(const char *pszFormat, ...)
|
---|
158 | {
|
---|
159 | va_list va;
|
---|
160 |
|
---|
161 | if (g_szErrorPrefix[0])
|
---|
162 | fprintf(stderr, "%s - error: ", g_szErrorPrefix);
|
---|
163 | else
|
---|
164 | fprintf(stderr, "error: ");
|
---|
165 |
|
---|
166 | va_start(va, pszFormat);
|
---|
167 | vfprintf(stderr, pszFormat, va);
|
---|
168 | va_end(va);
|
---|
169 | }
|
---|
170 |
|
---|
171 |
|
---|
172 | static void InfoMsg(unsigned uLevel, const char *pszFormat, ...)
|
---|
173 | {
|
---|
174 | if (uLevel <= g_cVerbosityLevel)
|
---|
175 | {
|
---|
176 | va_list va;
|
---|
177 |
|
---|
178 | if (g_szErrorPrefix[0])
|
---|
179 | fprintf(stderr, "%s - info: ", g_szErrorPrefix);
|
---|
180 | else
|
---|
181 | fprintf(stderr, "info: ");
|
---|
182 |
|
---|
183 | va_start(va, pszFormat);
|
---|
184 | vfprintf(stderr, pszFormat, va);
|
---|
185 | va_end(va);
|
---|
186 | }
|
---|
187 | }
|
---|
188 |
|
---|
189 |
|
---|
190 | static void SetErrorPrefix(const char *pszPrefix, ...)
|
---|
191 | {
|
---|
192 | int cch;
|
---|
193 | va_list va;
|
---|
194 |
|
---|
195 | va_start(va, pszPrefix);
|
---|
196 | #if defined(_MSC_VER) || defined(__sun__)
|
---|
197 | cch = vsprintf(g_szErrorPrefix, pszPrefix, va);
|
---|
198 | if (cch >= sizeof(g_szErrorPrefix))
|
---|
199 | FatalDie("Buffer overflow setting error prefix!\n");
|
---|
200 | #else
|
---|
201 | vsnprintf(g_szErrorPrefix, sizeof(g_szErrorPrefix), pszPrefix, va);
|
---|
202 | #endif
|
---|
203 | va_end(va);
|
---|
204 | (void)cch;
|
---|
205 | }
|
---|
206 |
|
---|
207 | #ifndef ELECTRIC_HEAP
|
---|
208 | void *xmalloc(size_t cb)
|
---|
209 | {
|
---|
210 | void *pv = malloc(cb);
|
---|
211 | if (!pv)
|
---|
212 | FatalDie("out of memory (%d)\n", (int)cb);
|
---|
213 | return pv;
|
---|
214 | }
|
---|
215 |
|
---|
216 |
|
---|
217 | void *xrealloc(void *pvOld, size_t cb)
|
---|
218 | {
|
---|
219 | void *pv = realloc(pvOld, cb);
|
---|
220 | if (!pv)
|
---|
221 | FatalDie("out of memory (%d)\n", (int)cb);
|
---|
222 | return pv;
|
---|
223 | }
|
---|
224 |
|
---|
225 |
|
---|
226 | char *xstrdup(const char *pszIn)
|
---|
227 | {
|
---|
228 | char *psz = strdup(pszIn);
|
---|
229 | if (!psz)
|
---|
230 | FatalDie("out of memory (%d)\n", (int)strlen(pszIn));
|
---|
231 | return psz;
|
---|
232 | }
|
---|
233 | #endif
|
---|
234 |
|
---|
235 |
|
---|
236 | void *xmallocz(size_t cb)
|
---|
237 | {
|
---|
238 | void *pv = xmalloc(cb);
|
---|
239 | memset(pv, 0, cb);
|
---|
240 | return pv;
|
---|
241 | }
|
---|
242 |
|
---|
243 |
|
---|
244 |
|
---|
245 |
|
---|
246 |
|
---|
247 | /**
|
---|
248 | * Gets the absolute path
|
---|
249 | *
|
---|
250 | * @returns A new heap buffer containing the absolute path.
|
---|
251 | * @param pszPath The path to make absolute. (Readonly)
|
---|
252 | */
|
---|
253 | static char *AbsPath(const char *pszPath)
|
---|
254 | {
|
---|
255 | char szTmp[PATH_MAX];
|
---|
256 | #if defined(__OS2__) || defined(__WIN__)
|
---|
257 | if (!_fullpath(szTmp, *pszPath ? pszPath : ".", sizeof(szTmp)))
|
---|
258 | return xstrdup(pszPath);
|
---|
259 | #else
|
---|
260 | if (!realpath(pszPath, szTmp))
|
---|
261 | return xstrdup(pszPath);
|
---|
262 | #endif
|
---|
263 | return xstrdup(szTmp);
|
---|
264 | }
|
---|
265 |
|
---|
266 |
|
---|
267 | /**
|
---|
268 | * Utility function that finds the filename part in a path.
|
---|
269 | *
|
---|
270 | * @returns Pointer to the file name part (this may be "").
|
---|
271 | * @param pszPath The path to parse.
|
---|
272 | */
|
---|
273 | static const char *FindFilenameInPath(const char *pszPath)
|
---|
274 | {
|
---|
275 | const char *pszFilename = strchr(pszPath, '\0') - 1;
|
---|
276 | while ( pszFilename > pszPath
|
---|
277 | && !IS_SLASH_DRV(pszFilename[-1]))
|
---|
278 | pszFilename--;
|
---|
279 | return pszFilename;
|
---|
280 | }
|
---|
281 |
|
---|
282 |
|
---|
283 | /**
|
---|
284 | * Utility function that combines a filename and a directory into a path.
|
---|
285 | *
|
---|
286 | * @returns malloced buffer containing the result.
|
---|
287 | * @param pszName The file name.
|
---|
288 | * @param pszDir The directory path.
|
---|
289 | */
|
---|
290 | static char *MakePathFromDirAndFile(const char *pszName, const char *pszDir)
|
---|
291 | {
|
---|
292 | size_t cchName = strlen(pszName);
|
---|
293 | size_t cchDir = strlen(pszDir);
|
---|
294 | char *pszBuf = xmalloc(cchName + cchDir + 2);
|
---|
295 | memcpy(pszBuf, pszDir, cchDir);
|
---|
296 | if (cchDir > 0 && !IS_SLASH_DRV(pszDir[cchDir - 1]))
|
---|
297 | pszBuf[cchDir++] = PATH_SLASH;
|
---|
298 | memcpy(pszBuf + cchDir, pszName, cchName + 1);
|
---|
299 | return pszBuf;
|
---|
300 | }
|
---|
301 |
|
---|
302 |
|
---|
303 | /**
|
---|
304 | * Compares two path strings to see if they are identical.
|
---|
305 | *
|
---|
306 | * This doesn't do anything fancy, just the case ignoring and
|
---|
307 | * slash unification.
|
---|
308 | *
|
---|
309 | * @returns 1 if equal, 0 otherwise.
|
---|
310 | * @param pszPath1 The first path.
|
---|
311 | * @param pszPath2 The second path.
|
---|
312 | * @param cch The number of characters to compare.
|
---|
313 | */
|
---|
314 | static int ArePathsIdentical(const char *pszPath1, const char *pszPath2, size_t cch)
|
---|
315 | {
|
---|
316 | #if defined(__OS2__) || defined(__WIN__)
|
---|
317 | if (strnicmp(pszPath1, pszPath2, cch))
|
---|
318 | {
|
---|
319 | /* Slashes may differ, compare char by char. */
|
---|
320 | const char *psz1 = pszPath1;
|
---|
321 | const char *psz2 = pszPath2;
|
---|
322 | for (;cch; psz1++, psz2++, cch--)
|
---|
323 | {
|
---|
324 | if (*psz1 != *psz2)
|
---|
325 | {
|
---|
326 | if ( tolower(*psz1) != tolower(*psz2)
|
---|
327 | && toupper(*psz1) != toupper(*psz2)
|
---|
328 | && *psz1 != '/'
|
---|
329 | && *psz1 != '\\'
|
---|
330 | && *psz2 != '/'
|
---|
331 | && *psz2 != '\\')
|
---|
332 | return 0;
|
---|
333 | }
|
---|
334 | }
|
---|
335 | }
|
---|
336 | return 1;
|
---|
337 | #else
|
---|
338 | return !strncmp(pszPath1, pszPath2, cch);
|
---|
339 | #endif
|
---|
340 | }
|
---|
341 |
|
---|
342 |
|
---|
343 | /**
|
---|
344 | * Calculate how to get to pszPath from pszDir.
|
---|
345 | *
|
---|
346 | * @returns The relative path from pszDir to path pszPath.
|
---|
347 | * @param pszPath The path to the object.
|
---|
348 | * @param pszDir The directory it shall be relative to.
|
---|
349 | */
|
---|
350 | static char *CalcRelativeName(const char *pszPath, const char *pszDir)
|
---|
351 | {
|
---|
352 | char *pszRet = NULL;
|
---|
353 | char *pszAbsPath = NULL;
|
---|
354 | size_t cchDir = strlen(pszDir);
|
---|
355 |
|
---|
356 | /*
|
---|
357 | * This is indeed a bit tricky, so we'll try the easy way first...
|
---|
358 | */
|
---|
359 | if (ArePathsIdentical(pszPath, pszDir, cchDir))
|
---|
360 | {
|
---|
361 | if (pszPath[cchDir])
|
---|
362 | pszRet = (char *)pszPath + cchDir;
|
---|
363 | else
|
---|
364 | pszRet = "./";
|
---|
365 | }
|
---|
366 | else
|
---|
367 | {
|
---|
368 | pszAbsPath = AbsPath(pszPath);
|
---|
369 | if (ArePathsIdentical(pszAbsPath, pszDir, cchDir))
|
---|
370 | {
|
---|
371 | if (pszPath[cchDir])
|
---|
372 | pszRet = pszAbsPath + cchDir;
|
---|
373 | else
|
---|
374 | pszRet = "./";
|
---|
375 | }
|
---|
376 | }
|
---|
377 | if (pszRet)
|
---|
378 | {
|
---|
379 | while (IS_SLASH_DRV(*pszRet))
|
---|
380 | pszRet++;
|
---|
381 | pszRet = xstrdup(pszRet);
|
---|
382 | free(pszAbsPath);
|
---|
383 | return pszRet;
|
---|
384 | }
|
---|
385 |
|
---|
386 | /*
|
---|
387 | * Damn, it's gonna be complicated. Deal with that later.
|
---|
388 | */
|
---|
389 | FatalDie("complicated relative path stuff isn't implemented yet. sorry.\n");
|
---|
390 | return NULL;
|
---|
391 | }
|
---|
392 |
|
---|
393 |
|
---|
394 | /**
|
---|
395 | * Utility function that combines a filename and directory and passes it onto fopen.
|
---|
396 | *
|
---|
397 | * @returns fopen return value.
|
---|
398 | * @param pszName The file name.
|
---|
399 | * @param pszDir The directory path.
|
---|
400 | * @param pszMode The fopen mode string.
|
---|
401 | */
|
---|
402 | static FILE *FOpenFileInDir(const char *pszName, const char *pszDir, const char *pszMode)
|
---|
403 | {
|
---|
404 | char *pszPath = MakePathFromDirAndFile(pszName, pszDir);
|
---|
405 | FILE *pFile = fopen(pszPath, pszMode);
|
---|
406 | free(pszPath);
|
---|
407 | return pFile;
|
---|
408 | }
|
---|
409 |
|
---|
410 |
|
---|
411 | /**
|
---|
412 | * Utility function that combines a filename and directory and passes it onto open.
|
---|
413 | *
|
---|
414 | * @returns open return value.
|
---|
415 | * @param pszName The file name.
|
---|
416 | * @param pszDir The directory path.
|
---|
417 | * @param fFlags The open flags.
|
---|
418 | * @param fCreateMode The file creation mode.
|
---|
419 | */
|
---|
420 | static int OpenFileInDir(const char *pszName, const char *pszDir, int fFlags, int fCreateMode)
|
---|
421 | {
|
---|
422 | char *pszPath = MakePathFromDirAndFile(pszName, pszDir);
|
---|
423 | int fd = open(pszPath, fFlags, fCreateMode);
|
---|
424 | free(pszPath);
|
---|
425 | return fd;
|
---|
426 | }
|
---|
427 |
|
---|
428 |
|
---|
429 |
|
---|
430 | /**
|
---|
431 | * Deletes a file in a directory.
|
---|
432 | *
|
---|
433 | * @returns whatever unlink returns.
|
---|
434 | * @param pszName The file name.
|
---|
435 | * @param pszDir The directory path.
|
---|
436 | */
|
---|
437 | static int UnlinkFileInDir(const char *pszName, const char *pszDir)
|
---|
438 | {
|
---|
439 | char *pszPath = MakePathFromDirAndFile(pszName, pszDir);
|
---|
440 | int rc = unlink(pszPath);
|
---|
441 | free(pszPath);
|
---|
442 | return rc;
|
---|
443 | }
|
---|
444 |
|
---|
445 |
|
---|
446 | /**
|
---|
447 | * Renames a file in a directory.
|
---|
448 | *
|
---|
449 | * @returns whatever rename returns.
|
---|
450 | * @param pszOldName The new file name.
|
---|
451 | * @param pszNewName The old file name.
|
---|
452 | * @param pszDir The directory path.
|
---|
453 | */
|
---|
454 | static int RenameFileInDir(const char *pszOldName, const char *pszNewName, const char *pszDir)
|
---|
455 | {
|
---|
456 | char *pszOldPath = MakePathFromDirAndFile(pszOldName, pszDir);
|
---|
457 | char *pszNewPath = MakePathFromDirAndFile(pszNewName, pszDir);
|
---|
458 | int rc = rename(pszOldPath, pszNewPath);
|
---|
459 | free(pszOldPath);
|
---|
460 | free(pszNewPath);
|
---|
461 | return rc;
|
---|
462 | }
|
---|
463 |
|
---|
464 |
|
---|
465 | /**
|
---|
466 | * Check if a (regular) file exists in a directory.
|
---|
467 | *
|
---|
468 | * @returns 1 if it exists and is a regular file, 0 if not.
|
---|
469 | * @param pszName The file name.
|
---|
470 | * @param pszDir The directory path.
|
---|
471 | */
|
---|
472 | static int DoesFileInDirExist(const char *pszName, const char *pszDir)
|
---|
473 | {
|
---|
474 | char *pszPath = MakePathFromDirAndFile(pszName, pszDir);
|
---|
475 | struct stat st;
|
---|
476 | int rc = stat(pszPath, &st);
|
---|
477 | free(pszPath);
|
---|
478 | #ifdef S_ISREG
|
---|
479 | return !rc && S_ISREG(st.st_mode);
|
---|
480 | #elif defined(_MSC_VER)
|
---|
481 | return !rc && (st.st_mode & _S_IFMT) == _S_IFREG;
|
---|
482 | #else
|
---|
483 | #error "Port me"
|
---|
484 | #endif
|
---|
485 | }
|
---|
486 |
|
---|
487 |
|
---|
488 | /**
|
---|
489 | * Reads into memory an entire file.
|
---|
490 | *
|
---|
491 | * @returns Pointer to the heap allocation containing the file.
|
---|
492 | * On failure NULL and errno is returned.
|
---|
493 | * @param pszName The file.
|
---|
494 | * @param pszDir The directory the file resides in.
|
---|
495 | * @param pcbFile Where to store the file size.
|
---|
496 | */
|
---|
497 | static void *ReadFileInDir(const char *pszName, const char *pszDir, size_t *pcbFile)
|
---|
498 | {
|
---|
499 | int SavedErrno;
|
---|
500 | char *pszPath = MakePathFromDirAndFile(pszName, pszDir);
|
---|
501 | int fd = open(pszPath, O_RDONLY | O_BINARY);
|
---|
502 | if (fd >= 0)
|
---|
503 | {
|
---|
504 | off_t cbFile = lseek(fd, 0, SEEK_END);
|
---|
505 | if ( cbFile >= 0
|
---|
506 | && lseek(fd, 0, SEEK_SET) == 0)
|
---|
507 | {
|
---|
508 | char *pb = malloc(cbFile + 1);
|
---|
509 | if (pb)
|
---|
510 | {
|
---|
511 | if (read(fd, pb, cbFile) == cbFile)
|
---|
512 | {
|
---|
513 | close(fd);
|
---|
514 | pb[cbFile] = '\0';
|
---|
515 | *pcbFile = (size_t)cbFile;
|
---|
516 | return pb;
|
---|
517 | }
|
---|
518 | SavedErrno = errno;
|
---|
519 | free(pb);
|
---|
520 | }
|
---|
521 | else
|
---|
522 | SavedErrno = ENOMEM;
|
---|
523 | }
|
---|
524 | else
|
---|
525 | SavedErrno = errno;
|
---|
526 | close(fd);
|
---|
527 | }
|
---|
528 | else
|
---|
529 | SavedErrno = errno;
|
---|
530 | free(pszPath);
|
---|
531 | errno = SavedErrno;
|
---|
532 | return NULL;
|
---|
533 | }
|
---|
534 |
|
---|
535 |
|
---|
536 | /**
|
---|
537 | * Creates a directory including all necessary parent directories.
|
---|
538 | *
|
---|
539 | * @returns 0 on success, -1 + errno on failure.
|
---|
540 | * @param pszDir The directory.
|
---|
541 | */
|
---|
542 | static int MakePath(const char *pszPath)
|
---|
543 | {
|
---|
544 | int iErr = 0;
|
---|
545 | char *pszAbsPath = AbsPath(pszPath);
|
---|
546 | char *psz = pszAbsPath;
|
---|
547 |
|
---|
548 | /* Skip to the root slash (PC). */
|
---|
549 | while (!IS_SLASH(*psz) && *psz)
|
---|
550 | psz++;
|
---|
551 | /** @todo UNC */
|
---|
552 | for (;;)
|
---|
553 | {
|
---|
554 | char chSaved;
|
---|
555 |
|
---|
556 | /* skip slashes */
|
---|
557 | while (IS_SLASH(*psz))
|
---|
558 | psz++;
|
---|
559 | if (!*psz)
|
---|
560 | break;
|
---|
561 |
|
---|
562 | /* find the next slash or end and terminate the string. */
|
---|
563 | while (!IS_SLASH(*psz) && *psz)
|
---|
564 | psz++;
|
---|
565 | chSaved = *psz;
|
---|
566 | *psz = '\0';
|
---|
567 |
|
---|
568 | /* try create the directory, ignore failure because the directory already exists. */
|
---|
569 | errno = 0;
|
---|
570 | #ifdef _MSC_VER
|
---|
571 | if ( _mkdir(pszAbsPath)
|
---|
572 | && errno != EEXIST)
|
---|
573 | #else
|
---|
574 | if ( mkdir(pszAbsPath, 0777)
|
---|
575 | && errno != EEXIST)
|
---|
576 | #endif
|
---|
577 | {
|
---|
578 | iErr = errno;
|
---|
579 | break;
|
---|
580 | }
|
---|
581 |
|
---|
582 | /* restore the slash/terminator */
|
---|
583 | *psz = chSaved;
|
---|
584 | }
|
---|
585 |
|
---|
586 | free(pszAbsPath);
|
---|
587 | return iErr ? -1 : 0;
|
---|
588 | }
|
---|
589 |
|
---|
590 |
|
---|
591 | /**
|
---|
592 | * Adds the arguments found in the pszCmdLine string to argument vector.
|
---|
593 | *
|
---|
594 | * The parsing of the pszCmdLine string isn't very sophisticated, no
|
---|
595 | * escaping or quotes.
|
---|
596 | *
|
---|
597 | * @param pcArgs Pointer to the argument counter.
|
---|
598 | * @param ppapszArgs Pointer to the argument vector pointer.
|
---|
599 | * @param pszCmdLine The command line to parse and append.
|
---|
600 | * @param pszWedgeArg Argument to put infront of anything found in pszCmdLine.
|
---|
601 | */
|
---|
602 | static void AppendArgs(int *pcArgs, char ***ppapszArgs, const char *pszCmdLine, const char *pszWedgeArg)
|
---|
603 | {
|
---|
604 | int i;
|
---|
605 | int cExtraArgs;
|
---|
606 | const char *psz;
|
---|
607 | char **papszArgs;
|
---|
608 |
|
---|
609 | /*
|
---|
610 | * Count the new arguments.
|
---|
611 | */
|
---|
612 | cExtraArgs = 0;
|
---|
613 | psz = pszCmdLine;
|
---|
614 | while (*psz)
|
---|
615 | {
|
---|
616 | while (isspace(*psz))
|
---|
617 | psz++;
|
---|
618 | if (!psz)
|
---|
619 | break;
|
---|
620 | cExtraArgs++;
|
---|
621 | while (!isspace(*psz) && *psz)
|
---|
622 | psz++;
|
---|
623 | }
|
---|
624 | if (!cExtraArgs)
|
---|
625 | return;
|
---|
626 |
|
---|
627 | /*
|
---|
628 | * Allocate a new vector that can hold the arguments.
|
---|
629 | * (Reallocating might not work since the argv might not be allocated
|
---|
630 | * from the heap but off the stack or somewhere... )
|
---|
631 | */
|
---|
632 | i = *pcArgs;
|
---|
633 | *pcArgs = i + cExtraArgs + !!pszWedgeArg;
|
---|
634 | papszArgs = xmalloc((*pcArgs + 1) * sizeof(char *));
|
---|
635 | *ppapszArgs = memcpy(papszArgs, *ppapszArgs, i * sizeof(char *));
|
---|
636 |
|
---|
637 | if (pszWedgeArg)
|
---|
638 | papszArgs[i++] = xstrdup(pszWedgeArg);
|
---|
639 |
|
---|
640 | psz = pszCmdLine;
|
---|
641 | while (*psz)
|
---|
642 | {
|
---|
643 | size_t cch;
|
---|
644 | const char *pszEnd;
|
---|
645 | while (isspace(*psz))
|
---|
646 | psz++;
|
---|
647 | if (!psz)
|
---|
648 | break;
|
---|
649 | pszEnd = psz;
|
---|
650 | while (!isspace(*pszEnd) && *pszEnd)
|
---|
651 | pszEnd++;
|
---|
652 |
|
---|
653 | cch = pszEnd - psz;
|
---|
654 | papszArgs[i] = xmalloc(cch + 1);
|
---|
655 | memcpy(papszArgs[i], psz, cch);
|
---|
656 | papszArgs[i][cch] = '\0';
|
---|
657 |
|
---|
658 | i++;
|
---|
659 | psz = pszEnd;
|
---|
660 | }
|
---|
661 |
|
---|
662 | papszArgs[i] = NULL;
|
---|
663 | }
|
---|
664 |
|
---|
665 |
|
---|
666 |
|
---|
667 |
|
---|
668 |
|
---|
669 | /** A checksum list entry.
|
---|
670 | * We keep a list checksums (of precompiler output) that matches, The planned
|
---|
671 | * matching algorithm doesn't require the precompiler output to be indentical,
|
---|
672 | * only to produce the same object files.
|
---|
673 | */
|
---|
674 | typedef struct KOCSUM
|
---|
675 | {
|
---|
676 | /** The next checksum. */
|
---|
677 | struct KOCSUM *pNext;
|
---|
678 | /** The crc32 checksum. */
|
---|
679 | uint32_t crc32;
|
---|
680 | /** The MD5 digest. */
|
---|
681 | unsigned char md5[16];
|
---|
682 | /** Valid or not. */
|
---|
683 | unsigned fUsed;
|
---|
684 | } KOCSUM;
|
---|
685 | /** Pointer to a KOCSUM. */
|
---|
686 | typedef KOCSUM *PKOCSUM;
|
---|
687 | /** Pointer to a const KOCSUM. */
|
---|
688 | typedef const KOCSUM *PCKOCSUM;
|
---|
689 |
|
---|
690 |
|
---|
691 | /**
|
---|
692 | * Temporary context record used when calculating
|
---|
693 | * the checksum of some data.
|
---|
694 | */
|
---|
695 | typedef struct KOCSUMCTX
|
---|
696 | {
|
---|
697 | /** The MD5 context. */
|
---|
698 | struct MD5Context MD5Ctx;
|
---|
699 | } KOCSUMCTX;
|
---|
700 | /** Pointer to a check context record. */
|
---|
701 | typedef KOCSUMCTX *PKOCSUMCTX;
|
---|
702 |
|
---|
703 |
|
---|
704 |
|
---|
705 | /**
|
---|
706 | * Initializes a checksum object with an associated context.
|
---|
707 | *
|
---|
708 | * @param pSum The checksum object.
|
---|
709 | * @param pCtx The checksum context.
|
---|
710 | */
|
---|
711 | static void kOCSumInitWithCtx(PKOCSUM pSum, PKOCSUMCTX pCtx)
|
---|
712 | {
|
---|
713 | memset(pSum, 0, sizeof(*pSum));
|
---|
714 | MD5Init(&pCtx->MD5Ctx);
|
---|
715 | }
|
---|
716 |
|
---|
717 |
|
---|
718 | /**
|
---|
719 | * Updates the checksum calculation.
|
---|
720 | *
|
---|
721 | * @param pSum The checksum.
|
---|
722 | * @param pCtx The checksum calcuation context.
|
---|
723 | * @param pvBuf The input data to checksum.
|
---|
724 | * @param cbBuf The size of the input data.
|
---|
725 | */
|
---|
726 | static void kOCSumUpdate(PKOCSUM pSum, PKOCSUMCTX pCtx, const void *pvBuf, size_t cbBuf)
|
---|
727 | {
|
---|
728 | /*
|
---|
729 | * Take in relativly small chunks to try keep it in the cache.
|
---|
730 | */
|
---|
731 | const unsigned char *pb = (const unsigned char *)pvBuf;
|
---|
732 | while (cbBuf > 0)
|
---|
733 | {
|
---|
734 | size_t cb = cbBuf >= 128*1024 ? 128*1024 : cbBuf;
|
---|
735 | pSum->crc32 = crc32(pSum->crc32, pb, cb);
|
---|
736 | MD5Update(&pCtx->MD5Ctx, pb, cb);
|
---|
737 | cbBuf -= cb;
|
---|
738 | }
|
---|
739 | }
|
---|
740 |
|
---|
741 |
|
---|
742 | /**
|
---|
743 | * Finalizes a checksum calculation.
|
---|
744 | *
|
---|
745 | * @param pSum The checksum.
|
---|
746 | * @param pCtx The checksum calcuation context.
|
---|
747 | */
|
---|
748 | static void kOCSumFinalize(PKOCSUM pSum, PKOCSUMCTX pCtx)
|
---|
749 | {
|
---|
750 | MD5Final(&pSum->md5[0], &pCtx->MD5Ctx);
|
---|
751 | pSum->fUsed = 1;
|
---|
752 | }
|
---|
753 |
|
---|
754 |
|
---|
755 | /**
|
---|
756 | * Init a check sum chain head.
|
---|
757 | *
|
---|
758 | * @param pSumHead The checksum head to init.
|
---|
759 | */
|
---|
760 | static void kOCSumInit(PKOCSUM pSumHead)
|
---|
761 | {
|
---|
762 | memset(pSumHead, 0, sizeof(*pSumHead));
|
---|
763 | }
|
---|
764 |
|
---|
765 |
|
---|
766 | /**
|
---|
767 | * Parses the given string into a checksum head object.
|
---|
768 | *
|
---|
769 | * @returns 0 on success, -1 on format error.
|
---|
770 | * @param pSumHead The checksum head to init.
|
---|
771 | * @param pszVal The string to initialized it from.
|
---|
772 | */
|
---|
773 | static int kOCSumInitFromString(PKOCSUM pSumHead, const char *pszVal)
|
---|
774 | {
|
---|
775 | unsigned i;
|
---|
776 | char *pszNext;
|
---|
777 | char *pszMD5;
|
---|
778 |
|
---|
779 | memset(pSumHead, 0, sizeof(*pSumHead));
|
---|
780 |
|
---|
781 | pszMD5 = strchr(pszVal, ':');
|
---|
782 | if (pszMD5 == NULL)
|
---|
783 | return -1;
|
---|
784 | *pszMD5++ = '\0';
|
---|
785 |
|
---|
786 | /* crc32 */
|
---|
787 | pSumHead->crc32 = (uint32_t)strtoul(pszVal, &pszNext, 16);
|
---|
788 | if (pszNext && *pszNext)
|
---|
789 | return -1;
|
---|
790 |
|
---|
791 | /* md5 */
|
---|
792 | for (i = 0; i < sizeof(pSumHead->md5) * 2; i++)
|
---|
793 | {
|
---|
794 | unsigned char ch = pszMD5[i];
|
---|
795 | int x;
|
---|
796 | if ((unsigned char)(ch - '0') <= 9)
|
---|
797 | x = ch - '0';
|
---|
798 | else if ((unsigned char)(ch - 'a') <= 5)
|
---|
799 | x = ch - 'a' + 10;
|
---|
800 | else if ((unsigned char)(ch - 'A') <= 5)
|
---|
801 | x = ch - 'A' + 10;
|
---|
802 | else
|
---|
803 | return -1;
|
---|
804 | if (!(i & 1))
|
---|
805 | pSumHead->md5[i >> 1] = x << 4;
|
---|
806 | else
|
---|
807 | pSumHead->md5[i >> 1] |= x;
|
---|
808 | }
|
---|
809 |
|
---|
810 | pSumHead->fUsed = 1;
|
---|
811 | return 0;
|
---|
812 | }
|
---|
813 |
|
---|
814 |
|
---|
815 | /**
|
---|
816 | * Delete a check sum chain.
|
---|
817 | *
|
---|
818 | * @param pSumHead The head of the checksum chain.
|
---|
819 | */
|
---|
820 | static void kOCSumDeleteChain(PKOCSUM pSumHead)
|
---|
821 | {
|
---|
822 | void *pv;
|
---|
823 | while ((pv = pSumHead->pNext))
|
---|
824 | {
|
---|
825 | pSumHead = pSumHead->pNext;
|
---|
826 | free(pv);
|
---|
827 | }
|
---|
828 | memset(pSumHead, 0, sizeof(*pSumHead));
|
---|
829 | }
|
---|
830 |
|
---|
831 |
|
---|
832 | /**
|
---|
833 | * Insert a check sum into the chain.
|
---|
834 | *
|
---|
835 | * @param pSumHead The head of the checksum list.
|
---|
836 | * @param pSumAdd The checksum to add (duplicate).
|
---|
837 | */
|
---|
838 | static void kOCSumAdd(PKOCSUM pSumHead, PCKOCSUM pSumAdd)
|
---|
839 | {
|
---|
840 | if (pSumHead->fUsed)
|
---|
841 | {
|
---|
842 | PKOCSUM pNew = xmalloc(sizeof(*pNew));
|
---|
843 | *pNew = *pSumAdd;
|
---|
844 | pNew->pNext = pSumHead->pNext;
|
---|
845 | pNew->fUsed = 1;
|
---|
846 | pSumHead->pNext = pNew;
|
---|
847 | }
|
---|
848 | else
|
---|
849 | {
|
---|
850 | *pSumHead = *pSumAdd;
|
---|
851 | pSumHead->pNext = NULL;
|
---|
852 | pSumHead->fUsed = 1;
|
---|
853 | }
|
---|
854 | }
|
---|
855 |
|
---|
856 |
|
---|
857 | /**
|
---|
858 | * Inserts an entrie chain into the given check sum chain.
|
---|
859 | *
|
---|
860 | * @param pSumHead The head of the checksum list.
|
---|
861 | * @param pSumHeadAdd The head of the checksum list to be added.
|
---|
862 | */
|
---|
863 | static void kOCSumAddChain(PKOCSUM pSumHead, PCKOCSUM pSumHeadAdd)
|
---|
864 | {
|
---|
865 | while (pSumHeadAdd)
|
---|
866 | {
|
---|
867 | kOCSumAdd(pSumHead, pSumHeadAdd);
|
---|
868 | pSumHeadAdd = pSumHeadAdd->pNext;
|
---|
869 | }
|
---|
870 | }
|
---|
871 |
|
---|
872 |
|
---|
873 |
|
---|
874 | /**
|
---|
875 | * Prints the checksum to the specified stream.
|
---|
876 | *
|
---|
877 | * @param pSum The checksum.
|
---|
878 | * @param pFile The output file stream
|
---|
879 | */
|
---|
880 | static void kOCSumFPrintf(PCKOCSUM pSum, FILE *pFile)
|
---|
881 | {
|
---|
882 | fprintf(pFile, "%#x:%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x\n",
|
---|
883 | pSum->crc32,
|
---|
884 | pSum->md5[0], pSum->md5[1], pSum->md5[2], pSum->md5[3],
|
---|
885 | pSum->md5[4], pSum->md5[5], pSum->md5[6], pSum->md5[7],
|
---|
886 | pSum->md5[8], pSum->md5[9], pSum->md5[10], pSum->md5[11],
|
---|
887 | pSum->md5[12], pSum->md5[13], pSum->md5[14], pSum->md5[15]);
|
---|
888 | }
|
---|
889 |
|
---|
890 |
|
---|
891 | /**
|
---|
892 | * Displays the checksum (not chain!) using the InfoMsg() method.
|
---|
893 | *
|
---|
894 | * @param pSum The checksum.
|
---|
895 | * @param uLevel The info message level.
|
---|
896 | * @param pszMsg Message to prefix the info message with.
|
---|
897 | */
|
---|
898 | static void kOCSumInfo(PCKOCSUM pSum, unsigned uLevel, const char *pszMsg)
|
---|
899 | {
|
---|
900 | InfoMsg(uLevel,
|
---|
901 | "%s: crc32=%#010x md5=%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x\n",
|
---|
902 | pszMsg,
|
---|
903 | pSum->crc32,
|
---|
904 | pSum->md5[0], pSum->md5[1], pSum->md5[2], pSum->md5[3],
|
---|
905 | pSum->md5[4], pSum->md5[5], pSum->md5[6], pSum->md5[7],
|
---|
906 | pSum->md5[8], pSum->md5[9], pSum->md5[10], pSum->md5[11],
|
---|
907 | pSum->md5[12], pSum->md5[13], pSum->md5[14], pSum->md5[15]);
|
---|
908 | }
|
---|
909 |
|
---|
910 |
|
---|
911 | /**
|
---|
912 | * Compares two check sum entries.
|
---|
913 | *
|
---|
914 | * @returns 1 if equal, 0 if not equal.
|
---|
915 | *
|
---|
916 | * @param pSum1 The first checksum.
|
---|
917 | * @param pSum2 The second checksum.
|
---|
918 | */
|
---|
919 | static int kOCSumIsEqual(PCKOCSUM pSum1, PCKOCSUM pSum2)
|
---|
920 | {
|
---|
921 | if (pSum1 == pSum2)
|
---|
922 | return 1;
|
---|
923 | if (!pSum1 || !pSum2)
|
---|
924 | return 0;
|
---|
925 | if (pSum1->crc32 != pSum2->crc32)
|
---|
926 | return 0;
|
---|
927 | if (memcmp(&pSum1->md5[0], &pSum2->md5[0], sizeof(pSum1->md5)))
|
---|
928 | return 0;
|
---|
929 | return 1;
|
---|
930 | }
|
---|
931 |
|
---|
932 |
|
---|
933 | /**
|
---|
934 | * Checks if the specified checksum equals one of the
|
---|
935 | * checksums in the chain.
|
---|
936 | *
|
---|
937 | * @returns 1 if equals one of them, 0 if not.
|
---|
938 | *
|
---|
939 | * @param pSumHead The checksum chain too look in.
|
---|
940 | * @param pSum The checksum to look for.
|
---|
941 | * @todo ugly name. fix.
|
---|
942 | */
|
---|
943 | static int kOCSumHasEqualInChain(PCKOCSUM pSumHead, PCKOCSUM pSum)
|
---|
944 | {
|
---|
945 | for (; pSumHead; pSumHead = pSumHead->pNext)
|
---|
946 | {
|
---|
947 | if (pSumHead == pSum)
|
---|
948 | return 1;
|
---|
949 | if (pSumHead->crc32 != pSum->crc32)
|
---|
950 | continue;
|
---|
951 | if (memcmp(&pSumHead->md5[0], &pSum->md5[0], sizeof(pSumHead->md5)))
|
---|
952 | continue;
|
---|
953 | return 1;
|
---|
954 | }
|
---|
955 | return 0;
|
---|
956 | }
|
---|
957 |
|
---|
958 |
|
---|
959 | /**
|
---|
960 | * Checks if the checksum (chain) empty.
|
---|
961 | *
|
---|
962 | * @returns 1 if empty, 0 if it there is one or more checksums.
|
---|
963 | * @param pSum The checksum to test.
|
---|
964 | */
|
---|
965 | static int kOCSumIsEmpty(PCKOCSUM pSum)
|
---|
966 | {
|
---|
967 | return !pSum->fUsed;
|
---|
968 | }
|
---|
969 |
|
---|
970 |
|
---|
971 |
|
---|
972 |
|
---|
973 |
|
---|
974 |
|
---|
975 | /**
|
---|
976 | * The representation of a cache entry.
|
---|
977 | */
|
---|
978 | typedef struct KOCENTRY
|
---|
979 | {
|
---|
980 | /** The name of the cache entry. */
|
---|
981 | const char *pszName;
|
---|
982 | /** The dir that all other names are relative to. */
|
---|
983 | char *pszDir;
|
---|
984 | /** The absolute path. */
|
---|
985 | char *pszAbsPath;
|
---|
986 | /** Set if the object needs to be (re)compiled. */
|
---|
987 | unsigned fNeedCompiling;
|
---|
988 | /** Whether the precompiler runs in piped mode. If clear it's file
|
---|
989 | * mode (it could be redirected stdout, but that's essentially the
|
---|
990 | * same from our point of view). */
|
---|
991 | unsigned fPipedPreComp;
|
---|
992 | /** Whether the compiler runs in piped mode (precompiler output on stdin). */
|
---|
993 | unsigned fPipedCompile;
|
---|
994 | /** Cache entry key that's used for some quick digest validation. */
|
---|
995 | uint32_t uKey;
|
---|
996 |
|
---|
997 | /** The file data. */
|
---|
998 | struct KOCENTRYDATA
|
---|
999 | {
|
---|
1000 | /** The name of file containing the precompiler output. */
|
---|
1001 | char *pszCppName;
|
---|
1002 | /** Pointer to the precompiler output. */
|
---|
1003 | char *pszCppMapping;
|
---|
1004 | /** The size of the precompiler output. 0 if not determined. */
|
---|
1005 | size_t cbCpp;
|
---|
1006 | /** The precompiler output checksums that will produce the cached object. */
|
---|
1007 | KOCSUM SumHead;
|
---|
1008 | /** The object filename (relative to the cache file). */
|
---|
1009 | char *pszObjName;
|
---|
1010 | /** The compile argument vector used to build the object. */
|
---|
1011 | char **papszArgvCompile;
|
---|
1012 | /** The size of the compile */
|
---|
1013 | unsigned cArgvCompile;
|
---|
1014 | /** The checksum of the compiler argument vector. */
|
---|
1015 | KOCSUM SumCompArgv;
|
---|
1016 | /** The target os/arch identifier. */
|
---|
1017 | char *pszTarget;
|
---|
1018 | }
|
---|
1019 | /** The old data.*/
|
---|
1020 | Old,
|
---|
1021 | /** The new data. */
|
---|
1022 | New;
|
---|
1023 | } KOCENTRY;
|
---|
1024 | /** Pointer to a KOCENTRY. */
|
---|
1025 | typedef KOCENTRY *PKOCENTRY;
|
---|
1026 | /** Pointer to a const KOCENTRY. */
|
---|
1027 | typedef const KOCENTRY *PCKOCENTRY;
|
---|
1028 |
|
---|
1029 |
|
---|
1030 | /**
|
---|
1031 | * Creates a cache entry for the given cache file name.
|
---|
1032 | *
|
---|
1033 | * @returns Pointer to a cache entry.
|
---|
1034 | * @param pszFilename The cache file name.
|
---|
1035 | */
|
---|
1036 | static PKOCENTRY kOCEntryCreate(const char *pszFilename)
|
---|
1037 | {
|
---|
1038 | PKOCENTRY pEntry;
|
---|
1039 | size_t off;
|
---|
1040 |
|
---|
1041 | /*
|
---|
1042 | * Allocate an empty entry.
|
---|
1043 | */
|
---|
1044 | pEntry = xmallocz(sizeof(*pEntry));
|
---|
1045 |
|
---|
1046 | kOCSumInit(&pEntry->New.SumHead);
|
---|
1047 | kOCSumInit(&pEntry->Old.SumHead);
|
---|
1048 |
|
---|
1049 | kOCSumInit(&pEntry->New.SumCompArgv);
|
---|
1050 | kOCSumInit(&pEntry->Old.SumCompArgv);
|
---|
1051 |
|
---|
1052 | /*
|
---|
1053 | * Setup the directory and cache file name.
|
---|
1054 | */
|
---|
1055 | pEntry->pszAbsPath = AbsPath(pszFilename);
|
---|
1056 | pEntry->pszName = FindFilenameInPath(pEntry->pszAbsPath);
|
---|
1057 | off = pEntry->pszName - pEntry->pszAbsPath;
|
---|
1058 | if (!off)
|
---|
1059 | FatalDie("Failed to find abs path for '%s'!\n", pszFilename);
|
---|
1060 | pEntry->pszDir = xmalloc(off);
|
---|
1061 | memcpy(pEntry->pszDir, pEntry->pszAbsPath, off - 1);
|
---|
1062 | pEntry->pszDir[off - 1] = '\0';
|
---|
1063 |
|
---|
1064 | return pEntry;
|
---|
1065 | }
|
---|
1066 |
|
---|
1067 |
|
---|
1068 | /**
|
---|
1069 | * Destroys the cache entry freeing up all it's resources.
|
---|
1070 | *
|
---|
1071 | * @param pEntry The entry to free.
|
---|
1072 | */
|
---|
1073 | static void kOCEntryDestroy(PKOCENTRY pEntry)
|
---|
1074 | {
|
---|
1075 | free(pEntry->pszDir);
|
---|
1076 | free(pEntry->pszAbsPath);
|
---|
1077 |
|
---|
1078 | kOCSumDeleteChain(&pEntry->New.SumHead);
|
---|
1079 | kOCSumDeleteChain(&pEntry->Old.SumHead);
|
---|
1080 |
|
---|
1081 | kOCSumDeleteChain(&pEntry->New.SumCompArgv);
|
---|
1082 | kOCSumDeleteChain(&pEntry->Old.SumCompArgv);
|
---|
1083 |
|
---|
1084 | free(pEntry->New.pszCppName);
|
---|
1085 | free(pEntry->Old.pszCppName);
|
---|
1086 |
|
---|
1087 | free(pEntry->New.pszCppMapping);
|
---|
1088 | free(pEntry->Old.pszCppMapping);
|
---|
1089 |
|
---|
1090 | free(pEntry->New.pszObjName);
|
---|
1091 | free(pEntry->Old.pszObjName);
|
---|
1092 |
|
---|
1093 | free(pEntry->New.pszTarget);
|
---|
1094 | free(pEntry->Old.pszTarget);
|
---|
1095 |
|
---|
1096 | while (pEntry->New.cArgvCompile > 0)
|
---|
1097 | free(pEntry->New.papszArgvCompile[--pEntry->New.cArgvCompile]);
|
---|
1098 | while (pEntry->Old.cArgvCompile > 0)
|
---|
1099 | free(pEntry->Old.papszArgvCompile[--pEntry->Old.cArgvCompile]);
|
---|
1100 |
|
---|
1101 | free(pEntry->New.papszArgvCompile);
|
---|
1102 | free(pEntry->Old.papszArgvCompile);
|
---|
1103 |
|
---|
1104 | free(pEntry);
|
---|
1105 | }
|
---|
1106 |
|
---|
1107 |
|
---|
1108 | /**
|
---|
1109 | * Calculates the checksum of an compiler argument vector.
|
---|
1110 | *
|
---|
1111 | * @param pEntry The cache entry.
|
---|
1112 | * @param papszArgv The argument vector.
|
---|
1113 | * @param cArgc The number of entries in the vector.
|
---|
1114 | * @param pszIgnorePath Path to ignore when encountered at the end of arguments.
|
---|
1115 | * (Not quite safe for simple file names, but what the heck.)
|
---|
1116 | * @param pSum Where to store the check sum.
|
---|
1117 | */
|
---|
1118 | static void kOCEntryCalcArgvSum(PKOCENTRY pEntry, const char * const *papszArgv, unsigned cArgc,
|
---|
1119 | const char *pszIgnorePath, PKOCSUM pSum)
|
---|
1120 | {
|
---|
1121 | size_t cchIgnorePath = strlen(pszIgnorePath);
|
---|
1122 | KOCSUMCTX Ctx;
|
---|
1123 | unsigned i;
|
---|
1124 |
|
---|
1125 | kOCSumInitWithCtx(pSum, &Ctx);
|
---|
1126 | for (i = 0; i < cArgc; i++)
|
---|
1127 | {
|
---|
1128 | size_t cch = strlen(papszArgv[i]);
|
---|
1129 | if ( cch < cchIgnorePath
|
---|
1130 | || !ArePathsIdentical(papszArgv[i] + cch - cchIgnorePath, pszIgnorePath, cch))
|
---|
1131 | kOCSumUpdate(pSum, &Ctx, papszArgv[i], cch + 1);
|
---|
1132 | }
|
---|
1133 | kOCSumFinalize(pSum, &Ctx);
|
---|
1134 | }
|
---|
1135 |
|
---|
1136 |
|
---|
1137 | /**
|
---|
1138 | * Reads and parses the cache file.
|
---|
1139 | *
|
---|
1140 | * @param pEntry The entry to read it into.
|
---|
1141 | */
|
---|
1142 | static void kOCEntryRead(PKOCENTRY pEntry)
|
---|
1143 | {
|
---|
1144 | FILE *pFile;
|
---|
1145 | pFile = FOpenFileInDir(pEntry->pszName, pEntry->pszDir, "rb");
|
---|
1146 | if (pFile)
|
---|
1147 | {
|
---|
1148 | InfoMsg(4, "reading cache entry...\n");
|
---|
1149 |
|
---|
1150 | /*
|
---|
1151 | * Check the magic.
|
---|
1152 | */
|
---|
1153 | if ( !fgets(g_szLine, sizeof(g_szLine), pFile)
|
---|
1154 | || strcmp(g_szLine, "magic=kObjCacheEntry-v0.1.0\n"))
|
---|
1155 | {
|
---|
1156 | InfoMsg(2, "bad cache file (magic)\n");
|
---|
1157 | pEntry->fNeedCompiling = 1;
|
---|
1158 | }
|
---|
1159 | else
|
---|
1160 | {
|
---|
1161 | /*
|
---|
1162 | * Parse the rest of the file (relaxed order).
|
---|
1163 | */
|
---|
1164 | unsigned i;
|
---|
1165 | int fBad = 0;
|
---|
1166 | int fBadBeforeMissing = 1;
|
---|
1167 | while (fgets(g_szLine, sizeof(g_szLine), pFile))
|
---|
1168 | {
|
---|
1169 | char *pszNl;
|
---|
1170 | char *pszVal;
|
---|
1171 |
|
---|
1172 | /* Split the line and drop the trailing newline. */
|
---|
1173 | pszVal = strchr(g_szLine, '=');
|
---|
1174 | if ((fBad = pszVal == NULL))
|
---|
1175 | break;
|
---|
1176 | *pszVal++ = '\0';
|
---|
1177 |
|
---|
1178 | pszNl = strchr(pszVal, '\n');
|
---|
1179 | if (pszNl)
|
---|
1180 | *pszNl = '\0';
|
---|
1181 |
|
---|
1182 | /* string case on variable name */
|
---|
1183 | if (!strcmp(g_szLine, "obj"))
|
---|
1184 | {
|
---|
1185 | if ((fBad = pEntry->Old.pszObjName != NULL))
|
---|
1186 | break;
|
---|
1187 | pEntry->Old.pszObjName = xstrdup(pszVal);
|
---|
1188 | }
|
---|
1189 | else if (!strcmp(g_szLine, "cpp"))
|
---|
1190 | {
|
---|
1191 | if ((fBad = pEntry->Old.pszCppName != NULL))
|
---|
1192 | break;
|
---|
1193 | pEntry->Old.pszCppName = xstrdup(pszVal);
|
---|
1194 | }
|
---|
1195 | else if (!strcmp(g_szLine, "cpp-size"))
|
---|
1196 | {
|
---|
1197 | char *pszNext;
|
---|
1198 | if ((fBad = pEntry->Old.cbCpp != 0))
|
---|
1199 | break;
|
---|
1200 | pEntry->Old.cbCpp = strtoul(pszVal, &pszNext, 0);
|
---|
1201 | if ((fBad = pszNext && *pszNext))
|
---|
1202 | break;
|
---|
1203 | }
|
---|
1204 | else if (!strcmp(g_szLine, "cpp-sum"))
|
---|
1205 | {
|
---|
1206 | KOCSUM Sum;
|
---|
1207 | if ((fBad = kOCSumInitFromString(&Sum, pszVal)))
|
---|
1208 | break;
|
---|
1209 | kOCSumAdd(&pEntry->Old.SumHead, &Sum);
|
---|
1210 | }
|
---|
1211 | else if (!strcmp(g_szLine, "cc-argc"))
|
---|
1212 | {
|
---|
1213 | if ((fBad = pEntry->Old.papszArgvCompile != NULL))
|
---|
1214 | break;
|
---|
1215 | pEntry->Old.cArgvCompile = atoi(pszVal); /* if wrong, we'll fail below. */
|
---|
1216 | pEntry->Old.papszArgvCompile = xmallocz((pEntry->Old.cArgvCompile + 1) * sizeof(pEntry->Old.papszArgvCompile[0]));
|
---|
1217 | }
|
---|
1218 | else if (!strncmp(g_szLine, "cc-argv-#", sizeof("cc-argv-#") - 1))
|
---|
1219 | {
|
---|
1220 | char *pszNext;
|
---|
1221 | unsigned i = strtoul(&g_szLine[sizeof("cc-argv-#") - 1], &pszNext, 0);
|
---|
1222 | if ((fBad = i >= pEntry->Old.cArgvCompile || pEntry->Old.papszArgvCompile[i] || (pszNext && *pszNext)))
|
---|
1223 | break;
|
---|
1224 | pEntry->Old.papszArgvCompile[i] = xstrdup(pszVal);
|
---|
1225 | }
|
---|
1226 | else if (!strcmp(g_szLine, "cc-argv-sum"))
|
---|
1227 | {
|
---|
1228 | if ((fBad = !kOCSumIsEmpty(&pEntry->Old.SumCompArgv)))
|
---|
1229 | break;
|
---|
1230 | if ((fBad = kOCSumInitFromString(&pEntry->Old.SumCompArgv, pszVal)))
|
---|
1231 | break;
|
---|
1232 | }
|
---|
1233 | else if (!strcmp(g_szLine, "target"))
|
---|
1234 | {
|
---|
1235 | if ((fBad = pEntry->Old.pszTarget != NULL))
|
---|
1236 | break;
|
---|
1237 | pEntry->Old.pszTarget = xstrdup(pszVal);
|
---|
1238 | }
|
---|
1239 | else if (!strcmp(g_szLine, "key"))
|
---|
1240 | {
|
---|
1241 | char *pszNext;
|
---|
1242 | if ((fBad = pEntry->uKey != 0))
|
---|
1243 | break;
|
---|
1244 | pEntry->uKey = strtoul(pszVal, &pszNext, 0);
|
---|
1245 | if ((fBad = pszNext && *pszNext))
|
---|
1246 | break;
|
---|
1247 | }
|
---|
1248 | else if (!strcmp(g_szLine, "the-end"))
|
---|
1249 | {
|
---|
1250 | fBadBeforeMissing = fBad = strcmp(pszVal, "fine");
|
---|
1251 | break;
|
---|
1252 | }
|
---|
1253 | else
|
---|
1254 | {
|
---|
1255 | fBad = 1;
|
---|
1256 | break;
|
---|
1257 | }
|
---|
1258 | } /* parse loop */
|
---|
1259 |
|
---|
1260 | /*
|
---|
1261 | * Did we find everything and does it add up correctly?
|
---|
1262 | */
|
---|
1263 | if (!fBad && fBadBeforeMissing)
|
---|
1264 | {
|
---|
1265 | InfoMsg(2, "bad cache file (no end)\n");
|
---|
1266 | fBad = 1;
|
---|
1267 | }
|
---|
1268 | else
|
---|
1269 | {
|
---|
1270 | fBadBeforeMissing = fBad;
|
---|
1271 | if ( !fBad
|
---|
1272 | && ( !pEntry->Old.papszArgvCompile
|
---|
1273 | || !pEntry->Old.pszObjName
|
---|
1274 | || !pEntry->Old.pszCppName
|
---|
1275 | || kOCSumIsEmpty(&pEntry->Old.SumHead)))
|
---|
1276 | fBad = 1;
|
---|
1277 | if (!fBad)
|
---|
1278 | for (i = 0; i < pEntry->Old.cArgvCompile; i++)
|
---|
1279 | if ((fBad = !pEntry->Old.papszArgvCompile[i]))
|
---|
1280 | break;
|
---|
1281 | if (!fBad)
|
---|
1282 | {
|
---|
1283 | KOCSUM Sum;
|
---|
1284 | kOCEntryCalcArgvSum(pEntry, (const char * const *)pEntry->Old.papszArgvCompile,
|
---|
1285 | pEntry->Old.cArgvCompile, pEntry->Old.pszObjName, &Sum);
|
---|
1286 | fBad = !kOCSumIsEqual(&pEntry->Old.SumCompArgv, &Sum);
|
---|
1287 | }
|
---|
1288 | if (fBad)
|
---|
1289 | InfoMsg(2, "bad cache file (%s)\n", fBadBeforeMissing ? g_szLine : "missing stuff");
|
---|
1290 | else if (ferror(pFile))
|
---|
1291 | {
|
---|
1292 | InfoMsg(2, "cache file read error\n");
|
---|
1293 | fBad = 1;
|
---|
1294 | }
|
---|
1295 |
|
---|
1296 | /*
|
---|
1297 | * Verify the existance of the object file.
|
---|
1298 | */
|
---|
1299 | if (!fBad)
|
---|
1300 | {
|
---|
1301 | struct stat st;
|
---|
1302 | char *pszPath = MakePathFromDirAndFile(pEntry->Old.pszObjName, pEntry->pszDir);
|
---|
1303 | if (stat(pszPath, &st) != 0)
|
---|
1304 | {
|
---|
1305 | InfoMsg(2, "failed to stat object file: %s\n", strerror(errno));
|
---|
1306 | fBad = 1;
|
---|
1307 | }
|
---|
1308 | else
|
---|
1309 | {
|
---|
1310 | /** @todo verify size and the timestamp. */
|
---|
1311 | }
|
---|
1312 | }
|
---|
1313 | }
|
---|
1314 | pEntry->fNeedCompiling = fBad;
|
---|
1315 | }
|
---|
1316 | fclose(pFile);
|
---|
1317 | }
|
---|
1318 | else
|
---|
1319 | {
|
---|
1320 | InfoMsg(2, "no cache file\n");
|
---|
1321 | pEntry->fNeedCompiling = 1;
|
---|
1322 | }
|
---|
1323 | }
|
---|
1324 |
|
---|
1325 |
|
---|
1326 | /**
|
---|
1327 | * Writes the cache file.
|
---|
1328 | *
|
---|
1329 | * @param pEntry The entry to write.
|
---|
1330 | */
|
---|
1331 | static void kOCEntryWrite(PKOCENTRY pEntry)
|
---|
1332 | {
|
---|
1333 | FILE *pFile;
|
---|
1334 | PCKOCSUM pSum;
|
---|
1335 | unsigned i;
|
---|
1336 |
|
---|
1337 | InfoMsg(4, "writing cache entry '%s'...\n", pEntry->pszName);
|
---|
1338 | pFile = FOpenFileInDir(pEntry->pszName, pEntry->pszDir, "wb");
|
---|
1339 | if (!pFile)
|
---|
1340 | FatalDie("Failed to open '%s' in '%s': %s\n",
|
---|
1341 | pEntry->pszName, pEntry->pszDir, strerror(errno));
|
---|
1342 |
|
---|
1343 | #define CHECK_LEN(expr) \
|
---|
1344 | do { int cch = expr; if (cch >= KOBJCACHE_MAX_LINE_LEN) FatalDie("Line too long: %d (max %d)\nexpr: %s\n", cch, KOBJCACHE_MAX_LINE_LEN, #expr); } while (0)
|
---|
1345 |
|
---|
1346 | fprintf(pFile, "magic=kObjCacheEntry-v0.1.0\n");
|
---|
1347 | CHECK_LEN(fprintf(pFile, "target=%s\n", pEntry->New.pszTarget ? pEntry->New.pszTarget : pEntry->Old.pszTarget));
|
---|
1348 | CHECK_LEN(fprintf(pFile, "key=%u\n", (unsigned long)pEntry->uKey));
|
---|
1349 | CHECK_LEN(fprintf(pFile, "obj=%s\n", pEntry->New.pszObjName ? pEntry->New.pszObjName : pEntry->Old.pszObjName));
|
---|
1350 | CHECK_LEN(fprintf(pFile, "cpp=%s\n", pEntry->New.pszCppName ? pEntry->New.pszCppName : pEntry->Old.pszCppName));
|
---|
1351 | CHECK_LEN(fprintf(pFile, "cpp-size=%lu\n", pEntry->New.pszCppName ? pEntry->New.cbCpp : pEntry->Old.cbCpp));
|
---|
1352 |
|
---|
1353 | if (!kOCSumIsEmpty(&pEntry->New.SumCompArgv))
|
---|
1354 | {
|
---|
1355 | CHECK_LEN(fprintf(pFile, "cc-argc=%u\n", pEntry->New.cArgvCompile));
|
---|
1356 | for (i = 0; i < pEntry->New.cArgvCompile; i++)
|
---|
1357 | CHECK_LEN(fprintf(pFile, "cc-argv-#%u=%s\n", i, pEntry->New.papszArgvCompile[i]));
|
---|
1358 | fprintf(pFile, "cc-argv-sum=");
|
---|
1359 | kOCSumFPrintf(&pEntry->New.SumCompArgv, pFile);
|
---|
1360 | }
|
---|
1361 | else
|
---|
1362 | {
|
---|
1363 | CHECK_LEN(fprintf(pFile, "cc-argc=%u\n", pEntry->Old.cArgvCompile));
|
---|
1364 | for (i = 0; i < pEntry->Old.cArgvCompile; i++)
|
---|
1365 | CHECK_LEN(fprintf(pFile, "cc-argv-#%u=%s\n", i, pEntry->Old.papszArgvCompile[i]));
|
---|
1366 | fprintf(pFile, "cc-argv-sum=");
|
---|
1367 | kOCSumFPrintf(&pEntry->Old.SumCompArgv, pFile);
|
---|
1368 | }
|
---|
1369 |
|
---|
1370 |
|
---|
1371 | for (pSum = !kOCSumIsEmpty(&pEntry->New.SumHead) ? &pEntry->New.SumHead : &pEntry->Old.SumHead;
|
---|
1372 | pSum;
|
---|
1373 | pSum = pSum->pNext)
|
---|
1374 | {
|
---|
1375 | fprintf(pFile, "cpp-sum=");
|
---|
1376 | kOCSumFPrintf(pSum, pFile);
|
---|
1377 | }
|
---|
1378 |
|
---|
1379 | fprintf(pFile, "the-end=fine\n");
|
---|
1380 |
|
---|
1381 | #undef CHECK_LEN
|
---|
1382 |
|
---|
1383 | /*
|
---|
1384 | * Flush the file and check for errors.
|
---|
1385 | * On failure delete the file so we won't be seeing any invalid
|
---|
1386 | * files the next time or upset make with new timestamps.
|
---|
1387 | */
|
---|
1388 | errno = 0;
|
---|
1389 | if ( fflush(pFile) < 0
|
---|
1390 | || ferror(pFile))
|
---|
1391 | {
|
---|
1392 | int iErr = errno;
|
---|
1393 | fclose(pFile);
|
---|
1394 | UnlinkFileInDir(pEntry->pszName, pEntry->pszDir);
|
---|
1395 | FatalDie("Stream error occured while writing '%s' in '%s': %s\n",
|
---|
1396 | pEntry->pszName, pEntry->pszDir, strerror(iErr));
|
---|
1397 | }
|
---|
1398 | fclose(pFile);
|
---|
1399 | }
|
---|
1400 |
|
---|
1401 |
|
---|
1402 | /**
|
---|
1403 | * Checks that the read cache entry is valid.
|
---|
1404 | * It sets fNeedCompiling if it isn't.
|
---|
1405 | *
|
---|
1406 | * @returns 1 valid, 0 invalid.
|
---|
1407 | * @param pEntry The cache entry.
|
---|
1408 | */
|
---|
1409 | static int kOCEntryCheck(PKOCENTRY pEntry)
|
---|
1410 | {
|
---|
1411 | return !pEntry->fNeedCompiling;
|
---|
1412 | }
|
---|
1413 |
|
---|
1414 |
|
---|
1415 | /**
|
---|
1416 | * Sets the object name and compares it with the old name if present.
|
---|
1417 | *
|
---|
1418 | * @param pEntry The cache entry.
|
---|
1419 | * @param pszObjName The new object name.
|
---|
1420 | */
|
---|
1421 | static void kOCEntrySetCompileObjName(PKOCENTRY pEntry, const char *pszObjName)
|
---|
1422 | {
|
---|
1423 | assert(!pEntry->New.pszObjName);
|
---|
1424 | pEntry->New.pszObjName = CalcRelativeName(pszObjName, pEntry->pszDir);
|
---|
1425 |
|
---|
1426 | if ( !pEntry->fNeedCompiling
|
---|
1427 | && ( !pEntry->Old.pszObjName
|
---|
1428 | || strcmp(pEntry->New.pszObjName, pEntry->Old.pszObjName)))
|
---|
1429 | {
|
---|
1430 | InfoMsg(2, "object file name differs\n");
|
---|
1431 | pEntry->fNeedCompiling = 1;
|
---|
1432 | }
|
---|
1433 |
|
---|
1434 | if ( !pEntry->fNeedCompiling
|
---|
1435 | && !DoesFileInDirExist(pEntry->New.pszObjName, pEntry->pszDir))
|
---|
1436 | {
|
---|
1437 | InfoMsg(2, "object file doesn't exist\n");
|
---|
1438 | pEntry->fNeedCompiling = 1;
|
---|
1439 | }
|
---|
1440 | }
|
---|
1441 |
|
---|
1442 |
|
---|
1443 | /**
|
---|
1444 | * Set the new compiler args, calc their checksum, and comparing them with any old ones.
|
---|
1445 | *
|
---|
1446 | * @param pEntry The cache entry.
|
---|
1447 | * @param papszArgvCompile The new argument vector for compilation.
|
---|
1448 | * @param cArgvCompile The number of arguments in the vector.
|
---|
1449 | *
|
---|
1450 | * @remark Must call kOCEntrySetCompileObjName before this function!
|
---|
1451 | */
|
---|
1452 | static void kOCEntrySetCompileArgv(PKOCENTRY pEntry, const char * const *papszArgvCompile, unsigned cArgvCompile)
|
---|
1453 | {
|
---|
1454 | unsigned i;
|
---|
1455 |
|
---|
1456 | /* call me only once! */
|
---|
1457 | assert(!pEntry->New.cArgvCompile);
|
---|
1458 | /* call kOCEntrySetCompilerObjName first! */
|
---|
1459 | assert(pEntry->New.pszObjName);
|
---|
1460 |
|
---|
1461 | /*
|
---|
1462 | * Copy the argument vector and calculate the checksum.
|
---|
1463 | */
|
---|
1464 | pEntry->New.cArgvCompile = cArgvCompile;
|
---|
1465 | pEntry->New.papszArgvCompile = xmalloc((cArgvCompile + 1) * sizeof(pEntry->New.papszArgvCompile[0]));
|
---|
1466 | for (i = 0; i < cArgvCompile; i++)
|
---|
1467 | pEntry->New.papszArgvCompile[i] = xstrdup(papszArgvCompile[i]);
|
---|
1468 | pEntry->New.papszArgvCompile[i] = NULL; /* for exev/spawnv */
|
---|
1469 |
|
---|
1470 | kOCEntryCalcArgvSum(pEntry, papszArgvCompile, cArgvCompile, pEntry->New.pszObjName, &pEntry->New.SumCompArgv);
|
---|
1471 | kOCSumInfo(&pEntry->New.SumCompArgv, 4, "comp-argv");
|
---|
1472 |
|
---|
1473 | /*
|
---|
1474 | * Compare with the old argument vector.
|
---|
1475 | */
|
---|
1476 | if ( !pEntry->fNeedCompiling
|
---|
1477 | && !kOCSumIsEqual(&pEntry->New.SumCompArgv, &pEntry->Old.SumCompArgv))
|
---|
1478 | {
|
---|
1479 | InfoMsg(2, "compiler args differs\n");
|
---|
1480 | pEntry->fNeedCompiling = 1;
|
---|
1481 | }
|
---|
1482 | }
|
---|
1483 |
|
---|
1484 |
|
---|
1485 | /**
|
---|
1486 | * Sets the arch/os target and compares it with the old name if present.
|
---|
1487 | *
|
---|
1488 | * @param pEntry The cache entry.
|
---|
1489 | * @param pszObjName The new object name.
|
---|
1490 | */
|
---|
1491 | static void kOCEntrySetTarget(PKOCENTRY pEntry, const char *pszTarget)
|
---|
1492 | {
|
---|
1493 | assert(!pEntry->New.pszTarget);
|
---|
1494 | pEntry->New.pszTarget = xstrdup(pszTarget);
|
---|
1495 |
|
---|
1496 | if ( !pEntry->fNeedCompiling
|
---|
1497 | && ( !pEntry->Old.pszTarget
|
---|
1498 | || strcmp(pEntry->New.pszTarget, pEntry->Old.pszTarget)))
|
---|
1499 | {
|
---|
1500 | InfoMsg(2, "target differs\n");
|
---|
1501 | pEntry->fNeedCompiling = 1;
|
---|
1502 | }
|
---|
1503 | }
|
---|
1504 |
|
---|
1505 |
|
---|
1506 | /**
|
---|
1507 | * Sets the precompiler output filename.
|
---|
1508 | * We don't generally care if this matches the old name or not.
|
---|
1509 | *
|
---|
1510 | * @param pEntry The cache entry.
|
---|
1511 | * @param pszCppName The precompiler output filename.
|
---|
1512 | */
|
---|
1513 | static void kOCEntrySetCppName(PKOCENTRY pEntry, const char *pszCppName)
|
---|
1514 | {
|
---|
1515 | assert(!pEntry->New.pszCppName);
|
---|
1516 | pEntry->New.pszCppName = CalcRelativeName(pszCppName, pEntry->pszDir);
|
---|
1517 | }
|
---|
1518 |
|
---|
1519 |
|
---|
1520 | /**
|
---|
1521 | * Sets the piped mode of the precompiler and compiler.
|
---|
1522 | *
|
---|
1523 | * @param pEntry The cache entry.
|
---|
1524 | * @param fRedirPreCompStdOut Whether the precompiler is in piped mode.
|
---|
1525 | * @param fRedirCompileStdIn Whether the compiler is in piped mode.
|
---|
1526 | */
|
---|
1527 | static void kOCEntrySetPipedMode(PKOCENTRY pEntry, int fRedirPreCompStdOut, int fRedirCompileStdIn)
|
---|
1528 | {
|
---|
1529 | pEntry->fPipedPreComp = fRedirPreCompStdOut;
|
---|
1530 | pEntry->fPipedCompile = fRedirCompileStdIn;
|
---|
1531 | }
|
---|
1532 |
|
---|
1533 |
|
---|
1534 | /**
|
---|
1535 | * Spawns a child in a synchronous fashion.
|
---|
1536 | * Terminating on failure.
|
---|
1537 | *
|
---|
1538 | * @param papszArgv Argument vector. The cArgv element is NULL.
|
---|
1539 | * @param cArgv The number of arguments in the vector.
|
---|
1540 | */
|
---|
1541 | static void kOCEntrySpawn(PCKOCENTRY pEntry, const char * const *papszArgv, unsigned cArgv, const char *pszMsg, const char *pszStdOut)
|
---|
1542 | {
|
---|
1543 | #if defined(__OS2__) || defined(__WIN__)
|
---|
1544 | intptr_t rc;
|
---|
1545 | int fdStdOut = -1;
|
---|
1546 | if (pszStdOut)
|
---|
1547 | {
|
---|
1548 | int fdReDir;
|
---|
1549 | fdStdOut = dup(STDOUT_FILENO);
|
---|
1550 | close(STDOUT_FILENO);
|
---|
1551 | fdReDir = open(pszStdOut, O_CREAT | O_TRUNC | O_WRONLY, 0666);
|
---|
1552 | if (fdReDir < 0)
|
---|
1553 | FatalDie("%s - failed to create stdout redirection file '%s': %s\n",
|
---|
1554 | pszMsg, pszStdOut, strerror(errno));
|
---|
1555 |
|
---|
1556 | if (fdReDir != STDOUT_FILENO)
|
---|
1557 | {
|
---|
1558 | if (dup2(fdReDir, STDOUT_FILENO) < 0)
|
---|
1559 | FatalDie("%s - dup2 failed: %s\n", pszMsg, strerror(errno));
|
---|
1560 | close(fdReDir);
|
---|
1561 | }
|
---|
1562 | }
|
---|
1563 |
|
---|
1564 | errno = 0;
|
---|
1565 | rc = _spawnvp(_P_WAIT, papszArgv[0], papszArgv);
|
---|
1566 | if (rc < 0)
|
---|
1567 | FatalDie("%s - _spawnvp failed (rc=0x%p): %s\n", pszMsg, rc, strerror(errno));
|
---|
1568 | if (rc > 0)
|
---|
1569 | FatalDie("%s - failed rc=%d\n", pszMsg, (int)rc);
|
---|
1570 | if (fdStdOut)
|
---|
1571 | {
|
---|
1572 | close(STDOUT_FILENO);
|
---|
1573 | fdStdOut = dup2(fdStdOut, STDOUT_FILENO);
|
---|
1574 | close(fdStdOut);
|
---|
1575 | }
|
---|
1576 |
|
---|
1577 | #else
|
---|
1578 | int iStatus;
|
---|
1579 | pid_t pidWait;
|
---|
1580 | pid_t pid = fork();
|
---|
1581 | if (!pid)
|
---|
1582 | {
|
---|
1583 | if (pszStdOut)
|
---|
1584 | {
|
---|
1585 | int fdReDir;
|
---|
1586 |
|
---|
1587 | close(STDOUT_FILENO);
|
---|
1588 | fdReDir = open(pszStdOut, O_CREAT | O_TRUNC | O_WRONLY, 0666);
|
---|
1589 | if (fdReDir < 0)
|
---|
1590 | FatalDie("%s - failed to create stdout redirection file '%s': %s\n",
|
---|
1591 | pszMsg, pszStdOut, strerror(errno));
|
---|
1592 | if (fdReDir != STDOUT_FILENO)
|
---|
1593 | {
|
---|
1594 | if (dup2(fdReDir, STDOUT_FILENO) < 0)
|
---|
1595 | FatalDie("%s - dup2 failed: %s\n", pszMsg, strerror(errno));
|
---|
1596 | close(fdReDir);
|
---|
1597 | }
|
---|
1598 | }
|
---|
1599 |
|
---|
1600 | execvp(papszArgv[0], (char **)papszArgv);
|
---|
1601 | FatalDie("%s - execvp failed: %s\n",
|
---|
1602 | pszMsg, strerror(errno));
|
---|
1603 | }
|
---|
1604 | if (pid == -1)
|
---|
1605 | FatalDie("%s - fork() failed: %s\n", pszMsg, strerror(errno));
|
---|
1606 |
|
---|
1607 | pidWait = waitpid(pid, &iStatus, 0);
|
---|
1608 | while (pidWait < 0 && errno == EINTR)
|
---|
1609 | pidWait = waitpid(pid, &iStatus, 0);
|
---|
1610 | if (pidWait != pid)
|
---|
1611 | FatalDie("%s - waitpid failed rc=%d: %s\n",
|
---|
1612 | pszMsg, pidWait, strerror(errno));
|
---|
1613 | if (!WIFEXITED(iStatus))
|
---|
1614 | FatalDie("%s - abended (iStatus=%#x)\n", pszMsg, iStatus);
|
---|
1615 | if (WEXITSTATUS(iStatus))
|
---|
1616 | FatalDie("%s - failed with rc %d\n", pszMsg, WEXITSTATUS(iStatus));
|
---|
1617 | #endif
|
---|
1618 | (void)cArgv;
|
---|
1619 | }
|
---|
1620 |
|
---|
1621 |
|
---|
1622 | /**
|
---|
1623 | * Spawns child with optional redirection of stdin and stdout.
|
---|
1624 | *
|
---|
1625 | * @param pEntry The cache entry.
|
---|
1626 | * @param papszArgv Argument vector. The cArgv element is NULL.
|
---|
1627 | * @param cArgv The number of arguments in the vector.
|
---|
1628 | * @param fdStdIn Child stdin, -1 if it should inherit our stdin. Will be closed.
|
---|
1629 | * @param fdStdOut Child stdout, -1 if it should inherit our stdout. Will be closed.
|
---|
1630 | * @param pszMsg Message to start the info/error messages with.
|
---|
1631 | */
|
---|
1632 | static pid_t kOCEntrySpawnChild(PCKOCENTRY pEntry, const char * const *papszArgv, unsigned cArgv, int fdStdIn, int fdStdOut, const char *pszMsg)
|
---|
1633 | {
|
---|
1634 | pid_t pid;
|
---|
1635 | int fdSavedStdOut = -1;
|
---|
1636 | int fdSavedStdIn = -1;
|
---|
1637 |
|
---|
1638 | /*
|
---|
1639 | * Setup redirection.
|
---|
1640 | */
|
---|
1641 | if (fdStdOut != -1 && fdStdOut != STDOUT_FILENO)
|
---|
1642 | {
|
---|
1643 | fdSavedStdOut = dup(STDOUT_FILENO);
|
---|
1644 | if (dup2(fdStdOut, STDOUT_FILENO) < 0)
|
---|
1645 | FatalDie("%s - dup2(,1) failed: %s\n", pszMsg, strerror(errno));
|
---|
1646 | close(fdStdOut);
|
---|
1647 | #ifndef __WIN__
|
---|
1648 | fcntl(fdSavedStdOut, F_SETFD, FD_CLOEXEC);
|
---|
1649 | #endif
|
---|
1650 | }
|
---|
1651 | if (fdStdIn != -1 && fdStdIn != STDIN_FILENO)
|
---|
1652 | {
|
---|
1653 | fdSavedStdIn = dup(STDIN_FILENO);
|
---|
1654 | if (dup2(fdStdIn, STDIN_FILENO) < 0)
|
---|
1655 | FatalDie("%s - dup2(,0) failed: %s\n", pszMsg, strerror(errno));
|
---|
1656 | close(fdStdIn);
|
---|
1657 | #ifndef __WIN__
|
---|
1658 | fcntl(fdSavedStdIn, F_SETFD, FD_CLOEXEC);
|
---|
1659 | #endif
|
---|
1660 | }
|
---|
1661 |
|
---|
1662 | /*
|
---|
1663 | * Create the child process.
|
---|
1664 | */
|
---|
1665 | #if defined(__OS2__) || defined(__WIN__)
|
---|
1666 | errno = 0;
|
---|
1667 | pid = _spawnvp(_P_NOWAIT, papszArgv[0], papszArgv);
|
---|
1668 | if (pid == -1)
|
---|
1669 | FatalDie("precompile - _spawnvp failed: %s\n", strerror(errno));
|
---|
1670 |
|
---|
1671 | #else
|
---|
1672 | pid = fork();
|
---|
1673 | if (!pid)
|
---|
1674 | {
|
---|
1675 | execvp(papszArgv[0], (char **)papszArgv);
|
---|
1676 | FatalDie("precompile - execvp failed: %s\n", strerror(errno));
|
---|
1677 | }
|
---|
1678 | if (pid == -1)
|
---|
1679 | FatalDie("precompile - fork() failed: %s\n", strerror(errno));
|
---|
1680 | #endif
|
---|
1681 |
|
---|
1682 | /*
|
---|
1683 | * Restore stdout & stdin.
|
---|
1684 | */
|
---|
1685 | if (fdSavedStdIn != -1)
|
---|
1686 | {
|
---|
1687 | close(STDIN_FILENO);
|
---|
1688 | dup2(fdStdOut, STDIN_FILENO);
|
---|
1689 | close(fdSavedStdIn);
|
---|
1690 | }
|
---|
1691 | if (fdSavedStdOut != -1)
|
---|
1692 | {
|
---|
1693 | close(STDOUT_FILENO);
|
---|
1694 | dup2(fdSavedStdOut, STDOUT_FILENO);
|
---|
1695 | close(fdSavedStdOut);
|
---|
1696 | }
|
---|
1697 |
|
---|
1698 | InfoMsg(3, "%s - spawned %ld\n", pszMsg, (long)pid);
|
---|
1699 | (void)cArgv;
|
---|
1700 | (void)pEntry;
|
---|
1701 | return pid;
|
---|
1702 | }
|
---|
1703 |
|
---|
1704 |
|
---|
1705 | /**
|
---|
1706 | * Waits for a child and exits fatally if the child failed in any way.
|
---|
1707 | *
|
---|
1708 | * @param pEntry The cache entry.
|
---|
1709 | * @param pid The child to wait for.
|
---|
1710 | * @param pszMsg Message to start the info/error messages with.
|
---|
1711 | */
|
---|
1712 | static void kOCEntryWaitChild(PCKOCENTRY pEntry, pid_t pid, const char *pszMsg)
|
---|
1713 | {
|
---|
1714 | int iStatus = -1;
|
---|
1715 | pid_t pidWait;
|
---|
1716 | InfoMsg(3, "%s - wait-child %ld\n", pszMsg, (long)pid);
|
---|
1717 |
|
---|
1718 | #ifdef __WIN__
|
---|
1719 | pidWait = _cwait(&iStatus, pid, _WAIT_CHILD);
|
---|
1720 | if (pidWait == -1)
|
---|
1721 | FatalDie("%s - waitpid failed: %s\n", pszMsg, strerror(errno));
|
---|
1722 | if (iStatus)
|
---|
1723 | FatalDie("%s - failed with rc %d\n", pszMsg, iStatus);
|
---|
1724 | #else
|
---|
1725 | pidWait = waitpid(pid, &iStatus, 0);
|
---|
1726 | while (pidWait < 0 && errno == EINTR)
|
---|
1727 | pidWait = waitpid(pid, &iStatus, 0);
|
---|
1728 | if (pidWait != pid)
|
---|
1729 | FatalDie("%s - waitpid failed rc=%d: %s\n", pidWait, strerror(errno));
|
---|
1730 | if (!WIFEXITED(iStatus))
|
---|
1731 | FatalDie("%s - abended (iStatus=%#x)\n", pszMsg, iStatus);
|
---|
1732 | if (WEXITSTATUS(iStatus))
|
---|
1733 | FatalDie("%s - failed with rc %d\n", pszMsg, WEXITSTATUS(iStatus));
|
---|
1734 | #endif
|
---|
1735 | (void)pEntry;
|
---|
1736 | }
|
---|
1737 |
|
---|
1738 |
|
---|
1739 | /**
|
---|
1740 | * Creates a pipe for setting up redirected stdin/stdout.
|
---|
1741 | *
|
---|
1742 | * @param pEntry The cache entry.
|
---|
1743 | * @param pFDs Where to store the two file descriptors.
|
---|
1744 | * @param pszMsg The operation message for info/error messages.
|
---|
1745 | */
|
---|
1746 | static void kOCEntryCreatePipe(PKOCENTRY pEntry, int *pFDs, const char *pszMsg)
|
---|
1747 | {
|
---|
1748 | pFDs[0] = pFDs[1] = -1;
|
---|
1749 | #if defined(__WIN__)
|
---|
1750 | if (_pipe(pFDs, 0, _O_NOINHERIT | _O_BINARY) < 0)
|
---|
1751 | #else
|
---|
1752 | if (pipe(pFDs) < 0)
|
---|
1753 | #endif
|
---|
1754 | FatalDie("%s - pipe failed: %s\n", pszMsg, strerror(errno));
|
---|
1755 | #if !defined(__WIN__)
|
---|
1756 | fcntl(pFDs[0], F_SETFD, FD_CLOEXEC);
|
---|
1757 | fcntl(pFDs[1], F_SETFD, FD_CLOEXEC);
|
---|
1758 | #endif
|
---|
1759 | }
|
---|
1760 |
|
---|
1761 |
|
---|
1762 | /**
|
---|
1763 | * Spawns a child that produces output to stdout.
|
---|
1764 | *
|
---|
1765 | * @param papszArgv Argument vector. The cArgv element is NULL.
|
---|
1766 | * @param cArgv The number of arguments in the vector.
|
---|
1767 | * @param pszMsg The operation message for info/error messages.
|
---|
1768 | * @param pfnConsumer Pointer to a consumer callback function that is responsible
|
---|
1769 | * for servicing the child output and closing the pipe.
|
---|
1770 | */
|
---|
1771 | static void kOCEntrySpawnProducer(PKOCENTRY pEntry, const char * const *papszArgv, unsigned cArgv, const char *pszMsg,
|
---|
1772 | void (*pfnConsumer)(PKOCENTRY, int))
|
---|
1773 | {
|
---|
1774 | int fds[2];
|
---|
1775 | pid_t pid;
|
---|
1776 |
|
---|
1777 | kOCEntryCreatePipe(pEntry, fds, pszMsg);
|
---|
1778 | pid = kOCEntrySpawnChild(pEntry, papszArgv, cArgv, -1, fds[1 /* write */], pszMsg);
|
---|
1779 |
|
---|
1780 | pfnConsumer(pEntry, fds[0 /* read */]);
|
---|
1781 |
|
---|
1782 | kOCEntryWaitChild(pEntry, pid, pszMsg);
|
---|
1783 | }
|
---|
1784 |
|
---|
1785 |
|
---|
1786 | /**
|
---|
1787 | * Spawns a child that consumes input on stdin.
|
---|
1788 | *
|
---|
1789 | * @param papszArgv Argument vector. The cArgv element is NULL.
|
---|
1790 | * @param cArgv The number of arguments in the vector.
|
---|
1791 | * @param pszMsg The operation message for info/error messages.
|
---|
1792 | * @param pfnProducer Pointer to a producer callback function that is responsible
|
---|
1793 | * for serving the child input and closing the pipe.
|
---|
1794 | */
|
---|
1795 | static void kOCEntrySpawnConsumer(PKOCENTRY pEntry, const char * const *papszArgv, unsigned cArgv, const char *pszMsg,
|
---|
1796 | void (*pfnProducer)(PKOCENTRY, int))
|
---|
1797 | {
|
---|
1798 | int fds[2];
|
---|
1799 | pid_t pid;
|
---|
1800 |
|
---|
1801 | kOCEntryCreatePipe(pEntry, fds, pszMsg);
|
---|
1802 | pid = kOCEntrySpawnChild(pEntry, papszArgv, cArgv, fds[0 /* read */], -1, pszMsg);
|
---|
1803 |
|
---|
1804 | pfnProducer(pEntry, fds[1 /* write */]);
|
---|
1805 |
|
---|
1806 | kOCEntryWaitChild(pEntry, pid, pszMsg);
|
---|
1807 | }
|
---|
1808 |
|
---|
1809 |
|
---|
1810 | /**
|
---|
1811 | * Spawns two child processes, one producing output and one consuming.
|
---|
1812 | * Terminating on failure.
|
---|
1813 | *
|
---|
1814 | * @param papszArgv Argument vector. The cArgv element is NULL.
|
---|
1815 | * @param cArgv The number of arguments in the vector.
|
---|
1816 | * @param pszMsg The operation message for info/error messages.
|
---|
1817 | * @param pfnConsumer Pointer to a consumer callback function that is responsible
|
---|
1818 | * for servicing the child output and closing the pipe.
|
---|
1819 | */
|
---|
1820 | static void kOCEntrySpawnTee(PKOCENTRY pEntry, const char * const *papszProdArgv, unsigned cProdArgv,
|
---|
1821 | const char * const *papszConsArgv, unsigned cConsArgv,
|
---|
1822 | const char *pszMsg, void (*pfnTeeConsumer)(PKOCENTRY, int, int))
|
---|
1823 | {
|
---|
1824 | int fds[2];
|
---|
1825 | int fdIn, fdOut;
|
---|
1826 | pid_t pidProducer, pidConsumer;
|
---|
1827 |
|
---|
1828 | /*
|
---|
1829 | * The producer.
|
---|
1830 | */
|
---|
1831 | kOCEntryCreatePipe(pEntry, fds, pszMsg);
|
---|
1832 | pidConsumer = kOCEntrySpawnChild(pEntry, papszProdArgv, cProdArgv, -1, fds[1 /* write */], pszMsg);
|
---|
1833 | fdIn = fds[0 /* read */];
|
---|
1834 |
|
---|
1835 | /*
|
---|
1836 | * The consumer.
|
---|
1837 | */
|
---|
1838 | kOCEntryCreatePipe(pEntry, fds, pszMsg);
|
---|
1839 | pidProducer = kOCEntrySpawnChild(pEntry, papszConsArgv, cConsArgv, fds[0 /* read */], -1, pszMsg);
|
---|
1840 | fdOut = fds[1 /* write */];
|
---|
1841 |
|
---|
1842 | /*
|
---|
1843 | * Hand it on to the tee consumer.
|
---|
1844 | */
|
---|
1845 | pfnTeeConsumer(pEntry, fdIn, fdOut);
|
---|
1846 |
|
---|
1847 | /*
|
---|
1848 | * Reap the children.
|
---|
1849 | */
|
---|
1850 | kOCEntryWaitChild(pEntry, pidProducer, pszMsg);
|
---|
1851 | kOCEntryWaitChild(pEntry, pidConsumer, pszMsg);
|
---|
1852 | }
|
---|
1853 |
|
---|
1854 |
|
---|
1855 | /**
|
---|
1856 | * Reads the output from the precompiler.
|
---|
1857 | *
|
---|
1858 | * @param pEntry The cache entry. New.cbCpp and New.pszCppMapping will be updated.
|
---|
1859 | * @param pWhich Specifies what to read (old/new).
|
---|
1860 | * @param fNonFatal Whether failure is fatal or not.
|
---|
1861 | */
|
---|
1862 | static int kOCEntryReadCppOutput(PKOCENTRY pEntry, struct KOCENTRYDATA *pWhich, int fNonFatal)
|
---|
1863 | {
|
---|
1864 | pWhich->pszCppMapping = ReadFileInDir(pWhich->pszCppName, pEntry->pszDir, &pWhich->cbCpp);
|
---|
1865 | if (!pWhich->pszCppMapping)
|
---|
1866 | {
|
---|
1867 | if (!fNonFatal)
|
---|
1868 | FatalDie("failed to open/read '%s' in '%s': %s\n",
|
---|
1869 | pWhich->pszCppName, pEntry->pszDir, strerror(errno));
|
---|
1870 | InfoMsg(2, "failed to open/read '%s' in '%s': %s\n",
|
---|
1871 | pWhich->pszCppName, pEntry->pszDir, strerror(errno));
|
---|
1872 | return -1;
|
---|
1873 | }
|
---|
1874 |
|
---|
1875 | InfoMsg(3, "precompiled file is %lu bytes long\n", (unsigned long)pWhich->cbCpp);
|
---|
1876 | return 0;
|
---|
1877 | }
|
---|
1878 |
|
---|
1879 |
|
---|
1880 | /**
|
---|
1881 | * Worker for kOCEntryPreCompile and calculates the checksum of
|
---|
1882 | * the precompiler output.
|
---|
1883 | *
|
---|
1884 | * @param pEntry The cache entry. NewSum will be updated.
|
---|
1885 | */
|
---|
1886 | static void kOCEntryCalcChecksum(PKOCENTRY pEntry)
|
---|
1887 | {
|
---|
1888 | KOCSUMCTX Ctx;
|
---|
1889 | kOCSumInitWithCtx(&pEntry->New.SumHead, &Ctx);
|
---|
1890 | kOCSumUpdate(&pEntry->New.SumHead, &Ctx, pEntry->New.pszCppMapping, pEntry->New.cbCpp);
|
---|
1891 | kOCSumFinalize(&pEntry->New.SumHead, &Ctx);
|
---|
1892 | kOCSumInfo(&pEntry->New.SumHead, 4, "cpp (file)");
|
---|
1893 | }
|
---|
1894 |
|
---|
1895 |
|
---|
1896 | /**
|
---|
1897 | * This consumes the precompiler output and checksums it.
|
---|
1898 | *
|
---|
1899 | * @param pEntry The cache entry.
|
---|
1900 | * @param fdIn The precompiler output pipe.
|
---|
1901 | * @param fdOut The compiler input pipe, -1 if no compiler.
|
---|
1902 | */
|
---|
1903 | static void kOCEntryPreCompileConsumer(PKOCENTRY pEntry, int fdIn)
|
---|
1904 | {
|
---|
1905 | KOCSUMCTX Ctx;
|
---|
1906 | long cbLeft;
|
---|
1907 | long cbAlloc;
|
---|
1908 | char *psz;
|
---|
1909 |
|
---|
1910 | kOCSumInitWithCtx(&pEntry->New.SumHead, &Ctx);
|
---|
1911 | cbAlloc = pEntry->Old.cbCpp ? (pEntry->Old.cbCpp + 4*1024*1024 + 4096) & ~(4*1024*1024 - 1) : 4*1024*1024;
|
---|
1912 | cbLeft = cbAlloc;
|
---|
1913 | pEntry->New.pszCppMapping = psz = xmalloc(cbAlloc);
|
---|
1914 | for (;;)
|
---|
1915 | {
|
---|
1916 | /*
|
---|
1917 | * Read data from the pipe.
|
---|
1918 | */
|
---|
1919 | long cbRead = read(fdIn, psz, cbLeft - 1);
|
---|
1920 | if (!cbRead)
|
---|
1921 | break;
|
---|
1922 | if (cbRead < 0)
|
---|
1923 | {
|
---|
1924 | if (errno == EINTR)
|
---|
1925 | continue;
|
---|
1926 | FatalDie("precompile - read(%d,,%ld) failed: %s\n",
|
---|
1927 | fdIn, (long)cbLeft, strerror(errno));
|
---|
1928 | }
|
---|
1929 |
|
---|
1930 | /*
|
---|
1931 | * Process the data.
|
---|
1932 | */
|
---|
1933 | psz[cbRead] = '\0';
|
---|
1934 | kOCSumUpdate(&pEntry->New.SumHead, &Ctx, psz, cbRead);
|
---|
1935 |
|
---|
1936 | /*
|
---|
1937 | * Advance.
|
---|
1938 | */
|
---|
1939 | psz += cbRead;
|
---|
1940 | cbLeft -= cbRead;
|
---|
1941 | if (cbLeft <= 1)
|
---|
1942 | {
|
---|
1943 | size_t off = psz - pEntry->New.pszCppMapping;
|
---|
1944 | assert(off == cbAlloc);
|
---|
1945 | cbLeft = 4*1024*1024;
|
---|
1946 | cbAlloc += cbLeft;
|
---|
1947 | pEntry->New.pszCppMapping = xrealloc(pEntry->New.pszCppMapping, cbAlloc);
|
---|
1948 | psz = pEntry->New.pszCppMapping + off;
|
---|
1949 | }
|
---|
1950 | }
|
---|
1951 |
|
---|
1952 | close(fdIn);
|
---|
1953 | pEntry->New.cbCpp = cbAlloc - cbLeft;
|
---|
1954 | kOCSumFinalize(&pEntry->New.SumHead, &Ctx);
|
---|
1955 | kOCSumInfo(&pEntry->New.SumHead, 4, "cpp (pipe)");
|
---|
1956 | }
|
---|
1957 |
|
---|
1958 |
|
---|
1959 |
|
---|
1960 |
|
---|
1961 | /**
|
---|
1962 | * Run the precompiler and calculate the checksum of the output.
|
---|
1963 | *
|
---|
1964 | * @param pEntry The cache entry.
|
---|
1965 | * @param papszArgvPreComp The argument vector for executing precompiler. The cArgvPreComp'th argument must be NULL.
|
---|
1966 | * @param cArgvPreComp The number of arguments.
|
---|
1967 | */
|
---|
1968 | static void kOCEntryPreCompile(PKOCENTRY pEntry, const char * const *papszArgvPreComp, unsigned cArgvPreComp)
|
---|
1969 | {
|
---|
1970 | /*
|
---|
1971 | * If we're executing the precompiler in piped mode, it's relatively simple.
|
---|
1972 | */
|
---|
1973 | if (pEntry->fPipedPreComp)
|
---|
1974 | kOCEntrySpawnProducer(pEntry, papszArgvPreComp, cArgvPreComp, "precompile",
|
---|
1975 | kOCEntryPreCompileConsumer);
|
---|
1976 | else
|
---|
1977 | {
|
---|
1978 | /*
|
---|
1979 | * Rename the old precompiled output to '-old' so the precompiler won't
|
---|
1980 | * overwrite it when we execute it.
|
---|
1981 | */
|
---|
1982 | if ( pEntry->Old.pszCppName
|
---|
1983 | && DoesFileInDirExist(pEntry->Old.pszCppName, pEntry->pszDir))
|
---|
1984 | {
|
---|
1985 | size_t cch = strlen(pEntry->Old.pszCppName);
|
---|
1986 | char *psz = xmalloc(cch + sizeof("-old"));
|
---|
1987 | memcpy(psz, pEntry->Old.pszCppName, cch);
|
---|
1988 | memcpy(psz + cch, "-old", sizeof("-old"));
|
---|
1989 |
|
---|
1990 | InfoMsg(3, "renaming '%s' to '%s' in '%s'\n", pEntry->Old.pszCppName, psz, pEntry->pszDir);
|
---|
1991 | UnlinkFileInDir(psz, pEntry->pszDir);
|
---|
1992 | if (RenameFileInDir(pEntry->Old.pszCppName, psz, pEntry->pszDir))
|
---|
1993 | FatalDie("failed to rename '%s' -> '%s' in '%s': %s\n",
|
---|
1994 | pEntry->Old.pszCppName, psz, pEntry->pszDir, strerror(errno));
|
---|
1995 | free(pEntry->Old.pszCppName);
|
---|
1996 | pEntry->Old.pszCppName = psz;
|
---|
1997 | }
|
---|
1998 |
|
---|
1999 | /*
|
---|
2000 | * Precompile it and calculate the checksum on the output.
|
---|
2001 | */
|
---|
2002 | InfoMsg(3, "precompiling -> '%s'...\n", pEntry->New.pszCppName);
|
---|
2003 | kOCEntrySpawn(pEntry, papszArgvPreComp, cArgvPreComp, "precompile", NULL);
|
---|
2004 | kOCEntryReadCppOutput(pEntry, &pEntry->New, 0 /* fatal */);
|
---|
2005 | kOCEntryCalcChecksum(pEntry);
|
---|
2006 | }
|
---|
2007 | }
|
---|
2008 |
|
---|
2009 |
|
---|
2010 | /**
|
---|
2011 | * Worker function for kOCEntryTeeConsumer and kOCEntryCompileIt that
|
---|
2012 | * writes the precompiler output to disk.
|
---|
2013 | *
|
---|
2014 | * @param pEntry The cache entry.
|
---|
2015 | * @param fFreeIt Whether we can free it after writing it or not.
|
---|
2016 | */
|
---|
2017 | static void kOCEntryWriteCppOutput(PKOCENTRY pEntry, int fFreeIt)
|
---|
2018 | {
|
---|
2019 | /*
|
---|
2020 | * Remove old files.
|
---|
2021 | */
|
---|
2022 | if (pEntry->Old.pszCppName)
|
---|
2023 | UnlinkFileInDir(pEntry->Old.pszCppName, pEntry->pszDir);
|
---|
2024 | if (pEntry->New.pszCppName)
|
---|
2025 | UnlinkFileInDir(pEntry->New.pszCppName, pEntry->pszDir);
|
---|
2026 |
|
---|
2027 | /*
|
---|
2028 | * Write it to disk if we've got a file name.
|
---|
2029 | */
|
---|
2030 | if (pEntry->New.pszCppName)
|
---|
2031 | {
|
---|
2032 | long cbLeft;
|
---|
2033 | char *psz;
|
---|
2034 | int fd = OpenFileInDir(pEntry->New.pszCppName, pEntry->pszDir,
|
---|
2035 | O_WRONLY | O_CREAT | O_TRUNC | O_BINARY, 0666);
|
---|
2036 | if (fd == -1)
|
---|
2037 | FatalDie("Failed to create '%s' in '%s': %s\n",
|
---|
2038 | pEntry->New.pszCppName, pEntry->pszDir, strerror(errno));
|
---|
2039 | psz = pEntry->New.pszCppMapping;
|
---|
2040 | cbLeft = pEntry->New.cbCpp;
|
---|
2041 | while (cbLeft > 0)
|
---|
2042 | {
|
---|
2043 | long cbWritten = write(fd, psz, cbLeft);
|
---|
2044 | if (cbWritten < 0)
|
---|
2045 | {
|
---|
2046 | int iErr = errno;
|
---|
2047 | if (iErr == EINTR)
|
---|
2048 | continue;
|
---|
2049 | close(fd);
|
---|
2050 | UnlinkFileInDir(pEntry->New.pszCppName, pEntry->pszDir);
|
---|
2051 | FatalDie("error writing '%s' in '%s': %s\n",
|
---|
2052 | pEntry->New.pszCppName, pEntry->pszDir, strerror(iErr));
|
---|
2053 | }
|
---|
2054 |
|
---|
2055 | psz += cbWritten;
|
---|
2056 | cbLeft -= cbWritten;
|
---|
2057 | }
|
---|
2058 | close(fd);
|
---|
2059 | }
|
---|
2060 |
|
---|
2061 | /*
|
---|
2062 | * Free it.
|
---|
2063 | */
|
---|
2064 | if (fFreeIt)
|
---|
2065 | {
|
---|
2066 | free(pEntry->New.pszCppMapping);
|
---|
2067 | pEntry->New.pszCppMapping = NULL;
|
---|
2068 | }
|
---|
2069 | }
|
---|
2070 |
|
---|
2071 |
|
---|
2072 | /**
|
---|
2073 | * kOCEntrySpawnConsumer callback that passes the precompiler
|
---|
2074 | * output to the compiler and writes it to the disk (latter only when necesary).
|
---|
2075 | *
|
---|
2076 | * @param pEntry The cache entry.
|
---|
2077 | * @param fdOut The pipe handle connected to the childs stdin.
|
---|
2078 | */
|
---|
2079 | static void kOCEntryCompileProducer(PKOCENTRY pEntry, int fdOut)
|
---|
2080 | {
|
---|
2081 | const char *psz = pEntry->New.pszCppMapping;
|
---|
2082 | long cbLeft = pEntry->New.cbCpp;
|
---|
2083 | while (cbLeft > 0)
|
---|
2084 | {
|
---|
2085 | long cbWritten = write(fdOut, psz, cbLeft);
|
---|
2086 | if (cbWritten < 0)
|
---|
2087 | {
|
---|
2088 | if (errno == EINTR)
|
---|
2089 | continue;
|
---|
2090 | FatalDie("compile - write(%d,,%ld) failed: %s\n", fdOut, cbLeft, strerror(errno));
|
---|
2091 | }
|
---|
2092 | psz += cbWritten;
|
---|
2093 | cbLeft -= cbWritten;
|
---|
2094 | }
|
---|
2095 | close(fdOut);
|
---|
2096 |
|
---|
2097 | if (pEntry->fPipedPreComp)
|
---|
2098 | kOCEntryWriteCppOutput(pEntry, 1 /* free it */);
|
---|
2099 | }
|
---|
2100 |
|
---|
2101 |
|
---|
2102 | /**
|
---|
2103 | * Does the actual compiling.
|
---|
2104 | *
|
---|
2105 | * @param pEntry The cache entry.
|
---|
2106 | */
|
---|
2107 | static void kOCEntryCompileIt(PKOCENTRY pEntry)
|
---|
2108 | {
|
---|
2109 | /*
|
---|
2110 | * Delete the object files and free old cpp output that's no longer needed.
|
---|
2111 | */
|
---|
2112 | if (pEntry->Old.pszObjName)
|
---|
2113 | UnlinkFileInDir(pEntry->Old.pszObjName, pEntry->pszDir);
|
---|
2114 | UnlinkFileInDir(pEntry->New.pszObjName, pEntry->pszDir);
|
---|
2115 |
|
---|
2116 | free(pEntry->Old.pszCppMapping);
|
---|
2117 | pEntry->Old.pszCppMapping = NULL;
|
---|
2118 | if (!pEntry->fPipedPreComp && !pEntry->fPipedCompile)
|
---|
2119 | {
|
---|
2120 | free(pEntry->New.pszCppMapping);
|
---|
2121 | pEntry->New.pszCppMapping = NULL;
|
---|
2122 | }
|
---|
2123 |
|
---|
2124 | /*
|
---|
2125 | * Do the (re-)compile job.
|
---|
2126 | */
|
---|
2127 | if (pEntry->fPipedCompile)
|
---|
2128 | {
|
---|
2129 | if ( !pEntry->fPipedPreComp
|
---|
2130 | && !pEntry->New.pszCppMapping)
|
---|
2131 | kOCEntryReadCppOutput(pEntry, &pEntry->New, 0 /* fatal */);
|
---|
2132 | InfoMsg(3, "compiling -> '%s'...\n", pEntry->New.pszObjName);
|
---|
2133 | kOCEntrySpawnConsumer(pEntry, (const char * const *)pEntry->New.papszArgvCompile, pEntry->New.cArgvCompile,
|
---|
2134 | "compile", kOCEntryCompileProducer);
|
---|
2135 | }
|
---|
2136 | else
|
---|
2137 | {
|
---|
2138 | if (pEntry->fPipedPreComp)
|
---|
2139 | kOCEntryWriteCppOutput(pEntry, 1 /* free it */);
|
---|
2140 | InfoMsg(3, "compiling -> '%s'...\n", pEntry->New.pszObjName);
|
---|
2141 | kOCEntrySpawn(pEntry, (const char * const *)pEntry->New.papszArgvCompile, pEntry->New.cArgvCompile, "compile", NULL);
|
---|
2142 | }
|
---|
2143 | }
|
---|
2144 |
|
---|
2145 |
|
---|
2146 | /**
|
---|
2147 | * kOCEntrySpawnTee callback that works sort of like 'tee'.
|
---|
2148 | *
|
---|
2149 | * It will calculate the precompiled output checksum and
|
---|
2150 | * write it to disk while the compiler is busy compiling it.
|
---|
2151 | *
|
---|
2152 | * @param pEntry The cache entry.
|
---|
2153 | * @param fdIn The input handle (connected to the precompiler).
|
---|
2154 | * @param fdOut The output handle (connected to the compiler).
|
---|
2155 | */
|
---|
2156 | static void kOCEntryTeeConsumer(PKOCENTRY pEntry, int fdIn, int fdOut)
|
---|
2157 | {
|
---|
2158 | KOCSUMCTX Ctx;
|
---|
2159 | long cbLeft;
|
---|
2160 | long cbAlloc;
|
---|
2161 | char *psz;
|
---|
2162 |
|
---|
2163 | kOCSumInitWithCtx(&pEntry->New.SumHead, &Ctx);
|
---|
2164 | cbAlloc = pEntry->Old.cbCpp ? (pEntry->Old.cbCpp + 4*1024*1024 + 4096) & ~(4*1024*1024 - 1) : 4*1024*1024;
|
---|
2165 | cbLeft = cbAlloc;
|
---|
2166 | pEntry->New.pszCppMapping = psz = xmalloc(cbAlloc);
|
---|
2167 | InfoMsg(3, "precompiler|compile - starting passhtru...\n");
|
---|
2168 | for (;;)
|
---|
2169 | {
|
---|
2170 | /*
|
---|
2171 | * Read data from the pipe.
|
---|
2172 | */
|
---|
2173 | long cbRead = read(fdIn, psz, cbLeft - 1);
|
---|
2174 | if (!cbRead)
|
---|
2175 | break;
|
---|
2176 | if (cbRead < 0)
|
---|
2177 | {
|
---|
2178 | if (errno == EINTR)
|
---|
2179 | continue;
|
---|
2180 | FatalDie("precompile|compile - read(%d,,%ld) failed: %s\n",
|
---|
2181 | fdIn, (long)cbLeft, strerror(errno));
|
---|
2182 | }
|
---|
2183 | InfoMsg(3, "precompiler|compile - read %d\n", cbRead);
|
---|
2184 |
|
---|
2185 | /*
|
---|
2186 | * Process the data.
|
---|
2187 | */
|
---|
2188 | psz[cbRead] = '\0';
|
---|
2189 | kOCSumUpdate(&pEntry->New.SumHead, &Ctx, psz, cbRead);
|
---|
2190 | do
|
---|
2191 | {
|
---|
2192 | long cbWritten = write(fdOut, psz, cbRead);
|
---|
2193 | if (cbWritten < 0)
|
---|
2194 | {
|
---|
2195 | if (errno == EINTR)
|
---|
2196 | continue;
|
---|
2197 | FatalDie("precompile|compile - write(%d,,%ld) failed: %s\n", fdOut, cbRead, strerror(errno));
|
---|
2198 | }
|
---|
2199 | psz += cbWritten;
|
---|
2200 | cbRead -= cbWritten;
|
---|
2201 | cbLeft -= cbWritten;
|
---|
2202 | } while (cbRead > 0);
|
---|
2203 |
|
---|
2204 | /*
|
---|
2205 | * Expand the buffer?
|
---|
2206 | */
|
---|
2207 | if (cbLeft <= 1)
|
---|
2208 | {
|
---|
2209 | size_t off = psz - pEntry->New.pszCppMapping;
|
---|
2210 | cbLeft = 4*1024*1024;
|
---|
2211 | cbAlloc += cbLeft;
|
---|
2212 | pEntry->New.pszCppMapping = xrealloc(pEntry->New.pszCppMapping, cbAlloc);
|
---|
2213 | psz = pEntry->New.pszCppMapping + off;
|
---|
2214 | }
|
---|
2215 | }
|
---|
2216 | InfoMsg(3, "precompiler|compile - done passhtru\n");
|
---|
2217 |
|
---|
2218 | close(fdIn);
|
---|
2219 | close(fdOut);
|
---|
2220 | pEntry->New.cbCpp = cbAlloc - cbLeft;
|
---|
2221 | kOCSumFinalize(&pEntry->New.SumHead, &Ctx);
|
---|
2222 | kOCSumInfo(&pEntry->New.SumHead, 4, "cpp (tee)");
|
---|
2223 |
|
---|
2224 | /*
|
---|
2225 | * Write the precompiler output to disk and free the memory it
|
---|
2226 | * occupies while the compiler is busy compiling.
|
---|
2227 | */
|
---|
2228 | kOCEntryWriteCppOutput(pEntry, 1 /* free it */);
|
---|
2229 | }
|
---|
2230 |
|
---|
2231 |
|
---|
2232 | /**
|
---|
2233 | * Performs pre-compile and compile in one go (typical clean build scenario).
|
---|
2234 | *
|
---|
2235 | * @param pEntry The cache entry.
|
---|
2236 | * @param papszArgvPreComp The argument vector for executing precompiler. The cArgvPreComp'th argument must be NULL.
|
---|
2237 | * @param cArgvPreComp The number of arguments.
|
---|
2238 | */
|
---|
2239 | static void kOCEntryPreCompileAndCompile(PKOCENTRY pEntry, const char * const *papszArgvPreComp, unsigned cArgvPreComp)
|
---|
2240 | {
|
---|
2241 | if ( pEntry->fPipedCompile
|
---|
2242 | && pEntry->fPipedPreComp)
|
---|
2243 | {
|
---|
2244 | /*
|
---|
2245 | * Clean up old stuff first.
|
---|
2246 | */
|
---|
2247 | if (pEntry->Old.pszObjName)
|
---|
2248 | UnlinkFileInDir(pEntry->Old.pszObjName, pEntry->pszDir);
|
---|
2249 | if (pEntry->New.pszObjName)
|
---|
2250 | UnlinkFileInDir(pEntry->New.pszObjName, pEntry->pszDir);
|
---|
2251 | if (pEntry->Old.pszCppName)
|
---|
2252 | UnlinkFileInDir(pEntry->Old.pszCppName, pEntry->pszDir);
|
---|
2253 | if (pEntry->New.pszCppName)
|
---|
2254 | UnlinkFileInDir(pEntry->New.pszCppName, pEntry->pszDir);
|
---|
2255 |
|
---|
2256 | /*
|
---|
2257 | * Do the actual compile and write the precompiler output to disk.
|
---|
2258 | */
|
---|
2259 | kOCEntrySpawnTee(pEntry, papszArgvPreComp, cArgvPreComp,
|
---|
2260 | (const char * const *)pEntry->New.papszArgvCompile, pEntry->New.cArgvCompile,
|
---|
2261 | "precompile|compile", kOCEntryTeeConsumer);
|
---|
2262 | }
|
---|
2263 | else
|
---|
2264 | {
|
---|
2265 | kOCEntryPreCompile(pEntry, papszArgvPreComp, cArgvPreComp);
|
---|
2266 | kOCEntryCompileIt(pEntry);
|
---|
2267 | }
|
---|
2268 | }
|
---|
2269 |
|
---|
2270 |
|
---|
2271 | /**
|
---|
2272 | * Check whether the string is a '#line' statement.
|
---|
2273 | *
|
---|
2274 | * @returns 1 if it is, 0 if it isn't.
|
---|
2275 | * @param psz The line to examin.
|
---|
2276 | * @parma piLine Where to store the line number.
|
---|
2277 | * @parma ppszFile Where to store the start of the filename.
|
---|
2278 | */
|
---|
2279 | static int kOCEntryIsLineStatement(const char *psz, unsigned *piLine, const char **ppszFile)
|
---|
2280 | {
|
---|
2281 | unsigned iLine;
|
---|
2282 |
|
---|
2283 | /* Expect a hash. */
|
---|
2284 | if (*psz++ != '#')
|
---|
2285 | return 0;
|
---|
2286 |
|
---|
2287 | /* Skip blanks between '#' and the line / number */
|
---|
2288 | while (*psz == ' ' || *psz == '\t')
|
---|
2289 | psz++;
|
---|
2290 |
|
---|
2291 | /* Skip the 'line' if present. */
|
---|
2292 | if (!strncmp(psz, "line", sizeof("line") - 1))
|
---|
2293 | psz += sizeof("line");
|
---|
2294 |
|
---|
2295 | /* Expect a line number now. */
|
---|
2296 | if ((unsigned char)(*psz - '0') > 9)
|
---|
2297 | return 0;
|
---|
2298 | iLine = 0;
|
---|
2299 | do
|
---|
2300 | {
|
---|
2301 | iLine *= 10;
|
---|
2302 | iLine += (*psz - '0');
|
---|
2303 | psz++;
|
---|
2304 | }
|
---|
2305 | while ((unsigned char)(*psz - '0') <= 9);
|
---|
2306 |
|
---|
2307 | /* Expect one or more space now. */
|
---|
2308 | if (*psz != ' ' && *psz != '\t')
|
---|
2309 | return 0;
|
---|
2310 | do psz++;
|
---|
2311 | while (*psz == ' ' || *psz == '\t');
|
---|
2312 |
|
---|
2313 | /* that's good enough. */
|
---|
2314 | *piLine = iLine;
|
---|
2315 | *ppszFile = psz;
|
---|
2316 | return 1;
|
---|
2317 | }
|
---|
2318 |
|
---|
2319 |
|
---|
2320 | /**
|
---|
2321 | * Scan backwards for the previous #line statement.
|
---|
2322 | *
|
---|
2323 | * @returns The filename in the previous statement.
|
---|
2324 | * @param pszStart Where to start.
|
---|
2325 | * @param pszStop Where to stop. Less than pszStart.
|
---|
2326 | * @param piLine The line number count to adjust.
|
---|
2327 | */
|
---|
2328 | static const char *kOCEntryFindFileStatement(const char *pszStart, const char *pszStop, unsigned *piLine)
|
---|
2329 | {
|
---|
2330 | unsigned iLine = *piLine;
|
---|
2331 | assert(pszStart >= pszStop);
|
---|
2332 | while (pszStart >= pszStop)
|
---|
2333 | {
|
---|
2334 | if (*pszStart == '\n')
|
---|
2335 | iLine++;
|
---|
2336 | else if (*pszStart == '#')
|
---|
2337 | {
|
---|
2338 | unsigned iLineTmp;
|
---|
2339 | const char *pszFile;
|
---|
2340 | const char *psz = pszStart - 1;
|
---|
2341 | while (psz >= pszStop && (*psz == ' ' || *psz =='\t'))
|
---|
2342 | psz--;
|
---|
2343 | if ( (psz < pszStop || *psz == '\n')
|
---|
2344 | && kOCEntryIsLineStatement(pszStart, &iLineTmp, &pszFile))
|
---|
2345 | {
|
---|
2346 | *piLine = iLine + iLineTmp - 1;
|
---|
2347 | return pszFile;
|
---|
2348 | }
|
---|
2349 | }
|
---|
2350 | pszStart--;
|
---|
2351 | }
|
---|
2352 | return NULL;
|
---|
2353 | }
|
---|
2354 |
|
---|
2355 |
|
---|
2356 | /**
|
---|
2357 | * Worker for kOCEntryCompareOldAndNewOutput() that compares the
|
---|
2358 | * precompiled output using a fast but not very good method.
|
---|
2359 | *
|
---|
2360 | * @returns 1 if matching, 0 if not matching.
|
---|
2361 | * @param pEntry The entry containing the names of the files to compare.
|
---|
2362 | * The entry is not updated in any way.
|
---|
2363 | */
|
---|
2364 | static int kOCEntryCompareFast(PCKOCENTRY pEntry)
|
---|
2365 | {
|
---|
2366 | const char * psz1 = pEntry->New.pszCppMapping;
|
---|
2367 | const char * const pszEnd1 = psz1 + pEntry->New.cbCpp;
|
---|
2368 | const char * psz2 = pEntry->Old.pszCppMapping;
|
---|
2369 | const char * const pszEnd2 = psz2 + pEntry->Old.cbCpp;
|
---|
2370 |
|
---|
2371 | assert(*pszEnd1 == '\0');
|
---|
2372 | assert(*pszEnd2 == '\0');
|
---|
2373 |
|
---|
2374 | /*
|
---|
2375 | * Iterate block by block and backtrack when we find a difference.
|
---|
2376 | */
|
---|
2377 | for (;;)
|
---|
2378 | {
|
---|
2379 | size_t cch = pszEnd1 - psz1;
|
---|
2380 | if (cch > (size_t)(pszEnd2 - psz2))
|
---|
2381 | cch = pszEnd2 - psz2;
|
---|
2382 | if (cch > 4096)
|
---|
2383 | cch = 4096;
|
---|
2384 | if ( cch
|
---|
2385 | && !memcmp(psz1, psz2, cch))
|
---|
2386 | {
|
---|
2387 | /* no differences */
|
---|
2388 | psz1 += cch;
|
---|
2389 | psz2 += cch;
|
---|
2390 | }
|
---|
2391 | else
|
---|
2392 | {
|
---|
2393 | /*
|
---|
2394 | * Pinpoint the difference exactly and the try find the start
|
---|
2395 | * of that line. Then skip forward until we find something to
|
---|
2396 | * work on that isn't spaces, #line statements or closing curly
|
---|
2397 | * braces.
|
---|
2398 | *
|
---|
2399 | * The closing curly braces are ignored because they are frequently
|
---|
2400 | * found at the end of header files (__END_DECLS) and the worst
|
---|
2401 | * thing that may happen if it isn't one of these braces we're
|
---|
2402 | * ignoring is that the final line in a function block is a little
|
---|
2403 | * bit off in the debug info.
|
---|
2404 | *
|
---|
2405 | * Since we might be skipping a few new empty headers, it is
|
---|
2406 | * possible that we will omit this header from the dependencies
|
---|
2407 | * when using VCC. This might not be a problem, since it seems
|
---|
2408 | * we'll have to use the precompiler output to generate the deps
|
---|
2409 | * anyway.
|
---|
2410 | */
|
---|
2411 | const char *psz;
|
---|
2412 | const char *pszMismatch1;
|
---|
2413 | const char *pszFile1 = NULL;
|
---|
2414 | unsigned iLine1 = 0;
|
---|
2415 | unsigned cCurlyBraces1 = 0;
|
---|
2416 | const char *pszMismatch2;
|
---|
2417 | const char *pszFile2 = NULL;
|
---|
2418 | unsigned iLine2 = 0;
|
---|
2419 | unsigned cCurlyBraces2 = 0;
|
---|
2420 |
|
---|
2421 | /* locate the difference. */
|
---|
2422 | while (cch >= 512 && !memcmp(psz1, psz2, 512))
|
---|
2423 | psz1 += 512, psz2 += 512, cch -= 512;
|
---|
2424 | while (cch >= 64 && !memcmp(psz1, psz2, 64))
|
---|
2425 | psz1 += 64, psz2 += 64, cch -= 64;
|
---|
2426 | while (*psz1 == *psz2 && cch > 0)
|
---|
2427 | psz1++, psz2++, cch--;
|
---|
2428 |
|
---|
2429 | /* locate the start of that line. */
|
---|
2430 | psz = psz1;
|
---|
2431 | while ( psz > pEntry->New.pszCppMapping
|
---|
2432 | && psz[-1] != '\n')
|
---|
2433 | psz--;
|
---|
2434 | psz2 -= (psz1 - psz);
|
---|
2435 | pszMismatch2 = psz2;
|
---|
2436 | pszMismatch1 = psz1 = psz;
|
---|
2437 |
|
---|
2438 | /* Parse the 1st file line by line. */
|
---|
2439 | while (psz1 < pszEnd1)
|
---|
2440 | {
|
---|
2441 | if (*psz1 == '\n')
|
---|
2442 | {
|
---|
2443 | psz1++;
|
---|
2444 | iLine1++;
|
---|
2445 | }
|
---|
2446 | else
|
---|
2447 | {
|
---|
2448 | psz = psz1;
|
---|
2449 | while (isspace(*psz) && *psz != '\n')
|
---|
2450 | psz++;
|
---|
2451 | if (*psz == '\n')
|
---|
2452 | {
|
---|
2453 | psz1 = psz + 1;
|
---|
2454 | iLine1++;
|
---|
2455 | }
|
---|
2456 | else if (*psz == '#' && kOCEntryIsLineStatement(psz, &iLine1, &pszFile1))
|
---|
2457 | {
|
---|
2458 | psz1 = memchr(psz, '\n', pszEnd1 - psz);
|
---|
2459 | if (!psz1++)
|
---|
2460 | psz1 = pszEnd1;
|
---|
2461 | }
|
---|
2462 | else if (*psz == '}')
|
---|
2463 | {
|
---|
2464 | do psz++;
|
---|
2465 | while (isspace(*psz) && *psz != '\n');
|
---|
2466 | if (*psz == '\n')
|
---|
2467 | iLine1++;
|
---|
2468 | else if (psz != pszEnd1)
|
---|
2469 | break;
|
---|
2470 | cCurlyBraces1++;
|
---|
2471 | psz1 = psz;
|
---|
2472 | }
|
---|
2473 | else if (psz == pszEnd1)
|
---|
2474 | psz1 = psz;
|
---|
2475 | else /* found something that can be compared. */
|
---|
2476 | break;
|
---|
2477 | }
|
---|
2478 | }
|
---|
2479 |
|
---|
2480 | /* Ditto for the 2nd file. */
|
---|
2481 | while (psz2 < pszEnd2)
|
---|
2482 | {
|
---|
2483 | if (*psz2 == '\n')
|
---|
2484 | {
|
---|
2485 | psz2++;
|
---|
2486 | iLine2++;
|
---|
2487 | }
|
---|
2488 | else
|
---|
2489 | {
|
---|
2490 | psz = psz2;
|
---|
2491 | while (isspace(*psz) && *psz != '\n')
|
---|
2492 | psz++;
|
---|
2493 | if (*psz == '\n')
|
---|
2494 | {
|
---|
2495 | psz2 = psz + 1;
|
---|
2496 | iLine2++;
|
---|
2497 | }
|
---|
2498 | else if (*psz == '#' && kOCEntryIsLineStatement(psz, &iLine2, &pszFile2))
|
---|
2499 | {
|
---|
2500 | psz2 = memchr(psz, '\n', pszEnd2 - psz);
|
---|
2501 | if (!psz2++)
|
---|
2502 | psz2 = pszEnd2;
|
---|
2503 | }
|
---|
2504 | else if (*psz == '}')
|
---|
2505 | {
|
---|
2506 | do psz++;
|
---|
2507 | while (isspace(*psz) && *psz != '\n');
|
---|
2508 | if (*psz == '\n')
|
---|
2509 | iLine2++;
|
---|
2510 | else if (psz != pszEnd2)
|
---|
2511 | break;
|
---|
2512 | cCurlyBraces2++;
|
---|
2513 | psz2 = psz;
|
---|
2514 | }
|
---|
2515 | else if (psz == pszEnd2)
|
---|
2516 | psz2 = psz;
|
---|
2517 | else /* found something that can be compared. */
|
---|
2518 | break;
|
---|
2519 | }
|
---|
2520 | }
|
---|
2521 |
|
---|
2522 | /* Match the number of ignored closing curly braces. */
|
---|
2523 | if (cCurlyBraces1 != cCurlyBraces2)
|
---|
2524 | return 0;
|
---|
2525 |
|
---|
2526 | /* Reaching the end of any of them means the return statement can decide. */
|
---|
2527 | if ( psz1 == pszEnd1
|
---|
2528 | || psz2 == pszEnd2)
|
---|
2529 | break;
|
---|
2530 |
|
---|
2531 | /* Match the current line. */
|
---|
2532 | psz = memchr(psz1, '\n', pszEnd1 - psz1);
|
---|
2533 | if (!psz)
|
---|
2534 | psz = pszEnd1;
|
---|
2535 | cch = psz - psz1;
|
---|
2536 | if (psz2 + cch > pszEnd2)
|
---|
2537 | break;
|
---|
2538 | if (memcmp(psz1, psz2, cch))
|
---|
2539 | break;
|
---|
2540 |
|
---|
2541 | /* Check that we're at the same location now. */
|
---|
2542 | if (!pszFile1)
|
---|
2543 | pszFile1 = kOCEntryFindFileStatement(pszMismatch1, pEntry->New.pszCppMapping, &iLine1);
|
---|
2544 | if (!pszFile2)
|
---|
2545 | pszFile2 = kOCEntryFindFileStatement(pszMismatch2, pEntry->Old.pszCppMapping, &iLine2);
|
---|
2546 | if (pszFile1 && pszFile2)
|
---|
2547 | {
|
---|
2548 | if (iLine1 != iLine2)
|
---|
2549 | break;
|
---|
2550 | while (*pszFile1 == *pszFile2 && *pszFile1 != '\n' && *pszFile1)
|
---|
2551 | pszFile1++, pszFile2++;
|
---|
2552 | if (*pszFile1 != *pszFile2)
|
---|
2553 | break;
|
---|
2554 | }
|
---|
2555 | else if (pszFile1 || pszFile2)
|
---|
2556 | {
|
---|
2557 | assert(0); /* this shouldn't happen. */
|
---|
2558 | break;
|
---|
2559 | }
|
---|
2560 |
|
---|
2561 | /* Try align psz1 on 8 or 4 bytes so at least one of the buffers are aligned. */
|
---|
2562 | psz1 += cch;
|
---|
2563 | psz2 += cch;
|
---|
2564 | if (cch >= ((uintptr_t)psz1 & 7))
|
---|
2565 | {
|
---|
2566 | psz2 -= ((uintptr_t)psz1 & 7);
|
---|
2567 | psz1 -= ((uintptr_t)psz1 & 7);
|
---|
2568 | }
|
---|
2569 | else if (cch >= ((uintptr_t)psz1 & 3))
|
---|
2570 | {
|
---|
2571 | psz2 -= ((uintptr_t)psz1 & 3);
|
---|
2572 | psz1 -= ((uintptr_t)psz1 & 3);
|
---|
2573 | }
|
---|
2574 | }
|
---|
2575 | }
|
---|
2576 |
|
---|
2577 | return psz1 == pszEnd1
|
---|
2578 | && psz2 == pszEnd2;
|
---|
2579 | }
|
---|
2580 |
|
---|
2581 |
|
---|
2582 | /**
|
---|
2583 | * Worker for kOCEntryCompileIfNeeded that compares the
|
---|
2584 | * precompiled output.
|
---|
2585 | *
|
---|
2586 | * @returns 1 if matching, 0 if not matching.
|
---|
2587 | * @param pEntry The entry containing the names of the files to compare.
|
---|
2588 | * This will load the old cpp output (changing pszOldCppName and Old.cbCpp).
|
---|
2589 | */
|
---|
2590 | static int kOCEntryCompareOldAndNewOutput(PKOCENTRY pEntry)
|
---|
2591 | {
|
---|
2592 | /*
|
---|
2593 | * I may implement a more sophisticated alternative method later... maybe.
|
---|
2594 | */
|
---|
2595 | if (kOCEntryReadCppOutput(pEntry, &pEntry->Old, 1 /* nonfatal */) == -1)
|
---|
2596 | return 0;
|
---|
2597 | //if ()
|
---|
2598 | // return kOCEntryCompareBest(pEntry);
|
---|
2599 | return kOCEntryCompareFast(pEntry);
|
---|
2600 | }
|
---|
2601 |
|
---|
2602 |
|
---|
2603 | /**
|
---|
2604 | * Check if re-compilation is required.
|
---|
2605 | * This sets the fNeedCompile flag.
|
---|
2606 | *
|
---|
2607 | * @param pEntry The cache entry.
|
---|
2608 | */
|
---|
2609 | static void kOCEntryCalcRecompile(PKOCENTRY pEntry)
|
---|
2610 | {
|
---|
2611 | if (pEntry->fNeedCompiling)
|
---|
2612 | return;
|
---|
2613 |
|
---|
2614 | /*
|
---|
2615 | * Check if the precompiler output differ in any significant way?
|
---|
2616 | */
|
---|
2617 | if (!kOCSumHasEqualInChain(&pEntry->Old.SumHead, &pEntry->New.SumHead))
|
---|
2618 | {
|
---|
2619 | InfoMsg(2, "no checksum match - comparing output\n");
|
---|
2620 | if (!kOCEntryCompareOldAndNewOutput(pEntry))
|
---|
2621 | pEntry->fNeedCompiling = 1;
|
---|
2622 | else
|
---|
2623 | kOCSumAddChain(&pEntry->New.SumHead, &pEntry->Old.SumHead);
|
---|
2624 | }
|
---|
2625 | }
|
---|
2626 |
|
---|
2627 |
|
---|
2628 | /**
|
---|
2629 | * Does this cache entry need compiling or what?
|
---|
2630 | *
|
---|
2631 | * @returns 1 if it does, 0 if it doesn't.
|
---|
2632 | * @param pEntry The cache entry in question.
|
---|
2633 | */
|
---|
2634 | static int kOCEntryNeedsCompiling(PCKOCENTRY pEntry)
|
---|
2635 | {
|
---|
2636 | return pEntry->fNeedCompiling;
|
---|
2637 | }
|
---|
2638 |
|
---|
2639 |
|
---|
2640 | /**
|
---|
2641 | * Worker function for kOCEntryCopy.
|
---|
2642 | *
|
---|
2643 | * @param pEntry The entry we're coping to, which pszTo is relative to.
|
---|
2644 | * @param pszTo The destination.
|
---|
2645 | * @param pszFrom The source. This path will be freed.
|
---|
2646 | */
|
---|
2647 | static void kOCEntryCopyFile(PCKOCENTRY pEntry, const char *pszTo, char *pszSrc)
|
---|
2648 | {
|
---|
2649 | char *pszDst = MakePathFromDirAndFile(pszTo, pEntry->pszDir);
|
---|
2650 | char *pszBuf = xmalloc(256 * 1024);
|
---|
2651 | char *psz;
|
---|
2652 | int fdSrc;
|
---|
2653 | int fdDst;
|
---|
2654 |
|
---|
2655 | /*
|
---|
2656 | * Open the files.
|
---|
2657 | */
|
---|
2658 | fdSrc = open(pszSrc, O_RDONLY | O_BINARY);
|
---|
2659 | if (fdSrc == -1)
|
---|
2660 | FatalDie("failed to open '%s': %s\n", pszSrc, strerror(errno));
|
---|
2661 |
|
---|
2662 | unlink(pszDst);
|
---|
2663 | fdDst = open(pszDst, O_WRONLY | O_CREAT | O_TRUNC | O_BINARY, 0666);
|
---|
2664 | if (fdDst == -1)
|
---|
2665 | FatalDie("failed to create '%s': %s\n", pszDst, strerror(errno));
|
---|
2666 |
|
---|
2667 | /*
|
---|
2668 | * Copy them.
|
---|
2669 | */
|
---|
2670 | for (;;)
|
---|
2671 | {
|
---|
2672 | /* read a chunk. */
|
---|
2673 | long cbRead = read(fdSrc, pszBuf, 256*1024);
|
---|
2674 | if (cbRead < 0)
|
---|
2675 | {
|
---|
2676 | if (errno == EINTR)
|
---|
2677 | continue;
|
---|
2678 | FatalDie("read '%s' failed: %s\n", pszSrc, strerror(errno));
|
---|
2679 | }
|
---|
2680 | if (!cbRead)
|
---|
2681 | break; /* eof */
|
---|
2682 |
|
---|
2683 | /* write the chunk. */
|
---|
2684 | psz = pszBuf;
|
---|
2685 | do
|
---|
2686 | {
|
---|
2687 | long cbWritten = write(fdDst, psz, cbRead);
|
---|
2688 | if (cbWritten < 0)
|
---|
2689 | {
|
---|
2690 | if (errno == EINTR)
|
---|
2691 | continue;
|
---|
2692 | FatalDie("write '%s' failed: %s\n", pszSrc, strerror(errno));
|
---|
2693 | }
|
---|
2694 | psz += cbWritten;
|
---|
2695 | cbRead -= cbWritten;
|
---|
2696 | } while (cbRead > 0);
|
---|
2697 | }
|
---|
2698 |
|
---|
2699 | /* cleanup */
|
---|
2700 | if (close(fdDst) != 0)
|
---|
2701 | FatalDie("closing '%s' failed: %s\n", pszDst, strerror(errno));
|
---|
2702 | close(fdSrc);
|
---|
2703 | free(pszBuf);
|
---|
2704 | free(pszDst);
|
---|
2705 | free(pszSrc);
|
---|
2706 | }
|
---|
2707 |
|
---|
2708 |
|
---|
2709 | /**
|
---|
2710 | * Copies the object (and whatever else) from one cache entry to another.
|
---|
2711 | *
|
---|
2712 | * This is called when a matching cache entry has been found and we don't
|
---|
2713 | * need to recompile anything.
|
---|
2714 | *
|
---|
2715 | * @param pEntry The entry to copy to.
|
---|
2716 | * @param pFrom The entry to copy from.
|
---|
2717 | */
|
---|
2718 | static void kOCEntryCopy(PKOCENTRY pEntry, PCKOCENTRY pFrom)
|
---|
2719 | {
|
---|
2720 | kOCEntryCopyFile(pEntry, pEntry->New.pszObjName,
|
---|
2721 | MakePathFromDirAndFile(pFrom->New.pszObjName
|
---|
2722 | ? pFrom->New.pszObjName : pFrom->Old.pszObjName,
|
---|
2723 | pFrom->pszDir));
|
---|
2724 | }
|
---|
2725 |
|
---|
2726 |
|
---|
2727 | /**
|
---|
2728 | * Gets the absolute path to the cache entry.
|
---|
2729 | *
|
---|
2730 | * @returns absolute path to the cache entry.
|
---|
2731 | * @param pEntry The cache entry in question.
|
---|
2732 | */
|
---|
2733 | static const char *kOCEntryAbsPath(PCKOCENTRY pEntry)
|
---|
2734 | {
|
---|
2735 | return pEntry->pszAbsPath;
|
---|
2736 | }
|
---|
2737 |
|
---|
2738 |
|
---|
2739 |
|
---|
2740 |
|
---|
2741 |
|
---|
2742 |
|
---|
2743 | /**
|
---|
2744 | * Digest of one cache entry.
|
---|
2745 | *
|
---|
2746 | * This contains all the information required to find a matching
|
---|
2747 | * cache entry without having to open each of the files.
|
---|
2748 | */
|
---|
2749 | typedef struct KOCDIGEST
|
---|
2750 | {
|
---|
2751 | /** The relative path to the entry. Optional if pszAbsPath is set. */
|
---|
2752 | char *pszRelPath;
|
---|
2753 | /** The absolute path to the entry. Optional if pszRelPath is set. */
|
---|
2754 | char *pszAbsPath;
|
---|
2755 | /** The target os/arch identifier. */
|
---|
2756 | char *pszTarget;
|
---|
2757 | /** A unique number assigned to the entry when it's (re)-inserted
|
---|
2758 | * into the cache. This is used for simple consitency checking. */
|
---|
2759 | uint32_t uKey;
|
---|
2760 | /** The checksum of the compile argument vector. */
|
---|
2761 | KOCSUM SumCompArgv;
|
---|
2762 | /** The list of precompiler output checksums that's . */
|
---|
2763 | KOCSUM SumHead;
|
---|
2764 | } KOCDIGEST;
|
---|
2765 | /** Pointer to a file digest. */
|
---|
2766 | typedef KOCDIGEST *PKOCDIGEST;
|
---|
2767 | /** Pointer to a const file digest. */
|
---|
2768 | typedef KOCDIGEST *PCKOCDIGEST;
|
---|
2769 |
|
---|
2770 |
|
---|
2771 | /**
|
---|
2772 | * Initializes the specified digest.
|
---|
2773 | *
|
---|
2774 | * @param pDigest The digest.
|
---|
2775 | */
|
---|
2776 | static void kOCDigestInit(PKOCDIGEST pDigest)
|
---|
2777 | {
|
---|
2778 | memset(pDigest, 0, sizeof(*pDigest));
|
---|
2779 | kOCSumInit(&pDigest->SumHead);
|
---|
2780 | }
|
---|
2781 |
|
---|
2782 |
|
---|
2783 | /**
|
---|
2784 | * Initializes the digest for the specified entry.
|
---|
2785 | *
|
---|
2786 | * @param pDigest The (uninitialized) digest.
|
---|
2787 | * @param pEntry The entry.
|
---|
2788 | */
|
---|
2789 | static void kOCDigestInitFromEntry(PKOCDIGEST pDigest, PCKOCENTRY pEntry)
|
---|
2790 | {
|
---|
2791 | kOCDigestInit(pDigest);
|
---|
2792 |
|
---|
2793 | pDigest->uKey = pEntry->uKey;
|
---|
2794 | pDigest->pszTarget = xstrdup(pEntry->New.pszTarget ? pEntry->New.pszTarget : pEntry->Old.pszTarget);
|
---|
2795 |
|
---|
2796 | kOCSumInit(&pDigest->SumCompArgv);
|
---|
2797 | if (!kOCSumIsEmpty(&pEntry->New.SumCompArgv))
|
---|
2798 | kOCSumAdd(&pDigest->SumCompArgv, &pEntry->New.SumCompArgv);
|
---|
2799 | else
|
---|
2800 | kOCSumAdd(&pDigest->SumCompArgv, &pEntry->Old.SumCompArgv);
|
---|
2801 |
|
---|
2802 | kOCSumInit(&pDigest->SumHead);
|
---|
2803 | if (!kOCSumIsEmpty(&pEntry->New.SumHead))
|
---|
2804 | kOCSumAddChain(&pDigest->SumHead, &pEntry->New.SumHead);
|
---|
2805 | else
|
---|
2806 | kOCSumAddChain(&pDigest->SumHead, &pEntry->Old.SumHead);
|
---|
2807 |
|
---|
2808 | /** @todo implement selective relative path support. */
|
---|
2809 | pDigest->pszRelPath = NULL;
|
---|
2810 | pDigest->pszAbsPath = xstrdup(kOCEntryAbsPath(pEntry));
|
---|
2811 | }
|
---|
2812 |
|
---|
2813 |
|
---|
2814 | /**
|
---|
2815 | * Purges a digest, freeing all resources and returning
|
---|
2816 | * it to the initial state.
|
---|
2817 | *
|
---|
2818 | * @param pDigest The digest.
|
---|
2819 | */
|
---|
2820 | static void kOCDigestPurge(PKOCDIGEST pDigest)
|
---|
2821 | {
|
---|
2822 | free(pDigest->pszRelPath);
|
---|
2823 | free(pDigest->pszAbsPath);
|
---|
2824 | free(pDigest->pszTarget);
|
---|
2825 | pDigest->pszTarget = pDigest->pszAbsPath = pDigest->pszRelPath = NULL;
|
---|
2826 | pDigest->uKey = 0;
|
---|
2827 | kOCSumDeleteChain(&pDigest->SumCompArgv);
|
---|
2828 | kOCSumDeleteChain(&pDigest->SumHead);
|
---|
2829 | }
|
---|
2830 |
|
---|
2831 |
|
---|
2832 | /**
|
---|
2833 | * Returns the absolute path to the entry, calculating
|
---|
2834 | * the path if necessary.
|
---|
2835 | *
|
---|
2836 | * @returns absolute path.
|
---|
2837 | * @param pDigest The digest.
|
---|
2838 | * @param pszDir The cache directory that it might be relative to.
|
---|
2839 | */
|
---|
2840 | static const char *kOCDigestAbsPath(PCKOCDIGEST pDigest, const char *pszDir)
|
---|
2841 | {
|
---|
2842 | if (!pDigest->pszAbsPath)
|
---|
2843 | {
|
---|
2844 | char *pszPath = MakePathFromDirAndFile(pDigest->pszRelPath, pszDir);
|
---|
2845 | ((PKOCDIGEST)pDigest)->pszAbsPath = AbsPath(pszPath);
|
---|
2846 | free(pszPath);
|
---|
2847 | }
|
---|
2848 | return pDigest->pszAbsPath;
|
---|
2849 | }
|
---|
2850 |
|
---|
2851 |
|
---|
2852 | /**
|
---|
2853 | * Checks that the digest matches the
|
---|
2854 | *
|
---|
2855 | * @returns 1 if valid, 0 if invalid in some way.
|
---|
2856 | *
|
---|
2857 | * @param pDigest The digest to validate.
|
---|
2858 | * @param pEntry What to validate it against.
|
---|
2859 | */
|
---|
2860 | static int kOCDigestIsValid(PCKOCDIGEST pDigest, PCKOCENTRY pEntry)
|
---|
2861 | {
|
---|
2862 | PCKOCSUM pSum;
|
---|
2863 | PCKOCSUM pSumEntry;
|
---|
2864 |
|
---|
2865 | if (pDigest->uKey != pEntry->uKey)
|
---|
2866 | return 0;
|
---|
2867 |
|
---|
2868 | if (!kOCSumIsEqual(&pDigest->SumCompArgv,
|
---|
2869 | kOCSumIsEmpty(&pEntry->New.SumCompArgv)
|
---|
2870 | ? &pEntry->Old.SumCompArgv : &pEntry->New.SumCompArgv))
|
---|
2871 | return 0;
|
---|
2872 |
|
---|
2873 | if (strcmp(pDigest->pszTarget, pEntry->New.pszTarget ? pEntry->New.pszTarget : pEntry->Old.pszTarget))
|
---|
2874 | return 0;
|
---|
2875 |
|
---|
2876 | /* match the checksums */
|
---|
2877 | pSumEntry = kOCSumIsEmpty(&pEntry->New.SumHead)
|
---|
2878 | ? &pEntry->Old.SumHead : &pEntry->New.SumHead;
|
---|
2879 | for (pSum = &pDigest->SumHead; pSum; pSum = pSum->pNext)
|
---|
2880 | if (!kOCSumHasEqualInChain(pSumEntry, pSum))
|
---|
2881 | return 0;
|
---|
2882 |
|
---|
2883 | return 1;
|
---|
2884 | }
|
---|
2885 |
|
---|
2886 |
|
---|
2887 |
|
---|
2888 |
|
---|
2889 |
|
---|
2890 | /**
|
---|
2891 | * The structure for the central cache entry.
|
---|
2892 | */
|
---|
2893 | typedef struct KOBJCACHE
|
---|
2894 | {
|
---|
2895 | /** The entry name. */
|
---|
2896 | const char *pszName;
|
---|
2897 | /** The dir that relative names in the digest are relative to. */
|
---|
2898 | char *pszDir;
|
---|
2899 | /** The absolute path. */
|
---|
2900 | char *pszAbsPath;
|
---|
2901 |
|
---|
2902 | /** The cache file descriptor. */
|
---|
2903 | int fd;
|
---|
2904 | /** The stream associated with fd. */
|
---|
2905 | FILE *pFile;
|
---|
2906 | /** Whether it's currently locked or not. */
|
---|
2907 | unsigned fLocked;
|
---|
2908 | /** Whether the cache file is dirty and needs writing back. */
|
---|
2909 | unsigned fDirty;
|
---|
2910 | /** Whether this is a new cache or not. */
|
---|
2911 | unsigned fNewCache;
|
---|
2912 |
|
---|
2913 | /** The cache file generation. */
|
---|
2914 | uint32_t uGeneration;
|
---|
2915 | /** The next valid key. (Determin at load time.) */
|
---|
2916 | uint32_t uNextKey;
|
---|
2917 |
|
---|
2918 | /** Number of digests in paDigests. */
|
---|
2919 | unsigned cDigests;
|
---|
2920 | /** Array of digests for the KOCENTRY objects in the cache. */
|
---|
2921 | PKOCDIGEST paDigests;
|
---|
2922 |
|
---|
2923 | } KOBJCACHE;
|
---|
2924 | /** Pointer to a cache. */
|
---|
2925 | typedef KOBJCACHE *PKOBJCACHE;
|
---|
2926 | /** Pointer to a const cache. */
|
---|
2927 | typedef KOBJCACHE const *PCKOBJCACHE;
|
---|
2928 |
|
---|
2929 |
|
---|
2930 | /**
|
---|
2931 | * Creates an empty cache.
|
---|
2932 | *
|
---|
2933 | * This doesn't touch the file system, it just create the data structure.
|
---|
2934 | *
|
---|
2935 | * @returns Pointer to a cache.
|
---|
2936 | * @param pszCacheFile The cache file.
|
---|
2937 | */
|
---|
2938 | static PKOBJCACHE kObjCacheCreate(const char *pszCacheFile)
|
---|
2939 | {
|
---|
2940 | PKOBJCACHE pCache;
|
---|
2941 | size_t off;
|
---|
2942 |
|
---|
2943 | /*
|
---|
2944 | * Allocate an empty entry.
|
---|
2945 | */
|
---|
2946 | pCache = xmallocz(sizeof(*pCache));
|
---|
2947 | pCache->fd = -1;
|
---|
2948 |
|
---|
2949 | /*
|
---|
2950 | * Setup the directory and cache file name.
|
---|
2951 | */
|
---|
2952 | pCache->pszAbsPath = AbsPath(pszCacheFile);
|
---|
2953 | pCache->pszName = FindFilenameInPath(pCache->pszAbsPath);
|
---|
2954 | off = pCache->pszName - pCache->pszAbsPath;
|
---|
2955 | if (!off)
|
---|
2956 | FatalDie("Failed to find abs path for '%s'!\n", pszCacheFile);
|
---|
2957 | pCache->pszDir = xmalloc(off);
|
---|
2958 | memcpy(pCache->pszDir, pCache->pszAbsPath, off - 1);
|
---|
2959 | pCache->pszDir[off - 1] = '\0';
|
---|
2960 |
|
---|
2961 | return pCache;
|
---|
2962 | }
|
---|
2963 |
|
---|
2964 |
|
---|
2965 | /**
|
---|
2966 | * Destroys the cache - closing any open files, freeing up heap memory and such.
|
---|
2967 | *
|
---|
2968 | * @param pCache The cache.
|
---|
2969 | */
|
---|
2970 | static void kObjCacheDestroy(PKOBJCACHE pCache)
|
---|
2971 | {
|
---|
2972 | if (pCache->pFile)
|
---|
2973 | {
|
---|
2974 | errno = 0;
|
---|
2975 | if (fclose(pCache->pFile) != 0)
|
---|
2976 | FatalMsg("fclose failed: %s\n", strerror(errno));
|
---|
2977 | pCache->pFile = NULL;
|
---|
2978 | pCache->fd = -1;
|
---|
2979 | }
|
---|
2980 | free(pCache->paDigests);
|
---|
2981 | free(pCache->pszAbsPath);
|
---|
2982 | free(pCache->pszDir);
|
---|
2983 | free(pCache);
|
---|
2984 | }
|
---|
2985 |
|
---|
2986 |
|
---|
2987 | /**
|
---|
2988 | * Purges the data in the cache object.
|
---|
2989 | *
|
---|
2990 | * @param pCache The cache object.
|
---|
2991 | */
|
---|
2992 | static void kObjCachePurge(PKOBJCACHE pCache)
|
---|
2993 | {
|
---|
2994 | while (pCache->cDigests > 0)
|
---|
2995 | kOCDigestPurge(&pCache->paDigests[--pCache->cDigests]);
|
---|
2996 | free(pCache->paDigests);
|
---|
2997 | pCache->paDigests = NULL;
|
---|
2998 | pCache->uGeneration = 0;
|
---|
2999 | pCache->uNextKey = 0;
|
---|
3000 | }
|
---|
3001 |
|
---|
3002 |
|
---|
3003 | /**
|
---|
3004 | * (Re-)reads the file.
|
---|
3005 | *
|
---|
3006 | * @param pCache The cache to (re)-read.
|
---|
3007 | */
|
---|
3008 | static void kObjCacheRead(PKOBJCACHE pCache)
|
---|
3009 | {
|
---|
3010 | unsigned i;
|
---|
3011 | char szBuf[8192];
|
---|
3012 | int fBad = 0;
|
---|
3013 |
|
---|
3014 | InfoMsg(4, "reading cache file...\n");
|
---|
3015 |
|
---|
3016 | /*
|
---|
3017 | * Rewind the file & stream, and associate a temporary buffer
|
---|
3018 | * with the stream to speed up reading.
|
---|
3019 | */
|
---|
3020 | if (lseek(pCache->fd, 0, SEEK_SET) == -1)
|
---|
3021 | FatalDie("lseek(cache-fd) failed: %s\n", strerror(errno));
|
---|
3022 | rewind(pCache->pFile);
|
---|
3023 | if (setvbuf(pCache->pFile, szBuf, _IOFBF, sizeof(szBuf)) != 0)
|
---|
3024 | FatalDie("fdopen(cache-fd,rb) failed: %s\n", strerror(errno));
|
---|
3025 |
|
---|
3026 | /*
|
---|
3027 | * Read magic and generation.
|
---|
3028 | */
|
---|
3029 | if ( !fgets(g_szLine, sizeof(g_szLine), pCache->pFile)
|
---|
3030 | || strcmp(g_szLine, "magic=kObjCache-v0.1.0\n"))
|
---|
3031 | {
|
---|
3032 | InfoMsg(2, "bad cache file (magic)\n");
|
---|
3033 | fBad = 1;
|
---|
3034 | }
|
---|
3035 | else if ( !fgets(g_szLine, sizeof(g_szLine), pCache->pFile)
|
---|
3036 | || strncmp(g_szLine, "generation=", sizeof("generation=") - 1))
|
---|
3037 | {
|
---|
3038 | InfoMsg(2, "bad cache file (generation)\n");
|
---|
3039 | fBad = 1;
|
---|
3040 | }
|
---|
3041 | else if ( pCache->uGeneration
|
---|
3042 | && pCache->uGeneration == atol(&g_szLine[sizeof("generation=") - 1]))
|
---|
3043 | {
|
---|
3044 | InfoMsg(3, "drop re-read unmodified cache file\n");
|
---|
3045 | fBad = 0;
|
---|
3046 | }
|
---|
3047 | else
|
---|
3048 | {
|
---|
3049 | int fBadBeforeMissing;
|
---|
3050 |
|
---|
3051 | /*
|
---|
3052 | * Read everything (anew).
|
---|
3053 | */
|
---|
3054 | kObjCachePurge(pCache);
|
---|
3055 | do
|
---|
3056 | {
|
---|
3057 | PKOCDIGEST pDigest;
|
---|
3058 | char *pszNl;
|
---|
3059 | char *pszVal;
|
---|
3060 | char *psz;
|
---|
3061 |
|
---|
3062 | /* Split the line and drop the trailing newline. */
|
---|
3063 | pszVal = strchr(g_szLine, '=');
|
---|
3064 | if ((fBad = pszVal == NULL))
|
---|
3065 | break;
|
---|
3066 | *pszVal++ = '\0';
|
---|
3067 |
|
---|
3068 | pszNl = strchr(pszVal, '\n');
|
---|
3069 | if (pszNl)
|
---|
3070 | *pszNl = '\0';
|
---|
3071 |
|
---|
3072 | /* digest '#'? */
|
---|
3073 | psz = strchr(g_szLine, '#');
|
---|
3074 | if (psz)
|
---|
3075 | {
|
---|
3076 | char *pszNext;
|
---|
3077 | i = strtoul(++psz, &pszNext, 0);
|
---|
3078 | if ((fBad = pszNext && *pszNext))
|
---|
3079 | break;
|
---|
3080 | if ((fBad = i >= pCache->cDigests))
|
---|
3081 | break;
|
---|
3082 | pDigest = &pCache->paDigests[i];
|
---|
3083 | *psz = '\0';
|
---|
3084 | }
|
---|
3085 | else
|
---|
3086 | pDigest = NULL;
|
---|
3087 |
|
---|
3088 |
|
---|
3089 | /* string case on value name. */
|
---|
3090 | if (!strcmp(g_szLine, "sum-#"))
|
---|
3091 | {
|
---|
3092 | KOCSUM Sum;
|
---|
3093 | if ((fBad = kOCSumInitFromString(&Sum, pszVal) != 0))
|
---|
3094 | break;
|
---|
3095 | kOCSumAdd(&pDigest->SumHead, &Sum);
|
---|
3096 | }
|
---|
3097 | else if (!strcmp(g_szLine, "digest-abs-#"))
|
---|
3098 | {
|
---|
3099 | if ((fBad = pDigest->pszAbsPath != NULL))
|
---|
3100 | break;
|
---|
3101 | pDigest->pszAbsPath = xstrdup(pszVal);
|
---|
3102 | }
|
---|
3103 | else if (!strcmp(g_szLine, "digest-rel-#"))
|
---|
3104 | {
|
---|
3105 | if ((fBad = pDigest->pszRelPath != NULL))
|
---|
3106 | break;
|
---|
3107 | pDigest->pszRelPath = xstrdup(pszVal);
|
---|
3108 | }
|
---|
3109 | else if (!strcmp(g_szLine, "key-#"))
|
---|
3110 | {
|
---|
3111 | if ((fBad = pDigest->uKey != 0))
|
---|
3112 | break;
|
---|
3113 | pDigest->uKey = strtoul(pszVal, &psz, 0);
|
---|
3114 | if ((fBad = psz && *psz))
|
---|
3115 | break;
|
---|
3116 | if (pDigest->uKey >= pCache->uNextKey)
|
---|
3117 | pCache->uNextKey = pDigest->uKey + 1;
|
---|
3118 | }
|
---|
3119 | else if (!strcmp(g_szLine, "comp-argv-sum-#"))
|
---|
3120 | {
|
---|
3121 | if ((fBad = !kOCSumIsEmpty(&pDigest->SumCompArgv)))
|
---|
3122 | break;
|
---|
3123 | if ((fBad = kOCSumInitFromString(&pDigest->SumCompArgv, pszVal) != 0))
|
---|
3124 | break;
|
---|
3125 | }
|
---|
3126 | else if (!strcmp(g_szLine, "target-#"))
|
---|
3127 | {
|
---|
3128 | if ((fBad = pDigest->pszTarget != NULL))
|
---|
3129 | break;
|
---|
3130 | pDigest->pszTarget = xstrdup(pszVal);
|
---|
3131 | }
|
---|
3132 | else if (!strcmp(g_szLine, "digests"))
|
---|
3133 | {
|
---|
3134 | if ((fBad = pCache->paDigests != NULL))
|
---|
3135 | break;
|
---|
3136 | pCache->cDigests = strtoul(pszVal, &psz, 0);
|
---|
3137 | if ((fBad = psz && *psz))
|
---|
3138 | break;
|
---|
3139 | i = (pCache->cDigests + 4) & ~3;
|
---|
3140 | pCache->paDigests = xmalloc(i * sizeof(pCache->paDigests[0]));
|
---|
3141 | for (i = 0; i < pCache->cDigests; i++)
|
---|
3142 | kOCDigestInit(&pCache->paDigests[i]);
|
---|
3143 | }
|
---|
3144 | else if (!strcmp(g_szLine, "generation"))
|
---|
3145 | {
|
---|
3146 | if ((fBad = pCache->uGeneration != 0))
|
---|
3147 | break;
|
---|
3148 | pCache->uGeneration = strtoul(pszVal, &psz, 0);
|
---|
3149 | if ((fBad = psz && *psz))
|
---|
3150 | break;
|
---|
3151 | }
|
---|
3152 | else if (!strcmp(g_szLine, "the-end"))
|
---|
3153 | {
|
---|
3154 | fBad = strcmp(pszVal, "fine");
|
---|
3155 | break;
|
---|
3156 | }
|
---|
3157 | else
|
---|
3158 | {
|
---|
3159 | fBad = 1;
|
---|
3160 | break;
|
---|
3161 | }
|
---|
3162 | } while (fgets(g_szLine, sizeof(g_szLine), pCache->pFile));
|
---|
3163 |
|
---|
3164 | /*
|
---|
3165 | * Did we find everything?
|
---|
3166 | */
|
---|
3167 | fBadBeforeMissing = fBad;
|
---|
3168 | if ( !fBad
|
---|
3169 | && !pCache->uGeneration)
|
---|
3170 | fBad = 1;
|
---|
3171 | if (!fBad)
|
---|
3172 | for (i = 0; i < pCache->cDigests; i++)
|
---|
3173 | {
|
---|
3174 | if ((fBad = kOCSumIsEmpty(&pCache->paDigests[i].SumCompArgv)))
|
---|
3175 | break;
|
---|
3176 | if ((fBad = kOCSumIsEmpty(&pCache->paDigests[i].SumHead)))
|
---|
3177 | break;
|
---|
3178 | if ((fBad = pCache->paDigests[i].uKey == 0))
|
---|
3179 | break;
|
---|
3180 | if ((fBad = pCache->paDigests[i].pszAbsPath == NULL
|
---|
3181 | && pCache->paDigests[i].pszRelPath == NULL))
|
---|
3182 | break;
|
---|
3183 | if ((fBad = pCache->paDigests[i].pszTarget == NULL))
|
---|
3184 | break;
|
---|
3185 | InfoMsg(4, "digest-%u: %s\n", i, pCache->paDigests[i].pszAbsPath
|
---|
3186 | ? pCache->paDigests[i].pszAbsPath : pCache->paDigests[i].pszRelPath);
|
---|
3187 | }
|
---|
3188 | if (fBad)
|
---|
3189 | InfoMsg(2, "bad cache file (%s)\n", fBadBeforeMissing ? g_szLine : "missing stuff");
|
---|
3190 | else if (ferror(pCache->pFile))
|
---|
3191 | {
|
---|
3192 | InfoMsg(2, "cache file read error\n");
|
---|
3193 | fBad = 1;
|
---|
3194 | }
|
---|
3195 | }
|
---|
3196 | if (fBad)
|
---|
3197 | {
|
---|
3198 | kObjCachePurge(pCache);
|
---|
3199 | pCache->fNewCache = 1;
|
---|
3200 | }
|
---|
3201 |
|
---|
3202 | /*
|
---|
3203 | * Disassociate the buffer from the stream changing
|
---|
3204 | * it to non-buffered mode.
|
---|
3205 | */
|
---|
3206 | if (setvbuf(pCache->pFile, NULL, _IONBF, 0) != 0)
|
---|
3207 | FatalDie("setvbuf(,0,,0) failed: %s\n", strerror(errno));
|
---|
3208 | }
|
---|
3209 |
|
---|
3210 |
|
---|
3211 | /**
|
---|
3212 | * Re-writes the cache file.
|
---|
3213 | *
|
---|
3214 | * @param pCache The cache to commit and unlock.
|
---|
3215 | */
|
---|
3216 | static void kObjCacheWrite(PKOBJCACHE pCache)
|
---|
3217 | {
|
---|
3218 | unsigned i;
|
---|
3219 | off_t cb;
|
---|
3220 | char szBuf[8192];
|
---|
3221 | assert(pCache->fLocked);
|
---|
3222 | assert(pCache->fDirty);
|
---|
3223 |
|
---|
3224 | /*
|
---|
3225 | * Rewind the file & stream, and associate a temporary buffer
|
---|
3226 | * with the stream to speed up the writing.
|
---|
3227 | */
|
---|
3228 | if (lseek(pCache->fd, 0, SEEK_SET) == -1)
|
---|
3229 | FatalDie("lseek(cache-fd) failed: %s\n", strerror(errno));
|
---|
3230 | rewind(pCache->pFile);
|
---|
3231 | if (setvbuf(pCache->pFile, szBuf, _IOFBF, sizeof(szBuf)) != 0)
|
---|
3232 | FatalDie("setvbuf failed: %s\n", strerror(errno));
|
---|
3233 |
|
---|
3234 | /*
|
---|
3235 | * Write the header.
|
---|
3236 | */
|
---|
3237 | pCache->uGeneration++;
|
---|
3238 | fprintf(pCache->pFile,
|
---|
3239 | "magic=kObjCache-v0.1.0\n"
|
---|
3240 | "generation=%d\n"
|
---|
3241 | "digests=%d\n",
|
---|
3242 | pCache->uGeneration,
|
---|
3243 | pCache->cDigests);
|
---|
3244 |
|
---|
3245 | /*
|
---|
3246 | * Write the digests.
|
---|
3247 | */
|
---|
3248 | for (i = 0; i < pCache->cDigests; i++)
|
---|
3249 | {
|
---|
3250 | PCKOCDIGEST pDigest = &pCache->paDigests[i];
|
---|
3251 | PKOCSUM pSum;
|
---|
3252 |
|
---|
3253 | if (pDigest->pszAbsPath)
|
---|
3254 | fprintf(pCache->pFile, "digest-abs-#%u=%s\n", i, pDigest->pszAbsPath);
|
---|
3255 | if (pDigest->pszRelPath)
|
---|
3256 | fprintf(pCache->pFile, "digest-rel-#%u=%s\n", i, pDigest->pszRelPath);
|
---|
3257 | fprintf(pCache->pFile, "key-#%u=%u\n", i, pDigest->uKey);
|
---|
3258 | fprintf(pCache->pFile, "target-#%u=%s\n", i, pDigest->pszTarget);
|
---|
3259 | fprintf(pCache->pFile, "comp-argv-sum-#%u=", i);
|
---|
3260 | kOCSumFPrintf(&pDigest->SumCompArgv, pCache->pFile);
|
---|
3261 | for (pSum = &pDigest->SumHead; pSum; pSum = pSum->pNext)
|
---|
3262 | {
|
---|
3263 | fprintf(pCache->pFile, "sum-#%u=", i);
|
---|
3264 | kOCSumFPrintf(pSum, pCache->pFile);
|
---|
3265 | }
|
---|
3266 | }
|
---|
3267 |
|
---|
3268 | /*
|
---|
3269 | * Close the stream and unlock fhe file.
|
---|
3270 | * (Closing the stream shouldn't close the file handle IIRC...)
|
---|
3271 | */
|
---|
3272 | fprintf(pCache->pFile, "the-end=fine\n");
|
---|
3273 | errno = 0;
|
---|
3274 | if ( fflush(pCache->pFile) < 0
|
---|
3275 | || ferror(pCache->pFile))
|
---|
3276 | {
|
---|
3277 | int iErr = errno;
|
---|
3278 | fclose(pCache->pFile);
|
---|
3279 | UnlinkFileInDir(pCache->pszName, pCache->pszDir);
|
---|
3280 | FatalDie("Stream error occured while writing '%s' in '%s': %s\n",
|
---|
3281 | pCache->pszName, pCache->pszDir, strerror(iErr));
|
---|
3282 | }
|
---|
3283 | if (setvbuf(pCache->pFile, NULL, _IONBF, 0) != 0)
|
---|
3284 | FatalDie("setvbuf(,0,,0) failed: %s\n", strerror(errno));
|
---|
3285 |
|
---|
3286 | cb = lseek(pCache->fd, 0, SEEK_CUR);
|
---|
3287 | if (cb == -1)
|
---|
3288 | FatalDie("lseek(cache-file,0,CUR) failed: %s\n", strerror(errno));
|
---|
3289 | #if defined(__WIN__)
|
---|
3290 | if (_chsize(pCache->fd, cb) == -1)
|
---|
3291 | #else
|
---|
3292 | if (ftruncate(pCache->fd, cb) == -1)
|
---|
3293 | #endif
|
---|
3294 | FatalDie("file truncation failed: %s\n", strerror(errno));
|
---|
3295 | InfoMsg(4, "wrote '%s' in '%s', %d bytes\n", pCache->pszName, pCache->pszDir, cb);
|
---|
3296 | }
|
---|
3297 |
|
---|
3298 |
|
---|
3299 | /**
|
---|
3300 | * Cleans out all invalid digests.s
|
---|
3301 | *
|
---|
3302 | * This is done periodically from the unlock routine to make
|
---|
3303 | * sure we don't accidentally accumulate stale digests.
|
---|
3304 | *
|
---|
3305 | * @param pCache The cache to chek.
|
---|
3306 | */
|
---|
3307 | static void kObjCacheClean(PKOBJCACHE pCache)
|
---|
3308 | {
|
---|
3309 | unsigned i = pCache->cDigests;
|
---|
3310 | while (i-- > 0)
|
---|
3311 | {
|
---|
3312 | /*
|
---|
3313 | * Try open it and purge it if it's bad.
|
---|
3314 | * (We don't kill the entry file because that's kmk clean's job.)
|
---|
3315 | */
|
---|
3316 | PCKOCDIGEST pDigest = &pCache->paDigests[i];
|
---|
3317 | PKOCENTRY pEntry = kOCEntryCreate(kOCDigestAbsPath(pDigest, pCache->pszDir));
|
---|
3318 | kOCEntryRead(pEntry);
|
---|
3319 | if ( !kOCEntryCheck(pEntry)
|
---|
3320 | || !kOCDigestIsValid(pDigest, pEntry))
|
---|
3321 | {
|
---|
3322 | unsigned cLeft;
|
---|
3323 | kOCDigestPurge(pDigest);
|
---|
3324 |
|
---|
3325 | pCache->cDigests--;
|
---|
3326 | cLeft = pCache->cDigests - i;
|
---|
3327 | if (cLeft)
|
---|
3328 | memmove(pDigest, pDigest + 1, cLeft * sizeof(*pDigest));
|
---|
3329 |
|
---|
3330 | pCache->fDirty = 1;
|
---|
3331 | }
|
---|
3332 | kOCEntryDestroy(pEntry);
|
---|
3333 | }
|
---|
3334 | }
|
---|
3335 |
|
---|
3336 |
|
---|
3337 | /**
|
---|
3338 | * Locks the cache for exclusive access.
|
---|
3339 | *
|
---|
3340 | * This will open the file if necessary and lock the entire file
|
---|
3341 | * using the best suitable platform API (tricky).
|
---|
3342 | *
|
---|
3343 | * @param pCache The cache to lock.
|
---|
3344 | */
|
---|
3345 | static void kObjCacheLock(PKOBJCACHE pCache)
|
---|
3346 | {
|
---|
3347 | struct stat st;
|
---|
3348 | #if defined(__WIN__)
|
---|
3349 | OVERLAPPED OverLapped;
|
---|
3350 | #endif
|
---|
3351 |
|
---|
3352 | assert(!pCache->fLocked);
|
---|
3353 |
|
---|
3354 | /*
|
---|
3355 | * Open it?
|
---|
3356 | */
|
---|
3357 | if (pCache->fd < 0)
|
---|
3358 | {
|
---|
3359 | pCache->fd = OpenFileInDir(pCache->pszName, pCache->pszDir, O_CREAT | O_RDWR | O_BINARY, 0666);
|
---|
3360 | if (pCache->fd == -1)
|
---|
3361 | {
|
---|
3362 | MakePath(pCache->pszDir);
|
---|
3363 | pCache->fd = OpenFileInDir(pCache->pszName, pCache->pszDir, O_CREAT | O_RDWR | O_BINARY, 0666);
|
---|
3364 | if (pCache->fd == -1)
|
---|
3365 | FatalDie("Failed to create '%s' in '%s': %s\n", pCache->pszName, pCache->pszDir, strerror(errno));
|
---|
3366 | }
|
---|
3367 |
|
---|
3368 | pCache->pFile = fdopen(pCache->fd, "r+b");
|
---|
3369 | if (!pCache->pFile)
|
---|
3370 | FatalDie("fdopen failed: %s\n", strerror(errno));
|
---|
3371 | if (setvbuf(pCache->pFile, NULL, _IONBF, 0) != 0)
|
---|
3372 | FatalDie("setvbuf(,0,,0) failed: %s\n", strerror(errno));
|
---|
3373 | }
|
---|
3374 |
|
---|
3375 | /*
|
---|
3376 | * Lock it.
|
---|
3377 | */
|
---|
3378 | #if defined(__WIN__)
|
---|
3379 | memset(&OverLapped, 0, sizeof(OverLapped));
|
---|
3380 | if (!LockFileEx((HANDLE)_get_osfhandle(pCache->fd), LOCKFILE_EXCLUSIVE_LOCK, 0, ~0, 0, &OverLapped))
|
---|
3381 | FatalDie("Failed to lock the cache file: Windows Error %d\n", GetLastError());
|
---|
3382 | #else
|
---|
3383 | if (flock(pCache->fd, LOCK_EX) != 0)
|
---|
3384 | FatalDie("Failed to lock the cache file: %s\n", strerror(errno));
|
---|
3385 | #endif
|
---|
3386 | pCache->fLocked = 1;
|
---|
3387 |
|
---|
3388 | /*
|
---|
3389 | * Check for new cache and read it it's an existing cache.
|
---|
3390 | *
|
---|
3391 | * There is no point in initializing a new cache until we've finished
|
---|
3392 | * compiling and has something to put into it, so we'll leave it as a
|
---|
3393 | * 0 byte file.
|
---|
3394 | */
|
---|
3395 | if (fstat(pCache->fd, &st) == -1)
|
---|
3396 | FatalDie("fstat(cache-fd) failed: %s\n", strerror(errno));
|
---|
3397 | if (st.st_size)
|
---|
3398 | kObjCacheRead(pCache);
|
---|
3399 | else
|
---|
3400 | {
|
---|
3401 | pCache->fNewCache = 1;
|
---|
3402 | InfoMsg(2, "the cache file is empty\n");
|
---|
3403 | }
|
---|
3404 | }
|
---|
3405 |
|
---|
3406 |
|
---|
3407 | /**
|
---|
3408 | * Unlocks the cache (without writing anything back).
|
---|
3409 | *
|
---|
3410 | * @param pCache The cache to unlock.
|
---|
3411 | */
|
---|
3412 | static void kObjCacheUnlock(PKOBJCACHE pCache)
|
---|
3413 | {
|
---|
3414 | #if defined(__WIN__)
|
---|
3415 | OVERLAPPED OverLapped;
|
---|
3416 | #endif
|
---|
3417 | assert(pCache->fLocked);
|
---|
3418 |
|
---|
3419 | /*
|
---|
3420 | * Write it back if it's dirty.
|
---|
3421 | */
|
---|
3422 | if (pCache->fDirty)
|
---|
3423 | {
|
---|
3424 | if ( pCache->cDigests >= 16
|
---|
3425 | && (pCache->uGeneration % 19) == 19)
|
---|
3426 | kObjCacheClean(pCache);
|
---|
3427 | kObjCacheWrite(pCache);
|
---|
3428 | pCache->fDirty = 0;
|
---|
3429 | }
|
---|
3430 |
|
---|
3431 | /*
|
---|
3432 | * Lock it.
|
---|
3433 | */
|
---|
3434 | #if defined(__WIN__)
|
---|
3435 | memset(&OverLapped, 0, sizeof(OverLapped));
|
---|
3436 | if (!UnlockFileEx((HANDLE)_get_osfhandle(pCache->fd), 0, ~0U, 0, &OverLapped))
|
---|
3437 | FatalDie("Failed to unlock the cache file: Windows Error %d\n", GetLastError());
|
---|
3438 | #else
|
---|
3439 | if (flock(pCache->fd, LOCK_UN) != 0)
|
---|
3440 | FatalDie("Failed to unlock the cache file: %s\n", strerror(errno));
|
---|
3441 | #endif
|
---|
3442 | pCache->fLocked = 0;
|
---|
3443 | }
|
---|
3444 |
|
---|
3445 |
|
---|
3446 | /**
|
---|
3447 | * Removes the entry from the cache.
|
---|
3448 | *
|
---|
3449 | * The entry doesn't need to be in the cache.
|
---|
3450 | * The cache entry (file) itself is not touched.
|
---|
3451 | *
|
---|
3452 | * @param pCache The cache.
|
---|
3453 | * @param pEntry The entry.
|
---|
3454 | */
|
---|
3455 | static void kObjCacheRemoveEntry(PKOBJCACHE pCache, PCKOCENTRY pEntry)
|
---|
3456 | {
|
---|
3457 | unsigned i = pCache->cDigests;
|
---|
3458 | while (i-- > 0)
|
---|
3459 | {
|
---|
3460 | PKOCDIGEST pDigest = &pCache->paDigests[i];
|
---|
3461 | if (ArePathsIdentical(kOCDigestAbsPath(pDigest, pCache->pszDir),
|
---|
3462 | kOCEntryAbsPath(pEntry), ~0U))
|
---|
3463 | {
|
---|
3464 | unsigned cLeft;
|
---|
3465 | kOCDigestPurge(pDigest);
|
---|
3466 |
|
---|
3467 | pCache->cDigests--;
|
---|
3468 | cLeft = pCache->cDigests - i;
|
---|
3469 | if (cLeft)
|
---|
3470 | memmove(pDigest, pDigest + 1, cLeft * sizeof(*pDigest));
|
---|
3471 |
|
---|
3472 | pCache->fDirty = 1;
|
---|
3473 | InfoMsg(3, "removing entry '%s'; %d left.\n", kOCEntryAbsPath(pEntry), pCache->cDigests);
|
---|
3474 | }
|
---|
3475 | }
|
---|
3476 | }
|
---|
3477 |
|
---|
3478 |
|
---|
3479 | /**
|
---|
3480 | * Inserts the entry into the cache.
|
---|
3481 | *
|
---|
3482 | * The cache entry (file) itself is not touched by this operation,
|
---|
3483 | * the pEntry object otoh is.
|
---|
3484 | *
|
---|
3485 | * @param pCache The cache.
|
---|
3486 | * @param pEntry The entry.
|
---|
3487 | */
|
---|
3488 | static void kObjCacheInsertEntry(PKOBJCACHE pCache, PKOCENTRY pEntry)
|
---|
3489 | {
|
---|
3490 | unsigned i;
|
---|
3491 |
|
---|
3492 | /*
|
---|
3493 | * Find a new key.
|
---|
3494 | */
|
---|
3495 | pEntry->uKey = pCache->uNextKey++;
|
---|
3496 | if (!pEntry->uKey)
|
---|
3497 | pEntry->uKey = pCache->uNextKey++;
|
---|
3498 | i = pCache->cDigests;
|
---|
3499 | while (i-- > 0)
|
---|
3500 | if (pCache->paDigests[i].uKey == pEntry->uKey)
|
---|
3501 | {
|
---|
3502 | pEntry->uKey = pCache->uNextKey++;
|
---|
3503 | if (!pEntry->uKey)
|
---|
3504 | pEntry->uKey = pCache->uNextKey++;
|
---|
3505 | i = pCache->cDigests;
|
---|
3506 | }
|
---|
3507 |
|
---|
3508 | /*
|
---|
3509 | * Reallocate the digest array?
|
---|
3510 | */
|
---|
3511 | if ( !(pCache->cDigests & 3)
|
---|
3512 | && (pCache->cDigests || !pCache->paDigests))
|
---|
3513 | pCache->paDigests = xrealloc(pCache->paDigests, sizeof(pCache->paDigests[0]) * (pCache->cDigests + 4));
|
---|
3514 |
|
---|
3515 | /*
|
---|
3516 | * Create a new digest.
|
---|
3517 | */
|
---|
3518 | kOCDigestInitFromEntry(&pCache->paDigests[pCache->cDigests], pEntry);
|
---|
3519 | pCache->cDigests++;
|
---|
3520 | InfoMsg(4, "Inserted digest #%u: %s\n", pCache->cDigests - 1, kOCEntryAbsPath(pEntry));
|
---|
3521 |
|
---|
3522 | pCache->fDirty = 1;
|
---|
3523 | }
|
---|
3524 |
|
---|
3525 |
|
---|
3526 | /**
|
---|
3527 | * Find a matching cache entry.
|
---|
3528 | */
|
---|
3529 | static PKOCENTRY kObjCacheFindMatchingEntry(PKOBJCACHE pCache, PCKOCENTRY pEntry)
|
---|
3530 | {
|
---|
3531 | unsigned i = pCache->cDigests;
|
---|
3532 |
|
---|
3533 | assert(pEntry->fNeedCompiling);
|
---|
3534 | assert(!kOCSumIsEmpty(&pEntry->New.SumCompArgv));
|
---|
3535 | assert(!kOCSumIsEmpty(&pEntry->New.SumHead));
|
---|
3536 |
|
---|
3537 | while (i-- > 0)
|
---|
3538 | {
|
---|
3539 | /*
|
---|
3540 | * Matching?
|
---|
3541 | */
|
---|
3542 | PCKOCDIGEST pDigest = &pCache->paDigests[i];
|
---|
3543 | if ( kOCSumIsEqual(&pDigest->SumCompArgv, &pEntry->New.SumCompArgv)
|
---|
3544 | && kOCSumHasEqualInChain(&pDigest->SumHead, &pEntry->New.SumHead))
|
---|
3545 | {
|
---|
3546 | /*
|
---|
3547 | * Try open it.
|
---|
3548 | */
|
---|
3549 | unsigned cLeft;
|
---|
3550 | PKOCENTRY pRetEntry = kOCEntryCreate(kOCDigestAbsPath(pDigest, pCache->pszDir));
|
---|
3551 | kOCEntryRead(pRetEntry);
|
---|
3552 | if ( kOCEntryCheck(pRetEntry)
|
---|
3553 | && kOCDigestIsValid(pDigest, pRetEntry))
|
---|
3554 | return pRetEntry;
|
---|
3555 | kOCEntryDestroy(pRetEntry);
|
---|
3556 |
|
---|
3557 | /* bad entry, purge it. */
|
---|
3558 | InfoMsg(3, "removing bad digest '%s'\n", kOCDigestAbsPath(pDigest, pCache->pszDir));
|
---|
3559 | kOCDigestPurge(pDigest);
|
---|
3560 |
|
---|
3561 | pCache->cDigests--;
|
---|
3562 | cLeft = pCache->cDigests - i;
|
---|
3563 | if (cLeft)
|
---|
3564 | memmove(pDigest, pDigest + 1, cLeft * sizeof(*pDigest));
|
---|
3565 |
|
---|
3566 | pCache->fDirty = 1;
|
---|
3567 | }
|
---|
3568 | }
|
---|
3569 |
|
---|
3570 | return NULL;
|
---|
3571 | }
|
---|
3572 |
|
---|
3573 |
|
---|
3574 | /**
|
---|
3575 | * Is this a new cache?
|
---|
3576 | *
|
---|
3577 | * @returns 1 if new, 0 if not new.
|
---|
3578 | * @param pEntry The entry.
|
---|
3579 | */
|
---|
3580 | static int kObjCacheIsNew(PKOBJCACHE pCache)
|
---|
3581 | {
|
---|
3582 | return pCache->fNewCache;
|
---|
3583 | }
|
---|
3584 |
|
---|
3585 |
|
---|
3586 | /**
|
---|
3587 | * Prints a syntax error and returns the appropriate exit code
|
---|
3588 | *
|
---|
3589 | * @returns approriate exit code.
|
---|
3590 | * @param pszFormat The syntax error message.
|
---|
3591 | * @param ... Message args.
|
---|
3592 | */
|
---|
3593 | static int SyntaxError(const char *pszFormat, ...)
|
---|
3594 | {
|
---|
3595 | va_list va;
|
---|
3596 | fprintf(stderr, "kObjCache: syntax error: ");
|
---|
3597 | va_start(va, pszFormat);
|
---|
3598 | vfprintf(stderr, pszFormat, va);
|
---|
3599 | va_end(va);
|
---|
3600 | return 1;
|
---|
3601 | }
|
---|
3602 |
|
---|
3603 |
|
---|
3604 | /**
|
---|
3605 | * Prints the usage.
|
---|
3606 | * @returns 0.
|
---|
3607 | */
|
---|
3608 | static int usage(void)
|
---|
3609 | {
|
---|
3610 | printf("syntax: kObjCache [--kObjCache-options] [-v|--verbose]\n"
|
---|
3611 | " < [-c|--cache-file <cache-file>]\n"
|
---|
3612 | " | [-n|--name <name-in-cache>] [[-d|--cache-dir <cache-dir>]] >\n"
|
---|
3613 | " <-f|--file <local-cache-file>>\n"
|
---|
3614 | " <-t|--target <target-name>>\n"
|
---|
3615 | " [-r|--redir-stdout] [-p|--passthru]\n"
|
---|
3616 | " --kObjCache-cpp <filename> <precompiler + args>\n"
|
---|
3617 | " --kObjCache-cc <object> <compiler + args>\n"
|
---|
3618 | " [--kObjCache-both [args]]\n"
|
---|
3619 | " [--kObjCache-cpp|--kObjCache-cc [more args]]\n"
|
---|
3620 | " kObjCache <-V|--version>\n"
|
---|
3621 | " kObjCache [-?|/?|-h|/h|--help|/help]\n"
|
---|
3622 | "\n"
|
---|
3623 | "The env.var. KOBJCACHE_DIR sets the default cache diretory (-d).\n"
|
---|
3624 | "The env.var. KOBJCACHE_OPTS allow you to specifie additional options\n"
|
---|
3625 | "without having to mess with the makefiles. These are appended with "
|
---|
3626 | "a --kObjCache-options between them and the command args.\n"
|
---|
3627 | "\n");
|
---|
3628 | return 0;
|
---|
3629 | }
|
---|
3630 |
|
---|
3631 |
|
---|
3632 | int main(int argc, char **argv)
|
---|
3633 | {
|
---|
3634 | PKOBJCACHE pCache;
|
---|
3635 | PKOCENTRY pEntry;
|
---|
3636 |
|
---|
3637 | const char *pszCacheDir = getenv("KOBJCACHE_DIR");
|
---|
3638 | const char *pszCacheName = NULL;
|
---|
3639 | const char *pszCacheFile = NULL;
|
---|
3640 | const char *pszEntryFile = NULL;
|
---|
3641 |
|
---|
3642 | const char **papszArgvPreComp = NULL;
|
---|
3643 | unsigned cArgvPreComp = 0;
|
---|
3644 | const char *pszPreCompName = NULL;
|
---|
3645 | int fRedirPreCompStdOut = 0;
|
---|
3646 |
|
---|
3647 | const char **papszArgvCompile = NULL;
|
---|
3648 | unsigned cArgvCompile = 0;
|
---|
3649 | const char *pszObjName = NULL;
|
---|
3650 | int fRedirCompileStdIn = 0;
|
---|
3651 |
|
---|
3652 | const char *pszTarget = NULL;
|
---|
3653 |
|
---|
3654 | enum { kOC_Options, kOC_CppArgv, kOC_CcArgv, kOC_BothArgv } enmMode = kOC_Options;
|
---|
3655 |
|
---|
3656 | size_t cch;
|
---|
3657 | char *psz;
|
---|
3658 | int i;
|
---|
3659 |
|
---|
3660 | SetErrorPrefix("kObjCache");
|
---|
3661 |
|
---|
3662 | /*
|
---|
3663 | * Arguments passed in the environmnet?
|
---|
3664 | */
|
---|
3665 | psz = getenv("KOBJCACHE_OPTS");
|
---|
3666 | if (psz)
|
---|
3667 | AppendArgs(&argc, &argv, psz, "--kObjCache-options");
|
---|
3668 |
|
---|
3669 | /*
|
---|
3670 | * Parse the arguments.
|
---|
3671 | */
|
---|
3672 | if (argc <= 1)
|
---|
3673 | return usage();
|
---|
3674 | for (i = 1; i < argc; i++)
|
---|
3675 | {
|
---|
3676 | if (!strcmp(argv[i], "--kObjCache-cpp"))
|
---|
3677 | {
|
---|
3678 | enmMode = kOC_CppArgv;
|
---|
3679 | if (!pszPreCompName)
|
---|
3680 | {
|
---|
3681 | if (++i >= argc)
|
---|
3682 | return SyntaxError("--kObjCache-cpp requires an object filename!\n");
|
---|
3683 | pszPreCompName = argv[i];
|
---|
3684 | }
|
---|
3685 | }
|
---|
3686 | else if (!strcmp(argv[i], "--kObjCache-cc"))
|
---|
3687 | {
|
---|
3688 | enmMode = kOC_CcArgv;
|
---|
3689 | if (!pszObjName)
|
---|
3690 | {
|
---|
3691 | if (++i >= argc)
|
---|
3692 | return SyntaxError("--kObjCache-cc requires an precompiler output filename!\n");
|
---|
3693 | pszObjName = argv[i];
|
---|
3694 | }
|
---|
3695 | }
|
---|
3696 | else if (!strcmp(argv[i], "--kObjCache-both"))
|
---|
3697 | enmMode = kOC_BothArgv;
|
---|
3698 | else if (!strcmp(argv[i], "--kObjCache-options"))
|
---|
3699 | enmMode = kOC_Options;
|
---|
3700 | else if (!strcmp(argv[i], "--help"))
|
---|
3701 | return usage();
|
---|
3702 | else if (enmMode != kOC_Options)
|
---|
3703 | {
|
---|
3704 | if (enmMode == kOC_CppArgv || enmMode == kOC_BothArgv)
|
---|
3705 | {
|
---|
3706 | if (!(cArgvPreComp % 16))
|
---|
3707 | papszArgvPreComp = xrealloc((void *)papszArgvPreComp, (cArgvPreComp + 17) * sizeof(papszArgvPreComp[0]));
|
---|
3708 | papszArgvPreComp[cArgvPreComp++] = argv[i];
|
---|
3709 | papszArgvPreComp[cArgvPreComp] = NULL;
|
---|
3710 | }
|
---|
3711 | if (enmMode == kOC_CcArgv || enmMode == kOC_BothArgv)
|
---|
3712 | {
|
---|
3713 | if (!(cArgvCompile % 16))
|
---|
3714 | papszArgvCompile = xrealloc((void *)papszArgvCompile, (cArgvCompile + 17) * sizeof(papszArgvCompile[0]));
|
---|
3715 | papszArgvCompile[cArgvCompile++] = argv[i];
|
---|
3716 | papszArgvCompile[cArgvCompile] = NULL;
|
---|
3717 | }
|
---|
3718 | }
|
---|
3719 | else if (!strcmp(argv[i], "-f") || !strcmp(argv[i], "--entry-file"))
|
---|
3720 | {
|
---|
3721 | if (i + 1 >= argc)
|
---|
3722 | return SyntaxError("%s requires a cache entry filename!\n", argv[i]);
|
---|
3723 | pszEntryFile = argv[++i];
|
---|
3724 | }
|
---|
3725 | else if (!strcmp(argv[i], "-c") || !strcmp(argv[i], "--cache-file"))
|
---|
3726 | {
|
---|
3727 | if (i + 1 >= argc)
|
---|
3728 | return SyntaxError("%s requires a cache filename!\n", argv[i]);
|
---|
3729 | pszCacheFile = argv[++i];
|
---|
3730 | }
|
---|
3731 | else if (!strcmp(argv[i], "-n") || !strcmp(argv[i], "--name"))
|
---|
3732 | {
|
---|
3733 | if (i + 1 >= argc)
|
---|
3734 | return SyntaxError("%s requires a cache name!\n", argv[i]);
|
---|
3735 | pszCacheName = argv[++i];
|
---|
3736 | }
|
---|
3737 | else if (!strcmp(argv[i], "-d") || !strcmp(argv[i], "--cache-dir"))
|
---|
3738 | {
|
---|
3739 | if (i + 1 >= argc)
|
---|
3740 | return SyntaxError("%s requires a cache directory!\n", argv[i]);
|
---|
3741 | pszCacheDir = argv[++i];
|
---|
3742 | }
|
---|
3743 | else if (!strcmp(argv[i], "-t") || !strcmp(argv[i], "--target"))
|
---|
3744 | {
|
---|
3745 | if (i + 1 >= argc)
|
---|
3746 | return SyntaxError("%s requires a target platform/arch name!\n", argv[i]);
|
---|
3747 | pszTarget = argv[++i];
|
---|
3748 | }
|
---|
3749 | else if (!strcmp(argv[i], "-p") || !strcmp(argv[i], "--passthru"))
|
---|
3750 | fRedirPreCompStdOut = fRedirCompileStdIn = 1;
|
---|
3751 | else if (!strcmp(argv[i], "-r") || !strcmp(argv[i], "--redir-stdout"))
|
---|
3752 | fRedirPreCompStdOut = 1;
|
---|
3753 | else if (!strcmp(argv[i], "-v") || !strcmp(argv[i], "--verbose"))
|
---|
3754 | g_cVerbosityLevel++;
|
---|
3755 | else if (!strcmp(argv[i], "-q") || !strcmp(argv[i], "--quiet"))
|
---|
3756 | g_cVerbosityLevel = 0;
|
---|
3757 | else if (!strcmp(argv[i], "-h") || !strcmp(argv[i], "-?")
|
---|
3758 | || !strcmp(argv[i], "/h") || !strcmp(argv[i], "/?") || !strcmp(argv[i], "/help"))
|
---|
3759 | return usage();
|
---|
3760 | else if (!strcmp(argv[i], "-V") || !strcmp(argv[i], "--version"))
|
---|
3761 | {
|
---|
3762 | printf("kObjCache v0.0.0 ($Revision: 1047 $)\n");
|
---|
3763 | return 0;
|
---|
3764 | }
|
---|
3765 | else
|
---|
3766 | return SyntaxError("Doesn't grok '%s'!\n", argv[i]);
|
---|
3767 | }
|
---|
3768 | if (!pszEntryFile)
|
---|
3769 | return SyntaxError("No cache entry filename (-f)!\n");
|
---|
3770 | if (!pszTarget)
|
---|
3771 | return SyntaxError("No target name (-t)!\n");
|
---|
3772 | if (!cArgvCompile)
|
---|
3773 | return SyntaxError("No compiler arguments (--kObjCache-cc)!\n");
|
---|
3774 | if (!cArgvPreComp)
|
---|
3775 | return SyntaxError("No precompiler arguments (--kObjCache-cc)!\n");
|
---|
3776 |
|
---|
3777 | /*
|
---|
3778 | * Calc the cache file name.
|
---|
3779 | * It's a bit messy since the extension has to be replaced.
|
---|
3780 | */
|
---|
3781 | if (!pszCacheFile)
|
---|
3782 | {
|
---|
3783 | if (!pszCacheDir)
|
---|
3784 | return SyntaxError("No cache dir (-d / KOBJCACHE_DIR) and no cache filename!\n");
|
---|
3785 | if (!pszCacheName)
|
---|
3786 | {
|
---|
3787 | psz = (char *)FindFilenameInPath(pszEntryFile);
|
---|
3788 | if (!*psz)
|
---|
3789 | return SyntaxError("The cache file (-f) specifies a directory / nothing!\n");
|
---|
3790 | cch = psz - pszEntryFile;
|
---|
3791 | pszCacheName = memcpy(xmalloc(cch + 5), psz, cch + 1);
|
---|
3792 | psz = strrchr(pszCacheName, '.');
|
---|
3793 | if (!psz || psz <= pszCacheName)
|
---|
3794 | psz = (char *)pszCacheName + cch;
|
---|
3795 | memcpy(psz, ".koc", sizeof(".koc") - 1);
|
---|
3796 | }
|
---|
3797 | pszCacheFile = MakePathFromDirAndFile(pszCacheName, pszCacheDir);
|
---|
3798 | }
|
---|
3799 |
|
---|
3800 | /*
|
---|
3801 | * Create and initialize the two objects we'll be working on.
|
---|
3802 | *
|
---|
3803 | * We're supposed to be the only ones actually writing to the local file,
|
---|
3804 | * so it's perfectly fine to read it here before we lock it. This simplifies
|
---|
3805 | * the detection of object name and compiler argument changes.
|
---|
3806 | */
|
---|
3807 | SetErrorPrefix("kObjCache - %s", FindFilenameInPath(pszCacheFile));
|
---|
3808 | pCache = kObjCacheCreate(pszCacheFile);
|
---|
3809 |
|
---|
3810 | pEntry = kOCEntryCreate(pszEntryFile);
|
---|
3811 | kOCEntryRead(pEntry);
|
---|
3812 | kOCEntrySetCompileObjName(pEntry, pszObjName);
|
---|
3813 | kOCEntrySetCompileArgv(pEntry, papszArgvCompile, cArgvCompile);
|
---|
3814 | kOCEntrySetTarget(pEntry, pszTarget);
|
---|
3815 | kOCEntrySetCppName(pEntry, pszPreCompName);
|
---|
3816 | kOCEntrySetPipedMode(pEntry, fRedirPreCompStdOut, fRedirCompileStdIn);
|
---|
3817 |
|
---|
3818 | /*
|
---|
3819 | * Open (& lock) the two files and do validity checks and such.
|
---|
3820 | */
|
---|
3821 | kObjCacheLock(pCache);
|
---|
3822 | if ( kObjCacheIsNew(pCache)
|
---|
3823 | && kOCEntryNeedsCompiling(pEntry))
|
---|
3824 | {
|
---|
3825 | /*
|
---|
3826 | * Both files are missing/invalid.
|
---|
3827 | * Optimize this path as it is frequently used when making a clean build.
|
---|
3828 | */
|
---|
3829 | kObjCacheUnlock(pCache);
|
---|
3830 | InfoMsg(1, "doing full compile\n");
|
---|
3831 | kOCEntryPreCompileAndCompile(pEntry, papszArgvPreComp, cArgvPreComp);
|
---|
3832 | kObjCacheLock(pCache);
|
---|
3833 | }
|
---|
3834 | else
|
---|
3835 | {
|
---|
3836 | /*
|
---|
3837 | * Do the precompile (don't need to lock the cache file for this).
|
---|
3838 | */
|
---|
3839 | kObjCacheUnlock(pCache);
|
---|
3840 | kOCEntryPreCompile(pEntry, papszArgvPreComp, cArgvPreComp);
|
---|
3841 |
|
---|
3842 | /*
|
---|
3843 | * Check if we need to recompile. If we do, try see if the is a cache entry first.
|
---|
3844 | */
|
---|
3845 | kOCEntryCalcRecompile(pEntry);
|
---|
3846 | if (kOCEntryNeedsCompiling(pEntry))
|
---|
3847 | {
|
---|
3848 | PKOCENTRY pUseEntry;
|
---|
3849 | kObjCacheLock(pCache);
|
---|
3850 | kObjCacheRemoveEntry(pCache, pEntry);
|
---|
3851 | pUseEntry = kObjCacheFindMatchingEntry(pCache, pEntry);
|
---|
3852 | if (pUseEntry)
|
---|
3853 | {
|
---|
3854 | InfoMsg(1, "using cache entry '%s'\n", kOCEntryAbsPath(pUseEntry));
|
---|
3855 | kOCEntryCopy(pEntry, pUseEntry);
|
---|
3856 | kOCEntryDestroy(pUseEntry);
|
---|
3857 | }
|
---|
3858 | else
|
---|
3859 | {
|
---|
3860 | kObjCacheUnlock(pCache);
|
---|
3861 | InfoMsg(1, "recompiling\n");
|
---|
3862 | kOCEntryCompileIt(pEntry);
|
---|
3863 | kObjCacheLock(pCache);
|
---|
3864 | }
|
---|
3865 | }
|
---|
3866 | else
|
---|
3867 | {
|
---|
3868 | InfoMsg(1, "no need to recompile\n");
|
---|
3869 | kObjCacheLock(pCache);
|
---|
3870 | }
|
---|
3871 | }
|
---|
3872 |
|
---|
3873 | /*
|
---|
3874 | * Update the cache files.
|
---|
3875 | */
|
---|
3876 | kObjCacheRemoveEntry(pCache, pEntry);
|
---|
3877 | kObjCacheInsertEntry(pCache, pEntry);
|
---|
3878 | kOCEntryWrite(pEntry);
|
---|
3879 | kObjCacheUnlock(pCache);
|
---|
3880 | kObjCacheDestroy(pCache);
|
---|
3881 | return 0;
|
---|
3882 | }
|
---|
3883 |
|
---|
3884 |
|
---|
3885 | /** @page kObjCache Benchmarks.
|
---|
3886 | *
|
---|
3887 | * 2007-06-02 - 21-23:00:
|
---|
3888 | * Mac OS X debug -j 3 clobber build (rm -Rf out/darwin.x86/debug ; sync ; svn diff ; sync ; sleep 1 ; time kmk -j 3):
|
---|
3889 | * real 10m26.077s
|
---|
3890 | * user 13m13.291s
|
---|
3891 | * sys 2m58.193s
|
---|
3892 | *
|
---|
3893 | * Mac OS X debug -j 3 depend build (touch include/iprt/err.h ; sync ; svn diff ; sync ; sleep 1 ; time kmk -j 3):
|
---|
3894 | * real 3m55.275s
|
---|
3895 | * user 4m11.852s
|
---|
3896 | * sys 0m54.931s
|
---|
3897 | *
|
---|
3898 | * Mac OS X debug -j 3 cached clobber build (rm -Rf out/darwin.x86/debug ; sync ; svn diff ; sync ; sleep 1 ; time kmk -j 3 USE_KOBJCACHE=1):
|
---|
3899 | * real 11m42.513s
|
---|
3900 | * user 14m27.736s
|
---|
3901 | * sys 3m39.512s
|
---|
3902 | *
|
---|
3903 | * Mac OS X debug -j 3 cached depend build (touch include/iprt/err.h ; sync ; svn diff ; sync ; sleep 1 ; time kmk -j 3 USE_KOBJCACHE=1):
|
---|
3904 | * real 1m17.445s
|
---|
3905 | * user 1m13.410s
|
---|
3906 | * sys 0m22.789s
|
---|
3907 | *
|
---|
3908 | * Mac OS X debug -j3 cached depend build (touch include/iprt/cdefs.h ; sync ; svn diff ; sync ; sleep 1 ; time kmk -j 3 USE_KOBJCACHE=1):
|
---|
3909 | * real 1m29.315s
|
---|
3910 | * user 1m31.391s
|
---|
3911 | * sys 0m32.748s
|
---|
3912 | *
|
---|
3913 | */
|
---|