1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
3 * Copyright (C) 2011 Norbert Thiebaud
7 /* define to activate stats reporting on hash usage*/
8 /* #define HASH_STAT */
10 /* ===============================================
11 * Set-up: defines to identify the system and system related properties
12 * ===============================================
17 #undef CORE_BIG_ENDIAN
18 #define CORE_LITTLE_ENDIAN
20 #define CORE_BIG_ENDIAN
21 #undef CORE_LITTLE_ENDIAN
26 #define CORE_BIG_ENDIAN
27 #undef CORE_LITTLE_ENDIAN
32 #undef CORE_BIG_ENDIAN
33 #define CORE_LITTLE_ENDIAN
34 #endif /* Def _MSC_VER */
36 #if defined(__linux) || defined(__FreeBSD_kernel__)
37 #include <sys/param.h>
38 #if __BYTE_ORDER == __LITTLE_ENDIAN
39 #undef CORE_BIG_ENDIAN
40 #define CORE_LITTLE_ENDIAN
41 #else /* !(__BYTE_ORDER == __LITTLE_ENDIAN) */
42 #if __BYTE_ORDER == __BIG_ENDIAN
43 #define CORE_BIG_ENDIAN
44 #undef CORE_LITTLE_ENDIAN
45 #endif /* __BYTE_ORDER == __BIG_ENDIAN */
46 #endif /* !(__BYTE_ORDER == __LITTLE_ENDIAN) */
47 #endif /* Def __linux */
49 #if defined(__OpenBSD__) || defined(__FreeBSD__) || \
50 defined(__NetBSD__) || defined(__DragonFly__)
51 #include <machine/endian.h>
52 #if _BYTE_ORDER == _LITTLE_ENDIAN
53 #undef CORE_BIG_ENDIAN
54 #define CORE_LITTLE_ENDIAN
55 #else /* !(_BYTE_ORDER == _LITTLE_ENDIAN) */
56 #if _BYTE_ORDER == _BIG_ENDIAN
57 #define CORE_BIG_ENDIAN
58 #undef CORE_LITTLE_ENDIAN
59 #endif /* _BYTE_ORDER == _BIG_ENDIAN */
60 #endif /* !(_BYTE_ORDER == _LITTLE_ENDIAN) */
65 #define CORE_BIG_ENDIAN
66 #undef CORE_LITTLE_ENDIAN
67 #else /* Ndef __sparc */
68 #undef CORE_BIG_ENDIAN
69 #define CORE_LITTLE_ENDIAN
70 #endif /* Ndef __sparc */
71 #endif /* Def __sun */
76 #include <sys/types.h>
89 #include <config_options.h>
93 #define FILE_O_RDONLY _O_RDONLY
94 #define FILE_O_BINARY _O_BINARY
95 #define PATHNCMP _strnicmp /* MSVC converts paths to lower-case sometimes? */
96 #define inline __inline
98 #define S_ISREG(mode) (((mode) & _S_IFMT) == (_S_IFREG)) /* MSVC does not have this macro */
99 #else /* not windaube */
100 #define FILE_O_RDONLY O_RDONLY
101 #define FILE_O_BINARY 0
102 #define PATHNCMP strncmp
103 #endif /* not windaube */
112 int internal_boost
= 0;
113 static char* base_dir
;
114 static char* work_dir
;
118 #define clz __builtin_clz
120 static inline int clz(unsigned int value
)
133 static inline unsigned int get_unaligned_uint(const unsigned char* cursor
)
137 memcpy(&result
, cursor
, sizeof(unsigned int));
141 /* ===============================================
142 * memory pool for fast fix-size allocation (non-thread-safe)
143 * ===============================================
147 void* head_free
; /**< head of a linked list of freed element */
148 char* fresh
; /**< top of a memory block to dig new element */
149 char* tail
; /**< to detect end of extent... when fresh pass tail */
150 void* extent
; /**< pointer to the primary extent block */
151 int size_elem
; /**< size of an element. */
152 int primary
; /**< primary allocation in bytes */
153 int secondary
; /**< secondary allocation in bytes */
155 #define POOL_ALIGN_INCREMENT 8 /**< Alignement, must be a power of 2 and of size > to sizeof(void*) */
158 static void* pool_take_extent(struct pool
* pool
, int allocate
)
160 unsigned int size
= 0;
166 /* we already have an extent, so this is a secondary */
169 size
= pool
->secondary
;
174 assert(pool
->primary
);
175 size
= pool
->primary
;
179 extent
= malloc(size
);
182 *(void**)extent
= pool
->extent
;
183 pool
->extent
= extent
;
186 data
= ((char*)extent
) + POOL_ALIGN_INCREMENT
;
187 pool
->fresh
= ((char*)data
) + pool
->size_elem
;
188 pool
->tail
= pool
->fresh
+ (size
- pool
->size_elem
);
192 pool
->fresh
= ((char*)extent
) + POOL_ALIGN_INCREMENT
;
193 pool
->tail
= pool
->fresh
+ (size
- pool
->size_elem
);
200 /* Create a memory pool for fix size objects
201 * this is a simplified implementation that
202 * is _not_ thread safe.
204 static struct pool
* pool_create(int size_elem
, int primary
, int secondary
)
209 assert(secondary
>= 0);
210 assert(size_elem
> 0);
212 pool
= (struct pool
*)calloc(1, sizeof(struct pool
));
213 if(!pool
) return NULL
;
214 /* Adjust the element size so that it be aligned, and so that an element could
215 * at least contain a void*
217 pool
->size_elem
= size_elem
= (size_elem
+ POOL_ALIGN_INCREMENT
- 1) & ~(POOL_ALIGN_INCREMENT
- 1);
219 pool
->primary
= (size_elem
* primary
) + POOL_ALIGN_INCREMENT
;
220 pool
->secondary
= secondary
> 0 ? (size_elem
* secondary
) + POOL_ALIGN_INCREMENT
: 0;
221 pool_take_extent(pool
, FALSE
);
227 static void pool_destroy(struct pool
* pool
)
234 extent
= pool
->extent
;
237 next
= *(void**)extent
;
245 static inline void* pool_alloc(struct pool
* pool
)
249 data
= pool
->head_free
;
252 /* we have no old-freed elem */
253 if(pool
->fresh
<= pool
->tail
)
255 /* pick a slice of the current extent */
256 data
= (void*)pool
->fresh
;
257 pool
->fresh
+= pool
->size_elem
;
261 /* allocate a new extent */
262 data
= pool_take_extent(pool
, TRUE
);
267 /* re-used old freed element by chopipng the head of the free list */
268 pool
->head_free
= *(void**)data
;
275 /* ===============================================
276 * Hash implementation customized to be just tracking
277 * a unique list of string (i.e no data associated
278 * with the key, no need for retrieval, etc..
280 * This is tuned for the particular use-case we have here
281 * measures in tail_build showed that
282 * we can get north of 4000 distinct values stored in a hash
283 * the collision rate is at worse around 2%
284 * the collision needing an expensive memcmp to resolve
285 * have a rate typically at 1 per 1000
286 * for tail_build we register 37229 unique key
287 * with a total of 377 extra memcmp needed
288 * which is completely negligible compared to the
289 * number of memcmp required to eliminate duplicate
290 * entry (north of 2.5 millions for tail_build)
291 * ===============================================
296 struct hash_elem
* next
;
303 struct hash_elem
** array
;
304 struct pool
* elems_pool
;
308 unsigned int load_limit
;
316 #define HASH_F_NO_RESIZE (1<<0)
318 /* The following hash_compute function was adapted from :
319 * lookup3.c, by Bob Jenkins, May 2006, Public Domain.
321 * The changes from the original are mostly cosmetic
323 #define rot(x,k) (((x)<<(k)) | ((x)>>(32-(k))))
326 #if defined CORE_BIG_ENDIAN
327 #define MASK_C1 0xFFFFFF00
328 #define MASK_C2 0xFFFF0000
329 #define MASK_C3 0xFF000000
330 #elif defined CORE_LITTLE_ENDIAN
331 #define MASK_C1 0xFFFFFF
332 #define MASK_C2 0xFFFF
335 #error "Missing Endianness definition"
341 a -= c; a ^= rot(c, 4); c += b; \
342 b -= a; b ^= rot(a, 6); a += c; \
343 c -= b; c ^= rot(b, 8); b += a; \
344 a -= c; a ^= rot(c,16); c += b; \
345 b -= a; b ^= rot(a,19); a += c; \
346 c -= b; c ^= rot(b, 4); b += a; \
348 #define final(a,b,c) \
350 c ^= b; c -= rot(b,14); \
351 a ^= c; a -= rot(c,11); \
352 b ^= a; b -= rot(a,25); \
353 c ^= b; c -= rot(b,16); \
354 a ^= c; a -= rot(c,4); \
355 b ^= a; b -= rot(a,14); \
356 c ^= b; c -= rot(b,24); \
359 static unsigned int hash_compute( struct hash
* hash
, const char* key
, int length
)
363 unsigned int c
; /* internal state */
364 const unsigned char* uk
= (const unsigned char*)key
;
366 /* Set up the internal state */
367 a
= b
= c
= 0xdeadbeef + (length
<< 2);
369 /* we use this to 'hash' full path with mostly a common root
370 * let's now waste too much cycles hashing mostly constant stuff
377 /*------ all but last block: aligned reads and affect 32 bits of (a,b,c) */
380 a
+= get_unaligned_uint(uk
);
381 b
+= get_unaligned_uint(uk
+4);
382 c
+= get_unaligned_uint(uk
+8);
388 /*----------------------------- handle the last (probably partial) block */
389 /* Note: we possibly over-read, which would trigger complaint from VALGRIND
390 * but we mask the undefined stuff if any, so we are still good, thanks
391 * to alignment of memory allocation and tail-memory management overhead
392 * we always can read 3 bytes past the official end without triggering
393 * a segfault -- if you find a platform/compiler couple for which that postulat
394 * is false, then you just need to over-allocate by 2 more bytes in file_load()
395 * file_load already over-allocate by 1 to sitck a \0 at the end of the buffer.
399 case 12: c
+=get_unaligned_uint(uk
+8); b
+=get_unaligned_uint(uk
+4); a
+=get_unaligned_uint(uk
); break;
400 case 11: c
+=get_unaligned_uint(uk
+8) & MASK_C1
; b
+=get_unaligned_uint(uk
+4); a
+=get_unaligned_uint(uk
); break;
401 case 10: c
+=get_unaligned_uint(uk
+8) & MASK_C2
; b
+=get_unaligned_uint(uk
+4); a
+=get_unaligned_uint(uk
); break;
402 case 9 : c
+=get_unaligned_uint(uk
+8) & MASK_C3
; b
+=get_unaligned_uint(uk
+4); a
+=get_unaligned_uint(uk
); break;
403 case 8 : b
+=get_unaligned_uint(uk
+4); a
+=get_unaligned_uint(uk
); break;
404 case 7 : b
+=get_unaligned_uint(uk
+4) & MASK_C1
; a
+=get_unaligned_uint(uk
); break;
405 case 6 : b
+=get_unaligned_uint(uk
+4) & MASK_C2
; a
+=get_unaligned_uint(uk
); break;
406 case 5 : b
+=get_unaligned_uint(uk
+4) & MASK_C3
; a
+=get_unaligned_uint(uk
); break;
407 case 4 : a
+=get_unaligned_uint(uk
); break;
408 case 3 : a
+=get_unaligned_uint(uk
) & MASK_C1
; break;
409 case 2 : a
+=get_unaligned_uint(uk
) & MASK_C2
; break;
410 case 1 : a
+=get_unaligned_uint(uk
) & MASK_C3
; break;
411 case 0 : return c
& hash
->size
; /* zero length strings require no mixing */
415 return c
& hash
->size
;
418 static void hash_destroy(struct hash
* hash
)
428 pool_destroy(hash
->elems_pool
);
434 static struct hash
* hash_create(unsigned int size
)
439 hash
= calloc(1, sizeof(struct hash
));
442 size
+= (size
>> 2) + 1; /* ~ 75% load factor */
445 hash
->size
= (((unsigned int)0xFFFFFFFF) >> clz((unsigned int)size
));
449 hash
->size
= size
= 15;
451 hash
->load_limit
= hash
->size
- (hash
->size
>> 2);
453 hash
->array
= (struct hash_elem
**)calloc(hash
->size
+ 1, sizeof(struct hash_elem
*));
454 if(hash
->array
== NULL
)
462 hash
->elems_pool
= pool_create(sizeof(struct hash_elem
),
464 if(!hash
->elems_pool
)
473 static void hash_resize(struct hash
* hash
)
475 unsigned int old_size
= hash
->size
;
477 struct hash_elem
* hash_elem
;
478 struct hash_elem
* next
;
479 struct hash_elem
** array
;
482 hash
->size
= (old_size
<< 1) + 1;
483 /* we really should avoid to get there... so print a message to alert of the condition */
484 fprintf(stderr
, "resize hash %d -> %d\n", old_size
, hash
->size
);
485 if(hash
->size
== old_size
)
487 hash
->flags
|= HASH_F_NO_RESIZE
;
490 array
= calloc(hash
->size
+ 1, sizeof(struct hash_elem
*));
493 hash
->load_limit
= hash
->size
- (hash
->size
>> 2);
494 for(i
=0; i
<= old_size
; i
++)
496 hash_elem
= (struct hash_elem
*)hash
->array
[i
];
499 next
= hash_elem
->next
;
501 hashed
= hash_compute(hash
, hash_elem
->key
, hash_elem
->key_len
);
502 hash_elem
->next
= array
[hashed
];
503 array
[hashed
] = hash_elem
;
508 hash
->array
= (struct hash_elem
**)array
;
512 hash
->size
= old_size
;
513 hash
->flags
|= HASH_F_NO_RESIZE
;
518 static inline int compare_key(struct hash
* hash
, const char* a
, const char* b
, int len
, int* cost
)
522 return memcmp(a
,b
, len
);
525 #define compare_key(h,a,b,l,c) memcmp(a,b,l)
528 /* a customized hash_store function that just store the key and return
529 * TRUE if the key was effectively stored, or FALSE if the key was already there
531 static int hash_store(struct hash
* hash
, const char* key
, int key_len
)
534 struct hash_elem
* hash_elem
;
538 hashed
= hash_compute(hash
, key
, key_len
);
542 hash_elem
= (struct hash_elem
*)hash
->array
[hashed
];
543 while(hash_elem
&& (hash_elem
->key_len
!= key_len
|| compare_key(hash
, hash_elem
->key
, key
, key_len
, &cost
)))
545 hash_elem
= hash_elem
->next
;
550 hash_elem
= pool_alloc(hash
->elems_pool
);
553 hash_elem
->key
= key
;
554 hash_elem
->key_len
= key_len
;
555 hash_elem
->next
= hash
->array
[hashed
];
560 hash
->collisions
+= 1;
564 hash
->array
[hashed
] = hash_elem
;
566 if(hash
->used
> hash
->load_limit
)
576 static int file_stat(const char* name
, struct stat
* buffer_stat
, int* rc
)
580 rc_local
= stat(name
, buffer_stat
);
588 static off_t
file_get_size(const char* name
, int* rc
)
590 struct stat buffer_stat
;
593 if (!file_stat(name
, &buffer_stat
, rc
))
595 if(S_ISREG(buffer_stat
.st_mode
))
597 size
= buffer_stat
.st_size
;
607 #if !ENABLE_RUNTIME_OPTIMIZATIONS
608 static void * file_load_buffers
[100000];
609 static size_t file_load_buffer_count
= 0;
612 static char* file_load(const char* name
, off_t
* size
, int* return_rc
)
614 off_t local_size
= 0;
619 assert(name
!= NULL
);
625 *size
= file_get_size(name
, &rc
);
626 if (!rc
&& *size
>= 0)
628 fd
= open(name
, FILE_O_RDONLY
| FILE_O_BINARY
);
631 buffer
= malloc((size_t)(*size
+ 1));
632 #if !ENABLE_RUNTIME_OPTIMIZATIONS
635 if (file_load_buffer_count
== 100000)
642 file_load_buffers
[file_load_buffer_count
++] = buffer
;
655 i
= read(fd
, buffer
, (size_t)(*size
));
692 static void cancel_relative(char const * base
, char** ref_cursor
, char** ref_cursor_out
, char const * end
)
694 char* cursor
= *ref_cursor
;
695 char* cursor_out
= *ref_cursor_out
;
700 while(cursor_out
> base
&& cursor_out
[-1] == '/')
702 while(cursor_out
> base
&& *--cursor_out
!= '/');
704 while(cursor
+ 3 < end
&& !memcmp(cursor
, "/../", 4));
705 *ref_cursor
= cursor
;
706 *ref_cursor_out
= cursor_out
;
709 static inline void eat_space(char ** token
)
711 while ((' ' == **token
) || ('\t' == **token
)) {
717 * Prune LibreOffice specific duplicate dependencies to improve
718 * gnumake startup time, and shrink the disk-space footprint.
721 elide_dependency(const char* key
, int key_len
, const char **unpacked_end
)
726 fprintf (stderr
, "elide?%d!: '", internal_boost
);
727 for (i
= 0; i
< key_len
; i
++) {
728 fprintf (stderr
, "%c", key
[i
]);
730 fprintf (stderr
, "'\n");
734 /* boost brings a plague of header files */
737 /* walk down path elements */
738 for (i
= 0; i
< key_len
- 1; i
++)
744 if (!PATHNCMP(key
+ i
+ 1, "workdir/", 8))
752 if (!PATHNCMP(key
+ i
+ 1, "UnpackedTarball/", 16))
755 *unpacked_end
= strchr(key
+ i
+ 17, '/');
766 * We collapse tens of internal boost headers to the unpacked target, such
767 * that you can re-compile / install boost and all is well.
769 static void emit_single_boost_header(void)
771 #define BOOST_TARGET "/UnpackedTarball/boost.done"
772 fprintf(stdout
, "%s" BOOST_TARGET
" ", work_dir
);
775 static void emit_unpacked_target(const char* token
, const char* end
)
777 fwrite(token
, 1, end
-token
, stdout
);
778 fputs(".done ", stdout
);
781 /* prefix paths to absolute */
782 static inline void print_fullpaths(char* line
)
788 const char * unpacked_end
= NULL
; /* end of UnpackedTarget match (if any) */
789 /* for UnpackedTarget the target is GenC{,xx}Object, don't mangle! */
797 /* hard to believe that in this day and age drive letters still exist */
798 if (*end
&& (':' == *(end
+1)) &&
799 (('\\' == *(end
+2)) || ('/' == *(end
+2))) && isalpha(*end
))
801 end
= end
+ 3; /* only one cross, err drive letter per filename */
803 while (*end
&& (' ' != *end
) && ('\t' != *end
) && (':' != *end
)) {
806 token_len
= end
- token
;
808 elide_dependency(token
, token_len
, &unpacked_end
))
812 if (internal_boost
&& !PATHNCMP(unpacked_end
- 5, "boost", 5))
815 if (boost_count
== 1)
816 emit_single_boost_header();
819 /* don't output, and swallow trailing \\\n if any */
822 if (token
[0] == '\\' && token
[1] == '\n')
828 emit_unpacked_target(token
, unpacked_end
);
835 if (fwrite(token
, token_len
, 1, stdout
) != 1)
854 static inline char * eat_space_at_end(char * end
)
857 assert('\0' == *end
);
859 while (' ' == *real_end
|| '\t' == *real_end
|| '\n' == *real_end
861 { /* eat colon and whitespace at end */
867 static char* phony_content_buffer
;
868 static inline char* generate_phony_line(char const * phony_target
, char const * extension
)
872 char* last_dot
= NULL
;
873 //fprintf(stderr, "generate_phony_line called with phony_target %s and extension %s\n", phony_target, extension);
874 for(dest
= phony_content_buffer
+work_dir_len
, src
= phony_target
; *src
!= 0; ++src
, ++dest
)
882 //fprintf(stderr, "generate_phony_line after phony_target copy: %s\n", phony_content_buffer);
883 for(dest
= last_dot
+1, src
= extension
; *src
!= 0; ++src
, ++dest
)
887 //fprintf(stderr, "generate_phony_line after extension add: %s\n", phony_content_buffer);
888 strcpy(dest
, ": $(gb_Helper_PHONY)\n");
889 //fprintf(stderr, "generate_phony_line after phony add: %s\n", phony_content_buffer);
890 return phony_content_buffer
;
893 static inline int generate_phony_file(char* fn
, char const * content
)
896 depfile
= fopen(fn
, "w");
899 fprintf(stderr
, "Could not open '%s' for writing: %s\n", fn
, strerror(errno
));
903 fputs(content
, depfile
);
909 static int process(struct hash
* dep_hash
, char* fn
)
917 char* created_line
= NULL
;
919 int continuation
= 0;
923 buffer
= file_load(fn
, &size
, &rc
);
926 base
= cursor_out
= cursor
= end
= buffer
;
929 /* first eat unneeded space at the beginning of file
931 while(cursor
< end
&& (*cursor
== ' ' || *cursor
== '\\'))
939 *cursor_out
++ = *cursor
++;
941 else if(*cursor
== '/')
945 if(!memcmp(cursor
, "/../", 4))
947 cancel_relative(base
, &cursor
, &cursor_out
, end
);
950 *cursor_out
++ = *cursor
++;
952 else if(*cursor
== '\n')
959 /* here we have a complete rule */
962 /* if the rule ended in ':' that is a no-dep rule
963 * these are the one for which we want to filter
966 int key_len
= eat_space_at_end(cursor_out
) - base
;
967 if (!elide_dependency(base
,key_len
+ 1, NULL
)
968 && hash_store(dep_hash
, base
, key_len
))
970 /* DO NOT modify base after it has been added
971 as key by hash_store */
972 print_fullpaths(base
);
978 /* rule with dep, just write it */
979 print_fullpaths(base
);
982 last_ns
= ' '; // cannot hurt to reset it
985 base
= cursor_out
= cursor
;
989 /* here we have a '\' followed by \n this is a continuation
990 * i.e not a complete rule yet
992 *cursor_out
++ = *cursor
++;
993 continuation
= 0; // cancel current one (empty lines!)
999 /* not using isspace() here save 25% of I refs and 75% of D refs based on cachegrind */
1000 if(*cursor
!= ' ' && *cursor
!= '\n' && *cursor
!= '\t' )
1004 *cursor_out
++ = *cursor
++;
1007 /* just in case the file did not end with a \n, there may be a pending rule */
1008 if(base
< cursor_out
)
1012 int key_len
= eat_space_at_end(cursor_out
) - base
;
1013 if (!elide_dependency(base
,key_len
+ 1, NULL
) &&
1014 hash_store(dep_hash
, base
, key_len
))
1029 if(strncmp(fn
, work_dir
, work_dir_len
) == 0)
1031 if(strncmp(fn
+work_dir_len
, "/Dep/", 5) == 0)
1033 src_relative
= fn
+work_dir_len
+5;
1034 // cases ordered by frequency
1035 if(strncmp(src_relative
, "CxxObject/", 10) == 0)
1037 created_line
= generate_phony_line(src_relative
+10, "o");
1038 rc
= generate_phony_file(fn
, created_line
);
1040 else if(strncmp(fn
+work_dir_len
+5, "SrsPartTarget/", 14) == 0)
1042 created_line
= generate_phony_line(src_relative
+14, "");
1043 rc
= generate_phony_file(fn
, created_line
);
1045 else if(strncmp(src_relative
, "GenCxxObject/", 13) == 0)
1047 created_line
= generate_phony_line(src_relative
+13, "o");
1048 rc
= generate_phony_file(fn
, created_line
);
1050 else if(strncmp(src_relative
, "CObject/", 8) == 0)
1052 created_line
= generate_phony_line(src_relative
+8, "o");
1053 rc
= generate_phony_file(fn
, created_line
);
1055 else if(strncmp(src_relative
, "GenCObject/", 11) == 0)
1057 created_line
= generate_phony_line(src_relative
+11, "o");
1058 rc
= generate_phony_file(fn
, created_line
);
1060 else if(strncmp(src_relative
, "SdiObject/", 10) == 0)
1062 created_line
= generate_phony_line(src_relative
+10, "o");
1063 rc
= generate_phony_file(fn
, created_line
);
1065 else if(strncmp(src_relative
, "AsmObject/", 10) == 0)
1067 created_line
= generate_phony_line(src_relative
+10, "o");
1068 rc
= generate_phony_file(fn
, created_line
);
1070 else if(strncmp(src_relative
, "ObjCxxObject/", 13) == 0)
1072 created_line
= generate_phony_line(src_relative
+13, "o");
1073 rc
= generate_phony_file(fn
, created_line
);
1075 else if(strncmp(src_relative
, "ObjCObject/", 11) == 0)
1077 created_line
= generate_phony_line(src_relative
+11, "o");
1078 rc
= generate_phony_file(fn
, created_line
);
1082 fprintf(stderr
, "no magic for %s(%s) in %s\n", fn
, src_relative
, work_dir
);
1091 /* Note: yes we are going to leak 'buffer'
1092 * this is on purpose, to avoid cloning the 'key' out of it and our special
1093 * 'hash' just store the pointer to the key inside of buffer, hence it need
1094 * to remain allocated
1096 // coverity[leaked_storage] - this is on purpose
1100 static void usage(void)
1102 fputs("Usage: concat-deps <file that contains dep_files>\n", stderr
);
1105 #define kDEFAULT_HASH_SIZE 4096
1106 #define PHONY_TARGET_BUFFER 4096
1108 static int get_var(char **var
, const char *name
)
1110 *var
= (char *)getenv(name
);
1113 fprintf(stderr
,"Error: %s is missing in the environment\n", name
);
1119 int main(int argc
, char** argv
)
1122 off_t in_list_size
= 0;
1124 char* in_list_cursor
;
1126 struct hash
* dep_hash
= NULL
;
1127 const char *env_str
;
1134 if(get_var(&base_dir
, "SRCDIR") || get_var(&work_dir
, "WORKDIR"))
1136 work_dir_len
= strlen(work_dir
);
1137 phony_content_buffer
= malloc(PHONY_TARGET_BUFFER
);
1138 strcpy(phony_content_buffer
, work_dir
);
1139 phony_content_buffer
[work_dir_len
] = '/';
1141 env_str
= getenv("SYSTEM_BOOST");
1142 internal_boost
= !env_str
|| strcmp(env_str
,"TRUE");
1144 in_list
= file_load(argv
[1], &in_list_size
, &rc
);
1147 dep_hash
= hash_create( kDEFAULT_HASH_SIZE
);
1148 in_list_base
= in_list_cursor
= in_list
;
1150 /* extract filename of dep file from a 'space' separated list */
1151 while(*in_list_cursor
)
1153 /* the input here may contain Win32 \r\n EOL */
1154 if(*in_list_cursor
== ' '
1155 || *in_list_cursor
== '\n' || *in_list_cursor
== '\r')
1157 *in_list_cursor
= 0;
1158 if(in_list_base
< in_list_cursor
)
1160 rc
= process(dep_hash
, in_list_base
);
1166 in_list_cursor
+= 1;
1167 in_list_base
= in_list_cursor
;
1171 in_list_cursor
+= 1;
1176 /* catch the last entry in case the input did not terminate with a 'space' */
1177 if(in_list_base
< in_list_cursor
)
1179 rc
= process(dep_hash
, in_list_base
);
1183 fprintf(stderr
, "stats: u:%d s:%d l:%d t:%d c:%d m:%d $:%d\n",
1184 dep_hash
->used
, dep_hash
->size
, dep_hash
->load_limit
, dep_hash
->stored
,
1185 dep_hash
->collisions
, dep_hash
->memcmp
, dep_hash
->cost
);
1188 #if !ENABLE_RUNTIME_OPTIMIZATIONS
1191 hash_destroy(dep_hash
);
1192 for (i
= 0; i
!= file_load_buffer_count
; ++i
)
1194 free(file_load_buffers
[i
]);
1201 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */