VirtualBox

source: kBuild/trunk/src/kmk/read.c@ 1826

Last change on this file since 1826 was 1821, checked in by bird, 16 years ago

kmk: incdep bug fix, dependencies was chopped of because they are now loaded after we define the rule.

  • Property svn:eol-style set to native
File size: 103.6 KB
Line 
1/* Reading and parsing of makefiles for GNU Make.
2Copyright (C) 1988, 1989, 1990, 1991, 1992, 1993, 1994, 1995, 1996, 1997,
31998, 1999, 2000, 2001, 2002, 2003, 2004, 2005, 2006 Free Software
4Foundation, Inc.
5This file is part of GNU Make.
6
7GNU Make is free software; you can redistribute it and/or modify it under the
8terms of the GNU General Public License as published by the Free Software
9Foundation; either version 2, or (at your option) any later version.
10
11GNU Make is distributed in the hope that it will be useful, but WITHOUT ANY
12WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR
13A PARTICULAR PURPOSE. See the GNU General Public License for more details.
14
15You should have received a copy of the GNU General Public License along with
16GNU Make; see the file COPYING. If not, write to the Free Software
17Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. */
18
19#include "make.h"
20
21#include <assert.h>
22
23#include <glob.h>
24
25#include "dep.h"
26#include "filedef.h"
27#include "job.h"
28#include "commands.h"
29#include "variable.h"
30#include "rule.h"
31#include "debug.h"
32#include "hash.h"
33#ifdef KMK
34# include "kbuild.h"
35#endif
36
37#ifndef WINDOWS32
38#ifndef _AMIGA
39#ifndef VMS
40#include <pwd.h>
41#else
42struct passwd *getpwnam (char *name);
43#endif
44#endif
45#endif /* !WINDOWS32 */
46
47/* A 'struct ebuffer' controls the origin of the makefile we are currently
48 eval'ing.
49*/
50
51struct ebuffer
52 {
53 char *buffer; /* Start of the current line in the buffer. */
54 char *bufnext; /* Start of the next line in the buffer. */
55 char *bufstart; /* Start of the entire buffer. */
56#ifdef CONFIG_WITH_VALUE_LENGTH
57 char *eol; /* End of the current line in the buffer. */
58#endif
59 unsigned int size; /* Malloc'd size of buffer. */
60 FILE *fp; /* File, or NULL if this is an internal buffer. */
61 struct floc floc; /* Info on the file in fp (if any). */
62 };
63
64/* Types of "words" that can be read in a makefile. */
65enum make_word_type
66 {
67 w_bogus, w_eol, w_static, w_variable, w_colon, w_dcolon, w_semicolon,
68 w_varassign
69 };
70
71
72/* A `struct conditionals' contains the information describing
73 all the active conditionals in a makefile.
74
75 The global variable `conditionals' contains the conditionals
76 information for the current makefile. It is initialized from
77 the static structure `toplevel_conditionals' and is later changed
78 to new structures for included makefiles. */
79
80struct conditionals
81 {
82 unsigned int if_cmds; /* Depth of conditional nesting. */
83 unsigned int allocated; /* Elts allocated in following arrays. */
84 char *ignoring; /* Are we ignoring or interpreting?
85 0=interpreting, 1=not yet interpreted,
86 2=already interpreted */
87 char *seen_else; /* Have we already seen an `else'? */
88 };
89
90static struct conditionals toplevel_conditionals;
91static struct conditionals *conditionals = &toplevel_conditionals;
92
93
94/* Default directories to search for include files in */
95
96static const char *default_include_directories[] =
97 {
98#ifndef KMK
99#if defined(WINDOWS32) && !defined(INCLUDEDIR)
100/* This completely up to the user when they install MSVC or other packages.
101 This is defined as a placeholder. */
102# define INCLUDEDIR "."
103#endif
104# ifdef INCLUDEDIR /* bird */
105 INCLUDEDIR,
106# else /* bird */
107 ".", /* bird */
108# endif /* bird */
109#ifndef _AMIGA
110 "/usr/gnu/include",
111 "/usr/local/include",
112 "/usr/include",
113#endif
114#endif /* !KMK */
115 0
116 };
117
118/* List of directories to search for include files in */
119
120static const char **include_directories;
121
122/* Maximum length of an element of the above. */
123
124static unsigned int max_incl_len;
125
126/* The filename and pointer to line number of the
127 makefile currently being read in. */
128
129const struct floc *reading_file = 0;
130
131/* The chain of makefiles read by read_makefile. */
132
133static struct dep *read_makefiles = 0;
134
135static int eval_makefile (const char *filename, int flags);
136static int eval (struct ebuffer *buffer, int flags);
137
138static long readline (struct ebuffer *ebuf);
139static void do_define (char *name, unsigned int namelen,
140 enum variable_origin origin, struct ebuffer *ebuf);
141#ifndef CONFIG_WITH_VALUE_LENGTH
142static int conditional_line (char *line, int len, const struct floc *flocp);
143#else
144static int conditional_line (char *line, char *eol, int len, const struct floc *flocp);
145#endif
146#ifndef CONFIG_WITH_INCLUDEDEP
147static void record_files (struct nameseq *filenames, const char *pattern,
148 const char *pattern_percent, struct dep *deps,
149 unsigned int cmds_started, char *commands,
150 unsigned int commands_idx, int two_colon,
151 const struct floc *flocp);
152#endif /* !KMK */
153static void record_target_var (struct nameseq *filenames, char *defn,
154 enum variable_origin origin, int enabled,
155 const struct floc *flocp);
156static enum make_word_type get_next_mword (char *buffer, char *delim,
157 char **startp, unsigned int *length);
158#ifndef CONFIG_WITH_VALUE_LENGTH
159static void remove_comments (char *line);
160#else
161static char *remove_comments (char *line, char *eol);
162#endif
163static char *find_char_unquote (char *string, int stop1, int stop2,
164 int blank, int ignorevars);
165
166
167/* Read in all the makefiles and return the chain of their names. */
168
169struct dep *
170read_all_makefiles (const char **makefiles)
171{
172 unsigned int num_makefiles = 0;
173
174 /* Create *_LIST variables, to hold the makefiles, targets, and variables
175 we will be reading. */
176
177 define_variable ("MAKEFILE_LIST", sizeof ("MAKEFILE_LIST")-1, "", o_file, 0);
178
179 DB (DB_BASIC, (_("Reading makefiles...\n")));
180
181 /* If there's a non-null variable MAKEFILES, its value is a list of
182 files to read first thing. But don't let it prevent reading the
183 default makefiles and don't let the default goal come from there. */
184
185 {
186 char *value;
187 char *name, *p;
188 unsigned int length;
189
190 {
191 /* Turn off --warn-undefined-variables while we expand MAKEFILES. */
192 int save = warn_undefined_variables_flag;
193 warn_undefined_variables_flag = 0;
194
195#ifndef CONFIG_WITH_VALUE_LENGTH
196 value = allocated_variable_expand ("$(MAKEFILES)");
197#else
198 value = allocated_variable_expand_2 (STRING_SIZE_TUPLE("$(MAKEFILES)"), NULL);
199#endif
200
201 warn_undefined_variables_flag = save;
202 }
203
204 /* Set NAME to the start of next token and LENGTH to its length.
205 MAKEFILES is updated for finding remaining tokens. */
206 p = value;
207
208 while ((name = find_next_token ((const char **)&p, &length)) != 0)
209 {
210 if (*p != '\0')
211 *p++ = '\0';
212 eval_makefile (name, RM_NO_DEFAULT_GOAL|RM_INCLUDED|RM_DONTCARE);
213 }
214
215 free (value);
216 }
217
218 /* Read makefiles specified with -f switches. */
219
220 if (makefiles != 0)
221 while (*makefiles != 0)
222 {
223 struct dep *tail = read_makefiles;
224 register struct dep *d;
225
226 if (! eval_makefile (*makefiles, 0))
227 perror_with_name ("", *makefiles);
228
229 /* Find the right element of read_makefiles. */
230 d = read_makefiles;
231 while (d->next != tail)
232 d = d->next;
233
234 /* Use the storage read_makefile allocates. */
235 *makefiles = dep_name (d);
236 ++num_makefiles;
237 ++makefiles;
238 }
239
240 /* If there were no -f switches, try the default names. */
241
242 if (num_makefiles == 0)
243 {
244 static char *default_makefiles[] =
245#ifdef VMS
246 /* all lower case since readdir() (the vms version) 'lowercasifies' */
247# ifdef KMK
248 { "makefile.kmk", "makefile.vms", "gnumakefile.", "makefile.", 0 };
249# else
250 { "makefile.vms", "gnumakefile.", "makefile.", 0 };
251# endif
252#else
253#ifdef _AMIGA
254 /* what's the deal here? no dots? */
255# ifdef KMK
256 { "Makefile.kmk", "makefile.kmk", "GNUmakefile", "Makefile", "SMakefile", 0 };
257# else
258 { "GNUmakefile", "Makefile", "SMakefile", 0 };
259# endif
260#else /* !Amiga && !VMS */
261# ifdef KMK
262 { "Makefile.kmk", "makefile.kmk", "GNUmakefile", "makefile", "Makefile", 0 };
263# else
264 { "GNUmakefile", "makefile", "Makefile", 0 };
265# endif
266#endif /* AMIGA */
267#endif /* VMS */
268 register char **p = default_makefiles;
269 while (*p != 0 && !file_exists_p (*p))
270 ++p;
271
272 if (*p != 0)
273 {
274 if (! eval_makefile (*p, 0))
275 perror_with_name ("", *p);
276 }
277 else
278 {
279 /* No default makefile was found. Add the default makefiles to the
280 `read_makefiles' chain so they will be updated if possible. */
281 struct dep *tail = read_makefiles;
282 /* Add them to the tail, after any MAKEFILES variable makefiles. */
283 while (tail != 0 && tail->next != 0)
284 tail = tail->next;
285 for (p = default_makefiles; *p != 0; ++p)
286 {
287 struct dep *d = alloc_dep ();
288 d->file = enter_file (strcache_add (*p));
289 d->file->dontcare = 1;
290 /* Tell update_goal_chain to bail out as soon as this file is
291 made, and main not to die if we can't make this file. */
292 d->changed = RM_DONTCARE;
293 if (tail == 0)
294 read_makefiles = d;
295 else
296 tail->next = d;
297 tail = d;
298 }
299 if (tail != 0)
300 tail->next = 0;
301 }
302 }
303
304 return read_makefiles;
305}
306
307
308/* Install a new conditional and return the previous one. */
309
310static struct conditionals *
311install_conditionals (struct conditionals *new)
312{
313 struct conditionals *save = conditionals;
314
315 memset (new, '\0', sizeof (*new));
316 conditionals = new;
317
318 return save;
319}
320
321/* Free the current conditionals and reinstate a saved one. */
322
323static void
324restore_conditionals (struct conditionals *saved)
325{
326 /* Free any space allocated by conditional_line. */
327 if (conditionals->ignoring)
328 free (conditionals->ignoring);
329 if (conditionals->seen_else)
330 free (conditionals->seen_else);
331
332 /* Restore state. */
333 conditionals = saved;
334}
335
336
337static int
338eval_makefile (const char *filename, int flags)
339{
340 struct dep *deps;
341 struct ebuffer ebuf;
342 const struct floc *curfile;
343 char *expanded = 0;
344 int makefile_errno;
345 int r;
346
347 filename = strcache_add (filename);
348 ebuf.floc.filenm = filename;
349 ebuf.floc.lineno = 1;
350
351 if (ISDB (DB_VERBOSE))
352 {
353 printf (_("Reading makefile `%s'"), filename);
354 if (flags & RM_NO_DEFAULT_GOAL)
355 printf (_(" (no default goal)"));
356 if (flags & RM_INCLUDED)
357 printf (_(" (search path)"));
358 if (flags & RM_DONTCARE)
359 printf (_(" (don't care)"));
360 if (flags & RM_NO_TILDE)
361 printf (_(" (no ~ expansion)"));
362 puts ("...");
363 }
364
365 /* First, get a stream to read. */
366
367 /* Expand ~ in FILENAME unless it came from `include',
368 in which case it was already done. */
369 if (!(flags & RM_NO_TILDE) && filename[0] == '~')
370 {
371 expanded = tilde_expand (filename);
372 if (expanded != 0)
373 filename = expanded;
374 }
375
376 ebuf.fp = fopen (filename, "r");
377 /* Save the error code so we print the right message later. */
378 makefile_errno = errno;
379
380 /* If the makefile wasn't found and it's either a makefile from
381 the `MAKEFILES' variable or an included makefile,
382 search the included makefile search path for this makefile. */
383 if (ebuf.fp == 0 && (flags & RM_INCLUDED) && *filename != '/')
384 {
385 unsigned int i;
386 for (i = 0; include_directories[i] != 0; ++i)
387 {
388 const char *included = concat (include_directories[i], "/", filename);
389 ebuf.fp = fopen (included, "r");
390 if (ebuf.fp)
391 {
392 filename = strcache_add (included);
393 break;
394 }
395 }
396 }
397
398 /* Add FILENAME to the chain of read makefiles. */
399 deps = alloc_dep ();
400 deps->next = read_makefiles;
401 read_makefiles = deps;
402 deps->file = lookup_file (filename);
403 if (deps->file == 0)
404 deps->file = enter_file (filename);
405 filename = deps->file->name;
406 deps->changed = flags;
407 if (flags & RM_DONTCARE)
408 deps->file->dontcare = 1;
409
410 if (expanded)
411 free (expanded);
412
413 /* If the makefile can't be found at all, give up entirely. */
414
415 if (ebuf.fp == 0)
416 {
417 /* If we did some searching, errno has the error from the last
418 attempt, rather from FILENAME itself. Restore it in case the
419 caller wants to use it in a message. */
420 errno = makefile_errno;
421 return 0;
422 }
423
424 /* Add this makefile to the list. */
425 do_variable_definition (&ebuf.floc, "MAKEFILE_LIST", filename, o_file,
426 f_append, 0);
427
428#ifdef KMK
429 /* Buffer the entire file or at least 256KB (footer.kmk) of it. */
430 {
431 void *stream_buf = NULL;
432 struct stat st;
433 if (!fstat (fileno (ebuf.fp), &st))
434 {
435 int stream_buf_size = 256*1024;
436 if (st.st_size < stream_buf_size)
437 stream_buf_size = (st.st_size + 0xfff) & ~0xfff;
438 stream_buf = xmalloc (stream_buf_size);
439 setvbuf (ebuf.fp, stream_buf, _IOFBF, stream_buf_size);
440 }
441#endif
442
443 /* Evaluate the makefile */
444
445 ebuf.size = 200;
446 ebuf.buffer = ebuf.bufnext = ebuf.bufstart = xmalloc (ebuf.size);
447#ifdef CONFIG_WITH_VALUE_LENGTH
448 ebuf.eol = NULL;
449#endif
450
451 curfile = reading_file;
452 reading_file = &ebuf.floc;
453
454 r = eval (&ebuf, !(flags & RM_NO_DEFAULT_GOAL));
455
456 reading_file = curfile;
457
458 fclose (ebuf.fp);
459
460#ifdef KMK
461 if (stream_buf)
462 free (stream_buf);
463 }
464#endif
465 free (ebuf.bufstart);
466 alloca (0);
467 return r;
468}
469
470int
471eval_buffer (char *buffer)
472{
473 struct ebuffer ebuf;
474 struct conditionals *saved;
475 struct conditionals new;
476 const struct floc *curfile;
477 int r;
478
479 /* Evaluate the buffer */
480
481 ebuf.size = strlen (buffer);
482 ebuf.buffer = ebuf.bufnext = ebuf.bufstart = buffer;
483 ebuf.fp = NULL;
484#ifdef CONFIG_WITH_VALUE_LENGTH
485 ebuf.eol = ebuf.buffer + ebuf.size;
486#endif
487
488 ebuf.floc = *reading_file;
489
490 curfile = reading_file;
491 reading_file = &ebuf.floc;
492
493 saved = install_conditionals (&new);
494
495 r = eval (&ebuf, 1);
496
497 restore_conditionals (saved);
498
499 reading_file = curfile;
500
501 alloca (0);
502 return r;
503}
504
505
506
507/* Read file FILENAME as a makefile and add its contents to the data base.
508
509 SET_DEFAULT is true if we are allowed to set the default goal. */
510
511
512static int
513eval (struct ebuffer *ebuf, int set_default)
514{
515 char *collapsed = 0;
516 unsigned int collapsed_length = 0;
517 unsigned int commands_len = 200;
518 char *commands;
519 unsigned int commands_idx = 0;
520 unsigned int cmds_started, tgts_started;
521 int ignoring = 0, in_ignored_define = 0;
522 int no_targets = 0; /* Set when reading a rule without targets. */
523 struct nameseq *filenames = 0;
524 struct dep *deps = 0;
525 long nlines = 0;
526 int two_colon = 0;
527 const char *pattern = 0;
528 const char *pattern_percent;
529 struct floc *fstart;
530 struct floc fi;
531
532#define record_waiting_files() \
533 do \
534 { \
535 if (filenames != 0) \
536 { \
537 fi.lineno = tgts_started; \
538 record_files (filenames, pattern, pattern_percent, deps, \
539 cmds_started, commands, commands_idx, two_colon, \
540 &fi); \
541 } \
542 filenames = 0; \
543 commands_idx = 0; \
544 no_targets = 0; \
545 pattern = 0; \
546 } while (0)
547
548 pattern_percent = 0;
549 cmds_started = tgts_started = 1;
550
551 fstart = &ebuf->floc;
552 fi.filenm = ebuf->floc.filenm;
553
554 /* Loop over lines in the file.
555 The strategy is to accumulate target names in FILENAMES, dependencies
556 in DEPS and commands in COMMANDS. These are used to define a rule
557 when the start of the next rule (or eof) is encountered.
558
559 When you see a "continue" in the loop below, that means we are moving on
560 to the next line _without_ ending any rule that we happen to be working
561 with at the moment. If you see a "goto rule_complete", then the
562 statement we just parsed also finishes the previous rule. */
563
564 commands = xmalloc (200);
565
566 while (1)
567 {
568 unsigned int linelen;
569#ifdef CONFIG_WITH_VALUE_LENGTH
570 char *eol;
571#endif
572 char *line;
573 unsigned int wlen;
574 char *p;
575 char *p2;
576
577 /* Grab the next line to be evaluated */
578 ebuf->floc.lineno += nlines;
579 nlines = readline (ebuf);
580
581 /* If there is nothing left to eval, we're done. */
582 if (nlines < 0)
583 break;
584
585 /* If this line is empty, skip it. */
586 line = ebuf->buffer;
587 if (line[0] == '\0')
588 continue;
589
590#ifndef CONFIG_WITH_VALUE_LENGTH
591 linelen = strlen (line);
592#else
593 linelen = ebuf->eol - line;
594 assert (strlen (line) == linelen);
595#endif
596
597 /* Check for a shell command line first.
598 If it is not one, we can stop treating tab specially. */
599 if (line[0] == cmd_prefix)
600 {
601 if (no_targets)
602 /* Ignore the commands in a rule with no targets. */
603 continue;
604
605 /* If there is no preceding rule line, don't treat this line
606 as a command, even though it begins with a tab character.
607 SunOS 4 make appears to behave this way. */
608
609 if (filenames != 0)
610 {
611 if (ignoring)
612 /* Yep, this is a shell command, and we don't care. */
613 continue;
614
615 /* Append this command line to the line being accumulated. */
616 if (commands_idx == 0)
617 cmds_started = ebuf->floc.lineno;
618
619 if (linelen + 1 + commands_idx > commands_len)
620 {
621 commands_len = (linelen + 1 + commands_idx) * 2;
622 commands = xrealloc (commands, commands_len);
623 }
624 memcpy (&commands[commands_idx], line, linelen);
625 commands_idx += linelen;
626 commands[commands_idx++] = '\n';
627
628 continue;
629 }
630 }
631
632 /* This line is not a shell command line. Don't worry about tabs.
633 Get more space if we need it; we don't need to preserve the current
634 contents of the buffer. */
635
636 if (collapsed_length < linelen+1)
637 {
638 collapsed_length = linelen+1;
639 if (collapsed)
640 free (collapsed);
641 collapsed = xmalloc (collapsed_length);
642 }
643#ifndef CONFIG_WITH_VALUE_LENGTH
644 strcpy (collapsed, line);
645 /* Collapse continuation lines. */
646 collapse_continuations (collapsed);
647 remove_comments (collapsed);
648#else
649 memcpy (collapsed, line, linelen + 1);
650 /* Collapse continuation lines. */
651 eol = collapse_continuations (collapsed, linelen);
652 assert (strchr (collapsed, '\0') == eol);
653 eol = remove_comments (collapsed, eol);
654 assert (strchr (collapsed, '\0') == eol);
655#endif
656
657 /* Compare a word, both length and contents. */
658#define word1eq(s) (wlen == sizeof(s)-1 && strneq (s, p, sizeof(s)-1))
659 p = collapsed;
660 while (isspace ((unsigned char)*p))
661 ++p;
662
663 if (*p == '\0')
664 /* This line is completely empty--ignore it. */
665 continue;
666
667 /* Find the end of the first token. Note we don't need to worry about
668 * ":" here since we compare tokens by length (so "export" will never
669 * be equal to "export:").
670 */
671 for (p2 = p+1; *p2 != '\0' && !isspace ((unsigned char)*p2); ++p2)
672 ;
673 wlen = p2 - p;
674
675 /* Find the start of the second token. If it looks like a target or
676 variable definition it can't be a preprocessor token so skip
677 them--this allows variables/targets named `ifdef', `export', etc. */
678 while (isspace ((unsigned char)*p2))
679 ++p2;
680
681 if ((p2[0] == ':' || p2[0] == '+' || p2[0] == '=') && p2[1] == '\0')
682 {
683 /* It can't be a preprocessor token so skip it if we're ignoring */
684 if (ignoring)
685 continue;
686
687 goto skip_conditionals;
688 }
689
690 /* We must first check for conditional and `define' directives before
691 ignoring anything, since they control what we will do with
692 following lines. */
693
694 if (!in_ignored_define)
695 {
696#ifndef CONFIG_WITH_VALUE_LENGTH
697 int i = conditional_line (p, wlen, fstart);
698#else
699 int i = conditional_line (p, eol, wlen, fstart);
700#endif
701 if (i != -2)
702 {
703 if (i == -1)
704 fatal (fstart, _("invalid syntax in conditional"));
705
706 ignoring = i;
707 continue;
708 }
709 }
710
711 if (word1eq ("endef"))
712 {
713 if (!in_ignored_define)
714 fatal (fstart, _("extraneous `endef'"));
715 in_ignored_define = 0;
716 continue;
717 }
718
719 if (word1eq ("define"))
720 {
721 if (ignoring)
722 in_ignored_define = 1;
723 else
724 {
725 if (*p2 == '\0')
726 fatal (fstart, _("empty variable name"));
727
728 /* Let the variable name be the whole rest of the line,
729 with trailing blanks stripped (comments have already been
730 removed), so it could be a complex variable/function
731 reference that might contain blanks. */
732 p = strchr (p2, '\0');
733 while (isblank ((unsigned char)p[-1]))
734 --p;
735 do_define (p2, p - p2, o_file, ebuf);
736 }
737 continue;
738 }
739
740 if (word1eq ("override"))
741 {
742 if (*p2 == '\0')
743 error (fstart, _("empty `override' directive"));
744
745 if (strneq (p2, "define", 6)
746 && (isblank ((unsigned char)p2[6]) || p2[6] == '\0'))
747 {
748 if (ignoring)
749 in_ignored_define = 1;
750 else
751 {
752 p2 = next_token (p2 + 6);
753 if (*p2 == '\0')
754 fatal (fstart, _("empty variable name"));
755
756 /* Let the variable name be the whole rest of the line,
757 with trailing blanks stripped (comments have already been
758 removed), so it could be a complex variable/function
759 reference that might contain blanks. */
760 p = strchr (p2, '\0');
761 while (isblank ((unsigned char)p[-1]))
762 --p;
763 do_define (p2, p - p2, o_override, ebuf);
764 }
765 }
766 else if (!ignoring
767#ifndef CONFIG_WITH_VALUE_LENGTH
768 && !try_variable_definition (fstart, p2, o_override, 0))
769#else
770 && !try_variable_definition (fstart, p2, eol, o_override, 0))
771#endif
772 error (fstart, _("invalid `override' directive"));
773
774 continue;
775 }
776#ifdef CONFIG_WITH_LOCAL_VARIABLES
777
778 if (word1eq ("local"))
779 {
780 if (*p2 == '\0')
781 error (fstart, _("empty `local' directive"));
782
783 if (strneq (p2, "define", 6)
784 && (isblank ((unsigned char)p2[6]) || p2[6] == '\0'))
785 {
786 if (ignoring)
787 in_ignored_define = 1;
788 else
789 {
790 p2 = next_token (p2 + 6);
791 if (*p2 == '\0')
792 fatal (fstart, _("empty variable name"));
793
794 /* Let the variable name be the whole rest of the line,
795 with trailing blanks stripped (comments have already been
796 removed), so it could be a complex variable/function
797 reference that might contain blanks. */
798 p = strchr (p2, '\0');
799 while (isblank ((unsigned char)p[-1]))
800 --p;
801 do_define (p2, p - p2, o_local, ebuf);
802 }
803 }
804 else if (!ignoring
805# ifndef CONFIG_WITH_VALUE_LENGTH
806 && !try_variable_definition (fstart, p2, o_local, 0))
807# else
808 && !try_variable_definition (fstart, p2, eol, o_local, 0))
809# endif
810 error (fstart, _("invalid `local' directive"));
811
812 continue;
813 }
814#endif /* CONFIG_WITH_LOCAL_VARIABLES */
815
816 if (ignoring)
817 /* Ignore the line. We continue here so conditionals
818 can appear in the middle of a rule. */
819 continue;
820
821 if (word1eq ("export"))
822 {
823 /* 'export' by itself causes everything to be exported. */
824 if (*p2 == '\0')
825 export_all_variables = 1;
826 else
827 {
828 struct variable *v;
829
830#ifndef CONFIG_WITH_VALUE_LENGTH
831 v = try_variable_definition (fstart, p2, o_file, 0);
832#else
833 v = try_variable_definition (fstart, p2, eol, o_file, 0);
834#endif
835 if (v != 0)
836 v->export = v_export;
837 else
838 {
839 unsigned int l;
840 const char *cp;
841 char *ap;
842
843 /* Expand the line so we can use indirect and constructed
844 variable names in an export command. */
845#ifndef CONFIG_WITH_VALUE_LENGTH
846 cp = ap = allocated_variable_expand (p2);
847#else
848 cp = ap = allocated_variable_expand_2 (p2, eol - p2, NULL);
849#endif
850
851 for (p = find_next_token (&cp, &l); p != 0;
852 p = find_next_token (&cp, &l))
853 {
854 v = lookup_variable (p, l);
855 if (v == 0)
856 v = define_variable_loc (p, l, "", o_file, 0, fstart);
857 v->export = v_export;
858 }
859
860 free (ap);
861 }
862 }
863 goto rule_complete;
864 }
865
866 if (word1eq ("unexport"))
867 {
868 if (*p2 == '\0')
869 export_all_variables = 0;
870 else
871 {
872 unsigned int l;
873 struct variable *v;
874 const char *cp;
875 char *ap;
876
877 /* Expand the line so we can use indirect and constructed
878 variable names in an unexport command. */
879#ifndef CONFIG_WITH_VALUE_LENGTH
880 cp = ap = allocated_variable_expand (p2);
881#else
882 cp = ap = allocated_variable_expand_2 (p2, eol - p2, NULL);
883#endif
884
885 for (p = find_next_token (&cp, &l); p != 0;
886 p = find_next_token (&cp, &l))
887 {
888 v = lookup_variable (p, l);
889 if (v == 0)
890 v = define_variable_loc (p, l, "", o_file, 0, fstart);
891
892 v->export = v_noexport;
893 }
894
895 free (ap);
896 }
897 goto rule_complete;
898 }
899
900 skip_conditionals:
901 if (word1eq ("vpath"))
902 {
903 const char *cp;
904 char *vpat;
905 unsigned int l;
906 cp = variable_expand (p2);
907 p = find_next_token (&cp, &l);
908 if (p != 0)
909 {
910 vpat = savestring (p, l);
911 p = find_next_token (&cp, &l);
912 /* No searchpath means remove all previous
913 selective VPATH's with the same pattern. */
914 }
915 else
916 /* No pattern means remove all previous selective VPATH's. */
917 vpat = 0;
918 construct_vpath_list (vpat, p);
919 if (vpat != 0)
920 free (vpat);
921
922 goto rule_complete;
923 }
924
925#ifdef CONFIG_WITH_INCLUDEDEP
926 assert (strchr (p2, '\0') == eol);
927 if (word1eq ("includedep") || word1eq ("includedep-queue") || word1eq ("includedep-flush"))
928 {
929 /* We have found an `includedep' line specifying one or more dep files
930 to be read at this point. This include variation does no
931 globbing and do not support multiple names. It's trying to save
932 time by being dead simple as well as ignoring errors. */
933 enum incdep_op op = p[wlen - 1] == 'p'
934 ? incdep_read_it
935 : p[wlen - 1] == 'e'
936 ? incdep_queue : incdep_flush;
937 char *free_me = NULL;
938 char *name = p2;
939
940 if (memchr (name, '$', eol - name))
941 {
942 unsigned int name_len;
943 free_me = name = allocated_variable_expand_2 (name, eol - name, &name_len);
944 eol = name + name_len;
945 while (isspace ((unsigned char)*name))
946 ++name;
947 }
948
949 while (eol > name && isspace ((unsigned char)eol[-1]))
950 --eol;
951
952 *eol = '\0';
953 eval_include_dep (name, fstart, op);
954
955 if (free_me)
956 free (free_me);
957 goto rule_complete;
958 }
959#endif /* CONFIG_WITH_INCLUDEDEP */
960
961 if (word1eq ("include") || word1eq ("-include") || word1eq ("sinclude"))
962 {
963 /* We have found an `include' line specifying a nested
964 makefile to be read at this point. */
965 struct conditionals *save;
966 struct conditionals new_conditionals;
967 struct nameseq *files;
968 /* "-include" (vs "include") says no error if the file does not
969 exist. "sinclude" is an alias for this from SGI. */
970 int noerror = (p[0] != 'i');
971
972#ifndef CONFIG_WITH_VALUE_LENGTH
973 p = allocated_variable_expand (p2);
974#else
975 p = allocated_variable_expand_2 (p2, eol - p2, NULL);
976#endif
977
978 /* If no filenames, it's a no-op. */
979 if (*p == '\0')
980 {
981 free (p);
982 continue;
983 }
984
985 /* Parse the list of file names. */
986 p2 = p;
987 files = multi_glob (parse_file_seq (&p2, '\0',
988 sizeof (struct nameseq),
989 1),
990 sizeof (struct nameseq));
991 free (p);
992
993 /* Save the state of conditionals and start
994 the included makefile with a clean slate. */
995 save = install_conditionals (&new_conditionals);
996
997 /* Record the rules that are waiting so they will determine
998 the default goal before those in the included makefile. */
999 record_waiting_files ();
1000
1001 /* Read each included makefile. */
1002 while (files != 0)
1003 {
1004 struct nameseq *next = files->next;
1005 const char *name = files->name;
1006 int r;
1007
1008 free (files);
1009 files = next;
1010
1011 r = eval_makefile (name, (RM_INCLUDED | RM_NO_TILDE
1012 | (noerror ? RM_DONTCARE : 0)));
1013 if (!r && !noerror)
1014 error (fstart, "%s: %s", name, strerror (errno));
1015 }
1016
1017 /* Restore conditional state. */
1018 restore_conditionals (save);
1019
1020 goto rule_complete;
1021 }
1022
1023#ifndef CONFIG_WITH_VALUE_LENGTH
1024 if (try_variable_definition (fstart, p, o_file, 0))
1025#else
1026 if (try_variable_definition (fstart, p, eol, o_file, 0))
1027#endif
1028 /* This line has been dealt with. */
1029 goto rule_complete;
1030
1031 /* This line starts with a tab but was not caught above because there
1032 was no preceding target, and the line might have been usable as a
1033 variable definition. But now we know it is definitely lossage. */
1034 if (line[0] == cmd_prefix)
1035 fatal(fstart, _("commands commence before first target"));
1036
1037 /* This line describes some target files. This is complicated by
1038 the existence of target-specific variables, because we can't
1039 expand the entire line until we know if we have one or not. So
1040 we expand the line word by word until we find the first `:',
1041 then check to see if it's a target-specific variable.
1042
1043 In this algorithm, `lb_next' will point to the beginning of the
1044 unexpanded parts of the input buffer, while `p2' points to the
1045 parts of the expanded buffer we haven't searched yet. */
1046
1047 {
1048 enum make_word_type wtype;
1049 enum variable_origin v_origin;
1050 int exported;
1051 char *cmdleft, *semip, *lb_next;
1052 unsigned int plen = 0;
1053 char *colonp;
1054 const char *end, *beg; /* Helpers for whitespace stripping. */
1055
1056 /* Record the previous rule. */
1057
1058 record_waiting_files ();
1059 tgts_started = fstart->lineno;
1060
1061 /* Search the line for an unquoted ; that is not after an
1062 unquoted #. */
1063 cmdleft = find_char_unquote (line, ';', '#', 0, 1);
1064 if (cmdleft != 0 && *cmdleft == '#')
1065 {
1066 /* We found a comment before a semicolon. */
1067 *cmdleft = '\0';
1068 cmdleft = 0;
1069 }
1070 else if (cmdleft != 0)
1071 /* Found one. Cut the line short there before expanding it. */
1072 *(cmdleft++) = '\0';
1073 semip = cmdleft;
1074
1075#ifndef CONFIG_WITH_VALUE_LENGTH
1076 collapse_continuations (line);
1077#else
1078 collapse_continuations (line, strlen (line)); /**@todo fix this */
1079#endif
1080
1081 /* We can't expand the entire line, since if it's a per-target
1082 variable we don't want to expand it. So, walk from the
1083 beginning, expanding as we go, and looking for "interesting"
1084 chars. The first word is always expandable. */
1085 wtype = get_next_mword(line, NULL, &lb_next, &wlen);
1086 switch (wtype)
1087 {
1088 case w_eol:
1089 if (cmdleft != 0)
1090 fatal(fstart, _("missing rule before commands"));
1091 /* This line contained something but turned out to be nothing
1092 but whitespace (a comment?). */
1093 continue;
1094
1095 case w_colon:
1096 case w_dcolon:
1097 /* We accept and ignore rules without targets for
1098 compatibility with SunOS 4 make. */
1099 no_targets = 1;
1100 continue;
1101
1102 default:
1103 break;
1104 }
1105
1106 p2 = variable_expand_string(NULL, lb_next, wlen);
1107
1108 while (1)
1109 {
1110 lb_next += wlen;
1111 if (cmdleft == 0)
1112 {
1113 /* Look for a semicolon in the expanded line. */
1114 cmdleft = find_char_unquote (p2, ';', 0, 0, 0);
1115
1116 if (cmdleft != 0)
1117 {
1118 unsigned long p2_off = p2 - variable_buffer;
1119 unsigned long cmd_off = cmdleft - variable_buffer;
1120 char *pend = p2 + strlen(p2);
1121
1122 /* Append any remnants of lb, then cut the line short
1123 at the semicolon. */
1124 *cmdleft = '\0';
1125
1126 /* One school of thought says that you shouldn't expand
1127 here, but merely copy, since now you're beyond a ";"
1128 and into a command script. However, the old parser
1129 expanded the whole line, so we continue that for
1130 backwards-compatiblity. Also, it wouldn't be
1131 entirely consistent, since we do an unconditional
1132 expand below once we know we don't have a
1133 target-specific variable. */
1134 (void)variable_expand_string(pend, lb_next, (long)-1);
1135 lb_next += strlen(lb_next);
1136 p2 = variable_buffer + p2_off;
1137 cmdleft = variable_buffer + cmd_off + 1;
1138 }
1139 }
1140
1141 colonp = find_char_unquote(p2, ':', 0, 0, 0);
1142#ifdef HAVE_DOS_PATHS
1143 /* The drive spec brain-damage strikes again... */
1144 /* Note that the only separators of targets in this context
1145 are whitespace and a left paren. If others are possible,
1146 they should be added to the string in the call to index. */
1147 while (colonp && (colonp[1] == '/' || colonp[1] == '\\') &&
1148 colonp > p2 && isalpha ((unsigned char)colonp[-1]) &&
1149 (colonp == p2 + 1 || strchr (" \t(", colonp[-2]) != 0))
1150 colonp = find_char_unquote(colonp + 1, ':', 0, 0, 0);
1151#endif
1152 if (colonp != 0)
1153 break;
1154
1155 wtype = get_next_mword(lb_next, NULL, &lb_next, &wlen);
1156 if (wtype == w_eol)
1157 break;
1158
1159 p2 += strlen(p2);
1160 *(p2++) = ' ';
1161 p2 = variable_expand_string(p2, lb_next, wlen);
1162 /* We don't need to worry about cmdleft here, because if it was
1163 found in the variable_buffer the entire buffer has already
1164 been expanded... we'll never get here. */
1165 }
1166
1167 p2 = next_token (variable_buffer);
1168
1169 /* If the word we're looking at is EOL, see if there's _anything_
1170 on the line. If not, a variable expanded to nothing, so ignore
1171 it. If so, we can't parse this line so punt. */
1172 if (wtype == w_eol)
1173 {
1174 if (*p2 != '\0')
1175 /* There's no need to be ivory-tower about this: check for
1176 one of the most common bugs found in makefiles... */
1177 fatal (fstart, _("missing separator%s"),
1178 !strneq(line, " ", 8) ? ""
1179 : _(" (did you mean TAB instead of 8 spaces?)"));
1180 continue;
1181 }
1182
1183 /* Make the colon the end-of-string so we know where to stop
1184 looking for targets. */
1185 *colonp = '\0';
1186 filenames = multi_glob (parse_file_seq (&p2, '\0',
1187 sizeof (struct nameseq),
1188 1),
1189 sizeof (struct nameseq));
1190 *p2 = ':';
1191
1192 if (!filenames)
1193 {
1194 /* We accept and ignore rules without targets for
1195 compatibility with SunOS 4 make. */
1196 no_targets = 1;
1197 continue;
1198 }
1199 /* This should never be possible; we handled it above. */
1200 assert (*p2 != '\0');
1201 ++p2;
1202
1203 /* Is this a one-colon or two-colon entry? */
1204 two_colon = *p2 == ':';
1205 if (two_colon)
1206 p2++;
1207
1208 /* Test to see if it's a target-specific variable. Copy the rest
1209 of the buffer over, possibly temporarily (we'll expand it later
1210 if it's not a target-specific variable). PLEN saves the length
1211 of the unparsed section of p2, for later. */
1212 if (*lb_next != '\0')
1213 {
1214 unsigned int l = p2 - variable_buffer;
1215 plen = strlen (p2);
1216 variable_buffer_output (p2+plen, lb_next, strlen (lb_next)+1);
1217 p2 = variable_buffer + l;
1218 }
1219
1220 /* See if it's an "override" or "export" keyword; if so see if what
1221 comes after it looks like a variable definition. */
1222
1223 wtype = get_next_mword (p2, NULL, &p, &wlen);
1224
1225 v_origin = o_file;
1226 exported = 0;
1227 if (wtype == w_static)
1228 {
1229 if (word1eq ("override"))
1230 {
1231 v_origin = o_override;
1232 wtype = get_next_mword (p+wlen, NULL, &p, &wlen);
1233 }
1234 else if (word1eq ("export"))
1235 {
1236 exported = 1;
1237 wtype = get_next_mword (p+wlen, NULL, &p, &wlen);
1238 }
1239 }
1240
1241 if (wtype != w_eol)
1242 wtype = get_next_mword (p+wlen, NULL, NULL, NULL);
1243
1244 if (wtype == w_varassign)
1245 {
1246 /* If there was a semicolon found, add it back, plus anything
1247 after it. */
1248 if (semip)
1249 {
1250 unsigned int l = p - variable_buffer;
1251 *(--semip) = ';';
1252 variable_buffer_output (p2 + strlen (p2),
1253 semip, strlen (semip)+1);
1254 p = variable_buffer + l;
1255 }
1256 record_target_var (filenames, p, v_origin, exported, fstart);
1257 filenames = 0;
1258 continue;
1259 }
1260
1261 /* This is a normal target, _not_ a target-specific variable.
1262 Unquote any = in the dependency list. */
1263 find_char_unquote (lb_next, '=', 0, 0, 0);
1264
1265 /* We have some targets, so don't ignore the following commands. */
1266 no_targets = 0;
1267
1268 /* Expand the dependencies, etc. */
1269 if (*lb_next != '\0')
1270 {
1271 unsigned int l = p2 - variable_buffer;
1272 (void) variable_expand_string (p2 + plen, lb_next, (long)-1);
1273 p2 = variable_buffer + l;
1274
1275 /* Look for a semicolon in the expanded line. */
1276 if (cmdleft == 0)
1277 {
1278 cmdleft = find_char_unquote (p2, ';', 0, 0, 0);
1279 if (cmdleft != 0)
1280 *(cmdleft++) = '\0';
1281 }
1282 }
1283
1284 /* Is this a static pattern rule: `target: %targ: %dep; ...'? */
1285 p = strchr (p2, ':');
1286 while (p != 0 && p[-1] == '\\')
1287 {
1288 register char *q = &p[-1];
1289 register int backslash = 0;
1290 while (*q-- == '\\')
1291 backslash = !backslash;
1292 if (backslash)
1293 p = strchr (p + 1, ':');
1294 else
1295 break;
1296 }
1297#ifdef _AMIGA
1298 /* Here, the situation is quite complicated. Let's have a look
1299 at a couple of targets:
1300
1301 install: dev:make
1302
1303 dev:make: make
1304
1305 dev:make:: xyz
1306
1307 The rule is that it's only a target, if there are TWO :'s
1308 OR a space around the :.
1309 */
1310 if (p && !(isspace ((unsigned char)p[1]) || !p[1]
1311 || isspace ((unsigned char)p[-1])))
1312 p = 0;
1313#endif
1314#ifdef HAVE_DOS_PATHS
1315 {
1316 int check_again;
1317 do {
1318 check_again = 0;
1319 /* For DOS-style paths, skip a "C:\..." or a "C:/..." */
1320 if (p != 0 && (p[1] == '\\' || p[1] == '/') &&
1321 isalpha ((unsigned char)p[-1]) &&
1322 (p == p2 + 1 || strchr (" \t:(", p[-2]) != 0)) {
1323 p = strchr (p + 1, ':');
1324 check_again = 1;
1325 }
1326 } while (check_again);
1327 }
1328#endif
1329 if (p != 0)
1330 {
1331 struct nameseq *target;
1332 target = parse_file_seq (&p2, ':', sizeof (struct nameseq), 1);
1333 ++p2;
1334 if (target == 0)
1335 fatal (fstart, _("missing target pattern"));
1336 else if (target->next != 0)
1337 fatal (fstart, _("multiple target patterns (target `%s')"), target->name); /* bird */
1338 pattern_percent = find_percent_cached (&target->name);
1339 pattern = target->name;
1340 if (pattern_percent == 0)
1341 fatal (fstart, _("target pattern contains no `%%' (target `%s')"), target->name); /* bird */
1342 free (target);
1343 }
1344 else
1345 pattern = 0;
1346
1347 /* Strip leading and trailing whitespaces. */
1348 beg = p2;
1349 end = beg + strlen (beg) - 1;
1350 strip_whitespace (&beg, &end);
1351
1352 if (beg <= end && *beg != '\0')
1353 {
1354 /* Put all the prerequisites here; they'll be parsed later. */
1355 deps = alloc_dep ();
1356 deps->name = strcache_add_len (beg, end - beg + 1);
1357 }
1358 else
1359 deps = 0;
1360
1361 commands_idx = 0;
1362 if (cmdleft != 0)
1363 {
1364 /* Semicolon means rest of line is a command. */
1365 unsigned int l = strlen (cmdleft);
1366
1367 cmds_started = fstart->lineno;
1368
1369 /* Add this command line to the buffer. */
1370 if (l + 2 > commands_len)
1371 {
1372 commands_len = (l + 2) * 2;
1373 commands = xrealloc (commands, commands_len);
1374 }
1375 memcpy (commands, cmdleft, l);
1376 commands_idx += l;
1377 commands[commands_idx++] = '\n';
1378 }
1379
1380 /* Determine if this target should be made default. We used to do
1381 this in record_files() but because of the delayed target recording
1382 and because preprocessor directives are legal in target's commands
1383 it is too late. Consider this fragment for example:
1384
1385 foo:
1386
1387 ifeq ($(.DEFAULT_GOAL),foo)
1388 ...
1389 endif
1390
1391 Because the target is not recorded until after ifeq directive is
1392 evaluated the .DEFAULT_GOAL does not contain foo yet as one
1393 would expect. Because of this we have to move some of the logic
1394 here. */
1395
1396 if (**default_goal_name == '\0' && set_default)
1397 {
1398 const char *name;
1399 struct dep *d;
1400 struct nameseq *t = filenames;
1401
1402 for (; t != 0; t = t->next)
1403 {
1404 int reject = 0;
1405 name = t->name;
1406
1407 /* We have nothing to do if this is an implicit rule. */
1408 if (strchr (name, '%') != 0)
1409 break;
1410
1411 /* See if this target's name does not start with a `.',
1412 unless it contains a slash. */
1413 if (*name == '.' && strchr (name, '/') == 0
1414#ifdef HAVE_DOS_PATHS
1415 && strchr (name, '\\') == 0
1416#endif
1417 )
1418 continue;
1419
1420
1421 /* If this file is a suffix, don't let it be
1422 the default goal file. */
1423 for (d = suffix_file->deps; d != 0; d = d->next)
1424 {
1425 register struct dep *d2;
1426 if (*dep_name (d) != '.' && streq (name, dep_name (d)))
1427 {
1428 reject = 1;
1429 break;
1430 }
1431 for (d2 = suffix_file->deps; d2 != 0; d2 = d2->next)
1432 {
1433 unsigned int l = strlen (dep_name (d2));
1434 if (!strneq (name, dep_name (d2), l))
1435 continue;
1436 if (streq (name + l, dep_name (d)))
1437 {
1438 reject = 1;
1439 break;
1440 }
1441 }
1442
1443 if (reject)
1444 break;
1445 }
1446
1447 if (!reject)
1448 {
1449 define_variable_global (".DEFAULT_GOAL", 13, t->name,
1450 o_file, 0, NILF);
1451 break;
1452 }
1453 }
1454 }
1455
1456 continue;
1457 }
1458
1459 /* We get here except in the case that we just read a rule line.
1460 Record now the last rule we read, so following spurious
1461 commands are properly diagnosed. */
1462 rule_complete:
1463 record_waiting_files ();
1464 }
1465
1466#undef word1eq
1467
1468 if (conditionals->if_cmds)
1469 fatal (fstart, _("missing `endif'"));
1470
1471 /* At eof, record the last rule. */
1472 record_waiting_files ();
1473
1474 if (collapsed)
1475 free (collapsed);
1476 free (commands);
1477
1478 return 1;
1479}
1480
1481
1482
1483/* Remove comments from LINE.
1484 This is done by copying the text at LINE onto itself. */
1485
1486#ifndef CONFIG_WITH_VALUE_LENGTH
1487static void
1488remove_comments (char *line)
1489#else
1490static char *
1491remove_comments (char *line, char *eol)
1492#endif
1493{
1494 char *comment;
1495
1496 comment = find_char_unquote (line, '#', 0, 0, 0);
1497
1498 if (comment != 0)
1499 /* Cut off the line at the #. */
1500 *comment = '\0';
1501
1502#ifdef CONFIG_WITH_VALUE_LENGTH
1503 if (comment)
1504 eol = comment;
1505 assert (strchr (line, '\0') == eol);
1506 return eol;
1507#endif
1508}
1509
1510/* Execute a `define' directive.
1511 The first line has already been read, and NAME is the name of
1512 the variable to be defined. The following lines remain to be read. */
1513
1514static void
1515do_define (char *name, unsigned int namelen,
1516 enum variable_origin origin, struct ebuffer *ebuf)
1517{
1518 struct floc defstart;
1519 long nlines = 0;
1520 int nlevels = 1;
1521 unsigned int length = 100;
1522 char *definition = xmalloc (length);
1523 unsigned int idx = 0;
1524 char *p;
1525
1526 /* Expand the variable name. */
1527 char *var = alloca (namelen + 1);
1528 memcpy (var, name, namelen);
1529 var[namelen] = '\0';
1530 var = variable_expand (var);
1531
1532 defstart = ebuf->floc;
1533
1534 while (1)
1535 {
1536 unsigned int len;
1537 char *line;
1538
1539 nlines = readline (ebuf);
1540 ebuf->floc.lineno += nlines;
1541
1542 /* If there is nothing left to eval, we're done. */
1543 if (nlines < 0)
1544 break;
1545
1546 line = ebuf->buffer;
1547
1548#ifndef CONFIG_WITH_VALUE_LENGTH
1549 collapse_continuations (line);
1550#else
1551 ebuf->eol = collapse_continuations (line, ebuf->eol - line);
1552#endif
1553
1554 /* If the line doesn't begin with a tab, test to see if it introduces
1555 another define, or ends one. */
1556
1557 /* Stop if we find an 'endef' */
1558 if (line[0] != cmd_prefix)
1559 {
1560 p = next_token (line);
1561#ifndef CONFIG_WITH_VALUE_LENGTH
1562 len = strlen (p);
1563#else
1564 len = ebuf->eol - p;
1565 assert (len == strlen (p));
1566#endif
1567
1568 /* If this is another 'define', increment the level count. */
1569 if ((len == 6 || (len > 6 && isblank ((unsigned char)p[6])))
1570 && strneq (p, "define", 6))
1571 ++nlevels;
1572
1573 /* If this is an 'endef', decrement the count. If it's now 0,
1574 we've found the last one. */
1575 else if ((len == 5 || (len > 5 && isblank ((unsigned char)p[5])))
1576 && strneq (p, "endef", 5))
1577 {
1578 p += 5;
1579#ifndef CONFIG_WITH_VALUE_LENGTH
1580 remove_comments (p);
1581#else
1582 ebuf->eol = remove_comments (p, ebuf->eol);
1583#endif
1584 if (*next_token (p) != '\0')
1585 error (&ebuf->floc,
1586 _("Extraneous text after `endef' directive"));
1587
1588 if (--nlevels == 0)
1589 {
1590 /* Define the variable. */
1591 if (idx == 0)
1592 definition[0] = '\0';
1593 else
1594 definition[idx - 1] = '\0';
1595
1596 /* Always define these variables in the global set. */
1597 define_variable_global (var, strlen (var), definition,
1598 origin, 1, &defstart);
1599 free (definition);
1600 return;
1601 }
1602 }
1603 }
1604
1605 /* Otherwise add this line to the variable definition. */
1606#ifndef CONFIG_WITH_VALUE_LENGTH
1607 len = strlen (line);
1608#else
1609 len = ebuf->eol - line;
1610 assert (len == strlen (line));
1611#endif
1612 if (idx + len + 1 > length)
1613 {
1614 length = (idx + len) * 2;
1615 definition = xrealloc (definition, length + 1);
1616 }
1617
1618 memcpy (&definition[idx], line, len);
1619 idx += len;
1620 /* Separate lines with a newline. */
1621 definition[idx++] = '\n';
1622 }
1623
1624 /* No `endef'!! */
1625 fatal (&defstart, _("missing `endef', unterminated `define'"));
1626
1627 /* NOTREACHED */
1628 return;
1629}
1630
1631
1632/* Interpret conditional commands "ifdef", "ifndef", "ifeq",
1633 "ifneq", "if1of", "ifn1of", "else" and "endif".
1634 LINE is the input line, with the command as its first word.
1635
1636 FILENAME and LINENO are the filename and line number in the
1637 current makefile. They are used for error messages.
1638
1639 Value is -2 if the line is not a conditional at all,
1640 -1 if the line is an invalid conditional,
1641 0 if following text should be interpreted,
1642 1 if following text should be ignored. */
1643
1644static int
1645#ifndef CONFIG_WITH_VALUE_LENGTH
1646conditional_line (char *line, int len, const struct floc *flocp)
1647#else
1648conditional_line (char *line, char *eol, int len, const struct floc *flocp)
1649#endif
1650{
1651 char *cmdname;
1652 enum { c_ifdef, c_ifndef, c_ifeq, c_ifneq,
1653#ifdef CONFIG_WITH_SET_CONDITIONALS
1654 c_if1of, c_ifn1of,
1655#endif
1656#ifdef CONFIG_WITH_IF_CONDITIONALS
1657 c_ifcond,
1658#endif
1659 c_else, c_endif
1660 } cmdtype;
1661 unsigned int i;
1662 unsigned int o;
1663#ifdef CONFIG_WITH_VALUE_LENGTH
1664 assert (strchr (line, '\0') == eol);
1665#endif
1666
1667 /* Compare a word, both length and contents. */
1668#define word1eq(s) (len == sizeof(s)-1 && strneq (s, line, sizeof(s)-1))
1669#define chkword(s, t) if (word1eq (s)) { cmdtype = (t); cmdname = (s); }
1670
1671 /* Make sure this line is a conditional. */
1672 chkword ("ifdef", c_ifdef)
1673 else chkword ("ifndef", c_ifndef)
1674 else chkword ("ifeq", c_ifeq)
1675 else chkword ("ifneq", c_ifneq)
1676#ifdef CONFIG_WITH_SET_CONDITIONALS
1677 else chkword ("if1of", c_if1of)
1678 else chkword ("ifn1of", c_ifn1of)
1679#endif
1680#ifdef CONFIG_WITH_IF_CONDITIONALS
1681 else chkword ("if", c_ifcond)
1682#endif
1683 else chkword ("else", c_else)
1684 else chkword ("endif", c_endif)
1685 else
1686 return -2;
1687
1688 /* Found one: skip past it and any whitespace after it. */
1689 line = next_token (line + len);
1690
1691#define EXTRANEOUS() error (flocp, _("Extraneous text after `%s' directive"), cmdname)
1692
1693 /* An 'endif' cannot contain extra text, and reduces the if-depth by 1 */
1694 if (cmdtype == c_endif)
1695 {
1696 if (*line != '\0')
1697 EXTRANEOUS ();
1698
1699 if (!conditionals->if_cmds)
1700 fatal (flocp, _("extraneous `%s'"), cmdname);
1701
1702 --conditionals->if_cmds;
1703
1704 goto DONE;
1705 }
1706
1707 /* An 'else' statement can either be simple, or it can have another
1708 conditional after it. */
1709 if (cmdtype == c_else)
1710 {
1711 const char *p;
1712
1713 if (!conditionals->if_cmds)
1714 fatal (flocp, _("extraneous `%s'"), cmdname);
1715
1716 o = conditionals->if_cmds - 1;
1717
1718 if (conditionals->seen_else[o])
1719 fatal (flocp, _("only one `else' per conditional"));
1720
1721 /* Change the state of ignorance. */
1722 switch (conditionals->ignoring[o])
1723 {
1724 case 0:
1725 /* We've just been interpreting. Never do it again. */
1726 conditionals->ignoring[o] = 2;
1727 break;
1728 case 1:
1729 /* We've never interpreted yet. Maybe this time! */
1730 conditionals->ignoring[o] = 0;
1731 break;
1732 }
1733
1734 /* It's a simple 'else'. */
1735 if (*line == '\0')
1736 {
1737 conditionals->seen_else[o] = 1;
1738 goto DONE;
1739 }
1740
1741 /* The 'else' has extra text. That text must be another conditional
1742 and cannot be an 'else' or 'endif'. */
1743
1744 /* Find the length of the next word. */
1745 for (p = line+1; *p != '\0' && !isspace ((unsigned char)*p); ++p)
1746 ;
1747 len = p - line;
1748
1749 /* If it's 'else' or 'endif' or an illegal conditional, fail. */
1750 if (word1eq("else") || word1eq("endif")
1751#ifndef CONFIG_WITH_VALUE_LENGTH
1752 || conditional_line (line, len, flocp) < 0)
1753#else
1754 || conditional_line (line, eol, len, flocp) < 0)
1755#endif
1756 EXTRANEOUS ();
1757 else
1758 {
1759 /* conditional_line() created a new level of conditional.
1760 Raise it back to this level. */
1761 if (conditionals->ignoring[o] < 2)
1762 conditionals->ignoring[o] = conditionals->ignoring[o+1];
1763 --conditionals->if_cmds;
1764 }
1765
1766 goto DONE;
1767 }
1768
1769 if (conditionals->allocated == 0)
1770 {
1771 conditionals->allocated = 5;
1772 conditionals->ignoring = xmalloc (conditionals->allocated);
1773 conditionals->seen_else = xmalloc (conditionals->allocated);
1774 }
1775
1776 o = conditionals->if_cmds++;
1777 if (conditionals->if_cmds > conditionals->allocated)
1778 {
1779 conditionals->allocated += 5;
1780 conditionals->ignoring = xrealloc (conditionals->ignoring,
1781 conditionals->allocated);
1782 conditionals->seen_else = xrealloc (conditionals->seen_else,
1783 conditionals->allocated);
1784 }
1785
1786 /* Record that we have seen an `if...' but no `else' so far. */
1787 conditionals->seen_else[o] = 0;
1788
1789 /* Search through the stack to see if we're already ignoring. */
1790 for (i = 0; i < o; ++i)
1791 if (conditionals->ignoring[i])
1792 {
1793 /* We are already ignoring, so just push a level to match the next
1794 "else" or "endif", and keep ignoring. We don't want to expand
1795 variables in the condition. */
1796 conditionals->ignoring[o] = 1;
1797 return 1;
1798 }
1799
1800 if (cmdtype == c_ifdef || cmdtype == c_ifndef)
1801 {
1802 char *var;
1803 struct variable *v;
1804 char *p;
1805
1806 /* Expand the thing we're looking up, so we can use indirect and
1807 constructed variable names. */
1808#ifndef CONFIG_WITH_VALUE_LENGTH
1809 var = allocated_variable_expand (line);
1810#else
1811 var = allocated_variable_expand_2 (line, eol - line, NULL);
1812#endif
1813
1814 /* Make sure there's only one variable name to test. */
1815 p = end_of_token (var);
1816 i = p - var;
1817 p = next_token (p);
1818 if (*p != '\0')
1819 return -1;
1820
1821 var[i] = '\0';
1822 v = lookup_variable (var, i);
1823
1824 conditionals->ignoring[o] =
1825 ((v != 0 && *v->value != '\0') == (cmdtype == c_ifndef));
1826
1827 free (var);
1828 }
1829#ifdef CONFIG_WITH_IF_CONDITIONALS
1830 else if (cmdtype == c_ifcond)
1831 {
1832 int rval = expr_eval_if_conditionals (line, flocp);
1833 if (rval == -1)
1834 return rval;
1835 conditionals->ignoring[o] = rval;
1836 }
1837#endif
1838 else
1839 {
1840#ifdef CONFIG_WITH_SET_CONDITIONALS
1841 /* "ifeq", "ifneq", "if1of" or "ifn1of". */
1842#else
1843 /* "ifeq" or "ifneq". */
1844#endif
1845 char *s1, *s2;
1846 unsigned int l;
1847 char termin = *line == '(' ? ',' : *line;
1848#ifdef CONFIG_WITH_VALUE_LENGTH
1849 char *buf_pos;
1850#endif
1851
1852 if (termin != ',' && termin != '"' && termin != '\'')
1853 return -1;
1854
1855 s1 = ++line;
1856 /* Find the end of the first string. */
1857 if (termin == ',')
1858 {
1859 int count = 0;
1860 for (; *line != '\0'; ++line)
1861 if (*line == '(')
1862 ++count;
1863 else if (*line == ')')
1864 --count;
1865 else if (*line == ',' && count <= 0)
1866 break;
1867 }
1868 else
1869 while (*line != '\0' && *line != termin)
1870 ++line;
1871
1872 if (*line == '\0')
1873 return -1;
1874
1875 if (termin == ',')
1876 {
1877 /* Strip blanks after the first string. */
1878 char *p = line++;
1879 while (isblank ((unsigned char)p[-1]))
1880 --p;
1881 *p = '\0';
1882#ifdef CONFIG_WITH_VALUE_LENGTH
1883 l = p - s1;
1884#endif
1885 }
1886 else
1887 {
1888#ifdef CONFIG_WITH_VALUE_LENGTH
1889 l = line - s1;
1890#endif
1891 *line++ = '\0';
1892 }
1893
1894#ifndef CONFIG_WITH_VALUE_LENGTH
1895 s2 = variable_expand (s1);
1896 /* We must allocate a new copy of the expanded string because
1897 variable_expand re-uses the same buffer. */
1898 l = strlen (s2);
1899 s1 = alloca (l + 1);
1900 memcpy (s1, s2, l + 1);
1901#else
1902 s1 = variable_expand_string_2 (NULL, s1, l, &buf_pos);
1903 ++buf_pos;
1904#endif
1905
1906 if (termin != ',')
1907 /* Find the start of the second string. */
1908 line = next_token (line);
1909
1910 termin = termin == ',' ? ')' : *line;
1911 if (termin != ')' && termin != '"' && termin != '\'')
1912 return -1;
1913
1914 /* Find the end of the second string. */
1915 if (termin == ')')
1916 {
1917 int count = 0;
1918 s2 = next_token (line);
1919 for (line = s2; *line != '\0'; ++line)
1920 {
1921 if (*line == '(')
1922 ++count;
1923 else if (*line == ')')
1924 {
1925 if (count <= 0)
1926 break;
1927 else
1928 --count;
1929 }
1930 }
1931 }
1932 else
1933 {
1934 ++line;
1935 s2 = line;
1936 while (*line != '\0' && *line != termin)
1937 ++line;
1938 }
1939
1940 if (*line == '\0')
1941 return -1;
1942
1943 *line = '\0';
1944#ifdef CONFIG_WITH_VALUE_LENGTH
1945 l = line - s2;
1946#endif
1947 line = next_token (++line);
1948 if (*line != '\0')
1949 EXTRANEOUS ();
1950
1951#ifndef CONFIG_WITH_VALUE_LENGTH
1952 s2 = variable_expand (s2);
1953#else
1954 if ((size_t)buf_pos & 7)
1955 buf_pos = variable_buffer_output (buf_pos, "\0\0\0\0\0\0\0\0",
1956 8 - (size_t)buf_pos & 7);
1957 s2 = variable_expand_string (buf_pos, s2, l);
1958#endif
1959#ifdef CONFIG_WITH_SET_CONDITIONALS
1960 if (cmdtype == c_if1of || cmdtype == c_ifn1of)
1961 {
1962 const char *s1_cur;
1963 unsigned int s1_len;
1964 const char *s1_iterator = s1;
1965
1966 conditionals->ignoring[o] = (cmdtype == c_if1of); /* if not found */
1967 while ((s1_cur = find_next_token (&s1_iterator, &s1_len)) != 0)
1968 {
1969 const char *s2_cur;
1970 unsigned int s2_len;
1971 const char *s2_iterator = s2;
1972 while ((s2_cur = find_next_token (&s2_iterator, &s2_len)) != 0)
1973 if (s2_len == s1_len
1974 && strneq (s2_cur, s1_cur, s1_len) )
1975 {
1976 conditionals->ignoring[o] = (cmdtype != c_if1of); /* found */
1977 break;
1978 }
1979 }
1980 }
1981 else
1982 conditionals->ignoring[o] = (streq (s1, s2) == (cmdtype == c_ifneq));
1983#else
1984 conditionals->ignoring[o] = (streq (s1, s2) == (cmdtype == c_ifneq));
1985#endif
1986 }
1987
1988 DONE:
1989 /* Search through the stack to see if we're ignoring. */
1990 for (i = 0; i < conditionals->if_cmds; ++i)
1991 if (conditionals->ignoring[i])
1992 return 1;
1993 return 0;
1994}
1995
1996
1997/* Remove duplicate dependencies in CHAIN. */
1998
1999static unsigned long
2000dep_hash_1 (const void *key)
2001{
2002 return_STRING_HASH_1 (dep_name ((struct dep const *) key));
2003}
2004
2005static unsigned long
2006dep_hash_2 (const void *key)
2007{
2008 return_STRING_HASH_2 (dep_name ((struct dep const *) key));
2009}
2010
2011static int
2012dep_hash_cmp (const void *x, const void *y)
2013{
2014 struct dep *dx = (struct dep *) x;
2015 struct dep *dy = (struct dep *) y;
2016 int cmp = strcmp (dep_name (dx), dep_name (dy));
2017
2018 /* If the names are the same but ignore_mtimes are not equal, one of these
2019 is an order-only prerequisite and one isn't. That means that we should
2020 remove the one that isn't and keep the one that is. */
2021
2022 if (!cmp && dx->ignore_mtime != dy->ignore_mtime)
2023 dx->ignore_mtime = dy->ignore_mtime = 0;
2024
2025 return cmp;
2026}
2027
2028
2029void
2030uniquize_deps (struct dep *chain)
2031{
2032 struct hash_table deps;
2033 register struct dep **depp;
2034
2035 hash_init (&deps, 500, dep_hash_1, dep_hash_2, dep_hash_cmp);
2036
2037 /* Make sure that no dependencies are repeated. This does not
2038 really matter for the purpose of updating targets, but it
2039 might make some names be listed twice for $^ and $?. */
2040
2041 depp = &chain;
2042 while (*depp)
2043 {
2044 struct dep *dep = *depp;
2045 struct dep **dep_slot = (struct dep **) hash_find_slot (&deps, dep);
2046 if (HASH_VACANT (*dep_slot))
2047 {
2048 hash_insert_at (&deps, dep, dep_slot);
2049 depp = &dep->next;
2050 }
2051 else
2052 {
2053 /* Don't bother freeing duplicates.
2054 It's dangerous and little benefit accrues. */
2055 *depp = dep->next;
2056 }
2057 }
2058
2059 hash_free (&deps, 0);
2060}
2061
2062
2063/* Record target-specific variable values for files FILENAMES.
2064 TWO_COLON is nonzero if a double colon was used.
2065
2066 The links of FILENAMES are freed, and so are any names in it
2067 that are not incorporated into other data structures.
2068
2069 If the target is a pattern, add the variable to the pattern-specific
2070 variable value list. */
2071
2072static void
2073record_target_var (struct nameseq *filenames, char *defn,
2074 enum variable_origin origin, int exported,
2075 const struct floc *flocp)
2076{
2077 struct nameseq *nextf;
2078 struct variable_set_list *global;
2079
2080 global = current_variable_set_list;
2081
2082 /* If the variable is an append version, store that but treat it as a
2083 normal recursive variable. */
2084
2085 for (; filenames != 0; filenames = nextf)
2086 {
2087 struct variable *v;
2088 const char *name = filenames->name;
2089 const char *fname;
2090 const char *percent;
2091 struct pattern_var *p;
2092
2093 nextf = filenames->next;
2094 free (filenames);
2095
2096 /* If it's a pattern target, then add it to the pattern-specific
2097 variable list. */
2098 percent = find_percent_cached (&name);
2099 if (percent)
2100 {
2101 /* Get a reference for this pattern-specific variable struct. */
2102 p = create_pattern_var (name, percent);
2103 p->variable.fileinfo = *flocp;
2104 /* I don't think this can fail since we already determined it was a
2105 variable definition. */
2106#ifndef CONFIG_WITH_VALUE_LENGTH
2107 v = parse_variable_definition (&p->variable, defn);
2108#else
2109 v = parse_variable_definition (&p->variable, defn, NULL);
2110#endif
2111 assert (v != 0);
2112
2113 if (v->flavor == f_simple)
2114 v->value = allocated_variable_expand (v->value);
2115 else
2116 v->value = xstrdup (v->value);
2117
2118 fname = p->target;
2119 }
2120 else
2121 {
2122 struct file *f;
2123
2124 /* Get a file reference for this file, and initialize it.
2125 We don't want to just call enter_file() because that allocates a
2126 new entry if the file is a double-colon, which we don't want in
2127 this situation. */
2128 f = lookup_file (name);
2129 if (!f)
2130 f = enter_file (strcache_add (name));
2131 else if (f->double_colon)
2132 f = f->double_colon;
2133
2134 initialize_file_variables (f, 1);
2135 fname = f->name;
2136
2137 current_variable_set_list = f->variables;
2138#ifndef CONFIG_WITH_VALUE_LENGTH
2139 v = try_variable_definition (flocp, defn, origin, 1);
2140#else
2141 v = try_variable_definition (flocp, defn, NULL, origin, 1);
2142#endif
2143 if (!v)
2144 error (flocp, _("Malformed target-specific variable definition"));
2145 current_variable_set_list = global;
2146 }
2147
2148 /* Set up the variable to be *-specific. */
2149 v->origin = origin;
2150 v->per_target = 1;
2151 v->export = exported ? v_export : v_default;
2152
2153 /* If it's not an override, check to see if there was a command-line
2154 setting. If so, reset the value. */
2155 if (origin != o_override)
2156 {
2157 struct variable *gv;
2158 int len = strlen(v->name);
2159
2160 gv = lookup_variable (v->name, len);
2161 if (gv && (gv->origin == o_env_override || gv->origin == o_command))
2162 {
2163 if (v->value != 0)
2164 free (v->value);
2165 v->value = xstrdup (gv->value);
2166 v->origin = gv->origin;
2167 v->recursive = gv->recursive;
2168 v->append = 0;
2169 }
2170 }
2171 }
2172}
2173
2174
2175/* Record a description line for files FILENAMES,
2176 with dependencies DEPS, commands to execute described
2177 by COMMANDS and COMMANDS_IDX, coming from FILENAME:COMMANDS_STARTED.
2178 TWO_COLON is nonzero if a double colon was used.
2179 If not nil, PATTERN is the `%' pattern to make this
2180 a static pattern rule, and PATTERN_PERCENT is a pointer
2181 to the `%' within it.
2182
2183 The links of FILENAMES are freed, and so are any names in it
2184 that are not incorporated into other data structures. */
2185
2186#ifndef CONFIG_WITH_INCLUDEDEP
2187static void
2188#else
2189void
2190#endif
2191record_files (struct nameseq *filenames, const char *pattern,
2192 const char *pattern_percent, struct dep *deps,
2193 unsigned int cmds_started, char *commands,
2194 unsigned int commands_idx, int two_colon,
2195 const struct floc *flocp)
2196{
2197 struct nameseq *nextf;
2198 int implicit = 0;
2199 unsigned int max_targets = 0, target_idx = 0;
2200 const char **targets = 0, **target_percents = 0;
2201 struct commands *cmds;
2202#ifdef CONFIG_WITH_EXPLICIT_MULTITARGET
2203 struct file *prev_file = 0;
2204 enum multitarget_mode { m_unsettled, m_no, m_yes, m_yes_maybe }
2205 multi_mode = !two_colon && !pattern ? m_unsettled : m_no;
2206#endif
2207
2208 /* If we've already snapped deps, that means we're in an eval being
2209 resolved after the makefiles have been read in. We can't add more rules
2210 at this time, since they won't get snapped and we'll get core dumps.
2211 See Savannah bug # 12124. */
2212 if (snapped_deps)
2213 fatal (flocp, _("prerequisites cannot be defined in command scripts"));
2214
2215 if (commands_idx > 0)
2216 {
2217 cmds = xmalloc (sizeof (struct commands));
2218 cmds->fileinfo.filenm = flocp->filenm;
2219 cmds->fileinfo.lineno = cmds_started;
2220 cmds->commands = savestring (commands, commands_idx);
2221 cmds->command_lines = 0;
2222 }
2223 else
2224 cmds = 0;
2225
2226 for (; filenames != 0; filenames = nextf)
2227 {
2228 const char *name = filenames->name;
2229 struct file *f;
2230 struct dep *this = 0;
2231 const char *implicit_percent;
2232
2233 nextf = filenames->next;
2234 free (filenames);
2235
2236 /* Check for special targets. Do it here instead of, say, snap_deps()
2237 so that we can immediately use the value. */
2238
2239 if (streq (name, ".POSIX"))
2240 posix_pedantic = 1;
2241 else if (streq (name, ".SECONDEXPANSION"))
2242 second_expansion = 1;
2243#ifdef CONFIG_WITH_2ND_TARGET_EXPANSION
2244 else if (streq (name, ".SECONDTARGETEXPANSION"))
2245 second_target_expansion = 1;
2246#endif
2247
2248 implicit_percent = find_percent_cached (&name);
2249 implicit |= implicit_percent != 0;
2250
2251 if (implicit)
2252 {
2253 if (pattern != 0)
2254 fatal (flocp, _("mixed implicit and static pattern rules"));
2255
2256 if (implicit_percent == 0)
2257 fatal (flocp, _("mixed implicit and normal rules"));
2258
2259 if (targets == 0)
2260 {
2261 max_targets = 5;
2262 targets = xmalloc (5 * sizeof (char *));
2263 target_percents = xmalloc (5 * sizeof (char *));
2264 target_idx = 0;
2265 }
2266 else if (target_idx == max_targets - 1)
2267 {
2268 max_targets += 5;
2269 targets = xrealloc ((void *)targets, max_targets * sizeof (char *));
2270 target_percents = xrealloc ((void *)target_percents,
2271 max_targets * sizeof (char *));
2272 }
2273 targets[target_idx] = name;
2274 target_percents[target_idx] = implicit_percent;
2275 ++target_idx;
2276 continue;
2277 }
2278
2279#ifdef CONFIG_WITH_EXPLICIT_MULTITARGET
2280 /* Check for the explicit multitarget mode operators. For this to be
2281 identified as an explicit multiple target rule, the first + or +|
2282 operator *must* appear between the first two files. If not found as
2283 the 2nd file or if found as the 1st file, the rule will be rejected
2284 as a potential multiple first target rule. For the subsequent files
2285 the operator is only required to switch between maybe and non-maybe
2286 mode:
2287 `primary + 2nd 3rd +| 4th-maybe + 5th-for-sure: deps; cmds'
2288
2289 The whole idea of the maybe-updated files is this:
2290 timestamp +| maybe.h: src1.c src2.c
2291 grep goes-into-maybe.h $* > timestamp
2292 cmp timestamp maybe.h || cp -f timestamp maybe.h
2293
2294 This is implemented in remake.c where we don't consider the mtime of
2295 the maybe-updated targets. */
2296 if (multi_mode != m_no && name[0] == '+'
2297 && (name[1] == '\0' || (name[1] == '|' && name[2] == '\0')))
2298 {
2299 if (!prev_file)
2300 multi_mode = m_no; /* first */
2301 else
2302 {
2303 if (multi_mode == m_unsettled)
2304 {
2305 prev_file->multi_head = prev_file;
2306
2307 /* Only the primary file needs the dependencies. */
2308 if (deps)
2309 {
2310 free_dep_chain (deps);
2311 deps = NULL;
2312 }
2313 }
2314 multi_mode = name[1] == '\0' ? m_yes : m_yes_maybe;
2315 continue;
2316 }
2317 }
2318 else if (multi_mode == m_unsettled && prev_file)
2319 multi_mode = m_no;
2320#endif
2321
2322 /* If this is a static pattern rule:
2323 `targets: target%pattern: dep%pattern; cmds',
2324 make sure the pattern matches this target name. */
2325 if (pattern && !pattern_matches (pattern, pattern_percent, name))
2326 error (flocp, _("target `%s' doesn't match the target pattern"), name);
2327 else if (deps)
2328 {
2329 /* If there are multiple filenames, copy the chain DEPS for all but
2330 the last one. It is not safe for the same deps to go in more
2331 than one place in the database. */
2332 this = nextf != 0 ? copy_dep_chain (deps) : deps;
2333 this->need_2nd_expansion = (second_expansion
2334 && strchr (this->name, '$'));
2335 }
2336
2337 if (!two_colon)
2338 {
2339 /* Single-colon. Combine these dependencies
2340 with others in file's existing record, if any. */
2341 f = enter_file (strcache_add (name));
2342
2343 if (f->double_colon)
2344 fatal (flocp,
2345 _("target file `%s' has both : and :: entries"), f->name);
2346
2347 /* If CMDS == F->CMDS, this target was listed in this rule
2348 more than once. Just give a warning since this is harmless. */
2349 if (cmds != 0 && cmds == f->cmds)
2350 error (flocp,
2351 _("target `%s' given more than once in the same rule."),
2352 f->name);
2353
2354 /* Check for two single-colon entries both with commands.
2355 Check is_target so that we don't lose on files such as .c.o
2356 whose commands were preinitialized. */
2357 else if (cmds != 0 && f->cmds != 0 && f->is_target)
2358 {
2359 error (&cmds->fileinfo,
2360 _("warning: overriding commands for target `%s'"),
2361 f->name);
2362 error (&f->cmds->fileinfo,
2363 _("warning: ignoring old commands for target `%s'"),
2364 f->name);
2365 }
2366
2367 f->is_target = 1;
2368
2369 /* Defining .DEFAULT with no deps or cmds clears it. */
2370 if (f == default_file && this == 0 && cmds == 0)
2371 f->cmds = 0;
2372 if (cmds != 0)
2373 f->cmds = cmds;
2374
2375#ifdef CONFIG_WITH_EXPLICIT_MULTITARGET
2376 /* If this is an explicit multi target rule, add it to the
2377 target chain and set the multi_maybe flag according to
2378 the current mode. */
2379
2380 if (multi_mode >= m_yes)
2381 {
2382 f->multi_maybe = multi_mode == m_yes_maybe;
2383 prev_file->multi_next = f;
2384 assert (prev_file->multi_head != 0);
2385 f->multi_head = prev_file->multi_head;
2386
2387 if (f == suffix_file)
2388 error (flocp,
2389 _(".SUFFIXES encountered in an explicit multi target rule"));
2390 }
2391 prev_file = f;
2392#endif
2393
2394 /* Defining .SUFFIXES with no dependencies clears out the list of
2395 suffixes. */
2396 if (f == suffix_file && this == 0)
2397 {
2398 free_dep_chain (f->deps);
2399 f->deps = 0;
2400 }
2401 else if (this != 0)
2402 {
2403 /* Add the file's old deps and the new ones in THIS together. */
2404
2405 if (f->deps != 0)
2406 {
2407 struct dep **d_ptr = &f->deps;
2408
2409 while ((*d_ptr)->next != 0)
2410 d_ptr = &(*d_ptr)->next;
2411
2412 if (cmds != 0)
2413 /* This is the rule with commands, so put its deps
2414 last. The rationale behind this is that $< expands to
2415 the first dep in the chain, and commands use $<
2416 expecting to get the dep that rule specifies. However
2417 the second expansion algorithm reverses the order thus
2418 we need to make it last here. */
2419 (*d_ptr)->next = this;
2420 else
2421 {
2422 /* This is the rule without commands. Put its
2423 dependencies at the end but before dependencies from
2424 the rule with commands (if any). This way everything
2425 appears in makefile order. */
2426
2427 if (f->cmds != 0)
2428 {
2429#ifndef KMK /* bugfix: Don't chop the chain! */
2430 this->next = *d_ptr;
2431 *d_ptr = this;
2432#else /* KMK */
2433 struct dep *this_last = this;
2434 while (this_last->next)
2435 this_last = this_last->next;
2436 this_last->next = *d_ptr;
2437 *d_ptr = this;
2438#endif /* KMK */
2439 }
2440 else
2441 (*d_ptr)->next = this;
2442 }
2443 }
2444 else
2445 f->deps = this;
2446
2447 /* This is a hack. I need a way to communicate to snap_deps()
2448 that the last dependency line in this file came with commands
2449 (so that logic in snap_deps() can put it in front and all
2450 this $< -logic works). I cannot simply rely on file->cmds
2451 being not 0 because of the cases like the following:
2452
2453 foo: bar
2454 foo:
2455 ...
2456
2457 I am going to temporarily "borrow" UPDATING member in
2458 `struct file' for this. */
2459
2460 if (cmds != 0)
2461 f->updating = 1;
2462 }
2463 }
2464 else
2465 {
2466 /* Double-colon. Make a new record even if there already is one. */
2467 f = lookup_file (name);
2468
2469 /* Check for both : and :: rules. Check is_target so
2470 we don't lose on default suffix rules or makefiles. */
2471 if (f != 0 && f->is_target && !f->double_colon)
2472 fatal (flocp,
2473 _("target file `%s' has both : and :: entries"), f->name);
2474 f = enter_file (strcache_add (name));
2475 /* If there was an existing entry and it was a double-colon entry,
2476 enter_file will have returned a new one, making it the prev
2477 pointer of the old one, and setting its double_colon pointer to
2478 the first one. */
2479 if (f->double_colon == 0)
2480 /* This is the first entry for this name, so we must set its
2481 double_colon pointer to itself. */
2482 f->double_colon = f;
2483 f->is_target = 1;
2484 f->deps = this;
2485 f->cmds = cmds;
2486 }
2487
2488 /* If this is a static pattern rule, set the stem to the part of its
2489 name that matched the `%' in the pattern, so you can use $* in the
2490 commands. */
2491 if (pattern)
2492 {
2493 static const char *percent = "%";
2494 char *buffer = variable_expand ("");
2495 const size_t buffer_offset = buffer - variable_buffer; /* bird */
2496 char *o = patsubst_expand_pat (buffer, name, pattern, percent,
2497 pattern_percent+1, percent+1);
2498 buffer = variable_buffer + buffer_offset; /* bird - variable_buffer may have been reallocated. */
2499 f->stem = strcache_add_len (buffer, o - buffer);
2500 if (this)
2501 {
2502 this->staticpattern = 1;
2503 this->stem = f->stem;
2504 }
2505 }
2506
2507 name = f->name;
2508
2509 /* If this target is a default target, update DEFAULT_GOAL_FILE. */
2510 if (streq (*default_goal_name, name)
2511 && (default_goal_file == 0
2512 || ! streq (default_goal_file->name, name)))
2513 default_goal_file = f;
2514 }
2515
2516 if (implicit)
2517 {
2518 if (deps)
2519 deps->need_2nd_expansion = second_expansion;
2520 create_pattern_rule (targets, target_percents, target_idx,
2521 two_colon, deps, cmds, 1);
2522 }
2523}
2524
2525
2526/* Search STRING for an unquoted STOPCHAR or blank (if BLANK is nonzero).
2527 Backslashes quote STOPCHAR, blanks if BLANK is nonzero, and backslash.
2528 Quoting backslashes are removed from STRING by compacting it into
2529 itself. Returns a pointer to the first unquoted STOPCHAR if there is
2530 one, or nil if there are none. STOPCHARs inside variable references are
2531 ignored if IGNOREVARS is true.
2532
2533 STOPCHAR _cannot_ be '$' if IGNOREVARS is true. */
2534
2535static char *
2536find_char_unquote (char *string, int stop1, int stop2, int blank,
2537 int ignorevars)
2538{
2539 unsigned int string_len = 0;
2540 char *p = string;
2541 register int ch; /* bird: 'optimiziations' */
2542
2543 if (ignorevars)
2544 ignorevars = '$';
2545
2546 while (1)
2547 {
2548 if (stop2 && blank)
2549 while ((ch = *p) != '\0' && ch != ignorevars && ch != stop1 && ch != stop2
2550 && ! isblank ((unsigned char) ch))
2551 ++p;
2552 else if (stop2)
2553 while ((ch = *p) != '\0' && ch != ignorevars && ch != stop1 && ch != stop2)
2554 ++p;
2555 else if (blank)
2556 while ((ch = *p) != '\0' && ch != ignorevars && ch != stop1
2557 && ! isblank ((unsigned char) ch))
2558 ++p;
2559 else
2560 while ((ch = *p) != '\0' && ch != ignorevars && ch != stop1)
2561 ++p;
2562
2563 if (ch == '\0')
2564 break;
2565
2566 /* If we stopped due to a variable reference, skip over its contents. */
2567 if (ch == ignorevars)
2568 {
2569 char openparen = p[1];
2570
2571 p += 2;
2572
2573 /* Skip the contents of a non-quoted, multi-char variable ref. */
2574 if (openparen == '(' || openparen == '{')
2575 {
2576 unsigned int pcount = 1;
2577 char closeparen = (openparen == '(' ? ')' : '}');
2578
2579 while ((ch = *p))
2580 {
2581 if (ch == openparen)
2582 ++pcount;
2583 else if (ch == closeparen)
2584 if (--pcount == 0)
2585 {
2586 ++p;
2587 break;
2588 }
2589 ++p;
2590 }
2591 }
2592
2593 /* Skipped the variable reference: look for STOPCHARS again. */
2594 continue;
2595 }
2596
2597 if (p > string && p[-1] == '\\')
2598 {
2599 /* Search for more backslashes. */
2600 int i = -2;
2601 while (&p[i] >= string && p[i] == '\\')
2602 --i;
2603 ++i;
2604 /* Only compute the length if really needed. */
2605 if (string_len == 0)
2606 string_len = strlen (string);
2607 /* The number of backslashes is now -I.
2608 Copy P over itself to swallow half of them. */
2609 memmove (&p[i], &p[i/2], (string_len - (p - string)) - (i/2) + 1);
2610 p += i/2;
2611 if (i % 2 == 0)
2612 /* All the backslashes quoted each other; the STOPCHAR was
2613 unquoted. */
2614 return p;
2615
2616 /* The STOPCHAR was quoted by a backslash. Look for another. */
2617 }
2618 else
2619 /* No backslash in sight. */
2620 return p;
2621 }
2622
2623 /* Never hit a STOPCHAR or blank (with BLANK nonzero). */
2624 return 0;
2625}
2626
2627/* Search PATTERN for an unquoted % and handle quoting. */
2628
2629char *
2630find_percent (char *pattern)
2631{
2632 return find_char_unquote (pattern, '%', 0, 0, 0);
2633}
2634
2635/* Search STRING for an unquoted % and handle quoting. Returns a pointer to
2636 the % or NULL if no % was found.
2637 This version is used with strings in the string cache: if there's a need to
2638 modify the string a new version will be added to the string cache and
2639 *STRING will be set to that. */
2640
2641const char *
2642find_percent_cached (const char **string)
2643{
2644 const char *p = *string;
2645 char *new = 0;
2646 int slen;
2647
2648 /* If the first char is a % return now. This lets us avoid extra tests
2649 inside the loop. */
2650 if (*p == '%')
2651 return p;
2652
2653 while (1)
2654 {
2655 while (*p != '\0' && *p != '%')
2656 ++p;
2657
2658 if (*p == '\0')
2659 break;
2660
2661 /* See if this % is escaped with a backslash; if not we're done. */
2662 if (p[-1] != '\\')
2663 break;
2664
2665 {
2666 /* Search for more backslashes. */
2667 char *pv;
2668 int i = -2;
2669
2670 while (&p[i] >= *string && p[i] == '\\')
2671 --i;
2672 ++i;
2673
2674 /* At this point we know we'll need to allocate a new string.
2675 Make a copy if we haven't yet done so. */
2676 if (! new)
2677 {
2678 slen = strlen (*string);
2679 new = alloca (slen + 1);
2680 memcpy (new, *string, slen + 1);
2681 p = new + (p - *string);
2682 *string = new;
2683 }
2684
2685 /* At this point *string, p, and new all point into the same string.
2686 Get a non-const version of p so we can modify new. */
2687 pv = new + (p - *string);
2688
2689 /* The number of backslashes is now -I.
2690 Copy P over itself to swallow half of them. */
2691 memmove (&pv[i], &pv[i/2], (slen - (pv - new)) - (i/2) + 1);
2692 p += i/2;
2693
2694 /* If the backslashes quoted each other; the % was unquoted. */
2695 if (i % 2 == 0)
2696 break;
2697 }
2698 }
2699
2700 /* If we had to change STRING, add it to the strcache. */
2701 if (new)
2702 {
2703 *string = strcache_add (*string);
2704 p = *string + (p - new);
2705 }
2706
2707 /* If we didn't find a %, return NULL. Otherwise return a ptr to it. */
2708 return (*p == '\0') ? NULL : p;
2709}
2710
2711
2712/* Parse a string into a sequence of filenames represented as a
2713 chain of struct nameseq's in reverse order and return that chain.
2714
2715 The string is passed as STRINGP, the address of a string pointer.
2716 The string pointer is updated to point at the first character
2717 not parsed, which either is a null char or equals STOPCHAR.
2718
2719 SIZE is how big to construct chain elements.
2720 This is useful if we want them actually to be other structures
2721 that have room for additional info.
2722
2723 If STRIP is nonzero, strip `./'s off the beginning. */
2724
2725struct nameseq *
2726parse_file_seq (char **stringp, int stopchar, unsigned int size, int strip)
2727{
2728 struct nameseq *new = 0;
2729 struct nameseq *new1;
2730#ifndef NO_ARCHIVES /* bird: MSC warning */
2731 struct nameseq *lastnew1;
2732#endif
2733 char *p = *stringp;
2734
2735#ifdef VMS
2736# define VMS_COMMA ','
2737#else
2738# define VMS_COMMA 0
2739#endif
2740
2741 while (1)
2742 {
2743 const char *name;
2744 char *q;
2745
2746 /* Skip whitespace; see if any more names are left. */
2747 p = next_token (p);
2748 if (*p == '\0')
2749 break;
2750 if (*p == stopchar)
2751 break;
2752
2753 /* There are, so find the end of the next name. */
2754 q = p;
2755 p = find_char_unquote (q, stopchar, VMS_COMMA, 1, 0);
2756#ifdef VMS
2757 /* convert comma separated list to space separated */
2758 if (p && *p == ',')
2759 *p =' ';
2760#endif
2761#ifdef _AMIGA
2762 if (stopchar == ':' && p && *p == ':'
2763 && !(isspace ((unsigned char)p[1]) || !p[1]
2764 || isspace ((unsigned char)p[-1])))
2765 p = find_char_unquote (p+1, stopchar, VMS_COMMA, 1, 0);
2766#endif
2767#ifdef HAVE_DOS_PATHS
2768 /* For DOS paths, skip a "C:\..." or a "C:/..." until we find the
2769 first colon which isn't followed by a slash or a backslash.
2770 Note that tokens separated by spaces should be treated as separate
2771 tokens since make doesn't allow path names with spaces */
2772 if (stopchar == ':')
2773 while (p != 0 && !isspace ((unsigned char)*p) &&
2774 (p[1] == '\\' || p[1] == '/') && isalpha ((unsigned char)p[-1]))
2775 p = find_char_unquote (p + 1, stopchar, VMS_COMMA, 1, 0);
2776#endif
2777 if (p == 0)
2778 p = q + strlen (q);
2779
2780 if (strip)
2781#ifdef VMS
2782 /* Skip leading `[]'s. */
2783 while (p - q > 2 && q[0] == '[' && q[1] == ']')
2784#else
2785 /* Skip leading `./'s. */
2786 while (p - q > 2 && q[0] == '.' && q[1] == '/')
2787#endif
2788 {
2789 q += 2; /* Skip "./". */
2790 while (q < p && *q == '/')
2791 /* Skip following slashes: ".//foo" is "foo", not "/foo". */
2792 ++q;
2793 }
2794
2795 /* Extract the filename just found, and skip it. */
2796
2797 if (q == p)
2798 /* ".///" was stripped to "". */
2799#if defined(VMS)
2800 continue;
2801#elif defined(_AMIGA)
2802 name = "";
2803#else
2804 name = "./";
2805#endif
2806 else
2807#ifdef VMS
2808/* VMS filenames can have a ':' in them but they have to be '\'ed but we need
2809 * to remove this '\' before we can use the filename.
2810 * Savestring called because q may be read-only string constant.
2811 */
2812 {
2813 char *qbase = xstrdup (q);
2814 char *pbase = qbase + (p-q);
2815 char *q1 = qbase;
2816 char *q2 = q1;
2817 char *p1 = pbase;
2818
2819 while (q1 != pbase)
2820 {
2821 if (*q1 == '\\' && *(q1+1) == ':')
2822 {
2823 q1++;
2824 p1--;
2825 }
2826 *q2++ = *q1++;
2827 }
2828 name = strcache_add_len (qbase, p1 - qbase);
2829 free (qbase);
2830 }
2831#else
2832 name = strcache_add_len (q, p - q);
2833#endif
2834
2835 /* Add it to the front of the chain. */
2836 new1 = xmalloc (size);
2837 new1->name = name;
2838 new1->next = new;
2839 new = new1;
2840 }
2841
2842#ifndef NO_ARCHIVES
2843
2844 /* Look for multi-word archive references.
2845 They are indicated by a elt ending with an unmatched `)' and
2846 an elt further down the chain (i.e., previous in the file list)
2847 with an unmatched `(' (e.g., "lib(mem"). */
2848
2849 new1 = new;
2850 lastnew1 = 0;
2851 while (new1 != 0)
2852 if (new1->name[0] != '(' /* Don't catch "(%)" and suchlike. */
2853 && new1->name[strlen (new1->name) - 1] == ')'
2854 && strchr (new1->name, '(') == 0)
2855 {
2856 /* NEW1 ends with a `)' but does not contain a `('.
2857 Look back for an elt with an opening `(' but no closing `)'. */
2858
2859 struct nameseq *n = new1->next, *lastn = new1;
2860 char *paren = 0;
2861 while (n != 0 && (paren = strchr (n->name, '(')) == 0)
2862 {
2863 lastn = n;
2864 n = n->next;
2865 }
2866 if (n != 0
2867 /* Ignore something starting with `(', as that cannot actually
2868 be an archive-member reference (and treating it as such
2869 results in an empty file name, which causes much lossage). */
2870 && n->name[0] != '(')
2871 {
2872 /* N is the first element in the archive group.
2873 Its name looks like "lib(mem" (with no closing `)'). */
2874
2875 char *libname;
2876
2877 /* Copy "lib(" into LIBNAME. */
2878 ++paren;
2879 libname = alloca (paren - n->name + 1);
2880 memcpy (libname, n->name, paren - n->name);
2881 libname[paren - n->name] = '\0';
2882
2883 if (*paren == '\0')
2884 {
2885 /* N was just "lib(", part of something like "lib( a b)".
2886 Edit it out of the chain and free its storage. */
2887 lastn->next = n->next;
2888 free (n);
2889 /* LASTN->next is the new stopping elt for the loop below. */
2890 n = lastn->next;
2891 }
2892 else
2893 {
2894 /* Replace N's name with the full archive reference. */
2895 n->name = strcache_add (concat (libname, paren, ")"));
2896 }
2897
2898 if (new1->name[1] == '\0')
2899 {
2900 /* NEW1 is just ")", part of something like "lib(a b )".
2901 Omit it from the chain and free its storage. */
2902 if (lastnew1 == 0)
2903 new = new1->next;
2904 else
2905 lastnew1->next = new1->next;
2906 lastn = new1;
2907 new1 = new1->next;
2908 free (lastn);
2909 }
2910 else
2911 {
2912 /* Replace also NEW1->name, which already has closing `)'. */
2913 new1->name = strcache_add (concat (libname, new1->name, ""));
2914 new1 = new1->next;
2915 }
2916
2917 /* Trace back from NEW1 (the end of the list) until N
2918 (the beginning of the list), rewriting each name
2919 with the full archive reference. */
2920
2921 while (new1 != n)
2922 {
2923 new1->name = strcache_add (concat (libname, new1->name, ")"));
2924 lastnew1 = new1;
2925 new1 = new1->next;
2926 }
2927 }
2928 else
2929 {
2930 /* No frobnication happening. Just step down the list. */
2931 lastnew1 = new1;
2932 new1 = new1->next;
2933 }
2934 }
2935 else
2936 {
2937 lastnew1 = new1;
2938 new1 = new1->next;
2939 }
2940
2941#endif
2942
2943 *stringp = p;
2944 return new;
2945}
2946
2947
2948/* Find the next line of text in an eval buffer, combining continuation lines
2949 into one line.
2950 Return the number of actual lines read (> 1 if continuation lines).
2951 Returns -1 if there's nothing left in the buffer.
2952
2953 After this function, ebuf->buffer points to the first character of the
2954 line we just found.
2955 */
2956
2957/* Read a line of text from a STRING.
2958 Since we aren't really reading from a file, don't bother with linenumbers.
2959 */
2960
2961static unsigned long
2962readstring (struct ebuffer *ebuf)
2963{
2964 char *eol;
2965#ifdef CONFIG_WITH_VALUE_LENGTH
2966 char *end;
2967#endif
2968
2969 /* If there is nothing left in this buffer, return 0. */
2970 if (ebuf->bufnext >= ebuf->bufstart + ebuf->size)
2971 return -1;
2972
2973 /* Set up a new starting point for the buffer, and find the end of the
2974 next logical line (taking into account backslash/newline pairs). */
2975
2976 eol = ebuf->buffer = ebuf->bufnext;
2977#ifdef CONFIG_WITH_VALUE_LENGTH
2978 end = ebuf->bufstart + ebuf->size;
2979#endif
2980
2981 while (1)
2982 {
2983 int backslash = 0;
2984 char *bol = eol;
2985 char *p;
2986
2987 /* Find the next newline. At EOS, stop. */
2988#ifndef CONFIG_WITH_VALUE_LENGTH
2989 eol = p = strchr (eol , '\n');
2990#else
2991 p = (char *)memchr (eol, '\n', end - eol);
2992 assert (!memchr (eol, '\0', p != 0 ? p - eol : end - eol));
2993 eol = p;
2994#endif
2995 if (!eol)
2996 {
2997 ebuf->bufnext = ebuf->bufstart + ebuf->size + 1;
2998#ifdef CONFIG_WITH_VALUE_LENGTH
2999 ebuf->eol = end;
3000#endif
3001 return 0;
3002 }
3003
3004 /* Found a newline; if it's escaped continue; else we're done. */
3005 while (p > bol && *(--p) == '\\')
3006 backslash = !backslash;
3007 if (!backslash)
3008 break;
3009 ++eol;
3010 }
3011
3012 /* Overwrite the newline char. */
3013 *eol = '\0';
3014 ebuf->bufnext = eol+1;
3015#ifdef CONFIG_WITH_VALUE_LENGTH
3016 ebuf->eol = eol;
3017#endif
3018
3019 return 0;
3020}
3021
3022static long
3023readline (struct ebuffer *ebuf)
3024{
3025 char *p;
3026 char *end;
3027 char *start;
3028 long nlines = 0;
3029
3030 /* The behaviors between string and stream buffers are different enough to
3031 warrant different functions. Do the Right Thing. */
3032
3033 if (!ebuf->fp)
3034 return readstring (ebuf);
3035
3036 /* When reading from a file, we always start over at the beginning of the
3037 buffer for each new line. */
3038
3039 p = start = ebuf->bufstart;
3040 end = p + ebuf->size;
3041 *p = '\0';
3042#ifdef CONFIG_WITH_VALUE_LENGTH
3043 ebuf->eol = p;
3044#endif
3045
3046 while (fgets (p, end - p, ebuf->fp) != 0)
3047 {
3048 char *p2;
3049 unsigned long len;
3050 int backslash;
3051
3052 len = strlen (p);
3053 if (len == 0)
3054 {
3055 /* This only happens when the first thing on the line is a '\0'.
3056 It is a pretty hopeless case, but (wonder of wonders) Athena
3057 lossage strikes again! (xmkmf puts NULs in its makefiles.)
3058 There is nothing really to be done; we synthesize a newline so
3059 the following line doesn't appear to be part of this line. */
3060 error (&ebuf->floc,
3061 _("warning: NUL character seen; rest of line ignored"));
3062 p[0] = '\n';
3063 len = 1;
3064 }
3065
3066 /* Jump past the text we just read. */
3067 p += len;
3068
3069 /* If the last char isn't a newline, the whole line didn't fit into the
3070 buffer. Get some more buffer and try again. */
3071 if (p[-1] != '\n')
3072 goto more_buffer;
3073
3074 /* We got a newline, so add one to the count of lines. */
3075 ++nlines;
3076
3077#if !defined(WINDOWS32) && !defined(__MSDOS__) && !defined(__EMX__)
3078 /* Check to see if the line was really ended with CRLF; if so ignore
3079 the CR. */
3080 if ((p - start) > 1 && p[-2] == '\r')
3081 {
3082 --p;
3083 p[-1] = '\n';
3084 }
3085#endif
3086
3087 backslash = 0;
3088 for (p2 = p - 2; p2 >= start; --p2)
3089 {
3090 if (*p2 != '\\')
3091 break;
3092 backslash = !backslash;
3093 }
3094
3095 if (!backslash)
3096 {
3097 p[-1] = '\0';
3098#ifdef CONFIG_WITH_VALUE_LENGTH
3099 ebuf->eol = p - 1;
3100#endif
3101 break;
3102 }
3103
3104 /* It was a backslash/newline combo. If we have more space, read
3105 another line. */
3106 if (end - p >= 80)
3107 {
3108#ifdef CONFIG_WITH_VALUE_LENGTH
3109 ebuf->eol = p;
3110#endif
3111 continue;
3112 }
3113
3114 /* We need more space at the end of our buffer, so realloc it.
3115 Make sure to preserve the current offset of p. */
3116 more_buffer:
3117 {
3118 unsigned long off = p - start;
3119 ebuf->size *= 2;
3120 start = ebuf->buffer = ebuf->bufstart = xrealloc (start, ebuf->size);
3121 p = start + off;
3122 end = start + ebuf->size;
3123 *p = '\0';
3124#ifdef CONFIG_WITH_VALUE_LENGTH
3125 ebuf->eol = p;
3126#endif
3127 }
3128 }
3129
3130 if (ferror (ebuf->fp))
3131 pfatal_with_name (ebuf->floc.filenm);
3132
3133 /* If we found some lines, return how many.
3134 If we didn't, but we did find _something_, that indicates we read the last
3135 line of a file with no final newline; return 1.
3136 If we read nothing, we're at EOF; return -1. */
3137
3138 return nlines ? nlines : p == ebuf->bufstart ? -1 : 1;
3139}
3140
3141
3142/* Parse the next "makefile word" from the input buffer, and return info
3143 about it.
3144
3145 A "makefile word" is one of:
3146
3147 w_bogus Should never happen
3148 w_eol End of input
3149 w_static A static word; cannot be expanded
3150 w_variable A word containing one or more variables/functions
3151 w_colon A colon
3152 w_dcolon A double-colon
3153 w_semicolon A semicolon
3154 w_varassign A variable assignment operator (=, :=, +=, >=, or ?=)
3155
3156 Note that this function is only used when reading certain parts of the
3157 makefile. Don't use it where special rules hold sway (RHS of a variable,
3158 in a command list, etc.) */
3159
3160static enum make_word_type
3161get_next_mword (char *buffer, char *delim, char **startp, unsigned int *length)
3162{
3163 enum make_word_type wtype = w_bogus;
3164 char *p = buffer, *beg;
3165 char c;
3166
3167 /* Skip any leading whitespace. */
3168 while (isblank ((unsigned char)*p))
3169 ++p;
3170
3171 beg = p;
3172 c = *(p++);
3173 switch (c)
3174 {
3175 case '\0':
3176 wtype = w_eol;
3177 break;
3178
3179 case ';':
3180 wtype = w_semicolon;
3181 break;
3182
3183 case '=':
3184 wtype = w_varassign;
3185 break;
3186
3187 case ':':
3188 wtype = w_colon;
3189 switch (*p)
3190 {
3191 case ':':
3192 ++p;
3193 wtype = w_dcolon;
3194 break;
3195
3196 case '=':
3197 ++p;
3198 wtype = w_varassign;
3199 break;
3200 }
3201 break;
3202
3203 case '+':
3204 case '?':
3205#ifdef CONFIG_WITH_PREPEND_ASSIGNMENT
3206 case '>':
3207#endif
3208 if (*p == '=')
3209 {
3210 ++p;
3211 wtype = w_varassign;
3212 break;
3213 }
3214
3215 default:
3216 if (delim && strchr (delim, c))
3217 wtype = w_static;
3218 break;
3219 }
3220
3221 /* Did we find something? If so, return now. */
3222 if (wtype != w_bogus)
3223 goto done;
3224
3225 /* This is some non-operator word. A word consists of the longest
3226 string of characters that doesn't contain whitespace, one of [:=#],
3227 or [?+]=, or one of the chars in the DELIM string. */
3228
3229 /* We start out assuming a static word; if we see a variable we'll
3230 adjust our assumptions then. */
3231 wtype = w_static;
3232
3233 /* We already found the first value of "c", above. */
3234 while (1)
3235 {
3236 char closeparen;
3237 int count;
3238
3239 switch (c)
3240 {
3241 case '\0':
3242 case ' ':
3243 case '\t':
3244 case '=':
3245 goto done_word;
3246
3247 case ':':
3248#ifdef HAVE_DOS_PATHS
3249 /* A word CAN include a colon in its drive spec. The drive
3250 spec is allowed either at the beginning of a word, or as part
3251 of the archive member name, like in "libfoo.a(d:/foo/bar.o)". */
3252 if (!(p - beg >= 2
3253 && (*p == '/' || *p == '\\') && isalpha ((unsigned char)p[-2])
3254 && (p - beg == 2 || p[-3] == '(')))
3255#endif
3256 goto done_word;
3257
3258 case '$':
3259 c = *(p++);
3260 if (c == '$')
3261 break;
3262
3263 /* This is a variable reference, so note that it's expandable.
3264 Then read it to the matching close paren. */
3265 wtype = w_variable;
3266
3267 if (c == '(')
3268 closeparen = ')';
3269 else if (c == '{')
3270 closeparen = '}';
3271 else
3272 /* This is a single-letter variable reference. */
3273 break;
3274
3275 for (count=0; *p != '\0'; ++p)
3276 {
3277 if (*p == c)
3278 ++count;
3279 else if (*p == closeparen && --count < 0)
3280 {
3281 ++p;
3282 break;
3283 }
3284 }
3285 break;
3286
3287 case '?':
3288 case '+':
3289#ifdef CONFIG_WITH_PREPEND_ASSIGNMENT
3290 case '>':
3291#endif
3292 if (*p == '=')
3293 goto done_word;
3294 break;
3295
3296 case '\\':
3297 switch (*p)
3298 {
3299 case ':':
3300 case ';':
3301 case '=':
3302 case '\\':
3303 ++p;
3304 break;
3305 }
3306 break;
3307
3308 default:
3309 if (delim && strchr (delim, c))
3310 goto done_word;
3311 break;
3312 }
3313
3314 c = *(p++);
3315 }
3316 done_word:
3317 --p;
3318
3319 done:
3320 if (startp)
3321 *startp = beg;
3322 if (length)
3323 *length = p - beg;
3324 return wtype;
3325}
3326
3327
3328/* Construct the list of include directories
3329 from the arguments and the default list. */
3330
3331void
3332construct_include_path (const char **arg_dirs)
3333{
3334#ifdef VAXC /* just don't ask ... */
3335 stat_t stbuf;
3336#else
3337 struct stat stbuf;
3338#endif
3339 const char **dirs;
3340 const char **cpp;
3341 unsigned int idx;
3342
3343 /* Compute the number of pointers we need in the table. */
3344 idx = sizeof (default_include_directories) / sizeof (const char *);
3345 if (arg_dirs)
3346 for (cpp = arg_dirs; *cpp != 0; ++cpp)
3347 ++idx;
3348
3349#ifdef __MSDOS__
3350 /* Add one for $DJDIR. */
3351 ++idx;
3352#endif
3353#ifdef KMK
3354 /* Add one for the kBuild directory. */
3355 ++idx;
3356#endif
3357
3358 dirs = xmalloc (idx * sizeof (const char *));
3359
3360 idx = 0;
3361 max_incl_len = 0;
3362
3363 /* First consider any dirs specified with -I switches.
3364 Ignore any that don't exist. Remember the maximum string length. */
3365
3366 if (arg_dirs)
3367 while (*arg_dirs != 0)
3368 {
3369 const char *dir = *(arg_dirs++);
3370 char *expanded = 0;
3371 int e;
3372
3373 if (dir[0] == '~')
3374 {
3375 expanded = tilde_expand (dir);
3376 if (expanded != 0)
3377 dir = expanded;
3378 }
3379
3380 EINTRLOOP (e, stat (dir, &stbuf));
3381 if (e == 0 && S_ISDIR (stbuf.st_mode))
3382 {
3383 unsigned int len = strlen (dir);
3384 /* If dir name is written with trailing slashes, discard them. */
3385 while (len > 1 && dir[len - 1] == '/')
3386 --len;
3387 if (len > max_incl_len)
3388 max_incl_len = len;
3389 dirs[idx++] = strcache_add_len (dir, len);
3390 }
3391
3392 if (expanded)
3393 free (expanded);
3394 }
3395
3396 /* Now add the standard default dirs at the end. */
3397
3398#ifdef __MSDOS__
3399 {
3400 /* The environment variable $DJDIR holds the root of the DJGPP directory
3401 tree; add ${DJDIR}/include. */
3402 struct variable *djdir = lookup_variable ("DJDIR", 5);
3403
3404 if (djdir)
3405 {
3406 unsigned int len = strlen (djdir->value) + 8;
3407 char *defdir = alloca (len + 1);
3408
3409 strcat (strcpy (defdir, djdir->value), "/include");
3410 dirs[idx++] = strcache_add (defdir);
3411
3412 if (len > max_incl_len)
3413 max_incl_len = len;
3414 }
3415 }
3416#endif
3417#ifdef KMK
3418 /* Add $(KBUILD_PATH). */
3419 {
3420 size_t len = strlen (get_kbuild_path ());
3421 dirs[idx++] = strcache_add_len (get_kbuild_path (), len);
3422 if (len > max_incl_len)
3423 max_incl_len = len;
3424 }
3425#endif
3426
3427 for (cpp = default_include_directories; *cpp != 0; ++cpp)
3428 {
3429 int e;
3430
3431 EINTRLOOP (e, stat (*cpp, &stbuf));
3432 if (e == 0 && S_ISDIR (stbuf.st_mode))
3433 {
3434 unsigned int len = strlen (*cpp);
3435 /* If dir name is written with trailing slashes, discard them. */
3436 while (len > 1 && (*cpp)[len - 1] == '/')
3437 --len;
3438 if (len > max_incl_len)
3439 max_incl_len = len;
3440 dirs[idx++] = strcache_add_len (*cpp, len - 1);
3441 }
3442 }
3443
3444 dirs[idx] = 0;
3445
3446 /* Now add each dir to the .INCLUDE_DIRS variable. */
3447
3448 for (cpp = dirs; *cpp != 0; ++cpp)
3449 do_variable_definition (NILF, ".INCLUDE_DIRS", *cpp,
3450 o_default, f_append, 0);
3451
3452 include_directories = dirs;
3453}
3454
3455
3456/* Expand ~ or ~USER at the beginning of NAME.
3457 Return a newly malloc'd string or 0. */
3458
3459char *
3460tilde_expand (const char *name)
3461{
3462#ifndef VMS
3463 if (name[1] == '/' || name[1] == '\0')
3464 {
3465 extern char *getenv ();
3466 char *home_dir;
3467 int is_variable;
3468
3469 {
3470 /* Turn off --warn-undefined-variables while we expand HOME. */
3471 int save = warn_undefined_variables_flag;
3472 warn_undefined_variables_flag = 0;
3473
3474#ifndef CONFIG_WITH_VALUE_LENGTH
3475 home_dir = allocated_variable_expand ("$(HOME)");
3476#else
3477 home_dir = allocated_variable_expand_2 (STRING_SIZE_TUPLE("$(HOME)"), NULL);
3478#endif
3479
3480 warn_undefined_variables_flag = save;
3481 }
3482
3483 is_variable = home_dir[0] != '\0';
3484 if (!is_variable)
3485 {
3486 free (home_dir);
3487 home_dir = getenv ("HOME");
3488 }
3489# if !defined(_AMIGA) && !defined(WINDOWS32)
3490 if (home_dir == 0 || home_dir[0] == '\0')
3491 {
3492 extern char *getlogin ();
3493 char *logname = getlogin ();
3494 home_dir = 0;
3495 if (logname != 0)
3496 {
3497 struct passwd *p = getpwnam (logname);
3498 if (p != 0)
3499 home_dir = p->pw_dir;
3500 }
3501 }
3502# endif /* !AMIGA && !WINDOWS32 */
3503 if (home_dir != 0)
3504 {
3505 char *new = xstrdup (concat (home_dir, "", name + 1));
3506 if (is_variable)
3507 free (home_dir);
3508 return new;
3509 }
3510 }
3511# if !defined(_AMIGA) && !defined(WINDOWS32)
3512 else
3513 {
3514 struct passwd *pwent;
3515 char *userend = strchr (name + 1, '/');
3516 if (userend != 0)
3517 *userend = '\0';
3518 pwent = getpwnam (name + 1);
3519 if (pwent != 0)
3520 {
3521 if (userend == 0)
3522 return xstrdup (pwent->pw_dir);
3523 else
3524 return xstrdup (concat (pwent->pw_dir, "/", userend + 1));
3525 }
3526 else if (userend != 0)
3527 *userend = '/';
3528 }
3529# endif /* !AMIGA && !WINDOWS32 */
3530#endif /* !VMS */
3531 return 0;
3532}
3533
3534/* Given a chain of struct nameseq's describing a sequence of filenames,
3535 in reverse of the intended order, return a new chain describing the
3536 result of globbing the filenames. The new chain is in forward order.
3537 The links of the old chain are freed or used in the new chain.
3538 Likewise for the names in the old chain.
3539
3540 SIZE is how big to construct chain elements.
3541 This is useful if we want them actually to be other structures
3542 that have room for additional info. */
3543
3544struct nameseq *
3545multi_glob (struct nameseq *chain, unsigned int size)
3546{
3547 void dir_setup_glob (glob_t *);
3548 struct nameseq *new = 0;
3549 struct nameseq *old;
3550 struct nameseq *nexto;
3551 glob_t gl;
3552#if defined(KMK) || defined(__EMX__) /* speed optimization */
3553 int rc;
3554#endif
3555
3556 dir_setup_glob (&gl);
3557
3558 for (old = chain; old != 0; old = nexto)
3559 {
3560 const char *gname;
3561#ifndef NO_ARCHIVES
3562 char *arname = 0;
3563 char *memname = 0;
3564#endif
3565 nexto = old->next;
3566 gname = old->name;
3567
3568 if (gname[0] == '~')
3569 {
3570 char *newname = tilde_expand (old->name);
3571 if (newname != 0)
3572 gname = newname;
3573 }
3574
3575#ifndef NO_ARCHIVES
3576 if (ar_name (gname))
3577 {
3578 /* OLD->name is an archive member reference. Replace it with the
3579 archive file name, and save the member name in MEMNAME. We will
3580 glob on the archive name and then reattach MEMNAME later. */
3581 ar_parse_name (gname, &arname, &memname);
3582 gname = arname;
3583 }
3584#endif /* !NO_ARCHIVES */
3585
3586#if defined(KMK) || defined(__EMX__) /* speed optimization */
3587 if (!strpbrk(gname, "*?["))
3588 {
3589 gl.gl_pathc = 1;
3590 gl.gl_pathv = (char **)&gname;
3591 rc = 0;
3592 }
3593 else
3594 rc = glob (gname, GLOB_NOCHECK|GLOB_ALTDIRFUNC, NULL, &gl);
3595 switch (rc)
3596#else
3597 switch (glob (gname, GLOB_NOCHECK|GLOB_ALTDIRFUNC, NULL, &gl))
3598#endif
3599 {
3600 case 0: /* Success. */
3601 {
3602 int i = gl.gl_pathc;
3603 while (i-- > 0)
3604 {
3605#ifndef NO_ARCHIVES
3606 if (memname != 0)
3607 {
3608 /* Try to glob on MEMNAME within the archive. */
3609 struct nameseq *found
3610 = ar_glob (gl.gl_pathv[i], memname, size);
3611 if (! found)
3612 {
3613 /* No matches. Use MEMNAME as-is. */
3614 unsigned int alen = strlen (gl.gl_pathv[i]);
3615 unsigned int mlen = strlen (memname);
3616 char *name;
3617 struct nameseq *elt = xmalloc (size);
3618 memset (elt, '\0', size);
3619
3620 name = alloca (alen + 1 + mlen + 2);
3621 memcpy (name, gl.gl_pathv[i], alen);
3622 name[alen] = '(';
3623 memcpy (name+alen+1, memname, mlen);
3624 name[alen + 1 + mlen] = ')';
3625 name[alen + 1 + mlen + 1] = '\0';
3626 elt->name = strcache_add (name);
3627 elt->next = new;
3628 new = elt;
3629 }
3630 else
3631 {
3632 /* Find the end of the FOUND chain. */
3633 struct nameseq *f = found;
3634 while (f->next != 0)
3635 f = f->next;
3636
3637 /* Attach the chain being built to the end of the FOUND
3638 chain, and make FOUND the new NEW chain. */
3639 f->next = new;
3640 new = found;
3641 }
3642 }
3643 else
3644#endif /* !NO_ARCHIVES */
3645 {
3646 struct nameseq *elt = xmalloc (size);
3647 memset (elt, '\0', size);
3648 elt->name = strcache_add (gl.gl_pathv[i]);
3649 elt->next = new;
3650 new = elt;
3651 }
3652 }
3653#if defined(KMK) || defined(__EMX__) /* speed optimization */
3654 if (gl.gl_pathv != (char **)&gname)
3655#endif
3656 globfree (&gl);
3657 free (old);
3658 break;
3659 }
3660
3661 case GLOB_NOSPACE:
3662 fatal (NILF, _("virtual memory exhausted"));
3663 break;
3664
3665 default:
3666 old->next = new;
3667 new = old;
3668 break;
3669 }
3670
3671#ifndef NO_ARCHIVES
3672 if (arname)
3673 free (arname);
3674#endif
3675 }
3676
3677 return new;
3678}
3679
Note: See TracBrowser for help on using the repository browser.

© 2025 Oracle Support Privacy / Do Not Sell My Info Terms of Use Trademark Policy Automated Access Etiquette