1 /* Reading and parsing of makefiles for GNU Make.
2 Copyright (C) 1988, 89, 90, 91, 92, 93, 94 Free Software Foundation, Inc.
3 This file is part of GNU Make.
5 GNU Make is free software; you can redistribute it and/or modify
6 it under the terms of the GNU General Public License as published by
7 the Free Software Foundation; either version 2, or (at your option)
10 GNU Make is distributed in the hope that it will be useful,
11 but WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 GNU General Public License for more details.
15 You should have received a copy of the GNU General Public License
16 along with GNU Make; see the file COPYING. If not, write to
17 the Free Software Foundation, 675 Mass Ave, Cambridge, MA 02139, USA. */
25 /* This is POSIX.2, but most systems using -DPOSIX probably don't have it. */
29 #include "glob/glob.h"
33 struct passwd
*getpwnam ();
36 static int read_makefile ();
37 static unsigned int readline (), do_define ();
38 static int conditional_line ();
39 static void record_files ();
40 static char *find_semicolon ();
43 /* A `struct linebuffer' is a structure which holds a line of text.
44 `readline' reads a line from a stream into a linebuffer
45 and works regardless of the length of the line. */
49 /* Note: This is the number of bytes malloc'ed for `buffer'
50 It does not indicate `buffer's real length.
51 Instead, a null char indicates end-of-string. */
56 #define initbuffer(lb) (lb)->buffer = (char *) xmalloc ((lb)->size = 200)
57 #define freebuffer(lb) free ((lb)->buffer)
60 /* A `struct conditionals' contains the information describing
61 all the active conditionals in a makefile.
63 The global variable `conditionals' contains the conditionals
64 information for the current makefile. It is initialized from
65 the static structure `toplevel_conditionals' and is later changed
66 to new structures for included makefiles. */
70 unsigned int if_cmds
; /* Depth of conditional nesting. */
71 unsigned int allocated
; /* Elts allocated in following arrays. */
72 char *ignoring
; /* Are we ignoring or interepreting? */
73 char *seen_else
; /* Have we already seen an `else'? */
76 static struct conditionals toplevel_conditionals
;
77 static struct conditionals
*conditionals
= &toplevel_conditionals
;
80 /* Default directories to search for include files in */
82 static char *default_include_directories
[] =
91 /* List of directories to search for include files in */
93 static char **include_directories
;
95 /* Maximum length of an element of the above. */
97 static unsigned int max_incl_len
;
99 /* The filename and pointer to line number of the
100 makefile currently being read in. */
102 char *reading_filename
;
103 unsigned int *reading_lineno_ptr
;
105 /* The chain of makefiles read by read_makefile. */
107 static struct dep
*read_makefiles
= 0;
109 /* Read in all the makefiles and return the chain of their names. */
112 read_all_makefiles (makefiles
)
115 unsigned int num_makefiles
= 0;
118 puts ("Reading makefiles...");
120 /* If there's a non-null variable MAKEFILES, its value is a list of
121 files to read first thing. But don't let it prevent reading the
122 default makefiles and don't let the default goal come from there. */
130 /* Turn off --warn-undefined-variables while we expand MAKEFILES. */
131 int save
= warn_undefined_variables_flag
;
132 warn_undefined_variables_flag
= 0;
134 value
= allocated_variable_expand ("$(MAKEFILES)");
136 warn_undefined_variables_flag
= save
;
139 /* Set NAME to the start of next token and LENGTH to its length.
140 MAKEFILES is updated for finding remaining tokens. */
142 while ((name
= find_next_token (&p
, &length
)) != 0)
146 (void) read_makefile (name
,
147 RM_NO_DEFAULT_GOAL
| RM_INCLUDED
| RM_DONTCARE
);
153 /* Read makefiles specified with -f switches. */
156 while (*makefiles
!= 0)
158 struct dep
*tail
= read_makefiles
;
159 register struct dep
*d
;
161 if (! read_makefile (*makefiles
, 0))
162 perror_with_name ("", *makefiles
);
164 /* Find the right element of read_makefiles. */
166 while (d
->next
!= tail
)
169 /* Use the storage read_makefile allocates. */
170 *makefiles
= dep_name (d
);
175 /* If there were no -f switches, try the default names. */
177 if (num_makefiles
== 0)
179 static char *default_makefiles
[] =
180 { "GNUmakefile", "makefile", "Makefile", 0 };
181 register char **p
= default_makefiles
;
182 while (*p
!= 0 && !file_exists_p (*p
))
187 if (! read_makefile (*p
, 0))
188 perror_with_name ("", *p
);
192 /* No default makefile was found. Add the default makefiles to the
193 `read_makefiles' chain so they will be updated if possible. */
194 struct dep
*tail
= read_makefiles
;
195 for (p
= default_makefiles
; *p
!= 0; ++p
)
197 struct dep
*d
= (struct dep
*) xmalloc (sizeof (struct dep
));
199 d
->file
= enter_file (*p
);
200 d
->file
->dontcare
= 1;
201 /* Tell update_goal_chain to bail out as soon as this file is
202 made, and main not to die if we can't make this file. */
203 d
->changed
= RM_DONTCARE
;
215 return read_makefiles
;
218 /* Read file FILENAME as a makefile and add its contents to the data base.
220 FLAGS contains bits as above.
222 FILENAME is added to the `read_makefiles' chain.
224 Returns 1 if a file was found and read, 0 if not. */
227 read_makefile (filename
, flags
)
231 static char *collapsed
= 0;
232 static unsigned int collapsed_length
= 0;
233 register FILE *infile
;
234 struct linebuffer lb
;
235 unsigned int commands_len
= 200;
236 char *commands
= (char *) xmalloc (200);
237 unsigned int commands_idx
= 0;
238 unsigned int commands_started
;
241 int ignoring
= 0, in_ignored_define
= 0;
242 int no_targets
= 0; /* Set when reading a rule without targets. */
243 char *passed_filename
= filename
;
245 struct nameseq
*filenames
= 0;
247 unsigned int lineno
= 1;
248 unsigned int nlines
= 0;
250 char *pattern
= 0, *pattern_percent
;
254 #define record_waiting_files() \
257 if (filenames != 0) \
258 record_files (filenames, pattern, pattern_percent, deps, \
259 commands_started, commands, commands_idx, \
260 two_colon, filename, lineno, \
261 !(flags & RM_NO_DEFAULT_GOAL)); \
267 #ifdef lint /* Suppress `used before set' messages. */
273 printf ("Reading makefile `%s'", filename
);
274 if (flags
& RM_NO_DEFAULT_GOAL
)
275 printf (" (no default goal)");
276 if (flags
& RM_INCLUDED
)
277 printf (" (search path)");
278 if (flags
& RM_DONTCARE
)
279 printf (" (don't care)");
280 if (flags
& RM_NO_TILDE
)
281 printf (" (no ~ expansion)");
285 /* First, get a stream to read. */
287 /* Expand ~ in FILENAME unless it came from `include',
288 in which case it was already done. */
289 if (!(flags
& RM_NO_TILDE
) && filename
[0] == '~')
291 char *expanded
= tilde_expand (filename
);
296 infile
= fopen (filename
, "r");
297 /* Save the error code so we print the right message later. */
298 makefile_errno
= errno
;
300 /* If the makefile wasn't found and it's either a makefile from
301 the `MAKEFILES' variable or an included makefile,
302 search the included makefile search path for this makefile. */
304 if (infile
== 0 && (flags
& RM_INCLUDED
) && *filename
!= '/')
306 register unsigned int i
;
307 for (i
= 0; include_directories
[i
] != 0; ++i
)
309 char *name
= concat (include_directories
[i
], "/", filename
);
310 infile
= fopen (name
, "r");
321 /* Add FILENAME to the chain of read makefiles. */
322 deps
= (struct dep
*) xmalloc (sizeof (struct dep
));
323 deps
->next
= read_makefiles
;
324 read_makefiles
= deps
;
326 deps
->file
= lookup_file (filename
);
329 deps
->file
= enter_file (savestring (filename
, strlen (filename
)));
330 if (flags
& RM_DONTCARE
)
331 deps
->file
->dontcare
= 1;
333 if (filename
!= passed_filename
)
335 filename
= deps
->file
->name
;
336 deps
->changed
= flags
;
339 /* If the makefile can't be found at all, give up entirely. */
343 /* If we did some searching, errno has the error from the last
344 attempt, rather from FILENAME itself. Restore it in case the
345 caller wants to use it in a message. */
346 errno
= makefile_errno
;
350 reading_filename
= filename
;
351 reading_lineno_ptr
= &lineno
;
353 /* Loop over lines in the file.
354 The strategy is to accumulate target names in FILENAMES, dependencies
355 in DEPS and commands in COMMANDS. These are used to define a rule
356 when the start of the next rule (or eof) is encountered. */
360 while (!feof (infile
))
363 nlines
= readline (&lb
, infile
, filename
, lineno
);
365 /* Check for a shell command line first.
366 If it is not one, we can stop treating tab specially. */
367 if (lb
.buffer
[0] == '\t')
369 /* This line is a probably shell command. */
373 /* Ignore the commands in a rule with no targets. */
376 /* If there is no preceding rule line, don't treat this line
377 as a command, even though it begins with a tab character.
378 SunOS 4 make appears to behave this way. */
383 /* Yep, this is a shell command, and we don't care. */
386 /* Append this command line to the line being accumulated. */
388 if (commands_idx
== 0)
389 commands_started
= lineno
;
391 if (len
+ 1 + commands_idx
> commands_len
)
393 commands_len
= (len
+ 1 + commands_idx
) * 2;
394 commands
= (char *) xrealloc (commands
, commands_len
);
396 bcopy (p
, &commands
[commands_idx
], len
);
398 commands
[commands_idx
++] = '\n';
404 /* This line is not a shell command line. Don't worry about tabs. */
406 if (collapsed_length
< lb
.size
)
408 collapsed_length
= lb
.size
;
411 collapsed
= (char *) xmalloc (collapsed_length
);
413 strcpy (collapsed
, lb
.buffer
);
414 /* Collapse continuation lines. */
415 collapse_continuations (collapsed
);
416 remove_comments (collapsed
);
418 /* strncmp is first to avoid dereferencing out into space. */
419 #define word1eq(s, l) (!strncmp (s, p, l) \
420 && (p[l] == '\0' || isblank (p[l])))
425 /* This line is completely empty. */
428 /* We must first check for conditional and `define' directives before
429 ignoring anything, since they control what we will do with
432 if (!in_ignored_define
433 && (word1eq ("ifdef", 5) || word1eq ("ifndef", 6)
434 || word1eq ("ifeq", 4) || word1eq ("ifneq", 5)
435 || word1eq ("else", 4) || word1eq ("endif", 5)))
437 int i
= conditional_line (p
, filename
, lineno
);
441 makefile_fatal (filename
, lineno
,
442 "invalid syntax in conditional");
445 else if (word1eq ("endef", 5))
447 if (in_ignored_define
)
448 in_ignored_define
= 0;
450 makefile_fatal (filename
, lineno
, "extraneous `endef'");
453 else if (word1eq ("define", 6))
456 in_ignored_define
= 1;
459 p2
= next_token (p
+ 6);
460 /* Let the variable name be the whole rest of the line,
461 with trailing blanks stripped (comments have already been
462 removed), so it could be a complex variable/function
463 reference that might contain blanks. */
464 p
= index (p2
, '\0');
465 while (isblank (p
[-1]))
467 lineno
= do_define (p2
, p
- p2
, o_file
,
468 lineno
, infile
, filename
);
472 else if (word1eq ("override", 8))
474 p2
= next_token (p
+ 8);
476 makefile_error (filename
, lineno
, "empty `override' directive");
477 if (!strncmp (p2
, "define", 6) && (isblank (p2
[6]) || p2
[6] == '\0'))
480 in_ignored_define
= 1;
483 p2
= next_token (p2
+ 6);
484 /* Let the variable name be the whole rest of the line,
485 with trailing blanks stripped (comments have already been
486 removed), so it could be a complex variable/function
487 reference that might contain blanks. */
488 p
= index (p2
, '\0');
489 while (isblank (p
[-1]))
491 lineno
= do_define (p2
, p
- p2
, o_override
,
492 lineno
, infile
, filename
);
496 && !try_variable_definition (filename
, lineno
,
498 makefile_error (filename
, lineno
, "empty `override' directive");
504 /* Ignore the line. We continue here so conditionals
505 can appear in the middle of a rule. */
507 else if (word1eq ("export", 6))
510 p2
= next_token (p
+ 6);
512 export_all_variables
= 1;
513 v
= try_variable_definition (filename
, lineno
, p2
, o_file
);
515 v
->export
= v_export
;
519 for (p
= find_next_token (&p2
, &len
); p
!= 0;
520 p
= find_next_token (&p2
, &len
))
522 v
= lookup_variable (p
, len
);
524 v
= define_variable (p
, len
, "", o_file
, 0);
525 v
->export
= v_export
;
529 else if (word1eq ("unexport", 8))
533 p2
= next_token (p
+ 8);
535 export_all_variables
= 0;
536 for (p
= find_next_token (&p2
, &len
); p
!= 0;
537 p
= find_next_token (&p2
, &len
))
539 v
= lookup_variable (p
, len
);
541 v
= define_variable (p
, len
, "", o_file
, 0);
542 v
->export
= v_noexport
;
545 else if (word1eq ("include", 7) || word1eq ("-include", 8))
547 /* We have found an `include' line specifying a nested
548 makefile to be read at this point. */
549 struct conditionals
*save
, new_conditionals
;
550 struct nameseq
*files
;
551 /* "-include" (vs "include") says no
552 error if the file does not exist. */
553 int noerror
= p
[0] == '-';
555 p
= allocated_variable_expand (next_token (p
+ (noerror
? 9 : 8)));
558 makefile_error (filename
, lineno
,
559 "no file name for `%sinclude'",
564 /* Parse the list of file names. */
566 files
= multi_glob (parse_file_seq (&p2
, '\0',
567 sizeof (struct nameseq
),
569 sizeof (struct nameseq
));
572 /* Save the state of conditionals and start
573 the included makefile with a clean slate. */
575 bzero ((char *) &new_conditionals
, sizeof new_conditionals
);
576 conditionals
= &new_conditionals
;
578 /* Record the rules that are waiting so they will determine
579 the default goal before those in the included makefile. */
580 record_waiting_files ();
582 /* Read each included makefile. */
585 struct nameseq
*next
= files
->next
;
586 char *name
= files
->name
;
590 if (! read_makefile (name
, (RM_INCLUDED
| RM_NO_TILDE
591 | (noerror
? RM_DONTCARE
: 0)))
593 makefile_error (filename
, lineno
,
594 "%s: %s", name
, strerror (errno
));
597 /* Free any space allocated by conditional_line. */
598 if (conditionals
->ignoring
)
599 free (conditionals
->ignoring
);
600 if (conditionals
->seen_else
)
601 free (conditionals
->seen_else
);
605 reading_filename
= filename
;
606 reading_lineno_ptr
= &lineno
;
608 else if (word1eq ("vpath", 5))
612 p2
= variable_expand (p
+ 5);
613 p
= find_next_token (&p2
, &len
);
616 pattern
= savestring (p
, len
);
617 p
= find_next_token (&p2
, &len
);
618 /* No searchpath means remove all previous
619 selective VPATH's with the same pattern. */
622 /* No pattern means remove all previous selective VPATH's. */
624 construct_vpath_list (pattern
, p
);
629 else if (try_variable_definition (filename
, lineno
, p
, o_file
))
630 /* This line has been dealt with. */
632 else if (lb
.buffer
[0] == '\t')
638 /* The line is completely blank; that is harmless. */
640 /* This line starts with a tab but was not caught above
641 because there was no preceding target, and the line
642 might have been usable as a variable definition.
643 But now it is definitely lossage. */
644 makefile_fatal (filename
, lineno
,
645 "commands commence before first target");
649 /* This line describes some target files. */
653 /* Record the previous rule. */
655 record_waiting_files ();
657 /* Look for a semicolon in the unexpanded line. */
658 cmdleft
= find_semicolon (lb
.buffer
);
660 /* Found one. Cut the line short there before expanding it. */
663 collapse_continuations (lb
.buffer
);
665 /* Expand variable and function references before doing anything
666 else so that special characters can be inside variables. */
667 p
= variable_expand (lb
.buffer
);
670 /* Look for a semicolon in the expanded line. */
671 cmdleft
= find_semicolon (p
);
674 /* Cut the line short at the semicolon. */
677 /* Remove comments from the line. */
684 makefile_fatal (filename
, lineno
,
685 "missing rule before commands");
687 /* This line contained a variable reference that
688 expanded to nothing but whitespace. */
693 /* We accept and ignore rules without targets for
694 compatibility with SunOS 4 make. */
699 filenames
= multi_glob (parse_file_seq (&p2
, ':',
700 sizeof (struct nameseq
),
702 sizeof (struct nameseq
));
704 makefile_fatal (filename
, lineno
, "missing separator");
705 /* Is this a one-colon or two-colon entry? */
706 two_colon
= *p2
== ':';
710 /* We have some targets, so don't ignore the following commands. */
713 /* Is this a static pattern rule: `target: %targ: %dep; ...'? */
715 while (p
!= 0 && p
[-1] == '\\')
717 register char *q
= &p
[-1];
718 register int backslash
= 0;
720 backslash
= !backslash
;
722 p
= index (p
+ 1, ':');
728 struct nameseq
*target
;
729 target
= parse_file_seq (&p2
, ':', sizeof (struct nameseq
), 1);
732 makefile_fatal (filename
, lineno
, "missing target pattern");
733 else if (target
->next
!= 0)
734 makefile_fatal (filename
, lineno
, "multiple target patterns");
735 pattern
= target
->name
;
736 pattern_percent
= find_percent (pattern
);
737 if (pattern_percent
== 0)
738 makefile_fatal (filename
, lineno
,
739 "target pattern contains no `%%'");
744 /* Parse the dependencies. */
745 deps
= (struct dep
*)
746 multi_glob (parse_file_seq (&p2
, '\0', sizeof (struct dep
), 1),
747 sizeof (struct dep
));
752 /* Semicolon means rest of line is a command. */
753 unsigned int len
= strlen (cmdleft
+ 1);
755 commands_started
= lineno
;
757 /* Add this command line to the buffer. */
758 if (len
+ 2 > commands_len
)
760 commands_len
= (len
+ 2) * 2;
761 commands
= (char *) xrealloc (commands
, commands_len
);
763 bcopy (cmdleft
+ 1, commands
, len
);
765 commands
[commands_idx
++] = '\n';
771 /* We get here except in the case that we just read a rule line.
772 Record now the last rule we read, so following spurious
773 commands are properly diagnosed. */
774 record_waiting_files ();
778 if (conditionals
->if_cmds
)
779 makefile_fatal (filename
, lineno
, "missing `endif'");
781 /* At eof, record the last rule. */
782 record_waiting_files ();
785 free ((char *) commands
);
788 reading_filename
= 0;
789 reading_lineno_ptr
= 0;
794 /* Execute a `define' directive.
795 The first line has already been read, and NAME is the name of
796 the variable to be defined. The following lines remain to be read.
797 LINENO, INFILE and FILENAME refer to the makefile being read.
798 The value returned is LINENO, updated for lines read here. */
801 do_define (name
, namelen
, origin
, lineno
, infile
, filename
)
803 unsigned int namelen
;
804 enum variable_origin origin
;
809 struct linebuffer lb
;
810 unsigned int nlines
= 0;
811 unsigned int length
= 100;
812 char *definition
= (char *) xmalloc (100);
813 register unsigned int idx
= 0;
816 /* Expand the variable name. */
817 char *var
= (char *) alloca (namelen
+ 1);
818 bcopy (name
, var
, namelen
);
820 var
= variable_expand (var
);
823 while (!feof (infile
))
826 nlines
= readline (&lb
, infile
, filename
, lineno
);
828 collapse_continuations (lb
.buffer
);
830 p
= next_token (lb
.buffer
);
831 if ((p
[5] == '\0' || isblank (p
[5])) && !strncmp (p
, "endef", 5))
835 if (*next_token (p
) != '\0')
836 makefile_error (filename
, lineno
,
837 "Extraneous text after `endef' directive");
838 /* Define the variable. */
840 definition
[0] = '\0';
842 definition
[idx
- 1] = '\0';
843 (void) define_variable (var
, strlen (var
), definition
, origin
, 1);
850 unsigned int len
= strlen (lb
.buffer
);
852 /* Increase the buffer size if necessary. */
853 if (idx
+ len
+ 1 > length
)
855 length
= (idx
+ len
) * 2;
856 definition
= (char *) xrealloc (definition
, length
+ 1);
859 bcopy (lb
.buffer
, &definition
[idx
], len
);
861 /* Separate lines with a newline. */
862 definition
[idx
++] = '\n';
867 makefile_fatal (filename
, lineno
, "missing `endef', unterminated `define'");
873 /* Interpret conditional commands "ifdef", "ifndef", "ifeq",
874 "ifneq", "else" and "endif".
875 LINE is the input line, with the command as its first word.
877 FILENAME and LINENO are the filename and line number in the
878 current makefile. They are used for error messages.
880 Value is -1 if the line is invalid,
881 0 if following text should be interpreted,
882 1 if following text should be ignored. */
885 conditional_line (line
, filename
, lineno
)
892 register unsigned int i
;
896 /* It's an "if..." command. */
897 notdef
= line
[2] == 'n';
900 cmdname
= line
[3] == 'd' ? "ifndef" : "ifneq";
901 line
+= cmdname
[3] == 'd' ? 7 : 6;
905 cmdname
= line
[2] == 'd' ? "ifdef" : "ifeq";
906 line
+= cmdname
[2] == 'd' ? 6 : 5;
911 /* It's an "else" or "endif" command. */
912 notdef
= line
[1] == 'n';
913 cmdname
= notdef
? "endif" : "else";
914 line
+= notdef
? 5 : 4;
917 line
= next_token (line
);
922 makefile_error (filename
, lineno
,
923 "Extraneous text after `%s' directive",
925 /* "Else" or "endif". */
926 if (conditionals
->if_cmds
== 0)
927 makefile_fatal (filename
, lineno
, "extraneous `%s'", cmdname
);
928 /* NOTDEF indicates an `endif' command. */
930 --conditionals
->if_cmds
;
931 else if (conditionals
->seen_else
[conditionals
->if_cmds
- 1])
932 makefile_fatal (filename
, lineno
, "only one `else' per conditional");
935 /* Toggle the state of ignorance. */
936 conditionals
->ignoring
[conditionals
->if_cmds
- 1]
937 = !conditionals
->ignoring
[conditionals
->if_cmds
- 1];
938 /* Record that we have seen an `else' in this conditional.
939 A second `else' will be erroneous. */
940 conditionals
->seen_else
[conditionals
->if_cmds
- 1] = 1;
942 for (i
= 0; i
< conditionals
->if_cmds
; ++i
)
943 if (conditionals
->ignoring
[i
])
948 if (conditionals
->allocated
== 0)
950 conditionals
->allocated
= 5;
951 conditionals
->ignoring
= (char *) xmalloc (conditionals
->allocated
);
952 conditionals
->seen_else
= (char *) xmalloc (conditionals
->allocated
);
955 ++conditionals
->if_cmds
;
956 if (conditionals
->if_cmds
> conditionals
->allocated
)
958 conditionals
->allocated
+= 5;
959 conditionals
->ignoring
= (char *)
960 xrealloc (conditionals
->ignoring
, conditionals
->allocated
);
961 conditionals
->seen_else
= (char *)
962 xrealloc (conditionals
->seen_else
, conditionals
->allocated
);
965 /* Record that we have seen an `if...' but no `else' so far. */
966 conditionals
->seen_else
[conditionals
->if_cmds
- 1] = 0;
968 /* Search through the stack to see if we're already ignoring. */
969 for (i
= 0; i
< conditionals
->if_cmds
- 1; ++i
)
970 if (conditionals
->ignoring
[i
])
972 /* We are already ignoring, so just push a level
973 to match the next "else" or "endif", and keep ignoring.
974 We don't want to expand variables in the condition. */
975 conditionals
->ignoring
[conditionals
->if_cmds
- 1] = 1;
979 if (cmdname
[notdef
? 3 : 2] == 'd')
981 /* "Ifdef" or "ifndef". */
983 register char *p
= end_of_token (line
);
988 v
= lookup_variable (line
, i
);
989 conditionals
->ignoring
[conditionals
->if_cmds
- 1]
990 = (v
!= 0 && *v
->value
!= '\0') == notdef
;
994 /* "Ifeq" or "ifneq". */
997 char termin
= *line
== '(' ? ',' : *line
;
999 if (termin
!= ',' && termin
!= '"' && termin
!= '\'')
1003 /* Find the end of the first string. */
1006 register int count
= 0;
1007 for (; *line
!= '\0'; ++line
)
1010 else if (*line
== ')')
1012 else if (*line
== ',' && count
<= 0)
1016 while (*line
!= '\0' && *line
!= termin
)
1024 s2
= variable_expand (s1
);
1025 /* We must allocate a new copy of the expanded string because
1026 variable_expand re-uses the same buffer. */
1028 s1
= (char *) alloca (len
+ 1);
1029 bcopy (s2
, s1
, len
+ 1);
1032 /* Find the start of the second string. */
1033 line
= next_token (line
);
1035 termin
= termin
== ',' ? ')' : *line
;
1036 if (termin
!= ')' && termin
!= '"' && termin
!= '\'')
1039 /* Find the end of the second string. */
1042 register int count
= 0;
1043 s2
= next_token (line
);
1044 for (line
= s2
; *line
!= '\0'; ++line
)
1048 else if (*line
== ')')
1059 while (*line
!= '\0' && *line
!= termin
)
1067 line
= next_token (++line
);
1069 makefile_error (filename
, lineno
,
1070 "Extraneous text after `%s' directive",
1073 s2
= variable_expand (s2
);
1074 conditionals
->ignoring
[conditionals
->if_cmds
- 1]
1075 = streq (s1
, s2
) == notdef
;
1078 /* Search through the stack to see if we're ignoring. */
1079 for (i
= 0; i
< conditionals
->if_cmds
; ++i
)
1080 if (conditionals
->ignoring
[i
])
1085 /* Remove duplicate dependencies in CHAIN. */
1088 uniquize_deps (chain
)
1091 register struct dep
*d
;
1093 /* Make sure that no dependencies are repeated. This does not
1094 really matter for the purpose of updating targets, but it
1095 might make some names be listed twice for $^ and $?. */
1097 for (d
= chain
; d
!= 0; d
= d
->next
)
1099 struct dep
*last
, *next
;
1104 if (streq (dep_name (d
), dep_name (next
)))
1106 struct dep
*n
= next
->next
;
1108 if (next
->name
!= 0 && next
->name
!= d
->name
)
1111 free ((char *) next
);
1122 /* Record a description line for files FILENAMES,
1123 with dependencies DEPS, commands to execute described
1124 by COMMANDS and COMMANDS_IDX, coming from FILENAME:COMMANDS_STARTED.
1125 TWO_COLON is nonzero if a double colon was used.
1126 If not nil, PATTERN is the `%' pattern to make this
1127 a static pattern rule, and PATTERN_PERCENT is a pointer
1128 to the `%' within it.
1130 The links of FILENAMES are freed, and so are any names in it
1131 that are not incorporated into other data structures. */
1134 record_files (filenames
, pattern
, pattern_percent
, deps
, commands_started
,
1135 commands
, commands_idx
, two_colon
, filename
, lineno
, set_default
)
1136 struct nameseq
*filenames
;
1137 char *pattern
, *pattern_percent
;
1139 unsigned int commands_started
;
1141 unsigned int commands_idx
;
1144 unsigned int lineno
;
1147 struct nameseq
*nextf
;
1149 unsigned int max_targets
, target_idx
;
1150 char **targets
= 0, **target_percents
= 0;
1151 struct commands
*cmds
;
1153 if (commands_idx
> 0)
1155 cmds
= (struct commands
*) xmalloc (sizeof (struct commands
));
1156 cmds
->filename
= filename
;
1157 cmds
->lineno
= commands_started
;
1158 cmds
->commands
= savestring (commands
, commands_idx
);
1159 cmds
->command_lines
= 0;
1164 for (; filenames
!= 0; filenames
= nextf
)
1166 register char *name
= filenames
->name
;
1167 register struct file
*f
;
1168 register struct dep
*d
;
1170 char *implicit_percent
;
1172 nextf
= filenames
->next
;
1173 free ((char *) filenames
);
1175 implicit_percent
= find_percent (name
);
1176 implicit
|= implicit_percent
!= 0;
1178 if (implicit
&& pattern
!= 0)
1179 makefile_fatal (filename
, lineno
,
1180 "mixed implicit and static pattern rules");
1182 if (implicit
&& implicit_percent
== 0)
1183 makefile_fatal (filename
, lineno
, "mixed implicit and normal rules");
1190 targets
= (char **) xmalloc (5 * sizeof (char *));
1191 target_percents
= (char **) xmalloc (5 * sizeof (char *));
1194 else if (target_idx
== max_targets
- 1)
1197 targets
= (char **) xrealloc ((char *) targets
,
1198 max_targets
* sizeof (char *));
1200 = (char **) xrealloc ((char *) target_percents
,
1201 max_targets
* sizeof (char *));
1203 targets
[target_idx
] = name
;
1204 target_percents
[target_idx
] = implicit_percent
;
1209 /* If there are multiple filenames, copy the chain DEPS
1210 for all but the last one. It is not safe for the same deps
1211 to go in more than one place in the data base. */
1212 this = nextf
!= 0 ? copy_dep_chain (deps
) : deps
;
1215 /* If this is an extended static rule:
1216 `targets: target%pattern: dep%pattern; cmds',
1217 translate each dependency pattern into a plain filename
1218 using the target pattern and this target's name. */
1219 if (!pattern_matches (pattern
, pattern_percent
, name
))
1221 /* Give a warning if the rule is meaningless. */
1222 makefile_error (filename
, lineno
,
1223 "target `%s' doesn't match the target pattern",
1229 /* We use patsubst_expand to do the work of translating
1230 the target pattern, the target's name and the dependencies'
1231 patterns into plain dependency names. */
1232 char *buffer
= variable_expand ("");
1234 for (d
= this; d
!= 0; d
= d
->next
)
1237 char *percent
= find_percent (d
->name
);
1240 o
= patsubst_expand (buffer
, name
, pattern
, d
->name
,
1241 pattern_percent
, percent
);
1243 d
->name
= savestring (buffer
, o
- buffer
);
1249 /* Single-colon. Combine these dependencies
1250 with others in file's existing record, if any. */
1251 f
= enter_file (name
);
1253 if (f
->double_colon
)
1254 makefile_fatal (filename
, lineno
,
1255 "target file `%s' has both : and :: entries",
1258 /* If CMDS == F->CMDS, this target was listed in this rule
1259 more than once. Just give a warning since this is harmless. */
1260 if (cmds
!= 0 && cmds
== f
->cmds
)
1263 "target `%s' given more than once in the same rule.",
1266 /* Check for two single-colon entries both with commands.
1267 Check is_target so that we don't lose on files such as .c.o
1268 whose commands were preinitialized. */
1269 else if (cmds
!= 0 && f
->cmds
!= 0 && f
->is_target
)
1271 makefile_error (cmds
->filename
, cmds
->lineno
,
1272 "warning: overriding commands for target `%s'",
1274 makefile_error (f
->cmds
->filename
, f
->cmds
->lineno
,
1275 "warning: ignoring old commands for target `%s'",
1281 /* Defining .DEFAULT with no deps or cmds clears it. */
1282 if (f
== default_file
&& this == 0 && cmds
== 0)
1286 /* Defining .SUFFIXES with no dependencies
1287 clears out the list of suffixes. */
1288 if (f
== suffix_file
&& this == 0)
1293 struct dep
*nextd
= d
->next
;
1300 else if (f
->deps
!= 0)
1302 /* Add the file's old deps and the new ones in THIS together. */
1304 struct dep
*firstdeps
, *moredeps
;
1307 /* This is the rule with commands, so put its deps first.
1308 The rationale behind this is that $< expands to the
1309 first dep in the chain, and commands use $< expecting
1310 to get the dep that rule specifies. */
1316 /* Append the new deps to the old ones. */
1317 firstdeps
= f
->deps
;
1322 firstdeps
= moredeps
;
1326 while (d
->next
!= 0)
1331 f
->deps
= firstdeps
;
1336 /* If this is a static pattern rule, set the file's stem to
1337 the part of its name that matched the `%' in the pattern,
1338 so you can use $* in the commands. */
1341 static char *percent
= "%";
1342 char *buffer
= variable_expand ("");
1343 char *o
= patsubst_expand (buffer
, name
, pattern
, percent
,
1344 pattern_percent
, percent
);
1345 f
->stem
= savestring (buffer
, o
- buffer
);
1350 /* Double-colon. Make a new record
1351 even if the file already has one. */
1352 f
= lookup_file (name
);
1353 /* Check for both : and :: rules. Check is_target so
1354 we don't lose on default suffix rules or makefiles. */
1355 if (f
!= 0 && f
->is_target
&& !f
->double_colon
)
1356 makefile_fatal (filename
, lineno
,
1357 "target file `%s' has both : and :: entries",
1359 f
= enter_file (name
);
1360 /* If there was an existing entry and it was a double-colon
1361 entry, enter_file will have returned a new one, making it the
1362 prev pointer of the old one, and setting its double_colon
1363 pointer to the first one. */
1364 if (f
->double_colon
== 0)
1365 /* This is the first entry for this name, so we must
1366 set its double_colon pointer to itself. */
1367 f
->double_colon
= f
;
1373 /* Free name if not needed further. */
1374 if (f
!= 0 && name
!= f
->name
1375 && (name
< f
->name
|| name
> f
->name
+ strlen (f
->name
)))
1381 /* See if this is first target seen whose name does
1382 not start with a `.', unless it contains a slash. */
1383 if (default_goal_file
== 0 && set_default
1384 && (*name
!= '.' || index (name
, '/') != 0))
1388 /* If this file is a suffix, don't
1389 let it be the default goal file. */
1391 for (d
= suffix_file
->deps
; d
!= 0; d
= d
->next
)
1393 register struct dep
*d2
;
1394 if (*dep_name (d
) != '.' && streq (name
, dep_name (d
)))
1399 for (d2
= suffix_file
->deps
; d2
!= 0; d2
= d2
->next
)
1401 register unsigned int len
= strlen (dep_name (d2
));
1402 if (strncmp (name
, dep_name (d2
), len
))
1404 if (streq (name
+ len
, dep_name (d
)))
1415 default_goal_file
= f
;
1421 targets
[target_idx
] = 0;
1422 target_percents
[target_idx
] = 0;
1423 create_pattern_rule (targets
, target_percents
, two_colon
, deps
, cmds
, 1);
1424 free ((char *) target_percents
);
1428 /* Search STRING for an unquoted STOPCHAR or blank (if BLANK is nonzero).
1429 Backslashes quote STOPCHAR, blanks if BLANK is nonzero, and backslash.
1430 Quoting backslashes are removed from STRING by compacting it into
1431 itself. Returns a pointer to the first unquoted STOPCHAR if there is
1432 one, or nil if there are none. */
1435 find_char_unquote (string
, stopchar
, blank
)
1440 unsigned int string_len
= strlen (string
);
1441 register char *p
= string
;
1447 while (*p
!= '\0' && *p
!= stopchar
&& !isblank (*p
))
1454 p
= index (p
, stopchar
);
1458 if (p
> string
&& p
[-1] == '\\')
1460 /* Search for more backslashes. */
1461 register int i
= -2;
1462 while (&p
[i
] >= string
&& p
[i
] == '\\')
1465 /* The number of backslashes is now -I.
1466 Copy P over itself to swallow half of them. */
1467 bcopy (&p
[i
/ 2], &p
[i
], (string_len
- (p
- string
)) - (i
/ 2) + 1);
1470 /* All the backslashes quoted each other; the STOPCHAR was
1474 /* The STOPCHAR was quoted by a backslash. Look for another. */
1477 /* No backslash in sight. */
1481 /* Never hit a STOPCHAR or blank (with BLANK nonzero). */
1485 /* Search PATTERN for an unquoted %. */
1488 find_percent (pattern
)
1491 return find_char_unquote (pattern
, '%', 0);
1494 /* Search STRING for an unquoted ; that is not after an unquoted #. */
1497 find_semicolon (string
)
1500 return (find_char_unquote (string
, '#', 0) == 0
1501 ? find_char_unquote (string
, ';', 0) : 0);
1504 /* Parse a string into a sequence of filenames represented as a
1505 chain of struct nameseq's in reverse order and return that chain.
1507 The string is passed as STRINGP, the address of a string pointer.
1508 The string pointer is updated to point at the first character
1509 not parsed, which either is a null char or equals STOPCHAR.
1511 SIZE is how big to construct chain elements.
1512 This is useful if we want them actually to be other structures
1513 that have room for additional info.
1515 If STRIP is nonzero, strip `./'s off the beginning. */
1518 parse_file_seq (stringp
, stopchar
, size
, strip
)
1524 register struct nameseq
*new = 0;
1525 register struct nameseq
*new1
, *lastnew1
;
1526 register char *p
= *stringp
;
1532 /* Skip whitespace; see if any more names are left. */
1538 /* Yes, find end of next name. */
1540 p
= find_char_unquote (q
, stopchar
, 1);
1545 /* Skip leading `./'s. */
1546 while (p
- q
> 2 && q
[0] == '.' && q
[1] == '/')
1548 q
+= 2; /* Skip "./". */
1549 while (q
< p
&& *q
== '/')
1550 /* Skip following slashes: ".//foo" is "foo", not "/foo". */
1554 /* Extract the filename just found, and skip it. */
1557 /* ".///" was stripped to "". */
1558 name
= savestring ("./", 2);
1560 name
= savestring (q
, p
- q
);
1562 /* Add it to the front of the chain. */
1563 new1
= (struct nameseq
*) xmalloc (size
);
1571 /* Look for multi-word archive references.
1572 They are indicated by a elt ending with an unmatched `)' and
1573 an elt further down the chain (i.e., previous in the file list)
1574 with an unmatched `(' (e.g., "lib(mem"). */
1576 for (new1
= new, lastnew1
= 0; new1
!= 0; lastnew1
= new1
, new1
= new1
->next
)
1577 if (new1
->name
[0] != '(' /* Don't catch "(%)" and suchlike. */
1578 && new1
->name
[strlen (new1
->name
) - 1] == ')'
1579 && index (new1
->name
, '(') == 0)
1581 /* NEW1 ends with a `)' but does not contain a `('.
1582 Look back for an elt with an opening `(' but no closing `)'. */
1584 struct nameseq
*n
= new1
->next
, *lastn
= new1
;
1586 while (n
!= 0 && (paren
= index (n
->name
, '(')) == 0)
1592 /* Ignore something starting with `(', as that cannot actually
1593 be an archive-member reference (and treating it as such
1594 results in an empty file name, which causes much lossage). */
1595 && n
->name
[0] != '(')
1597 /* N is the first element in the archive group.
1598 Its name looks like "lib(mem" (with no closing `)'). */
1602 /* Copy "lib(" into LIBNAME. */
1604 libname
= (char *) alloca (paren
- n
->name
+ 1);
1605 bcopy (n
->name
, libname
, paren
- n
->name
);
1606 libname
[paren
- n
->name
] = '\0';
1610 /* N was just "lib(", part of something like "lib( a b)".
1611 Edit it out of the chain and free its storage. */
1612 lastn
->next
= n
->next
;
1615 /* LASTN->next is the new stopping elt for the loop below. */
1620 /* Replace N's name with the full archive reference. */
1621 name
= concat (libname
, paren
, ")");
1626 if (new1
->name
[1] == '\0')
1628 /* NEW1 is just ")", part of something like "lib(a b )".
1629 Omit it from the chain and free its storage. */
1633 lastnew1
->next
= new1
->next
;
1637 free ((char *) lastn
);
1641 /* Replace also NEW1->name, which already has closing `)'. */
1642 name
= concat (libname
, new1
->name
, "");
1648 /* Trace back from NEW1 (the end of the list) until N
1649 (the beginning of the list), rewriting each name
1650 with the full archive reference. */
1654 name
= concat (libname
, new1
->name
, ")");
1661 /* We might have slurped up the whole list,
1662 and continuing the loop would dereference NEW1. */
1673 /* Read a line of text from STREAM into LINEBUFFER.
1674 Combine continuation lines into one line.
1675 Return the number of actual lines read (> 1 if hacked continuation lines).
1679 readline (linebuffer
, stream
, filename
, lineno
)
1680 struct linebuffer
*linebuffer
;
1683 unsigned int lineno
;
1685 char *buffer
= linebuffer
->buffer
;
1686 register char *p
= linebuffer
->buffer
;
1687 register char *end
= p
+ linebuffer
->size
;
1688 register int len
, lastlen
= 0;
1690 register unsigned int nlines
= 0;
1691 register int backslash
;
1695 while (fgets (p
, end
- p
, stream
) != 0)
1700 /* This only happens when the first thing on the line is a '\0'.
1701 It is a pretty hopeless case, but (wonder of wonders) Athena
1702 lossage strikes again! (xmkmf puts NULs in its makefiles.)
1703 There is nothing really to be done; we synthesize a newline so
1704 the following line doesn't appear to be part of this line. */
1705 makefile_error (filename
, lineno
,
1706 "warning: NUL character seen; rest of line ignored");
1714 /* Probably ran out of buffer space. */
1715 register unsigned int p_off
= p
- buffer
;
1716 linebuffer
->size
*= 2;
1717 buffer
= (char *) xrealloc (buffer
, linebuffer
->size
);
1719 end
= buffer
+ linebuffer
->size
;
1720 linebuffer
->buffer
= buffer
;
1728 if (len
== 1 && p
> buffer
)
1729 /* P is pointing at a newline and it's the beginning of
1730 the buffer returned by the last fgets call. However,
1731 it is not necessarily the beginning of a line if P is
1732 pointing past the beginning of the holding buffer.
1733 If the buffer was just enlarged (right before the newline),
1734 we must account for that, so we pretend that the two lines
1739 for (p2
= p
- 2; --len
> 0; --p2
)
1742 backslash
= !backslash
;
1755 /* Enlarge the buffer. */
1756 register unsigned int p_off
= p
- buffer
;
1757 linebuffer
->size
*= 2;
1758 buffer
= (char *) xrealloc (buffer
, linebuffer
->size
);
1760 end
= buffer
+ linebuffer
->size
;
1761 linebuffer
->buffer
= buffer
;
1765 if (ferror (stream
))
1766 pfatal_with_name (filename
);
1771 /* Construct the list of include directories
1772 from the arguments and the default list. */
1775 construct_include_path (arg_dirs
)
1778 register unsigned int i
;
1781 /* Table to hold the dirs. */
1783 register unsigned int defsize
= (sizeof (default_include_directories
)
1784 / sizeof (default_include_directories
[0]));
1785 register unsigned int max
= 5;
1786 register char **dirs
= (char **) xmalloc ((5 + defsize
) * sizeof (char *));
1787 register unsigned int idx
= 0;
1789 /* First consider any dirs specified with -I switches.
1790 Ignore dirs that don't exist. */
1793 while (*arg_dirs
!= 0)
1795 char *dir
= *arg_dirs
++;
1799 char *expanded
= tilde_expand (dir
);
1804 if (safe_stat (dir
, &stbuf
) == 0 && S_ISDIR (stbuf
.st_mode
))
1810 xrealloc ((char *) dirs
, (max
+ defsize
) * sizeof (char *));
1814 else if (dir
!= arg_dirs
[-1])
1818 /* Now add at the end the standard default dirs. */
1820 for (i
= 0; default_include_directories
[i
] != 0; ++i
)
1821 if (safe_stat (default_include_directories
[i
], &stbuf
) == 0
1822 && S_ISDIR (stbuf
.st_mode
))
1823 dirs
[idx
++] = default_include_directories
[i
];
1827 /* Now compute the maximum length of any name in it. */
1830 for (i
= 0; i
< idx
; ++i
)
1832 unsigned int len
= strlen (dirs
[i
]);
1833 /* If dir name is written with a trailing slash, discard it. */
1834 if (dirs
[i
][len
- 1] == '/')
1835 /* We can't just clobber a null in because it may have come from
1836 a literal string and literal strings may not be writable. */
1837 dirs
[i
] = savestring (dirs
[i
], len
- 1);
1838 if (len
> max_incl_len
)
1842 include_directories
= dirs
;
1845 /* Expand ~ or ~USER at the beginning of NAME.
1846 Return a newly malloc'd string or 0. */
1852 if (name
[1] == '/' || name
[1] == '\0')
1854 extern char *getenv ();
1859 /* Turn off --warn-undefined-variables while we expand HOME. */
1860 int save
= warn_undefined_variables_flag
;
1861 warn_undefined_variables_flag
= 0;
1863 home_dir
= allocated_variable_expand ("$(HOME)");
1865 warn_undefined_variables_flag
= save
;
1868 is_variable
= home_dir
[0] != '\0';
1872 home_dir
= getenv ("HOME");
1874 if (home_dir
== 0 || home_dir
[0] == '\0')
1876 extern char *getlogin ();
1877 char *name
= getlogin ();
1881 struct passwd
*p
= getpwnam (name
);
1883 home_dir
= p
->pw_dir
;
1888 char *new = concat (home_dir
, "", name
+ 1);
1896 struct passwd
*pwent
;
1897 char *userend
= index (name
+ 1, '/');
1900 pwent
= getpwnam (name
+ 1);
1904 return savestring (pwent
->pw_dir
, strlen (pwent
->pw_dir
));
1906 return concat (pwent
->pw_dir
, "/", userend
+ 1);
1908 else if (userend
!= 0)
1915 /* Given a chain of struct nameseq's describing a sequence of filenames,
1916 in reverse of the intended order, return a new chain describing the
1917 result of globbing the filenames. The new chain is in forward order.
1918 The links of the old chain are freed or used in the new chain.
1919 Likewise for the names in the old chain.
1921 SIZE is how big to construct chain elements.
1922 This is useful if we want them actually to be other structures
1923 that have room for additional info. */
1926 multi_glob (chain
, size
)
1927 struct nameseq
*chain
;
1930 register struct nameseq
*new = 0;
1931 register struct nameseq
*old
;
1932 struct nameseq
*nexto
;
1934 for (old
= chain
; old
!= 0; old
= nexto
)
1943 if (old
->name
[0] == '~')
1945 char *newname
= tilde_expand (old
->name
);
1949 old
->name
= newname
;
1954 if (ar_name (old
->name
))
1956 /* OLD->name is an archive member reference.
1957 Replace it with the archive file name,
1958 and save the member name in MEMNAME.
1959 We will glob on the archive name and then
1960 reattach MEMNAME later. */
1962 ar_parse_name (old
->name
, &arname
, &memname
);
1970 switch (glob (old
->name
, GLOB_NOCHECK
, NULL
, &gl
))
1972 case 0: /* Success. */
1974 register int i
= gl
.gl_pathc
;
1980 /* Try to glob on MEMNAME within the archive. */
1981 struct nameseq
*found
1982 = ar_glob (gl
.gl_pathv
[i
], memname
, size
);
1985 /* No matches. Use MEMNAME as-is. */
1987 = (struct nameseq
*) xmalloc (size
);
1988 unsigned int alen
= strlen (gl
.gl_pathv
[i
]);
1989 unsigned int mlen
= strlen (memname
);
1990 elt
->name
= (char *) xmalloc (alen
+ 1 + mlen
+ 2);
1991 bcopy (gl
.gl_pathv
[i
], elt
->name
, alen
);
1992 elt
->name
[alen
] = '(';
1993 bcopy (memname
, &elt
->name
[alen
+ 1], mlen
);
1994 elt
->name
[alen
+ 1 + mlen
] = ')';
1995 elt
->name
[alen
+ 1 + mlen
+ 1] = '\0';
2001 /* Find the end of the FOUND chain. */
2002 struct nameseq
*f
= found
;
2003 while (f
->next
!= 0)
2006 /* Attach the chain being built to the end of the FOUND
2007 chain, and make FOUND the new NEW chain. */
2017 struct nameseq
*elt
= (struct nameseq
*) xmalloc (size
);
2018 elt
->name
= savestring (gl
.gl_pathv
[i
],
2019 strlen (gl
.gl_pathv
[i
]));
2031 fatal ("virtual memory exhausted");