1 /* Copyright (C) 1988, 1989, 1990, 1991, 1992, 1993
2 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 void 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 read_makefile (name
, 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 read_makefile (*makefiles
, 0);
162 /* Find the right element of read_makefiles. */
164 while (d
->next
!= tail
)
167 /* Use the storage read_makefile allocates. */
168 *makefiles
= dep_name (d
);
173 /* If there were no -f switches, try the default names. */
175 if (num_makefiles
== 0)
177 static char *default_makefiles
[] =
178 { "GNUmakefile", "makefile", "Makefile", 0 };
179 register char **p
= default_makefiles
;
180 while (*p
!= 0 && !file_exists_p (*p
))
184 read_makefile (*p
, 0);
187 /* No default makefile was found. Add the default makefiles to the
188 `read_makefiles' chain so they will be updated if possible. */
189 struct dep
*tail
= read_makefiles
;
190 for (p
= default_makefiles
; *p
!= 0; ++p
)
192 struct dep
*d
= (struct dep
*) xmalloc (sizeof (struct dep
));
194 d
->file
= enter_file (*p
);
195 d
->file
->dontcare
= 1;
196 /* Tell update_goal_chain to bail out as soon as this file is
197 made, and main not to die if we can't make this file. */
198 d
->changed
= RM_DONTCARE
;
210 return read_makefiles
;
213 /* Read file FILENAME as a makefile and add its contents to the data base.
215 FLAGS contains bits as above.
217 FILENAME is added to the `read_makefiles' chain. */
220 read_makefile (filename
, flags
)
224 static char *collapsed
= 0;
225 static unsigned int collapsed_length
= 0;
226 register FILE *infile
;
227 struct linebuffer lb
;
228 unsigned int commands_len
= 200;
229 char *commands
= (char *) xmalloc (200);
230 unsigned int commands_idx
= 0;
231 unsigned int commands_started
;
234 int ignoring
= 0, in_ignored_define
= 0;
235 int no_targets
= 0; /* Set when reading a rule without targets. */
237 struct nameseq
*filenames
= 0;
239 unsigned int lineno
= 1;
240 unsigned int nlines
= 0;
242 char *pattern
= 0, *pattern_percent
;
246 #define record_waiting_files() \
249 if (filenames != 0) \
250 record_files (filenames, pattern, pattern_percent, deps, \
251 commands_started, commands, commands_idx, \
252 two_colon, filename, lineno, \
253 !(flags & RM_NO_DEFAULT_GOAL)); \
259 #ifdef lint /* Suppress `used before set' messages. */
263 /* First, get a stream to read. */
265 /* Expand ~ in FILENAME unless it came from `include',
266 in which case it was already done. */
267 if (!(flags
& RM_NO_TILDE
) && filename
[0] == '~')
269 char *expanded
= tilde_expand (filename
);
270 /* This is a possible memory leak, but I don't care. */
275 infile
= fopen (filename
, "r");
276 /* Save the error code so we print the right message later. */
277 makefile_errno
= errno
;
279 /* If the makefile wasn't found and it's either a makefile from
280 the `MAKEFILES' variable or an included makefile,
281 search the included makefile search path for this makefile. */
283 if (infile
== 0 && (flags
& RM_INCLUDED
) && *filename
!= '/')
285 register unsigned int i
;
286 for (i
= 0; include_directories
[i
] != 0; ++i
)
288 char *name
= concat (include_directories
[i
], "/", filename
);
289 infile
= fopen (name
, "r");
300 /* Add FILENAME to the chain of read makefiles. */
301 deps
= (struct dep
*) xmalloc (sizeof (struct dep
));
302 deps
->next
= read_makefiles
;
303 read_makefiles
= deps
;
305 deps
->file
= lookup_file (filename
);
308 deps
->file
= enter_file (savestring (filename
, strlen (filename
)));
309 if (flags
& RM_DONTCARE
)
310 deps
->file
->dontcare
= 1;
312 filename
= deps
->file
->name
;
313 deps
->file
->precious
= 1;
314 deps
->changed
= flags
;
317 /* If the makefile can't be found at all,
318 either ignore it or give up entirely. */
322 if (! (flags
& RM_DONTCARE
))
324 /* If we did some searching, errno has the error
325 from the last attempt, rather from FILENAME itself. */
326 errno
= makefile_errno
;
327 perror_with_name ("fopen: ", filename
);
332 reading_filename
= filename
;
333 reading_lineno_ptr
= &lineno
;
335 /* Loop over lines in the file.
336 The strategy is to accumulate target names in FILENAMES, dependencies
337 in DEPS and commands in COMMANDS. These are used to define a rule
338 when the start of the next rule (or eof) is encountered. */
342 while (!feof (infile
))
345 nlines
= readline (&lb
, infile
, filename
, lineno
);
347 if (collapsed_length
< lb
.size
)
349 collapsed_length
= lb
.size
;
352 collapsed
= (char *) xmalloc (collapsed_length
);
354 strcpy (collapsed
, lb
.buffer
);
355 /* Collapse continuation lines. */
356 collapse_continuations (collapsed
);
357 remove_comments (collapsed
);
362 /* We cannot consider a line containing just a tab to be empty
363 because it might constitute an empty command for a target. */
364 if (*p
== '\0' && lb
.buffer
[0] != '\t')
367 /* strncmp is first to avoid dereferencing out into space. */
368 #define word1eq(s, l) (!strncmp (s, p, l) \
369 && (p[l] == '\0' || isblank (p[l])))
370 if (!in_ignored_define
371 && (word1eq ("ifdef", 5) || word1eq ("ifndef", 6)
372 || word1eq ("ifeq", 4) || word1eq ("ifneq", 5)
373 || word1eq ("else", 4) || word1eq ("endif", 5)))
375 int i
= conditional_line (p
, filename
, lineno
);
379 makefile_fatal (filename
, lineno
,
380 "invalid syntax in conditional");
383 else if (word1eq ("endef", 5))
385 if (in_ignored_define
)
386 in_ignored_define
= 0;
388 makefile_fatal (filename
, lineno
, "extraneous `endef'");
391 else if (word1eq ("define", 6))
394 in_ignored_define
= 1;
397 p2
= next_token (p
+ 6);
398 p
= end_of_token (p2
);
399 lineno
= do_define (p2
, p
- p2
, o_file
,
400 lineno
, infile
, filename
);
404 else if (word1eq ("override", 8))
406 p2
= next_token (p
+ 8);
408 makefile_error (filename
, lineno
, "empty `override' directive");
409 if (!strncmp (p2
, "define", 6) && (isblank (p2
[6]) || p2
[6] == '\0'))
412 in_ignored_define
= 1;
416 p2
= end_of_token (p2
);
417 p
= find_next_token (&p2
, &len
);
418 lineno
= do_define (p
, len
, o_override
,
419 lineno
, infile
, filename
);
423 && !try_variable_definition (filename
, lineno
,
425 makefile_error (filename
, lineno
, "empty `override' directive");
432 /* Ignore the line. We continue here so conditionals
433 can appear in the middle of a rule. */
435 else if (lb
.buffer
[0] == '\t')
437 /* This line is a shell command. */
441 /* Ignore the commands in a rule with no targets. */
444 /* If there is no preceding rule line, don't treat this line
445 as a command, even though it begins with a tab character.
446 SunOS 4 make appears to behave this way. */
450 /* Append this command line to the line being accumulated. */
452 if (commands_idx
== 0)
453 commands_started
= lineno
;
455 if (len
+ 1 + commands_idx
> commands_len
)
457 commands_len
= (len
+ 1 + commands_idx
) * 2;
458 commands
= (char *) xrealloc (commands
, commands_len
);
460 bcopy (p
, &commands
[commands_idx
], len
);
462 commands
[commands_idx
++] = '\n';
468 if (word1eq ("export", 6))
471 p2
= next_token (p
+ 6);
473 export_all_variables
= 1;
474 v
= try_variable_definition (filename
, lineno
, p2
, o_file
);
476 v
->export
= v_export
;
480 for (p
= find_next_token (&p2
, &len
); p
!= 0;
481 p
= find_next_token (&p2
, &len
))
483 v
= lookup_variable (p
, len
);
485 v
= define_variable (p
, len
, "", o_file
, 0);
486 v
->export
= v_export
;
490 else if (word1eq ("unexport", 8))
494 p2
= next_token (p
+ 8);
496 export_all_variables
= 0;
497 for (p
= find_next_token (&p2
, &len
); p
!= 0;
498 p
= find_next_token (&p2
, &len
))
500 v
= lookup_variable (p
, len
);
502 v
= define_variable (p
, len
, "", o_file
, 0);
503 v
->export
= v_noexport
;
506 else if (word1eq ("include", 7) || word1eq ("-include", 8))
508 /* We have found an `include' line specifying a nested
509 makefile to be read at this point. */
510 struct conditionals
*save
, new_conditionals
;
511 struct nameseq
*files
;
512 /* "-include" (vs "include") says no
513 error if the file does not exist. */
514 int noerror
= p
[0] == '-';
516 p
= allocated_variable_expand (next_token (p
+ (noerror
? 9 : 8)));
519 makefile_error (filename
, lineno
,
520 "no file name for `%sinclude'",
525 /* Parse the list of file names. */
527 files
= multi_glob (parse_file_seq (&p2
, '\0',
528 sizeof (struct nameseq
),
530 sizeof (struct nameseq
));
533 /* Save the state of conditionals and start
534 the included makefile with a clean slate. */
536 bzero ((char *) &new_conditionals
, sizeof new_conditionals
);
537 conditionals
= &new_conditionals
;
539 /* Record the rules that are waiting so they will determine
540 the default goal before those in the included makefile. */
541 record_waiting_files ();
543 /* Read each included makefile. */
546 struct nameseq
*next
= files
->next
;
547 char *name
= files
->name
;
551 read_makefile (name
, (RM_INCLUDED
| RM_NO_TILDE
552 | (noerror
? RM_DONTCARE
: 0)));
557 reading_filename
= filename
;
558 reading_lineno_ptr
= &lineno
;
560 else if (word1eq ("vpath", 5))
564 p2
= variable_expand (p
+ 5);
565 p
= find_next_token (&p2
, &len
);
568 pattern
= savestring (p
, len
);
569 p
= find_next_token (&p2
, &len
);
570 /* No searchpath means remove all previous
571 selective VPATH's with the same pattern. */
574 /* No pattern means remove all previous selective VPATH's. */
576 construct_vpath_list (pattern
, p
);
581 else if (try_variable_definition (filename
, lineno
, p
, o_file
))
582 /* This line has been dealt with. */
586 /* This line describes some target files. */
590 /* Record the previous rule. */
592 record_waiting_files ();
594 /* Look for a semicolon in the unexpanded line. */
595 cmdleft
= find_semicolon (lb
.buffer
);
597 /* Found one. Cut the line short there before expanding it. */
600 collapse_continuations (lb
.buffer
);
602 /* Expand variable and function references before doing anything
603 else so that special characters can be inside variables. */
604 p
= variable_expand (lb
.buffer
);
607 /* Look for a semicolon in the expanded line. */
608 cmdleft
= find_semicolon (p
);
611 /* Cut the line short at the semicolon. */
614 /* Remove comments from the line. */
621 makefile_fatal (filename
, lineno
,
622 "missing rule before commands");
624 /* This line contained a variable reference that
625 expanded to nothing but whitespace. */
630 /* We accept and ignore rules without targets for
631 compatibility with SunOS 4 make. */
636 filenames
= multi_glob (parse_file_seq (&p2
, ':',
637 sizeof (struct nameseq
),
639 sizeof (struct nameseq
));
641 makefile_fatal (filename
, lineno
, "missing separator");
642 /* Is this a one-colon or two-colon entry? */
643 two_colon
= *p2
== ':';
647 /* We have some targets, so don't ignore the following commands. */
650 /* Is this a static pattern rule: `target: %targ: %dep; ...'? */
652 while (p
!= 0 && p
[-1] == '\\')
654 register char *q
= &p
[-1];
655 register int backslash
= 0;
657 backslash
= !backslash
;
659 p
= index (p
+ 1, ':');
665 struct nameseq
*target
;
666 target
= parse_file_seq (&p2
, ':', sizeof (struct nameseq
), 1);
669 makefile_fatal (filename
, lineno
, "missing target pattern");
670 else if (target
->next
!= 0)
671 makefile_fatal (filename
, lineno
, "multiple target patterns");
672 pattern
= target
->name
;
673 pattern_percent
= find_percent (pattern
);
674 if (pattern_percent
== 0)
675 makefile_fatal (filename
, lineno
,
676 "target pattern contains no `%%'");
681 /* Parse the dependencies. */
682 deps
= (struct dep
*)
683 multi_glob (parse_file_seq (&p2
, '\0', sizeof (struct dep
), 1),
684 sizeof (struct dep
));
689 /* Semicolon means rest of line is a command. */
690 unsigned int len
= strlen (cmdleft
+ 1);
692 commands_started
= lineno
;
694 /* Add this command line to the buffer. */
695 if (len
+ 2 > commands_len
)
697 commands_len
= (len
+ 2) * 2;
698 commands
= (char *) xrealloc (commands
, commands_len
);
700 bcopy (cmdleft
+ 1, commands
, len
);
702 commands
[commands_idx
++] = '\n';
708 /* We get here except in the case that we just read a rule line.
709 Record now the last rule we read, so following spurious
710 commands are properly diagnosed. */
711 record_waiting_files ();
715 if (conditionals
->if_cmds
)
716 makefile_fatal (filename
, lineno
, "missing `endif'");
718 /* At eof, record the last rule. */
719 record_waiting_files ();
722 free ((char *) commands
);
725 reading_filename
= 0;
726 reading_lineno_ptr
= 0;
729 /* Execute a `define' directive.
730 The first line has already been read, and NAME is the name of
731 the variable to be defined. The following lines remain to be read.
732 LINENO, INFILE and FILENAME refer to the makefile being read.
733 The value returned is LINENO, updated for lines read here. */
736 do_define (name
, namelen
, origin
, lineno
, infile
, filename
)
738 unsigned int namelen
;
739 enum variable_origin origin
;
744 struct linebuffer lb
;
745 unsigned int nlines
= 0;
746 unsigned int length
= 100;
747 char *definition
= (char *) xmalloc (100);
748 register unsigned int idx
= 0;
751 /* Expand the variable name. */
752 char *var
= (char *) alloca (namelen
+ 1);
753 bcopy (name
, var
, namelen
);
755 var
= variable_expand (var
);
758 while (!feof (infile
))
761 nlines
= readline (&lb
, infile
, filename
, lineno
);
762 p
= next_token (lb
.buffer
);
764 if ((p
[5] == '\0' || isblank (p
[5])) && !strncmp (p
, "endef", 5))
767 collapse_continuations (p
);
769 if (*next_token (p
) != '\0')
770 makefile_error (filename
, lineno
,
771 "Extraneous text after `endef' directive");
772 /* Define the variable. */
774 definition
[0] = '\0';
776 definition
[idx
- 1] = '\0';
777 (void) define_variable (var
, strlen (var
), definition
, origin
, 1);
784 unsigned int len
= strlen (p
);
786 /* Increase the buffer size if necessary. */
787 if (idx
+ len
+ 1 > length
)
789 length
= (idx
+ len
) * 2;
790 definition
= (char *) xrealloc (definition
, length
+ 1);
793 bcopy (p
, &definition
[idx
], len
);
795 /* Separate lines with a newline. */
796 definition
[idx
++] = '\n';
801 makefile_fatal (filename
, lineno
, "missing `endef', unterminated `define'");
807 /* Interpret conditional commands "ifdef", "ifndef", "ifeq",
808 "ifneq", "else" and "endif".
809 LINE is the input line, with the command as its first word.
811 FILENAME and LINENO are the filename and line number in the
812 current makefile. They are used for error messages.
814 Value is -1 if the line is invalid,
815 0 if following text should be interpreted,
816 1 if following text should be ignored. */
819 conditional_line (line
, filename
, lineno
)
826 register unsigned int i
;
830 /* It's an "if..." command. */
831 notdef
= line
[2] == 'n';
834 cmdname
= line
[3] == 'd' ? "ifndef" : "ifneq";
835 line
+= cmdname
[3] == 'd' ? 7 : 6;
839 cmdname
= line
[2] == 'd' ? "ifdef" : "ifeq";
840 line
+= cmdname
[2] == 'd' ? 6 : 5;
845 /* It's an "else" or "endif" command. */
846 notdef
= line
[1] == 'n';
847 cmdname
= notdef
? "endif" : "else";
848 line
+= notdef
? 5 : 4;
851 line
= next_token (line
);
856 makefile_error (filename
, lineno
,
857 "Extraneous text after `%s' directive",
859 /* "Else" or "endif". */
860 if (conditionals
->if_cmds
== 0)
861 makefile_fatal (filename
, lineno
, "extraneous `%s'", cmdname
);
862 /* NOTDEF indicates an `endif' command. */
864 --conditionals
->if_cmds
;
865 else if (conditionals
->seen_else
[conditionals
->if_cmds
- 1])
866 makefile_fatal (filename
, lineno
, "only one `else' per conditional");
869 /* Toggle the state of ignorance. */
870 conditionals
->ignoring
[conditionals
->if_cmds
- 1]
871 = !conditionals
->ignoring
[conditionals
->if_cmds
- 1];
872 /* Record that we have seen an `else' in this conditional.
873 A second `else' will be erroneous. */
874 conditionals
->seen_else
[conditionals
->if_cmds
- 1] = 1;
876 for (i
= 0; i
< conditionals
->if_cmds
; ++i
)
877 if (conditionals
->ignoring
[i
])
882 if (conditionals
->allocated
== 0)
884 conditionals
->allocated
= 5;
885 conditionals
->ignoring
= (char *) xmalloc (conditionals
->allocated
);
886 conditionals
->seen_else
= (char *) xmalloc (conditionals
->allocated
);
889 ++conditionals
->if_cmds
;
890 if (conditionals
->if_cmds
> conditionals
->allocated
)
892 conditionals
->allocated
+= 5;
893 conditionals
->ignoring
= (char *)
894 xrealloc (conditionals
->ignoring
, conditionals
->allocated
);
895 conditionals
->seen_else
= (char *)
896 xrealloc (conditionals
->seen_else
, conditionals
->allocated
);
899 /* Record that we have seen an `if...' but no `else' so far. */
900 conditionals
->seen_else
[conditionals
->if_cmds
- 1] = 0;
902 /* Search through the stack to see if we're already ignoring. */
903 for (i
= 0; i
< conditionals
->if_cmds
- 1; ++i
)
904 if (conditionals
->ignoring
[i
])
906 /* We are already ignoring, so just push a level
907 to match the next "else" or "endif", and keep ignoring.
908 We don't want to expand variables in the condition. */
909 conditionals
->ignoring
[conditionals
->if_cmds
- 1] = 1;
913 if (cmdname
[notdef
? 3 : 2] == 'd')
915 /* "Ifdef" or "ifndef". */
917 register char *p
= end_of_token (line
);
922 v
= lookup_variable (line
, i
);
923 conditionals
->ignoring
[conditionals
->if_cmds
- 1]
924 = (v
!= 0 && *v
->value
!= '\0') == notdef
;
928 /* "Ifeq" or "ifneq". */
931 char termin
= *line
== '(' ? ',' : *line
;
933 if (termin
!= ',' && termin
!= '"' && termin
!= '\'')
937 /* Find the end of the first string. */
940 register int count
= 0;
941 for (; *line
!= '\0'; ++line
)
944 else if (*line
== ')')
946 else if (*line
== ',' && count
<= 0)
950 while (*line
!= '\0' && *line
!= termin
)
958 s2
= variable_expand (s1
);
959 /* We must allocate a new copy of the expanded string because
960 variable_expand re-uses the same buffer. */
962 s1
= (char *) alloca (len
+ 1);
963 bcopy (s2
, s1
, len
+ 1);
966 /* Find the start of the second string. */
967 line
= next_token (line
);
969 termin
= termin
== ',' ? ')' : *line
;
970 if (termin
!= ')' && termin
!= '"' && termin
!= '\'')
973 /* Find the end of the second string. */
976 register int count
= 0;
977 s2
= next_token (line
);
978 for (line
= s2
; *line
!= '\0'; ++line
)
982 else if (*line
== ')')
993 while (*line
!= '\0' && *line
!= termin
)
1001 line
= next_token (++line
);
1003 makefile_error (filename
, lineno
,
1004 "Extraneous text after `%s' directive",
1007 s2
= variable_expand (s2
);
1008 conditionals
->ignoring
[conditionals
->if_cmds
- 1]
1009 = streq (s1
, s2
) == notdef
;
1012 /* Search through the stack to see if we're ignoring. */
1013 for (i
= 0; i
< conditionals
->if_cmds
; ++i
)
1014 if (conditionals
->ignoring
[i
])
1019 /* Remove duplicate dependencies in CHAIN. */
1022 uniquize_deps (chain
)
1025 register struct dep
*d
;
1027 /* Make sure that no dependencies are repeated. This does not
1028 really matter for the purpose of updating targets, but it
1029 might make some names be listed twice for $^ and $?. */
1031 for (d
= chain
; d
!= 0; d
= d
->next
)
1033 struct dep
*last
, *next
;
1038 if (streq (dep_name (d
), dep_name (next
)))
1040 struct dep
*n
= next
->next
;
1042 if (next
->name
!= 0 && next
->name
!= d
->name
)
1045 free ((char *) next
);
1056 /* Record a description line for files FILENAMES,
1057 with dependencies DEPS, commands to execute described
1058 by COMMANDS and COMMANDS_IDX, coming from FILENAME:COMMANDS_STARTED.
1059 TWO_COLON is nonzero if a double colon was used.
1060 If not nil, PATTERN is the `%' pattern to make this
1061 a static pattern rule, and PATTERN_PERCENT is a pointer
1062 to the `%' within it.
1064 The links of FILENAMES are freed, and so are any names in it
1065 that are not incorporated into other data structures. */
1068 record_files (filenames
, pattern
, pattern_percent
, deps
, commands_started
,
1069 commands
, commands_idx
, two_colon
, filename
, lineno
, set_default
)
1070 struct nameseq
*filenames
;
1071 char *pattern
, *pattern_percent
;
1073 unsigned int commands_started
;
1075 unsigned int commands_idx
;
1078 unsigned int lineno
;
1081 struct nameseq
*nextf
;
1083 unsigned int max_targets
, target_idx
;
1084 char **targets
= 0, **target_percents
= 0;
1085 struct commands
*cmds
;
1087 if (commands_idx
> 0)
1089 cmds
= (struct commands
*) xmalloc (sizeof (struct commands
));
1090 cmds
->filename
= filename
;
1091 cmds
->lineno
= commands_started
;
1092 cmds
->commands
= savestring (commands
, commands_idx
);
1093 cmds
->command_lines
= 0;
1098 for (; filenames
!= 0; filenames
= nextf
)
1100 register char *name
= filenames
->name
;
1101 register struct file
*f
;
1102 register struct dep
*d
;
1104 char *implicit_percent
;
1106 nextf
= filenames
->next
;
1107 free ((char *) filenames
);
1109 implicit_percent
= find_percent (name
);
1110 implicit
|= implicit_percent
!= 0;
1112 if (implicit
&& pattern
!= 0)
1113 makefile_fatal (filename
, lineno
,
1114 "mixed implicit and static pattern rules");
1116 if (implicit
&& implicit_percent
== 0)
1117 makefile_fatal (filename
, lineno
, "mixed implicit and normal rules");
1124 targets
= (char **) xmalloc (5 * sizeof (char *));
1125 target_percents
= (char **) xmalloc (5 * sizeof (char *));
1128 else if (target_idx
== max_targets
- 1)
1131 targets
= (char **) xrealloc ((char *) targets
,
1132 max_targets
* sizeof (char *));
1134 = (char **) xrealloc ((char *) target_percents
,
1135 max_targets
* sizeof (char *));
1137 targets
[target_idx
] = name
;
1138 target_percents
[target_idx
] = implicit_percent
;
1143 /* If there are multiple filenames, copy the chain DEPS
1144 for all but the last one. It is not safe for the same deps
1145 to go in more than one place in the data base. */
1146 this = nextf
!= 0 ? copy_dep_chain (deps
) : deps
;
1149 /* If this is an extended static rule:
1150 `targets: target%pattern: dep%pattern; cmds',
1151 translate each dependency pattern into a plain filename
1152 using the target pattern and this target's name. */
1153 if (!pattern_matches (pattern
, pattern_percent
, name
))
1155 /* Give a warning if the rule is meaningless. */
1156 makefile_error (filename
, lineno
,
1157 "target `%s' doesn't match the target pattern",
1163 /* We use patsubst_expand to do the work of translating
1164 the target pattern, the target's name and the dependencies'
1165 patterns into plain dependency names. */
1166 char *buffer
= variable_expand ("");
1168 for (d
= this; d
!= 0; d
= d
->next
)
1171 char *percent
= find_percent (d
->name
);
1174 o
= patsubst_expand (buffer
, name
, pattern
, d
->name
,
1175 pattern_percent
, percent
);
1177 d
->name
= savestring (buffer
, o
- buffer
);
1183 /* Single-colon. Combine these dependencies
1184 with others in file's existing record, if any. */
1185 f
= enter_file (name
);
1187 if (f
->double_colon
)
1188 makefile_fatal (filename
, lineno
,
1189 "target file `%s' has both : and :: entries",
1192 /* If CMDS == F->CMDS, this target was listed in this rule
1193 more than once. Just give a warning since this is harmless. */
1194 if (cmds
!= 0 && cmds
== f
->cmds
)
1197 "target `%s' given more than once in the same rule.",
1200 /* Check for two single-colon entries both with commands.
1201 Check is_target so that we don't lose on files such as .c.o
1202 whose commands were preinitialized. */
1203 else if (cmds
!= 0 && f
->cmds
!= 0 && f
->is_target
)
1205 makefile_error (cmds
->filename
, cmds
->lineno
,
1206 "warning: overriding commands for target `%s'",
1208 makefile_error (f
->cmds
->filename
, f
->cmds
->lineno
,
1209 "warning: ignoring old commands for target `%s'",
1215 /* Defining .DEFAULT with no deps or cmds clears it. */
1216 if (f
== default_file
&& this == 0 && cmds
== 0)
1220 /* Defining .SUFFIXES with no dependencies
1221 clears out the list of suffixes. */
1222 if (f
== suffix_file
&& this == 0)
1227 struct dep
*nextd
= d
->next
;
1234 else if (f
->deps
!= 0)
1236 /* Add the file's old deps and the new ones in THIS together. */
1238 struct dep
*firstdeps
, *moredeps
;
1241 /* This is the rule with commands, so put its deps first.
1242 The rationale behind this is that $< expands to the
1243 first dep in the chain, and commands use $< expecting
1244 to get the dep that rule specifies. */
1250 /* Append the new deps to the old ones. */
1251 firstdeps
= f
->deps
;
1256 firstdeps
= moredeps
;
1260 while (d
->next
!= 0)
1265 f
->deps
= firstdeps
;
1270 /* If this is a static pattern rule, set the file's stem to
1271 the part of its name that matched the `%' in the pattern,
1272 so you can use $* in the commands. */
1275 static char *percent
= "%";
1276 char *buffer
= variable_expand ("");
1277 char *o
= patsubst_expand (buffer
, name
, pattern
, percent
,
1278 pattern_percent
, percent
);
1279 f
->stem
= savestring (buffer
, o
- buffer
);
1284 /* Double-colon. Make a new record
1285 even if the file already has one. */
1286 f
= lookup_file (name
);
1287 /* Check for both : and :: rules. Check is_target so
1288 we don't lose on default suffix rules or makefiles. */
1289 if (f
!= 0 && f
->is_target
&& !f
->double_colon
)
1290 makefile_fatal (filename
, lineno
,
1291 "target file `%s' has both : and :: entries",
1293 f
= enter_file (name
);
1294 /* If there was an existing entry and it was a
1295 double-colon entry, enter_file will have returned a
1296 new one, making it the prev pointer of the old one. */
1298 f
->double_colon
= 1;
1303 /* Free name if not needed further. */
1304 if (f
!= 0 && name
!= f
->name
1305 && (name
< f
->name
|| name
> f
->name
+ strlen (f
->name
)))
1311 /* See if this is first target seen whose name does
1312 not start with a `.', unless it contains a slash. */
1313 if (default_goal_file
== 0 && set_default
1314 && (*name
!= '.' || index (name
, '/') != 0))
1318 /* If this file is a suffix, don't
1319 let it be the default goal file. */
1321 for (d
= suffix_file
->deps
; d
!= 0; d
= d
->next
)
1323 register struct dep
*d2
;
1324 if (*dep_name (d
) != '.' && streq (name
, dep_name (d
)))
1329 for (d2
= suffix_file
->deps
; d2
!= 0; d2
= d2
->next
)
1331 register unsigned int len
= strlen (dep_name (d2
));
1332 if (strncmp (name
, dep_name (d2
), len
))
1334 if (streq (name
+ len
, dep_name (d
)))
1345 default_goal_file
= f
;
1351 targets
[target_idx
] = 0;
1352 target_percents
[target_idx
] = 0;
1353 create_pattern_rule (targets
, target_percents
, two_colon
, deps
, cmds
, 1);
1354 free ((char *) target_percents
);
1358 /* Search STRING for an unquoted ; that is not after an unquoted #. */
1361 find_semicolon (string
)
1366 found
= index (string
, ';');
1367 while (found
!= 0 && found
[-1] == '\\')
1369 register char *q
= &found
[-1];
1370 register int backslash
= 0;
1371 while (*q
-- == '\\')
1372 backslash
= !backslash
;
1374 found
= index (found
+ 1, ';');
1381 /* Look for a comment character (#) before the ; we found. */
1382 p
= lindex (string
, found
, '#');
1383 while (p
!= 0 && p
[-1] == '\\')
1385 register char *q
= &p
[-1];
1386 register int backslash
= 0;
1387 while (*q
-- == '\\')
1388 backslash
= !backslash
;
1390 p
= lindex (p
+ 1, found
, '#');
1399 /* Search PATTERN for an unquoted %. Backslashes quote % and backslash.
1400 Quoting backslashes are removed from PATTERN by compacting it into
1401 itself. Returns a pointer to the first unquoted % if there is one,
1402 or nil if there are none. */
1405 find_percent (pattern
)
1408 unsigned int pattern_len
= strlen (pattern
);
1409 register char *p
= pattern
;
1411 while ((p
= index (p
, '%')) != 0)
1412 if (p
> pattern
&& p
[-1] == '\\')
1414 /* Search for more backslashes. */
1415 register int i
= -2;
1416 while (&p
[i
] >= pattern
&& p
[i
] == '\\')
1419 /* The number of backslashes is now -I.
1420 Copy P over itself to swallow half of them. */
1421 bcopy (&p
[i
/ 2], &p
[i
], (pattern_len
- (p
- pattern
)) - (i
/ 2) + 1);
1424 /* All the backslashes quoted each other; the % was unquoted. */
1427 /* The % was quoted by a backslash. Look for another. */
1430 /* No backslash in sight. */
1433 /* Never hit a %. */
1437 /* Parse a string into a sequence of filenames represented as a
1438 chain of struct nameseq's in reverse order and return that chain.
1440 The string is passed as STRINGP, the address of a string pointer.
1441 The string pointer is updated to point at the first character
1442 not parsed, which either is a null char or equals STOPCHAR.
1444 SIZE is how big to construct chain elements.
1445 This is useful if we want them actually to be other structures
1446 that have room for additional info.
1448 If STRIP is nonzero, strip `./'s off the beginning. */
1451 parse_file_seq (stringp
, stopchar
, size
, strip
)
1457 register struct nameseq
*new = 0;
1458 register struct nameseq
*new1
;
1459 register char *p
= *stringp
;
1466 /* Skip whitespace; see if any more names are left. */
1472 /* Yes, find end of next name. */
1479 else if (c
== '\\' &&
1480 (*p
== '\\' || isblank (*p
) || *p
== stopchar
))
1482 else if (isblank (c
) || c
== stopchar
)
1488 /* Skip leading `./'s. */
1489 while (p
- q
> 2 && q
[0] == '.' && q
[1] == '/')
1491 q
+= 2; /* Skip "./". */
1492 while (q
< p
&& *q
== '/')
1493 /* Skip following slashes: ".//foo" is "foo", not "/foo". */
1497 /* Extract the filename just found, and skip it. */
1500 /* ".///" was stripped to "". */
1501 name
= savestring ("./", 2);
1503 name
= savestring (q
, p
- q
);
1505 /* Add it to the front of the chain. */
1506 new1
= (struct nameseq
*) xmalloc (size
);
1514 /* Look for multi-word archive references.
1515 They are indicated by a elt ending with an unmatched `)' and
1516 an elt further down the chain (i.e., previous in the file list)
1517 with an unmatched `(' (e.g., "lib(mem"). */
1519 for (new1
= new; new1
!= 0; new1
= new1
->next
)
1520 if (new1
->name
[0] != '(' /* Don't catch "(%)" and suchlike. */
1521 && new1
->name
[strlen (new1
->name
) - 1] == ')'
1522 && index (new1
->name
, '(') == 0)
1524 /* NEW1 ends with a `)' but does not contain a `('.
1525 Look back for an elt with an opening `(' but no closing `)'. */
1527 struct nameseq
*n
= new1
->next
, *lastn
= new1
;
1529 while (n
!= 0 && (paren
= index (n
->name
, '(')) == 0)
1536 /* N is the first element in the archive group.
1537 Its name looks like "lib(mem" (with no closing `)'). */
1541 /* Copy "lib(" into LIBNAME. */
1543 libname
= (char *) alloca (paren
- n
->name
+ 1);
1544 bcopy (n
->name
, libname
, paren
- n
->name
);
1545 libname
[paren
- n
->name
] = '\0';
1549 /* N was just "lib(", part of something like "lib( a b)".
1550 Edit it out of the chain and free its storage. */
1551 lastn
->next
= n
->next
;
1554 /* LASTN->next is the new stopping elt for the loop below. */
1559 /* Replace N's name with the full archive reference. */
1560 name
= concat (libname
, paren
, ")");
1565 if (new1
->name
[1] == '\0')
1567 /* NEW1 is just ")", part of something like "lib(a b )".
1568 Omit it from the chain and free its storage. */
1574 free ((char *) lastn
);
1578 /* Replace also NEW1->name, which already has closing `)'. */
1579 name
= concat (libname
, new1
->name
, "");
1585 /* Trace back from NEW1 (the end of the list) until N
1586 (the beginning of the list), rewriting each name
1587 with the full archive reference. */
1591 name
= concat (libname
, new1
->name
, ")");
1605 /* Read a line of text from STREAM into LINEBUFFER.
1606 Combine continuation lines into one line.
1607 Return the number of actual lines read (> 1 if hacked continuation lines).
1611 readline (linebuffer
, stream
, filename
, lineno
)
1612 struct linebuffer
*linebuffer
;
1615 unsigned int lineno
;
1617 char *buffer
= linebuffer
->buffer
;
1618 register char *p
= linebuffer
->buffer
;
1619 register char *end
= p
+ linebuffer
->size
;
1620 register int len
, lastlen
= 0;
1622 register unsigned int nlines
= 0;
1623 register int backslash
;
1629 if (fgets (p
, end
- p
, stream
) == 0)
1633 pfatal_with_name (filename
);
1637 /* This only happens when the first thing on the line is a '\0'. */
1638 makefile_fatal (filename
, lineno
, "NUL not allowed in makefile");
1643 /* Probably ran out of buffer space. */
1644 register unsigned int p_off
= p
- buffer
;
1645 linebuffer
->size
*= 2;
1646 buffer
= (char *) xrealloc (buffer
, linebuffer
->size
);
1648 end
= buffer
+ linebuffer
->size
;
1649 linebuffer
->buffer
= buffer
;
1657 if (len
== 1 && p
> buffer
)
1658 /* P is pointing at a newline and it's the beginning of
1659 the buffer returned by the last fgets call. However,
1660 it is not necessarily the beginning of a line if P is
1661 pointing past the beginning of the holding buffer.
1662 If the buffer was just enlarged (right before the newline),
1663 we must account for that, so we pretend that the two lines
1668 for (p2
= p
- 2; --len
> 0; --p2
)
1671 backslash
= !backslash
;
1684 /* Enlarge the buffer. */
1685 register unsigned int p_off
= p
- buffer
;
1686 linebuffer
->size
*= 2;
1687 buffer
= (char *) xrealloc (buffer
, linebuffer
->size
);
1689 end
= buffer
+ linebuffer
->size
;
1690 linebuffer
->buffer
= buffer
;
1697 /* Construct the list of include directories
1698 from the arguments and the default list. */
1701 construct_include_path (arg_dirs
)
1704 register unsigned int i
;
1707 /* Table to hold the dirs. */
1709 register unsigned int defsize
= (sizeof (default_include_directories
)
1710 / sizeof (default_include_directories
[0]));
1711 register unsigned int max
= 5;
1712 register char **dirs
= (char **) xmalloc ((5 + defsize
) * sizeof (char *));
1713 register unsigned int idx
= 0;
1715 /* First consider any dirs specified with -I switches.
1716 Ignore dirs that don't exist. */
1719 while (*arg_dirs
!= 0)
1721 char *dir
= *arg_dirs
++;
1725 char *expanded
= tilde_expand (dir
);
1730 if (stat (dir
, &stbuf
) == 0 && S_ISDIR (stbuf
.st_mode
))
1736 xrealloc ((char *) dirs
, (max
+ defsize
) * sizeof (char *));
1740 else if (dir
!= arg_dirs
[-1])
1744 /* Now add at the end the standard default dirs. */
1746 for (i
= 0; default_include_directories
[i
] != 0; ++i
)
1747 if (stat (default_include_directories
[i
], &stbuf
) == 0
1748 && S_ISDIR (stbuf
.st_mode
))
1749 dirs
[idx
++] = default_include_directories
[i
];
1753 /* Now compute the maximum length of any name in it. */
1756 for (i
= 0; i
< idx
; ++i
)
1758 unsigned int len
= strlen (dirs
[i
]);
1759 /* If dir name is written with a trailing slash, discard it. */
1760 if (dirs
[i
][len
- 1] == '/')
1761 /* We can't just clobber a null in because it may have come from
1762 a literal string and literal strings may not be writable. */
1763 dirs
[i
] = savestring (dirs
[i
], len
- 1);
1764 if (len
> max_incl_len
)
1768 include_directories
= dirs
;
1771 /* Expand ~ or ~USER at the beginning of NAME.
1772 Return a newly malloc'd string or 0. */
1778 if (name
[1] == '/' || name
[1] == '\0')
1780 extern char *getenv ();
1785 /* Turn off --warn-undefined-variables while we expand HOME. */
1786 int save
= warn_undefined_variables_flag
;
1787 warn_undefined_variables_flag
= 0;
1789 home_dir
= allocated_variable_expand ("$(HOME)");
1791 warn_undefined_variables_flag
= save
;
1794 is_variable
= home_dir
[0] != '\0';
1798 home_dir
= getenv ("HOME");
1800 if (home_dir
== 0 || home_dir
[0] == '\0')
1802 extern char *getlogin ();
1803 char *name
= getlogin ();
1807 struct passwd
*p
= getpwnam (name
);
1809 home_dir
= p
->pw_dir
;
1814 char *new = concat (home_dir
, "", name
+ 1);
1822 struct passwd
*pwent
;
1823 char *userend
= index (name
+ 1, '/');
1826 pwent
= getpwnam (name
+ 1);
1830 return savestring (pwent
->pw_dir
, strlen (pwent
->pw_dir
));
1832 return concat (pwent
->pw_dir
, "/", userend
+ 1);
1834 else if (userend
!= 0)
1841 /* Given a chain of struct nameseq's describing a sequence of filenames,
1842 in reverse of the intended order, return a new chain describing the
1843 result of globbing the filenames. The new chain is in forward order.
1844 The links of the old chain are freed or used in the new chain.
1845 Likewise for the names in the old chain.
1847 SIZE is how big to construct chain elements.
1848 This is useful if we want them actually to be other structures
1849 that have room for additional info. */
1852 multi_glob (chain
, size
)
1853 struct nameseq
*chain
;
1856 register struct nameseq
*new = 0;
1857 register struct nameseq
*old
;
1858 struct nameseq
*nexto
;
1860 for (old
= chain
; old
!= 0; old
= nexto
)
1869 if (old
->name
[0] == '~')
1871 char *newname
= tilde_expand (old
->name
);
1875 old
->name
= newname
;
1880 if (ar_name (old
->name
))
1882 /* OLD->name is an archive member reference.
1883 Replace it with the archive file name,
1884 and save the member name in MEMNAME.
1885 We will glob on the archive name and then
1886 reattach MEMNAME later. */
1888 ar_parse_name (old
->name
, &arname
, &memname
);
1896 switch (glob (old
->name
, GLOB_NOCHECK
, NULL
, &gl
))
1898 case 0: /* Success. */
1900 register int i
= gl
.gl_pathc
;
1906 /* Try to glob on MEMNAME within the archive. */
1907 struct nameseq
*found
1908 = ar_glob (gl
.gl_pathv
[i
], memname
, size
);
1911 /* No matches. Use MEMNAME as-is. */
1913 = (struct nameseq
*) xmalloc (size
);
1914 unsigned int alen
= strlen (gl
.gl_pathv
[i
]);
1915 unsigned int mlen
= strlen (memname
);
1916 elt
->name
= (char *) xmalloc (alen
+ 1 + mlen
+ 2);
1917 bcopy (gl
.gl_pathv
[i
], elt
->name
, alen
);
1918 elt
->name
[alen
] = '(';
1919 bcopy (memname
, &elt
->name
[alen
+ 1], mlen
);
1920 elt
->name
[alen
+ 1 + mlen
] = ')';
1921 elt
->name
[alen
+ 1 + mlen
+ 1] = '\0';
1927 /* Find the end of the FOUND chain. */
1928 struct nameseq
*f
= found
;
1929 while (f
->next
!= 0)
1932 /* Attach the chain being built to the end of the FOUND
1933 chain, and make FOUND the new NEW chain. */
1943 struct nameseq
*elt
= (struct nameseq
*) xmalloc (size
);
1944 elt
->name
= savestring (gl
.gl_pathv
[i
],
1945 strlen (gl
.gl_pathv
[i
]));
1957 fatal ("virtual memory exhausted");