1/* Miscellaneous global declarations and portability cruft for GNU Make.
2Copyright (C) 1988, 1989, 1990, 1991, 1992, 1993, 1994, 1995, 1996, 1997,
31998, 1999, 2000, 2001, 2002, 2003, 2004, 2005, 2006 Free Software
4Foundation, Inc.
5This file is part of GNU Make.
6
7GNU Make is free software; you can redistribute it and/or modify it under the
8terms of the GNU General Public License as published by the Free Software
9Foundation; either version 2, or (at your option) any later version.
10
11GNU Make is distributed in the hope that it will be useful, but WITHOUT ANY
12WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR
13A PARTICULAR PURPOSE.  See the GNU General Public License for more details.
14
15You should have received a copy of the GNU General Public License along with
16GNU Make; see the file COPYING.  If not, write to the Free Software
17Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.  */
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/* AIX requires this to be the first thing in the file.  */
27#ifndef __GNUC__
28# if HAVE_ALLOCA_H
29#  include <alloca.h>
30# else
31#  ifdef _AIX
32 #pragma alloca
33#  else
34#   ifndef alloca /* predefined by HP cc +Olibcalls */
35char *alloca ();
36#   endif
37#  endif
38# endif
39#endif
40
41
42/* Use prototypes if available.  */
43#if defined (__cplusplus) || defined (__STDC__)
44# undef  PARAMS
45# define PARAMS(protos)  protos
46#else /* Not C++ or ANSI C.  */
47# undef  PARAMS
48# define PARAMS(protos)  ()
49#endif /* C++ or ANSI C.  */
50
51/* Specify we want GNU source code.  This must be defined before any
52   system headers are included.  */
53
54#define _GNU_SOURCE 1
55
56
57#ifdef  CRAY
58/* This must happen before #include <signal.h> so
59   that the declaration therein is changed.  */
60# define signal bsdsignal
61#endif
62
63/* If we're compiling for the dmalloc debugger, turn off string inlining.  */
64#if defined(HAVE_DMALLOC_H) && defined(__GNUC__)
65# define __NO_STRING_INLINES
66#endif
67
68#include <sys/types.h>
69#include <sys/stat.h>
70#include <signal.h>
71#include <stdio.h>
72#include <ctype.h>
73#ifdef HAVE_SYS_TIMEB_H
74/* SCO 3.2 "devsys 4.2" has a prototype for `ftime' in <time.h> that bombs
75   unless <sys/timeb.h> has been included first.  Does every system have a
76   <sys/timeb.h>?  If any does not, configure should check for it.  */
77# include <sys/timeb.h>
78#endif
79
80#if TIME_WITH_SYS_TIME
81# include <sys/time.h>
82# include <time.h>
83#else
84# if HAVE_SYS_TIME_H
85#  include <sys/time.h>
86# else
87#  include <time.h>
88# endif
89#endif
90
91#include <errno.h>
92
93#ifndef errno
94extern int errno;
95#endif
96
97#ifndef isblank
98# define isblank(c)     ((c) == ' ' || (c) == '\t')
99#endif
100
101#ifdef  HAVE_UNISTD_H
102# include <unistd.h>
103/* Ultrix's unistd.h always defines _POSIX_VERSION, but you only get
104   POSIX.1 behavior with `cc -YPOSIX', which predefines POSIX itself!  */
105# if defined (_POSIX_VERSION) && !defined (ultrix) && !defined (VMS)
106#  define POSIX 1
107# endif
108#endif
109
110/* Some systems define _POSIX_VERSION but are not really POSIX.1.  */
111#if (defined (butterfly) || defined (__arm) || (defined (__mips) && defined (_SYSTYPE_SVR3)) || (defined (sequent) && defined (i386)))
112# undef POSIX
113#endif
114
115#if !defined (POSIX) && defined (_AIX) && defined (_POSIX_SOURCE)
116# define POSIX 1
117#endif
118
119#ifndef RETSIGTYPE
120# define RETSIGTYPE     void
121#endif
122
123#ifndef sigmask
124# define sigmask(sig)   (1 << ((sig) - 1))
125#endif
126
127#ifndef HAVE_SA_RESTART
128# define SA_RESTART 0
129#endif
130
131#ifdef  HAVE_LIMITS_H
132# include <limits.h>
133#endif
134#ifdef  HAVE_SYS_PARAM_H
135# include <sys/param.h>
136#endif
137
138#ifndef PATH_MAX
139# ifndef POSIX
140#  define PATH_MAX      MAXPATHLEN
141# endif
142#endif
143#ifndef MAXPATHLEN
144# define MAXPATHLEN 1024
145#endif
146
147#ifdef  PATH_MAX
148# define GET_PATH_MAX   PATH_MAX
149# define PATH_VAR(var)  char var[PATH_MAX]
150#else
151# define NEED_GET_PATH_MAX 1
152# define GET_PATH_MAX   (get_path_max ())
153# define PATH_VAR(var)  char *var = (char *) alloca (GET_PATH_MAX)
154extern unsigned int get_path_max PARAMS ((void));
155#endif
156
157#ifndef CHAR_BIT
158# define CHAR_BIT 8
159#endif
160
161/* Nonzero if the integer type T is signed.  */
162#define INTEGER_TYPE_SIGNED(t) ((t) -1 < 0)
163
164/* The minimum and maximum values for the integer type T.
165   Use ~ (t) 0, not -1, for portability to 1's complement hosts.  */
166#define INTEGER_TYPE_MINIMUM(t) \
167  (! INTEGER_TYPE_SIGNED (t) ? (t) 0 : ~ (t) 0 << (sizeof (t) * CHAR_BIT - 1))
168#define INTEGER_TYPE_MAXIMUM(t) (~ (t) 0 - INTEGER_TYPE_MINIMUM (t))
169
170#ifndef CHAR_MAX
171# define CHAR_MAX INTEGER_TYPE_MAXIMUM (char)
172#endif
173
174#ifdef STAT_MACROS_BROKEN
175# ifdef S_ISREG
176#  undef S_ISREG
177# endif
178# ifdef S_ISDIR
179#  undef S_ISDIR
180# endif
181#endif  /* STAT_MACROS_BROKEN.  */
182
183#ifndef S_ISREG
184# define S_ISREG(mode)  (((mode) & S_IFMT) == S_IFREG)
185#endif
186#ifndef S_ISDIR
187# define S_ISDIR(mode)  (((mode) & S_IFMT) == S_IFDIR)
188#endif
189
190#ifdef VMS
191# include <types.h>
192# include <unixlib.h>
193# include <unixio.h>
194# include <perror.h>
195/* Needed to use alloca on VMS.  */
196# include <builtins.h>
197#endif
198
199#ifndef __attribute__
200/* This feature is available in gcc versions 2.5 and later.  */
201# if __GNUC__ < 2 || (__GNUC__ == 2 && __GNUC_MINOR__ < 5) || __STRICT_ANSI__
202#  define __attribute__(x)
203# endif
204/* The __-protected variants of `format' and `printf' attributes
205   are accepted by gcc versions 2.6.4 (effectively 2.7) and later.  */
206# if __GNUC__ < 2 || (__GNUC__ == 2 && __GNUC_MINOR__ < 7)
207#  define __format__ format
208#  define __printf__ printf
209# endif
210#endif
211#define UNUSED  __attribute__ ((unused))
212
213#if defined (STDC_HEADERS) || defined (__GNU_LIBRARY__)
214# include <stdlib.h>
215# include <string.h>
216# define ANSI_STRING 1
217#else   /* No standard headers.  */
218# ifdef HAVE_STRING_H
219#  include <string.h>
220#  define ANSI_STRING 1
221# else
222#  include <strings.h>
223# endif
224# ifdef HAVE_MEMORY_H
225#  include <memory.h>
226# endif
227# ifdef HAVE_STDLIB_H
228#  include <stdlib.h>
229# else
230extern char *malloc PARAMS ((int));
231extern char *realloc PARAMS ((char *, int));
232extern void free PARAMS ((char *));
233
234extern void abort PARAMS ((void)) __attribute__ ((noreturn));
235extern void exit PARAMS ((int)) __attribute__ ((noreturn));
236# endif /* HAVE_STDLIB_H.  */
237
238#endif /* Standard headers.  */
239
240/* These should be in stdlib.h.  Make sure we have them.  */
241#ifndef EXIT_SUCCESS
242# define EXIT_SUCCESS 0
243#endif
244#ifndef EXIT_FAILURE
245# define EXIT_FAILURE 0
246#endif
247
248#ifdef  ANSI_STRING
249
250# ifndef bcmp
251#  define bcmp(s1, s2, n)   memcmp ((s1), (s2), (n))
252# endif
253# ifndef bzero
254#  define bzero(s, n)       memset ((s), 0, (n))
255# endif
256# if defined(HAVE_MEMMOVE) && !defined(bcopy)
257#  define bcopy(s, d, n)    memmove ((d), (s), (n))
258# endif
259
260#else   /* Not ANSI_STRING.  */
261
262# ifndef HAVE_STRCHR
263#  define strchr(s, c)      index((s), (c))
264#  define strrchr(s, c)     rindex((s), (c))
265# endif
266
267# ifndef bcmp
268extern int bcmp PARAMS ((const char *, const char *, int));
269# endif
270# ifndef bzero
271extern void bzero PARAMS ((char *, int));
272#endif
273# ifndef bcopy
274extern void bcopy PARAMS ((const char *b1, char *b2, int));
275# endif
276
277/* SCO Xenix has a buggy macro definition in <string.h>.  */
278#undef  strerror
279#if !defined(__DECC)
280extern char *strerror PARAMS ((int errnum));
281#endif
282
283#endif  /* !ANSI_STRING.  */
284#undef  ANSI_STRING
285
286#if HAVE_INTTYPES_H
287# include <inttypes.h>
288#endif
289#define FILE_TIMESTAMP uintmax_t
290
291#if !defined(HAVE_STRSIGNAL)
292extern char *strsignal PARAMS ((int signum));
293#endif
294
295/* ISDIGIT offers the following features:
296   - Its arg may be any int or unsigned int; it need not be an unsigned char.
297   - It's guaranteed to evaluate its argument exactly once.
298      NOTE!  Make relies on this behavior, don't change it!
299   - It's typically faster.
300   POSIX 1003.2-1992 section 2.5.2.1 page 50 lines 1556-1558 says that
301   only '0' through '9' are digits.  Prefer ISDIGIT to isdigit() unless
302   it's important to use the locale's definition of `digit' even when the
303   host does not conform to POSIX.  */
304#define ISDIGIT(c) ((unsigned) (c) - '0' <= 9)
305
306#ifndef iAPX286
307# define streq(a, b) \
308   ((a) == (b) || \
309    (*(a) == *(b) && (*(a) == '\0' || !strcmp ((a) + 1, (b) + 1))))
310# ifdef HAVE_CASE_INSENSITIVE_FS
311/* This is only used on Windows/DOS platforms, so we assume strcmpi().  */
312#  define strieq(a, b) \
313    ((a) == (b) \
314     || (tolower((unsigned char)*(a)) == tolower((unsigned char)*(b)) \
315         && (*(a) == '\0' || !strcmpi ((a) + 1, (b) + 1))))
316# else
317#  define strieq(a, b) streq(a, b)
318# endif
319#else
320/* Buggy compiler can't handle this.  */
321# define streq(a, b) (strcmp ((a), (b)) == 0)
322# define strieq(a, b) (strcmp ((a), (b)) == 0)
323#endif
324#define strneq(a, b, l) (strncmp ((a), (b), (l)) == 0)
325#ifdef  VMS
326extern int strcmpi (const char *,const char *);
327#endif
328
329#if defined(__GNUC__) || defined(ENUM_BITFIELDS)
330# define ENUM_BITFIELD(bits)    :bits
331#else
332# define ENUM_BITFIELD(bits)
333#endif
334
335/* Handle gettext and locales.  */
336
337#if HAVE_LOCALE_H
338# include <locale.h>
339#else
340# define setlocale(category, locale)
341#endif
342
343#include <gettext.h>
344
345#define _(msgid)            gettext (msgid)
346#define N_(msgid)           gettext_noop (msgid)
347#define S_(msg1,msg2,num)   ngettext (msg1,msg2,num)
348
349/* Handle other OSs.  */
350#if defined(HAVE_DOS_PATHS)
351# define PATH_SEPARATOR_CHAR ';'
352#elif defined(VMS)
353# define PATH_SEPARATOR_CHAR ','
354#else
355# define PATH_SEPARATOR_CHAR ':'
356#endif
357
358/* This is needed for getcwd() and chdir().  */
359#if defined(_MSC_VER) || defined(__BORLANDC__)
360# include <direct.h>
361#endif
362
363#ifdef WINDOWS32
364# include <fcntl.h>
365# include <malloc.h>
366# define pipe(p) _pipe(p, 512, O_BINARY)
367# define kill(pid,sig) w32_kill(pid,sig)
368
369extern void sync_Path_environment(void);
370extern int kill(int pid, int sig);
371extern char *end_of_token_w32(char *s, char stopchar);
372extern int find_and_set_default_shell(char *token);
373
374/* indicates whether or not we have Bourne shell */
375extern int no_default_sh_exe;
376
377/* is default_shell unixy? */
378extern int unixy_shell;
379#endif  /* WINDOWS32 */
380
381struct floc
382  {
383    const char *filenm;
384    unsigned long lineno;
385  };
386#define NILF ((struct floc *)0)
387
388#define STRING_SIZE_TUPLE(_s) (_s), (sizeof (_s)-1)
389
390
391/* We have to have stdarg.h or varargs.h AND v*printf or doprnt to use
392   variadic versions of these functions.  */
393
394#if HAVE_STDARG_H || HAVE_VARARGS_H
395# if HAVE_VPRINTF || HAVE_DOPRNT
396#  define USE_VARIADIC 1
397# endif
398#endif
399
400#if HAVE_ANSI_COMPILER && USE_VARIADIC && HAVE_STDARG_H
401extern void message (int prefix, const char *fmt, ...)
402                     __attribute__ ((__format__ (__printf__, 2, 3)));
403extern void error (const struct floc *flocp, const char *fmt, ...)
404                   __attribute__ ((__format__ (__printf__, 2, 3)));
405extern void fatal (const struct floc *flocp, const char *fmt, ...)
406                   __attribute__ ((noreturn, __format__ (__printf__, 2, 3)));
407#else
408extern void message ();
409extern void error ();
410extern void fatal ();
411#endif
412
413extern void die PARAMS ((int)) __attribute__ ((noreturn));
414extern void log_working_directory PARAMS ((int));
415extern void pfatal_with_name PARAMS ((const char *)) __attribute__ ((noreturn));
416extern void perror_with_name PARAMS ((const char *, const char *));
417extern char *savestring PARAMS ((const char *, unsigned int));
418extern char *concat PARAMS ((const char *, const char *, const char *));
419extern char *xmalloc PARAMS ((unsigned int));
420extern char *xrealloc PARAMS ((char *, unsigned int));
421extern char *xstrdup PARAMS ((const char *));
422extern char *find_next_token PARAMS ((char **, unsigned int *));
423extern char *next_token PARAMS ((const char *));
424extern char *end_of_token PARAMS ((const char *));
425extern void collapse_continuations PARAMS ((char *));
426extern char *lindex PARAMS ((const char *, const char *, int));
427extern int alpha_compare PARAMS ((const void *, const void *));
428extern void print_spaces PARAMS ((unsigned int));
429extern char *find_percent PARAMS ((char *));
430extern FILE *open_tmpfile PARAMS ((char **, const char *));
431
432#ifndef NO_ARCHIVES
433extern int ar_name PARAMS ((char *));
434extern void ar_parse_name PARAMS ((char *, char **, char **));
435extern int ar_touch PARAMS ((char *));
436extern time_t ar_member_date PARAMS ((char *));
437#endif
438
439extern int dir_file_exists_p PARAMS ((char *, char *));
440extern int file_exists_p PARAMS ((char *));
441extern int file_impossible_p PARAMS ((char *));
442extern void file_impossible PARAMS ((char *));
443extern char *dir_name PARAMS ((char *));
444extern void hash_init_directories PARAMS ((void));
445
446extern void define_default_variables PARAMS ((void));
447extern void set_default_suffixes PARAMS ((void));
448extern void install_default_suffix_rules PARAMS ((void));
449extern void install_default_implicit_rules PARAMS ((void));
450
451extern void build_vpath_lists PARAMS ((void));
452extern void construct_vpath_list PARAMS ((char *pattern, char *dirpath));
453extern int vpath_search PARAMS ((char **file, FILE_TIMESTAMP *mtime_ptr));
454extern int gpath_search PARAMS ((char *file, unsigned int len));
455
456extern void construct_include_path PARAMS ((char **arg_dirs));
457
458extern void user_access PARAMS ((void));
459extern void make_access PARAMS ((void));
460extern void child_access PARAMS ((void));
461
462extern void close_stdout PARAMS ((void));
463
464extern char *strip_whitespace PARAMS ((const char **begpp, const char **endpp));
465
466/* String caching  */
467extern void strcache_init PARAMS ((void));
468extern void strcache_print_stats PARAMS ((const char *prefix));
469extern int strcache_iscached PARAMS ((const char *str));
470extern const char *strcache_add PARAMS ((const char *str));
471extern const char *strcache_add_len PARAMS ((const char *str, int len));
472extern int strcache_setbufsize PARAMS ((int size));
473
474#ifdef  HAVE_VFORK_H
475# include <vfork.h>
476#endif
477
478/* We omit these declarations on non-POSIX systems which define _POSIX_VERSION,
479   because such systems often declare them in header files anyway.  */
480
481#if !defined (__GNU_LIBRARY__) && !defined (POSIX) && !defined (_POSIX_VERSION) && !defined(WINDOWS32)
482
483extern long int atol ();
484# ifndef VMS
485extern long int lseek ();
486# endif
487
488#endif  /* Not GNU C library or POSIX.  */
489
490#ifdef  HAVE_GETCWD
491# if !defined(VMS) && !defined(__DECC)
492extern char *getcwd ();
493# endif
494#else
495extern char *getwd ();
496# define getcwd(buf, len)       getwd (buf)
497#endif
498
499extern const struct floc *reading_file;
500extern const struct floc **expanding_var;
501
502extern char **environ;
503
504extern int just_print_flag, silent_flag, ignore_errors_flag, keep_going_flag;
505extern int print_data_base_flag, question_flag, touch_flag, always_make_flag;
506extern int env_overrides, no_builtin_rules_flag, no_builtin_variables_flag;
507extern int print_version_flag, print_directory_flag, check_symlink_flag;
508extern int warn_undefined_variables_flag, posix_pedantic, not_parallel;
509extern int second_expansion, clock_skew_detected, rebuilding_makefiles;
510
511/* can we run commands via 'sh -c xxx' or must we use batch files? */
512extern int batch_mode_shell;
513
514extern unsigned int job_slots;
515extern int job_fds[2];
516extern int job_rfd;
517#ifndef NO_FLOAT
518extern double max_load_average;
519#else
520extern int max_load_average;
521#endif
522
523extern char *program;
524extern char *starting_directory;
525extern unsigned int makelevel;
526extern char *version_string, *remote_description, *make_host;
527
528extern unsigned int commands_started;
529
530extern int handling_fatal_signal;
531
532
533#ifndef MIN
534#define MIN(_a,_b) ((_a)<(_b)?(_a):(_b))
535#endif
536#ifndef MAX
537#define MAX(_a,_b) ((_a)>(_b)?(_a):(_b))
538#endif
539
540#ifdef VMS
541#  define MAKE_SUCCESS 1
542#  define MAKE_TROUBLE 2
543#  define MAKE_FAILURE 3
544#else
545#  define MAKE_SUCCESS 0
546#  define MAKE_TROUBLE 1
547#  define MAKE_FAILURE 2
548#endif
549
550/* Set up heap debugging library dmalloc.  */
551
552#ifdef HAVE_DMALLOC_H
553#include <dmalloc.h>
554#endif
555
556#ifndef initialize_main
557# ifdef __EMX__
558#  define initialize_main(pargc, pargv) \
559                          { _wildcard(pargc, pargv); _response(pargc, pargv); }
560# else
561#  define initialize_main(pargc, pargv)
562# endif
563#endif
564
565
566#ifdef __EMX__
567# if !HAVE_STRCASECMP
568#  define strcasecmp stricmp
569# endif
570
571# if !defined chdir
572#  define chdir _chdir2
573# endif
574# if !defined getcwd
575#  define getcwd _getcwd2
576# endif
577
578/* NO_CHDIR2 causes make not to use _chdir2() and _getcwd2() instead of
579   chdir() and getcwd(). This avoids some error messages for the
580   make testsuite but restricts the drive letter support. */
581# ifdef NO_CHDIR2
582#  warning NO_CHDIR2: usage of drive letters restricted
583#  undef chdir
584#  undef getcwd
585# endif
586#endif
587
588#ifndef initialize_main
589# define initialize_main(pargc, pargv)
590#endif
591
592
593/* Some systems (like Solaris, PTX, etc.) do not support the SA_RESTART flag
594   properly according to POSIX.  So, we try to wrap common system calls with
595   checks for EINTR.  Note that there are still plenty of system calls that
596   can fail with EINTR but this, reportedly, gets the vast majority of
597   failure cases.  If you still experience failures you'll need to either get
598   a system where SA_RESTART works, or you need to avoid -j.  */
599
600#define EINTRLOOP(_v,_c)   while (((_v)=_c)==-1 && errno==EINTR)
601
602/* While system calls that return integers are pretty consistent about
603   returning -1 on failure and setting errno in that case, functions that
604   return pointers are not always so well behaved.  Sometimes they return
605   NULL for expected behavior: one good example is readdir() which returns
606   NULL at the end of the directory--and _doesn't_ reset errno.  So, we have
607   to do it ourselves here.  */
608
609#define ENULLLOOP(_v,_c)   do{ errno = 0; \
610                               while (((_v)=_c)==0 && errno==EINTR); }while(0)
611
612