VirtualBox

source: kBuild/trunk/src/kObjCache/kObjCache.c@ 2615

Last change on this file since 2615 was 2615, checked in by bird, 13 years ago

kObjCache: Forgot to clean out the debug code...

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

© 2025 Oracle Support Privacy / Do Not Sell My Info Terms of Use Trademark Policy Automated Access Etiquette