source: trunk/src/kObjCache/kObjCache.c@ 2613

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

kObjCache: Fixes to the make dependency feature.

  • Property svn:eol-style set to native
  • Property svn:keywords set to Author Date Id Revision
File size: 137.7 KB
Line 
1/* $Id: kObjCache.c 2613 2012-07-29 19:42:30Z bird $ */
2/** @file
3 * kObjCache - Object Cache.
4 */
5
6/*
7 * Copyright (c) 2007-2012 knut st. osmundsen <bird-kBuild-spamx@anduin.net>
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 pszIgnorePath Path to ignore when encountered at the end of arguments.
1571 * (Not quite safe for simple file names, but what the heck.)
1572 * @param pSum Where to store the check sum.
1573 */
1574static void kOCEntryCalcArgvSum(PKOCENTRY pEntry, const char * const *papszArgv, unsigned cArgc,
1575 const char *pszIgnorePath, PKOCSUM pSum)
1576{
1577 size_t cchIgnorePath = strlen(pszIgnorePath);
1578 KOCSUMCTX Ctx;
1579 unsigned i;
1580
1581 kOCSumInitWithCtx(pSum, &Ctx);
1582 for (i = 0; i < cArgc; i++)
1583 {
1584 size_t cch = strlen(papszArgv[i]);
1585 if ( cch < cchIgnorePath
1586 || !ArePathsIdenticalN(papszArgv[i] + cch - cchIgnorePath, pszIgnorePath, cch))
1587 kOCSumUpdate(pSum, &Ctx, papszArgv[i], cch + 1);
1588 }
1589 kOCSumFinalize(pSum, &Ctx);
1590
1591 (void)pEntry;
1592}
1593
1594
1595/**
1596 * Reads and parses the cache file.
1597 *
1598 * @param pEntry The entry to read it into.
1599 */
1600static void kOCEntryRead(PKOCENTRY pEntry)
1601{
1602 FILE *pFile;
1603 pFile = FOpenFileInDir(pEntry->pszName, pEntry->pszDir, "rb");
1604 if (pFile)
1605 {
1606 InfoMsg(4, "reading cache entry...\n");
1607
1608 /*
1609 * Check the magic.
1610 */
1611 if ( !fgets(g_szLine, sizeof(g_szLine), pFile)
1612 || ( strcmp(g_szLine, "magic=kObjCacheEntry-v0.1.0\n")
1613 && strcmp(g_szLine, "magic=kObjCacheEntry-v0.1.1\n"))
1614 )
1615 {
1616 InfoMsg(2, "bad cache file (magic)\n");
1617 pEntry->fNeedCompiling = 1;
1618 }
1619 else
1620 {
1621 /*
1622 * Parse the rest of the file (relaxed order).
1623 */
1624 unsigned i;
1625 int fBad = 0;
1626 int fBadBeforeMissing = 1;
1627 while (fgets(g_szLine, sizeof(g_szLine), pFile))
1628 {
1629 char *pszNl;
1630 char *pszVal;
1631
1632 /* Split the line and drop the trailing newline. */
1633 pszVal = strchr(g_szLine, '=');
1634 if ((fBad = pszVal == NULL))
1635 break;
1636 *pszVal++ = '\0';
1637
1638 pszNl = strchr(pszVal, '\n');
1639 if (pszNl)
1640 *pszNl = '\0';
1641
1642 /* string case on variable name */
1643 if (!strcmp(g_szLine, "obj"))
1644 {
1645 if ((fBad = pEntry->Old.pszObjName != NULL))
1646 break;
1647 pEntry->Old.pszObjName = xstrdup(pszVal);
1648 }
1649 else if (!strcmp(g_szLine, "cpp"))
1650 {
1651 if ((fBad = pEntry->Old.pszCppName != NULL))
1652 break;
1653 pEntry->Old.pszCppName = xstrdup(pszVal);
1654 }
1655 else if (!strcmp(g_szLine, "cpp-size"))
1656 {
1657 char *pszNext;
1658 if ((fBad = pEntry->Old.cbCpp != 0))
1659 break;
1660 pEntry->Old.cbCpp = strtoul(pszVal, &pszNext, 0);
1661 if ((fBad = pszNext && *pszNext))
1662 break;
1663 }
1664 else if (!strcmp(g_szLine, "cpp-sum"))
1665 {
1666 KOCSUM Sum;
1667 if ((fBad = kOCSumInitFromString(&Sum, pszVal)))
1668 break;
1669 kOCSumAdd(&pEntry->Old.SumHead, &Sum);
1670 }
1671 else if (!strcmp(g_szLine, "cpp-ms"))
1672 {
1673 char *pszNext;
1674 if ((fBad = pEntry->Old.cMsCpp != 0))
1675 break;
1676 pEntry->Old.cMsCpp = strtoul(pszVal, &pszNext, 0);
1677 if ((fBad = pszNext && *pszNext))
1678 break;
1679 }
1680 else if (!strcmp(g_szLine, "cc-argc"))
1681 {
1682 if ((fBad = pEntry->Old.papszArgvCompile != NULL))
1683 break;
1684 pEntry->Old.cArgvCompile = atoi(pszVal); /* if wrong, we'll fail below. */
1685 pEntry->Old.papszArgvCompile = xmallocz((pEntry->Old.cArgvCompile + 1) * sizeof(pEntry->Old.papszArgvCompile[0]));
1686 }
1687 else if (!strncmp(g_szLine, "cc-argv-#", sizeof("cc-argv-#") - 1))
1688 {
1689 char *pszNext;
1690 unsigned i = strtoul(&g_szLine[sizeof("cc-argv-#") - 1], &pszNext, 0);
1691 if ((fBad = i >= pEntry->Old.cArgvCompile || pEntry->Old.papszArgvCompile[i] || (pszNext && *pszNext)))
1692 break;
1693 pEntry->Old.papszArgvCompile[i] = xstrdup(pszVal);
1694 }
1695 else if (!strcmp(g_szLine, "cc-argv-sum"))
1696 {
1697 if ((fBad = !kOCSumIsEmpty(&pEntry->Old.SumCompArgv)))
1698 break;
1699 if ((fBad = kOCSumInitFromString(&pEntry->Old.SumCompArgv, pszVal)))
1700 break;
1701 }
1702 else if (!strcmp(g_szLine, "cc-ms"))
1703 {
1704 char *pszNext;
1705 if ((fBad = pEntry->Old.cMsCompile != 0))
1706 break;
1707 pEntry->Old.cMsCompile = strtoul(pszVal, &pszNext, 0);
1708 if ((fBad = pszNext && *pszNext))
1709 break;
1710 }
1711 else if (!strcmp(g_szLine, "target"))
1712 {
1713 if ((fBad = pEntry->Old.pszTarget != NULL))
1714 break;
1715 pEntry->Old.pszTarget = xstrdup(pszVal);
1716 }
1717 else if (!strcmp(g_szLine, "key"))
1718 {
1719 char *pszNext;
1720 if ((fBad = pEntry->uKey != 0))
1721 break;
1722 pEntry->uKey = strtoul(pszVal, &pszNext, 0);
1723 if ((fBad = pszNext && *pszNext))
1724 break;
1725 }
1726 else if (!strcmp(g_szLine, "the-end"))
1727 {
1728 fBadBeforeMissing = fBad = strcmp(pszVal, "fine");
1729 break;
1730 }
1731 else
1732 {
1733 fBad = 1;
1734 break;
1735 }
1736 } /* parse loop */
1737
1738 /*
1739 * Did we find everything and does it add up correctly?
1740 */
1741 if (!fBad && fBadBeforeMissing)
1742 {
1743 InfoMsg(2, "bad cache file (no end)\n");
1744 fBad = 1;
1745 }
1746 else
1747 {
1748 fBadBeforeMissing = fBad;
1749 if ( !fBad
1750 && ( !pEntry->Old.papszArgvCompile
1751 || !pEntry->Old.pszObjName
1752 || !pEntry->Old.pszCppName
1753 || kOCSumIsEmpty(&pEntry->Old.SumHead)))
1754 fBad = 1;
1755 if (!fBad)
1756 for (i = 0; i < pEntry->Old.cArgvCompile; i++)
1757 if ((fBad = !pEntry->Old.papszArgvCompile[i]))
1758 break;
1759 if (!fBad)
1760 {
1761 KOCSUM Sum;
1762 kOCEntryCalcArgvSum(pEntry, (const char * const *)pEntry->Old.papszArgvCompile,
1763 pEntry->Old.cArgvCompile, pEntry->Old.pszObjName, &Sum);
1764 fBad = !kOCSumIsEqual(&pEntry->Old.SumCompArgv, &Sum);
1765 }
1766 if (fBad)
1767 InfoMsg(2, "bad cache file (%s)\n", fBadBeforeMissing ? g_szLine : "missing stuff");
1768 else if (ferror(pFile))
1769 {
1770 InfoMsg(2, "cache file read error\n");
1771 fBad = 1;
1772 }
1773
1774 /*
1775 * Verify the existance of the object file.
1776 */
1777 if (!fBad)
1778 {
1779 struct stat st;
1780 char *pszPath = MakePathFromDirAndFile(pEntry->Old.pszObjName, pEntry->pszDir);
1781 if (stat(pszPath, &st) != 0)
1782 {
1783 InfoMsg(2, "failed to stat object file: %s\n", strerror(errno));
1784 fBad = 1;
1785 }
1786 else
1787 {
1788 /** @todo verify size and the timestamp. */
1789 }
1790 }
1791 }
1792 pEntry->fNeedCompiling = fBad;
1793 }
1794 fclose(pFile);
1795 }
1796 else
1797 {
1798 InfoMsg(2, "no cache file\n");
1799 pEntry->fNeedCompiling = 1;
1800 }
1801}
1802
1803
1804/**
1805 * Writes the cache file.
1806 *
1807 * @param pEntry The entry to write.
1808 */
1809static void kOCEntryWrite(PKOCENTRY pEntry)
1810{
1811 FILE *pFile;
1812 PCKOCSUM pSum;
1813 unsigned i;
1814
1815 InfoMsg(4, "writing cache entry '%s'...\n", pEntry->pszName);
1816 pFile = FOpenFileInDir(pEntry->pszName, pEntry->pszDir, "wb");
1817 if (!pFile)
1818 FatalDie("Failed to open '%s' in '%s': %s\n",
1819 pEntry->pszName, pEntry->pszDir, strerror(errno));
1820
1821#define CHECK_LEN(expr) \
1822 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)
1823
1824 fprintf(pFile, "magic=kObjCacheEntry-v0.1.1\n");
1825 CHECK_LEN(fprintf(pFile, "target=%s\n", pEntry->New.pszTarget ? pEntry->New.pszTarget : pEntry->Old.pszTarget));
1826 CHECK_LEN(fprintf(pFile, "key=%lu\n", (unsigned long)pEntry->uKey));
1827 CHECK_LEN(fprintf(pFile, "obj=%s\n", pEntry->New.pszObjName ? pEntry->New.pszObjName : pEntry->Old.pszObjName));
1828 CHECK_LEN(fprintf(pFile, "cpp=%s\n", pEntry->New.pszCppName ? pEntry->New.pszCppName : pEntry->Old.pszCppName));
1829 CHECK_LEN(fprintf(pFile, "cpp-size=%lu\n", pEntry->New.pszCppName ? pEntry->New.cbCpp : pEntry->Old.cbCpp));
1830 CHECK_LEN(fprintf(pFile, "cpp-ms=%lu\n", pEntry->New.pszCppName ? pEntry->New.cMsCpp : pEntry->Old.cMsCpp));
1831 CHECK_LEN(fprintf(pFile, "cc-ms=%lu\n", pEntry->New.pszCppName ? pEntry->New.cMsCompile : pEntry->Old.cMsCompile));
1832
1833 if (!kOCSumIsEmpty(&pEntry->New.SumCompArgv))
1834 {
1835 CHECK_LEN(fprintf(pFile, "cc-argc=%u\n", pEntry->New.cArgvCompile));
1836 for (i = 0; i < pEntry->New.cArgvCompile; i++)
1837 CHECK_LEN(fprintf(pFile, "cc-argv-#%u=%s\n", i, pEntry->New.papszArgvCompile[i]));
1838 fprintf(pFile, "cc-argv-sum=");
1839 kOCSumFPrintf(&pEntry->New.SumCompArgv, pFile);
1840 }
1841 else
1842 {
1843 CHECK_LEN(fprintf(pFile, "cc-argc=%u\n", pEntry->Old.cArgvCompile));
1844 for (i = 0; i < pEntry->Old.cArgvCompile; i++)
1845 CHECK_LEN(fprintf(pFile, "cc-argv-#%u=%s\n", i, pEntry->Old.papszArgvCompile[i]));
1846 fprintf(pFile, "cc-argv-sum=");
1847 kOCSumFPrintf(&pEntry->Old.SumCompArgv, pFile);
1848 }
1849
1850
1851 for (pSum = !kOCSumIsEmpty(&pEntry->New.SumHead) ? &pEntry->New.SumHead : &pEntry->Old.SumHead;
1852 pSum;
1853 pSum = pSum->pNext)
1854 {
1855 fprintf(pFile, "cpp-sum=");
1856 kOCSumFPrintf(pSum, pFile);
1857 }
1858
1859 fprintf(pFile, "the-end=fine\n");
1860
1861#undef CHECK_LEN
1862
1863 /*
1864 * Flush the file and check for errors.
1865 * On failure delete the file so we won't be seeing any invalid
1866 * files the next time or upset make with new timestamps.
1867 */
1868 errno = 0;
1869 if ( fflush(pFile) < 0
1870 || ferror(pFile))
1871 {
1872 int iErr = errno;
1873 fclose(pFile);
1874 UnlinkFileInDir(pEntry->pszName, pEntry->pszDir);
1875 FatalDie("Stream error occured while writing '%s' in '%s': %s\n",
1876 pEntry->pszName, pEntry->pszDir, strerror(iErr));
1877 }
1878 fclose(pFile);
1879}
1880
1881
1882/**
1883 * Checks that the read cache entry is valid.
1884 * It sets fNeedCompiling if it isn't.
1885 *
1886 * @returns 1 valid, 0 invalid.
1887 * @param pEntry The cache entry.
1888 */
1889static int kOCEntryCheck(PKOCENTRY pEntry)
1890{
1891 return !pEntry->fNeedCompiling;
1892}
1893
1894
1895/**
1896 * Sets the object name and compares it with the old name if present.
1897 *
1898 * @param pEntry The cache entry.
1899 * @param pszObjName The new object name.
1900 */
1901static void kOCEntrySetCompileObjName(PKOCENTRY pEntry, const char *pszObjName)
1902{
1903 assert(!pEntry->New.pszObjName);
1904 pEntry->New.pszObjName = CalcRelativeName(pszObjName, pEntry->pszDir);
1905
1906 if ( !pEntry->fNeedCompiling
1907 && ( !pEntry->Old.pszObjName
1908 || strcmp(pEntry->New.pszObjName, pEntry->Old.pszObjName)))
1909 {
1910 InfoMsg(2, "object file name differs\n");
1911 pEntry->fNeedCompiling = 1;
1912 }
1913
1914 if ( !pEntry->fNeedCompiling
1915 && !DoesFileInDirExist(pEntry->New.pszObjName, pEntry->pszDir))
1916 {
1917 InfoMsg(2, "object file doesn't exist\n");
1918 pEntry->fNeedCompiling = 1;
1919 }
1920}
1921
1922
1923/**
1924 * Set the new compiler args, calc their checksum, and comparing them with any old ones.
1925 *
1926 * @param pEntry The cache entry.
1927 * @param papszArgvCompile The new argument vector for compilation.
1928 * @param cArgvCompile The number of arguments in the vector.
1929 *
1930 * @remark Must call kOCEntrySetCompileObjName before this function!
1931 */
1932static void kOCEntrySetCompileArgv(PKOCENTRY pEntry, const char * const *papszArgvCompile, unsigned cArgvCompile)
1933{
1934 unsigned i;
1935
1936 /* call me only once! */
1937 assert(!pEntry->New.cArgvCompile);
1938 /* call kOCEntrySetCompilerObjName first! */
1939 assert(pEntry->New.pszObjName);
1940
1941 /*
1942 * Copy the argument vector and calculate the checksum.
1943 */
1944 pEntry->New.cArgvCompile = cArgvCompile;
1945 pEntry->New.papszArgvCompile = xmalloc((cArgvCompile + 1) * sizeof(pEntry->New.papszArgvCompile[0]));
1946 for (i = 0; i < cArgvCompile; i++)
1947 pEntry->New.papszArgvCompile[i] = xstrdup(papszArgvCompile[i]);
1948 pEntry->New.papszArgvCompile[i] = NULL; /* for exev/spawnv */
1949
1950 kOCEntryCalcArgvSum(pEntry, papszArgvCompile, cArgvCompile, pEntry->New.pszObjName, &pEntry->New.SumCompArgv);
1951 kOCSumInfo(&pEntry->New.SumCompArgv, 4, "comp-argv");
1952
1953 /*
1954 * Compare with the old argument vector.
1955 */
1956 if ( !pEntry->fNeedCompiling
1957 && !kOCSumIsEqual(&pEntry->New.SumCompArgv, &pEntry->Old.SumCompArgv))
1958 {
1959 InfoMsg(2, "compiler args differs\n");
1960 pEntry->fNeedCompiling = 1;
1961 }
1962}
1963
1964
1965/**
1966 * Sets the arch/os target and compares it with the old name if present.
1967 *
1968 * @param pEntry The cache entry.
1969 * @param pszObjName The new object name.
1970 */
1971static void kOCEntrySetTarget(PKOCENTRY pEntry, const char *pszTarget)
1972{
1973 assert(!pEntry->New.pszTarget);
1974 pEntry->New.pszTarget = xstrdup(pszTarget);
1975
1976 if ( !pEntry->fNeedCompiling
1977 && ( !pEntry->Old.pszTarget
1978 || strcmp(pEntry->New.pszTarget, pEntry->Old.pszTarget)))
1979 {
1980 InfoMsg(2, "target differs\n");
1981 pEntry->fNeedCompiling = 1;
1982 }
1983}
1984
1985
1986/**
1987 * Sets the preprocessor output filename. We don't generally care if this
1988 * matches the old name or not.
1989 *
1990 * @param pEntry The cache entry.
1991 * @param pszCppName The preprocessor output filename.
1992 */
1993static void kOCEntrySetCppName(PKOCENTRY pEntry, const char *pszCppName)
1994{
1995 assert(!pEntry->New.pszCppName);
1996 pEntry->New.pszCppName = CalcRelativeName(pszCppName, pEntry->pszDir);
1997}
1998
1999
2000/**
2001 * Sets the piped mode of the preprocessor and compiler.
2002 *
2003 * @param pEntry The cache entry.
2004 * @param fRedirPreCompStdOut Whether the preprocessor is in piped mode.
2005 * @param fRedirCompileStdIn Whether the compiler is in piped mode.
2006 * @param pszNmPipeCompile The name of the named pipe to use to feed
2007 * the microsoft compiler.
2008 */
2009static void kOCEntrySetPipedMode(PKOCENTRY pEntry, int fRedirPreCompStdOut, int fRedirCompileStdIn,
2010 const char *pszNmPipeCompile)
2011{
2012 pEntry->fPipedPreComp = fRedirPreCompStdOut;
2013 pEntry->fPipedCompile = fRedirCompileStdIn || pszNmPipeCompile;
2014 pEntry->pszNmPipeCompile = xstrdup(pszNmPipeCompile);
2015}
2016
2017
2018/**
2019 * Sets the dependency file.
2020 *
2021 * @param pEntry The cache entry.
2022 * @param pszMakeDepFilename The dependency filename.
2023 * @param fMakeDepFixCase Whether to fix the case of dependency files.
2024 * @param fMakeDepQuiet Whether to be quiet about the dependencies.
2025 * @param fMakeDepGenStubs Whether to generate stubs.
2026 */
2027static void kOCEntrySetDepFilename(PKOCENTRY pEntry, const char *pszMakeDepFilename,
2028 int fMakeDepFixCase, int fMakeDepQuiet, int fMakeDepGenStubs)
2029{
2030 pEntry->pszMakeDepFilename = xstrdup(pszMakeDepFilename);
2031 pEntry->fMakeDepFixCase = fMakeDepFixCase;
2032 pEntry->fMakeDepQuiet = fMakeDepQuiet;
2033 pEntry->fMakeDepGenStubs = fMakeDepGenStubs;
2034}
2035
2036
2037/**
2038 * Spawns a child in a synchronous fashion.
2039 * Terminating on failure.
2040 *
2041 * @param papszArgv Argument vector. The cArgv element is NULL.
2042 * @param pcMs The cache entry member use for time keeping. This
2043 * will be set to the current timestamp.
2044 * @param cArgv The number of arguments in the vector.
2045 * @param pszMsg Which operation this is, for use in messages.
2046 * @param pszStdOut Where to redirect standard out.
2047 */
2048static void kOCEntrySpawn(PCKOCENTRY pEntry, uint32_t *pcMs, const char * const *papszArgv, unsigned cArgv,
2049 const char *pszMsg, const char *pszStdOut)
2050{
2051#if defined(__OS2__) || defined(__WIN__)
2052 intptr_t rc;
2053 int fdStdOut = -1;
2054 if (pszStdOut)
2055 {
2056 int fdReDir;
2057 fdStdOut = dup(STDOUT_FILENO);
2058 close(STDOUT_FILENO);
2059 fdReDir = open(pszStdOut, O_CREAT | O_TRUNC | O_WRONLY, 0666);
2060 if (fdReDir < 0)
2061 FatalDie("%s - failed to create stdout redirection file '%s': %s\n",
2062 pszMsg, pszStdOut, strerror(errno));
2063
2064 if (fdReDir != STDOUT_FILENO)
2065 {
2066 if (dup2(fdReDir, STDOUT_FILENO) < 0)
2067 FatalDie("%s - dup2 failed: %s\n", pszMsg, strerror(errno));
2068 close(fdReDir);
2069 }
2070 }
2071
2072 *pcMs = NowMs();
2073 errno = 0;
2074# ifdef __WIN__
2075 rc = quoted_spawnvp(_P_WAIT, papszArgv[0], papszArgv);
2076# else
2077 rc = _spawnvp(_P_WAIT, papszArgv[0], papszArgv);
2078# endif
2079 *pcMs = NowMs() - *pcMs;
2080 if (rc < 0)
2081 FatalDie("%s - _spawnvp failed (rc=0x%p): %s\n", pszMsg, rc, strerror(errno));
2082 if (rc > 0)
2083 FatalDie("%s - failed rc=%d\n", pszMsg, (int)rc);
2084 if (fdStdOut != -1)
2085 {
2086 close(STDOUT_FILENO);
2087 fdStdOut = dup2(fdStdOut, STDOUT_FILENO);
2088 close(fdStdOut);
2089 }
2090
2091#else
2092 int iStatus;
2093 pid_t pidWait;
2094 pid_t pid;
2095
2096 *pcMs = NowMs();
2097 pid = fork();
2098 if (!pid)
2099 {
2100 if (pszStdOut)
2101 {
2102 int fdReDir;
2103
2104 close(STDOUT_FILENO);
2105 fdReDir = open(pszStdOut, O_CREAT | O_TRUNC | O_WRONLY, 0666);
2106 if (fdReDir < 0)
2107 FatalDie("%s - failed to create stdout redirection file '%s': %s\n",
2108 pszMsg, pszStdOut, strerror(errno));
2109 if (fdReDir != STDOUT_FILENO)
2110 {
2111 if (dup2(fdReDir, STDOUT_FILENO) < 0)
2112 FatalDie("%s - dup2 failed: %s\n", pszMsg, strerror(errno));
2113 close(fdReDir);
2114 }
2115 }
2116
2117 execvp(papszArgv[0], (char **)papszArgv);
2118 FatalDie("%s - execvp failed: %s\n",
2119 pszMsg, strerror(errno));
2120 }
2121 if (pid == -1)
2122 FatalDie("%s - fork() failed: %s\n", pszMsg, strerror(errno));
2123
2124 pidWait = waitpid(pid, &iStatus, 0);
2125 while (pidWait < 0 && errno == EINTR)
2126 pidWait = waitpid(pid, &iStatus, 0);
2127 *pcMs = NowMs() - *pcMs;
2128 if (pidWait != pid)
2129 FatalDie("%s - waitpid failed rc=%d: %s\n",
2130 pszMsg, pidWait, strerror(errno));
2131 if (!WIFEXITED(iStatus))
2132 FatalDie("%s - abended (iStatus=%#x)\n", pszMsg, iStatus);
2133 if (WEXITSTATUS(iStatus))
2134 FatalDie("%s - failed with rc %d\n", pszMsg, WEXITSTATUS(iStatus));
2135#endif
2136
2137 (void)pEntry; (void)cArgv;
2138}
2139
2140
2141/**
2142 * Spawns child with optional redirection of stdin and stdout.
2143 *
2144 * @param pEntry The cache entry.
2145 * @param pcMs The cache entry member use for time keeping. This
2146 * will be set to the current timestamp.
2147 * @param papszArgv Argument vector. The cArgv element is NULL.
2148 * @param cArgv The number of arguments in the vector.
2149 * @param fdStdIn Child stdin, -1 if it should inherit our stdin. Will be closed.
2150 * @param fdStdOut Child stdout, -1 if it should inherit our stdout. Will be closed.
2151 * @param pszMsg Message to start the info/error messages with.
2152 */
2153static pid_t kOCEntrySpawnChild(PCKOCENTRY pEntry, uint32_t *pcMs, const char * const *papszArgv, unsigned cArgv,
2154 int fdStdIn, int fdStdOut, const char *pszMsg)
2155{
2156 pid_t pid;
2157 int fdSavedStdOut = -1;
2158 int fdSavedStdIn = -1;
2159
2160 /*
2161 * Setup redirection.
2162 */
2163 if (fdStdOut != -1 && fdStdOut != STDOUT_FILENO)
2164 {
2165 fdSavedStdOut = dup(STDOUT_FILENO);
2166 if (dup2(fdStdOut, STDOUT_FILENO) < 0)
2167 FatalDie("%s - dup2(,1) failed: %s\n", pszMsg, strerror(errno));
2168 close(fdStdOut);
2169#ifndef __WIN__
2170 fcntl(fdSavedStdOut, F_SETFD, FD_CLOEXEC);
2171#endif
2172 }
2173 if (fdStdIn != -1 && fdStdIn != STDIN_FILENO)
2174 {
2175 fdSavedStdIn = dup(STDIN_FILENO);
2176 if (dup2(fdStdIn, STDIN_FILENO) < 0)
2177 FatalDie("%s - dup2(,0) failed: %s\n", pszMsg, strerror(errno));
2178 close(fdStdIn);
2179#ifndef __WIN__
2180 fcntl(fdSavedStdIn, F_SETFD, FD_CLOEXEC);
2181#endif
2182 }
2183
2184 /*
2185 * Create the child process.
2186 */
2187 *pcMs = NowMs();
2188#if defined(__OS2__) || defined(__WIN__)
2189 errno = 0;
2190# ifdef __WIN__
2191 pid = quoted_spawnvp(_P_NOWAIT, papszArgv[0], papszArgv);
2192# else
2193 pid = _spawnvp(_P_NOWAIT, papszArgv[0], papszArgv);
2194# endif
2195 if (pid == -1)
2196 FatalDie("preprocess - _spawnvp failed: %s\n", strerror(errno));
2197
2198#else
2199 pid = fork();
2200 if (!pid)
2201 {
2202 execvp(papszArgv[0], (char **)papszArgv);
2203 FatalDie("preprocess - execvp failed: %s\n", strerror(errno));
2204 }
2205 if (pid == -1)
2206 FatalDie("preprocess - fork() failed: %s\n", strerror(errno));
2207#endif
2208
2209 /*
2210 * Restore stdout & stdin.
2211 */
2212 if (fdSavedStdIn != -1)
2213 {
2214 close(STDIN_FILENO);
2215 dup2(fdStdOut, STDIN_FILENO);
2216 close(fdSavedStdIn);
2217 }
2218 if (fdSavedStdOut != -1)
2219 {
2220 close(STDOUT_FILENO);
2221 dup2(fdSavedStdOut, STDOUT_FILENO);
2222 close(fdSavedStdOut);
2223 }
2224
2225 InfoMsg(3, "%s - spawned %ld\n", pszMsg, (long)pid);
2226 (void)cArgv;
2227 (void)pEntry;
2228 return pid;
2229}
2230
2231
2232/**
2233 * Waits for a child and exits fatally if the child failed in any way.
2234 *
2235 * @param pEntry The cache entry.
2236 * @param pcMs The millisecond timestamp that should be convert to
2237 * elapsed time.
2238 * @param pid The child to wait for.
2239 * @param pszMsg Message to start the info/error messages with.
2240 */
2241static void kOCEntryWaitChild(PCKOCENTRY pEntry, uint32_t *pcMs, pid_t pid, const char *pszMsg)
2242{
2243 int iStatus = -1;
2244 pid_t pidWait;
2245 InfoMsg(3, "%s - wait-child %ld\n", pszMsg, (long)pid);
2246
2247#ifdef __WIN__
2248 pidWait = _cwait(&iStatus, pid, _WAIT_CHILD);
2249 *pcMs = NowMs() - *pcMs;
2250 if (pidWait == -1)
2251 FatalDie("%s - waitpid failed: %s\n", pszMsg, strerror(errno));
2252 if (iStatus)
2253 FatalDie("%s - failed with rc %d\n", pszMsg, iStatus);
2254#else
2255 pidWait = waitpid(pid, &iStatus, 0);
2256 while (pidWait < 0 && errno == EINTR)
2257 pidWait = waitpid(pid, &iStatus, 0);
2258 *pcMs = NowMs() - *pcMs;
2259 if (pidWait != pid)
2260 FatalDie("%s - waitpid failed rc=%d: %s\n", pidWait, strerror(errno));
2261 if (!WIFEXITED(iStatus))
2262 FatalDie("%s - abended (iStatus=%#x)\n", pszMsg, iStatus);
2263 if (WEXITSTATUS(iStatus))
2264 FatalDie("%s - failed with rc %d\n", pszMsg, WEXITSTATUS(iStatus));
2265#endif
2266 (void)pEntry;
2267}
2268
2269
2270/**
2271 * Creates a pipe for setting up redirected stdin/stdout.
2272 *
2273 * @param pEntry The cache entry.
2274 * @param paFDs Where to store the two file descriptors.
2275 * @param pszMsg The operation message for info/error messages.
2276 * @param pszPipeName The pipe name if it is supposed to be named. (Windows only.)
2277 */
2278static void kOCEntryCreatePipe(PKOCENTRY pEntry, int *paFDs, const char *pszPipeName, const char *pszMsg)
2279{
2280 paFDs[0] = paFDs[1] = -1;
2281#if defined(__WIN__)
2282 if (pszPipeName)
2283 {
2284 HANDLE hPipe = CreateNamedPipeA(pszPipeName,
2285 /*PIPE_ACCESS_OUTBOUND*/ PIPE_ACCESS_DUPLEX,
2286 PIPE_READMODE_BYTE | PIPE_WAIT,
2287 10 /* nMaxInstances */,
2288 0x10000 /* nOutBuffer */,
2289 0x10000 /* nInBuffer */,
2290 NMPWAIT_WAIT_FOREVER,
2291 NULL /* pSecurityAttributes */);
2292
2293 if (hPipe == INVALID_HANDLE_VALUE)
2294 FatalDie("%s - CreateNamedPipe(%s) failed: %d\n", pszMsg, pszPipeName, GetLastError());
2295
2296 paFDs[1 /* write */] = _open_osfhandle((intptr_t)hPipe, _O_WRONLY | _O_TEXT | _O_NOINHERIT);
2297 if (paFDs[1 /* write */] == -1)
2298 FatalDie("%s - _open_osfhandle failed: %d\n", pszMsg, strerror(errno));
2299 }
2300 else if ( _pipe(paFDs, 256*1024, _O_NOINHERIT | _O_BINARY) < 0
2301 && _pipe(paFDs, 0, _O_NOINHERIT | _O_BINARY) < 0)
2302#else
2303 if (pipe(paFDs) < 0)
2304#endif
2305 FatalDie("%s - pipe failed: %s\n", pszMsg, strerror(errno));
2306#if !defined(__WIN__)
2307 fcntl(paFDs[0], F_SETFD, FD_CLOEXEC);
2308 fcntl(paFDs[1], F_SETFD, FD_CLOEXEC);
2309#endif
2310
2311 (void)pEntry;
2312}
2313
2314
2315/**
2316 * Spawns a child that produces output to stdout.
2317 *
2318 * @param papszArgv Argument vector. The cArgv element is NULL.
2319 * @param cArgv The number of arguments in the vector.
2320 * @param pszMsg The operation message for info/error messages.
2321 * @param pfnConsumer Pointer to a consumer callback function that is responsible
2322 * for servicing the child output and closing the pipe.
2323 */
2324static void kOCEntrySpawnProducer(PKOCENTRY pEntry, const char * const *papszArgv, unsigned cArgv, const char *pszMsg,
2325 void (*pfnConsumer)(PKOCENTRY, int))
2326{
2327 int fds[2];
2328 pid_t pid;
2329
2330 kOCEntryCreatePipe(pEntry, fds, NULL, pszMsg);
2331 pid = kOCEntrySpawnChild(pEntry, &pEntry->New.cMsCpp, papszArgv, cArgv, -1, fds[1 /* write */], pszMsg);
2332
2333 pfnConsumer(pEntry, fds[0 /* read */]);
2334
2335 kOCEntryWaitChild(pEntry, &pEntry->New.cMsCpp, pid, pszMsg);
2336}
2337
2338
2339/**
2340 * Spawns a child that consumes input on stdin or via a named pipe.
2341 *
2342 * @param papszArgv Argument vector. The cArgv element is NULL.
2343 * @param cArgv The number of arguments in the vector.
2344 * @param pszMsg The operation message for info/error messages.
2345 * @param pfnProducer Pointer to a producer callback function that is responsible
2346 * for serving the child input and closing the pipe.
2347 */
2348static void kOCEntrySpawnConsumer(PKOCENTRY pEntry, const char * const *papszArgv, unsigned cArgv, const char *pszMsg,
2349 void (*pfnProducer)(PKOCENTRY, int))
2350{
2351 int fds[2];
2352 pid_t pid;
2353
2354 kOCEntryCreatePipe(pEntry, fds, pEntry->pszNmPipeCompile, pszMsg);
2355 pid = kOCEntrySpawnChild(pEntry, &pEntry->New.cMsCompile, papszArgv, cArgv, fds[0 /* read */], -1, pszMsg);
2356#ifdef __WIN__
2357 if (pEntry->pszNmPipeCompile && !ConnectNamedPipe((HANDLE)_get_osfhandle(fds[1 /* write */]), NULL))
2358 FatalDie("compile - ConnectNamedPipe failed: %d\n", GetLastError());
2359#endif
2360
2361 pfnProducer(pEntry, fds[1 /* write */]);
2362
2363 kOCEntryWaitChild(pEntry, &pEntry->New.cMsCompile, pid, pszMsg);
2364}
2365
2366
2367/**
2368 * Spawns two child processes, one producing output and one consuming.
2369 * Terminating on failure.
2370 *
2371 * @param papszArgv Argument vector. The cArgv element is NULL.
2372 * @param cArgv The number of arguments in the vector.
2373 * @param pszMsg The operation message for info/error messages.
2374 * @param pfnConsumer Pointer to a consumer callback function that is responsible
2375 * for servicing the child output and closing the pipe.
2376 */
2377static void kOCEntrySpawnTee(PKOCENTRY pEntry, const char * const *papszProdArgv, unsigned cProdArgv,
2378 const char * const *papszConsArgv, unsigned cConsArgv,
2379 const char *pszMsg, void (*pfnTeeConsumer)(PKOCENTRY, int, int))
2380{
2381 int fds[2];
2382 int fdIn, fdOut;
2383 pid_t pidProducer, pidConsumer;
2384
2385 /*
2386 * The producer.
2387 */
2388 kOCEntryCreatePipe(pEntry, fds, NULL, pszMsg);
2389 pidConsumer = kOCEntrySpawnChild(pEntry, &pEntry->New.cMsCpp, papszProdArgv, cProdArgv, -1, fds[1 /* write */], pszMsg);
2390 fdIn = fds[0 /* read */];
2391
2392 /*
2393 * The consumer.
2394 */
2395 kOCEntryCreatePipe(pEntry, fds, pEntry->pszNmPipeCompile, pszMsg);
2396 pidProducer = kOCEntrySpawnChild(pEntry, &pEntry->New.cMsCompile, papszConsArgv, cConsArgv, fds[0 /* read */], -1, pszMsg);
2397 fdOut = fds[1 /* write */];
2398
2399 /*
2400 * Hand it on to the tee consumer.
2401 */
2402 pfnTeeConsumer(pEntry, fdIn, fdOut);
2403
2404 /*
2405 * Reap the children.
2406 */
2407 kOCEntryWaitChild(pEntry, &pEntry->New.cMsCpp, pidProducer, pszMsg);
2408 kOCEntryWaitChild(pEntry, &pEntry->New.cMsCompile, pidConsumer, pszMsg);
2409}
2410
2411
2412/**
2413 * Reads the output from the preprocessor.
2414 *
2415 * @param pEntry The cache entry. New.cbCpp and New.pszCppMapping will be updated.
2416 * @param pWhich Specifies what to read (old/new).
2417 * @param fNonFatal Whether failure is fatal or not.
2418 */
2419static int kOCEntryReadCppOutput(PKOCENTRY pEntry, struct KOCENTRYDATA *pWhich, int fNonFatal)
2420{
2421 pWhich->pszCppMapping = ReadFileInDir(pWhich->pszCppName, pEntry->pszDir, &pWhich->cbCpp);
2422 if (!pWhich->pszCppMapping)
2423 {
2424 if (!fNonFatal)
2425 FatalDie("failed to open/read '%s' in '%s': %s\n",
2426 pWhich->pszCppName, pEntry->pszDir, strerror(errno));
2427 InfoMsg(2, "failed to open/read '%s' in '%s': %s\n",
2428 pWhich->pszCppName, pEntry->pszDir, strerror(errno));
2429 return -1;
2430 }
2431
2432 InfoMsg(3, "preprocessed file is %lu bytes long\n", (unsigned long)pWhich->cbCpp);
2433 return 0;
2434}
2435
2436
2437/**
2438 * Worker for kOCEntryPreProcess and calculates the checksum of
2439 * the preprocessor output.
2440 *
2441 * @param pEntry The cache entry. NewSum will be updated.
2442 */
2443static void kOCEntryCalcChecksum(PKOCENTRY pEntry)
2444{
2445 KOCSUMCTX Ctx;
2446 kOCSumInitWithCtx(&pEntry->New.SumHead, &Ctx);
2447 kOCSumUpdate(&pEntry->New.SumHead, &Ctx, pEntry->New.pszCppMapping, pEntry->New.cbCpp);
2448 kOCSumFinalize(&pEntry->New.SumHead, &Ctx);
2449 kOCSumInfo(&pEntry->New.SumHead, 4, "cpp (file)");
2450}
2451
2452
2453/**
2454 * This consumes the preprocessor output and checksums it.
2455 *
2456 * @param pEntry The cache entry.
2457 * @param fdIn The preprocessor output pipe.
2458 * @param fdOut The compiler input pipe, -1 if no compiler.
2459 */
2460static void kOCEntryPreProcessConsumer(PKOCENTRY pEntry, int fdIn)
2461{
2462 KOCSUMCTX Ctx;
2463 long cbLeft;
2464 long cbAlloc;
2465 char *psz;
2466
2467 kOCSumInitWithCtx(&pEntry->New.SumHead, &Ctx);
2468 cbAlloc = pEntry->Old.cbCpp ? ((long)pEntry->Old.cbCpp + 4*1024*1024 + 4096) & ~(4*1024*1024 - 1) : 4*1024*1024;
2469 cbLeft = cbAlloc;
2470 pEntry->New.pszCppMapping = psz = xmalloc(cbAlloc);
2471 for (;;)
2472 {
2473 /*
2474 * Read data from the pipe.
2475 */
2476 long cbRead = read(fdIn, psz, cbLeft - 1);
2477 if (!cbRead)
2478 break;
2479 if (cbRead < 0)
2480 {
2481 if (errno == EINTR)
2482 continue;
2483 FatalDie("PreProcess - read(%d,,%ld) failed: %s\n",
2484 fdIn, (long)cbLeft, strerror(errno));
2485 }
2486
2487 /*
2488 * Process the data.
2489 */
2490 psz[cbRead] = '\0';
2491 kOCSumUpdate(&pEntry->New.SumHead, &Ctx, psz, cbRead);
2492 if (pEntry->pszMakeDepFilename)
2493 kOCDepConsumer(&pEntry->DepState, psz, cbRead);
2494
2495 /*
2496 * Advance.
2497 */
2498 psz += cbRead;
2499 cbLeft -= cbRead;
2500 if (cbLeft <= 1)
2501 {
2502 size_t off = psz - pEntry->New.pszCppMapping;
2503 cbLeft += 4*1024*1024;
2504 cbAlloc += 4*1024*1024;
2505 pEntry->New.pszCppMapping = xrealloc(pEntry->New.pszCppMapping, cbAlloc);
2506 psz = pEntry->New.pszCppMapping + off;
2507 }
2508 }
2509
2510 close(fdIn);
2511 pEntry->New.cbCpp = cbAlloc - cbLeft;
2512 kOCSumFinalize(&pEntry->New.SumHead, &Ctx);
2513 kOCSumInfo(&pEntry->New.SumHead, 4, "cpp (pipe)");
2514}
2515
2516
2517
2518
2519/**
2520 * Run the preprocessor and calculate the checksum of the output.
2521 *
2522 * @param pEntry The cache entry.
2523 * @param papszArgvPreComp The argument vector for executing preprocessor.
2524 * The cArgvPreComp'th argument must be NULL.
2525 * @param cArgvPreComp The number of arguments.
2526 */
2527static void kOCEntryPreProcess(PKOCENTRY pEntry, const char * const *papszArgvPreComp, unsigned cArgvPreComp)
2528{
2529 /*
2530 * If we're executing the preprocessor in piped mode, it's relatively simple.
2531 */
2532 if (pEntry->fPipedPreComp)
2533 kOCEntrySpawnProducer(pEntry, papszArgvPreComp, cArgvPreComp, "preprocess",
2534 kOCEntryPreProcessConsumer);
2535 else
2536 {
2537 /*
2538 * Rename the old preprocessed output to '-old' so the preprocessor won't
2539 * overwrite it when we execute it.
2540 */
2541 if ( pEntry->Old.pszCppName
2542 && DoesFileInDirExist(pEntry->Old.pszCppName, pEntry->pszDir))
2543 {
2544 size_t cch = strlen(pEntry->Old.pszCppName);
2545 char *psz = xmalloc(cch + sizeof("-old"));
2546 memcpy(psz, pEntry->Old.pszCppName, cch);
2547 memcpy(psz + cch, "-old", sizeof("-old"));
2548
2549 InfoMsg(3, "renaming '%s' to '%s' in '%s'\n", pEntry->Old.pszCppName, psz, pEntry->pszDir);
2550 UnlinkFileInDir(psz, pEntry->pszDir);
2551 if (RenameFileInDir(pEntry->Old.pszCppName, psz, pEntry->pszDir))
2552 FatalDie("failed to rename '%s' -> '%s' in '%s': %s\n",
2553 pEntry->Old.pszCppName, psz, pEntry->pszDir, strerror(errno));
2554 free(pEntry->Old.pszCppName);
2555 pEntry->Old.pszCppName = psz;
2556 }
2557
2558 /*
2559 * Precompile it and calculate the checksum on the output.
2560 */
2561 InfoMsg(3, "precompiling -> '%s'...\n", pEntry->New.pszCppName);
2562 kOCEntrySpawn(pEntry, &pEntry->New.cMsCpp, papszArgvPreComp, cArgvPreComp, "preprocess", NULL);
2563 kOCEntryReadCppOutput(pEntry, &pEntry->New, 0 /* fatal */);
2564 kOCEntryCalcChecksum(pEntry);
2565 if (pEntry->pszMakeDepFilename)
2566 kOCDepConsumer(&pEntry->DepState, pEntry->New.pszCppMapping, pEntry->New.cbCpp);
2567 }
2568
2569 if (pEntry->pszMakeDepFilename)
2570 kOCDepWriteToFile(&pEntry->DepState, pEntry->pszMakeDepFilename, pEntry->New.pszObjName, pEntry->pszDir,
2571 pEntry->fMakeDepFixCase, pEntry->fMakeDepQuiet, pEntry->fMakeDepGenStubs);
2572}
2573
2574
2575/**
2576 * Worker function for kOCEntryTeeConsumer and kOCEntryCompileIt that
2577 * writes the preprocessor output to disk.
2578 *
2579 * @param pEntry The cache entry.
2580 * @param fFreeIt Whether we can free it after writing it or not.
2581 */
2582static void kOCEntryWriteCppOutput(PKOCENTRY pEntry, int fFreeIt)
2583{
2584 /*
2585 * Remove old files.
2586 */
2587 if (pEntry->Old.pszCppName)
2588 UnlinkFileInDir(pEntry->Old.pszCppName, pEntry->pszDir);
2589 if (pEntry->New.pszCppName)
2590 UnlinkFileInDir(pEntry->New.pszCppName, pEntry->pszDir);
2591
2592 /*
2593 * Write it to disk if we've got a file name.
2594 */
2595 if (pEntry->New.pszCppName)
2596 {
2597 long cbLeft;
2598 char *psz;
2599 int fd = OpenFileInDir(pEntry->New.pszCppName, pEntry->pszDir,
2600 O_WRONLY | O_CREAT | O_TRUNC | O_BINARY, 0666);
2601 if (fd == -1)
2602 FatalDie("Failed to create '%s' in '%s': %s\n",
2603 pEntry->New.pszCppName, pEntry->pszDir, strerror(errno));
2604 psz = pEntry->New.pszCppMapping;
2605 cbLeft = (long)pEntry->New.cbCpp;
2606 while (cbLeft > 0)
2607 {
2608 long cbWritten = write(fd, psz, cbLeft);
2609 if (cbWritten < 0)
2610 {
2611 int iErr = errno;
2612 if (iErr == EINTR)
2613 continue;
2614 close(fd);
2615 UnlinkFileInDir(pEntry->New.pszCppName, pEntry->pszDir);
2616 FatalDie("error writing '%s' in '%s': %s\n",
2617 pEntry->New.pszCppName, pEntry->pszDir, strerror(iErr));
2618 }
2619
2620 psz += cbWritten;
2621 cbLeft -= cbWritten;
2622 }
2623 close(fd);
2624 }
2625
2626 /*
2627 * Free it.
2628 */
2629 if (fFreeIt)
2630 {
2631 free(pEntry->New.pszCppMapping);
2632 pEntry->New.pszCppMapping = NULL;
2633 }
2634}
2635
2636
2637/**
2638 * kOCEntrySpawnConsumer callback that passes the preprocessor output to the
2639 * compiler and writes it to the disk (latter only when necesary).
2640 *
2641 * @param pEntry The cache entry.
2642 * @param fdOut The pipe handle connected to the childs stdin.
2643 */
2644static void kOCEntryCompileProducer(PKOCENTRY pEntry, int fdOut)
2645{
2646 const char *psz = pEntry->New.pszCppMapping;
2647 long cbLeft = (long)pEntry->New.cbCpp;
2648 while (cbLeft > 0)
2649 {
2650 long cbWritten = write(fdOut, psz, cbLeft);
2651 if (cbWritten < 0)
2652 {
2653 if (errno == EINTR)
2654 continue;
2655#ifdef __WIN__ /* HACK */
2656 if ( errno == EINVAL
2657 && pEntry->pszNmPipeCompile
2658 && DisconnectNamedPipe((HANDLE)_get_osfhandle(fdOut))
2659 && ConnectNamedPipe((HANDLE)_get_osfhandle(fdOut), NULL))
2660 {
2661 psz = pEntry->New.pszCppMapping;
2662 cbLeft = (long)pEntry->New.cbCpp;
2663 }
2664 FatalDie("compile - write(%d,,%ld) failed: %s - _doserrno=%d\n", fdOut, cbLeft, strerror(errno), _doserrno);
2665#else
2666 FatalDie("compile - write(%d,,%ld) failed: %s\n", fdOut, cbLeft, strerror(errno));
2667#endif
2668 }
2669 psz += cbWritten;
2670 cbLeft -= cbWritten;
2671 }
2672 close(fdOut);
2673
2674 if (pEntry->fPipedPreComp)
2675 kOCEntryWriteCppOutput(pEntry, 1 /* free it */);
2676}
2677
2678
2679/**
2680 * Does the actual compiling.
2681 *
2682 * @param pEntry The cache entry.
2683 */
2684static void kOCEntryCompileIt(PKOCENTRY pEntry)
2685{
2686 /*
2687 * Delete the object files and free old cpp output that's no longer needed.
2688 */
2689 if (pEntry->Old.pszObjName)
2690 UnlinkFileInDir(pEntry->Old.pszObjName, pEntry->pszDir);
2691 UnlinkFileInDir(pEntry->New.pszObjName, pEntry->pszDir);
2692
2693 free(pEntry->Old.pszCppMapping);
2694 pEntry->Old.pszCppMapping = NULL;
2695 if (!pEntry->fPipedPreComp && !pEntry->fPipedCompile)
2696 {
2697 free(pEntry->New.pszCppMapping);
2698 pEntry->New.pszCppMapping = NULL;
2699 }
2700
2701 /*
2702 * Do the (re-)compile job.
2703 */
2704 if (pEntry->fPipedCompile)
2705 {
2706 if ( !pEntry->fPipedPreComp
2707 && !pEntry->New.pszCppMapping)
2708 kOCEntryReadCppOutput(pEntry, &pEntry->New, 0 /* fatal */);
2709 InfoMsg(3, "compiling -> '%s'...\n", pEntry->New.pszObjName);
2710 kOCEntrySpawnConsumer(pEntry, (const char * const *)pEntry->New.papszArgvCompile,
2711 pEntry->New.cArgvCompile, "compile", kOCEntryCompileProducer);
2712 }
2713 else
2714 {
2715 if (pEntry->fPipedPreComp)
2716 kOCEntryWriteCppOutput(pEntry, 1 /* free it */);
2717 InfoMsg(3, "compiling -> '%s'...\n", pEntry->New.pszObjName);
2718 kOCEntrySpawn(pEntry, &pEntry->New.cMsCompile, (const char * const *)pEntry->New.papszArgvCompile,
2719 pEntry->New.cArgvCompile, "compile", NULL);
2720 }
2721}
2722
2723
2724/**
2725 * kOCEntrySpawnTee callback that works sort of like 'tee'.
2726 *
2727 * It will calculate the preprocessed output checksum and
2728 * write it to disk while the compiler is busy compiling it.
2729 *
2730 * @param pEntry The cache entry.
2731 * @param fdIn The input handle (connected to the preprocessor).
2732 * @param fdOut The output handle (connected to the compiler).
2733 */
2734static void kOCEntryTeeConsumer(PKOCENTRY pEntry, int fdIn, int fdOut)
2735{
2736#ifdef __WIN__
2737 unsigned fConnectedToCompiler = fdOut == -1 || pEntry->pszNmPipeCompile == NULL;
2738#endif
2739 KOCSUMCTX Ctx;
2740 long cbLeft;
2741 long cbAlloc;
2742 char *psz;
2743
2744 kOCSumInitWithCtx(&pEntry->New.SumHead, &Ctx);
2745 cbAlloc = pEntry->Old.cbCpp ? ((long)pEntry->Old.cbCpp + 4*1024*1024 + 4096) & ~(4*1024*1024 - 1) : 4*1024*1024;
2746 cbLeft = cbAlloc;
2747 pEntry->New.pszCppMapping = psz = xmalloc(cbAlloc);
2748 InfoMsg(3, "preprocessor|compile - starting passhtru...\n");
2749 for (;;)
2750 {
2751 /*
2752 * Read data from the pipe.
2753 */
2754 long cbRead = read(fdIn, psz, cbLeft - 1);
2755 if (!cbRead)
2756 break;
2757 if (cbRead < 0)
2758 {
2759 if (errno == EINTR)
2760 continue;
2761 FatalDie("preprocess|compile - read(%d,,%ld) failed: %s\n",
2762 fdIn, (long)cbLeft, strerror(errno));
2763 }
2764 InfoMsg(3, "preprocessor|compile - read %d\n", cbRead);
2765
2766 /*
2767 * Process the data.
2768 */
2769 psz[cbRead] = '\0';
2770 kOCSumUpdate(&pEntry->New.SumHead, &Ctx, psz, cbRead);
2771 if (pEntry->pszMakeDepFilename)
2772 kOCDepConsumer(&pEntry->DepState, psz, cbRead);
2773
2774#ifdef __WIN__
2775 if ( !fConnectedToCompiler
2776 && !(fConnectedToCompiler = ConnectNamedPipe((HANDLE)_get_osfhandle(fdOut), NULL)))
2777 FatalDie("preprocess|compile - ConnectNamedPipe failed: %d\n", GetLastError());
2778#endif
2779 do
2780 {
2781 long cbWritten = write(fdOut, psz, cbRead);
2782 if (cbWritten < 0)
2783 {
2784 if (errno == EINTR)
2785 continue;
2786 FatalDie("preprocess|compile - write(%d,,%ld) failed: %s\n", fdOut, cbRead, strerror(errno));
2787 }
2788 psz += cbWritten;
2789 cbRead -= cbWritten;
2790 cbLeft -= cbWritten;
2791 } while (cbRead > 0);
2792
2793 /*
2794 * Expand the buffer?
2795 */
2796 if (cbLeft <= 1)
2797 {
2798 size_t off = psz - pEntry->New.pszCppMapping;
2799 cbLeft = 4*1024*1024;
2800 cbAlloc += cbLeft;
2801 pEntry->New.pszCppMapping = xrealloc(pEntry->New.pszCppMapping, cbAlloc);
2802 psz = pEntry->New.pszCppMapping + off;
2803 }
2804 }
2805 InfoMsg(3, "preprocessor|compile - done passhtru\n");
2806
2807 close(fdIn);
2808 close(fdOut);
2809 pEntry->New.cbCpp = cbAlloc - cbLeft;
2810 kOCSumFinalize(&pEntry->New.SumHead, &Ctx);
2811 kOCSumInfo(&pEntry->New.SumHead, 4, "cpp (tee)");
2812
2813 /*
2814 * Write the preprocessor output to disk and free the memory it
2815 * occupies while the compiler is busy compiling.
2816 */
2817 kOCEntryWriteCppOutput(pEntry, 1 /* free it */);
2818}
2819
2820
2821/**
2822 * Performs pre-compile and compile in one go (typical clean build scenario).
2823 *
2824 * @param pEntry The cache entry.
2825 * @param papszArgvPreComp The argument vector for executing preprocessor.
2826 * The cArgvPreComp'th argument must be NULL.
2827 * @param cArgvPreComp The number of arguments.
2828 */
2829static void kOCEntryPreProcessAndCompile(PKOCENTRY pEntry, const char * const *papszArgvPreComp, unsigned cArgvPreComp)
2830{
2831 if ( pEntry->fPipedCompile
2832 && pEntry->fPipedPreComp)
2833 {
2834 /*
2835 * Clean up old stuff first.
2836 */
2837 if (pEntry->Old.pszObjName)
2838 UnlinkFileInDir(pEntry->Old.pszObjName, pEntry->pszDir);
2839 if (pEntry->New.pszObjName)
2840 UnlinkFileInDir(pEntry->New.pszObjName, pEntry->pszDir);
2841 if (pEntry->Old.pszCppName)
2842 UnlinkFileInDir(pEntry->Old.pszCppName, pEntry->pszDir);
2843 if (pEntry->New.pszCppName)
2844 UnlinkFileInDir(pEntry->New.pszCppName, pEntry->pszDir);
2845
2846 /*
2847 * Do the actual compile and write the preprocessor output to disk.
2848 */
2849 kOCEntrySpawnTee(pEntry, papszArgvPreComp, cArgvPreComp,
2850 (const char * const *)pEntry->New.papszArgvCompile, pEntry->New.cArgvCompile,
2851 "preprocess|compile", kOCEntryTeeConsumer);
2852 if (pEntry->pszMakeDepFilename)
2853 kOCDepWriteToFile(&pEntry->DepState, pEntry->pszMakeDepFilename, pEntry->New.pszObjName, pEntry->pszDir,
2854 pEntry->fMakeDepFixCase, pEntry->fMakeDepQuiet, pEntry->fMakeDepGenStubs);
2855 }
2856 else
2857 {
2858 kOCEntryPreProcess(pEntry, papszArgvPreComp, cArgvPreComp);
2859 kOCEntryCompileIt(pEntry);
2860 }
2861}
2862
2863
2864/**
2865 * Check whether the string is a '#line' statement.
2866 *
2867 * @returns 1 if it is, 0 if it isn't.
2868 * @param psz The line to examin.
2869 * @parma piLine Where to store the line number.
2870 * @parma ppszFile Where to store the start of the filename.
2871 */
2872static int kOCEntryIsLineStatement(const char *psz, unsigned *piLine, const char **ppszFile)
2873{
2874 unsigned iLine;
2875
2876 /* Expect a hash. */
2877 if (*psz++ != '#')
2878 return 0;
2879
2880 /* Skip blanks between '#' and the line / number */
2881 while (*psz == ' ' || *psz == '\t')
2882 psz++;
2883
2884 /* Skip the 'line' if present. */
2885 if (!strncmp(psz, "line", sizeof("line") - 1))
2886 psz += sizeof("line");
2887
2888 /* Expect a line number now. */
2889 if ((unsigned char)(*psz - '0') > 9)
2890 return 0;
2891 iLine = 0;
2892 do
2893 {
2894 iLine *= 10;
2895 iLine += (*psz - '0');
2896 psz++;
2897 }
2898 while ((unsigned char)(*psz - '0') <= 9);
2899
2900 /* Expect one or more space now. */
2901 if (*psz != ' ' && *psz != '\t')
2902 return 0;
2903 do psz++;
2904 while (*psz == ' ' || *psz == '\t');
2905
2906 /* that's good enough. */
2907 *piLine = iLine;
2908 *ppszFile = psz;
2909 return 1;
2910}
2911
2912
2913/**
2914 * Scan backwards for the previous #line statement.
2915 *
2916 * @returns The filename in the previous statement.
2917 * @param pszStart Where to start.
2918 * @param pszStop Where to stop. Less than pszStart.
2919 * @param piLine The line number count to adjust.
2920 */
2921static const char *kOCEntryFindFileStatement(const char *pszStart, const char *pszStop, unsigned *piLine)
2922{
2923 unsigned iLine = *piLine;
2924 assert(pszStart >= pszStop);
2925 while (pszStart >= pszStop)
2926 {
2927 if (*pszStart == '\n')
2928 iLine++;
2929 else if (*pszStart == '#')
2930 {
2931 unsigned iLineTmp;
2932 const char *pszFile;
2933 const char *psz = pszStart - 1;
2934 while (psz >= pszStop && (*psz == ' ' || *psz =='\t'))
2935 psz--;
2936 if ( (psz < pszStop || *psz == '\n')
2937 && kOCEntryIsLineStatement(pszStart, &iLineTmp, &pszFile))
2938 {
2939 *piLine = iLine + iLineTmp - 1;
2940 return pszFile;
2941 }
2942 }
2943 pszStart--;
2944 }
2945 return NULL;
2946}
2947
2948
2949/**
2950 * Worker for kOCEntryCompareOldAndNewOutput() that compares the
2951 * preprocessed output using a fast but not very good method.
2952 *
2953 * @returns 1 if matching, 0 if not matching.
2954 * @param pEntry The entry containing the names of the files to compare.
2955 * The entry is not updated in any way.
2956 */
2957static int kOCEntryCompareFast(PCKOCENTRY pEntry)
2958{
2959 const char * psz1 = pEntry->New.pszCppMapping;
2960 const char * const pszEnd1 = psz1 + pEntry->New.cbCpp;
2961 const char * psz2 = pEntry->Old.pszCppMapping;
2962 const char * const pszEnd2 = psz2 + pEntry->Old.cbCpp;
2963
2964 assert(*pszEnd1 == '\0');
2965 assert(*pszEnd2 == '\0');
2966
2967 /*
2968 * Iterate block by block and backtrack when we find a difference.
2969 */
2970 for (;;)
2971 {
2972 size_t cch = pszEnd1 - psz1;
2973 if (cch > (size_t)(pszEnd2 - psz2))
2974 cch = pszEnd2 - psz2;
2975 if (cch > 4096)
2976 cch = 4096;
2977 if ( cch
2978 && !memcmp(psz1, psz2, cch))
2979 {
2980 /* no differences */
2981 psz1 += cch;
2982 psz2 += cch;
2983 }
2984 else
2985 {
2986 /*
2987 * Pinpoint the difference exactly and the try find the start
2988 * of that line. Then skip forward until we find something to
2989 * work on that isn't spaces, #line statements or closing curly
2990 * braces.
2991 *
2992 * The closing curly braces are ignored because they are frequently
2993 * found at the end of header files (__END_DECLS) and the worst
2994 * thing that may happen if it isn't one of these braces we're
2995 * ignoring is that the final line in a function block is a little
2996 * bit off in the debug info.
2997 *
2998 * Since we might be skipping a few new empty headers, it is
2999 * possible that we will omit this header from the dependencies
3000 * when using VCC. This might not be a problem, since it seems
3001 * we'll have to use the preprocessor output to generate the deps
3002 * anyway.
3003 */
3004 const char *psz;
3005 const char *pszMismatch1;
3006 const char *pszFile1 = NULL;
3007 unsigned iLine1 = 0;
3008 unsigned cCurlyBraces1 = 0;
3009 const char *pszMismatch2;
3010 const char *pszFile2 = NULL;
3011 unsigned iLine2 = 0;
3012 unsigned cCurlyBraces2 = 0;
3013
3014 /* locate the difference. */
3015 while (cch >= 512 && !memcmp(psz1, psz2, 512))
3016 psz1 += 512, psz2 += 512, cch -= 512;
3017 while (cch >= 64 && !memcmp(psz1, psz2, 64))
3018 psz1 += 64, psz2 += 64, cch -= 64;
3019 while (*psz1 == *psz2 && cch > 0)
3020 psz1++, psz2++, cch--;
3021
3022 /* locate the start of that line. */
3023 psz = psz1;
3024 while ( psz > pEntry->New.pszCppMapping
3025 && psz[-1] != '\n')
3026 psz--;
3027 psz2 -= (psz1 - psz);
3028 pszMismatch2 = psz2;
3029 pszMismatch1 = psz1 = psz;
3030
3031 /* Parse the 1st file line by line. */
3032 while (psz1 < pszEnd1)
3033 {
3034 if (*psz1 == '\n')
3035 {
3036 psz1++;
3037 iLine1++;
3038 }
3039 else
3040 {
3041 psz = psz1;
3042 while (isspace(*psz) && *psz != '\n')
3043 psz++;
3044 if (*psz == '\n')
3045 {
3046 psz1 = psz + 1;
3047 iLine1++;
3048 }
3049 else if (*psz == '#' && kOCEntryIsLineStatement(psz, &iLine1, &pszFile1))
3050 {
3051 psz1 = memchr(psz, '\n', pszEnd1 - psz);
3052 if (!psz1++)
3053 psz1 = pszEnd1;
3054 }
3055 else if (*psz == '}')
3056 {
3057 do psz++;
3058 while (isspace(*psz) && *psz != '\n');
3059 if (*psz == '\n')
3060 iLine1++;
3061 else if (psz != pszEnd1)
3062 break;
3063 cCurlyBraces1++;
3064 psz1 = psz;
3065 }
3066 else if (psz == pszEnd1)
3067 psz1 = psz;
3068 else /* found something that can be compared. */
3069 break;
3070 }
3071 }
3072
3073 /* Ditto for the 2nd file. */
3074 while (psz2 < pszEnd2)
3075 {
3076 if (*psz2 == '\n')
3077 {
3078 psz2++;
3079 iLine2++;
3080 }
3081 else
3082 {
3083 psz = psz2;
3084 while (isspace(*psz) && *psz != '\n')
3085 psz++;
3086 if (*psz == '\n')
3087 {
3088 psz2 = psz + 1;
3089 iLine2++;
3090 }
3091 else if (*psz == '#' && kOCEntryIsLineStatement(psz, &iLine2, &pszFile2))
3092 {
3093 psz2 = memchr(psz, '\n', pszEnd2 - psz);
3094 if (!psz2++)
3095 psz2 = pszEnd2;
3096 }
3097 else if (*psz == '}')
3098 {
3099 do psz++;
3100 while (isspace(*psz) && *psz != '\n');
3101 if (*psz == '\n')
3102 iLine2++;
3103 else if (psz != pszEnd2)
3104 break;
3105 cCurlyBraces2++;
3106 psz2 = psz;
3107 }
3108 else if (psz == pszEnd2)
3109 psz2 = psz;
3110 else /* found something that can be compared. */
3111 break;
3112 }
3113 }
3114
3115 /* Match the number of ignored closing curly braces. */
3116 if (cCurlyBraces1 != cCurlyBraces2)
3117 return 0;
3118
3119 /* Reaching the end of any of them means the return statement can decide. */
3120 if ( psz1 == pszEnd1
3121 || psz2 == pszEnd2)
3122 break;
3123
3124 /* Match the current line. */
3125 psz = memchr(psz1, '\n', pszEnd1 - psz1);
3126 if (!psz++)
3127 psz = pszEnd1;
3128 cch = psz - psz1;
3129 if (psz2 + cch > pszEnd2)
3130 break;
3131 if (memcmp(psz1, psz2, cch))
3132 break;
3133
3134 /* Check that we're at the same location now. */
3135 if (!pszFile1)
3136 pszFile1 = kOCEntryFindFileStatement(pszMismatch1, pEntry->New.pszCppMapping, &iLine1);
3137 if (!pszFile2)
3138 pszFile2 = kOCEntryFindFileStatement(pszMismatch2, pEntry->Old.pszCppMapping, &iLine2);
3139 if (pszFile1 && pszFile2)
3140 {
3141 if (iLine1 != iLine2)
3142 break;
3143 while (*pszFile1 == *pszFile2 && *pszFile1 != '\n' && *pszFile1)
3144 pszFile1++, pszFile2++;
3145 if (*pszFile1 != *pszFile2)
3146 break;
3147 }
3148 else if (pszFile1 || pszFile2)
3149 {
3150 assert(0); /* this shouldn't happen. */
3151 break;
3152 }
3153
3154 /* Advance. We might now have a misaligned buffer, but that's memcmps problem... */
3155 psz1 += cch;
3156 psz2 += cch;
3157 }
3158 }
3159
3160 return psz1 == pszEnd1
3161 && psz2 == pszEnd2;
3162}
3163
3164
3165/**
3166 * Worker for kOCEntryCompileIfNeeded that compares the
3167 * preprocessed output.
3168 *
3169 * @returns 1 if matching, 0 if not matching.
3170 * @param pEntry The entry containing the names of the files to compare.
3171 * This will load the old cpp output (changing pszOldCppName and Old.cbCpp).
3172 */
3173static int kOCEntryCompareOldAndNewOutput(PKOCENTRY pEntry)
3174{
3175 /*
3176 * I may implement a more sophisticated alternative method later... maybe.
3177 */
3178 if (kOCEntryReadCppOutput(pEntry, &pEntry->Old, 1 /* nonfatal */) == -1)
3179 return 0;
3180 /*if ()
3181 return kOCEntryCompareBest(pEntry);*/
3182 return kOCEntryCompareFast(pEntry);
3183}
3184
3185
3186/**
3187 * Check if re-compilation is required.
3188 * This sets the fNeedCompile flag.
3189 *
3190 * @param pEntry The cache entry.
3191 */
3192static void kOCEntryCalcRecompile(PKOCENTRY pEntry)
3193{
3194 if (pEntry->fNeedCompiling)
3195 return;
3196
3197 /*
3198 * Check if the preprocessor output differ in any significant way?
3199 */
3200 if (!kOCSumHasEqualInChain(&pEntry->Old.SumHead, &pEntry->New.SumHead))
3201 {
3202 InfoMsg(2, "no checksum match - comparing output\n");
3203 if (!kOCEntryCompareOldAndNewOutput(pEntry))
3204 pEntry->fNeedCompiling = 1;
3205 else
3206 kOCSumAddChain(&pEntry->New.SumHead, &pEntry->Old.SumHead);
3207 }
3208}
3209
3210
3211/**
3212 * Does this cache entry need compiling or what?
3213 *
3214 * @returns 1 if it does, 0 if it doesn't.
3215 * @param pEntry The cache entry in question.
3216 */
3217static int kOCEntryNeedsCompiling(PCKOCENTRY pEntry)
3218{
3219 return pEntry->fNeedCompiling;
3220}
3221
3222
3223/**
3224 * Worker function for kOCEntryCopy.
3225 *
3226 * @param pEntry The entry we're coping to, which pszTo is relative to.
3227 * @param pszTo The destination.
3228 * @param pszFrom The source. This path will be freed.
3229 */
3230static void kOCEntryCopyFile(PCKOCENTRY pEntry, const char *pszTo, char *pszSrc)
3231{
3232 char *pszDst = MakePathFromDirAndFile(pszTo, pEntry->pszDir);
3233 char *pszBuf = xmalloc(256 * 1024);
3234 char *psz;
3235 int fdSrc;
3236 int fdDst;
3237
3238 /*
3239 * Open the files.
3240 */
3241 fdSrc = open(pszSrc, O_RDONLY | O_BINARY);
3242 if (fdSrc == -1)
3243 FatalDie("failed to open '%s': %s\n", pszSrc, strerror(errno));
3244
3245 unlink(pszDst);
3246 fdDst = open(pszDst, O_WRONLY | O_CREAT | O_TRUNC | O_BINARY, 0666);
3247 if (fdDst == -1)
3248 FatalDie("failed to create '%s': %s\n", pszDst, strerror(errno));
3249
3250 /*
3251 * Copy them.
3252 */
3253 for (;;)
3254 {
3255 /* read a chunk. */
3256 long cbRead = read(fdSrc, pszBuf, 256*1024);
3257 if (cbRead < 0)
3258 {
3259 if (errno == EINTR)
3260 continue;
3261 FatalDie("read '%s' failed: %s\n", pszSrc, strerror(errno));
3262 }
3263 if (!cbRead)
3264 break; /* eof */
3265
3266 /* write the chunk. */
3267 psz = pszBuf;
3268 do
3269 {
3270 long cbWritten = write(fdDst, psz, cbRead);
3271 if (cbWritten < 0)
3272 {
3273 if (errno == EINTR)
3274 continue;
3275 FatalDie("write '%s' failed: %s\n", pszSrc, strerror(errno));
3276 }
3277 psz += cbWritten;
3278 cbRead -= cbWritten;
3279 } while (cbRead > 0);
3280 }
3281
3282 /* cleanup */
3283 if (close(fdDst) != 0)
3284 FatalDie("closing '%s' failed: %s\n", pszDst, strerror(errno));
3285 close(fdSrc);
3286 free(pszBuf);
3287 free(pszDst);
3288 free(pszSrc);
3289}
3290
3291
3292/**
3293 * Copies the object (and whatever else) from one cache entry to another.
3294 *
3295 * This is called when a matching cache entry has been found and we don't
3296 * need to recompile anything.
3297 *
3298 * @param pEntry The entry to copy to.
3299 * @param pFrom The entry to copy from.
3300 */
3301static void kOCEntryCopy(PKOCENTRY pEntry, PCKOCENTRY pFrom)
3302{
3303 kOCEntryCopyFile(pEntry, pEntry->New.pszObjName,
3304 MakePathFromDirAndFile(pFrom->New.pszObjName
3305 ? pFrom->New.pszObjName : pFrom->Old.pszObjName,
3306 pFrom->pszDir));
3307}
3308
3309
3310/**
3311 * Gets the absolute path to the cache entry.
3312 *
3313 * @returns absolute path to the cache entry.
3314 * @param pEntry The cache entry in question.
3315 */
3316static const char *kOCEntryAbsPath(PCKOCENTRY pEntry)
3317{
3318 return pEntry->pszAbsPath;
3319}
3320
3321
3322
3323
3324
3325
3326/**
3327 * Digest of one cache entry.
3328 *
3329 * This contains all the information required to find a matching
3330 * cache entry without having to open each of the files.
3331 */
3332typedef struct KOCDIGEST
3333{
3334 /** The relative path to the entry. Optional if pszAbsPath is set. */
3335 char *pszRelPath;
3336 /** The absolute path to the entry. Optional if pszRelPath is set. */
3337 char *pszAbsPath;
3338 /** The target os/arch identifier. */
3339 char *pszTarget;
3340 /** A unique number assigned to the entry when it's (re)-inserted
3341 * into the cache. This is used for simple consitency checking. */
3342 uint32_t uKey;
3343 /** The checksum of the compile argument vector. */
3344 KOCSUM SumCompArgv;
3345 /** The list of preprocessor output checksums that's . */
3346 KOCSUM SumHead;
3347} KOCDIGEST;
3348/** Pointer to a file digest. */
3349typedef KOCDIGEST *PKOCDIGEST;
3350/** Pointer to a const file digest. */
3351typedef KOCDIGEST *PCKOCDIGEST;
3352
3353
3354/**
3355 * Initializes the specified digest.
3356 *
3357 * @param pDigest The digest.
3358 */
3359static void kOCDigestInit(PKOCDIGEST pDigest)
3360{
3361 memset(pDigest, 0, sizeof(*pDigest));
3362 kOCSumInit(&pDigest->SumHead);
3363}
3364
3365
3366/**
3367 * Initializes the digest for the specified entry.
3368 *
3369 * @param pDigest The (uninitialized) digest.
3370 * @param pEntry The entry.
3371 */
3372static void kOCDigestInitFromEntry(PKOCDIGEST pDigest, PCKOCENTRY pEntry)
3373{
3374 kOCDigestInit(pDigest);
3375
3376 pDigest->uKey = pEntry->uKey;
3377 pDigest->pszTarget = xstrdup(pEntry->New.pszTarget ? pEntry->New.pszTarget : pEntry->Old.pszTarget);
3378
3379 kOCSumInit(&pDigest->SumCompArgv);
3380 if (!kOCSumIsEmpty(&pEntry->New.SumCompArgv))
3381 kOCSumAdd(&pDigest->SumCompArgv, &pEntry->New.SumCompArgv);
3382 else
3383 kOCSumAdd(&pDigest->SumCompArgv, &pEntry->Old.SumCompArgv);
3384
3385 kOCSumInit(&pDigest->SumHead);
3386 if (!kOCSumIsEmpty(&pEntry->New.SumHead))
3387 kOCSumAddChain(&pDigest->SumHead, &pEntry->New.SumHead);
3388 else
3389 kOCSumAddChain(&pDigest->SumHead, &pEntry->Old.SumHead);
3390
3391 /** @todo implement selective relative path support. */
3392 pDigest->pszRelPath = NULL;
3393 pDigest->pszAbsPath = xstrdup(kOCEntryAbsPath(pEntry));
3394}
3395
3396
3397/**
3398 * Purges a digest, freeing all resources and returning
3399 * it to the initial state.
3400 *
3401 * @param pDigest The digest.
3402 */
3403static void kOCDigestPurge(PKOCDIGEST pDigest)
3404{
3405 free(pDigest->pszRelPath);
3406 free(pDigest->pszAbsPath);
3407 free(pDigest->pszTarget);
3408 pDigest->pszTarget = pDigest->pszAbsPath = pDigest->pszRelPath = NULL;
3409 pDigest->uKey = 0;
3410 kOCSumDeleteChain(&pDigest->SumCompArgv);
3411 kOCSumDeleteChain(&pDigest->SumHead);
3412}
3413
3414
3415/**
3416 * Returns the absolute path to the entry, calculating
3417 * the path if necessary.
3418 *
3419 * @returns absolute path.
3420 * @param pDigest The digest.
3421 * @param pszDir The cache directory that it might be relative to.
3422 */
3423static const char *kOCDigestAbsPath(PCKOCDIGEST pDigest, const char *pszDir)
3424{
3425 if (!pDigest->pszAbsPath)
3426 {
3427 char *pszPath = MakePathFromDirAndFile(pDigest->pszRelPath, pszDir);
3428 ((PKOCDIGEST)pDigest)->pszAbsPath = AbsPath(pszPath);
3429 free(pszPath);
3430 }
3431 return pDigest->pszAbsPath;
3432}
3433
3434
3435/**
3436 * Checks that the digest matches the
3437 *
3438 * @returns 1 if valid, 0 if invalid in some way.
3439 *
3440 * @param pDigest The digest to validate.
3441 * @param pEntry What to validate it against.
3442 */
3443static int kOCDigestIsValid(PCKOCDIGEST pDigest, PCKOCENTRY pEntry)
3444{
3445 PCKOCSUM pSum;
3446 PCKOCSUM pSumEntry;
3447
3448 if (pDigest->uKey != pEntry->uKey)
3449 return 0;
3450
3451 if (!kOCSumIsEqual(&pDigest->SumCompArgv,
3452 kOCSumIsEmpty(&pEntry->New.SumCompArgv)
3453 ? &pEntry->Old.SumCompArgv : &pEntry->New.SumCompArgv))
3454 return 0;
3455
3456 if (strcmp(pDigest->pszTarget, pEntry->New.pszTarget ? pEntry->New.pszTarget : pEntry->Old.pszTarget))
3457 return 0;
3458
3459 /* match the checksums */
3460 pSumEntry = kOCSumIsEmpty(&pEntry->New.SumHead)
3461 ? &pEntry->Old.SumHead : &pEntry->New.SumHead;
3462 for (pSum = &pDigest->SumHead; pSum; pSum = pSum->pNext)
3463 if (!kOCSumHasEqualInChain(pSumEntry, pSum))
3464 return 0;
3465
3466 return 1;
3467}
3468
3469
3470
3471
3472
3473/**
3474 * The structure for the central cache entry.
3475 */
3476typedef struct KOBJCACHE
3477{
3478 /** The entry name. */
3479 const char *pszName;
3480 /** The dir that relative names in the digest are relative to. */
3481 char *pszDir;
3482 /** The absolute path. */
3483 char *pszAbsPath;
3484
3485 /** The cache file descriptor. */
3486 int fd;
3487 /** The stream associated with fd. */
3488 FILE *pFile;
3489 /** Whether it's currently locked or not. */
3490 unsigned fLocked;
3491 /** Whether the cache file is dirty and needs writing back. */
3492 unsigned fDirty;
3493 /** Whether this is a new cache or not. */
3494 unsigned fNewCache;
3495
3496 /** The cache file generation. */
3497 uint32_t uGeneration;
3498 /** The next valid key. (Determin at load time.) */
3499 uint32_t uNextKey;
3500
3501 /** Number of digests in paDigests. */
3502 unsigned cDigests;
3503 /** Array of digests for the KOCENTRY objects in the cache. */
3504 PKOCDIGEST paDigests;
3505
3506} KOBJCACHE;
3507/** Pointer to a cache. */
3508typedef KOBJCACHE *PKOBJCACHE;
3509/** Pointer to a const cache. */
3510typedef KOBJCACHE const *PCKOBJCACHE;
3511
3512
3513/**
3514 * Creates an empty cache.
3515 *
3516 * This doesn't touch the file system, it just create the data structure.
3517 *
3518 * @returns Pointer to a cache.
3519 * @param pszCacheFile The cache file.
3520 */
3521static PKOBJCACHE kObjCacheCreate(const char *pszCacheFile)
3522{
3523 PKOBJCACHE pCache;
3524 size_t off;
3525
3526 /*
3527 * Allocate an empty entry.
3528 */
3529 pCache = xmallocz(sizeof(*pCache));
3530 pCache->fd = -1;
3531
3532 /*
3533 * Setup the directory and cache file name.
3534 */
3535 pCache->pszAbsPath = AbsPath(pszCacheFile);
3536 pCache->pszName = FindFilenameInPath(pCache->pszAbsPath);
3537 off = pCache->pszName - pCache->pszAbsPath;
3538 if (!off)
3539 FatalDie("Failed to find abs path for '%s'!\n", pszCacheFile);
3540 pCache->pszDir = xmalloc(off);
3541 memcpy(pCache->pszDir, pCache->pszAbsPath, off - 1);
3542 pCache->pszDir[off - 1] = '\0';
3543
3544 return pCache;
3545}
3546
3547
3548/**
3549 * Destroys the cache - closing any open files, freeing up heap memory and such.
3550 *
3551 * @param pCache The cache.
3552 */
3553static void kObjCacheDestroy(PKOBJCACHE pCache)
3554{
3555 if (pCache->pFile)
3556 {
3557 errno = 0;
3558 if (fclose(pCache->pFile) != 0)
3559 FatalMsg("fclose failed: %s\n", strerror(errno));
3560 pCache->pFile = NULL;
3561 pCache->fd = -1;
3562 }
3563 free(pCache->paDigests);
3564 free(pCache->pszAbsPath);
3565 free(pCache->pszDir);
3566 free(pCache);
3567}
3568
3569
3570/**
3571 * Purges the data in the cache object.
3572 *
3573 * @param pCache The cache object.
3574 */
3575static void kObjCachePurge(PKOBJCACHE pCache)
3576{
3577 while (pCache->cDigests > 0)
3578 kOCDigestPurge(&pCache->paDigests[--pCache->cDigests]);
3579 free(pCache->paDigests);
3580 pCache->paDigests = NULL;
3581 pCache->uGeneration = 0;
3582 pCache->uNextKey = 0;
3583}
3584
3585
3586/**
3587 * (Re-)reads the file.
3588 *
3589 * @param pCache The cache to (re)-read.
3590 */
3591static void kObjCacheRead(PKOBJCACHE pCache)
3592{
3593 unsigned i;
3594 char szBuf[8192];
3595 int fBad = 0;
3596
3597 InfoMsg(4, "reading cache file...\n");
3598
3599 /*
3600 * Rewind the file & stream, and associate a temporary buffer
3601 * with the stream to speed up reading.
3602 */
3603 if (lseek(pCache->fd, 0, SEEK_SET) == -1)
3604 FatalDie("lseek(cache-fd) failed: %s\n", strerror(errno));
3605 rewind(pCache->pFile);
3606 if (setvbuf(pCache->pFile, szBuf, _IOFBF, sizeof(szBuf)) != 0)
3607 FatalDie("fdopen(cache-fd,rb) failed: %s\n", strerror(errno));
3608
3609 /*
3610 * Read magic and generation.
3611 */
3612 if ( !fgets(g_szLine, sizeof(g_szLine), pCache->pFile)
3613 || strcmp(g_szLine, "magic=kObjCache-v0.1.0\n"))
3614 {
3615 InfoMsg(2, "bad cache file (magic)\n");
3616 fBad = 1;
3617 }
3618 else if ( !fgets(g_szLine, sizeof(g_szLine), pCache->pFile)
3619 || strncmp(g_szLine, "generation=", sizeof("generation=") - 1))
3620 {
3621 InfoMsg(2, "bad cache file (generation)\n");
3622 fBad = 1;
3623 }
3624 else if ( pCache->uGeneration
3625 && (long)pCache->uGeneration == atol(&g_szLine[sizeof("generation=") - 1]))
3626 {
3627 InfoMsg(3, "drop re-read unmodified cache file\n");
3628 fBad = 0;
3629 }
3630 else
3631 {
3632 int fBadBeforeMissing;
3633
3634 /*
3635 * Read everything (anew).
3636 */
3637 kObjCachePurge(pCache);
3638 do
3639 {
3640 PKOCDIGEST pDigest;
3641 char *pszNl;
3642 char *pszVal;
3643 char *psz;
3644
3645 /* Split the line and drop the trailing newline. */
3646 pszVal = strchr(g_szLine, '=');
3647 if ((fBad = pszVal == NULL))
3648 break;
3649 *pszVal++ = '\0';
3650
3651 pszNl = strchr(pszVal, '\n');
3652 if (pszNl)
3653 *pszNl = '\0';
3654
3655 /* digest '#'? */
3656 psz = strchr(g_szLine, '#');
3657 if (psz)
3658 {
3659 char *pszNext;
3660 i = strtoul(++psz, &pszNext, 0);
3661 if ((fBad = pszNext && *pszNext))
3662 break;
3663 if ((fBad = i >= pCache->cDigests))
3664 break;
3665 pDigest = &pCache->paDigests[i];
3666 *psz = '\0';
3667 }
3668 else
3669 pDigest = NULL;
3670
3671
3672 /* string case on value name. */
3673 if (!strcmp(g_szLine, "sum-#"))
3674 {
3675 KOCSUM Sum;
3676 if ((fBad = kOCSumInitFromString(&Sum, pszVal) != 0))
3677 break;
3678 kOCSumAdd(&pDigest->SumHead, &Sum);
3679 }
3680 else if (!strcmp(g_szLine, "digest-abs-#"))
3681 {
3682 if ((fBad = pDigest->pszAbsPath != NULL))
3683 break;
3684 pDigest->pszAbsPath = xstrdup(pszVal);
3685 }
3686 else if (!strcmp(g_szLine, "digest-rel-#"))
3687 {
3688 if ((fBad = pDigest->pszRelPath != NULL))
3689 break;
3690 pDigest->pszRelPath = xstrdup(pszVal);
3691 }
3692 else if (!strcmp(g_szLine, "key-#"))
3693 {
3694 if ((fBad = pDigest->uKey != 0))
3695 break;
3696 pDigest->uKey = strtoul(pszVal, &psz, 0);
3697 if ((fBad = psz && *psz))
3698 break;
3699 if (pDigest->uKey >= pCache->uNextKey)
3700 pCache->uNextKey = pDigest->uKey + 1;
3701 }
3702 else if (!strcmp(g_szLine, "comp-argv-sum-#"))
3703 {
3704 if ((fBad = !kOCSumIsEmpty(&pDigest->SumCompArgv)))
3705 break;
3706 if ((fBad = kOCSumInitFromString(&pDigest->SumCompArgv, pszVal) != 0))
3707 break;
3708 }
3709 else if (!strcmp(g_szLine, "target-#"))
3710 {
3711 if ((fBad = pDigest->pszTarget != NULL))
3712 break;
3713 pDigest->pszTarget = xstrdup(pszVal);
3714 }
3715 else if (!strcmp(g_szLine, "digests"))
3716 {
3717 if ((fBad = pCache->paDigests != NULL))
3718 break;
3719 pCache->cDigests = strtoul(pszVal, &psz, 0);
3720 if ((fBad = psz && *psz))
3721 break;
3722 i = (pCache->cDigests + 4) & ~3;
3723 pCache->paDigests = xmalloc(i * sizeof(pCache->paDigests[0]));
3724 for (i = 0; i < pCache->cDigests; i++)
3725 kOCDigestInit(&pCache->paDigests[i]);
3726 }
3727 else if (!strcmp(g_szLine, "generation"))
3728 {
3729 if ((fBad = pCache->uGeneration != 0))
3730 break;
3731 pCache->uGeneration = strtoul(pszVal, &psz, 0);
3732 if ((fBad = psz && *psz))
3733 break;
3734 }
3735 else if (!strcmp(g_szLine, "the-end"))
3736 {
3737 fBad = strcmp(pszVal, "fine");
3738 break;
3739 }
3740 else
3741 {
3742 fBad = 1;
3743 break;
3744 }
3745 } while (fgets(g_szLine, sizeof(g_szLine), pCache->pFile));
3746
3747 /*
3748 * Did we find everything?
3749 */
3750 fBadBeforeMissing = fBad;
3751 if ( !fBad
3752 && !pCache->uGeneration)
3753 fBad = 1;
3754 if (!fBad)
3755 for (i = 0; i < pCache->cDigests; i++)
3756 {
3757 if ((fBad = kOCSumIsEmpty(&pCache->paDigests[i].SumCompArgv)))
3758 break;
3759 if ((fBad = kOCSumIsEmpty(&pCache->paDigests[i].SumHead)))
3760 break;
3761 if ((fBad = pCache->paDigests[i].uKey == 0))
3762 break;
3763 if ((fBad = pCache->paDigests[i].pszAbsPath == NULL
3764 && pCache->paDigests[i].pszRelPath == NULL))
3765 break;
3766 if ((fBad = pCache->paDigests[i].pszTarget == NULL))
3767 break;
3768 InfoMsg(4, "digest-%u: %s\n", i, pCache->paDigests[i].pszAbsPath
3769 ? pCache->paDigests[i].pszAbsPath : pCache->paDigests[i].pszRelPath);
3770 }
3771 if (fBad)
3772 InfoMsg(2, "bad cache file (%s)\n", fBadBeforeMissing ? g_szLine : "missing stuff");
3773 else if (ferror(pCache->pFile))
3774 {
3775 InfoMsg(2, "cache file read error\n");
3776 fBad = 1;
3777 }
3778 }
3779 if (fBad)
3780 {
3781 kObjCachePurge(pCache);
3782 pCache->fNewCache = 1;
3783 }
3784
3785 /*
3786 * Disassociate the buffer from the stream changing
3787 * it to non-buffered mode.
3788 */
3789 if (setvbuf(pCache->pFile, NULL, _IONBF, 0) != 0)
3790 FatalDie("setvbuf(,0,,0) failed: %s\n", strerror(errno));
3791}
3792
3793
3794/**
3795 * Re-writes the cache file.
3796 *
3797 * @param pCache The cache to commit and unlock.
3798 */
3799static void kObjCacheWrite(PKOBJCACHE pCache)
3800{
3801 unsigned i;
3802 off_t cb;
3803 char szBuf[8192];
3804 assert(pCache->fLocked);
3805 assert(pCache->fDirty);
3806
3807 /*
3808 * Rewind the file & stream, and associate a temporary buffer
3809 * with the stream to speed up the writing.
3810 */
3811 if (lseek(pCache->fd, 0, SEEK_SET) == -1)
3812 FatalDie("lseek(cache-fd) failed: %s\n", strerror(errno));
3813 rewind(pCache->pFile);
3814 if (setvbuf(pCache->pFile, szBuf, _IOFBF, sizeof(szBuf)) != 0)
3815 FatalDie("setvbuf failed: %s\n", strerror(errno));
3816
3817 /*
3818 * Write the header.
3819 */
3820 pCache->uGeneration++;
3821 fprintf(pCache->pFile,
3822 "magic=kObjCache-v0.1.0\n"
3823 "generation=%d\n"
3824 "digests=%d\n",
3825 pCache->uGeneration,
3826 pCache->cDigests);
3827
3828 /*
3829 * Write the digests.
3830 */
3831 for (i = 0; i < pCache->cDigests; i++)
3832 {
3833 PCKOCDIGEST pDigest = &pCache->paDigests[i];
3834 PKOCSUM pSum;
3835
3836 if (pDigest->pszAbsPath)
3837 fprintf(pCache->pFile, "digest-abs-#%u=%s\n", i, pDigest->pszAbsPath);
3838 if (pDigest->pszRelPath)
3839 fprintf(pCache->pFile, "digest-rel-#%u=%s\n", i, pDigest->pszRelPath);
3840 fprintf(pCache->pFile, "key-#%u=%u\n", i, pDigest->uKey);
3841 fprintf(pCache->pFile, "target-#%u=%s\n", i, pDigest->pszTarget);
3842 fprintf(pCache->pFile, "comp-argv-sum-#%u=", i);
3843 kOCSumFPrintf(&pDigest->SumCompArgv, pCache->pFile);
3844 for (pSum = &pDigest->SumHead; pSum; pSum = pSum->pNext)
3845 {
3846 fprintf(pCache->pFile, "sum-#%u=", i);
3847 kOCSumFPrintf(pSum, pCache->pFile);
3848 }
3849 }
3850
3851 /*
3852 * Close the stream and unlock fhe file.
3853 * (Closing the stream shouldn't close the file handle IIRC...)
3854 */
3855 fprintf(pCache->pFile, "the-end=fine\n");
3856 errno = 0;
3857 if ( fflush(pCache->pFile) < 0
3858 || ferror(pCache->pFile))
3859 {
3860 int iErr = errno;
3861 fclose(pCache->pFile);
3862 UnlinkFileInDir(pCache->pszName, pCache->pszDir);
3863 FatalDie("Stream error occured while writing '%s' in '%s': %s\n",
3864 pCache->pszName, pCache->pszDir, strerror(iErr));
3865 }
3866 if (setvbuf(pCache->pFile, NULL, _IONBF, 0) != 0)
3867 FatalDie("setvbuf(,0,,0) failed: %s\n", strerror(errno));
3868
3869 cb = lseek(pCache->fd, 0, SEEK_CUR);
3870 if (cb == -1)
3871 FatalDie("lseek(cache-file,0,CUR) failed: %s\n", strerror(errno));
3872#if defined(__WIN__)
3873 if (_chsize(pCache->fd, cb) == -1)
3874#else
3875 if (ftruncate(pCache->fd, cb) == -1)
3876#endif
3877 FatalDie("file truncation failed: %s\n", strerror(errno));
3878 InfoMsg(4, "wrote '%s' in '%s', %d bytes\n", pCache->pszName, pCache->pszDir, cb);
3879}
3880
3881
3882/**
3883 * Cleans out all invalid digests.s
3884 *
3885 * This is done periodically from the unlock routine to make
3886 * sure we don't accidentally accumulate stale digests.
3887 *
3888 * @param pCache The cache to chek.
3889 */
3890static void kObjCacheClean(PKOBJCACHE pCache)
3891{
3892 unsigned i = pCache->cDigests;
3893 while (i-- > 0)
3894 {
3895 /*
3896 * Try open it and purge it if it's bad.
3897 * (We don't kill the entry file because that's kmk clean's job.)
3898 */
3899 PCKOCDIGEST pDigest = &pCache->paDigests[i];
3900 PKOCENTRY pEntry = kOCEntryCreate(kOCDigestAbsPath(pDigest, pCache->pszDir));
3901 kOCEntryRead(pEntry);
3902 if ( !kOCEntryCheck(pEntry)
3903 || !kOCDigestIsValid(pDigest, pEntry))
3904 {
3905 unsigned cLeft;
3906 kOCDigestPurge(pDigest);
3907
3908 pCache->cDigests--;
3909 cLeft = pCache->cDigests - i;
3910 if (cLeft)
3911 memmove(pDigest, pDigest + 1, cLeft * sizeof(*pDigest));
3912
3913 pCache->fDirty = 1;
3914 }
3915 kOCEntryDestroy(pEntry);
3916 }
3917}
3918
3919
3920/**
3921 * Locks the cache for exclusive access.
3922 *
3923 * This will open the file if necessary and lock the entire file
3924 * using the best suitable platform API (tricky).
3925 *
3926 * @param pCache The cache to lock.
3927 */
3928static void kObjCacheLock(PKOBJCACHE pCache)
3929{
3930 struct stat st;
3931#if defined(__WIN__)
3932 OVERLAPPED OverLapped;
3933#endif
3934
3935 assert(!pCache->fLocked);
3936
3937 /*
3938 * Open it?
3939 */
3940 if (pCache->fd < 0)
3941 {
3942 pCache->fd = OpenFileInDir(pCache->pszName, pCache->pszDir, O_CREAT | O_RDWR | O_BINARY, 0666);
3943 if (pCache->fd == -1)
3944 {
3945 MakePath(pCache->pszDir);
3946 pCache->fd = OpenFileInDir(pCache->pszName, pCache->pszDir, O_CREAT | O_RDWR | O_BINARY, 0666);
3947 if (pCache->fd == -1)
3948 FatalDie("Failed to create '%s' in '%s': %s\n", pCache->pszName, pCache->pszDir, strerror(errno));
3949 }
3950
3951 pCache->pFile = fdopen(pCache->fd, "r+b");
3952 if (!pCache->pFile)
3953 FatalDie("fdopen failed: %s\n", strerror(errno));
3954 if (setvbuf(pCache->pFile, NULL, _IONBF, 0) != 0)
3955 FatalDie("setvbuf(,0,,0) failed: %s\n", strerror(errno));
3956 }
3957
3958 /*
3959 * Lock it.
3960 */
3961#if defined(__WIN__)
3962 memset(&OverLapped, 0, sizeof(OverLapped));
3963 if (!LockFileEx((HANDLE)_get_osfhandle(pCache->fd), LOCKFILE_EXCLUSIVE_LOCK, 0, ~0, 0, &OverLapped))
3964 FatalDie("Failed to lock the cache file: Windows Error %d\n", GetLastError());
3965#elif defined(__sun__)
3966 {
3967 struct flock fl;
3968 fl.l_whence = 0;
3969 fl.l_start = 0;
3970 fl.l_len = 0;
3971 fl.l_type = F_WRLCK;
3972 if (fcntl(pCache->fd, F_SETLKW, &fl) != 0)
3973 FatalDie("Failed to lock the cache file: %s\n", strerror(errno));
3974 }
3975#else
3976 if (flock(pCache->fd, LOCK_EX) != 0)
3977 FatalDie("Failed to lock the cache file: %s\n", strerror(errno));
3978#endif
3979 pCache->fLocked = 1;
3980
3981 /*
3982 * Check for new cache and read it it's an existing cache.
3983 *
3984 * There is no point in initializing a new cache until we've finished
3985 * compiling and has something to put into it, so we'll leave it as a
3986 * 0 byte file.
3987 */
3988 if (fstat(pCache->fd, &st) == -1)
3989 FatalDie("fstat(cache-fd) failed: %s\n", strerror(errno));
3990 if (st.st_size)
3991 kObjCacheRead(pCache);
3992 else
3993 {
3994 pCache->fNewCache = 1;
3995 InfoMsg(2, "the cache file is empty\n");
3996 }
3997}
3998
3999
4000/**
4001 * Unlocks the cache (without writing anything back).
4002 *
4003 * @param pCache The cache to unlock.
4004 */
4005static void kObjCacheUnlock(PKOBJCACHE pCache)
4006{
4007#if defined(__WIN__)
4008 OVERLAPPED OverLapped;
4009#endif
4010 assert(pCache->fLocked);
4011
4012 /*
4013 * Write it back if it's dirty.
4014 */
4015 if (pCache->fDirty)
4016 {
4017 if ( pCache->cDigests >= 16
4018 && (pCache->uGeneration % 19) == 19)
4019 kObjCacheClean(pCache);
4020 kObjCacheWrite(pCache);
4021 pCache->fDirty = 0;
4022 }
4023
4024 /*
4025 * Lock it.
4026 */
4027#if defined(__WIN__)
4028 memset(&OverLapped, 0, sizeof(OverLapped));
4029 if (!UnlockFileEx((HANDLE)_get_osfhandle(pCache->fd), 0, ~0U, 0, &OverLapped))
4030 FatalDie("Failed to unlock the cache file: Windows Error %d\n", GetLastError());
4031#elif defined(__sun__)
4032 {
4033 struct flock fl;
4034 fl.l_whence = 0;
4035 fl.l_start = 0;
4036 fl.l_len = 0;
4037 fl.l_type = F_UNLCK;
4038 if (fcntl(pCache->fd, F_SETLKW, &fl) != 0)
4039 FatalDie("Failed to lock the cache file: %s\n", strerror(errno));
4040 }
4041#else
4042 if (flock(pCache->fd, LOCK_UN) != 0)
4043 FatalDie("Failed to unlock the cache file: %s\n", strerror(errno));
4044#endif
4045 pCache->fLocked = 0;
4046}
4047
4048
4049/**
4050 * Removes the entry from the cache.
4051 *
4052 * The entry doesn't need to be in the cache.
4053 * The cache entry (file) itself is not touched.
4054 *
4055 * @param pCache The cache.
4056 * @param pEntry The entry.
4057 */
4058static void kObjCacheRemoveEntry(PKOBJCACHE pCache, PCKOCENTRY pEntry)
4059{
4060 unsigned i = pCache->cDigests;
4061 while (i-- > 0)
4062 {
4063 PKOCDIGEST pDigest = &pCache->paDigests[i];
4064 if (ArePathsIdentical(kOCDigestAbsPath(pDigest, pCache->pszDir),
4065 kOCEntryAbsPath(pEntry)))
4066 {
4067 unsigned cLeft;
4068 kOCDigestPurge(pDigest);
4069
4070 pCache->cDigests--;
4071 cLeft = pCache->cDigests - i;
4072 if (cLeft)
4073 memmove(pDigest, pDigest + 1, cLeft * sizeof(*pDigest));
4074
4075 pCache->fDirty = 1;
4076 InfoMsg(3, "removing entry '%s'; %d left.\n", kOCEntryAbsPath(pEntry), pCache->cDigests);
4077 }
4078 }
4079}
4080
4081
4082/**
4083 * Inserts the entry into the cache.
4084 *
4085 * The cache entry (file) itself is not touched by this operation,
4086 * the pEntry object otoh is.
4087 *
4088 * @param pCache The cache.
4089 * @param pEntry The entry.
4090 */
4091static void kObjCacheInsertEntry(PKOBJCACHE pCache, PKOCENTRY pEntry)
4092{
4093 unsigned i;
4094
4095 /*
4096 * Find a new key.
4097 */
4098 pEntry->uKey = pCache->uNextKey++;
4099 if (!pEntry->uKey)
4100 pEntry->uKey = pCache->uNextKey++;
4101 i = pCache->cDigests;
4102 while (i-- > 0)
4103 if (pCache->paDigests[i].uKey == pEntry->uKey)
4104 {
4105 pEntry->uKey = pCache->uNextKey++;
4106 if (!pEntry->uKey)
4107 pEntry->uKey = pCache->uNextKey++;
4108 i = pCache->cDigests;
4109 }
4110
4111 /*
4112 * Reallocate the digest array?
4113 */
4114 if ( !(pCache->cDigests & 3)
4115 && (pCache->cDigests || !pCache->paDigests))
4116 pCache->paDigests = xrealloc(pCache->paDigests, sizeof(pCache->paDigests[0]) * (pCache->cDigests + 4));
4117
4118 /*
4119 * Create a new digest.
4120 */
4121 kOCDigestInitFromEntry(&pCache->paDigests[pCache->cDigests], pEntry);
4122 pCache->cDigests++;
4123 InfoMsg(4, "Inserted digest #%u: %s\n", pCache->cDigests - 1, kOCEntryAbsPath(pEntry));
4124
4125 pCache->fDirty = 1;
4126}
4127
4128
4129/**
4130 * Find a matching cache entry.
4131 */
4132static PKOCENTRY kObjCacheFindMatchingEntry(PKOBJCACHE pCache, PCKOCENTRY pEntry)
4133{
4134 unsigned i = pCache->cDigests;
4135
4136 assert(pEntry->fNeedCompiling);
4137 assert(!kOCSumIsEmpty(&pEntry->New.SumCompArgv));
4138 assert(!kOCSumIsEmpty(&pEntry->New.SumHead));
4139
4140 while (i-- > 0)
4141 {
4142 /*
4143 * Matching?
4144 */
4145 PCKOCDIGEST pDigest = &pCache->paDigests[i];
4146 if ( kOCSumIsEqual(&pDigest->SumCompArgv, &pEntry->New.SumCompArgv)
4147 && kOCSumHasEqualInChain(&pDigest->SumHead, &pEntry->New.SumHead))
4148 {
4149 /*
4150 * Try open it.
4151 */
4152 unsigned cLeft;
4153 PKOCENTRY pRetEntry = kOCEntryCreate(kOCDigestAbsPath(pDigest, pCache->pszDir));
4154 kOCEntryRead(pRetEntry);
4155 if ( kOCEntryCheck(pRetEntry)
4156 && kOCDigestIsValid(pDigest, pRetEntry))
4157 return pRetEntry;
4158 kOCEntryDestroy(pRetEntry);
4159
4160 /* bad entry, purge it. */
4161 InfoMsg(3, "removing bad digest '%s'\n", kOCDigestAbsPath(pDigest, pCache->pszDir));
4162 kOCDigestPurge(pDigest);
4163
4164 pCache->cDigests--;
4165 cLeft = pCache->cDigests - i;
4166 if (cLeft)
4167 memmove(pDigest, pDigest + 1, cLeft * sizeof(*pDigest));
4168
4169 pCache->fDirty = 1;
4170 }
4171 }
4172
4173 return NULL;
4174}
4175
4176
4177/**
4178 * Is this a new cache?
4179 *
4180 * @returns 1 if new, 0 if not new.
4181 * @param pEntry The entry.
4182 */
4183static int kObjCacheIsNew(PKOBJCACHE pCache)
4184{
4185 return pCache->fNewCache;
4186}
4187
4188
4189/**
4190 * Prints a syntax error and returns the appropriate exit code
4191 *
4192 * @returns approriate exit code.
4193 * @param pszFormat The syntax error message.
4194 * @param ... Message args.
4195 */
4196static int SyntaxError(const char *pszFormat, ...)
4197{
4198 va_list va;
4199 fprintf(stderr, "kObjCache: syntax error: ");
4200 va_start(va, pszFormat);
4201 vfprintf(stderr, pszFormat, va);
4202 va_end(va);
4203 return 1;
4204}
4205
4206
4207/**
4208 * Prints the usage.
4209 * @returns 0.
4210 */
4211static int usage(FILE *pOut)
4212{
4213 fprintf(pOut,
4214 "syntax: kObjCache [--kObjCache-options] [-v|--verbose]\n"
4215 " < [-c|--cache-file <cache-file>]\n"
4216 " | [-n|--name <name-in-cache>] [[-d|--cache-dir <cache-dir>]] >\n"
4217 " <-f|--file <local-cache-file>>\n"
4218 " <-t|--target <target-name>>\n"
4219 " [-r|--redir-stdout] [-p|--passthru] [--named-pipe-compile <pipename>]\n"
4220 " --kObjCache-cpp <filename> <preprocessor + args>\n"
4221 " --kObjCache-cc <object> <compiler + args>\n"
4222 " [--kObjCache-both [args]]\n"
4223 );
4224 fprintf(pOut,
4225 " [--kObjCache-cpp|--kObjCache-cc [more args]]\n"
4226 " kObjCache <-V|--version>\n"
4227 " kObjCache [-?|/?|-h|/h|--help|/help]\n"
4228 "\n"
4229 "The env.var. KOBJCACHE_DIR sets the default cache diretory (-d).\n"
4230 "The env.var. KOBJCACHE_OPTS allow you to specifie additional options\n"
4231 "without having to mess with the makefiles. These are appended with "
4232 "a --kObjCache-options between them and the command args.\n"
4233 "\n");
4234 return 0;
4235}
4236
4237
4238int main(int argc, char **argv)
4239{
4240 PKOBJCACHE pCache;
4241 PKOCENTRY pEntry;
4242
4243 const char *pszCacheDir = getenv("KOBJCACHE_DIR");
4244 const char *pszCacheName = NULL;
4245 const char *pszCacheFile = NULL;
4246 const char *pszEntryFile = NULL;
4247
4248 const char **papszArgvPreComp = NULL;
4249 unsigned cArgvPreComp = 0;
4250 const char *pszPreCompName = NULL;
4251 int fRedirPreCompStdOut = 0;
4252
4253 const char **papszArgvCompile = NULL;
4254 unsigned cArgvCompile = 0;
4255 const char *pszObjName = NULL;
4256 int fRedirCompileStdIn = 0;
4257 const char *pszNmPipeCompile = NULL;
4258
4259 const char *pszMakeDepFilename = NULL;
4260 int fMakeDepFixCase = 0;
4261 int fMakeDepGenStubs = 0;
4262 int fMakeDepQuiet = 0;
4263
4264 const char *pszTarget = NULL;
4265
4266 enum { kOC_Options, kOC_CppArgv, kOC_CcArgv, kOC_BothArgv } enmMode = kOC_Options;
4267
4268 size_t cch;
4269 char *psz;
4270 int i;
4271
4272 SetErrorPrefix("kObjCache");
4273
4274 /*
4275 * Arguments passed in the environmnet?
4276 */
4277 psz = getenv("KOBJCACHE_OPTS");
4278 if (psz)
4279 AppendArgs(&argc, &argv, psz, "--kObjCache-options");
4280
4281 /*
4282 * Parse the arguments.
4283 */
4284 if (argc <= 1)
4285 return usage(stderr);
4286 for (i = 1; i < argc; i++)
4287 {
4288 if (!strcmp(argv[i], "--kObjCache-cpp"))
4289 {
4290 enmMode = kOC_CppArgv;
4291 if (!pszPreCompName)
4292 {
4293 if (++i >= argc)
4294 return SyntaxError("--kObjCache-cpp requires an object filename!\n");
4295 pszPreCompName = argv[i];
4296 }
4297 }
4298 else if (!strcmp(argv[i], "--kObjCache-cc"))
4299 {
4300 enmMode = kOC_CcArgv;
4301 if (!pszObjName)
4302 {
4303 if (++i >= argc)
4304 return SyntaxError("--kObjCache-cc requires an preprocessor output filename!\n");
4305 pszObjName = argv[i];
4306 }
4307 }
4308 else if (!strcmp(argv[i], "--kObjCache-both"))
4309 enmMode = kOC_BothArgv;
4310 else if (!strcmp(argv[i], "--kObjCache-options"))
4311 enmMode = kOC_Options;
4312 else if (!strcmp(argv[i], "--help"))
4313 return usage(stderr);
4314 else if (enmMode != kOC_Options)
4315 {
4316 if (enmMode == kOC_CppArgv || enmMode == kOC_BothArgv)
4317 {
4318 if (!(cArgvPreComp % 16))
4319 papszArgvPreComp = xrealloc((void *)papszArgvPreComp, (cArgvPreComp + 17) * sizeof(papszArgvPreComp[0]));
4320 papszArgvPreComp[cArgvPreComp++] = argv[i];
4321 papszArgvPreComp[cArgvPreComp] = NULL;
4322 }
4323 if (enmMode == kOC_CcArgv || enmMode == kOC_BothArgv)
4324 {
4325 if (!(cArgvCompile % 16))
4326 papszArgvCompile = xrealloc((void *)papszArgvCompile, (cArgvCompile + 17) * sizeof(papszArgvCompile[0]));
4327 papszArgvCompile[cArgvCompile++] = argv[i];
4328 papszArgvCompile[cArgvCompile] = NULL;
4329 }
4330 }
4331 else if (!strcmp(argv[i], "-f") || !strcmp(argv[i], "--entry-file"))
4332 {
4333 if (i + 1 >= argc)
4334 return SyntaxError("%s requires a cache entry filename!\n", argv[i]);
4335 pszEntryFile = argv[++i];
4336 }
4337 else if (!strcmp(argv[i], "-c") || !strcmp(argv[i], "--cache-file"))
4338 {
4339 if (i + 1 >= argc)
4340 return SyntaxError("%s requires a cache filename!\n", argv[i]);
4341 pszCacheFile = argv[++i];
4342 }
4343 else if (!strcmp(argv[i], "-n") || !strcmp(argv[i], "--name"))
4344 {
4345 if (i + 1 >= argc)
4346 return SyntaxError("%s requires a cache name!\n", argv[i]);
4347 pszCacheName = argv[++i];
4348 }
4349 else if (!strcmp(argv[i], "-d") || !strcmp(argv[i], "--cache-dir"))
4350 {
4351 if (i + 1 >= argc)
4352 return SyntaxError("%s requires a cache directory!\n", argv[i]);
4353 pszCacheDir = argv[++i];
4354 }
4355 else if (!strcmp(argv[i], "-t") || !strcmp(argv[i], "--target"))
4356 {
4357 if (i + 1 >= argc)
4358 return SyntaxError("%s requires a target platform/arch name!\n", argv[i]);
4359 pszTarget = argv[++i];
4360 }
4361 else if (!strcmp(argv[i], "--named-pipe-compile"))
4362 {
4363 if (i + 1 >= argc)
4364 return SyntaxError("%s requires a pipe name!\n", argv[i]);
4365 pszNmPipeCompile = argv[++i];
4366 fRedirCompileStdIn = 0;
4367 }
4368 else if (!strcmp(argv[i], "-m") || !strcmp(argv[i], "--make-dep-file"))
4369 {
4370 if (i + 1 >= argc)
4371 return SyntaxError("%s requires a filename!\n", argv[i]);
4372 pszMakeDepFilename = argv[++i];
4373 }
4374 else if (!strcmp(argv[i], "--make-dep-fix-case"))
4375 fMakeDepFixCase = 1;
4376 else if (!strcmp(argv[i], "--make-dep-gen-stubs"))
4377 fMakeDepGenStubs = 1;
4378 else if (!strcmp(argv[i], "--make-dep-quiet"))
4379 fMakeDepQuiet = 1;
4380 else if (!strcmp(argv[i], "-p") || !strcmp(argv[i], "--passthru"))
4381 fRedirPreCompStdOut = fRedirCompileStdIn = 1;
4382 else if (!strcmp(argv[i], "-r") || !strcmp(argv[i], "--redir-stdout"))
4383 fRedirPreCompStdOut = 1;
4384 else if (!strcmp(argv[i], "-v") || !strcmp(argv[i], "--verbose"))
4385 g_cVerbosityLevel++;
4386 else if (!strcmp(argv[i], "-q") || !strcmp(argv[i], "--quiet"))
4387 g_cVerbosityLevel = 0;
4388 else if (!strcmp(argv[i], "-h") || !strcmp(argv[i], "-?")
4389 || !strcmp(argv[i], "/h") || !strcmp(argv[i], "/?") || !strcmp(argv[i], "/help"))
4390 {
4391 usage(stdout);
4392 return 0;
4393 }
4394 else if (!strcmp(argv[i], "-V") || !strcmp(argv[i], "--version"))
4395 {
4396 printf("kObjCache - kBuild version %d.%d.%d ($Revision: 2613 $)\n"
4397 "Copyright (c) 2007-2012 knut st. osmundsen\n",
4398 KBUILD_VERSION_MAJOR, KBUILD_VERSION_MINOR, KBUILD_VERSION_PATCH);
4399 return 0;
4400 }
4401 else
4402 return SyntaxError("Doesn't grok '%s'!\n", argv[i]);
4403 }
4404 if (!pszEntryFile)
4405 return SyntaxError("No cache entry filename (-f)!\n");
4406 if (!pszTarget)
4407 return SyntaxError("No target name (-t)!\n");
4408 if (!cArgvCompile)
4409 return SyntaxError("No compiler arguments (--kObjCache-cc)!\n");
4410 if (!cArgvPreComp)
4411 return SyntaxError("No preprocessor arguments (--kObjCache-cc)!\n");
4412
4413 /*
4414 * Calc the cache file name.
4415 * It's a bit messy since the extension has to be replaced.
4416 */
4417 if (!pszCacheFile)
4418 {
4419 if (!pszCacheDir)
4420 return SyntaxError("No cache dir (-d / KOBJCACHE_DIR) and no cache filename!\n");
4421 if (!pszCacheName)
4422 {
4423 psz = (char *)FindFilenameInPath(pszEntryFile);
4424 if (!*psz)
4425 return SyntaxError("The cache file (-f) specifies a directory / nothing!\n");
4426 cch = psz - pszEntryFile;
4427 pszCacheName = memcpy(xmalloc(cch + 5), psz, cch + 1);
4428 psz = strrchr(pszCacheName, '.');
4429 if (!psz || psz <= pszCacheName)
4430 psz = (char *)pszCacheName + cch;
4431 memcpy(psz, ".koc", sizeof(".koc") - 1);
4432 }
4433 pszCacheFile = MakePathFromDirAndFile(pszCacheName, pszCacheDir);
4434 }
4435
4436 /*
4437 * Create and initialize the two objects we'll be working on.
4438 *
4439 * We're supposed to be the only ones actually writing to the local file,
4440 * so it's perfectly fine to read it here before we lock it. This simplifies
4441 * the detection of object name and compiler argument changes.
4442 */
4443 SetErrorPrefix("kObjCache - %s", FindFilenameInPath(pszCacheFile));
4444 pCache = kObjCacheCreate(pszCacheFile);
4445
4446 pEntry = kOCEntryCreate(pszEntryFile);
4447 kOCEntryRead(pEntry);
4448 kOCEntrySetCompileObjName(pEntry, pszObjName);
4449 kOCEntrySetCompileArgv(pEntry, papszArgvCompile, cArgvCompile);
4450 kOCEntrySetTarget(pEntry, pszTarget);
4451 kOCEntrySetCppName(pEntry, pszPreCompName);
4452 kOCEntrySetPipedMode(pEntry, fRedirPreCompStdOut, fRedirCompileStdIn, pszNmPipeCompile);
4453 kOCEntrySetDepFilename(pEntry, pszMakeDepFilename, fMakeDepFixCase, fMakeDepQuiet, fMakeDepGenStubs);
4454
4455 /*
4456 * Open (& lock) the two files and do validity checks and such.
4457 */
4458 kObjCacheLock(pCache);
4459 if ( kObjCacheIsNew(pCache)
4460 && kOCEntryNeedsCompiling(pEntry))
4461 {
4462 /*
4463 * Both files are missing/invalid.
4464 * Optimize this path as it is frequently used when making a clean build.
4465 */
4466 kObjCacheUnlock(pCache);
4467 InfoMsg(1, "doing full compile\n");
4468 kOCEntryPreProcessAndCompile(pEntry, papszArgvPreComp, cArgvPreComp);
4469 kObjCacheLock(pCache);
4470 }
4471 else
4472 {
4473 /*
4474 * Do the preprocess (don't need to lock the cache file for this).
4475 */
4476 kObjCacheUnlock(pCache);
4477 kOCEntryPreProcess(pEntry, papszArgvPreComp, cArgvPreComp);
4478
4479 /*
4480 * Check if we need to recompile. If we do, try see if the is a cache entry first.
4481 */
4482 kOCEntryCalcRecompile(pEntry);
4483 if (kOCEntryNeedsCompiling(pEntry))
4484 {
4485 PKOCENTRY pUseEntry;
4486 kObjCacheLock(pCache);
4487 kObjCacheRemoveEntry(pCache, pEntry);
4488 pUseEntry = kObjCacheFindMatchingEntry(pCache, pEntry);
4489 if (pUseEntry)
4490 {
4491 InfoMsg(1, "using cache entry '%s'\n", kOCEntryAbsPath(pUseEntry));
4492 kOCEntryCopy(pEntry, pUseEntry);
4493 kOCEntryDestroy(pUseEntry);
4494 }
4495 else
4496 {
4497 kObjCacheUnlock(pCache);
4498 InfoMsg(1, "recompiling\n");
4499 kOCEntryCompileIt(pEntry);
4500 kObjCacheLock(pCache);
4501 }
4502 }
4503 else
4504 {
4505 InfoMsg(1, "no need to recompile\n");
4506 kObjCacheLock(pCache);
4507 }
4508 }
4509
4510 /*
4511 * Update the cache files.
4512 */
4513 kObjCacheRemoveEntry(pCache, pEntry);
4514 kObjCacheInsertEntry(pCache, pEntry);
4515 kOCEntryWrite(pEntry);
4516 kObjCacheUnlock(pCache);
4517 kObjCacheDestroy(pCache);
4518 return 0;
4519}
4520
4521
4522/** @page kObjCache Benchmarks.
4523 *
4524 * (2007-06-10)
4525 *
4526 * 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):
4527 * real 11m28.811s
4528 * user 13m59.291s
4529 * sys 3m24.590s
4530 *
4531 * 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):
4532 * real 1m26.895s
4533 * user 1m26.971s
4534 * sys 0m32.532s
4535 *
4536 * 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):
4537 * real 1m18.049s
4538 * user 1m20.462s
4539 * sys 0m27.887s
4540 *
4541 * 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):
4542 * real 13m27.751s
4543 * user 18m12.654s
4544 * sys 3m25.170s
4545 *
4546 * 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):
4547 * real 9m9.720s
4548 * user 8m53.005s
4549 * sys 2m13.110s
4550 *
4551 * 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):
4552 * real 10m18.129s
4553 * user 12m52.687s
4554 * sys 2m51.277s
4555 *
4556 * 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):
4557 * real 4m46.147s
4558 * user 5m27.087s
4559 * sys 1m11.775s
4560 *
4561 * 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):
4562 * real 4m17.572s
4563 * user 5m7.450s
4564 * sys 1m3.450s
4565 *
4566 * 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):
4567 * real 12m14.742s
4568 * user 17m11.794s
4569 * sys 2m51.454s
4570 *
4571 * 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):
4572 * real 12m33.821s
4573 * user 17m35.086s
4574 * sys 2m53.312s
4575 *
4576 * Note. The profile build can pick object files from the release build.
4577 * (all with KOBJCACHE_OPTS=-v; which means a bit more output and perhaps a second or two slower.)
4578 */
4579
Note: See TracBrowser for help on using the repository browser.