1 /* Reading and parsing of makefiles for GNU Make.
2 Copyright (C) 1988, 89, 90, 91, 92, 93, 94, 1995 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 ();
42 /* A `struct linebuffer' is a structure which holds a line of text.
43 `readline' reads a line from a stream into a linebuffer
44 and works regardless of the length of the line. */
48 /* Note: This is the number of bytes malloc'ed for `buffer'
49 It does not indicate `buffer's real length.
50 Instead, a null char indicates end-of-string. */
55 #define initbuffer(lb) (lb)->buffer = (char *) xmalloc ((lb)->size = 200)
56 #define freebuffer(lb) free ((lb)->buffer)
59 /* A `struct conditionals' contains the information describing
60 all the active conditionals in a makefile.
62 The global variable `conditionals' contains the conditionals
63 information for the current makefile. It is initialized from
64 the static structure `toplevel_conditionals' and is later changed
65 to new structures for included makefiles. */
69 unsigned int if_cmds
; /* Depth of conditional nesting. */
70 unsigned int allocated
; /* Elts allocated in following arrays. */
71 char *ignoring
; /* Are we ignoring or interepreting? */
72 char *seen_else
; /* Have we already seen an `else'? */
75 static struct conditionals toplevel_conditionals
;
76 static struct conditionals
*conditionals
= &toplevel_conditionals
;
79 /* Default directories to search for include files in */
81 static char *default_include_directories
[] =
90 /* List of directories to search for include files in */
92 static char **include_directories
;
94 /* Maximum length of an element of the above. */
96 static unsigned int max_incl_len
;
98 /* The filename and pointer to line number of the
99 makefile currently being read in. */
101 char *reading_filename
;
102 unsigned int *reading_lineno_ptr
;
104 /* The chain of makefiles read by read_makefile. */
106 static struct dep
*read_makefiles
= 0;
108 /* Read in all the makefiles and return the chain of their names. */
111 read_all_makefiles (makefiles
)
114 unsigned int num_makefiles
= 0;
117 puts ("Reading makefiles...");
119 /* If there's a non-null variable MAKEFILES, its value is a list of
120 files to read first thing. But don't let it prevent reading the
121 default makefiles and don't let the default goal come from there. */
129 /* Turn off --warn-undefined-variables while we expand MAKEFILES. */
130 int save
= warn_undefined_variables_flag
;
131 warn_undefined_variables_flag
= 0;
133 value
= allocated_variable_expand ("$(MAKEFILES)");
135 warn_undefined_variables_flag
= save
;
138 /* Set NAME to the start of next token and LENGTH to its length.
139 MAKEFILES is updated for finding remaining tokens. */
141 while ((name
= find_next_token (&p
, &length
)) != 0)
145 (void) read_makefile (name
,
146 RM_NO_DEFAULT_GOAL
| RM_INCLUDED
| RM_DONTCARE
);
152 /* Read makefiles specified with -f switches. */
155 while (*makefiles
!= 0)
157 struct dep
*tail
= read_makefiles
;
158 register struct dep
*d
;
160 if (! read_makefile (*makefiles
, 0))
161 perror_with_name ("", *makefiles
);
163 /* Find the right element of read_makefiles. */
165 while (d
->next
!= tail
)
168 /* Use the storage read_makefile allocates. */
169 *makefiles
= dep_name (d
);
174 /* If there were no -f switches, try the default names. */
176 if (num_makefiles
== 0)
178 static char *default_makefiles
[] =
179 { "GNUmakefile", "makefile", "Makefile", 0 };
180 register char **p
= default_makefiles
;
181 while (*p
!= 0 && !file_exists_p (*p
))
186 if (! read_makefile (*p
, 0))
187 perror_with_name ("", *p
);
191 /* No default makefile was found. Add the default makefiles to the
192 `read_makefiles' chain so they will be updated if possible. */
193 struct dep
*tail
= read_makefiles
;
194 /* Add them to the tail, after any MAKEFILES variable makefiles. */
195 while (tail
!= 0 && tail
->next
!= 0)
197 for (p
= default_makefiles
; *p
!= 0; ++p
)
199 struct dep
*d
= (struct dep
*) xmalloc (sizeof (struct dep
));
201 d
->file
= enter_file (*p
);
202 d
->file
->dontcare
= 1;
203 /* Tell update_goal_chain to bail out as soon as this file is
204 made, and main not to die if we can't make this file. */
205 d
->changed
= RM_DONTCARE
;
217 return read_makefiles
;
220 /* Read file FILENAME as a makefile and add its contents to the data base.
222 FLAGS contains bits as above.
224 FILENAME is added to the `read_makefiles' chain.
226 Returns 1 if a file was found and read, 0 if not. */
229 read_makefile (filename
, flags
)
233 static char *collapsed
= 0;
234 static unsigned int collapsed_length
= 0;
235 register FILE *infile
;
236 struct linebuffer lb
;
237 unsigned int commands_len
= 200;
238 char *commands
= (char *) xmalloc (200);
239 unsigned int commands_idx
= 0;
240 unsigned int commands_started
;
243 int ignoring
= 0, in_ignored_define
= 0;
244 int no_targets
= 0; /* Set when reading a rule without targets. */
245 char *passed_filename
= filename
;
247 struct nameseq
*filenames
= 0;
249 unsigned int lineno
= 1;
250 unsigned int nlines
= 0;
252 char *pattern
= 0, *pattern_percent
;
256 #define record_waiting_files() \
259 if (filenames != 0) \
260 record_files (filenames, pattern, pattern_percent, deps, \
261 commands_started, commands, commands_idx, \
262 two_colon, filename, lineno, \
263 !(flags & RM_NO_DEFAULT_GOAL)); \
269 #ifdef lint /* Suppress `used before set' messages. */
275 printf ("Reading makefile `%s'", filename
);
276 if (flags
& RM_NO_DEFAULT_GOAL
)
277 printf (" (no default goal)");
278 if (flags
& RM_INCLUDED
)
279 printf (" (search path)");
280 if (flags
& RM_DONTCARE
)
281 printf (" (don't care)");
282 if (flags
& RM_NO_TILDE
)
283 printf (" (no ~ expansion)");
287 /* First, get a stream to read. */
289 /* Expand ~ in FILENAME unless it came from `include',
290 in which case it was already done. */
291 if (!(flags
& RM_NO_TILDE
) && filename
[0] == '~')
293 char *expanded
= tilde_expand (filename
);
298 infile
= fopen (filename
, "r");
299 /* Save the error code so we print the right message later. */
300 makefile_errno
= errno
;
302 /* If the makefile wasn't found and it's either a makefile from
303 the `MAKEFILES' variable or an included makefile,
304 search the included makefile search path for this makefile. */
306 if (infile
== 0 && (flags
& RM_INCLUDED
) && *filename
!= '/')
308 register unsigned int i
;
309 for (i
= 0; include_directories
[i
] != 0; ++i
)
311 char *name
= concat (include_directories
[i
], "/", filename
);
312 infile
= fopen (name
, "r");
323 /* Add FILENAME to the chain of read makefiles. */
324 deps
= (struct dep
*) xmalloc (sizeof (struct dep
));
325 deps
->next
= read_makefiles
;
326 read_makefiles
= deps
;
328 deps
->file
= lookup_file (filename
);
331 deps
->file
= enter_file (savestring (filename
, strlen (filename
)));
332 if (flags
& RM_DONTCARE
)
333 deps
->file
->dontcare
= 1;
335 if (filename
!= passed_filename
)
337 filename
= deps
->file
->name
;
338 deps
->changed
= flags
;
341 /* If the makefile can't be found at all, give up entirely. */
345 /* If we did some searching, errno has the error from the last
346 attempt, rather from FILENAME itself. Restore it in case the
347 caller wants to use it in a message. */
348 errno
= makefile_errno
;
352 reading_filename
= filename
;
353 reading_lineno_ptr
= &lineno
;
355 /* Loop over lines in the file.
356 The strategy is to accumulate target names in FILENAMES, dependencies
357 in DEPS and commands in COMMANDS. These are used to define a rule
358 when the start of the next rule (or eof) is encountered. */
362 while (!feof (infile
))
365 nlines
= readline (&lb
, infile
, filename
, lineno
);
367 /* Check for a shell command line first.
368 If it is not one, we can stop treating tab specially. */
369 if (lb
.buffer
[0] == '\t')
371 /* This line is a probably shell command. */
375 /* Ignore the commands in a rule with no targets. */
378 /* If there is no preceding rule line, don't treat this line
379 as a command, even though it begins with a tab character.
380 SunOS 4 make appears to behave this way. */
385 /* Yep, this is a shell command, and we don't care. */
388 /* Append this command line to the line being accumulated. */
390 if (commands_idx
== 0)
391 commands_started
= lineno
;
393 if (len
+ 1 + commands_idx
> commands_len
)
395 commands_len
= (len
+ 1 + commands_idx
) * 2;
396 commands
= (char *) xrealloc (commands
, commands_len
);
398 bcopy (p
, &commands
[commands_idx
], len
);
400 commands
[commands_idx
++] = '\n';
406 /* This line is not a shell command line. Don't worry about tabs. */
408 if (collapsed_length
< lb
.size
)
410 collapsed_length
= lb
.size
;
413 collapsed
= (char *) xmalloc (collapsed_length
);
415 strcpy (collapsed
, lb
.buffer
);
416 /* Collapse continuation lines. */
417 collapse_continuations (collapsed
);
418 remove_comments (collapsed
);
420 /* strncmp is first to avoid dereferencing out into space. */
421 #define word1eq(s, l) (!strncmp (s, p, l) \
422 && (p[l] == '\0' || isblank (p[l])))
427 /* This line is completely empty. */
430 /* We must first check for conditional and `define' directives before
431 ignoring anything, since they control what we will do with
434 if (!in_ignored_define
435 && (word1eq ("ifdef", 5) || word1eq ("ifndef", 6)
436 || word1eq ("ifeq", 4) || word1eq ("ifneq", 5)
437 || word1eq ("else", 4) || word1eq ("endif", 5)))
439 int i
= conditional_line (p
, filename
, lineno
);
443 makefile_fatal (filename
, lineno
,
444 "invalid syntax in conditional");
447 else if (word1eq ("endef", 5))
449 if (in_ignored_define
)
450 in_ignored_define
= 0;
452 makefile_fatal (filename
, lineno
, "extraneous `endef'");
455 else if (word1eq ("define", 6))
458 in_ignored_define
= 1;
461 p2
= next_token (p
+ 6);
462 /* Let the variable name be the whole rest of the line,
463 with trailing blanks stripped (comments have already been
464 removed), so it could be a complex variable/function
465 reference that might contain blanks. */
466 p
= index (p2
, '\0');
467 while (isblank (p
[-1]))
469 lineno
= do_define (p2
, p
- p2
, o_file
,
470 lineno
, infile
, filename
);
474 else if (word1eq ("override", 8))
476 p2
= next_token (p
+ 8);
478 makefile_error (filename
, lineno
, "empty `override' directive");
479 if (!strncmp (p2
, "define", 6) && (isblank (p2
[6]) || p2
[6] == '\0'))
482 in_ignored_define
= 1;
485 p2
= next_token (p2
+ 6);
486 /* Let the variable name be the whole rest of the line,
487 with trailing blanks stripped (comments have already been
488 removed), so it could be a complex variable/function
489 reference that might contain blanks. */
490 p
= index (p2
, '\0');
491 while (isblank (p
[-1]))
493 lineno
= do_define (p2
, p
- p2
, o_override
,
494 lineno
, infile
, filename
);
498 && !try_variable_definition (filename
, lineno
,
500 makefile_error (filename
, lineno
, "empty `override' directive");
506 /* Ignore the line. We continue here so conditionals
507 can appear in the middle of a rule. */
509 else if (word1eq ("export", 6))
512 p2
= next_token (p
+ 6);
514 export_all_variables
= 1;
515 v
= try_variable_definition (filename
, lineno
, p2
, o_file
);
517 v
->export
= v_export
;
521 for (p
= find_next_token (&p2
, &len
); p
!= 0;
522 p
= find_next_token (&p2
, &len
))
524 v
= lookup_variable (p
, len
);
526 v
= define_variable (p
, len
, "", o_file
, 0);
527 v
->export
= v_export
;
531 else if (word1eq ("unexport", 8))
535 p2
= next_token (p
+ 8);
537 export_all_variables
= 0;
538 for (p
= find_next_token (&p2
, &len
); p
!= 0;
539 p
= find_next_token (&p2
, &len
))
541 v
= lookup_variable (p
, len
);
543 v
= define_variable (p
, len
, "", o_file
, 0);
544 v
->export
= v_noexport
;
547 else if (word1eq ("include", 7) || word1eq ("-include", 8))
549 /* We have found an `include' line specifying a nested
550 makefile to be read at this point. */
551 struct conditionals
*save
, new_conditionals
;
552 struct nameseq
*files
;
553 /* "-include" (vs "include") says no
554 error if the file does not exist. */
555 int noerror
= p
[0] == '-';
557 p
= allocated_variable_expand (next_token (p
+ (noerror
? 9 : 8)));
560 makefile_error (filename
, lineno
,
561 "no file name for `%sinclude'",
566 /* Parse the list of file names. */
568 files
= multi_glob (parse_file_seq (&p2
, '\0',
569 sizeof (struct nameseq
),
571 sizeof (struct nameseq
));
574 /* Save the state of conditionals and start
575 the included makefile with a clean slate. */
577 bzero ((char *) &new_conditionals
, sizeof new_conditionals
);
578 conditionals
= &new_conditionals
;
580 /* Record the rules that are waiting so they will determine
581 the default goal before those in the included makefile. */
582 record_waiting_files ();
584 /* Read each included makefile. */
587 struct nameseq
*next
= files
->next
;
588 char *name
= files
->name
;
592 if (! read_makefile (name
, (RM_INCLUDED
| RM_NO_TILDE
593 | (noerror
? RM_DONTCARE
: 0)))
595 makefile_error (filename
, lineno
,
596 "%s: %s", name
, strerror (errno
));
599 /* Free any space allocated by conditional_line. */
600 if (conditionals
->ignoring
)
601 free (conditionals
->ignoring
);
602 if (conditionals
->seen_else
)
603 free (conditionals
->seen_else
);
607 reading_filename
= filename
;
608 reading_lineno_ptr
= &lineno
;
610 else if (word1eq ("vpath", 5))
614 p2
= variable_expand (p
+ 5);
615 p
= find_next_token (&p2
, &len
);
618 pattern
= savestring (p
, len
);
619 p
= find_next_token (&p2
, &len
);
620 /* No searchpath means remove all previous
621 selective VPATH's with the same pattern. */
624 /* No pattern means remove all previous selective VPATH's. */
626 construct_vpath_list (pattern
, p
);
631 else if (try_variable_definition (filename
, lineno
, p
, o_file
))
632 /* This line has been dealt with. */
634 else if (lb
.buffer
[0] == '\t')
636 p
= collapsed
; /* Ignore comments. */
640 /* The line is completely blank; that is harmless. */
642 /* This line starts with a tab but was not caught above
643 because there was no preceding target, and the line
644 might have been usable as a variable definition.
645 But now it is definitely lossage. */
646 makefile_fatal (filename
, lineno
,
647 "commands commence before first target");
651 /* This line describes some target files. */
655 /* Record the previous rule. */
657 record_waiting_files ();
659 /* Search the line for an unquoted ; that is not after an
661 cmdleft
= find_char_unquote (lb
.buffer
, ";#", 0);
662 if (cmdleft
!= 0 && *cmdleft
== '#')
664 /* We found a comment before a semicolon. */
668 else if (cmdleft
!= 0)
669 /* Found one. Cut the line short there before expanding it. */
672 collapse_continuations (lb
.buffer
);
674 /* Expand variable and function references before doing anything
675 else so that special characters can be inside variables. */
676 p
= variable_expand (lb
.buffer
);
679 /* Look for a semicolon in the expanded line. */
680 cmdleft
= find_char_unquote (p
, ";", 0);
683 /* Cut the line short at the semicolon. */
690 makefile_fatal (filename
, lineno
,
691 "missing rule before commands");
693 /* This line contained a variable reference that
694 expanded to nothing but whitespace. */
699 /* We accept and ignore rules without targets for
700 compatibility with SunOS 4 make. */
705 filenames
= multi_glob (parse_file_seq (&p2
, ':',
706 sizeof (struct nameseq
),
708 sizeof (struct nameseq
));
710 makefile_fatal (filename
, lineno
, "missing separator");
711 /* Is this a one-colon or two-colon entry? */
712 two_colon
= *p2
== ':';
716 /* We have some targets, so don't ignore the following commands. */
719 /* Is this a static pattern rule: `target: %targ: %dep; ...'? */
721 while (p
!= 0 && p
[-1] == '\\')
723 register char *q
= &p
[-1];
724 register int backslash
= 0;
726 backslash
= !backslash
;
728 p
= index (p
+ 1, ':');
733 /* For MS-DOS, skip a "C:\...". */
734 if (p
!= 0 && p
[1] == '\\' && isalpha (p
[-1]))
739 struct nameseq
*target
;
740 target
= parse_file_seq (&p2
, ':', sizeof (struct nameseq
), 1);
743 makefile_fatal (filename
, lineno
, "missing target pattern");
744 else if (target
->next
!= 0)
745 makefile_fatal (filename
, lineno
, "multiple target patterns");
746 pattern
= target
->name
;
747 pattern_percent
= find_percent (pattern
);
748 if (pattern_percent
== 0)
749 makefile_fatal (filename
, lineno
,
750 "target pattern contains no `%%'");
755 /* Parse the dependencies. */
756 deps
= (struct dep
*)
757 multi_glob (parse_file_seq (&p2
, '\0', sizeof (struct dep
), 1),
758 sizeof (struct dep
));
763 /* Semicolon means rest of line is a command. */
764 unsigned int len
= strlen (cmdleft
+ 1);
766 commands_started
= lineno
;
768 /* Add this command line to the buffer. */
769 if (len
+ 2 > commands_len
)
771 commands_len
= (len
+ 2) * 2;
772 commands
= (char *) xrealloc (commands
, commands_len
);
774 bcopy (cmdleft
+ 1, commands
, len
);
776 commands
[commands_idx
++] = '\n';
782 /* We get here except in the case that we just read a rule line.
783 Record now the last rule we read, so following spurious
784 commands are properly diagnosed. */
785 record_waiting_files ();
789 if (conditionals
->if_cmds
)
790 makefile_fatal (filename
, lineno
, "missing `endif'");
792 /* At eof, record the last rule. */
793 record_waiting_files ();
796 free ((char *) commands
);
799 reading_filename
= 0;
800 reading_lineno_ptr
= 0;
805 /* Execute a `define' directive.
806 The first line has already been read, and NAME is the name of
807 the variable to be defined. The following lines remain to be read.
808 LINENO, INFILE and FILENAME refer to the makefile being read.
809 The value returned is LINENO, updated for lines read here. */
812 do_define (name
, namelen
, origin
, lineno
, infile
, filename
)
814 unsigned int namelen
;
815 enum variable_origin origin
;
820 struct linebuffer lb
;
821 unsigned int nlines
= 0;
822 unsigned int length
= 100;
823 char *definition
= (char *) xmalloc (100);
824 register unsigned int idx
= 0;
827 /* Expand the variable name. */
828 char *var
= (char *) alloca (namelen
+ 1);
829 bcopy (name
, var
, namelen
);
831 var
= variable_expand (var
);
834 while (!feof (infile
))
837 nlines
= readline (&lb
, infile
, filename
, lineno
);
839 collapse_continuations (lb
.buffer
);
841 p
= next_token (lb
.buffer
);
842 if ((p
[5] == '\0' || isblank (p
[5])) && !strncmp (p
, "endef", 5))
846 if (*next_token (p
) != '\0')
847 makefile_error (filename
, lineno
,
848 "Extraneous text after `endef' directive");
849 /* Define the variable. */
851 definition
[0] = '\0';
853 definition
[idx
- 1] = '\0';
854 (void) define_variable (var
, strlen (var
), definition
, origin
, 1);
861 unsigned int len
= strlen (lb
.buffer
);
863 /* Increase the buffer size if necessary. */
864 if (idx
+ len
+ 1 > length
)
866 length
= (idx
+ len
) * 2;
867 definition
= (char *) xrealloc (definition
, length
+ 1);
870 bcopy (lb
.buffer
, &definition
[idx
], len
);
872 /* Separate lines with a newline. */
873 definition
[idx
++] = '\n';
878 makefile_fatal (filename
, lineno
, "missing `endef', unterminated `define'");
884 /* Interpret conditional commands "ifdef", "ifndef", "ifeq",
885 "ifneq", "else" and "endif".
886 LINE is the input line, with the command as its first word.
888 FILENAME and LINENO are the filename and line number in the
889 current makefile. They are used for error messages.
891 Value is -1 if the line is invalid,
892 0 if following text should be interpreted,
893 1 if following text should be ignored. */
896 conditional_line (line
, filename
, lineno
)
903 register unsigned int i
;
907 /* It's an "if..." command. */
908 notdef
= line
[2] == 'n';
911 cmdname
= line
[3] == 'd' ? "ifndef" : "ifneq";
912 line
+= cmdname
[3] == 'd' ? 7 : 6;
916 cmdname
= line
[2] == 'd' ? "ifdef" : "ifeq";
917 line
+= cmdname
[2] == 'd' ? 6 : 5;
922 /* It's an "else" or "endif" command. */
923 notdef
= line
[1] == 'n';
924 cmdname
= notdef
? "endif" : "else";
925 line
+= notdef
? 5 : 4;
928 line
= next_token (line
);
933 makefile_error (filename
, lineno
,
934 "Extraneous text after `%s' directive",
936 /* "Else" or "endif". */
937 if (conditionals
->if_cmds
== 0)
938 makefile_fatal (filename
, lineno
, "extraneous `%s'", cmdname
);
939 /* NOTDEF indicates an `endif' command. */
941 --conditionals
->if_cmds
;
942 else if (conditionals
->seen_else
[conditionals
->if_cmds
- 1])
943 makefile_fatal (filename
, lineno
, "only one `else' per conditional");
946 /* Toggle the state of ignorance. */
947 conditionals
->ignoring
[conditionals
->if_cmds
- 1]
948 = !conditionals
->ignoring
[conditionals
->if_cmds
- 1];
949 /* Record that we have seen an `else' in this conditional.
950 A second `else' will be erroneous. */
951 conditionals
->seen_else
[conditionals
->if_cmds
- 1] = 1;
953 for (i
= 0; i
< conditionals
->if_cmds
; ++i
)
954 if (conditionals
->ignoring
[i
])
959 if (conditionals
->allocated
== 0)
961 conditionals
->allocated
= 5;
962 conditionals
->ignoring
= (char *) xmalloc (conditionals
->allocated
);
963 conditionals
->seen_else
= (char *) xmalloc (conditionals
->allocated
);
966 ++conditionals
->if_cmds
;
967 if (conditionals
->if_cmds
> conditionals
->allocated
)
969 conditionals
->allocated
+= 5;
970 conditionals
->ignoring
= (char *)
971 xrealloc (conditionals
->ignoring
, conditionals
->allocated
);
972 conditionals
->seen_else
= (char *)
973 xrealloc (conditionals
->seen_else
, conditionals
->allocated
);
976 /* Record that we have seen an `if...' but no `else' so far. */
977 conditionals
->seen_else
[conditionals
->if_cmds
- 1] = 0;
979 /* Search through the stack to see if we're already ignoring. */
980 for (i
= 0; i
< conditionals
->if_cmds
- 1; ++i
)
981 if (conditionals
->ignoring
[i
])
983 /* We are already ignoring, so just push a level
984 to match the next "else" or "endif", and keep ignoring.
985 We don't want to expand variables in the condition. */
986 conditionals
->ignoring
[conditionals
->if_cmds
- 1] = 1;
990 if (cmdname
[notdef
? 3 : 2] == 'd')
992 /* "Ifdef" or "ifndef". */
994 register char *p
= end_of_token (line
);
999 v
= lookup_variable (line
, i
);
1000 conditionals
->ignoring
[conditionals
->if_cmds
- 1]
1001 = (v
!= 0 && *v
->value
!= '\0') == notdef
;
1005 /* "Ifeq" or "ifneq". */
1008 char termin
= *line
== '(' ? ',' : *line
;
1010 if (termin
!= ',' && termin
!= '"' && termin
!= '\'')
1014 /* Find the end of the first string. */
1017 register int count
= 0;
1018 for (; *line
!= '\0'; ++line
)
1021 else if (*line
== ')')
1023 else if (*line
== ',' && count
<= 0)
1027 while (*line
!= '\0' && *line
!= termin
)
1035 s2
= variable_expand (s1
);
1036 /* We must allocate a new copy of the expanded string because
1037 variable_expand re-uses the same buffer. */
1039 s1
= (char *) alloca (len
+ 1);
1040 bcopy (s2
, s1
, len
+ 1);
1043 /* Find the start of the second string. */
1044 line
= next_token (line
);
1046 termin
= termin
== ',' ? ')' : *line
;
1047 if (termin
!= ')' && termin
!= '"' && termin
!= '\'')
1050 /* Find the end of the second string. */
1053 register int count
= 0;
1054 s2
= next_token (line
);
1055 for (line
= s2
; *line
!= '\0'; ++line
)
1059 else if (*line
== ')')
1070 while (*line
!= '\0' && *line
!= termin
)
1078 line
= next_token (++line
);
1080 makefile_error (filename
, lineno
,
1081 "Extraneous text after `%s' directive",
1084 s2
= variable_expand (s2
);
1085 conditionals
->ignoring
[conditionals
->if_cmds
- 1]
1086 = streq (s1
, s2
) == notdef
;
1089 /* Search through the stack to see if we're ignoring. */
1090 for (i
= 0; i
< conditionals
->if_cmds
; ++i
)
1091 if (conditionals
->ignoring
[i
])
1096 /* Remove duplicate dependencies in CHAIN. */
1099 uniquize_deps (chain
)
1102 register struct dep
*d
;
1104 /* Make sure that no dependencies are repeated. This does not
1105 really matter for the purpose of updating targets, but it
1106 might make some names be listed twice for $^ and $?. */
1108 for (d
= chain
; d
!= 0; d
= d
->next
)
1110 struct dep
*last
, *next
;
1115 if (streq (dep_name (d
), dep_name (next
)))
1117 struct dep
*n
= next
->next
;
1119 if (next
->name
!= 0 && next
->name
!= d
->name
)
1122 free ((char *) next
);
1133 /* Record a description line for files FILENAMES,
1134 with dependencies DEPS, commands to execute described
1135 by COMMANDS and COMMANDS_IDX, coming from FILENAME:COMMANDS_STARTED.
1136 TWO_COLON is nonzero if a double colon was used.
1137 If not nil, PATTERN is the `%' pattern to make this
1138 a static pattern rule, and PATTERN_PERCENT is a pointer
1139 to the `%' within it.
1141 The links of FILENAMES are freed, and so are any names in it
1142 that are not incorporated into other data structures. */
1145 record_files (filenames
, pattern
, pattern_percent
, deps
, commands_started
,
1146 commands
, commands_idx
, two_colon
, filename
, lineno
, set_default
)
1147 struct nameseq
*filenames
;
1148 char *pattern
, *pattern_percent
;
1150 unsigned int commands_started
;
1152 unsigned int commands_idx
;
1155 unsigned int lineno
;
1158 struct nameseq
*nextf
;
1160 unsigned int max_targets
, target_idx
;
1161 char **targets
= 0, **target_percents
= 0;
1162 struct commands
*cmds
;
1164 if (commands_idx
> 0)
1166 cmds
= (struct commands
*) xmalloc (sizeof (struct commands
));
1167 cmds
->filename
= filename
;
1168 cmds
->lineno
= commands_started
;
1169 cmds
->commands
= savestring (commands
, commands_idx
);
1170 cmds
->command_lines
= 0;
1175 for (; filenames
!= 0; filenames
= nextf
)
1177 register char *name
= filenames
->name
;
1178 register struct file
*f
;
1179 register struct dep
*d
;
1181 char *implicit_percent
;
1183 nextf
= filenames
->next
;
1184 free ((char *) filenames
);
1186 implicit_percent
= find_percent (name
);
1187 implicit
|= implicit_percent
!= 0;
1189 if (implicit
&& pattern
!= 0)
1190 makefile_fatal (filename
, lineno
,
1191 "mixed implicit and static pattern rules");
1193 if (implicit
&& implicit_percent
== 0)
1194 makefile_fatal (filename
, lineno
, "mixed implicit and normal rules");
1201 targets
= (char **) xmalloc (5 * sizeof (char *));
1202 target_percents
= (char **) xmalloc (5 * sizeof (char *));
1205 else if (target_idx
== max_targets
- 1)
1208 targets
= (char **) xrealloc ((char *) targets
,
1209 max_targets
* sizeof (char *));
1211 = (char **) xrealloc ((char *) target_percents
,
1212 max_targets
* sizeof (char *));
1214 targets
[target_idx
] = name
;
1215 target_percents
[target_idx
] = implicit_percent
;
1220 /* If there are multiple filenames, copy the chain DEPS
1221 for all but the last one. It is not safe for the same deps
1222 to go in more than one place in the data base. */
1223 this = nextf
!= 0 ? copy_dep_chain (deps
) : deps
;
1226 /* If this is an extended static rule:
1227 `targets: target%pattern: dep%pattern; cmds',
1228 translate each dependency pattern into a plain filename
1229 using the target pattern and this target's name. */
1230 if (!pattern_matches (pattern
, pattern_percent
, name
))
1232 /* Give a warning if the rule is meaningless. */
1233 makefile_error (filename
, lineno
,
1234 "target `%s' doesn't match the target pattern",
1240 /* We use patsubst_expand to do the work of translating
1241 the target pattern, the target's name and the dependencies'
1242 patterns into plain dependency names. */
1243 char *buffer
= variable_expand ("");
1245 for (d
= this; d
!= 0; d
= d
->next
)
1248 char *percent
= find_percent (d
->name
);
1251 o
= patsubst_expand (buffer
, name
, pattern
, d
->name
,
1252 pattern_percent
, percent
);
1254 d
->name
= savestring (buffer
, o
- buffer
);
1260 /* Single-colon. Combine these dependencies
1261 with others in file's existing record, if any. */
1262 f
= enter_file (name
);
1264 if (f
->double_colon
)
1265 makefile_fatal (filename
, lineno
,
1266 "target file `%s' has both : and :: entries",
1269 /* If CMDS == F->CMDS, this target was listed in this rule
1270 more than once. Just give a warning since this is harmless. */
1271 if (cmds
!= 0 && cmds
== f
->cmds
)
1274 "target `%s' given more than once in the same rule.",
1277 /* Check for two single-colon entries both with commands.
1278 Check is_target so that we don't lose on files such as .c.o
1279 whose commands were preinitialized. */
1280 else if (cmds
!= 0 && f
->cmds
!= 0 && f
->is_target
)
1282 makefile_error (cmds
->filename
, cmds
->lineno
,
1283 "warning: overriding commands for target `%s'",
1285 makefile_error (f
->cmds
->filename
, f
->cmds
->lineno
,
1286 "warning: ignoring old commands for target `%s'",
1292 /* Defining .DEFAULT with no deps or cmds clears it. */
1293 if (f
== default_file
&& this == 0 && cmds
== 0)
1297 /* Defining .SUFFIXES with no dependencies
1298 clears out the list of suffixes. */
1299 if (f
== suffix_file
&& this == 0)
1304 struct dep
*nextd
= d
->next
;
1311 else if (f
->deps
!= 0)
1313 /* Add the file's old deps and the new ones in THIS together. */
1315 struct dep
*firstdeps
, *moredeps
;
1318 /* This is the rule with commands, so put its deps first.
1319 The rationale behind this is that $< expands to the
1320 first dep in the chain, and commands use $< expecting
1321 to get the dep that rule specifies. */
1327 /* Append the new deps to the old ones. */
1328 firstdeps
= f
->deps
;
1333 firstdeps
= moredeps
;
1337 while (d
->next
!= 0)
1342 f
->deps
= firstdeps
;
1347 /* If this is a static pattern rule, set the file's stem to
1348 the part of its name that matched the `%' in the pattern,
1349 so you can use $* in the commands. */
1352 static char *percent
= "%";
1353 char *buffer
= variable_expand ("");
1354 char *o
= patsubst_expand (buffer
, name
, pattern
, percent
,
1355 pattern_percent
, percent
);
1356 f
->stem
= savestring (buffer
, o
- buffer
);
1361 /* Double-colon. Make a new record
1362 even if the file already has one. */
1363 f
= lookup_file (name
);
1364 /* Check for both : and :: rules. Check is_target so
1365 we don't lose on default suffix rules or makefiles. */
1366 if (f
!= 0 && f
->is_target
&& !f
->double_colon
)
1367 makefile_fatal (filename
, lineno
,
1368 "target file `%s' has both : and :: entries",
1370 f
= enter_file (name
);
1371 /* If there was an existing entry and it was a double-colon
1372 entry, enter_file will have returned a new one, making it the
1373 prev pointer of the old one, and setting its double_colon
1374 pointer to the first one. */
1375 if (f
->double_colon
== 0)
1376 /* This is the first entry for this name, so we must
1377 set its double_colon pointer to itself. */
1378 f
->double_colon
= f
;
1384 /* Free name if not needed further. */
1385 if (f
!= 0 && name
!= f
->name
1386 && (name
< f
->name
|| name
> f
->name
+ strlen (f
->name
)))
1392 /* See if this is first target seen whose name does
1393 not start with a `.', unless it contains a slash. */
1394 if (default_goal_file
== 0 && set_default
1395 && (*name
!= '.' || index (name
, '/') != 0))
1399 /* If this file is a suffix, don't
1400 let it be the default goal file. */
1402 for (d
= suffix_file
->deps
; d
!= 0; d
= d
->next
)
1404 register struct dep
*d2
;
1405 if (*dep_name (d
) != '.' && streq (name
, dep_name (d
)))
1410 for (d2
= suffix_file
->deps
; d2
!= 0; d2
= d2
->next
)
1412 register unsigned int len
= strlen (dep_name (d2
));
1413 if (strncmp (name
, dep_name (d2
), len
))
1415 if (streq (name
+ len
, dep_name (d
)))
1426 default_goal_file
= f
;
1432 targets
[target_idx
] = 0;
1433 target_percents
[target_idx
] = 0;
1434 create_pattern_rule (targets
, target_percents
, two_colon
, deps
, cmds
, 1);
1435 free ((char *) target_percents
);
1439 /* Search STRING for an unquoted STOPCHAR or blank (if BLANK is nonzero).
1440 Backslashes quote STOPCHAR, blanks if BLANK is nonzero, and backslash.
1441 Quoting backslashes are removed from STRING by compacting it into
1442 itself. Returns a pointer to the first unquoted STOPCHAR if there is
1443 one, or nil if there are none. */
1446 find_char_unquote (string
, stopchars
, blank
)
1451 unsigned int string_len
= strlen (string
);
1452 register char *p
= string
;
1456 while (*p
!= '\0' && index (stopchars
, *p
) == 0
1457 && (!blank
|| !isblank (*p
)))
1462 if (p
> string
&& p
[-1] == '\\')
1464 /* Search for more backslashes. */
1465 register int i
= -2;
1466 while (&p
[i
] >= string
&& p
[i
] == '\\')
1469 /* The number of backslashes is now -I.
1470 Copy P over itself to swallow half of them. */
1471 bcopy (&p
[i
/ 2], &p
[i
], (string_len
- (p
- string
)) - (i
/ 2) + 1);
1474 /* All the backslashes quoted each other; the STOPCHAR was
1478 /* The STOPCHAR was quoted by a backslash. Look for another. */
1481 /* No backslash in sight. */
1485 /* Never hit a STOPCHAR or blank (with BLANK nonzero). */
1489 /* Search PATTERN for an unquoted %. */
1492 find_percent (pattern
)
1495 return find_char_unquote (pattern
, "%", 0);
1498 /* Parse a string into a sequence of filenames represented as a
1499 chain of struct nameseq's in reverse order and return that chain.
1501 The string is passed as STRINGP, the address of a string pointer.
1502 The string pointer is updated to point at the first character
1503 not parsed, which either is a null char or equals STOPCHAR.
1505 SIZE is how big to construct chain elements.
1506 This is useful if we want them actually to be other structures
1507 that have room for additional info.
1509 If STRIP is nonzero, strip `./'s off the beginning. */
1512 parse_file_seq (stringp
, stopchar
, size
, strip
)
1518 register struct nameseq
*new = 0;
1519 register struct nameseq
*new1
, *lastnew1
;
1520 register char *p
= *stringp
;
1524 stopchars
[0] = stopchar
;
1525 stopchars
[1] = '\0';
1529 /* Skip whitespace; see if any more names are left. */
1535 /* Yes, find end of next name. */
1537 p
= find_char_unquote (q
, stopchars
, 1);
1539 /* For MS-DOS, skip a "C:\...". */
1540 if (stopchar
== ':' && p
!= 0 && p
[1] == '\\' && isalpha (p
[-1]))
1547 /* Skip leading `./'s. */
1548 while (p
- q
> 2 && q
[0] == '.' && q
[1] == '/')
1550 q
+= 2; /* Skip "./". */
1551 while (q
< p
&& *q
== '/')
1552 /* Skip following slashes: ".//foo" is "foo", not "/foo". */
1556 /* Extract the filename just found, and skip it. */
1559 /* ".///" was stripped to "". */
1560 name
= savestring ("./", 2);
1562 name
= savestring (q
, p
- q
);
1564 /* Add it to the front of the chain. */
1565 new1
= (struct nameseq
*) xmalloc (size
);
1573 /* Look for multi-word archive references.
1574 They are indicated by a elt ending with an unmatched `)' and
1575 an elt further down the chain (i.e., previous in the file list)
1576 with an unmatched `(' (e.g., "lib(mem"). */
1581 if (new1
->name
[0] != '(' /* Don't catch "(%)" and suchlike. */
1582 && new1
->name
[strlen (new1
->name
) - 1] == ')'
1583 && index (new1
->name
, '(') == 0)
1585 /* NEW1 ends with a `)' but does not contain a `('.
1586 Look back for an elt with an opening `(' but no closing `)'. */
1588 struct nameseq
*n
= new1
->next
, *lastn
= new1
;
1590 while (n
!= 0 && (paren
= index (n
->name
, '(')) == 0)
1596 /* Ignore something starting with `(', as that cannot actually
1597 be an archive-member reference (and treating it as such
1598 results in an empty file name, which causes much lossage). */
1599 && n
->name
[0] != '(')
1601 /* N is the first element in the archive group.
1602 Its name looks like "lib(mem" (with no closing `)'). */
1606 /* Copy "lib(" into LIBNAME. */
1608 libname
= (char *) alloca (paren
- n
->name
+ 1);
1609 bcopy (n
->name
, libname
, paren
- n
->name
);
1610 libname
[paren
- n
->name
] = '\0';
1614 /* N was just "lib(", part of something like "lib( a b)".
1615 Edit it out of the chain and free its storage. */
1616 lastn
->next
= n
->next
;
1619 /* LASTN->next is the new stopping elt for the loop below. */
1624 /* Replace N's name with the full archive reference. */
1625 name
= concat (libname
, paren
, ")");
1630 if (new1
->name
[1] == '\0')
1632 /* NEW1 is just ")", part of something like "lib(a b )".
1633 Omit it from the chain and free its storage. */
1637 lastnew1
->next
= new1
->next
;
1641 free ((char *) lastn
);
1645 /* Replace also NEW1->name, which already has closing `)'. */
1646 name
= concat (libname
, new1
->name
, "");
1652 /* Trace back from NEW1 (the end of the list) until N
1653 (the beginning of the list), rewriting each name
1654 with the full archive reference. */
1658 name
= concat (libname
, new1
->name
, ")");
1667 /* No frobnication happening. Just step down the list. */
1684 /* Read a line of text from STREAM into LINEBUFFER.
1685 Combine continuation lines into one line.
1686 Return the number of actual lines read (> 1 if hacked continuation lines).
1690 readline (linebuffer
, stream
, filename
, lineno
)
1691 struct linebuffer
*linebuffer
;
1694 unsigned int lineno
;
1696 char *buffer
= linebuffer
->buffer
;
1697 register char *p
= linebuffer
->buffer
;
1698 register char *end
= p
+ linebuffer
->size
;
1699 register int len
, lastlen
= 0;
1701 register unsigned int nlines
= 0;
1702 register int backslash
;
1706 while (fgets (p
, end
- p
, stream
) != 0)
1711 /* This only happens when the first thing on the line is a '\0'.
1712 It is a pretty hopeless case, but (wonder of wonders) Athena
1713 lossage strikes again! (xmkmf puts NULs in its makefiles.)
1714 There is nothing really to be done; we synthesize a newline so
1715 the following line doesn't appear to be part of this line. */
1716 makefile_error (filename
, lineno
,
1717 "warning: NUL character seen; rest of line ignored");
1725 /* Probably ran out of buffer space. */
1726 register unsigned int p_off
= p
- buffer
;
1727 linebuffer
->size
*= 2;
1728 buffer
= (char *) xrealloc (buffer
, linebuffer
->size
);
1730 end
= buffer
+ linebuffer
->size
;
1731 linebuffer
->buffer
= buffer
;
1739 if (len
== 1 && p
> buffer
)
1740 /* P is pointing at a newline and it's the beginning of
1741 the buffer returned by the last fgets call. However,
1742 it is not necessarily the beginning of a line if P is
1743 pointing past the beginning of the holding buffer.
1744 If the buffer was just enlarged (right before the newline),
1745 we must account for that, so we pretend that the two lines
1750 for (p2
= p
- 2; --len
> 0; --p2
)
1753 backslash
= !backslash
;
1766 /* Enlarge the buffer. */
1767 register unsigned int p_off
= p
- buffer
;
1768 linebuffer
->size
*= 2;
1769 buffer
= (char *) xrealloc (buffer
, linebuffer
->size
);
1771 end
= buffer
+ linebuffer
->size
;
1772 linebuffer
->buffer
= buffer
;
1776 if (ferror (stream
))
1777 pfatal_with_name (filename
);
1782 /* Construct the list of include directories
1783 from the arguments and the default list. */
1786 construct_include_path (arg_dirs
)
1789 register unsigned int i
;
1792 /* Table to hold the dirs. */
1794 register unsigned int defsize
= (sizeof (default_include_directories
)
1795 / sizeof (default_include_directories
[0]));
1796 register unsigned int max
= 5;
1797 register char **dirs
= (char **) xmalloc ((5 + defsize
) * sizeof (char *));
1798 register unsigned int idx
= 0;
1800 /* First consider any dirs specified with -I switches.
1801 Ignore dirs that don't exist. */
1804 while (*arg_dirs
!= 0)
1806 char *dir
= *arg_dirs
++;
1810 char *expanded
= tilde_expand (dir
);
1815 if (stat (dir
, &stbuf
) == 0 && S_ISDIR (stbuf
.st_mode
))
1821 xrealloc ((char *) dirs
, (max
+ defsize
) * sizeof (char *));
1825 else if (dir
!= arg_dirs
[-1])
1829 /* Now add at the end the standard default dirs. */
1831 for (i
= 0; default_include_directories
[i
] != 0; ++i
)
1832 if (stat (default_include_directories
[i
], &stbuf
) == 0
1833 && S_ISDIR (stbuf
.st_mode
))
1834 dirs
[idx
++] = default_include_directories
[i
];
1838 /* Now compute the maximum length of any name in it. */
1841 for (i
= 0; i
< idx
; ++i
)
1843 unsigned int len
= strlen (dirs
[i
]);
1844 /* If dir name is written with a trailing slash, discard it. */
1845 if (dirs
[i
][len
- 1] == '/')
1846 /* We can't just clobber a null in because it may have come from
1847 a literal string and literal strings may not be writable. */
1848 dirs
[i
] = savestring (dirs
[i
], len
- 1);
1849 if (len
> max_incl_len
)
1853 include_directories
= dirs
;
1856 /* Expand ~ or ~USER at the beginning of NAME.
1857 Return a newly malloc'd string or 0. */
1863 if (name
[1] == '/' || name
[1] == '\0')
1865 extern char *getenv ();
1870 /* Turn off --warn-undefined-variables while we expand HOME. */
1871 int save
= warn_undefined_variables_flag
;
1872 warn_undefined_variables_flag
= 0;
1874 home_dir
= allocated_variable_expand ("$(HOME)");
1876 warn_undefined_variables_flag
= save
;
1879 is_variable
= home_dir
[0] != '\0';
1883 home_dir
= getenv ("HOME");
1885 if (home_dir
== 0 || home_dir
[0] == '\0')
1887 extern char *getlogin ();
1888 char *name
= getlogin ();
1892 struct passwd
*p
= getpwnam (name
);
1894 home_dir
= p
->pw_dir
;
1899 char *new = concat (home_dir
, "", name
+ 1);
1907 struct passwd
*pwent
;
1908 char *userend
= index (name
+ 1, '/');
1911 pwent
= getpwnam (name
+ 1);
1915 return savestring (pwent
->pw_dir
, strlen (pwent
->pw_dir
));
1917 return concat (pwent
->pw_dir
, "/", userend
+ 1);
1919 else if (userend
!= 0)
1926 /* Given a chain of struct nameseq's describing a sequence of filenames,
1927 in reverse of the intended order, return a new chain describing the
1928 result of globbing the filenames. The new chain is in forward order.
1929 The links of the old chain are freed or used in the new chain.
1930 Likewise for the names in the old chain.
1932 SIZE is how big to construct chain elements.
1933 This is useful if we want them actually to be other structures
1934 that have room for additional info. */
1937 multi_glob (chain
, size
)
1938 struct nameseq
*chain
;
1941 extern void dir_setup_glob ();
1942 register struct nameseq
*new = 0;
1943 register struct nameseq
*old
;
1944 struct nameseq
*nexto
;
1947 dir_setup_glob (&gl
);
1949 for (old
= chain
; old
!= 0; old
= nexto
)
1957 if (old
->name
[0] == '~')
1959 char *newname
= tilde_expand (old
->name
);
1963 old
->name
= newname
;
1968 if (ar_name (old
->name
))
1970 /* OLD->name is an archive member reference.
1971 Replace it with the archive file name,
1972 and save the member name in MEMNAME.
1973 We will glob on the archive name and then
1974 reattach MEMNAME later. */
1976 ar_parse_name (old
->name
, &arname
, &memname
);
1984 switch (glob (old
->name
, GLOB_NOCHECK
|GLOB_ALTDIRFUNC
, NULL
, &gl
))
1986 case 0: /* Success. */
1988 register int i
= gl
.gl_pathc
;
1994 /* Try to glob on MEMNAME within the archive. */
1995 struct nameseq
*found
1996 = ar_glob (gl
.gl_pathv
[i
], memname
, size
);
1999 /* No matches. Use MEMNAME as-is. */
2001 = (struct nameseq
*) xmalloc (size
);
2002 unsigned int alen
= strlen (gl
.gl_pathv
[i
]);
2003 unsigned int mlen
= strlen (memname
);
2004 elt
->name
= (char *) xmalloc (alen
+ 1 + mlen
+ 2);
2005 bcopy (gl
.gl_pathv
[i
], elt
->name
, alen
);
2006 elt
->name
[alen
] = '(';
2007 bcopy (memname
, &elt
->name
[alen
+ 1], mlen
);
2008 elt
->name
[alen
+ 1 + mlen
] = ')';
2009 elt
->name
[alen
+ 1 + mlen
+ 1] = '\0';
2015 /* Find the end of the FOUND chain. */
2016 struct nameseq
*f
= found
;
2017 while (f
->next
!= 0)
2020 /* Attach the chain being built to the end of the FOUND
2021 chain, and make FOUND the new NEW chain. */
2031 struct nameseq
*elt
= (struct nameseq
*) xmalloc (size
);
2032 elt
->name
= savestring (gl
.gl_pathv
[i
],
2033 strlen (gl
.gl_pathv
[i
]));
2045 fatal ("virtual memory exhausted");