VirtualBox

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

Last change on this file since 1817 was 1817, checked in by bird, 17 years ago

kmk: pass end-of-line around to the condition evalalutation. Don't alloca and copy the first part of an if condition to the stack, just use the buffer. Use allocated_variable_expand_2 where possible.

  • Property svn:eol-style set to native
File size: 103.3 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 this->next = *d_ptr;
2430 *d_ptr = this;
2431 }
2432 else
2433 (*d_ptr)->next = this;
2434 }
2435 }
2436 else
2437 f->deps = this;
2438
2439 /* This is a hack. I need a way to communicate to snap_deps()
2440 that the last dependency line in this file came with commands
2441 (so that logic in snap_deps() can put it in front and all
2442 this $< -logic works). I cannot simply rely on file->cmds
2443 being not 0 because of the cases like the following:
2444
2445 foo: bar
2446 foo:
2447 ...
2448
2449 I am going to temporarily "borrow" UPDATING member in
2450 `struct file' for this. */
2451
2452 if (cmds != 0)
2453 f->updating = 1;
2454 }
2455 }
2456 else
2457 {
2458 /* Double-colon. Make a new record even if there already is one. */
2459 f = lookup_file (name);
2460
2461 /* Check for both : and :: rules. Check is_target so
2462 we don't lose on default suffix rules or makefiles. */
2463 if (f != 0 && f->is_target && !f->double_colon)
2464 fatal (flocp,
2465 _("target file `%s' has both : and :: entries"), f->name);
2466 f = enter_file (strcache_add (name));
2467 /* If there was an existing entry and it was a double-colon entry,
2468 enter_file will have returned a new one, making it the prev
2469 pointer of the old one, and setting its double_colon pointer to
2470 the first one. */
2471 if (f->double_colon == 0)
2472 /* This is the first entry for this name, so we must set its
2473 double_colon pointer to itself. */
2474 f->double_colon = f;
2475 f->is_target = 1;
2476 f->deps = this;
2477 f->cmds = cmds;
2478 }
2479
2480 /* If this is a static pattern rule, set the stem to the part of its
2481 name that matched the `%' in the pattern, so you can use $* in the
2482 commands. */
2483 if (pattern)
2484 {
2485 static const char *percent = "%";
2486 char *buffer = variable_expand ("");
2487 const size_t buffer_offset = buffer - variable_buffer; /* bird */
2488 char *o = patsubst_expand_pat (buffer, name, pattern, percent,
2489 pattern_percent+1, percent+1);
2490 buffer = variable_buffer + buffer_offset; /* bird - variable_buffer may have been reallocated. */
2491 f->stem = strcache_add_len (buffer, o - buffer);
2492 if (this)
2493 {
2494 this->staticpattern = 1;
2495 this->stem = f->stem;
2496 }
2497 }
2498
2499 name = f->name;
2500
2501 /* If this target is a default target, update DEFAULT_GOAL_FILE. */
2502 if (streq (*default_goal_name, name)
2503 && (default_goal_file == 0
2504 || ! streq (default_goal_file->name, name)))
2505 default_goal_file = f;
2506 }
2507
2508 if (implicit)
2509 {
2510 if (deps)
2511 deps->need_2nd_expansion = second_expansion;
2512 create_pattern_rule (targets, target_percents, target_idx,
2513 two_colon, deps, cmds, 1);
2514 }
2515}
2516
2517
2518/* Search STRING for an unquoted STOPCHAR or blank (if BLANK is nonzero).
2519 Backslashes quote STOPCHAR, blanks if BLANK is nonzero, and backslash.
2520 Quoting backslashes are removed from STRING by compacting it into
2521 itself. Returns a pointer to the first unquoted STOPCHAR if there is
2522 one, or nil if there are none. STOPCHARs inside variable references are
2523 ignored if IGNOREVARS is true.
2524
2525 STOPCHAR _cannot_ be '$' if IGNOREVARS is true. */
2526
2527static char *
2528find_char_unquote (char *string, int stop1, int stop2, int blank,
2529 int ignorevars)
2530{
2531 unsigned int string_len = 0;
2532 char *p = string;
2533 register int ch; /* bird: 'optimiziations' */
2534
2535 if (ignorevars)
2536 ignorevars = '$';
2537
2538 while (1)
2539 {
2540 if (stop2 && blank)
2541 while ((ch = *p) != '\0' && ch != ignorevars && ch != stop1 && ch != stop2
2542 && ! isblank ((unsigned char) ch))
2543 ++p;
2544 else if (stop2)
2545 while ((ch = *p) != '\0' && ch != ignorevars && ch != stop1 && ch != stop2)
2546 ++p;
2547 else if (blank)
2548 while ((ch = *p) != '\0' && ch != ignorevars && ch != stop1
2549 && ! isblank ((unsigned char) ch))
2550 ++p;
2551 else
2552 while ((ch = *p) != '\0' && ch != ignorevars && ch != stop1)
2553 ++p;
2554
2555 if (ch == '\0')
2556 break;
2557
2558 /* If we stopped due to a variable reference, skip over its contents. */
2559 if (ch == ignorevars)
2560 {
2561 char openparen = p[1];
2562
2563 p += 2;
2564
2565 /* Skip the contents of a non-quoted, multi-char variable ref. */
2566 if (openparen == '(' || openparen == '{')
2567 {
2568 unsigned int pcount = 1;
2569 char closeparen = (openparen == '(' ? ')' : '}');
2570
2571 while ((ch = *p))
2572 {
2573 if (ch == openparen)
2574 ++pcount;
2575 else if (ch == closeparen)
2576 if (--pcount == 0)
2577 {
2578 ++p;
2579 break;
2580 }
2581 ++p;
2582 }
2583 }
2584
2585 /* Skipped the variable reference: look for STOPCHARS again. */
2586 continue;
2587 }
2588
2589 if (p > string && p[-1] == '\\')
2590 {
2591 /* Search for more backslashes. */
2592 int i = -2;
2593 while (&p[i] >= string && p[i] == '\\')
2594 --i;
2595 ++i;
2596 /* Only compute the length if really needed. */
2597 if (string_len == 0)
2598 string_len = strlen (string);
2599 /* The number of backslashes is now -I.
2600 Copy P over itself to swallow half of them. */
2601 memmove (&p[i], &p[i/2], (string_len - (p - string)) - (i/2) + 1);
2602 p += i/2;
2603 if (i % 2 == 0)
2604 /* All the backslashes quoted each other; the STOPCHAR was
2605 unquoted. */
2606 return p;
2607
2608 /* The STOPCHAR was quoted by a backslash. Look for another. */
2609 }
2610 else
2611 /* No backslash in sight. */
2612 return p;
2613 }
2614
2615 /* Never hit a STOPCHAR or blank (with BLANK nonzero). */
2616 return 0;
2617}
2618
2619/* Search PATTERN for an unquoted % and handle quoting. */
2620
2621char *
2622find_percent (char *pattern)
2623{
2624 return find_char_unquote (pattern, '%', 0, 0, 0);
2625}
2626
2627/* Search STRING for an unquoted % and handle quoting. Returns a pointer to
2628 the % or NULL if no % was found.
2629 This version is used with strings in the string cache: if there's a need to
2630 modify the string a new version will be added to the string cache and
2631 *STRING will be set to that. */
2632
2633const char *
2634find_percent_cached (const char **string)
2635{
2636 const char *p = *string;
2637 char *new = 0;
2638 int slen;
2639
2640 /* If the first char is a % return now. This lets us avoid extra tests
2641 inside the loop. */
2642 if (*p == '%')
2643 return p;
2644
2645 while (1)
2646 {
2647 while (*p != '\0' && *p != '%')
2648 ++p;
2649
2650 if (*p == '\0')
2651 break;
2652
2653 /* See if this % is escaped with a backslash; if not we're done. */
2654 if (p[-1] != '\\')
2655 break;
2656
2657 {
2658 /* Search for more backslashes. */
2659 char *pv;
2660 int i = -2;
2661
2662 while (&p[i] >= *string && p[i] == '\\')
2663 --i;
2664 ++i;
2665
2666 /* At this point we know we'll need to allocate a new string.
2667 Make a copy if we haven't yet done so. */
2668 if (! new)
2669 {
2670 slen = strlen (*string);
2671 new = alloca (slen + 1);
2672 memcpy (new, *string, slen + 1);
2673 p = new + (p - *string);
2674 *string = new;
2675 }
2676
2677 /* At this point *string, p, and new all point into the same string.
2678 Get a non-const version of p so we can modify new. */
2679 pv = new + (p - *string);
2680
2681 /* The number of backslashes is now -I.
2682 Copy P over itself to swallow half of them. */
2683 memmove (&pv[i], &pv[i/2], (slen - (pv - new)) - (i/2) + 1);
2684 p += i/2;
2685
2686 /* If the backslashes quoted each other; the % was unquoted. */
2687 if (i % 2 == 0)
2688 break;
2689 }
2690 }
2691
2692 /* If we had to change STRING, add it to the strcache. */
2693 if (new)
2694 {
2695 *string = strcache_add (*string);
2696 p = *string + (p - new);
2697 }
2698
2699 /* If we didn't find a %, return NULL. Otherwise return a ptr to it. */
2700 return (*p == '\0') ? NULL : p;
2701}
2702
2703
2704/* Parse a string into a sequence of filenames represented as a
2705 chain of struct nameseq's in reverse order and return that chain.
2706
2707 The string is passed as STRINGP, the address of a string pointer.
2708 The string pointer is updated to point at the first character
2709 not parsed, which either is a null char or equals STOPCHAR.
2710
2711 SIZE is how big to construct chain elements.
2712 This is useful if we want them actually to be other structures
2713 that have room for additional info.
2714
2715 If STRIP is nonzero, strip `./'s off the beginning. */
2716
2717struct nameseq *
2718parse_file_seq (char **stringp, int stopchar, unsigned int size, int strip)
2719{
2720 struct nameseq *new = 0;
2721 struct nameseq *new1;
2722#ifndef NO_ARCHIVES /* bird: MSC warning */
2723 struct nameseq *lastnew1;
2724#endif
2725 char *p = *stringp;
2726
2727#ifdef VMS
2728# define VMS_COMMA ','
2729#else
2730# define VMS_COMMA 0
2731#endif
2732
2733 while (1)
2734 {
2735 const char *name;
2736 char *q;
2737
2738 /* Skip whitespace; see if any more names are left. */
2739 p = next_token (p);
2740 if (*p == '\0')
2741 break;
2742 if (*p == stopchar)
2743 break;
2744
2745 /* There are, so find the end of the next name. */
2746 q = p;
2747 p = find_char_unquote (q, stopchar, VMS_COMMA, 1, 0);
2748#ifdef VMS
2749 /* convert comma separated list to space separated */
2750 if (p && *p == ',')
2751 *p =' ';
2752#endif
2753#ifdef _AMIGA
2754 if (stopchar == ':' && p && *p == ':'
2755 && !(isspace ((unsigned char)p[1]) || !p[1]
2756 || isspace ((unsigned char)p[-1])))
2757 p = find_char_unquote (p+1, stopchar, VMS_COMMA, 1, 0);
2758#endif
2759#ifdef HAVE_DOS_PATHS
2760 /* For DOS paths, skip a "C:\..." or a "C:/..." until we find the
2761 first colon which isn't followed by a slash or a backslash.
2762 Note that tokens separated by spaces should be treated as separate
2763 tokens since make doesn't allow path names with spaces */
2764 if (stopchar == ':')
2765 while (p != 0 && !isspace ((unsigned char)*p) &&
2766 (p[1] == '\\' || p[1] == '/') && isalpha ((unsigned char)p[-1]))
2767 p = find_char_unquote (p + 1, stopchar, VMS_COMMA, 1, 0);
2768#endif
2769 if (p == 0)
2770 p = q + strlen (q);
2771
2772 if (strip)
2773#ifdef VMS
2774 /* Skip leading `[]'s. */
2775 while (p - q > 2 && q[0] == '[' && q[1] == ']')
2776#else
2777 /* Skip leading `./'s. */
2778 while (p - q > 2 && q[0] == '.' && q[1] == '/')
2779#endif
2780 {
2781 q += 2; /* Skip "./". */
2782 while (q < p && *q == '/')
2783 /* Skip following slashes: ".//foo" is "foo", not "/foo". */
2784 ++q;
2785 }
2786
2787 /* Extract the filename just found, and skip it. */
2788
2789 if (q == p)
2790 /* ".///" was stripped to "". */
2791#if defined(VMS)
2792 continue;
2793#elif defined(_AMIGA)
2794 name = "";
2795#else
2796 name = "./";
2797#endif
2798 else
2799#ifdef VMS
2800/* VMS filenames can have a ':' in them but they have to be '\'ed but we need
2801 * to remove this '\' before we can use the filename.
2802 * Savestring called because q may be read-only string constant.
2803 */
2804 {
2805 char *qbase = xstrdup (q);
2806 char *pbase = qbase + (p-q);
2807 char *q1 = qbase;
2808 char *q2 = q1;
2809 char *p1 = pbase;
2810
2811 while (q1 != pbase)
2812 {
2813 if (*q1 == '\\' && *(q1+1) == ':')
2814 {
2815 q1++;
2816 p1--;
2817 }
2818 *q2++ = *q1++;
2819 }
2820 name = strcache_add_len (qbase, p1 - qbase);
2821 free (qbase);
2822 }
2823#else
2824 name = strcache_add_len (q, p - q);
2825#endif
2826
2827 /* Add it to the front of the chain. */
2828 new1 = xmalloc (size);
2829 new1->name = name;
2830 new1->next = new;
2831 new = new1;
2832 }
2833
2834#ifndef NO_ARCHIVES
2835
2836 /* Look for multi-word archive references.
2837 They are indicated by a elt ending with an unmatched `)' and
2838 an elt further down the chain (i.e., previous in the file list)
2839 with an unmatched `(' (e.g., "lib(mem"). */
2840
2841 new1 = new;
2842 lastnew1 = 0;
2843 while (new1 != 0)
2844 if (new1->name[0] != '(' /* Don't catch "(%)" and suchlike. */
2845 && new1->name[strlen (new1->name) - 1] == ')'
2846 && strchr (new1->name, '(') == 0)
2847 {
2848 /* NEW1 ends with a `)' but does not contain a `('.
2849 Look back for an elt with an opening `(' but no closing `)'. */
2850
2851 struct nameseq *n = new1->next, *lastn = new1;
2852 char *paren = 0;
2853 while (n != 0 && (paren = strchr (n->name, '(')) == 0)
2854 {
2855 lastn = n;
2856 n = n->next;
2857 }
2858 if (n != 0
2859 /* Ignore something starting with `(', as that cannot actually
2860 be an archive-member reference (and treating it as such
2861 results in an empty file name, which causes much lossage). */
2862 && n->name[0] != '(')
2863 {
2864 /* N is the first element in the archive group.
2865 Its name looks like "lib(mem" (with no closing `)'). */
2866
2867 char *libname;
2868
2869 /* Copy "lib(" into LIBNAME. */
2870 ++paren;
2871 libname = alloca (paren - n->name + 1);
2872 memcpy (libname, n->name, paren - n->name);
2873 libname[paren - n->name] = '\0';
2874
2875 if (*paren == '\0')
2876 {
2877 /* N was just "lib(", part of something like "lib( a b)".
2878 Edit it out of the chain and free its storage. */
2879 lastn->next = n->next;
2880 free (n);
2881 /* LASTN->next is the new stopping elt for the loop below. */
2882 n = lastn->next;
2883 }
2884 else
2885 {
2886 /* Replace N's name with the full archive reference. */
2887 n->name = strcache_add (concat (libname, paren, ")"));
2888 }
2889
2890 if (new1->name[1] == '\0')
2891 {
2892 /* NEW1 is just ")", part of something like "lib(a b )".
2893 Omit it from the chain and free its storage. */
2894 if (lastnew1 == 0)
2895 new = new1->next;
2896 else
2897 lastnew1->next = new1->next;
2898 lastn = new1;
2899 new1 = new1->next;
2900 free (lastn);
2901 }
2902 else
2903 {
2904 /* Replace also NEW1->name, which already has closing `)'. */
2905 new1->name = strcache_add (concat (libname, new1->name, ""));
2906 new1 = new1->next;
2907 }
2908
2909 /* Trace back from NEW1 (the end of the list) until N
2910 (the beginning of the list), rewriting each name
2911 with the full archive reference. */
2912
2913 while (new1 != n)
2914 {
2915 new1->name = strcache_add (concat (libname, new1->name, ")"));
2916 lastnew1 = new1;
2917 new1 = new1->next;
2918 }
2919 }
2920 else
2921 {
2922 /* No frobnication happening. Just step down the list. */
2923 lastnew1 = new1;
2924 new1 = new1->next;
2925 }
2926 }
2927 else
2928 {
2929 lastnew1 = new1;
2930 new1 = new1->next;
2931 }
2932
2933#endif
2934
2935 *stringp = p;
2936 return new;
2937}
2938
2939
2940/* Find the next line of text in an eval buffer, combining continuation lines
2941 into one line.
2942 Return the number of actual lines read (> 1 if continuation lines).
2943 Returns -1 if there's nothing left in the buffer.
2944
2945 After this function, ebuf->buffer points to the first character of the
2946 line we just found.
2947 */
2948
2949/* Read a line of text from a STRING.
2950 Since we aren't really reading from a file, don't bother with linenumbers.
2951 */
2952
2953static unsigned long
2954readstring (struct ebuffer *ebuf)
2955{
2956 char *eol;
2957#ifdef CONFIG_WITH_VALUE_LENGTH
2958 char *end;
2959#endif
2960
2961 /* If there is nothing left in this buffer, return 0. */
2962 if (ebuf->bufnext >= ebuf->bufstart + ebuf->size)
2963 return -1;
2964
2965 /* Set up a new starting point for the buffer, and find the end of the
2966 next logical line (taking into account backslash/newline pairs). */
2967
2968 eol = ebuf->buffer = ebuf->bufnext;
2969#ifdef CONFIG_WITH_VALUE_LENGTH
2970 end = ebuf->bufstart + ebuf->size;
2971#endif
2972
2973 while (1)
2974 {
2975 int backslash = 0;
2976 char *bol = eol;
2977 char *p;
2978
2979 /* Find the next newline. At EOS, stop. */
2980#ifndef CONFIG_WITH_VALUE_LENGTH
2981 eol = p = strchr (eol , '\n');
2982#else
2983 p = (char *)memchr (eol, '\n', end - eol);
2984 assert (!memchr (eol, '\0', p != 0 ? p - eol : end - eol));
2985 eol = p;
2986#endif
2987 if (!eol)
2988 {
2989 ebuf->bufnext = ebuf->bufstart + ebuf->size + 1;
2990#ifdef CONFIG_WITH_VALUE_LENGTH
2991 ebuf->eol = end;
2992#endif
2993 return 0;
2994 }
2995
2996 /* Found a newline; if it's escaped continue; else we're done. */
2997 while (p > bol && *(--p) == '\\')
2998 backslash = !backslash;
2999 if (!backslash)
3000 break;
3001 ++eol;
3002 }
3003
3004 /* Overwrite the newline char. */
3005 *eol = '\0';
3006 ebuf->bufnext = eol+1;
3007#ifdef CONFIG_WITH_VALUE_LENGTH
3008 ebuf->eol = eol;
3009#endif
3010
3011 return 0;
3012}
3013
3014static long
3015readline (struct ebuffer *ebuf)
3016{
3017 char *p;
3018 char *end;
3019 char *start;
3020 long nlines = 0;
3021
3022 /* The behaviors between string and stream buffers are different enough to
3023 warrant different functions. Do the Right Thing. */
3024
3025 if (!ebuf->fp)
3026 return readstring (ebuf);
3027
3028 /* When reading from a file, we always start over at the beginning of the
3029 buffer for each new line. */
3030
3031 p = start = ebuf->bufstart;
3032 end = p + ebuf->size;
3033 *p = '\0';
3034#ifdef CONFIG_WITH_VALUE_LENGTH
3035 ebuf->eol = p;
3036#endif
3037
3038 while (fgets (p, end - p, ebuf->fp) != 0)
3039 {
3040 char *p2;
3041 unsigned long len;
3042 int backslash;
3043
3044 len = strlen (p);
3045 if (len == 0)
3046 {
3047 /* This only happens when the first thing on the line is a '\0'.
3048 It is a pretty hopeless case, but (wonder of wonders) Athena
3049 lossage strikes again! (xmkmf puts NULs in its makefiles.)
3050 There is nothing really to be done; we synthesize a newline so
3051 the following line doesn't appear to be part of this line. */
3052 error (&ebuf->floc,
3053 _("warning: NUL character seen; rest of line ignored"));
3054 p[0] = '\n';
3055 len = 1;
3056 }
3057
3058 /* Jump past the text we just read. */
3059 p += len;
3060
3061 /* If the last char isn't a newline, the whole line didn't fit into the
3062 buffer. Get some more buffer and try again. */
3063 if (p[-1] != '\n')
3064 goto more_buffer;
3065
3066 /* We got a newline, so add one to the count of lines. */
3067 ++nlines;
3068
3069#if !defined(WINDOWS32) && !defined(__MSDOS__) && !defined(__EMX__)
3070 /* Check to see if the line was really ended with CRLF; if so ignore
3071 the CR. */
3072 if ((p - start) > 1 && p[-2] == '\r')
3073 {
3074 --p;
3075 p[-1] = '\n';
3076 }
3077#endif
3078
3079 backslash = 0;
3080 for (p2 = p - 2; p2 >= start; --p2)
3081 {
3082 if (*p2 != '\\')
3083 break;
3084 backslash = !backslash;
3085 }
3086
3087 if (!backslash)
3088 {
3089 p[-1] = '\0';
3090#ifdef CONFIG_WITH_VALUE_LENGTH
3091 ebuf->eol = p - 1;
3092#endif
3093 break;
3094 }
3095
3096 /* It was a backslash/newline combo. If we have more space, read
3097 another line. */
3098 if (end - p >= 80)
3099 {
3100#ifdef CONFIG_WITH_VALUE_LENGTH
3101 ebuf->eol = p;
3102#endif
3103 continue;
3104 }
3105
3106 /* We need more space at the end of our buffer, so realloc it.
3107 Make sure to preserve the current offset of p. */
3108 more_buffer:
3109 {
3110 unsigned long off = p - start;
3111 ebuf->size *= 2;
3112 start = ebuf->buffer = ebuf->bufstart = xrealloc (start, ebuf->size);
3113 p = start + off;
3114 end = start + ebuf->size;
3115 *p = '\0';
3116#ifdef CONFIG_WITH_VALUE_LENGTH
3117 ebuf->eol = p;
3118#endif
3119 }
3120 }
3121
3122 if (ferror (ebuf->fp))
3123 pfatal_with_name (ebuf->floc.filenm);
3124
3125 /* If we found some lines, return how many.
3126 If we didn't, but we did find _something_, that indicates we read the last
3127 line of a file with no final newline; return 1.
3128 If we read nothing, we're at EOF; return -1. */
3129
3130 return nlines ? nlines : p == ebuf->bufstart ? -1 : 1;
3131}
3132
3133
3134/* Parse the next "makefile word" from the input buffer, and return info
3135 about it.
3136
3137 A "makefile word" is one of:
3138
3139 w_bogus Should never happen
3140 w_eol End of input
3141 w_static A static word; cannot be expanded
3142 w_variable A word containing one or more variables/functions
3143 w_colon A colon
3144 w_dcolon A double-colon
3145 w_semicolon A semicolon
3146 w_varassign A variable assignment operator (=, :=, +=, >=, or ?=)
3147
3148 Note that this function is only used when reading certain parts of the
3149 makefile. Don't use it where special rules hold sway (RHS of a variable,
3150 in a command list, etc.) */
3151
3152static enum make_word_type
3153get_next_mword (char *buffer, char *delim, char **startp, unsigned int *length)
3154{
3155 enum make_word_type wtype = w_bogus;
3156 char *p = buffer, *beg;
3157 char c;
3158
3159 /* Skip any leading whitespace. */
3160 while (isblank ((unsigned char)*p))
3161 ++p;
3162
3163 beg = p;
3164 c = *(p++);
3165 switch (c)
3166 {
3167 case '\0':
3168 wtype = w_eol;
3169 break;
3170
3171 case ';':
3172 wtype = w_semicolon;
3173 break;
3174
3175 case '=':
3176 wtype = w_varassign;
3177 break;
3178
3179 case ':':
3180 wtype = w_colon;
3181 switch (*p)
3182 {
3183 case ':':
3184 ++p;
3185 wtype = w_dcolon;
3186 break;
3187
3188 case '=':
3189 ++p;
3190 wtype = w_varassign;
3191 break;
3192 }
3193 break;
3194
3195 case '+':
3196 case '?':
3197#ifdef CONFIG_WITH_PREPEND_ASSIGNMENT
3198 case '>':
3199#endif
3200 if (*p == '=')
3201 {
3202 ++p;
3203 wtype = w_varassign;
3204 break;
3205 }
3206
3207 default:
3208 if (delim && strchr (delim, c))
3209 wtype = w_static;
3210 break;
3211 }
3212
3213 /* Did we find something? If so, return now. */
3214 if (wtype != w_bogus)
3215 goto done;
3216
3217 /* This is some non-operator word. A word consists of the longest
3218 string of characters that doesn't contain whitespace, one of [:=#],
3219 or [?+]=, or one of the chars in the DELIM string. */
3220
3221 /* We start out assuming a static word; if we see a variable we'll
3222 adjust our assumptions then. */
3223 wtype = w_static;
3224
3225 /* We already found the first value of "c", above. */
3226 while (1)
3227 {
3228 char closeparen;
3229 int count;
3230
3231 switch (c)
3232 {
3233 case '\0':
3234 case ' ':
3235 case '\t':
3236 case '=':
3237 goto done_word;
3238
3239 case ':':
3240#ifdef HAVE_DOS_PATHS
3241 /* A word CAN include a colon in its drive spec. The drive
3242 spec is allowed either at the beginning of a word, or as part
3243 of the archive member name, like in "libfoo.a(d:/foo/bar.o)". */
3244 if (!(p - beg >= 2
3245 && (*p == '/' || *p == '\\') && isalpha ((unsigned char)p[-2])
3246 && (p - beg == 2 || p[-3] == '(')))
3247#endif
3248 goto done_word;
3249
3250 case '$':
3251 c = *(p++);
3252 if (c == '$')
3253 break;
3254
3255 /* This is a variable reference, so note that it's expandable.
3256 Then read it to the matching close paren. */
3257 wtype = w_variable;
3258
3259 if (c == '(')
3260 closeparen = ')';
3261 else if (c == '{')
3262 closeparen = '}';
3263 else
3264 /* This is a single-letter variable reference. */
3265 break;
3266
3267 for (count=0; *p != '\0'; ++p)
3268 {
3269 if (*p == c)
3270 ++count;
3271 else if (*p == closeparen && --count < 0)
3272 {
3273 ++p;
3274 break;
3275 }
3276 }
3277 break;
3278
3279 case '?':
3280 case '+':
3281#ifdef CONFIG_WITH_PREPEND_ASSIGNMENT
3282 case '>':
3283#endif
3284 if (*p == '=')
3285 goto done_word;
3286 break;
3287
3288 case '\\':
3289 switch (*p)
3290 {
3291 case ':':
3292 case ';':
3293 case '=':
3294 case '\\':
3295 ++p;
3296 break;
3297 }
3298 break;
3299
3300 default:
3301 if (delim && strchr (delim, c))
3302 goto done_word;
3303 break;
3304 }
3305
3306 c = *(p++);
3307 }
3308 done_word:
3309 --p;
3310
3311 done:
3312 if (startp)
3313 *startp = beg;
3314 if (length)
3315 *length = p - beg;
3316 return wtype;
3317}
3318
3319
3320/* Construct the list of include directories
3321 from the arguments and the default list. */
3322
3323void
3324construct_include_path (const char **arg_dirs)
3325{
3326#ifdef VAXC /* just don't ask ... */
3327 stat_t stbuf;
3328#else
3329 struct stat stbuf;
3330#endif
3331 const char **dirs;
3332 const char **cpp;
3333 unsigned int idx;
3334
3335 /* Compute the number of pointers we need in the table. */
3336 idx = sizeof (default_include_directories) / sizeof (const char *);
3337 if (arg_dirs)
3338 for (cpp = arg_dirs; *cpp != 0; ++cpp)
3339 ++idx;
3340
3341#ifdef __MSDOS__
3342 /* Add one for $DJDIR. */
3343 ++idx;
3344#endif
3345#ifdef KMK
3346 /* Add one for the kBuild directory. */
3347 ++idx;
3348#endif
3349
3350 dirs = xmalloc (idx * sizeof (const char *));
3351
3352 idx = 0;
3353 max_incl_len = 0;
3354
3355 /* First consider any dirs specified with -I switches.
3356 Ignore any that don't exist. Remember the maximum string length. */
3357
3358 if (arg_dirs)
3359 while (*arg_dirs != 0)
3360 {
3361 const char *dir = *(arg_dirs++);
3362 char *expanded = 0;
3363 int e;
3364
3365 if (dir[0] == '~')
3366 {
3367 expanded = tilde_expand (dir);
3368 if (expanded != 0)
3369 dir = expanded;
3370 }
3371
3372 EINTRLOOP (e, stat (dir, &stbuf));
3373 if (e == 0 && S_ISDIR (stbuf.st_mode))
3374 {
3375 unsigned int len = strlen (dir);
3376 /* If dir name is written with trailing slashes, discard them. */
3377 while (len > 1 && dir[len - 1] == '/')
3378 --len;
3379 if (len > max_incl_len)
3380 max_incl_len = len;
3381 dirs[idx++] = strcache_add_len (dir, len);
3382 }
3383
3384 if (expanded)
3385 free (expanded);
3386 }
3387
3388 /* Now add the standard default dirs at the end. */
3389
3390#ifdef __MSDOS__
3391 {
3392 /* The environment variable $DJDIR holds the root of the DJGPP directory
3393 tree; add ${DJDIR}/include. */
3394 struct variable *djdir = lookup_variable ("DJDIR", 5);
3395
3396 if (djdir)
3397 {
3398 unsigned int len = strlen (djdir->value) + 8;
3399 char *defdir = alloca (len + 1);
3400
3401 strcat (strcpy (defdir, djdir->value), "/include");
3402 dirs[idx++] = strcache_add (defdir);
3403
3404 if (len > max_incl_len)
3405 max_incl_len = len;
3406 }
3407 }
3408#endif
3409#ifdef KMK
3410 /* Add $(KBUILD_PATH). */
3411 {
3412 size_t len = strlen (get_kbuild_path ());
3413 dirs[idx++] = strcache_add_len (get_kbuild_path (), len);
3414 if (len > max_incl_len)
3415 max_incl_len = len;
3416 }
3417#endif
3418
3419 for (cpp = default_include_directories; *cpp != 0; ++cpp)
3420 {
3421 int e;
3422
3423 EINTRLOOP (e, stat (*cpp, &stbuf));
3424 if (e == 0 && S_ISDIR (stbuf.st_mode))
3425 {
3426 unsigned int len = strlen (*cpp);
3427 /* If dir name is written with trailing slashes, discard them. */
3428 while (len > 1 && (*cpp)[len - 1] == '/')
3429 --len;
3430 if (len > max_incl_len)
3431 max_incl_len = len;
3432 dirs[idx++] = strcache_add_len (*cpp, len - 1);
3433 }
3434 }
3435
3436 dirs[idx] = 0;
3437
3438 /* Now add each dir to the .INCLUDE_DIRS variable. */
3439
3440 for (cpp = dirs; *cpp != 0; ++cpp)
3441 do_variable_definition (NILF, ".INCLUDE_DIRS", *cpp,
3442 o_default, f_append, 0);
3443
3444 include_directories = dirs;
3445}
3446
3447
3448/* Expand ~ or ~USER at the beginning of NAME.
3449 Return a newly malloc'd string or 0. */
3450
3451char *
3452tilde_expand (const char *name)
3453{
3454#ifndef VMS
3455 if (name[1] == '/' || name[1] == '\0')
3456 {
3457 extern char *getenv ();
3458 char *home_dir;
3459 int is_variable;
3460
3461 {
3462 /* Turn off --warn-undefined-variables while we expand HOME. */
3463 int save = warn_undefined_variables_flag;
3464 warn_undefined_variables_flag = 0;
3465
3466#ifndef CONFIG_WITH_VALUE_LENGTH
3467 home_dir = allocated_variable_expand ("$(HOME)");
3468#else
3469 home_dir = allocated_variable_expand_2 (STRING_SIZE_TUPLE("$(HOME)"), NULL);
3470#endif
3471
3472 warn_undefined_variables_flag = save;
3473 }
3474
3475 is_variable = home_dir[0] != '\0';
3476 if (!is_variable)
3477 {
3478 free (home_dir);
3479 home_dir = getenv ("HOME");
3480 }
3481# if !defined(_AMIGA) && !defined(WINDOWS32)
3482 if (home_dir == 0 || home_dir[0] == '\0')
3483 {
3484 extern char *getlogin ();
3485 char *logname = getlogin ();
3486 home_dir = 0;
3487 if (logname != 0)
3488 {
3489 struct passwd *p = getpwnam (logname);
3490 if (p != 0)
3491 home_dir = p->pw_dir;
3492 }
3493 }
3494# endif /* !AMIGA && !WINDOWS32 */
3495 if (home_dir != 0)
3496 {
3497 char *new = xstrdup (concat (home_dir, "", name + 1));
3498 if (is_variable)
3499 free (home_dir);
3500 return new;
3501 }
3502 }
3503# if !defined(_AMIGA) && !defined(WINDOWS32)
3504 else
3505 {
3506 struct passwd *pwent;
3507 char *userend = strchr (name + 1, '/');
3508 if (userend != 0)
3509 *userend = '\0';
3510 pwent = getpwnam (name + 1);
3511 if (pwent != 0)
3512 {
3513 if (userend == 0)
3514 return xstrdup (pwent->pw_dir);
3515 else
3516 return xstrdup (concat (pwent->pw_dir, "/", userend + 1));
3517 }
3518 else if (userend != 0)
3519 *userend = '/';
3520 }
3521# endif /* !AMIGA && !WINDOWS32 */
3522#endif /* !VMS */
3523 return 0;
3524}
3525
3526/* Given a chain of struct nameseq's describing a sequence of filenames,
3527 in reverse of the intended order, return a new chain describing the
3528 result of globbing the filenames. The new chain is in forward order.
3529 The links of the old chain are freed or used in the new chain.
3530 Likewise for the names in the old chain.
3531
3532 SIZE is how big to construct chain elements.
3533 This is useful if we want them actually to be other structures
3534 that have room for additional info. */
3535
3536struct nameseq *
3537multi_glob (struct nameseq *chain, unsigned int size)
3538{
3539 void dir_setup_glob (glob_t *);
3540 struct nameseq *new = 0;
3541 struct nameseq *old;
3542 struct nameseq *nexto;
3543 glob_t gl;
3544#if defined(KMK) || defined(__EMX__) /* speed optimization */
3545 int rc;
3546#endif
3547
3548 dir_setup_glob (&gl);
3549
3550 for (old = chain; old != 0; old = nexto)
3551 {
3552 const char *gname;
3553#ifndef NO_ARCHIVES
3554 char *arname = 0;
3555 char *memname = 0;
3556#endif
3557 nexto = old->next;
3558 gname = old->name;
3559
3560 if (gname[0] == '~')
3561 {
3562 char *newname = tilde_expand (old->name);
3563 if (newname != 0)
3564 gname = newname;
3565 }
3566
3567#ifndef NO_ARCHIVES
3568 if (ar_name (gname))
3569 {
3570 /* OLD->name is an archive member reference. Replace it with the
3571 archive file name, and save the member name in MEMNAME. We will
3572 glob on the archive name and then reattach MEMNAME later. */
3573 ar_parse_name (gname, &arname, &memname);
3574 gname = arname;
3575 }
3576#endif /* !NO_ARCHIVES */
3577
3578#if defined(KMK) || defined(__EMX__) /* speed optimization */
3579 if (!strpbrk(gname, "*?["))
3580 {
3581 gl.gl_pathc = 1;
3582 gl.gl_pathv = (char **)&gname;
3583 rc = 0;
3584 }
3585 else
3586 rc = glob (gname, GLOB_NOCHECK|GLOB_ALTDIRFUNC, NULL, &gl);
3587 switch (rc)
3588#else
3589 switch (glob (gname, GLOB_NOCHECK|GLOB_ALTDIRFUNC, NULL, &gl))
3590#endif
3591 {
3592 case 0: /* Success. */
3593 {
3594 int i = gl.gl_pathc;
3595 while (i-- > 0)
3596 {
3597#ifndef NO_ARCHIVES
3598 if (memname != 0)
3599 {
3600 /* Try to glob on MEMNAME within the archive. */
3601 struct nameseq *found
3602 = ar_glob (gl.gl_pathv[i], memname, size);
3603 if (! found)
3604 {
3605 /* No matches. Use MEMNAME as-is. */
3606 unsigned int alen = strlen (gl.gl_pathv[i]);
3607 unsigned int mlen = strlen (memname);
3608 char *name;
3609 struct nameseq *elt = xmalloc (size);
3610 memset (elt, '\0', size);
3611
3612 name = alloca (alen + 1 + mlen + 2);
3613 memcpy (name, gl.gl_pathv[i], alen);
3614 name[alen] = '(';
3615 memcpy (name+alen+1, memname, mlen);
3616 name[alen + 1 + mlen] = ')';
3617 name[alen + 1 + mlen + 1] = '\0';
3618 elt->name = strcache_add (name);
3619 elt->next = new;
3620 new = elt;
3621 }
3622 else
3623 {
3624 /* Find the end of the FOUND chain. */
3625 struct nameseq *f = found;
3626 while (f->next != 0)
3627 f = f->next;
3628
3629 /* Attach the chain being built to the end of the FOUND
3630 chain, and make FOUND the new NEW chain. */
3631 f->next = new;
3632 new = found;
3633 }
3634 }
3635 else
3636#endif /* !NO_ARCHIVES */
3637 {
3638 struct nameseq *elt = xmalloc (size);
3639 memset (elt, '\0', size);
3640 elt->name = strcache_add (gl.gl_pathv[i]);
3641 elt->next = new;
3642 new = elt;
3643 }
3644 }
3645#if defined(KMK) || defined(__EMX__) /* speed optimization */
3646 if (gl.gl_pathv != (char **)&gname)
3647#endif
3648 globfree (&gl);
3649 free (old);
3650 break;
3651 }
3652
3653 case GLOB_NOSPACE:
3654 fatal (NILF, _("virtual memory exhausted"));
3655 break;
3656
3657 default:
3658 old->next = new;
3659 new = old;
3660 break;
3661 }
3662
3663#ifndef NO_ARCHIVES
3664 if (arname)
3665 free (arname);
3666#endif
3667 }
3668
3669 return new;
3670}
3671
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