1 /* Miscellaneous global declarations and portability cruft for GNU Make.
2 Copyright (C) 1988, 1989, 1990, 1991, 1992, 1993, 1994, 1995, 1996, 1997,
3 1998, 1999, 2000, 2001, 2002, 2003, 2004, 2005, 2006, 2007, 2008, 2009,
4 2010 Free Software Foundation, Inc.
5 This file is part of GNU Make.
6 
7 GNU Make is free software; you can redistribute it and/or modify it under the
8 terms of the GNU General Public License as published by the Free Software
9 Foundation; either version 3 of the License, or (at your option) any later
10 version.
11 
12 GNU Make is distributed in the hope that it will be useful, but WITHOUT ANY
13 WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR
14 A PARTICULAR PURPOSE.  See the GNU General Public License for more details.
15 
16 You should have received a copy of the GNU General Public License along with
17 this program.  If not, see <http://www.gnu.org/licenses/>.  */
18 
19 /* We use <config.h> instead of "config.h" so that a compilation
20    using -I. -I$srcdir will use ./config.h rather than $srcdir/config.h
21    (which it would do because make.h was found in $srcdir).  */
22 #include <config.h>
23 #undef  HAVE_CONFIG_H
24 #define HAVE_CONFIG_H 1
25 
26 /* Specify we want GNU source code.  This must be defined before any
27    system headers are included.  */
28 
29 #define _GNU_SOURCE 1
30 
31 /* AIX requires this to be the first thing in the file.  */
32 #if HAVE_ALLOCA_H
33 # include <alloca.h>
34 #else
35 # ifdef _AIX
36  #pragma alloca
37 # else
38 #  if !defined(__GNUC__) && !defined(WINDOWS32)
39 #   ifndef alloca /* predefined by HP cc +Olibcalls */
40 char *alloca ();
41 #   endif
42 #  endif
43 # endif
44 #endif
45 
46 
47 #ifdef  CRAY
48 /* This must happen before #include <signal.h> so
49    that the declaration therein is changed.  */
50 # define signal bsdsignal
51 #endif
52 
53 /* If we're compiling for the dmalloc debugger, turn off string inlining.  */
54 #if defined(HAVE_DMALLOC_H) && defined(__GNUC__)
55 # define __NO_STRING_INLINES
56 #endif
57 
58 #include <sys/types.h>
59 #include <sys/stat.h>
60 #include <signal.h>
61 #include <stdio.h>
62 #include <ctype.h>
63 #ifdef HAVE_SYS_TIMEB_H
64 /* SCO 3.2 "devsys 4.2" has a prototype for `ftime' in <time.h> that bombs
65    unless <sys/timeb.h> has been included first.  Does every system have a
66    <sys/timeb.h>?  If any does not, configure should check for it.  */
67 # include <sys/timeb.h>
68 #endif
69 
70 #if TIME_WITH_SYS_TIME
71 # include <sys/time.h>
72 # include <time.h>
73 #else
74 # if HAVE_SYS_TIME_H
75 #  include <sys/time.h>
76 # else
77 #  include <time.h>
78 # endif
79 #endif
80 
81 #include <errno.h>
82 
83 #ifndef errno
84 extern int errno;
85 #endif
86 
87 #ifndef isblank
88 # define isblank(c)     ((c) == ' ' || (c) == '\t')
89 #endif
90 
91 #ifdef  HAVE_UNISTD_H
92 # include <unistd.h>
93 /* Ultrix's unistd.h always defines _POSIX_VERSION, but you only get
94    POSIX.1 behavior with `cc -YPOSIX', which predefines POSIX itself!  */
95 # if defined (_POSIX_VERSION) && !defined (ultrix) && !defined (VMS)
96 #  define POSIX 1
97 # endif
98 #endif
99 
100 /* Some systems define _POSIX_VERSION but are not really POSIX.1.  */
101 #if (defined (butterfly) || defined (__arm) || (defined (__mips) && defined (_SYSTYPE_SVR3)) || (defined (sequent) && defined (i386)))
102 # undef POSIX
103 #endif
104 
105 #if !defined (POSIX) && defined (_AIX) && defined (_POSIX_SOURCE)
106 # define POSIX 1
107 #endif
108 
109 #ifndef RETSIGTYPE
110 # define RETSIGTYPE     void
111 #endif
112 
113 #ifndef sigmask
114 # define sigmask(sig)   (1 << ((sig) - 1))
115 #endif
116 
117 #ifndef HAVE_SA_RESTART
118 # define SA_RESTART 0
119 #endif
120 
121 #ifdef  HAVE_LIMITS_H
122 # include <limits.h>
123 #endif
124 #ifdef  HAVE_SYS_PARAM_H
125 # include <sys/param.h>
126 #endif
127 
128 #ifndef PATH_MAX
129 # ifndef POSIX
130 #  define PATH_MAX      MAXPATHLEN
131 # endif
132 #endif
133 #ifndef MAXPATHLEN
134 # define MAXPATHLEN 1024
135 #endif
136 
137 #ifdef  PATH_MAX
138 # define GET_PATH_MAX   PATH_MAX
139 # define PATH_VAR(var)  char var[PATH_MAX]
140 #else
141 # define NEED_GET_PATH_MAX 1
142 # define GET_PATH_MAX   (get_path_max ())
143 # define PATH_VAR(var)  char *var = alloca (GET_PATH_MAX)
144 unsigned int get_path_max (void);
145 #endif
146 
147 #if defined (KMK) || defined (CONFIG_WITH_VALUE_LENGTH) \
148  || defined (CONFIG_WITH_ALLOC_CACHES) \
149  || defined (CONFIG_WITH_STRCACHE2)
150 # ifdef _MSC_VER
151 #  define MY_INLINE     _inline static
152 # elif defined(__GNUC__)
153 #  define MY_INLINE     static __inline__
154 # else
155 #  define MY_INLINE     static
156 # endif
157 
158 # ifdef __GNUC__
159 #  define MY_PREDICT_TRUE(expr)  __builtin_expect(!!(expr), 1)
160 #  define MY_PREDICT_FALSE(expr) __builtin_expect(!!(expr), 0)
161 # else
162 #  define MY_PREDICT_TRUE(expr)  (expr)
163 #  define MY_PREDICT_FALSE(expr) (expr)
164 # endif
165 #endif
166 
167 #if defined (KMK) || defined (CONFIG_WITH_VALUE_LENGTH) \
168  || defined (CONFIG_WITH_STRCACHE2)
169 # ifdef _MSC_VER
170 #  define MY_DBGBREAK   __debugbreak()
171 # elif defined(__GNUC__)
172 #  if defined(__i386__) || defined(__x86_64__)
173 #   define MY_DBGBREAK  __asm__ __volatile__ ("int3")
174 #  else
175 #   define MY_DBGBREAK  assert(0)
176 #  endif
177 # else
178 #  define MY_DBGBREAK   assert(0)
179 # endif
180 # ifndef NDEBUG
181 #  define MY_ASSERT_MSG(expr, printfargs) \
182     do { if (!(expr)) { printf printfargs; MY_DBGBREAK; } } while (0)
183 # else
184 #  define MY_ASSERT_MSG(expr, printfargs)   do { } while (0)
185 # endif
186 #endif
187 
188 #ifdef KMK
189 # include <ctype.h>
190 # if 1 /* See if this speeds things up (Windows is doing this anyway, so,
191           we might as well try be consistent in speed + features).  */
192 #  if 1
193 #   define MY_IS_BLANK(ch)          ((ch) == ' ' || (ch) == '\t')
194 #   define MY_IS_BLANK_OR_EOS(ch)   ((ch) == ' ' || (ch) == '\t' || (ch) == '\0')
195 #  else
196 #   define MY_IS_BLANK(ch)          (((ch) == ' ') | ((ch) == '\t'))
197 #   define MY_IS_BLANK_OR_EOS(ch)   (((ch) == ' ') | ((ch) == '\t') | ((ch) == '\0'))
198 #  endif
199 #  undef isblank
200 #  define isblank(ch)               MY_IS_BLANK(ch)
201 # else
202 #  define MY_IS_BLANK(ch)           isblank ((unsigned char)(ch))
203 #  define MY_IS_BLANK_OR_EOS(ch)    (isblank ((unsigned char)(ch)) || (ch) == '\0')
204 # endif
205 #endif
206 
207 #ifdef CONFIG_WITH_MAKE_STATS
208 extern long make_stats_allocations;
209 extern long make_stats_reallocations;
210 extern unsigned long make_stats_allocated;
211 extern unsigned long make_stats_ht_lookups;
212 extern unsigned long make_stats_ht_collisions;
213 
214 # ifdef __APPLE__
215 #  include <malloc/malloc.h>
216 #  define SIZE_OF_HEAP_BLOCK(ptr)   malloc_size(ptr)
217 
218 # elif defined(__linux__) /* glibc */
219 #  include <malloc.h>
220 #  define SIZE_OF_HEAP_BLOCK(ptr)   malloc_usable_size(ptr)
221 
222 # elif defined(_MSC_VER) || defined(__OS2__)
223 #  define SIZE_OF_HEAP_BLOCK(ptr)   _msize(ptr)
224 
225 # else
226 #  include <stdlib.h>
227 #  define SIZE_OF_HEAP_BLOCK(ptr)   0
228 #endif
229 
230 # define MAKE_STATS_3(expr) do { expr; } while (0)
231 # define MAKE_STATS_2(expr) do { expr; } while (0)
232 # define MAKE_STATS(expr)   do { expr; } while (0)
233 #else
234 # define MAKE_STATS_3(expr) do { } while (0)
235 # define MAKE_STATS_2(expr) do { } while (0)
236 # define MAKE_STATS(expr)   do { } while (0)
237 #endif
238 
239 /* bird - start */
240 #ifdef _MSC_VER
241 # include <intrin.h>
242 # define CURRENT_CLOCK_TICK() __rdtsc()
243 #else
244 # define CURRENT_CLOCK_TICK() 0
245 #endif
246 
247 #define COMMA ,
248 #ifdef CONFIG_WITH_VALUE_LENGTH
249 # define IF_WITH_VALUE_LENGTH(a_Expr)           a_Expr
250 # define IF_WITH_VALUE_LENGTH_PARAM(a_Expr)     , a_Expr
251 #else
252 # define IF_WITH_VALUE_LENGTH(a_Expr)
253 # define IF_WITH_VALUE_LENGTH_PARAM(a_Expr)
254 #endif
255 
256 #ifdef CONFIG_WITH_ALLOC_CACHES
257 # define IF_WITH_ALLOC_CACHES(a_Expr)           a_Expr
258 # define IF_WITH_ALLOC_CACHES_PARAM(a_Expr)     , a_Expr
259 #else
260 # define IF_WITH_ALLOC_CACHES(a_Expr)
261 # define IF_WITH_ALLOC_CACHES_PARAM(a_Expr)
262 #endif
263 
264 #ifdef CONFIG_WITH_COMMANDS_FUNC
265 # define IF_WITH_COMMANDS_FUNC(a_Expr)          a_Expr
266 # define IF_WITH_COMMANDS_FUNC_PARAM(a_Expr)    , a_Expr
267 #else
268 # define IF_WITH_COMMANDS_FUNC(a_Expr)
269 # define IF_WITH_COMMANDS_FUNC_PARAM(a_Expr)
270 #endif
271 
272 /* bird - end */
273 
274 
275 #ifndef CHAR_BIT
276 # define CHAR_BIT 8
277 #endif
278 
279 /* Nonzero if the integer type T is signed.  */
280 #define INTEGER_TYPE_SIGNED(t) ((t) -1 < 0)
281 
282 /* The minimum and maximum values for the integer type T.
283    Use ~ (t) 0, not -1, for portability to 1's complement hosts.  */
284 #define INTEGER_TYPE_MINIMUM(t) \
285   (! INTEGER_TYPE_SIGNED (t) ? (t) 0 : ~ (t) 0 << (sizeof (t) * CHAR_BIT - 1))
286 #define INTEGER_TYPE_MAXIMUM(t) (~ (t) 0 - INTEGER_TYPE_MINIMUM (t))
287 
288 #ifndef CHAR_MAX
289 # define CHAR_MAX INTEGER_TYPE_MAXIMUM (char)
290 #endif
291 
292 #ifdef STAT_MACROS_BROKEN
293 # ifdef S_ISREG
294 #  undef S_ISREG
295 # endif
296 # ifdef S_ISDIR
297 #  undef S_ISDIR
298 # endif
299 #endif  /* STAT_MACROS_BROKEN.  */
300 
301 #ifndef S_ISREG
302 # define S_ISREG(mode)  (((mode) & S_IFMT) == S_IFREG)
303 #endif
304 #ifndef S_ISDIR
305 # define S_ISDIR(mode)  (((mode) & S_IFMT) == S_IFDIR)
306 #endif
307 
308 #ifdef VMS
309 # include <types.h>
310 # include <unixlib.h>
311 # include <unixio.h>
312 # include <perror.h>
313 /* Needed to use alloca on VMS.  */
314 # include <builtins.h>
315 #endif
316 
317 #ifndef __attribute__
318 /* This feature is available in gcc versions 2.5 and later.  */
319 # if __GNUC__ < 2 || (__GNUC__ == 2 && __GNUC_MINOR__ < 5) || __STRICT_ANSI__
320 #  define __attribute__(x)
321 # endif
322 /* The __-protected variants of `format' and `printf' attributes
323    are accepted by gcc versions 2.6.4 (effectively 2.7) and later.  */
324 # if __GNUC__ < 2 || (__GNUC__ == 2 && __GNUC_MINOR__ < 7)
325 #  define __format__ format
326 #  define __printf__ printf
327 # endif
328 #endif
329 #define UNUSED  __attribute__ ((unused))
330 
331 #if defined (STDC_HEADERS) || defined (__GNU_LIBRARY__)
332 # include <stdlib.h>
333 # include <string.h>
334 # define ANSI_STRING 1
335 #else   /* No standard headers.  */
336 # ifdef HAVE_STRING_H
337 #  include <string.h>
338 #  define ANSI_STRING 1
339 # else
340 #  include <strings.h>
341 # endif
342 # ifdef HAVE_MEMORY_H
343 #  include <memory.h>
344 # endif
345 # ifdef HAVE_STDLIB_H
346 #  include <stdlib.h>
347 # else
348 void *malloc (int);
349 void *realloc (void *, int);
350 void free (void *);
351 
352 void abort (void) __attribute__ ((noreturn));
353 void exit (int) __attribute__ ((noreturn));
354 # endif /* HAVE_STDLIB_H.  */
355 
356 #endif /* Standard headers.  */
357 
358 /* These should be in stdlib.h.  Make sure we have them.  */
359 #ifndef EXIT_SUCCESS
360 # define EXIT_SUCCESS 0
361 #endif
362 #ifndef EXIT_FAILURE
363 # define EXIT_FAILURE 1
364 #endif
365 
366 #ifndef  ANSI_STRING
367 
368 /* SCO Xenix has a buggy macro definition in <string.h>.  */
369 #undef  strerror
370 #if !defined(__DECC)
371 char *strerror (int errnum);
372 #endif
373 
374 #endif  /* !ANSI_STRING.  */
375 #undef  ANSI_STRING
376 
377 #if HAVE_INTTYPES_H
378 # include <inttypes.h>
379 #endif
380 #define FILE_TIMESTAMP uintmax_t
381 
382 #if !defined(HAVE_STRSIGNAL)
383 char *strsignal (int signum);
384 #endif
385 
386 /* ISDIGIT offers the following features:
387    - Its arg may be any int or unsigned int; it need not be an unsigned char.
388    - It's guaranteed to evaluate its argument exactly once.
389       NOTE!  Make relies on this behavior, don't change it!
390    - It's typically faster.
391    POSIX 1003.2-1992 section 2.5.2.1 page 50 lines 1556-1558 says that
392    only '0' through '9' are digits.  Prefer ISDIGIT to isdigit() unless
393    it's important to use the locale's definition of `digit' even when the
394    host does not conform to POSIX.  */
395 #define ISDIGIT(c) ((unsigned) (c) - '0' <= 9)
396 
397 /* Test if two strings are equal. Is this worthwhile?  Should be profiled.  */
398 #define streq(a, b) \
399    ((a) == (b) || \
400     (*(a) == *(b) && (*(a) == '\0' || !strcmp ((a) + 1, (b) + 1))))
401 
402 /* Test if two strings are equal, but match case-insensitively on systems
403    which have case-insensitive filesystems.  Should only be used for
404    filenames!  */
405 #ifdef HAVE_CASE_INSENSITIVE_FS
406 # define patheq(a, b) \
407     ((a) == (b) \
408      || (tolower((unsigned char)*(a)) == tolower((unsigned char)*(b)) \
409          && (*(a) == '\0' || !strcasecmp ((a) + 1, (b) + 1))))
410 #else
411 # define patheq(a, b) streq(a, b)
412 #endif
413 
414 #define strneq(a, b, l) (strncmp ((a), (b), (l)) == 0)
415 
416 #if (defined(__GNUC__) || defined(ENUM_BITFIELDS)) && !defined(NO_ENUM_BITFIELDS)
417 # define ENUM_BITFIELD(bits)    :bits
418 #else
419 # define ENUM_BITFIELD(bits)
420 #endif
421 
422 /* Handle gettext and locales.  */
423 
424 #if HAVE_LOCALE_H
425 # include <locale.h>
426 #else
427 # define setlocale(category, locale)
428 #endif
429 
430 #include <gettext.h>
431 
432 #define _(msgid)            gettext (msgid)
433 #define N_(msgid)           gettext_noop (msgid)
434 #define S_(msg1,msg2,num)   ngettext (msg1,msg2,num)
435 
436 /* Handle other OSs.  */
437 #ifndef PATH_SEPARATOR_CHAR
438 # if defined(HAVE_DOS_PATHS)
439 #  define PATH_SEPARATOR_CHAR ';'
440 # elif defined(VMS)
441 #  define PATH_SEPARATOR_CHAR ','
442 # else
443 #  define PATH_SEPARATOR_CHAR ':'
444 # endif
445 #endif
446 
447 /* This is needed for getcwd() and chdir(), on some W32 systems.  */
448 #if defined(HAVE_DIRECT_H)
449 # include <direct.h>
450 #endif
451 
452 #ifdef WINDOWS32
453 # include <fcntl.h>
454 # include <malloc.h>
455 # define pipe(_p)        _pipe((_p), 512, O_BINARY)
456 # define kill(_pid,_sig) w32_kill((_pid),(_sig))
457 
458 void sync_Path_environment (void);
459 int w32_kill (pid_t pid, int sig);
460 char *end_of_token_w32 (const char *s, char stopchar);
461 int find_and_set_default_shell (const char *token);
462 
463 /* indicates whether or not we have Bourne shell */
464 extern int no_default_sh_exe;
465 
466 /* is default_shell unixy? */
467 extern int unixy_shell;
468 #endif  /* WINDOWS32 */
469 
470 #if defined(HAVE_SYS_RESOURCE_H) && defined(HAVE_GETRLIMIT) && defined(HAVE_SETRLIMIT)
471 # define SET_STACK_SIZE
472 #endif
473 #ifdef SET_STACK_SIZE
474 # include <sys/resource.h>
475 extern struct rlimit stack_limit;
476 #endif
477 
478 struct floc
479   {
480     const char *filenm;
481     unsigned long lineno;
482   };
483 #define NILF ((struct floc *)0)
484 
485 #define STRING_SIZE_TUPLE(_s) (_s), (sizeof (_s)-1)
486 
487 #if defined (CONFIG_WITH_MATH) \
488  || defined (CONFIG_WITH_NANOTS) \
489  || defined (CONFIG_WITH_FILE_SIZE) \
490  || defined (CONFIG_WITH_PRINT_TIME_SWITCH) /* bird */
491 # ifdef _MSC_VER
492 typedef __int64 big_int;
493 #  define BIG_INT_C(c)      (c ## LL)
494 typedef unsigned __int64 big_uint;
495 #  define BIG_UINT_C(c)     (c ## ULL)
496 # else
497 #  include <stdint.h>
498 typedef int64_t big_int;
499 #  define BIG_INT_C(c)      INT64_C(c)
500 typedef uint64_t big_uint;
501 #  define BIG_UINT_C(c)     UINT64_C(c)
502 # endif
503 #endif
504 
505 
506 /* We have to have stdarg.h or varargs.h AND v*printf or doprnt to use
507    variadic versions of these functions.  */
508 
509 #if HAVE_STDARG_H || HAVE_VARARGS_H
510 # if HAVE_VPRINTF || HAVE_DOPRNT
511 #  define USE_VARIADIC 1
512 # endif
513 #endif
514 
515 #if HAVE_ANSI_COMPILER && USE_VARIADIC && HAVE_STDARG_H
516 const char *concat (unsigned int, ...);
517 void message (int prefix, const char *fmt, ...)
518               __attribute__ ((__format__ (__printf__, 2, 3)));
519 void error (const struct floc *flocp, const char *fmt, ...)
520             __attribute__ ((__format__ (__printf__, 2, 3)));
521 void fatal (const struct floc *flocp, const char *fmt, ...)
522                    __attribute__ ((noreturn, __format__ (__printf__, 2, 3)));
523 #else
524 const char *concat ();
525 void message ();
526 void error ();
527 void fatal ();
528 #endif
529 
530 void die (int) __attribute__ ((noreturn));
531 void log_working_directory (int);
532 void pfatal_with_name (const char *) __attribute__ ((noreturn));
533 void perror_with_name (const char *, const char *);
534 void *xmalloc (unsigned int);
535 void *xcalloc (unsigned int);
536 void *xrealloc (void *, unsigned int);
537 char *xstrdup (const char *);
538 char *xstrndup (const char *, unsigned int);
539 #ifdef CONFIG_WITH_PRINT_STATS_SWITCH
540 void print_heap_stats (void);
541 #endif
542 char *find_next_token (const char **, unsigned int *);
543 char *next_token (const char *);
544 char *end_of_token (const char *);
545 #ifdef KMK
546 char *find_next_token_eos (const char **ptr, const char *eos, unsigned int *lengthptr);
547 #endif
548 #ifndef CONFIG_WITH_VALUE_LENGTH
549 void collapse_continuations (char *);
550 #else
551 char *collapse_continuations (char *, unsigned int);
552 #endif
553 #ifdef CONFIG_WITH_OPTIMIZATION_HACKS /* memchr is usually compiler intrinsic, thus faster. */
554 # define lindex(s, limit, c) ((char *)memchr((s), (c), (limit) - (s)))
555 #else
556 char *lindex (const char *, const char *, int);
557 #endif
558 int alpha_compare (const void *, const void *);
559 void print_spaces (unsigned int);
560 char *find_percent (char *);
561 const char *find_percent_cached (const char **);
562 FILE *open_tmpfile (char **, const char *);
563 
564 #ifndef NO_ARCHIVES
565 int ar_name (const char *);
566 void ar_parse_name (const char *, char **, char **);
567 int ar_touch (const char *);
568 time_t ar_member_date (const char *);
569 
570 typedef long int (*ar_member_func_t) (int desc, const char *mem, int truncated,
571 				      long int hdrpos, long int datapos,
572 				      long int size, long int date, int uid,
573 				      int gid, int mode, const void *arg);
574 
575 long int ar_scan (const char *archive, ar_member_func_t function, const void *arg);
576 int ar_name_equal (const char *name, const char *mem, int truncated);
577 #ifndef VMS
578 int ar_member_touch (const char *arname, const char *memname);
579 #endif
580 #endif
581 
582 int dir_file_exists_p (const char *, const char *);
583 int file_exists_p (const char *);
584 int file_impossible_p (const char *);
585 void file_impossible (const char *);
586 const char *dir_name (const char *);
587 void hash_init_directories (void);
588 
589 void define_default_variables (void);
590 void set_default_suffixes (void);
591 void install_default_suffix_rules (void);
592 void install_default_implicit_rules (void);
593 
594 void build_vpath_lists (void);
595 void construct_vpath_list (char *pattern, char *dirpath);
596 const char *vpath_search (const char *file, FILE_TIMESTAMP *mtime_ptr,
597                           unsigned int* vpath_index, unsigned int* path_index);
598 int gpath_search (const char *file, unsigned int len);
599 
600 void construct_include_path (const char **arg_dirs);
601 
602 void user_access (void);
603 void make_access (void);
604 void child_access (void);
605 
606 void close_stdout (void);
607 
608 char *strip_whitespace (const char **begpp, const char **endpp);
609 
610 #ifdef CONFIG_WITH_ALLOC_CACHES
611 /* alloccache (misc.c) */
612 
613 struct alloccache_free_ent
614 {
615   struct alloccache_free_ent *next;
616 };
617 
618 struct alloccache
619 {
620   char *free_start;
621   char *free_end;
622   struct alloccache_free_ent *free_head;
623   unsigned int size;
624   unsigned int total_count;
625   unsigned long alloc_count;
626   unsigned long free_count;
627   const char *name;
628   struct alloccache *next;
629   void *grow_arg;
630   void *(*grow_alloc)(void *grow_arg, unsigned int size);
631 };
632 
633 void alloccache_init (struct alloccache *cache, unsigned int size, const char *name,
634                       void *(*grow_alloc)(void *grow_arg, unsigned int size), void *grow_arg);
635 void alloccache_term (struct alloccache *cache,
636                       void (*term_free)(void *term_arg, void *ptr, unsigned int size), void *term_arg);
637 void alloccache_join (struct alloccache *cache, struct alloccache *eat);
638 void alloccache_print (struct alloccache *cache);
639 void alloccache_print_all (void);
640 struct alloccache_free_ent *alloccache_alloc_grow (struct alloccache *cache);
641 void alloccache_free (struct alloccache *cache, void *item);
642 
643 /* Allocate an item. */
644 MY_INLINE void *
alloccache_alloc(struct alloccache * cache)645 alloccache_alloc (struct alloccache *cache)
646 {
647   struct alloccache_free_ent *f;
648 # ifndef CONFIG_WITH_ALLOCCACHE_DEBUG
649   f = cache->free_head;
650   if (f)
651     cache->free_head = f->next;
652   else if (cache->free_start != cache->free_end)
653     {
654       f = (struct alloccache_free_ent *)cache->free_start;
655       cache->free_start += cache->size;
656     }
657   else
658 # endif
659     f = alloccache_alloc_grow (cache);
660   MAKE_STATS(cache->alloc_count++;);
661   return f;
662 }
663 
664 /* Allocate a cleared item. */
665 MY_INLINE void *
alloccache_calloc(struct alloccache * cache)666 alloccache_calloc (struct alloccache *cache)
667 {
668   void *item = alloccache_alloc (cache);
669   memset (item, '\0', cache->size);
670   return item;
671 }
672 
673 
674 /* the alloc caches */
675 extern struct alloccache dep_cache;
676 extern struct alloccache file_cache;
677 extern struct alloccache commands_cache;
678 extern struct alloccache nameseq_cache;
679 extern struct alloccache variable_cache;
680 extern struct alloccache variable_set_cache;
681 extern struct alloccache variable_set_list_cache;
682 
683 #endif /* CONFIG_WITH_ALLOC_CACHES */
684 
685 
686 /* String caching  */
687 void strcache_init (void);
688 void strcache_print_stats (const char *prefix);
689 #ifndef CONFIG_WITH_STRCACHE2
690 int strcache_iscached (const char *str);
691 const char *strcache_add (const char *str);
692 const char *strcache_add_len (const char *str, int len);
693 int strcache_setbufsize (int size);
694 #else  /* CONFIG_WITH_STRCACHE2 */
695 
696 # include "strcache2.h"
697 extern struct strcache2 file_strcache;
698 extern const char *suffixes_strcached;
699 
700 # define strcache_iscached(str)     strcache2_is_cached(&file_strcache, str)
701 # define strcache_add(str)          strcache2_add_file(&file_strcache, str, strlen (str))
702 # define strcache_add_len(str, len) strcache2_add_file(&file_strcache, str, len)
703 # define strcache_get_len(str)      strcache2_get_len(&file_strcache, str) /* FIXME: replace this and related checks ... */
704 
705 #endif /* CONFIG_WITH_STRCACHE2 */
706 
707 #ifdef  HAVE_VFORK_H
708 # include <vfork.h>
709 #endif
710 
711 /* We omit these declarations on non-POSIX systems which define _POSIX_VERSION,
712    because such systems often declare them in header files anyway.  */
713 
714 #if !defined (__GNU_LIBRARY__) && !defined (POSIX) && !defined (_POSIX_VERSION) && !defined(WINDOWS32)
715 
716 long int atol ();
717 # ifndef VMS
718 long int lseek ();
719 # endif
720 
721 #endif  /* Not GNU C library or POSIX.  */
722 
723 #ifdef  HAVE_GETCWD
724 # if !defined(VMS) && !defined(__DECC) && !defined(_MSC_VER) /* bird: MSC */
725 char *getcwd ();
726 # endif
727 #else
728 char *getwd ();
729 # define getcwd(buf, len)       getwd (buf)
730 #endif
731 
732 #if !HAVE_STRCASECMP
733 # if HAVE_STRICMP
734 #  define strcasecmp stricmp
735 # elif HAVE_STRCMPI
736 #  define strcasecmp strcmpi
737 # else
738 /* Create our own, in misc.c */
739 int strcasecmp (const char *s1, const char *s2);
740 # endif
741 #endif
742 
743 #if !HAVE_STRNCASECMP
744 # if HAVE_STRNICMP
745 #  define strncasecmp strnicmp
746 # elif HAVE_STRNCMPI
747 #  define strncasecmp strncmpi
748 # else
749 /* Create our own, in misc.c */
750 int strncasecmp (const char *s1, const char *s2, int n);
751 # endif
752 #endif
753 
754 extern const struct floc *reading_file;
755 extern const struct floc **expanding_var;
756 
757 #if !defined(_MSC_VER) /* bird */
758 extern char **environ;
759 #endif
760 
761 extern int just_print_flag, silent_flag, ignore_errors_flag, keep_going_flag;
762 extern int print_data_base_flag, question_flag, touch_flag, always_make_flag;
763 extern int env_overrides, no_builtin_rules_flag, no_builtin_variables_flag;
764 extern int print_version_flag, print_directory_flag, check_symlink_flag;
765 extern int warn_undefined_variables_flag, posix_pedantic, not_parallel;
766 extern int second_expansion, clock_skew_detected, rebuilding_makefiles;
767 extern int one_shell;
768 
769 #ifdef CONFIG_WITH_2ND_TARGET_EXPANSION
770 extern int second_target_expansion;
771 #endif
772 #ifdef CONFIG_PRETTY_COMMAND_PRINTING
773 extern int pretty_command_printing;
774 #endif
775 #ifdef CONFIG_WITH_PRINT_TIME_SWITCH
776 extern int print_time_min, print_time_width;
777 #endif
778 #if defined (CONFIG_WITH_MAKE_STATS) || defined (CONFIG_WITH_MINIMAL_STATS)
779 extern int make_expensive_statistics;
780 #endif
781 
782 
783 /* can we run commands via 'sh -c xxx' or must we use batch files? */
784 extern int batch_mode_shell;
785 
786 /* Resetting the command script introduction prefix character.  */
787 #define RECIPEPREFIX_NAME          ".RECIPEPREFIX"
788 #define RECIPEPREFIX_DEFAULT       '\t'
789 extern char cmd_prefix;
790 
791 extern unsigned int job_slots;
792 extern int job_fds[2];
793 extern int job_rfd;
794 #ifndef NO_FLOAT
795 extern double max_load_average;
796 #else
797 extern int max_load_average;
798 #endif
799 
800 extern char *program;
801 extern char *starting_directory;
802 extern unsigned int makelevel;
803 extern char *version_string, *remote_description, *make_host;
804 
805 extern unsigned int commands_started;
806 
807 extern int handling_fatal_signal;
808 
809 
810 #ifndef MIN
811 #define MIN(_a,_b) ((_a)<(_b)?(_a):(_b))
812 #endif
813 #ifndef MAX
814 #define MAX(_a,_b) ((_a)>(_b)?(_a):(_b))
815 #endif
816 
817 #ifdef VMS
818 #  define MAKE_SUCCESS 1
819 #  define MAKE_TROUBLE 2
820 #  define MAKE_FAILURE 3
821 #else
822 #  define MAKE_SUCCESS 0
823 #  define MAKE_TROUBLE 1
824 #  define MAKE_FAILURE 2
825 #endif
826 
827 /* Set up heap debugging library dmalloc.  */
828 
829 #ifdef HAVE_DMALLOC_H
830 #include <dmalloc.h>
831 #endif
832 
833 #ifndef initialize_main
834 # ifdef __EMX__
835 #  define initialize_main(pargc, pargv) \
836                           { _wildcard(pargc, pargv); _response(pargc, pargv); }
837 # else
838 #  define initialize_main(pargc, pargv)
839 # endif
840 #endif
841 
842 #ifdef __EMX__
843 # if !defined chdir
844 #  define chdir _chdir2
845 # endif
846 # if !defined getcwd
847 #  define getcwd _getcwd2
848 # endif
849 
850 /* NO_CHDIR2 causes make not to use _chdir2() and _getcwd2() instead of
851    chdir() and getcwd(). This avoids some error messages for the
852    make testsuite but restricts the drive letter support. */
853 # ifdef NO_CHDIR2
854 #  warning NO_CHDIR2: usage of drive letters restricted
855 #  undef chdir
856 #  undef getcwd
857 # endif
858 #endif
859 
860 #ifndef initialize_main
861 # define initialize_main(pargc, pargv)
862 #endif
863 
864 
865 /* Some systems (like Solaris, PTX, etc.) do not support the SA_RESTART flag
866    properly according to POSIX.  So, we try to wrap common system calls with
867    checks for EINTR.  Note that there are still plenty of system calls that
868    can fail with EINTR but this, reportedly, gets the vast majority of
869    failure cases.  If you still experience failures you'll need to either get
870    a system where SA_RESTART works, or you need to avoid -j.  */
871 
872 #define EINTRLOOP(_v,_c)   while (((_v)=_c)==-1 && errno==EINTR)
873 
874 /* While system calls that return integers are pretty consistent about
875    returning -1 on failure and setting errno in that case, functions that
876    return pointers are not always so well behaved.  Sometimes they return
877    NULL for expected behavior: one good example is readdir() which returns
878    NULL at the end of the directory--and _doesn't_ reset errno.  So, we have
879    to do it ourselves here.  */
880 
881 #define ENULLLOOP(_v,_c)   do { errno = 0; (_v) = _c; } \
882                            while((_v)==0 && errno==EINTR)
883 
884 
885 #if defined(__EMX__) && defined(CONFIG_WITH_OPTIMIZATION_HACKS) /* bird: saves 40-100ms on libc. */
886 static inline void *__my_rawmemchr (const void *__s, int __c);
887 #undef strchr
888 #define strchr(s, c) \
889   (__extension__ (__builtin_constant_p (c)				      \
890 		  ? ((c) == '\0'					      \
891 		     ? (char *) __my_rawmemchr ((s), (c))			      \
892 		     : __my_strchr_c ((s), ((c) & 0xff) << 8))		      \
893 		  : __my_strchr_g ((s), (c))))
__my_strchr_c(const char * __s,int __c)894 static inline char *__my_strchr_c (const char *__s, int __c)
895 {
896   register unsigned long int __d0;
897   register char *__res;
898   __asm__ __volatile__
899     ("1:\n\t"
900      "movb	(%0),%%al\n\t"
901      "cmpb	%%ah,%%al\n\t"
902      "je	2f\n\t"
903      "leal	1(%0),%0\n\t"
904      "testb	%%al,%%al\n\t"
905      "jne	1b\n\t"
906      "xorl	%0,%0\n"
907      "2:"
908      : "=r" (__res), "=&a" (__d0)
909      : "0" (__s), "1" (__c),
910        "m" ( *(struct { char __x[0xfffffff]; } *)__s)
911      : "cc");
912   return __res;
913 }
914 
__my_strchr_g(__const char * __s,int __c)915 static inline char *__my_strchr_g (__const char *__s, int __c)
916 {
917   register unsigned long int __d0;
918   register char *__res;
919   __asm__ __volatile__
920     ("movb	%%al,%%ah\n"
921      "1:\n\t"
922      "movb	(%0),%%al\n\t"
923      "cmpb	%%ah,%%al\n\t"
924      "je	2f\n\t"
925      "leal	1(%0),%0\n\t"
926      "testb	%%al,%%al\n\t"
927      "jne	1b\n\t"
928      "xorl	%0,%0\n"
929      "2:"
930      : "=r" (__res), "=&a" (__d0)
931      : "0" (__s), "1" (__c),
932        "m" ( *(struct { char __x[0xfffffff]; } *)__s)
933      : "cc");
934   return __res;
935 }
936 
__my_rawmemchr(const void * __s,int __c)937 static inline void *__my_rawmemchr (const void *__s, int __c)
938 {
939   register unsigned long int __d0;
940   register unsigned char *__res;
941   __asm__ __volatile__
942     ("cld\n\t"
943      "repne; scasb\n\t"
944      : "=D" (__res), "=&c" (__d0)
945      : "a" (__c), "0" (__s), "1" (0xffffffff),
946        "m" ( *(struct { char __x[0xfffffff]; } *)__s)
947      : "cc");
948   return __res - 1;
949 }
950 
951 #undef memchr
952 #define memchr(a,b,c) __my_memchr((a),(b),(c))
__my_memchr(__const void * __s,int __c,size_t __n)953 static inline void *__my_memchr (__const void *__s, int __c, size_t __n)
954 {
955   register unsigned long int __d0;
956   register unsigned char *__res;
957   if (__n == 0)
958     return NULL;
959   __asm__ __volatile__
960     ("repne; scasb\n\t"
961      "je	1f\n\t"
962      "movl	$1,%0\n"
963      "1:"
964      : "=D" (__res), "=&c" (__d0)
965      : "a" (__c), "0" (__s), "1" (__n),
966        "m" ( *(struct { __extension__ char __x[__n]; } *)__s)
967      : "cc");
968   return __res - 1;
969 }
970 
971 #endif /* __EMX__ (bird) */
972 
973 #ifdef CONFIG_WITH_IF_CONDITIONALS
974 extern int expr_eval_if_conditionals(const char *line, const struct floc *flocp);
975 extern char *expr_eval_to_string(char *o, const char *expr);
976 #endif
977 
978 #ifdef KMK
979 extern char *abspath(const char *name, char *apath);
980 extern char *func_breakpoint(char *o, char **argv, const char *funcname);
981 # ifdef KBUILD_OS_WINDOWS
982 extern void dir_cache_invalid_after_job (void);
983 extern void dir_cache_invalid_all (void);
984 extern void dir_cache_invalid_missing (void);
985 extern int dir_cache_volatile_dir (const char *dir);
986 extern int dir_cache_deleted_directory(const char *pszDir);
987 # endif
988 #endif
989 
990 #if defined (CONFIG_WITH_NANOTS) || defined (CONFIG_WITH_PRINT_TIME_SWITCH)
991 /* misc.c */
992 extern big_int nano_timestamp (void);
993 extern int format_elapsed_nano (char *buf, size_t size, big_int ts);
994 #endif
995 
996