1 /* $NetBSD: parse.c,v 1.217 2016/12/09 22:56:21 sjg Exp $ */
4 * Copyright (c) 1988, 1989, 1990, 1993
5 * The Regents of the University of California. All rights reserved.
7 * This code is derived from software contributed to Berkeley by
10 * Redistribution and use in source and binary forms, with or without
11 * modification, are permitted provided that the following conditions
13 * 1. Redistributions of source code must retain the above copyright
14 * notice, this list of conditions and the following disclaimer.
15 * 2. Redistributions in binary form must reproduce the above copyright
16 * notice, this list of conditions and the following disclaimer in the
17 * documentation and/or other materials provided with the distribution.
18 * 3. Neither the name of the University nor the names of its contributors
19 * may be used to endorse or promote products derived from this software
20 * without specific prior written permission.
22 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
23 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
24 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
25 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
26 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
27 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
28 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
29 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
30 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
31 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
36 * Copyright (c) 1989 by Berkeley Softworks
37 * All rights reserved.
39 * This code is derived from software contributed to Berkeley by
42 * Redistribution and use in source and binary forms, with or without
43 * modification, are permitted provided that the following conditions
45 * 1. Redistributions of source code must retain the above copyright
46 * notice, this list of conditions and the following disclaimer.
47 * 2. Redistributions in binary form must reproduce the above copyright
48 * notice, this list of conditions and the following disclaimer in the
49 * documentation and/or other materials provided with the distribution.
50 * 3. All advertising materials mentioning features or use of this software
51 * must display the following acknowledgement:
52 * This product includes software developed by the University of
53 * California, Berkeley and its contributors.
54 * 4. Neither the name of the University nor the names of its contributors
55 * may be used to endorse or promote products derived from this software
56 * without specific prior written permission.
58 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
59 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
60 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
61 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
62 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
63 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
64 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
65 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
66 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
67 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
72 static char rcsid
[] = "$NetBSD: parse.c,v 1.217 2016/12/09 22:56:21 sjg Exp $";
74 #include <sys/cdefs.h>
77 static char sccsid
[] = "@(#)parse.c 8.3 (Berkeley) 3/19/94";
79 __RCSID("$NetBSD: parse.c,v 1.217 2016/12/09 22:56:21 sjg Exp $");
86 * Functions to parse a makefile.
88 * One function, Parse_Init, must be called before any functions
89 * in this module are used. After that, the function Parse_File is the
90 * main entry point and controls most of the other functions in this
93 * Most important structures are kept in Lsts. Directories for
94 * the .include "..." function are kept in the 'parseIncPath' Lst, while
95 * those for the .include <...> are kept in the 'sysIncPath' Lst. The
96 * targets currently being defined are kept in the 'targets' Lst.
98 * The variables 'fname' and 'lineno' are used to track the name
99 * of the current file and the line number in that file so that error
100 * messages can be more meaningful.
103 * Parse_Init Initialization function which must be
104 * called before anything else in this module
107 * Parse_End Cleanup the module
109 * Parse_File Function used to parse a makefile. It must
110 * be given the name of the file, which should
111 * already have been opened, and a function
112 * to call to read a character from the file.
114 * Parse_IsVar Returns TRUE if the given line is a
115 * variable assignment. Used by MainParseArgs
116 * to determine if an argument is a target
117 * or a variable assignment. Used internally
118 * for pretty much the same thing...
120 * Parse_Error Function called when an error occurs in
121 * parsing. Used by the variable and
122 * conditional modules.
123 * Parse_MainName Returns a Lst of the main target to create.
126 #include <sys/types.h>
127 #include <sys/stat.h>
139 #include "pathnames.h"
142 #include <sys/mman.h>
145 #define MAP_COPY MAP_PRIVATE
152 ////////////////////////////////////////////////////////////
153 // types and constants
156 * Structure for a file being read ("included file")
158 typedef struct IFile
{
159 char *fname
; /* name of file */
160 int lineno
; /* current line number in file */
161 int first_lineno
; /* line number of start of text */
162 int cond_depth
; /* 'if' nesting when file opened */
163 Boolean depending
; /* state of doing_depend on EOF */
164 char *P_str
; /* point to base of string buffer */
165 char *P_ptr
; /* point to next char of string buffer */
166 char *P_end
; /* point to the end of string buffer */
167 char *(*nextbuf
)(void *, size_t *); /* Function to get more data */
168 void *nextbuf_arg
; /* Opaque arg for nextbuf() */
169 struct loadedfile
*lf
; /* loadedfile object, if any */
174 * These values are returned by ParseEOF to tell Parse_File whether to
175 * CONTINUE parsing, i.e. it had only reached the end of an include file,
182 * Tokens for target attributes
186 Default
, /* .DEFAULT */
187 DeleteOnError
, /* .DELETE_ON_ERROR */
189 dotError
, /* .ERROR */
190 Ignore
, /* .IGNORE */
191 Includes
, /* .INCLUDES */
192 Interrupt
, /* .INTERRUPT */
195 MFlags
, /* .MFLAGS or .MAKEFLAGS */
196 Main
, /* .MAIN and we don't have anything user-specified to
198 NoExport
, /* .NOEXPORT */
199 NoMeta
, /* .NOMETA */
200 NoMetaCmp
, /* .NOMETA_CMP */
201 NoPath
, /* .NOPATH */
202 Not
, /* Not special */
203 NotParallel
, /* .NOTPARALLEL */
205 ExObjdir
, /* .OBJDIR */
207 Parallel
, /* .PARALLEL */
213 Precious
, /* .PRECIOUS */
214 ExShell
, /* .SHELL */
215 Silent
, /* .SILENT */
216 SingleShell
, /* .SINGLESHELL */
218 Suffixes
, /* .SUFFIXES */
220 Attribute
/* Generic attribute */
230 ////////////////////////////////////////////////////////////
234 * The main target to create. This is the first target on the first
235 * dependency line in the first makefile.
237 static GNode
*mainNode
;
239 ////////////////////////////////////////////////////////////
242 /* targets we're working on */
246 /* command lines for targets */
251 * specType contains the SPECial TYPE of the current target. It is
252 * Not if the target is unspecial. If it *is* special, however, the children
253 * are linked as children of the parent but not vice versa. This variable is
254 * set in ParseDoDependency
256 static ParseSpecial specType
;
259 * Predecessor node for handling .ORDER. Initialized to NULL when .ORDER
260 * seen, then set to each successive source on the line.
262 static GNode
*predecessor
;
264 ////////////////////////////////////////////////////////////
267 /* true if currently in a dependency line or its commands */
268 static Boolean inLine
;
270 /* number of fatal errors */
271 static int fatals
= 0;
274 * Variables for doing includes
277 /* current file being read */
278 static IFile
*curFile
;
280 /* stack of IFiles generated by .includes */
283 /* include paths (lists of directories) */
284 Lst parseIncPath
; /* dirs for "..." includes */
285 Lst sysIncPath
; /* dirs for <...> includes */
286 Lst defIncPath
; /* default for sysIncPath */
288 ////////////////////////////////////////////////////////////
292 * The parseKeywords table is searched using binary search when deciding
293 * if a target or source is special. The 'spec' field is the ParseSpecial
294 * type of the keyword ("Not" if the keyword isn't special as a target) while
295 * the 'op' field is the operator to apply to the list of targets if the
296 * keyword is used as a source ("0" if the keyword isn't special as a source)
298 static const struct {
299 const char *name
; /* Name of keyword */
300 ParseSpecial spec
; /* Type when used as a target */
301 int op
; /* Operator when used as a source */
302 } parseKeywords
[] = {
303 { ".BEGIN", Begin
, 0 },
304 { ".DEFAULT", Default
, 0 },
305 { ".DELETE_ON_ERROR", DeleteOnError
, 0 },
307 { ".ERROR", dotError
, 0 },
308 { ".EXEC", Attribute
, OP_EXEC
},
309 { ".IGNORE", Ignore
, OP_IGNORE
},
310 { ".INCLUDES", Includes
, 0 },
311 { ".INTERRUPT", Interrupt
, 0 },
312 { ".INVISIBLE", Attribute
, OP_INVISIBLE
},
313 { ".JOIN", Attribute
, OP_JOIN
},
314 { ".LIBS", Libs
, 0 },
315 { ".MADE", Attribute
, OP_MADE
},
316 { ".MAIN", Main
, 0 },
317 { ".MAKE", Attribute
, OP_MAKE
},
318 { ".MAKEFLAGS", MFlags
, 0 },
319 { ".META", Meta
, OP_META
},
320 { ".MFLAGS", MFlags
, 0 },
321 { ".NOMETA", NoMeta
, OP_NOMETA
},
322 { ".NOMETA_CMP", NoMetaCmp
, OP_NOMETA_CMP
},
323 { ".NOPATH", NoPath
, OP_NOPATH
},
324 { ".NOTMAIN", Attribute
, OP_NOTMAIN
},
325 { ".NOTPARALLEL", NotParallel
, 0 },
326 { ".NO_PARALLEL", NotParallel
, 0 },
327 { ".NULL", Null
, 0 },
328 { ".OBJDIR", ExObjdir
, 0 },
329 { ".OPTIONAL", Attribute
, OP_OPTIONAL
},
330 { ".ORDER", Order
, 0 },
331 { ".PARALLEL", Parallel
, 0 },
332 { ".PATH", ExPath
, 0 },
333 { ".PHONY", Phony
, OP_PHONY
},
335 { ".POSIX", Posix
, 0 },
337 { ".PRECIOUS", Precious
, OP_PRECIOUS
},
338 { ".RECURSIVE", Attribute
, OP_MAKE
},
339 { ".SHELL", ExShell
, 0 },
340 { ".SILENT", Silent
, OP_SILENT
},
341 { ".SINGLESHELL", SingleShell
, 0 },
342 { ".STALE", Stale
, 0 },
343 { ".SUFFIXES", Suffixes
, 0 },
344 { ".USE", Attribute
, OP_USE
},
345 { ".USEBEFORE", Attribute
, OP_USEBEFORE
},
346 { ".WAIT", Wait
, 0 },
349 ////////////////////////////////////////////////////////////
352 static int ParseIsEscaped(const char *, const char *);
353 static void ParseErrorInternal(const char *, size_t, int, const char *, ...)
354 MAKE_ATTR_PRINTFLIKE(4,5);
355 static void ParseVErrorInternal(FILE *, const char *, size_t, int, const char *, va_list)
356 MAKE_ATTR_PRINTFLIKE(5, 0);
357 static int ParseFindKeyword(const char *);
358 static int ParseLinkSrc(void *, void *);
359 static int ParseDoOp(void *, void *);
360 static void ParseDoSrc(int, const char *);
361 static int ParseFindMain(void *, void *);
362 static int ParseAddDir(void *, void *);
363 static int ParseClearPath(void *, void *);
364 static void ParseDoDependency(char *);
365 static int ParseAddCmd(void *, void *);
366 static void ParseHasCommands(void *);
367 static void ParseDoInclude(char *);
368 static void ParseSetParseFile(const char *);
369 static void ParseSetIncludedFile(void);
371 static void ParseTraditionalInclude(char *);
374 static void ParseGmakeExport(char *);
376 static int ParseEOF(void);
377 static char *ParseReadLine(void);
378 static void ParseFinishLine(void);
379 static void ParseMark(GNode
*);
381 ////////////////////////////////////////////////////////////
385 const char *path
; /* name, for error reports */
386 char *buf
; /* contents buffer */
387 size_t len
; /* length of contents */
388 size_t maplen
; /* length of mmap area, or 0 */
389 Boolean used
; /* XXX: have we used the data yet */
393 * Constructor/destructor for loadedfile
395 static struct loadedfile
*
396 loadedfile_create(const char *path
)
398 struct loadedfile
*lf
;
400 lf
= bmake_malloc(sizeof(*lf
));
401 lf
->path
= (path
== NULL
? "(stdin)" : path
);
410 loadedfile_destroy(struct loadedfile
*lf
)
412 if (lf
->buf
!= NULL
) {
413 if (lf
->maplen
> 0) {
415 munmap(lf
->buf
, lf
->maplen
);
425 * nextbuf() operation for loadedfile, as needed by the weird and twisted
426 * logic below. Once that's cleaned up, we can get rid of lf->used...
429 loadedfile_nextbuf(void *x
, size_t *len
)
431 struct loadedfile
*lf
= x
;
442 * Try to get the size of a file.
445 load_getsize(int fd
, size_t *ret
)
449 if (fstat(fd
, &st
) < 0) {
453 if (!S_ISREG(st
.st_mode
)) {
458 * st_size is an off_t, which is 64 bits signed; *ret is
459 * size_t, which might be 32 bits unsigned or 64 bits
460 * unsigned. Rather than being elaborate, just punt on
461 * files that are more than 2^31 bytes. We should never
462 * see a makefile that size in practice...
464 * While we're at it reject negative sizes too, just in case.
466 if (st
.st_size
< 0 || st
.st_size
> 0x7fffffff) {
470 *ret
= (size_t) st
.st_size
;
477 * Until the path search logic can be moved under here instead of
478 * being in the caller in another source file, we need to have the fd
479 * passed in already open. Bleh.
481 * If the path is NULL use stdin and (to insure against fd leaks)
482 * assert that the caller passed in -1.
484 static struct loadedfile
*
485 loadfile(const char *path
, int fd
)
487 struct loadedfile
*lf
;
494 lf
= loadedfile_create(path
);
501 fd
= open(path
, O_RDONLY
);
504 Error("%s: %s", path
, strerror(errno
));
511 if (load_getsize(fd
, &lf
->len
) == SUCCESS
) {
512 /* found a size, try mmap */
514 pagesize
= sysconf(_SC_PAGESIZE
);
521 /* round size up to a page */
522 lf
->maplen
= pagesize
* ((lf
->len
+ pagesize
- 1)/pagesize
);
525 * XXX hack for dealing with empty files; remove when
526 * we're no longer limited by interfacing to the old
527 * logic elsewhere in this file.
529 if (lf
->maplen
== 0) {
530 lf
->maplen
= pagesize
;
534 * FUTURE: remove PROT_WRITE when the parser no longer
535 * needs to scribble on the input.
537 lf
->buf
= mmap(NULL
, lf
->maplen
, PROT_READ
|PROT_WRITE
,
538 MAP_FILE
|MAP_COPY
, fd
, 0);
539 if (lf
->buf
!= MAP_FAILED
) {
541 if (lf
->len
== lf
->maplen
&& lf
->buf
[lf
->len
- 1] != '\n') {
542 char *b
= malloc(lf
->len
+ 1);
544 memcpy(b
, lf
->buf
, lf
->len
++);
545 munmap(lf
->buf
, lf
->maplen
);
553 /* cannot mmap; load the traditional way */
557 lf
->buf
= bmake_malloc(lf
->len
);
561 assert(bufpos
<= lf
->len
);
562 if (bufpos
== lf
->len
) {
564 lf
->buf
= bmake_realloc(lf
->buf
, lf
->len
);
566 result
= read(fd
, lf
->buf
+ bufpos
, lf
->len
- bufpos
);
568 Error("%s: read error: %s", path
, strerror(errno
));
576 assert(bufpos
<= lf
->len
);
579 /* truncate malloc region to actual length (maybe not useful) */
581 lf
->buf
= bmake_realloc(lf
->buf
, lf
->len
);
593 ////////////////////////////////////////////////////////////
597 *----------------------------------------------------------------------
599 * Check if the current character is escaped on the current line
602 * 0 if the character is not backslash escaped, 1 otherwise
606 *----------------------------------------------------------------------
609 ParseIsEscaped(const char *line
, const char *c
)
622 *----------------------------------------------------------------------
623 * ParseFindKeyword --
624 * Look in the table of keywords for one matching the given string.
630 * The index of the keyword, or -1 if it isn't there.
634 *----------------------------------------------------------------------
637 ParseFindKeyword(const char *str
)
643 end
= (sizeof(parseKeywords
)/sizeof(parseKeywords
[0])) - 1;
646 cur
= start
+ ((end
- start
) / 2);
647 diff
= strcmp(str
, parseKeywords
[cur
].name
);
651 } else if (diff
< 0) {
656 } while (start
<= end
);
661 * ParseVErrorInternal --
662 * Error message abort function for parsing. Prints out the context
663 * of the error (line number and file) as well as the message with
664 * two optional arguments.
670 * "fatals" is incremented if the level is PARSE_FATAL.
674 ParseVErrorInternal(FILE *f
, const char *cfname
, size_t clineno
, int type
,
675 const char *fmt
, va_list ap
)
677 static Boolean fatal_warning_error_printed
= FALSE
;
679 (void)fprintf(f
, "%s: ", progname
);
681 if (cfname
!= NULL
) {
682 (void)fprintf(f
, "\"");
683 if (*cfname
!= '/' && strcmp(cfname
, "(stdin)") != 0) {
688 * Nothing is more annoying than not knowing
689 * which Makefile is the culprit.
691 dir
= Var_Value(".PARSEDIR", VAR_GLOBAL
, &cp
);
692 if (dir
== NULL
|| *dir
== '\0' ||
693 (*dir
== '.' && dir
[1] == '\0'))
694 dir
= Var_Value(".CURDIR", VAR_GLOBAL
, &cp
);
698 (void)fprintf(f
, "%s/%s", dir
, cfname
);
700 (void)fprintf(f
, "%s", cfname
);
702 (void)fprintf(f
, "\" line %d: ", (int)clineno
);
704 if (type
== PARSE_WARNING
)
705 (void)fprintf(f
, "warning: ");
706 (void)vfprintf(f
, fmt
, ap
);
707 (void)fprintf(f
, "\n");
709 if (type
== PARSE_FATAL
|| parseWarnFatal
)
711 if (parseWarnFatal
&& !fatal_warning_error_printed
) {
712 Error("parsing warnings being treated as errors");
713 fatal_warning_error_printed
= TRUE
;
718 * ParseErrorInternal --
729 ParseErrorInternal(const char *cfname
, size_t clineno
, int type
,
730 const char *fmt
, ...)
735 (void)fflush(stdout
);
736 ParseVErrorInternal(stderr
, cfname
, clineno
, type
, fmt
, ap
);
739 if (debug_file
!= stderr
&& debug_file
!= stdout
) {
741 ParseVErrorInternal(debug_file
, cfname
, clineno
, type
, fmt
, ap
);
748 * External interface to ParseErrorInternal; uses the default filename
759 Parse_Error(int type
, const char *fmt
, ...)
765 if (curFile
== NULL
) {
769 fname
= curFile
->fname
;
770 lineno
= curFile
->lineno
;
774 (void)fflush(stdout
);
775 ParseVErrorInternal(stderr
, fname
, lineno
, type
, fmt
, ap
);
778 if (debug_file
!= stderr
&& debug_file
!= stdout
) {
780 ParseVErrorInternal(debug_file
, fname
, lineno
, type
, fmt
, ap
);
788 * Parse a .info .warning or .error directive
790 * The input is the line minus the ".". We substitute
791 * variables, print the message and exit(1) (for .error) or just print
792 * a warning if the directive is malformed.
795 ParseMessage(char *line
)
804 mtype
= PARSE_WARNING
;
810 Parse_Error(PARSE_WARNING
, "invalid syntax: \".%s\"", line
);
814 while (isalpha((unsigned char)*line
))
816 if (!isspace((unsigned char)*line
))
817 return FALSE
; /* not for us */
818 while (isspace((unsigned char)*line
))
821 line
= Var_Subst(NULL
, line
, VAR_CMD
, VARF_WANTRES
);
822 Parse_Error(mtype
, "%s", line
);
825 if (mtype
== PARSE_FATAL
) {
826 /* Terminate immediately. */
833 *---------------------------------------------------------------------
835 * Link the parent node to its new child. Used in a Lst_ForEach by
836 * ParseDoDependency. If the specType isn't 'Not', the parent
837 * isn't linked as a parent of the child.
840 * pgnp The parent node
841 * cgpn The child node
847 * New elements are added to the parents list of cgn and the
848 * children list of cgn. the unmade field of pgn is updated
849 * to reflect the additional child.
850 *---------------------------------------------------------------------
853 ParseLinkSrc(void *pgnp
, void *cgnp
)
855 GNode
*pgn
= (GNode
*)pgnp
;
856 GNode
*cgn
= (GNode
*)cgnp
;
858 if ((pgn
->type
& OP_DOUBLEDEP
) && !Lst_IsEmpty (pgn
->cohorts
))
859 pgn
= (GNode
*)Lst_Datum(Lst_Last(pgn
->cohorts
));
860 (void)Lst_AtEnd(pgn
->children
, cgn
);
862 (void)Lst_AtEnd(cgn
->parents
, pgn
);
865 fprintf(debug_file
, "# %s: added child %s - %s\n", __func__
,
866 pgn
->name
, cgn
->name
);
867 Targ_PrintNode(pgn
, 0);
868 Targ_PrintNode(cgn
, 0);
874 *---------------------------------------------------------------------
876 * Apply the parsed operator to the given target node. Used in a
877 * Lst_ForEach call by ParseDoDependency once all targets have
878 * been found and their operator parsed. If the previous and new
879 * operators are incompatible, a major error is taken.
882 * gnp The node to which the operator is to be applied
883 * opp The operator to apply
889 * The type field of the node is altered to reflect any new bits in
891 *---------------------------------------------------------------------
894 ParseDoOp(void *gnp
, void *opp
)
896 GNode
*gn
= (GNode
*)gnp
;
897 int op
= *(int *)opp
;
899 * If the dependency mask of the operator and the node don't match and
900 * the node has actually had an operator applied to it before, and
901 * the operator actually has some dependency information in it, complain.
903 if (((op
& OP_OPMASK
) != (gn
->type
& OP_OPMASK
)) &&
904 !OP_NOP(gn
->type
) && !OP_NOP(op
))
906 Parse_Error(PARSE_FATAL
, "Inconsistent operator for %s", gn
->name
);
910 if ((op
== OP_DOUBLEDEP
) && ((gn
->type
& OP_OPMASK
) == OP_DOUBLEDEP
)) {
912 * If the node was the object of a :: operator, we need to create a
913 * new instance of it for the children and commands on this dependency
914 * line. The new instance is placed on the 'cohorts' list of the
915 * initial one (note the initial one is not on its own cohorts list)
916 * and the new instance is linked to all parents of the initial
922 * Propagate copied bits to the initial node. They'll be propagated
923 * back to the rest of the cohorts later.
925 gn
->type
|= op
& ~OP_OPMASK
;
927 cohort
= Targ_FindNode(gn
->name
, TARG_NOHASH
);
931 * Make the cohort invisible as well to avoid duplicating it into
932 * other variables. True, parents of this target won't tend to do
933 * anything with their local variables, but better safe than
934 * sorry. (I think this is pointless now, since the relevant list
935 * traversals will no longer see this node anyway. -mycroft)
937 cohort
->type
= op
| OP_INVISIBLE
;
938 (void)Lst_AtEnd(gn
->cohorts
, cohort
);
939 cohort
->centurion
= gn
;
940 gn
->unmade_cohorts
+= 1;
941 snprintf(cohort
->cohort_num
, sizeof cohort
->cohort_num
, "#%d",
945 * We don't want to nuke any previous flags (whatever they were) so we
946 * just OR the new operator into the old
955 *---------------------------------------------------------------------
957 * Given the name of a source, figure out if it is an attribute
958 * and apply it to the targets if it is. Else decide if there is
959 * some attribute which should be applied *to* the source because
960 * of some special target and apply it if so. Otherwise, make the
961 * source be a child of the targets in the list 'targets'
964 * tOp operator (if any) from special targets
965 * src name of the source to handle
971 * Operator bits may be added to the list of targets or to the source.
972 * The targets may have a new source added to their lists of children.
973 *---------------------------------------------------------------------
976 ParseDoSrc(int tOp
, const char *src
)
979 static int wait_number
= 0;
982 if (*src
== '.' && isupper ((unsigned char)src
[1])) {
983 int keywd
= ParseFindKeyword(src
);
985 int op
= parseKeywords
[keywd
].op
;
987 Lst_ForEach(targets
, ParseDoOp
, &op
);
990 if (parseKeywords
[keywd
].spec
== Wait
) {
992 * We add a .WAIT node in the dependency list.
993 * After any dynamic dependencies (and filename globbing)
994 * have happened, it is given a dependency on the each
995 * previous child back to and previous .WAIT node.
996 * The next child won't be scheduled until the .WAIT node
998 * We give each .WAIT node a unique name (mainly for diag).
1000 snprintf(wait_src
, sizeof wait_src
, ".WAIT_%u", ++wait_number
);
1001 gn
= Targ_FindNode(wait_src
, TARG_NOHASH
);
1004 gn
->type
= OP_WAIT
| OP_PHONY
| OP_DEPENDS
| OP_NOTMAIN
;
1005 Lst_ForEach(targets
, ParseLinkSrc
, gn
);
1014 * If we have noted the existence of a .MAIN, it means we need
1015 * to add the sources of said target to the list of things
1016 * to create. The string 'src' is likely to be free, so we
1017 * must make a new copy of it. Note that this will only be
1018 * invoked if the user didn't specify a target on the command
1019 * line. This is to allow #ifmake's to succeed, or something...
1021 (void)Lst_AtEnd(create
, bmake_strdup(src
));
1023 * Add the name to the .TARGETS variable as well, so the user can
1024 * employ that, if desired.
1026 Var_Append(".TARGETS", src
, VAR_GLOBAL
);
1031 * Create proper predecessor/successor links between the previous
1032 * source and the current one.
1034 gn
= Targ_FindNode(src
, TARG_CREATE
);
1037 if (predecessor
!= NULL
) {
1038 (void)Lst_AtEnd(predecessor
->order_succ
, gn
);
1039 (void)Lst_AtEnd(gn
->order_pred
, predecessor
);
1041 fprintf(debug_file
, "# %s: added Order dependency %s - %s\n",
1042 __func__
, predecessor
->name
, gn
->name
);
1043 Targ_PrintNode(predecessor
, 0);
1044 Targ_PrintNode(gn
, 0);
1048 * The current source now becomes the predecessor for the next one.
1055 * If the source is not an attribute, we need to find/create
1056 * a node for it. After that we can apply any operator to it
1057 * from a special target or link it to its parents, as
1060 * In the case of a source that was the object of a :: operator,
1061 * the attribute is applied to all of its instances (as kept in
1062 * the 'cohorts' list of the node) or all the cohorts are linked
1063 * to all the targets.
1066 /* Find/create the 'src' node and attach to all targets */
1067 gn
= Targ_FindNode(src
, TARG_CREATE
);
1073 Lst_ForEach(targets
, ParseLinkSrc
, gn
);
1080 *-----------------------------------------------------------------------
1082 * Find a real target in the list and set it to be the main one.
1083 * Called by ParseDoDependency when a main target hasn't been found
1087 * gnp Node to examine
1090 * 0 if main not found yet, 1 if it is.
1093 * mainNode is changed and Targ_SetMain is called.
1095 *-----------------------------------------------------------------------
1098 ParseFindMain(void *gnp
, void *dummy
)
1100 GNode
*gn
= (GNode
*)gnp
;
1101 if ((gn
->type
& OP_NOTARGET
) == 0) {
1104 return (dummy
? 1 : 1);
1106 return (dummy
? 0 : 0);
1111 *-----------------------------------------------------------------------
1113 * Front-end for Dir_AddDir to make sure Lst_ForEach keeps going
1121 *-----------------------------------------------------------------------
1124 ParseAddDir(void *path
, void *name
)
1126 (void)Dir_AddDir((Lst
) path
, (char *)name
);
1131 *-----------------------------------------------------------------------
1133 * Front-end for Dir_ClearPath to make sure Lst_ForEach keeps going
1141 *-----------------------------------------------------------------------
1144 ParseClearPath(void *path
, void *dummy
)
1146 Dir_ClearPath((Lst
) path
);
1147 return(dummy
? 0 : 0);
1151 *---------------------------------------------------------------------
1152 * ParseDoDependency --
1153 * Parse the dependency line in line.
1156 * line the line to parse
1162 * The nodes of the sources are linked as children to the nodes of the
1163 * targets. Some nodes may be created.
1165 * We parse a dependency line by first extracting words from the line and
1166 * finding nodes in the list of all targets with that name. This is done
1167 * until a character is encountered which is an operator character. Currently
1168 * these are only ! and :. At this point the operator is parsed and the
1169 * pointer into the line advanced until the first source is encountered.
1170 * The parsed operator is applied to each node in the 'targets' list,
1171 * which is where the nodes found for the targets are kept, by means of
1172 * the ParseDoOp function.
1173 * The sources are read in much the same way as the targets were except
1174 * that now they are expanded using the wildcarding scheme of the C-Shell
1175 * and all instances of the resulting words in the list of all targets
1176 * are found. Each of the resulting nodes is then linked to each of the
1177 * targets as one of its children.
1178 * Certain targets are handled specially. These are the ones detailed
1179 * by the specType variable.
1180 * The storing of transformation rules is also taken care of here.
1181 * A target is recognized as a transformation rule by calling
1182 * Suff_IsTransform. If it is a transformation rule, its node is gotten
1183 * from the suffix module via Suff_AddTransform rather than the standard
1184 * Targ_FindNode in the target module.
1185 *---------------------------------------------------------------------
1188 ParseDoDependency(char *line
)
1190 char *cp
; /* our current position */
1191 GNode
*gn
= NULL
; /* a general purpose temporary node */
1192 int op
; /* the operator on the line */
1193 char savec
; /* a place to save a character */
1194 Lst paths
; /* List of search paths to alter when parsing
1195 * a list of .PATH targets */
1196 int tOp
; /* operator from special target */
1197 Lst sources
; /* list of archive source names after
1199 Lst curTargs
; /* list of target names to be found and added
1200 * to the targets list */
1201 char *lstart
= line
;
1204 fprintf(debug_file
, "ParseDoDependency(%s)\n", line
);
1210 curTargs
= Lst_Init(FALSE
);
1213 * First, grind through the targets.
1218 * Here LINE points to the beginning of the next word, and
1219 * LSTART points to the actual beginning of the line.
1222 /* Find the end of the next word. */
1223 for (cp
= line
; *cp
&& (ParseIsEscaped(lstart
, cp
) ||
1224 !(isspace((unsigned char)*cp
) ||
1225 *cp
== '!' || *cp
== ':' || *cp
== LPAREN
));
1229 * Must be a dynamic source (would have been expanded
1230 * otherwise), so call the Var module to parse the puppy
1231 * so we can safely advance beyond it...There should be
1232 * no errors in this, as they would have been discovered
1233 * in the initial Var_Subst and we wouldn't be here.
1238 (void)Var_Parse(cp
, VAR_CMD
, VARF_UNDEFERR
|VARF_WANTRES
,
1246 * If the word is followed by a left parenthesis, it's the
1247 * name of an object file inside an archive (ar file).
1249 if (!ParseIsEscaped(lstart
, cp
) && *cp
== LPAREN
) {
1251 * Archives must be handled specially to make sure the OP_ARCHV
1252 * flag is set in their 'type' field, for one thing, and because
1253 * things like "archive(file1.o file2.o file3.o)" are permissible.
1254 * Arch_ParseArchive will set 'line' to be the first non-blank
1255 * after the archive-spec. It creates/finds nodes for the members
1256 * and places them on the given list, returning SUCCESS if all
1257 * went well and FAILURE if there was an error in the
1258 * specification. On error, line should remain untouched.
1260 if (Arch_ParseArchive(&line
, targets
, VAR_CMD
) != SUCCESS
) {
1261 Parse_Error(PARSE_FATAL
,
1262 "Error in archive specification: \"%s\"", line
);
1265 /* Done with this word; on to the next. */
1273 * We got to the end of the line while we were still
1274 * looking at targets.
1276 * Ending a dependency line without an operator is a Bozo
1277 * no-no. As a heuristic, this is also often triggered by
1278 * undetected conflicts from cvs/rcs merges.
1280 if ((strncmp(line
, "<<<<<<", 6) == 0) ||
1281 (strncmp(line
, "======", 6) == 0) ||
1282 (strncmp(line
, ">>>>>>", 6) == 0))
1283 Parse_Error(PARSE_FATAL
,
1284 "Makefile appears to contain unresolved cvs/rcs/??? merge conflicts");
1286 Parse_Error(PARSE_FATAL
, lstart
[0] == '.' ? "Unknown directive"
1287 : "Need an operator");
1291 /* Insert a null terminator. */
1296 * Got the word. See if it's a special target and if so set
1297 * specType to match it.
1299 if (*line
== '.' && isupper ((unsigned char)line
[1])) {
1301 * See if the target is a special target that must have it
1302 * or its sources handled specially.
1304 int keywd
= ParseFindKeyword(line
);
1306 if (specType
== ExPath
&& parseKeywords
[keywd
].spec
!= ExPath
) {
1307 Parse_Error(PARSE_FATAL
, "Mismatched special targets");
1311 specType
= parseKeywords
[keywd
].spec
;
1312 tOp
= parseKeywords
[keywd
].op
;
1315 * Certain special targets have special semantics:
1316 * .PATH Have to set the dirSearchPath
1318 * .MAIN Its sources are only used if
1319 * nothing has been specified to
1321 * .DEFAULT Need to create a node to hang
1322 * commands on, but we don't want
1323 * it in the graph, nor do we want
1324 * it to be the Main Target, so we
1325 * create it, set OP_NOTMAIN and
1326 * add it to the list, setting
1327 * DEFAULT to the new node for
1328 * later use. We claim the node is
1329 * A transformation rule to make
1330 * life easier later, when we'll
1331 * use Make_HandleUse to actually
1332 * apply the .DEFAULT commands.
1333 * .PHONY The list of targets
1334 * .NOPATH Don't search for file in the path
1340 * .INTERRUPT Are not to be considered the
1342 * .NOTPARALLEL Make only one target at a time.
1343 * .SINGLESHELL Create a shell for each command.
1344 * .ORDER Must set initial predecessor to NULL
1348 if (paths
== NULL
) {
1349 paths
= Lst_Init(FALSE
);
1351 (void)Lst_AtEnd(paths
, dirSearchPath
);
1354 if (!Lst_IsEmpty(create
)) {
1363 gn
= Targ_FindNode(line
, TARG_CREATE
);
1366 gn
->type
|= OP_NOTMAIN
|OP_SPECIAL
;
1367 (void)Lst_AtEnd(targets
, gn
);
1370 gn
= Targ_NewGN(".DEFAULT");
1371 gn
->type
|= (OP_NOTMAIN
|OP_TRANSFORM
);
1372 (void)Lst_AtEnd(targets
, gn
);
1376 deleteOnError
= TRUE
;
1390 } else if (strncmp(line
, ".PATH", 5) == 0) {
1392 * .PATH<suffix> has to be handled specially.
1393 * Call on the suffix module to give us a path to
1399 path
= Suff_GetPath(&line
[5]);
1401 Parse_Error(PARSE_FATAL
,
1402 "Suffix '%s' not defined (yet)",
1406 if (paths
== NULL
) {
1407 paths
= Lst_Init(FALSE
);
1409 (void)Lst_AtEnd(paths
, path
);
1415 * Have word in line. Get or create its node and stick it at
1416 * the end of the targets list
1418 if ((specType
== Not
) && (*line
!= '\0')) {
1419 if (Dir_HasWildcards(line
)) {
1421 * Targets are to be sought only in the current directory,
1422 * so create an empty path for the thing. Note we need to
1423 * use Dir_Destroy in the destruction of the path as the
1424 * Dir module could have added a directory to the path...
1426 Lst emptyPath
= Lst_Init(FALSE
);
1428 Dir_Expand(line
, emptyPath
, curTargs
);
1430 Lst_Destroy(emptyPath
, Dir_Destroy
);
1433 * No wildcards, but we want to avoid code duplication,
1434 * so create a list with the word on it.
1436 (void)Lst_AtEnd(curTargs
, line
);
1439 /* Apply the targets. */
1441 while(!Lst_IsEmpty(curTargs
)) {
1442 char *targName
= (char *)Lst_DeQueue(curTargs
);
1444 if (!Suff_IsTransform (targName
)) {
1445 gn
= Targ_FindNode(targName
, TARG_CREATE
);
1447 gn
= Suff_AddTransform(targName
);
1452 (void)Lst_AtEnd(targets
, gn
);
1454 } else if (specType
== ExPath
&& *line
!= '.' && *line
!= '\0') {
1455 Parse_Error(PARSE_WARNING
, "Extra target (%s) ignored", line
);
1458 /* Don't need the inserted null terminator any more. */
1462 * If it is a special type and not .PATH, it's the only target we
1463 * allow on this line...
1465 if (specType
!= Not
&& specType
!= ExPath
) {
1466 Boolean warning
= FALSE
;
1468 while (*cp
&& (ParseIsEscaped(lstart
, cp
) ||
1469 ((*cp
!= '!') && (*cp
!= ':')))) {
1470 if (ParseIsEscaped(lstart
, cp
) ||
1471 (*cp
!= ' ' && *cp
!= '\t')) {
1477 Parse_Error(PARSE_WARNING
, "Extra target ignored");
1480 while (*cp
&& isspace ((unsigned char)*cp
)) {
1485 } while (*line
&& (ParseIsEscaped(lstart
, line
) ||
1486 ((*line
!= '!') && (*line
!= ':'))));
1489 * Don't need the list of target names anymore...
1491 Lst_Destroy(curTargs
, NULL
);
1494 if (!Lst_IsEmpty(targets
)) {
1497 Parse_Error(PARSE_WARNING
, "Special and mundane targets don't mix. Mundane ones ignored");
1506 * These four create nodes on which to hang commands, so
1507 * targets shouldn't be empty...
1511 * Nothing special here -- targets can be empty if it wants.
1518 * Have now parsed all the target names. Must parse the operator next. The
1519 * result is left in op .
1523 } else if (*cp
== ':') {
1531 Parse_Error(PARSE_FATAL
, lstart
[0] == '.' ? "Unknown directive"
1532 : "Missing dependency operator");
1536 /* Advance beyond the operator */
1540 * Apply the operator to the target. This is how we remember which
1541 * operator a target was defined with. It fails if the operator
1542 * used isn't consistent across all references.
1544 Lst_ForEach(targets
, ParseDoOp
, &op
);
1547 * Onward to the sources.
1549 * LINE will now point to the first source word, if any, or the
1550 * end of the string if not.
1552 while (*cp
&& isspace ((unsigned char)*cp
)) {
1558 * Several special targets take different actions if present with no
1560 * a .SUFFIXES line with no sources clears out all old suffixes
1561 * a .PRECIOUS line makes all targets precious
1562 * a .IGNORE line ignores errors for all targets
1563 * a .SILENT line creates silence when making all targets
1564 * a .PATH removes all directories from the search path(s).
1569 Suff_ClearSuffixes();
1575 ignoreErrors
= TRUE
;
1581 Lst_ForEach(paths
, ParseClearPath
, NULL
);
1586 Var_Set("%POSIX", "1003.2", VAR_GLOBAL
, 0);
1592 } else if (specType
== MFlags
) {
1594 * Call on functions in main.c to deal with these arguments and
1595 * set the initial character to a null-character so the loop to
1596 * get sources won't get anything
1598 Main_ParseArgLine(line
);
1600 } else if (specType
== ExShell
) {
1601 if (Job_ParseShell(line
) != SUCCESS
) {
1602 Parse_Error(PARSE_FATAL
, "improper shell specification");
1606 } else if ((specType
== NotParallel
) || (specType
== SingleShell
) ||
1607 (specType
== DeleteOnError
)) {
1612 * NOW GO FOR THE SOURCES
1614 if ((specType
== Suffixes
) || (specType
== ExPath
) ||
1615 (specType
== Includes
) || (specType
== Libs
) ||
1616 (specType
== Null
) || (specType
== ExObjdir
))
1620 * If the target was one that doesn't take files as its sources
1621 * but takes something like suffixes, we take each
1622 * space-separated word on the line as a something and deal
1623 * with it accordingly.
1625 * If the target was .SUFFIXES, we take each source as a
1626 * suffix and add it to the list of suffixes maintained by the
1629 * If the target was a .PATH, we add the source as a directory
1630 * to search on the search path.
1632 * If it was .INCLUDES, the source is taken to be the suffix of
1633 * files which will be #included and whose search path should
1634 * be present in the .INCLUDES variable.
1636 * If it was .LIBS, the source is taken to be the suffix of
1637 * files which are considered libraries and whose search path
1638 * should be present in the .LIBS variable.
1640 * If it was .NULL, the source is the suffix to use when a file
1641 * has no valid suffix.
1643 * If it was .OBJDIR, the source is a new definition for .OBJDIR,
1644 * and will cause make to do a new chdir to that path.
1646 while (*cp
&& !isspace ((unsigned char)*cp
)) {
1653 Suff_AddSuffix(line
, &mainNode
);
1656 Lst_ForEach(paths
, ParseAddDir
, line
);
1659 Suff_AddInclude(line
);
1668 Main_SetObjdir("%s", line
);
1674 if (savec
!= '\0') {
1677 while (*cp
&& isspace ((unsigned char)*cp
)) {
1683 Lst_Destroy(paths
, NULL
);
1685 if (specType
== ExPath
)
1690 * The targets take real sources, so we must beware of archive
1691 * specifications (i.e. things with left parentheses in them)
1692 * and handle them accordingly.
1694 for (; *cp
&& !isspace ((unsigned char)*cp
); cp
++) {
1695 if ((*cp
== LPAREN
) && (cp
> line
) && (cp
[-1] != '$')) {
1697 * Only stop for a left parenthesis if it isn't at the
1698 * start of a word (that'll be for variable changes
1699 * later) and isn't preceded by a dollar sign (a dynamic
1706 if (*cp
== LPAREN
) {
1707 sources
= Lst_Init(FALSE
);
1708 if (Arch_ParseArchive(&line
, sources
, VAR_CMD
) != SUCCESS
) {
1709 Parse_Error(PARSE_FATAL
,
1710 "Error in source archive spec \"%s\"", line
);
1714 while (!Lst_IsEmpty (sources
)) {
1715 gn
= (GNode
*)Lst_DeQueue(sources
);
1716 ParseDoSrc(tOp
, gn
->name
);
1718 Lst_Destroy(sources
, NULL
);
1726 ParseDoSrc(tOp
, line
);
1728 while (*cp
&& isspace ((unsigned char)*cp
)) {
1735 if (mainNode
== NULL
) {
1737 * If we have yet to decide on a main target to make, in the
1738 * absence of any user input, we want the first target on
1739 * the first dependency line that is actually a real target
1740 * (i.e. isn't a .USE or .EXEC rule) to be made.
1742 Lst_ForEach(targets
, ParseFindMain
, NULL
);
1747 Lst_Destroy(curTargs
, NULL
);
1751 *---------------------------------------------------------------------
1753 * Return TRUE if the passed line is a variable assignment. A variable
1754 * assignment consists of a single word followed by optional whitespace
1755 * followed by either a += or an = operator.
1756 * This function is used both by the Parse_File function and main when
1757 * parsing the command-line arguments.
1760 * line the line to check
1763 * TRUE if it is. FALSE if it ain't
1767 *---------------------------------------------------------------------
1770 Parse_IsVar(char *line
)
1772 Boolean wasSpace
= FALSE
; /* set TRUE if found a space */
1775 #define ISEQOPERATOR(c) \
1776 (((c) == '+') || ((c) == ':') || ((c) == '?') || ((c) == '!'))
1779 * Skip to variable name
1781 for (;(*line
== ' ') || (*line
== '\t'); line
++)
1784 /* Scan for one of the assignment operators outside a variable expansion */
1785 while ((ch
= *line
++) != 0) {
1786 if (ch
== '(' || ch
== '{') {
1790 if (ch
== ')' || ch
== '}') {
1796 while (ch
== ' ' || ch
== '\t') {
1801 if (ch
== ':' && strncmp(line
, "sh", 2) == 0) {
1808 if (*line
== '=' && ISEQOPERATOR(ch
))
1818 *---------------------------------------------------------------------
1820 * Take the variable assignment in the passed line and do it in the
1823 * Note: There is a lexical ambiguity with assignment modifier characters
1824 * in variable names. This routine interprets the character before the =
1825 * as a modifier. Therefore, an assignment like
1827 * is interpreted as "C+ +=" instead of "C++ =".
1830 * line a line guaranteed to be a variable assignment.
1831 * This reduces error checks
1832 * ctxt Context in which to do the assignment
1838 * the variable structure of the given variable name is altered in the
1840 *---------------------------------------------------------------------
1843 Parse_DoVar(char *line
, GNode
*ctxt
)
1845 char *cp
; /* pointer into line */
1847 VAR_SUBST
, VAR_APPEND
, VAR_SHELL
, VAR_NORMAL
1848 } type
; /* Type of assignment */
1849 char *opc
; /* ptr to operator character to
1850 * null-terminate the variable name */
1851 Boolean freeCp
= FALSE
; /* TRUE if cp needs to be freed,
1852 * i.e. if any variable expansion was
1857 * Skip to variable name
1859 while ((*line
== ' ') || (*line
== '\t')) {
1864 * Skip to operator character, nulling out whitespace as we go
1865 * XXX Rather than counting () and {} we should look for $ and
1866 * then expand the variable.
1868 for (depth
= 0, cp
= line
+ 1; depth
> 0 || *cp
!= '='; cp
++) {
1869 if (*cp
== '(' || *cp
== '{') {
1873 if (*cp
== ')' || *cp
== '}') {
1877 if (depth
== 0 && isspace ((unsigned char)*cp
)) {
1881 opc
= cp
-1; /* operator is the previous character */
1882 *cp
++ = '\0'; /* nuke the = */
1885 * Check operator type
1895 * If the variable already has a value, we don't do anything.
1898 if (Var_Exists(line
, ctxt
)) {
1917 while (opc
> line
&& *opc
!= ':')
1920 if (strncmp(opc
, ":sh", 3) == 0) {
1930 while (isspace ((unsigned char)*cp
)) {
1934 if (type
== VAR_APPEND
) {
1935 Var_Append(line
, cp
, ctxt
);
1936 } else if (type
== VAR_SUBST
) {
1938 * Allow variables in the old value to be undefined, but leave their
1939 * invocation alone -- this is done by forcing oldVars to be false.
1940 * XXX: This can cause recursive variables, but that's not hard to do,
1941 * and this allows someone to do something like
1943 * CFLAGS = $(.INCLUDES)
1944 * CFLAGS := -I.. $(CFLAGS)
1946 * And not get an error.
1948 Boolean oldOldVars
= oldVars
;
1953 * make sure that we set the variable the first time to nothing
1954 * so that it gets substituted!
1956 if (!Var_Exists(line
, ctxt
))
1957 Var_Set(line
, "", ctxt
, 0);
1959 cp
= Var_Subst(NULL
, cp
, ctxt
, VARF_WANTRES
|VARF_ASSIGN
);
1960 oldVars
= oldOldVars
;
1963 Var_Set(line
, cp
, ctxt
, 0);
1964 } else if (type
== VAR_SHELL
) {
1968 if (strchr(cp
, '$') != NULL
) {
1970 * There's a dollar sign in the command, so perform variable
1971 * expansion on the whole thing. The resulting string will need
1972 * freeing when we're done, so set freeCmd to TRUE.
1974 cp
= Var_Subst(NULL
, cp
, VAR_CMD
, VARF_UNDEFERR
|VARF_WANTRES
);
1978 res
= Cmd_Exec(cp
, &error
);
1979 Var_Set(line
, res
, ctxt
, 0);
1983 Parse_Error(PARSE_WARNING
, error
, cp
);
1986 * Normal assignment -- just do it.
1988 Var_Set(line
, cp
, ctxt
, 0);
1990 if (strcmp(line
, MAKEOVERRIDES
) == 0)
1991 Main_ExportMAKEFLAGS(FALSE
); /* re-export MAKEFLAGS */
1992 else if (strcmp(line
, ".CURDIR") == 0) {
1994 * Somone is being (too?) clever...
1995 * Let's pretend they know what they are doing and
1996 * re-initialize the 'cur' Path.
2000 } else if (strcmp(line
, MAKE_JOB_PREFIX
) == 0) {
2002 } else if (strcmp(line
, MAKE_EXPORTED
) == 0) {
2011 * ParseMaybeSubMake --
2012 * Scan the command string to see if it a possible submake node
2014 * cmd the command to scan
2016 * TRUE if the command is possibly a submake, FALSE if not.
2019 ParseMaybeSubMake(const char *cmd
)
2026 #define MKV(A) { A, sizeof(A) - 1 }
2033 for (i
= 0; i
< sizeof(vals
)/sizeof(vals
[0]); i
++) {
2035 if ((ptr
= strstr(cmd
, vals
[i
].name
)) == NULL
)
2037 if ((ptr
== cmd
|| !isalnum((unsigned char)ptr
[-1]))
2038 && !isalnum((unsigned char)ptr
[vals
[i
].len
]))
2046 * Lst_ForEach function to add a command line to all targets
2049 * gnp the node to which the command is to be added
2050 * cmd the command to add
2056 * A new element is added to the commands list of the node,
2057 * and the node can be marked as a submake node if the command is
2058 * determined to be that.
2061 ParseAddCmd(void *gnp
, void *cmd
)
2063 GNode
*gn
= (GNode
*)gnp
;
2065 /* Add to last (ie current) cohort for :: targets */
2066 if ((gn
->type
& OP_DOUBLEDEP
) && !Lst_IsEmpty (gn
->cohorts
))
2067 gn
= (GNode
*)Lst_Datum(Lst_Last(gn
->cohorts
));
2069 /* if target already supplied, ignore commands */
2070 if (!(gn
->type
& OP_HAS_COMMANDS
)) {
2071 (void)Lst_AtEnd(gn
->commands
, cmd
);
2072 if (ParseMaybeSubMake(cmd
))
2073 gn
->type
|= OP_SUBMAKE
;
2077 /* XXX: We cannot do this until we fix the tree */
2078 (void)Lst_AtEnd(gn
->commands
, cmd
);
2079 Parse_Error(PARSE_WARNING
,
2080 "overriding commands for target \"%s\"; "
2081 "previous commands defined at %s: %d ignored",
2082 gn
->name
, gn
->fname
, gn
->lineno
);
2084 Parse_Error(PARSE_WARNING
,
2085 "duplicate script for target \"%s\" ignored",
2087 ParseErrorInternal(gn
->fname
, gn
->lineno
, PARSE_WARNING
,
2088 "using previous script for \"%s\" defined here",
2096 *-----------------------------------------------------------------------
2097 * ParseHasCommands --
2098 * Callback procedure for Parse_File when destroying the list of
2099 * targets on the last dependency line. Marks a target as already
2100 * having commands if it does, to keep from having shell commands
2101 * on multiple dependency lines.
2104 * gnp Node to examine
2110 * OP_HAS_COMMANDS may be set for the target.
2112 *-----------------------------------------------------------------------
2115 ParseHasCommands(void *gnp
)
2117 GNode
*gn
= (GNode
*)gnp
;
2118 if (!Lst_IsEmpty(gn
->commands
)) {
2119 gn
->type
|= OP_HAS_COMMANDS
;
2124 *-----------------------------------------------------------------------
2125 * Parse_AddIncludeDir --
2126 * Add a directory to the path searched for included makefiles
2127 * bracketed by double-quotes. Used by functions in main.c
2130 * dir The name of the directory to add
2136 * The directory is appended to the list.
2138 *-----------------------------------------------------------------------
2141 Parse_AddIncludeDir(char *dir
)
2143 (void)Dir_AddDir(parseIncPath
, dir
);
2147 *---------------------------------------------------------------------
2149 * Push to another file.
2151 * The input is the line minus the `.'. A file spec is a string
2152 * enclosed in <> or "". The former is looked for only in sysIncPath.
2153 * The latter in . and the directories specified by -I command line
2160 * A structure is added to the includes Lst and readProc, lineno,
2161 * fname and curFILE are altered for the new file
2162 *---------------------------------------------------------------------
2166 Parse_include_file(char *file
, Boolean isSystem
, Boolean depinc
, int silent
)
2168 struct loadedfile
*lf
;
2169 char *fullname
; /* full pathname of file */
2171 char *prefEnd
, *incdir
;
2176 * Now we know the file's name and its search path, we attempt to
2177 * find the durn thing. A return of NULL indicates the file don't
2180 fullname
= file
[0] == '/' ? bmake_strdup(file
) : NULL
;
2182 if (fullname
== NULL
&& !isSystem
) {
2184 * Include files contained in double-quotes are first searched for
2185 * relative to the including file's location. We don't want to
2186 * cd there, of course, so we just tack on the old file's
2187 * leading path components and call Dir_FindFile to see if
2188 * we can locate the beast.
2191 incdir
= bmake_strdup(curFile
->fname
);
2192 prefEnd
= strrchr(incdir
, '/');
2193 if (prefEnd
!= NULL
) {
2195 /* Now do lexical processing of leading "../" on the filename */
2196 for (i
= 0; strncmp(file
+ i
, "../", 3) == 0; i
+= 3) {
2197 prefEnd
= strrchr(incdir
+ 1, '/');
2198 if (prefEnd
== NULL
|| strcmp(prefEnd
, "/..") == 0)
2202 newName
= str_concat(incdir
, file
+ i
, STR_ADDSLASH
);
2203 fullname
= Dir_FindFile(newName
, parseIncPath
);
2204 if (fullname
== NULL
)
2205 fullname
= Dir_FindFile(newName
, dirSearchPath
);
2210 if (fullname
== NULL
) {
2212 * Makefile wasn't found in same directory as included makefile.
2213 * Search for it first on the -I search path,
2214 * then on the .PATH search path, if not found in a -I directory.
2215 * If we have a suffix specific path we should use that.
2218 Lst suffPath
= NULL
;
2220 if ((suff
= strrchr(file
, '.'))) {
2221 suffPath
= Suff_GetPath(suff
);
2222 if (suffPath
!= NULL
) {
2223 fullname
= Dir_FindFile(file
, suffPath
);
2226 if (fullname
== NULL
) {
2227 fullname
= Dir_FindFile(file
, parseIncPath
);
2228 if (fullname
== NULL
) {
2229 fullname
= Dir_FindFile(file
, dirSearchPath
);
2235 /* Looking for a system file or file still not found */
2236 if (fullname
== NULL
) {
2238 * Look for it on the system path
2240 fullname
= Dir_FindFile(file
,
2241 Lst_IsEmpty(sysIncPath
) ? defIncPath
: sysIncPath
);
2244 if (fullname
== NULL
) {
2246 Parse_Error(PARSE_FATAL
, "Could not find %s", file
);
2250 /* Actually open the file... */
2251 fd
= open(fullname
, O_RDONLY
);
2254 Parse_Error(PARSE_FATAL
, "Cannot open %s", fullname
);
2260 lf
= loadfile(fullname
, fd
);
2262 ParseSetIncludedFile();
2263 /* Start reading from this file next */
2264 Parse_SetInput(fullname
, 0, -1, loadedfile_nextbuf
, lf
);
2267 doing_depend
= depinc
; /* only turn it on */
2271 ParseDoInclude(char *line
)
2273 char endc
; /* the character which ends the file spec */
2274 char *cp
; /* current position in file spec */
2275 int silent
= (*line
!= 'i') ? 1 : 0;
2276 char *file
= &line
[7 + silent
];
2278 /* Skip to delimiter character so we know where to look */
2279 while (*file
== ' ' || *file
== '\t')
2282 if (*file
!= '"' && *file
!= '<') {
2283 Parse_Error(PARSE_FATAL
,
2284 ".include filename must be delimited by '\"' or '<'");
2289 * Set the search path on which to find the include file based on the
2290 * characters which bracket its name. Angle-brackets imply it's
2291 * a system Makefile while double-quotes imply it's a user makefile
2299 /* Skip to matching delimiter */
2300 for (cp
= ++file
; *cp
&& *cp
!= endc
; cp
++)
2304 Parse_Error(PARSE_FATAL
,
2305 "Unclosed %cinclude filename. '%c' expected",
2312 * Substitute for any variables in the file name before trying to
2315 file
= Var_Subst(NULL
, file
, VAR_CMD
, VARF_WANTRES
);
2317 Parse_include_file(file
, endc
== '>', (*line
== 'd'), silent
);
2323 *---------------------------------------------------------------------
2324 * ParseSetIncludedFile --
2325 * Set the .INCLUDEDFROMFILE variable to the contents of .PARSEFILE
2326 * and the .INCLUDEDFROMDIR variable to the contents of .PARSEDIR
2332 * The .INCLUDEDFROMFILE variable is overwritten by the contents
2333 * of .PARSEFILE and the .INCLUDEDFROMDIR variable is overwriten
2334 * by the contents of .PARSEDIR
2335 *---------------------------------------------------------------------
2338 ParseSetIncludedFile(void)
2340 char *pf
, *fp
= NULL
;
2341 char *pd
, *dp
= NULL
;
2343 pf
= Var_Value(".PARSEFILE", VAR_GLOBAL
, &fp
);
2344 Var_Set(".INCLUDEDFROMFILE", pf
, VAR_GLOBAL
, 0);
2345 pd
= Var_Value(".PARSEDIR", VAR_GLOBAL
, &dp
);
2346 Var_Set(".INCLUDEDFROMDIR", pd
, VAR_GLOBAL
, 0);
2349 fprintf(debug_file
, "%s: ${.INCLUDEDFROMDIR} = `%s' "
2350 "${.INCLUDEDFROMFILE} = `%s'\n", __func__
, pd
, pf
);
2356 *---------------------------------------------------------------------
2357 * ParseSetParseFile --
2358 * Set the .PARSEDIR and .PARSEFILE variables to the dirname and
2359 * basename of the given filename
2365 * The .PARSEDIR and .PARSEFILE variables are overwritten by the
2366 * dirname and basename of the given filename.
2367 *---------------------------------------------------------------------
2370 ParseSetParseFile(const char *filename
)
2372 char *slash
, *dirname
;
2373 const char *pd
, *pf
;
2376 slash
= strrchr(filename
, '/');
2377 if (slash
== NULL
) {
2378 Var_Set(".PARSEDIR", pd
= curdir
, VAR_GLOBAL
, 0);
2379 Var_Set(".PARSEFILE", pf
= filename
, VAR_GLOBAL
, 0);
2382 len
= slash
- filename
;
2383 dirname
= bmake_malloc(len
+ 1);
2384 memcpy(dirname
, filename
, len
);
2385 dirname
[len
] = '\0';
2386 Var_Set(".PARSEDIR", pd
= dirname
, VAR_GLOBAL
, 0);
2387 Var_Set(".PARSEFILE", pf
= slash
+ 1, VAR_GLOBAL
, 0);
2390 fprintf(debug_file
, "%s: ${.PARSEDIR} = `%s' ${.PARSEFILE} = `%s'\n",
2396 * Track the makefiles we read - so makefiles can
2397 * set dependencies on them.
2398 * Avoid adding anything more than once.
2402 ParseTrackInput(const char *name
)
2407 size_t name_len
= strlen(name
);
2409 old
= Var_Value(MAKE_MAKEFILES
, VAR_GLOBAL
, &fp
);
2411 ep
= old
+ strlen(old
) - name_len
;
2412 /* does it contain name? */
2413 for (; old
!= NULL
; old
= strchr(old
, ' ')) {
2417 break; /* cannot contain name */
2418 if (memcmp(old
, name
, name_len
) == 0
2419 && (old
[name_len
] == 0 || old
[name_len
] == ' '))
2423 Var_Append (MAKE_MAKEFILES
, name
, VAR_GLOBAL
);
2432 *---------------------------------------------------------------------
2434 * Start Parsing from the given source
2440 * A structure is added to the includes Lst and readProc, lineno,
2441 * fname and curFile are altered for the new file
2442 *---------------------------------------------------------------------
2445 Parse_SetInput(const char *name
, int line
, int fd
,
2446 char *(*nextbuf
)(void *, size_t *), void *arg
)
2452 name
= curFile
->fname
;
2454 ParseTrackInput(name
);
2457 fprintf(debug_file
, "%s: file %s, line %d, fd %d, nextbuf %p, arg %p\n",
2458 __func__
, name
, line
, fd
, nextbuf
, arg
);
2460 if (fd
== -1 && nextbuf
== NULL
)
2464 if (curFile
!= NULL
)
2465 /* Save exiting file info */
2466 Lst_AtFront(includes
, curFile
);
2468 /* Allocate and fill in new structure */
2469 curFile
= bmake_malloc(sizeof *curFile
);
2472 * Once the previous state has been saved, we can get down to reading
2473 * the new file. We set up the name of the file to be the absolute
2474 * name of the include file so error messages refer to the right
2477 curFile
->fname
= bmake_strdup(name
);
2478 curFile
->lineno
= line
;
2479 curFile
->first_lineno
= line
;
2480 curFile
->nextbuf
= nextbuf
;
2481 curFile
->nextbuf_arg
= arg
;
2483 curFile
->depending
= doing_depend
; /* restore this on EOF */
2485 assert(nextbuf
!= NULL
);
2487 /* Get first block of input data */
2488 buf
= curFile
->nextbuf(curFile
->nextbuf_arg
, &len
);
2490 /* Was all a waste of time ... */
2492 free(curFile
->fname
);
2496 curFile
->P_str
= buf
;
2497 curFile
->P_ptr
= buf
;
2498 curFile
->P_end
= buf
+len
;
2500 curFile
->cond_depth
= Cond_save_depth();
2501 ParseSetParseFile(name
);
2506 *---------------------------------------------------------------------
2507 * ParseTraditionalInclude --
2508 * Push to another file.
2510 * The input is the current line. The file name(s) are
2511 * following the "include".
2517 * A structure is added to the includes Lst and readProc, lineno,
2518 * fname and curFILE are altered for the new file
2519 *---------------------------------------------------------------------
2522 ParseTraditionalInclude(char *line
)
2524 char *cp
; /* current position in file spec */
2526 int silent
= (line
[0] != 'i') ? 1 : 0;
2527 char *file
= &line
[silent
+ 7];
2531 fprintf(debug_file
, "%s: %s\n", __func__
, file
);
2535 * Skip over whitespace
2537 while (isspace((unsigned char)*file
))
2541 * Substitute for any variables in the file name before trying to
2544 all_files
= Var_Subst(NULL
, file
, VAR_CMD
, VARF_WANTRES
);
2546 if (*file
== '\0') {
2547 Parse_Error(PARSE_FATAL
,
2548 "Filename missing from \"include\"");
2552 for (file
= all_files
; !done
; file
= cp
+ 1) {
2553 /* Skip to end of line or next whitespace */
2554 for (cp
= file
; *cp
&& !isspace((unsigned char) *cp
); cp
++)
2562 Parse_include_file(file
, FALSE
, FALSE
, silent
);
2570 *---------------------------------------------------------------------
2571 * ParseGmakeExport --
2572 * Parse export <variable>=<value>
2574 * And set the environment with it.
2581 *---------------------------------------------------------------------
2584 ParseGmakeExport(char *line
)
2586 char *variable
= &line
[6];
2590 fprintf(debug_file
, "%s: %s\n", __func__
, variable
);
2594 * Skip over whitespace
2596 while (isspace((unsigned char)*variable
))
2599 for (value
= variable
; *value
&& *value
!= '='; value
++)
2602 if (*value
!= '=') {
2603 Parse_Error(PARSE_FATAL
,
2604 "Variable/Value missing from \"export\"");
2607 *value
++ = '\0'; /* terminate variable */
2610 * Expand the value before putting it in the environment.
2612 value
= Var_Subst(NULL
, value
, VAR_CMD
, VARF_WANTRES
);
2613 setenv(variable
, value
, 1);
2618 *---------------------------------------------------------------------
2620 * Called when EOF is reached in the current file. If we were reading
2621 * an include file, the includes stack is popped and things set up
2622 * to go back to reading the previous file at the previous location.
2625 * CONTINUE if there's more to do. DONE if not.
2628 * The old curFILE, is closed. The includes list is shortened.
2629 * lineno, curFILE, and fname are changed if CONTINUE is returned.
2630 *---------------------------------------------------------------------
2638 assert(curFile
->nextbuf
!= NULL
);
2640 doing_depend
= curFile
->depending
; /* restore this */
2641 /* get next input buffer, if any */
2642 ptr
= curFile
->nextbuf(curFile
->nextbuf_arg
, &len
);
2643 curFile
->P_ptr
= ptr
;
2644 curFile
->P_str
= ptr
;
2645 curFile
->P_end
= ptr
+ len
;
2646 curFile
->lineno
= curFile
->first_lineno
;
2652 /* Ensure the makefile (or loop) didn't have mismatched conditionals */
2653 Cond_restore_depth(curFile
->cond_depth
);
2655 if (curFile
->lf
!= NULL
) {
2656 loadedfile_destroy(curFile
->lf
);
2660 /* Dispose of curFile info */
2661 /* Leak curFile->fname because all the gnodes have pointers to it */
2662 free(curFile
->P_str
);
2665 curFile
= Lst_DeQueue(includes
);
2667 if (curFile
== NULL
) {
2668 /* We've run out of input */
2669 Var_Delete(".PARSEDIR", VAR_GLOBAL
);
2670 Var_Delete(".PARSEFILE", VAR_GLOBAL
);
2671 Var_Delete(".INCLUDEDFROMDIR", VAR_GLOBAL
);
2672 Var_Delete(".INCLUDEDFROMFILE", VAR_GLOBAL
);
2677 fprintf(debug_file
, "ParseEOF: returning to file %s, line %d\n",
2678 curFile
->fname
, curFile
->lineno
);
2680 /* Restore the PARSEDIR/PARSEFILE variables */
2681 ParseSetParseFile(curFile
->fname
);
2686 #define PARSE_SKIP 2
2689 ParseGetLine(int flags
, int *length
)
2691 IFile
*cf
= curFile
;
2700 /* Loop through blank lines and comment lines */
2709 if (cf
->P_end
!= NULL
&& ptr
== cf
->P_end
) {
2715 if (ch
== 0 || (ch
== '\\' && ptr
[1] == 0)) {
2716 if (cf
->P_end
== NULL
)
2717 /* End of string (aka for loop) data */
2719 /* see if there is more we can parse */
2720 while (ptr
++ < cf
->P_end
) {
2721 if ((ch
= *ptr
) == '\n') {
2722 if (ptr
> line
&& ptr
[-1] == '\\')
2724 Parse_Error(PARSE_WARNING
,
2725 "Zero byte read from file, skipping rest of line.");
2729 if (cf
->nextbuf
!= NULL
) {
2731 * End of this buffer; return EOF and outer logic
2732 * will get the next one. (eww)
2736 Parse_Error(PARSE_FATAL
, "Zero byte read from file");
2741 /* Don't treat next character as special, remember first one */
2742 if (escaped
== NULL
)
2750 if (ch
== '#' && comment
== NULL
) {
2751 /* Remember first '#' for comment stripping */
2752 /* Unless previous char was '[', as in modifier :[#] */
2753 if (!(ptr
> line
&& ptr
[-1] == '['))
2759 if (!isspace((unsigned char)ch
))
2760 /* We are not interested in trailing whitespace */
2764 /* Save next 'to be processed' location */
2767 /* Check we have a non-comment, non-blank line */
2768 if (line_end
== line
|| comment
== line
) {
2770 /* At end of file */
2772 /* Parse another line */
2776 /* We now have a line of data */
2779 if (flags
& PARSE_RAW
) {
2780 /* Leave '\' (etc) in line buffer (eg 'for' lines) */
2781 *length
= line_end
- line
;
2785 if (flags
& PARSE_SKIP
) {
2786 /* Completely ignore non-directives */
2789 /* We could do more of the .else/.elif/.endif checks here */
2794 /* Brutally ignore anything after a non-escaped '#' in non-commands */
2795 if (comment
!= NULL
&& line
[0] != '\t') {
2800 /* If we didn't see a '\\' then the in-situ data is fine */
2801 if (escaped
== NULL
) {
2802 *length
= line_end
- line
;
2806 /* Remove escapes from '\n' and '#' */
2809 for (; ; *tp
++ = ch
) {
2819 /* Delete '\\' at end of buffer */
2824 if (ch
== '#' && line
[0] != '\t')
2825 /* Delete '\\' from before '#' on non-command lines */
2829 /* Leave '\\' in buffer for later */
2831 /* Make sure we don't delete an escaped ' ' from the line end */
2836 /* Escaped '\n' replace following whitespace with a single ' ' */
2837 while (ptr
[0] == ' ' || ptr
[0] == '\t')
2842 /* Delete any trailing spaces - eg from empty continuations */
2843 while (tp
> escaped
&& isspace((unsigned char)tp
[-1]))
2847 *length
= tp
- line
;
2852 *---------------------------------------------------------------------
2854 * Read an entire line from the input file. Called only by Parse_File.
2857 * A line w/o its newline
2860 * Only those associated with reading a character
2861 *---------------------------------------------------------------------
2866 char *line
; /* Result */
2867 int lineLength
; /* Length of result */
2868 int lineno
; /* Saved line # */
2872 line
= ParseGetLine(0, &lineLength
);
2880 * The line might be a conditional. Ask the conditional module
2881 * about it and act accordingly
2883 switch (Cond_Eval(line
)) {
2885 /* Skip to next conditional that evaluates to COND_PARSE. */
2887 line
= ParseGetLine(PARSE_SKIP
, &lineLength
);
2888 } while (line
&& Cond_Eval(line
) != COND_PARSE
);
2894 case COND_INVALID
: /* Not a conditional line */
2895 /* Check for .for loops */
2896 rval
= For_Eval(line
);
2898 /* Not a .for line */
2901 /* Syntax error - error printed, ignore line */
2903 /* Start of a .for loop */
2904 lineno
= curFile
->lineno
;
2905 /* Accumulate loop lines until matching .endfor */
2907 line
= ParseGetLine(PARSE_RAW
, &lineLength
);
2909 Parse_Error(PARSE_FATAL
,
2910 "Unexpected end of file in for loop.");
2913 } while (For_Accum(line
));
2914 /* Stash each iteration as a new 'input file' */
2916 /* Read next line from for-loop buffer */
2924 *-----------------------------------------------------------------------
2925 * ParseFinishLine --
2926 * Handle the end of a dependency group.
2932 * inLine set FALSE. 'targets' list destroyed.
2934 *-----------------------------------------------------------------------
2937 ParseFinishLine(void)
2940 Lst_ForEach(targets
, Suff_EndTransform
, NULL
);
2941 Lst_Destroy(targets
, ParseHasCommands
);
2949 *---------------------------------------------------------------------
2951 * Parse a file into its component parts, incorporating it into the
2952 * current dependency graph. This is the main function and controls
2953 * almost every other function in this module
2956 * name the name of the file being read
2957 * fd Open file to makefile to parse
2964 * Loads. Nodes are added to the list of all targets, nodes and links
2965 * are added to the dependency graph. etc. etc. etc.
2966 *---------------------------------------------------------------------
2969 Parse_File(const char *name
, int fd
)
2971 char *cp
; /* pointer into the line */
2972 char *line
; /* the line we're working on */
2973 struct loadedfile
*lf
;
2975 lf
= loadfile(name
, fd
);
2984 Parse_SetInput(name
, 0, -1, loadedfile_nextbuf
, lf
);
2988 for (; (line
= ParseReadLine()) != NULL
; ) {
2990 fprintf(debug_file
, "ParseReadLine (%d): '%s'\n",
2991 curFile
->lineno
, line
);
2994 * Lines that begin with the special character may be
2995 * include or undef directives.
2996 * On the other hand they can be suffix rules (.c.o: ...)
2997 * or just dependencies for filenames that start '.'.
2999 for (cp
= line
+ 1; isspace((unsigned char)*cp
); cp
++) {
3002 if (strncmp(cp
, "include", 7) == 0 ||
3003 ((cp
[0] == 'd' || cp
[0] == 's' || cp
[0] == '-') &&
3004 strncmp(&cp
[1], "include", 7) == 0)) {
3008 if (strncmp(cp
, "undef", 5) == 0) {
3010 for (cp
+= 5; isspace((unsigned char) *cp
); cp
++)
3012 for (cp2
= cp
; !isspace((unsigned char) *cp2
) &&
3013 (*cp2
!= '\0'); cp2
++)
3016 Var_Delete(cp
, VAR_GLOBAL
);
3018 } else if (strncmp(cp
, "export", 6) == 0) {
3019 for (cp
+= 6; isspace((unsigned char) *cp
); cp
++)
3023 } else if (strncmp(cp
, "unexport", 8) == 0) {
3026 } else if (strncmp(cp
, "info", 4) == 0 ||
3027 strncmp(cp
, "error", 5) == 0 ||
3028 strncmp(cp
, "warning", 7) == 0) {
3029 if (ParseMessage(cp
))
3034 if (*line
== '\t') {
3036 * If a line starts with a tab, it can only hope to be
3037 * a creation command.
3041 for (; isspace ((unsigned char)*cp
); cp
++) {
3046 Parse_Error(PARSE_FATAL
,
3047 "Unassociated shell command \"%s\"",
3050 * So long as it's not a blank line and we're actually
3051 * in a dependency spec, add the command to the list of
3052 * commands of all targets in the dependency spec
3055 cp
= bmake_strdup(cp
);
3056 Lst_ForEach(targets
, ParseAddCmd
, cp
);
3058 Lst_AtEnd(targCmds
, cp
);
3066 if (((strncmp(line
, "include", 7) == 0 &&
3067 isspace((unsigned char) line
[7])) ||
3068 ((line
[0] == 's' || line
[0] == '-') &&
3069 strncmp(&line
[1], "include", 7) == 0 &&
3070 isspace((unsigned char) line
[8]))) &&
3071 strchr(line
, ':') == NULL
) {
3073 * It's an S3/S5-style "include".
3075 ParseTraditionalInclude(line
);
3080 if (strncmp(line
, "export", 6) == 0 &&
3081 isspace((unsigned char) line
[6]) &&
3082 strchr(line
, ':') == NULL
) {
3084 * It's a Gmake "export".
3086 ParseGmakeExport(line
);
3090 if (Parse_IsVar(line
)) {
3092 Parse_DoVar(line
, VAR_GLOBAL
);
3098 * To make life easier on novices, if the line is indented we
3099 * first make sure the line has a dependency operator in it.
3100 * If it doesn't have an operator and we're in a dependency
3101 * line's script, we assume it's actually a shell command
3102 * and add it to the current list of targets.
3105 if (isspace((unsigned char) line
[0])) {
3106 while ((*cp
!= '\0') && isspace((unsigned char) *cp
))
3108 while (*cp
&& (ParseIsEscaped(line
, cp
) ||
3109 (*cp
!= ':') && (*cp
!= '!'))) {
3114 Parse_Error(PARSE_WARNING
,
3115 "Shell command needs a leading tab");
3124 * For some reason - probably to make the parser impossible -
3125 * a ';' can be used to separate commands from dependencies.
3126 * Attempt to avoid ';' inside substitution patterns.
3131 for (cp
= line
; *cp
!= 0; cp
++) {
3132 if (*cp
== '\\' && cp
[1] != 0) {
3137 (cp
[1] == '(' || cp
[1] == '{')) {
3142 if (*cp
== ')' || *cp
== '}') {
3146 } else if (*cp
== ';') {
3152 /* Terminate the dependency list at the ';' */
3158 * We now know it's a dependency line so it needs to have all
3159 * variables expanded before being parsed. Tell the variable
3160 * module to complain if some variable is undefined...
3162 line
= Var_Subst(NULL
, line
, VAR_CMD
, VARF_UNDEFERR
|VARF_WANTRES
);
3165 * Need a non-circular list for the target nodes
3168 Lst_Destroy(targets
, NULL
);
3170 targets
= Lst_Init(FALSE
);
3173 ParseDoDependency(line
);
3176 /* If there were commands after a ';', add them now */
3182 * Reached EOF, but it may be just EOF of an include file...
3184 } while (ParseEOF() == CONTINUE
);
3187 (void)fflush(stdout
);
3188 (void)fprintf(stderr
,
3189 "%s: Fatal errors encountered -- cannot continue",
3191 PrintOnError(NULL
, NULL
);
3197 *---------------------------------------------------------------------
3199 * initialize the parsing module
3205 * the parseIncPath list is initialized...
3206 *---------------------------------------------------------------------
3212 parseIncPath
= Lst_Init(FALSE
);
3213 sysIncPath
= Lst_Init(FALSE
);
3214 defIncPath
= Lst_Init(FALSE
);
3215 includes
= Lst_Init(FALSE
);
3217 targCmds
= Lst_Init(FALSE
);
3225 Lst_Destroy(targCmds
, (FreeProc
*)free
);
3227 Lst_Destroy(targets
, NULL
);
3228 Lst_Destroy(defIncPath
, Dir_Destroy
);
3229 Lst_Destroy(sysIncPath
, Dir_Destroy
);
3230 Lst_Destroy(parseIncPath
, Dir_Destroy
);
3231 Lst_Destroy(includes
, NULL
); /* Should be empty now */
3237 *-----------------------------------------------------------------------
3239 * Return a Lst of the main target to create for main()'s sake. If
3240 * no such target exists, we Punt with an obnoxious error message.
3243 * A Lst of the single node to create.
3248 *-----------------------------------------------------------------------
3251 Parse_MainName(void)
3253 Lst mainList
; /* result list */
3255 mainList
= Lst_Init(FALSE
);
3257 if (mainNode
== NULL
) {
3258 Punt("no target to make.");
3260 } else if (mainNode
->type
& OP_DOUBLEDEP
) {
3261 (void)Lst_AtEnd(mainList
, mainNode
);
3262 Lst_Concat(mainList
, mainNode
->cohorts
, LST_CONCNEW
);
3265 (void)Lst_AtEnd(mainList
, mainNode
);
3266 Var_Append(".TARGETS", mainNode
->name
, VAR_GLOBAL
);
3271 *-----------------------------------------------------------------------
3273 * Add the filename and lineno to the GNode so that we remember
3274 * where it was first defined.
3279 *-----------------------------------------------------------------------
3282 ParseMark(GNode
*gn
)
3284 gn
->fname
= curFile
->fname
;
3285 gn
->lineno
= curFile
->lineno
;