2 * Copyright (c) 1988, 1989, 1990, 1993
3 * The Regents of the University of California. All rights reserved.
4 * Copyright (c) 1989 by Berkeley Softworks
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. All advertising materials mentioning features or use of this software
19 * must display the following acknowledgement:
20 * This product includes software developed by the University of
21 * California, Berkeley and its contributors.
22 * 4. Neither the name of the University nor the names of its contributors
23 * may be used to endorse or promote products derived from this software
24 * without specific prior written permission.
26 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
27 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
28 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
29 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
30 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
31 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
32 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
33 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
34 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
35 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
38 * @(#)parse.c 8.3 (Berkeley) 3/19/94
39 * $FreeBSD: src/usr.bin/make/parse.c,v 1.22.2.2 2004/07/10 08:14:42 eik Exp $
40 * $DragonFly: src/usr.bin/make/parse.c,v 1.36 2005/01/10 12:36:06 okumoto Exp $
45 * Functions to parse a makefile.
47 * One function, Parse_Init, must be called before any functions
48 * in this module are used. After that, the function Parse_File is the
49 * main entry point and controls most of the other functions in this
52 * Most important structures are kept in Lsts. Directories for
53 * the #include "..." function are kept in the 'parseIncPath' Lst, while
54 * those for the #include <...> are kept in the 'sysIncPath' Lst. The
55 * targets currently being defined are kept in the 'targets' Lst.
57 * The variables 'curFile.fname' and 'curFile.lineno' are used to track
58 * the name of the current file and the line number in that file so that
59 * error messages can be more meaningful.
62 * Parse_Init Initialization function which must be
63 * called before anything else in this module
66 * Parse_File Function used to parse a makefile. It must
67 * be given the name of the file, which should
68 * already have been opened, and a function
69 * to call to read a character from the file.
71 * Parse_IsVar Returns TRUE if the given line is a
72 * variable assignment. Used by MainParseArgs
73 * to determine if an argument is a target
74 * or a variable assignment. Used internally
75 * for pretty much the same thing...
77 * Parse_Error Function called when an error occurs in
78 * parsing. Used by the variable and
79 * conditional modules.
80 * Parse_MainName Returns a Lst of the main target to create.
101 #include "pathnames.h"
109 * These values are returned by ParseEOF to tell Parse_File whether to
110 * CONTINUE parsing, i.e. it had only reached the end of an include file,
116 /* targets we're working on */
117 static Lst targets = Lst_Initializer(targets);
119 static Boolean inLine; /* true if currently in a dependency
120 * line or its commands */
121 static int fatals = 0;
123 static GNode *mainNode; /* The main target to create. This is the
124 * first target on the first dependency
125 * line in the first makefile */
127 IFile curFile; /* current makefile */
129 /* stack of IFiles generated by * #includes */
130 static Lst includes = Lst_Initializer(includes);
132 /* list of directories for "..." includes */
133 Lst parseIncPath = Lst_Initializer(parseIncPath);
135 /* list of directories for <...> includes */
136 Lst sysIncPath = Lst_Initializer(sysIncPath);
139 * specType contains the SPECial TYPE of the current target. It is
140 * Not if the target is unspecial. If it *is* special, however, the children
141 * are linked as children of the parent but not vice versa. This variable is
142 * set in ParseDoDependency
146 Default, /* .DEFAULT */
148 Ignore, /* .IGNORE */
149 Includes, /* .INCLUDES */
150 Interrupt, /* .INTERRUPT */
152 MFlags, /* .MFLAGS or .MAKEFLAGS */
153 Main, /* .MAIN and we don't have anything user-specified to
155 NoExport, /* .NOEXPORT */
156 Not, /* Not special */
157 NotParallel, /* .NOTPARALELL */
160 Parallel, /* .PARALLEL */
164 Precious, /* .PRECIOUS */
165 ExShell, /* .SHELL */
166 Silent, /* .SILENT */
167 SingleShell, /* .SINGLESHELL */
168 Suffixes, /* .SUFFIXES */
170 Attribute /* Generic attribute */
173 static ParseSpecial specType;
177 * Predecessor node for handling .ORDER. Initialized to NULL when .ORDER
178 * seen, then set to each successive source on the line.
180 static GNode *predecessor;
183 * The parseKeywords table is searched using binary search when deciding
184 * if a target or source is special. The 'spec' field is the ParseSpecial
185 * type of the keyword ("Not" if the keyword isn't special as a target) while
186 * the 'op' field is the operator to apply to the list of targets if the
187 * keyword is used as a source ("0" if the keyword isn't special as a source)
190 const char *name; /* Name of keyword */
191 ParseSpecial spec; /* Type when used as a target */
192 int op; /* Operator when used as a source */
193 } parseKeywords[] = {
194 { ".BEGIN", Begin, 0 },
195 { ".DEFAULT", Default, 0 },
197 { ".EXEC", Attribute, OP_EXEC },
198 { ".IGNORE", Ignore, OP_IGNORE },
199 { ".INCLUDES", Includes, 0 },
200 { ".INTERRUPT", Interrupt, 0 },
201 { ".INVISIBLE", Attribute, OP_INVISIBLE },
202 { ".JOIN", Attribute, OP_JOIN },
203 { ".LIBS", Libs, 0 },
204 { ".MAIN", Main, 0 },
205 { ".MAKE", Attribute, OP_MAKE },
206 { ".MAKEFLAGS", MFlags, 0 },
207 { ".MFLAGS", MFlags, 0 },
208 { ".NOTMAIN", Attribute, OP_NOTMAIN },
209 { ".NOTPARALLEL", NotParallel, 0 },
210 { ".NO_PARALLEL", NotParallel, 0 },
211 { ".NULL", Null, 0 },
212 { ".OPTIONAL", Attribute, OP_OPTIONAL },
213 { ".ORDER", Order, 0 },
214 { ".PARALLEL", Parallel, 0 },
215 { ".PATH", ExPath, 0 },
216 { ".PHONY", Phony, OP_PHONY },
217 { ".POSIX", Posix, 0 },
218 { ".PRECIOUS", Precious, OP_PRECIOUS },
219 { ".RECURSIVE", Attribute, OP_MAKE },
220 { ".SHELL", ExShell, 0 },
221 { ".SILENT", Silent, OP_SILENT },
222 { ".SINGLESHELL", SingleShell, 0 },
223 { ".SUFFIXES", Suffixes, 0 },
224 { ".USE", Attribute, OP_USE },
225 { ".WAIT", Wait, 0 },
228 static int ParseFindKeyword(char *);
229 static int ParseLinkSrc(void *, void *);
230 static int ParseDoOp(void *, void *);
231 static int ParseAddDep(void *, void *);
232 static void ParseDoSrc(int, char *, Lst *);
233 static int ParseFindMain(void *, void *);
234 static int ParseAddDir(void *, void *);
235 static int ParseClearPath(void *, void *);
236 static void ParseDoDependency(char *);
237 static int ParseAddCmd(void *, void *);
238 static int ParseReadc(void);
239 static void ParseUnreadc(int);
240 static void ParseHasCommands(void *);
241 static void ParseDoInclude(char *);
242 static void ParseDoError(char *);
243 static void ParseDoWarning(char *);
245 static void ParseTraditionalInclude(char *);
247 static int ParseEOF(int);
248 static char *ParseReadLine(void);
249 static char *ParseSkipLine(int, int);
250 static void ParseFinishLine(void);
253 *----------------------------------------------------------------------
254 * ParseFindKeyword --
255 * Look in the table of keywords for one matching the given string.
258 * The index of the keyword, or -1 if it isn't there.
262 *----------------------------------------------------------------------
265 ParseFindKeyword(char *str)
273 end = (sizeof(parseKeywords) / sizeof(parseKeywords[0])) - 1;
276 cur = start + ((end - start) / 2);
277 diff = strcmp(str, parseKeywords[cur].name);
281 } else if (diff < 0) {
286 } while (start <= end);
292 * Error message abort function for parsing. Prints out the context
293 * of the error (line number and file) as well as the message with
294 * two optional arguments.
300 * "fatals" is incremented if the level is PARSE_FATAL.
304 Parse_Error(int type, const char *fmt, ...)
309 fprintf(stderr, "\"%s\", line %d: ",
310 curFile.fname, curFile.lineno);
311 if (type == PARSE_WARNING)
312 fprintf(stderr, "warning: ");
313 vfprintf(stderr, fmt, ap);
315 fprintf(stderr, "\n");
317 if (type == PARSE_FATAL)
322 *---------------------------------------------------------------------
324 * Link the parent node to its new child. Used in a Lst_ForEach by
325 * ParseDoDependency. If the specType isn't 'Not', the parent
326 * isn't linked as a parent of the child.
332 * New elements are added to the parents list of cgn and the
333 * children list of cgn. the unmade field of pgn is updated
334 * to reflect the additional child.
335 *---------------------------------------------------------------------
338 ParseLinkSrc(void *pgnp, void *cgnp)
343 if (Lst_Member(&pgn->children, cgn) == NULL) {
344 Lst_AtEnd(&pgn->children, cgn);
345 if (specType == Not) {
346 Lst_AtEnd(&cgn->parents, pgn);
354 *---------------------------------------------------------------------
356 * Apply the parsed operator to the given target node. Used in a
357 * Lst_ForEach call by ParseDoDependency once all targets have
358 * been found and their operator parsed. If the previous and new
359 * operators are incompatible, a major error is taken.
365 * The type field of the node is altered to reflect any new bits in
367 *---------------------------------------------------------------------
370 ParseDoOp(void *gnp, void *opp)
373 int op = *(int *)opp;
376 * If the dependency mask of the operator and the node don't match and
377 * the node has actually had an operator applied to it before, and
378 * the operator actually has some dependency information in it, complain.
380 if (((op & OP_OPMASK) != (gn->type & OP_OPMASK)) &&
381 !OP_NOP(gn->type) && !OP_NOP(op))
383 Parse_Error(PARSE_FATAL, "Inconsistent operator for %s", gn->name);
387 if ((op == OP_DOUBLEDEP) && ((gn->type & OP_OPMASK) == OP_DOUBLEDEP)) {
389 * If the node was the object of a :: operator, we need to create a
390 * new instance of it for the children and commands on this dependency
391 * line. The new instance is placed on the 'cohorts' list of the
392 * initial one (note the initial one is not on its own cohorts list)
393 * and the new instance is linked to all parents of the initial
399 cohort = Targ_NewGN(gn->name);
401 * Duplicate links to parents so graph traversal is simple. Perhaps
402 * some type bits should be duplicated?
404 * Make the cohort invisible as well to avoid duplicating it into
405 * other variables. True, parents of this target won't tend to do
406 * anything with their local variables, but better safe than
409 Lst_ForEach(&gn->parents, ParseLinkSrc, cohort);
410 cohort->type = OP_DOUBLEDEP|OP_INVISIBLE;
411 Lst_AtEnd(&gn->cohorts, cohort);
414 * Replace the node in the targets list with the new copy
416 ln = Lst_Member(&targets, gn);
417 Lst_Replace(ln, cohort);
421 * We don't want to nuke any previous flags (whatever they were) so we
422 * just OR the new operator into the old
430 *---------------------------------------------------------------------
432 * Check if the pair of GNodes given needs to be synchronized.
433 * This has to be when two nodes are on different sides of a
437 * Returns 1 if the two targets need to be ordered, 0 otherwise.
438 * If it returns 1, the search can stop
441 * A dependency can be added between the two nodes.
443 *---------------------------------------------------------------------
446 ParseAddDep(void *pp, void *sp)
451 if (p->order < s->order) {
453 * XXX: This can cause loops, and loops can cause unmade targets,
454 * but checking is tedious, and the debugging output can show the
457 Lst_AtEnd(&p->successors, s);
458 Lst_AtEnd(&s->preds, p);
467 *---------------------------------------------------------------------
469 * Given the name of a source, figure out if it is an attribute
470 * and apply it to the targets if it is. Else decide if there is
471 * some attribute which should be applied *to* the source because
472 * of some special target and apply it if so. Otherwise, make the
473 * source be a child of the targets in the list 'targets'
479 * Operator bits may be added to the list of targets or to the source.
480 * The targets may have a new source added to their lists of children.
481 *---------------------------------------------------------------------
484 ParseDoSrc(int tOp, char *src, Lst *allsrc)
488 if (*src == '.' && isupper ((unsigned char) src[1])) {
489 int keywd = ParseFindKeyword(src);
491 int op = parseKeywords[keywd].op;
493 Lst_ForEach(&targets, ParseDoOp, &op);
496 if (parseKeywords[keywd].spec == Wait) {
506 * If we have noted the existence of a .MAIN, it means we need
507 * to add the sources of said target to the list of things
508 * to create. The string 'src' is likely to be free, so we
509 * must make a new copy of it. Note that this will only be
510 * invoked if the user didn't specify a target on the command
511 * line. This is to allow #ifmake's to succeed, or something...
513 Lst_AtEnd(&create, estrdup(src));
515 * Add the name to the .TARGETS variable as well, so the user cna
516 * employ that, if desired.
518 Var_Append(".TARGETS", src, VAR_GLOBAL);
523 * Create proper predecessor/successor links between the previous
524 * source and the current one.
526 gn = Targ_FindNode(src, TARG_CREATE);
527 if (predecessor != NULL) {
528 Lst_AtEnd(&predecessor->successors, gn);
529 Lst_AtEnd(&gn->preds, predecessor);
532 * The current source now becomes the predecessor for the next one.
539 * If the source is not an attribute, we need to find/create
540 * a node for it. After that we can apply any operator to it
541 * from a special target or link it to its parents, as
544 * In the case of a source that was the object of a :: operator,
545 * the attribute is applied to all of its instances (as kept in
546 * the 'cohorts' list of the node) or all the cohorts are linked
547 * to all the targets.
549 gn = Targ_FindNode(src, TARG_CREATE);
553 Lst_ForEach(&targets, ParseLinkSrc, gn);
555 if ((gn->type & OP_OPMASK) == OP_DOUBLEDEP) {
559 for (ln = Lst_First(&gn->cohorts); ln != NULL; ln = Lst_Succ(ln)) {
560 cohort = Lst_Datum(ln);
564 Lst_ForEach(&targets, ParseLinkSrc, cohort);
572 Lst_AtEnd(allsrc, gn);
574 Lst_ForEach(allsrc, ParseAddDep, gn);
579 *-----------------------------------------------------------------------
581 * Find a real target in the list and set it to be the main one.
582 * Called by ParseDoDependency when a main target hasn't been found
586 * 0 if main not found yet, 1 if it is.
589 * mainNode is changed and Targ_SetMain is called.
591 *-----------------------------------------------------------------------
594 ParseFindMain(void *gnp, void *dummy __unused)
598 if ((gn->type & (OP_NOTMAIN | OP_USE | OP_EXEC | OP_TRANSFORM)) == 0) {
608 *-----------------------------------------------------------------------
610 * Front-end for Dir_AddDir to make sure Lst_ForEach keeps going
618 *-----------------------------------------------------------------------
621 ParseAddDir(void *path, void *name)
624 Dir_AddDir(path, name);
629 *-----------------------------------------------------------------------
631 * Front-end for Dir_ClearPath to make sure Lst_ForEach keeps going
639 *-----------------------------------------------------------------------
642 ParseClearPath(void *path, void *dummy __unused)
650 *---------------------------------------------------------------------
651 * ParseDoDependency --
652 * Parse the dependency line in line.
658 * The nodes of the sources are linked as children to the nodes of the
659 * targets. Some nodes may be created.
661 * We parse a dependency line by first extracting words from the line and
662 * finding nodes in the list of all targets with that name. This is done
663 * until a character is encountered which is an operator character. Currently
664 * these are only ! and :. At this point the operator is parsed and the
665 * pointer into the line advanced until the first source is encountered.
666 * The parsed operator is applied to each node in the 'targets' list,
667 * which is where the nodes found for the targets are kept, by means of
668 * the ParseDoOp function.
669 * The sources are read in much the same way as the targets were except
670 * that now they are expanded using the wildcarding scheme of the C-Shell
671 * and all instances of the resulting words in the list of all targets
672 * are found. Each of the resulting nodes is then linked to each of the
673 * targets as one of its children.
674 * Certain targets are handled specially. These are the ones detailed
675 * by the specType variable.
676 * The storing of transformation rules is also taken care of here.
677 * A target is recognized as a transformation rule by calling
678 * Suff_IsTransform. If it is a transformation rule, its node is gotten
679 * from the suffix module via Suff_AddTransform rather than the standard
680 * Targ_FindNode in the target module.
681 *---------------------------------------------------------------------
684 ParseDoDependency(char *line)
686 char *cp; /* our current position */
687 GNode *gn; /* a general purpose temporary node */
688 int op; /* the operator on the line */
689 char savec; /* a place to save a character */
690 Lst paths; /* Search paths to alter when parsing a list of .PATH targets */
691 int tOp; /* operator from special target */
701 *cp && !isspace((unsigned char)*cp) && *cp != '(';
706 * Must be a dynamic source (would have been expanded
707 * otherwise), so call the Var module to parse the puppy
708 * so we can safely advance beyond it...There should be
709 * no errors in this, as they would have been discovered
710 * in the initial Var_Subst and we wouldn't be here.
716 result = Var_Parse(cp, VAR_CMD, TRUE, &length, &freeIt);
722 } else if (*cp == '!' || *cp == ':') {
724 * We don't want to end a word on ':' or '!' if there is a
725 * better match later on in the string (greedy matching).
726 * This allows the user to have targets like:
729 * where "fie::fi:fo" and "foo::bar" are the targets. In
730 * real life this is used for perl5 library man pages where
731 * "::" separates an object from its class.
732 * Ie: "File::Spec::Unix". This behaviour is also consistent
733 * with other versions of make.
737 if (*cp == ':' && *p == ':')
740 /* Found the best match already. */
741 if (*p == '\0' || isspace(*p))
744 p += strcspn(p, "!:");
746 /* No better match later on... */
754 * Archives must be handled specially to make sure the OP_ARCHV
755 * flag is set in their 'type' field, for one thing, and because
756 * things like "archive(file1.o file2.o file3.o)" are permissible.
757 * Arch_ParseArchive will set 'line' to be the first non-blank
758 * after the archive-spec. It creates/finds nodes for the members
759 * and places them on the given list, returning SUCCESS if all
760 * went well and FAILURE if there was an error in the
761 * specification. On error, line should remain untouched.
763 if (Arch_ParseArchive(&line, &targets, VAR_CMD) != SUCCESS) {
764 Parse_Error(PARSE_FATAL,
765 "Error in archive specification: \"%s\"", line);
775 * Ending a dependency line without an operator is a Bozo
776 * no-no. As a heuristic, this is also often triggered by
777 * undetected conflicts from cvs/rcs merges.
779 if ((strncmp(line, "<<<<<<", 6) == 0) ||
780 (strncmp(line, "======", 6) == 0) ||
781 (strncmp(line, ">>>>>>", 6) == 0))
782 Parse_Error(PARSE_FATAL,
783 "Makefile appears to contain unresolved cvs/rcs/??? merge conflicts");
785 Parse_Error(PARSE_FATAL, "Need an operator");
790 * Have a word in line. See if it's a special target and set
791 * specType to match it.
793 if (*line == '.' && isupper((unsigned char)line[1])) {
795 * See if the target is a special target that must have it
796 * or its sources handled specially.
798 int keywd = ParseFindKeyword(line);
800 if (specType == ExPath && parseKeywords[keywd].spec != ExPath) {
801 Parse_Error(PARSE_FATAL, "Mismatched special targets");
805 specType = parseKeywords[keywd].spec;
806 tOp = parseKeywords[keywd].op;
809 * Certain special targets have special semantics:
810 * .PATH Have to set the dirSearchPath
812 * .MAIN Its sources are only used if
813 * nothing has been specified to
815 * .DEFAULT Need to create a node to hang
816 * commands on, but we don't want
817 * it in the graph, nor do we want
818 * it to be the Main Target, so we
819 * create it, set OP_NOTMAIN and
820 * add it to the list, setting
821 * DEFAULT to the new node for
822 * later use. We claim the node is
823 * A transformation rule to make
824 * life easier later, when we'll
825 * use Make_HandleUse to actually
826 * apply the .DEFAULT commands.
827 * .PHONY The list of targets
830 * .INTERRUPT Are not to be considered the
832 * .NOTPARALLEL Make only one target at a time.
833 * .SINGLESHELL Create a shell for each command.
834 * .ORDER Must set initial predecessor to NULL
838 Lst_AtEnd(&paths, &dirSearchPath);
841 if (!Lst_IsEmpty(&create)) {
848 gn = Targ_FindNode(line, TARG_CREATE);
849 gn->type |= OP_NOTMAIN;
850 Lst_AtEnd(&targets, gn);
853 gn = Targ_NewGN(".DEFAULT");
854 gn->type |= (OP_NOTMAIN|OP_TRANSFORM);
855 Lst_AtEnd(&targets, gn);
872 } else if (strncmp(line, ".PATH", 5) == 0) {
874 * .PATH<suffix> has to be handled specially.
875 * Call on the suffix module to give us a path to
881 path = Suff_GetPath(&line[5]);
883 Parse_Error(PARSE_FATAL,
884 "Suffix '%s' not defined (yet)",
888 Lst_AtEnd(&paths, path);
893 * Have word in line. Get or create its node and stick it at
894 * the end of the targets list
896 if ((specType == Not) && (*line != '\0')) {
897 /* target names to be found and added to targets list */
898 Lst curTargs = Lst_Initializer(curTargs);
900 if (Dir_HasWildcards(line)) {
902 * Targets are to be sought only in the current directory,
903 * so create an empty path for the thing. Note we need to
904 * use Dir_Destroy in the destruction of the path as the
905 * Dir module could have added a directory to the path...
907 Lst emptyPath = Lst_Initializer(emptyPath);
909 Dir_Expand(line, &emptyPath, &curTargs);
911 Lst_Destroy(&emptyPath, Dir_Destroy);
914 * No wildcards, but we want to avoid code duplication,
915 * so create a list with the word on it.
917 Lst_AtEnd(&curTargs, line);
920 while (!Lst_IsEmpty(&curTargs)) {
921 char *targName = Lst_DeQueue(&curTargs);
923 if (!Suff_IsTransform (targName)) {
924 gn = Targ_FindNode(targName, TARG_CREATE);
926 gn = Suff_AddTransform(targName);
929 Lst_AtEnd(&targets, gn);
931 } else if (specType == ExPath && *line != '.' && *line != '\0') {
932 Parse_Error(PARSE_WARNING, "Extra target (%s) ignored", line);
937 * If it is a special type and not .PATH, it's the only target we
938 * allow on this line...
940 if (specType != Not && specType != ExPath) {
941 Boolean warnFlag = FALSE;
943 while ((*cp != '!') && (*cp != ':') && *cp) {
944 if (*cp != ' ' && *cp != '\t') {
950 Parse_Error(PARSE_WARNING, "Extra target ignored");
953 while (*cp && isspace((unsigned char)*cp)) {
958 } while ((*line != '!') && (*line != ':') && *line);
960 if (!Lst_IsEmpty(&targets)) {
963 Parse_Error(PARSE_WARNING, "Special and mundane targets don't mix. Mundane ones ignored");
970 * These four create nodes on which to hang commands, so
971 * targets shouldn't be empty...
975 * Nothing special here -- targets can be empty if it wants.
982 * Have now parsed all the target names. Must parse the operator next. The
983 * result is left in op .
987 } else if (*cp == ':') {
995 Parse_Error(PARSE_FATAL, "Missing dependency operator");
999 cp++; /* Advance beyond operator */
1001 Lst_ForEach(&targets, ParseDoOp, &op);
1004 * Get to the first source
1006 while (*cp && isspace((unsigned char)*cp)) {
1012 * Several special targets take different actions if present with no
1014 * a .SUFFIXES line with no sources clears out all old suffixes
1015 * a .PRECIOUS line makes all targets precious
1016 * a .IGNORE line ignores errors for all targets
1017 * a .SILENT line creates silence when making all targets
1018 * a .PATH removes all directories from the search path(s).
1023 Suff_ClearSuffixes();
1029 ignoreErrors = TRUE;
1035 Lst_ForEach(&paths, ParseClearPath, NULL);
1038 Var_Set("%POSIX", "1003.2", VAR_GLOBAL);
1043 } else if (specType == MFlags) {
1045 * Call on functions in main.c to deal with these arguments and
1046 * set the initial character to a null-character so the loop to
1047 * get sources won't get anything
1049 Main_ParseArgLine(line);
1051 } else if (specType == ExShell) {
1052 if (Job_ParseShell(line) != SUCCESS) {
1053 Parse_Error(PARSE_FATAL, "improper shell specification");
1057 } else if ((specType == NotParallel) || (specType == SingleShell)) {
1062 * NOW GO FOR THE SOURCES
1064 if ((specType == Suffixes) || (specType == ExPath) ||
1065 (specType == Includes) || (specType == Libs) ||
1070 * If the target was one that doesn't take files as its sources
1071 * but takes something like suffixes, we take each
1072 * space-separated word on the line as a something and deal
1073 * with it accordingly.
1075 * If the target was .SUFFIXES, we take each source as a
1076 * suffix and add it to the list of suffixes maintained by the
1079 * If the target was a .PATH, we add the source as a directory
1080 * to search on the search path.
1082 * If it was .INCLUDES, the source is taken to be the suffix of
1083 * files which will be #included and whose search path should
1084 * be present in the .INCLUDES variable.
1086 * If it was .LIBS, the source is taken to be the suffix of
1087 * files which are considered libraries and whose search path
1088 * should be present in the .LIBS variable.
1090 * If it was .NULL, the source is the suffix to use when a file
1091 * has no valid suffix.
1094 while (*cp && !isspace((unsigned char)*cp)) {
1101 Suff_AddSuffix(line);
1104 Lst_ForEach(&paths, ParseAddDir, line);
1107 Suff_AddInclude(line);
1119 if (savech != '\0') {
1122 while (*cp && isspace((unsigned char)*cp)) {
1127 Lst_Destroy(&paths, NOFREE);
1130 Lst curSrcs = Lst_Initializer(curSrc); /* list of sources in order */
1134 * The targets take real sources, so we must beware of archive
1135 * specifications (i.e. things with left parentheses in them)
1136 * and handle them accordingly.
1138 while (*cp && !isspace((unsigned char)*cp)) {
1139 if ((*cp == '(') && (cp > line) && (cp[-1] != '$')) {
1141 * Only stop for a left parenthesis if it isn't at the
1142 * start of a word (that'll be for variable changes
1143 * later) and isn't preceded by a dollar sign (a dynamic
1154 /* list of archive source names after expansion */
1155 Lst sources = Lst_Initializer(sources);
1157 if (Arch_ParseArchive(&line, &sources, VAR_CMD) != SUCCESS) {
1158 Parse_Error(PARSE_FATAL,
1159 "Error in source archive spec \"%s\"", line);
1163 while (!Lst_IsEmpty(&sources)) {
1164 gnp = Lst_DeQueue(&sources);
1165 ParseDoSrc(tOp, gnp->name, &curSrcs);
1174 ParseDoSrc(tOp, line, &curSrcs);
1176 while (*cp && isspace((unsigned char)*cp)) {
1181 Lst_Destroy(&curSrcs, NOFREE);
1184 if (mainNode == NULL) {
1186 * If we have yet to decide on a main target to make, in the
1187 * absence of any user input, we want the first target on
1188 * the first dependency line that is actually a real target
1189 * (i.e. isn't a .USE or .EXEC rule) to be made.
1191 Lst_ForEach(&targets, ParseFindMain, NULL);
1196 *---------------------------------------------------------------------
1198 * Return TRUE if the passed line is a variable assignment. A variable
1199 * assignment consists of a single word followed by optional whitespace
1200 * followed by either a += or an = operator.
1201 * This function is used both by the Parse_File function and main when
1202 * parsing the command-line arguments.
1205 * TRUE if it is. FALSE if it ain't
1209 *---------------------------------------------------------------------
1212 Parse_IsVar(char *line)
1214 Boolean wasSpace = FALSE; /* set TRUE if found a space */
1215 Boolean haveName = FALSE; /* Set TRUE if have a variable name */
1218 #define ISEQOPERATOR(c) \
1219 (((c) == '+') || ((c) == ':') || ((c) == '?') || ((c) == '!'))
1222 * Skip to variable name
1224 for (; (*line == ' ') || (*line == '\t'); line++)
1227 for (; *line != '=' || level != 0; line++)
1231 * end-of-line -- can't be a variable assignment.
1238 * there can be as much white space as desired so long as there is
1239 * only one word before the operator
1255 if (wasSpace && haveName) {
1256 if (ISEQOPERATOR(*line)) {
1258 * We must have a finished word
1264 * When an = operator [+?!:] is found, the next
1265 * character must be an = or it ain't a valid
1272 * This is a shell command
1274 if (strncmp(line, ":sh", 3) == 0)
1279 * This is the start of another word, so not assignment.
1294 *---------------------------------------------------------------------
1296 * Take the variable assignment in the passed line and do it in the
1299 * Note: There is a lexical ambiguity with assignment modifier characters
1300 * in variable names. This routine interprets the character before the =
1301 * as a modifier. Therefore, an assignment like
1303 * is interpreted as "C+ +=" instead of "C++ =".
1309 * the variable structure of the given variable name is altered in the
1311 *---------------------------------------------------------------------
1314 Parse_DoVar(char *line, GNode *ctxt)
1316 char *cp; /* pointer into line */
1318 VAR_SUBST, VAR_APPEND, VAR_SHELL, VAR_NORMAL
1319 } type; /* Type of assignment */
1320 char *opc; /* ptr to operator character to
1321 * null-terminate the variable name */
1323 * Avoid clobbered variable warnings by forcing the compiler
1324 * to ``unregister'' variables
1332 * Skip to variable name
1334 while ((*line == ' ') || (*line == '\t')) {
1339 * Skip to operator character, nulling out whitespace as we go
1341 for (cp = line + 1; *cp != '='; cp++) {
1342 if (isspace((unsigned char)*cp)) {
1346 opc = cp - 1; /* operator is the previous character */
1347 *cp++ = '\0'; /* nuke the = */
1350 * Check operator type
1360 * If the variable already has a value, we don't do anything.
1363 if (Var_Exists(line, ctxt)) {
1388 if (strncmp(opc, ":sh", 3) == 0) {
1398 while (isspace((unsigned char)*cp)) {
1402 if (type == VAR_APPEND) {
1403 Var_Append(line, cp, ctxt);
1404 } else if (type == VAR_SUBST) {
1406 * Allow variables in the old value to be undefined, but leave their
1407 * invocation alone -- this is done by forcing oldVars to be false.
1408 * XXX: This can cause recursive variables, but that's not hard to do,
1409 * and this allows someone to do something like
1411 * CFLAGS = $(.INCLUDES)
1412 * CFLAGS := -I.. $(CFLAGS)
1414 * And not get an error.
1416 Boolean oldOldVars = oldVars;
1421 * make sure that we set the variable the first time to nothing
1422 * so that it gets substituted!
1424 if (!Var_Exists(line, ctxt))
1425 Var_Set(line, "", ctxt);
1427 cp = Var_Subst(NULL, cp, ctxt, FALSE);
1428 oldVars = oldOldVars;
1430 Var_Set(line, cp, ctxt);
1432 } else if (type == VAR_SHELL) {
1433 Boolean freeCmd = FALSE; /* TRUE if the command needs to be freed, i.e.
1434 * if any variable expansion was performed */
1438 if (strchr(cp, '$') != NULL) {
1440 * There's a dollar sign in the command, so perform variable
1441 * expansion on the whole thing. The resulting string will need
1442 * freeing when we're done, so set freeCmd to TRUE.
1444 cp = Var_Subst(NULL, cp, VAR_CMD, TRUE);
1448 res = Cmd_Exec(cp, &error);
1449 Var_Set(line, res, ctxt);
1453 Parse_Error(PARSE_WARNING, error, cp);
1459 * Normal assignment -- just do it.
1461 Var_Set(line, cp, ctxt);
1467 * Lst_ForEach function to add a command line to all targets
1473 * A new element is added to the commands list of the node.
1476 ParseAddCmd(void *gnp, void *cmd)
1480 /* if target already supplied, ignore commands */
1481 if (!(gn->type & OP_HAS_COMMANDS))
1482 Lst_AtEnd(&gn->commands, cmd);
1484 Parse_Error(PARSE_WARNING,
1485 "duplicate script for target \"%s\" ignored",
1491 *-----------------------------------------------------------------------
1492 * ParseHasCommands --
1493 * Callback procedure for Parse_File when destroying the list of
1494 * targets on the last dependency line. Marks a target as already
1495 * having commands if it does, to keep from having shell commands
1496 * on multiple dependency lines.
1502 * OP_HAS_COMMANDS may be set for the target.
1504 *-----------------------------------------------------------------------
1507 ParseHasCommands(void *gnp)
1511 if (!Lst_IsEmpty(&gn->commands)) {
1512 gn->type |= OP_HAS_COMMANDS;
1517 *-----------------------------------------------------------------------
1518 * Parse_AddIncludeDir --
1519 * Add a directory to the path searched for included makefiles
1520 * bracketed by double-quotes. Used by functions in main.c
1526 * The directory is appended to the list.
1528 *-----------------------------------------------------------------------
1531 Parse_AddIncludeDir(char *dir)
1534 Dir_AddDir(&parseIncPath, dir);
1537 /*---------------------------------------------------------------------
1539 * Handle error directive
1541 * The input is the line minus the ".error". We substitute variables,
1542 * print the message and exit(1) or just print a warning if the ".error"
1543 * directive is malformed.
1545 *---------------------------------------------------------------------
1548 ParseDoError(char *errmsg)
1551 if (!isspace((unsigned char)*errmsg)) {
1552 Parse_Error(PARSE_WARNING, "invalid syntax: .error%s", errmsg);
1556 while (isspace((unsigned char)*errmsg))
1559 errmsg = Var_Subst(NULL, errmsg, VAR_GLOBAL, FALSE);
1561 Parse_Error(PARSE_FATAL, "%s", errmsg);
1562 /* Terminate immediately. */
1566 /*---------------------------------------------------------------------
1568 * Handle warning directive
1570 * The input is the line minus the ".warning". We substitute variables
1571 * and print the message or just print a warning if the ".warning"
1572 * directive is malformed.
1574 *---------------------------------------------------------------------
1577 ParseDoWarning(char *warnmsg)
1580 if (!isspace((unsigned char)*warnmsg)) {
1581 Parse_Error(PARSE_WARNING, "invalid syntax: .warning%s",
1586 while (isspace((unsigned char)*warnmsg))
1589 warnmsg = Var_Subst(NULL, warnmsg, VAR_GLOBAL, FALSE);
1591 Parse_Error(PARSE_WARNING, "%s", warnmsg);
1595 *---------------------------------------------------------------------
1597 * Push to another file.
1599 * The input is the line minus the #include. A file spec is a string
1600 * enclosed in <> or "". The former is looked for only in sysIncPath.
1601 * The latter in . and the directories specified by -I command line
1608 * A structure is added to the includes Lst and readProc, curFile.lineno,
1609 * curFile.fname and curFile.F are altered for the new file
1610 *---------------------------------------------------------------------
1613 ParseDoInclude(char *file)
1615 char *fullname; /* full pathname of file */
1616 IFile *oldFile; /* state associated with current file */
1617 char endc; /* the character which ends the file spec */
1618 char *cp; /* current position in file spec */
1619 Boolean isSystem; /* TRUE if makefile is a system makefile */
1622 * Skip to delimiter character so we know where to look
1624 while ((*file == ' ') || (*file == '\t')) {
1628 if ((*file != '"') && (*file != '<')) {
1629 Parse_Error(PARSE_FATAL,
1630 ".include filename must be delimited by '\"' or '<'");
1635 * Set the search path on which to find the include file based on the
1636 * characters which bracket its name. Angle-brackets imply it's
1637 * a system Makefile while double-quotes imply it's a user makefile
1648 * Skip to matching delimiter
1650 for (cp = ++file; *cp && *cp != endc; cp++) {
1655 Parse_Error(PARSE_FATAL,
1656 "Unclosed %cinclude filename. '%c' expected",
1663 * Substitute for any variables in the file name before trying to
1666 file = Var_Subst(NULL, file, VAR_CMD, FALSE);
1669 * Now we know the file's name and its search path, we attempt to
1670 * find the durn thing. A return of NULL indicates the file don't
1675 * Include files contained in double-quotes are first searched for
1676 * relative to the including file's location. We don't want to
1677 * cd there, of course, so we just tack on the old file's
1678 * leading path components and call Dir_FindFile to see if
1679 * we can locate the beast.
1681 char *prefEnd, *Fname;
1683 /* Make a temporary copy of this, to be safe. */
1684 Fname = estrdup(curFile.fname);
1686 prefEnd = strrchr(Fname, '/');
1687 if (prefEnd != (char *)NULL) {
1692 newName = estrdup(file);
1694 newName = str_concat(Fname, file, STR_ADDSLASH);
1695 fullname = Dir_FindFile(newName, &parseIncPath);
1696 if (fullname == NULL) {
1697 fullname = Dir_FindFile(newName, &dirSearchPath);
1709 if (fullname == NULL) {
1711 * System makefile or makefile wasn't found in same directory as
1712 * included makefile. Search for it first on the -I search path,
1713 * then on the .PATH search path, if not found in a -I directory.
1714 * XXX: Suffix specific?
1716 fullname = Dir_FindFile(file, &parseIncPath);
1717 if (fullname == NULL) {
1718 fullname = Dir_FindFile(file, &dirSearchPath);
1722 if (fullname == NULL) {
1724 * Still haven't found the makefile. Look for it on the system
1725 * path as a last resort.
1727 fullname = Dir_FindFile(file, &sysIncPath);
1730 if (fullname == NULL) {
1732 Parse_Error(PARSE_FATAL, "Could not find %s", file);
1739 * Once we find the absolute path to the file, we get to save all the
1740 * state from the current file before we can start reading this
1741 * include file. The state is stored in an IFile structure which
1742 * is placed on a list with other IFile structures. The list makes
1743 * a very nice stack to track how we got here...
1745 oldFile = emalloc(sizeof(IFile));
1746 memcpy(oldFile, &curFile, sizeof(IFile));
1748 Lst_AtFront(&includes, oldFile);
1751 * Once the previous state has been saved, we can get down to reading
1752 * the new file. We set up the name of the file to be the absolute
1753 * name of the include file so error messages refer to the right
1754 * place. Naturally enough, we start reading at line number 0.
1756 curFile.fname = fullname;
1759 curFile.F = fopen(fullname, "r");
1761 if (curFile.F == NULL) {
1762 Parse_Error(PARSE_FATAL, "Cannot open %s", fullname);
1764 * Pop to previous file
1768 Var_Append(".MAKEFILE_LIST", fullname, VAR_GLOBAL);
1773 *---------------------------------------------------------------------
1774 * Parse_FromString --
1775 * Start Parsing from the given string
1781 * A structure is added to the includes Lst and readProc, curFile.lineno,
1782 * curFile.fname and curFile.F are altered for the new file
1783 *---------------------------------------------------------------------
1786 Parse_FromString(char *str, int lineno)
1788 IFile *oldFile; /* state associated with this file */
1790 DEBUGF(FOR, ("%s\n---- at line %d\n", str, lineno));
1792 oldFile = emalloc(sizeof(IFile));
1793 memcpy(oldFile, &curFile, sizeof(IFile));
1795 Lst_AtFront(&includes, oldFile);
1798 curFile.p = emalloc(sizeof(PTR));
1799 curFile.p->str = curFile.p->ptr = str;
1800 curFile.lineno = lineno;
1801 curFile.fname = estrdup(curFile.fname);
1806 *---------------------------------------------------------------------
1807 * ParseTraditionalInclude --
1808 * Push to another file.
1810 * The input is the line minus the "include". The file name is
1811 * the string following the "include".
1817 * A structure is added to the includes Lst and readProc, curFile.lineno,
1818 * curFile.fname and curFile.F are altered for the new file
1819 *---------------------------------------------------------------------
1822 ParseTraditionalInclude(char *file)
1824 char *fullname; /* full pathname of file */
1825 IFile *oldFile; /* state associated with current file */
1826 char *cp; /* current position in file spec */
1829 * Skip over whitespace
1831 while ((*file == ' ') || (*file == '\t')) {
1835 if (*file == '\0') {
1836 Parse_Error(PARSE_FATAL,
1837 "Filename missing from \"include\"");
1842 * Skip to end of line or next whitespace
1844 for (cp = file; *cp && *cp != '\n' && *cp != '\t' && *cp != ' '; cp++) {
1851 * Substitute for any variables in the file name before trying to
1854 file = Var_Subst(NULL, file, VAR_CMD, FALSE);
1857 * Now we know the file's name, we attempt to find the durn thing.
1858 * Search for it first on the -I search path, then on the .PATH
1859 * search path, if not found in a -I directory.
1861 fullname = Dir_FindFile(file, &parseIncPath);
1862 if (fullname == NULL) {
1863 fullname = Dir_FindFile(file, &dirSearchPath);
1866 if (fullname == NULL) {
1868 * Still haven't found the makefile. Look for it on the system
1869 * path as a last resort.
1871 fullname = Dir_FindFile(file, &sysIncPath);
1874 if (fullname == NULL) {
1875 Parse_Error(PARSE_FATAL, "Could not find %s", file);
1880 * Once we find the absolute path to the file, we get to save all the
1881 * state from the current file before we can start reading this
1882 * include file. The state is stored in an IFile structure which
1883 * is placed on a list with other IFile structures. The list makes
1884 * a very nice stack to track how we got here...
1886 oldFile = emalloc(sizeof(IFile));
1887 memcpy(oldFile, &curFile, sizeof(IFile));
1889 Lst_AtFront(&includes, oldFile);
1892 * Once the previous state has been saved, we can get down to reading
1893 * the new file. We set up the name of the file to be the absolute
1894 * name of the include file so error messages refer to the right
1895 * place. Naturally enough, we start reading at line number 0.
1897 curFile.fname = fullname;
1900 curFile.F = fopen(fullname, "r");
1902 if (curFile.F == NULL) {
1903 Parse_Error(PARSE_FATAL, "Cannot open %s", fullname);
1905 * Pop to previous file
1909 Var_Append(".MAKEFILE_LIST", fullname, VAR_GLOBAL);
1915 *---------------------------------------------------------------------
1917 * Called when EOF is reached in the current file. If we were reading
1918 * an include file, the includes stack is popped and things set up
1919 * to go back to reading the previous file at the previous location.
1922 * CONTINUE if there's more to do. DONE if not.
1925 * The old curFile.F is closed. The includes list is shortened.
1926 * curFile.lineno, curFile.F, and curFile.fname are changed if
1927 * CONTINUE is returned.
1928 *---------------------------------------------------------------------
1931 ParseEOF(int opened)
1933 IFile *ifile; /* the state on the top of the includes stack */
1935 if (Lst_IsEmpty(&includes)) {
1936 Var_Append(".MAKEFILE_LIST", "..", VAR_GLOBAL);
1940 ifile = Lst_DeQueue(&includes);
1941 free(curFile.fname);
1942 if (opened && curFile.F) {
1944 Var_Append(".MAKEFILE_LIST", "..", VAR_GLOBAL);
1947 free(curFile.p->str);
1950 memcpy(&curFile, ifile, sizeof(IFile));
1956 *---------------------------------------------------------------------
1958 * Read a character from the current file
1961 * The character that was read
1964 *---------------------------------------------------------------------
1971 return (fgetc(curFile.F));
1973 if (curFile.p && *curFile.p->ptr)
1974 return (*curFile.p->ptr++);
1980 *---------------------------------------------------------------------
1982 * Put back a character to the current file
1988 *---------------------------------------------------------------------
1995 ungetc(c, curFile.F);
1999 *--(curFile.p->ptr) = c;
2006 * Grab the next line unless it begins with a dot (`.') and we're told to
2007 * ignore such lines.
2010 ParseSkipLine(int skip, int keep_newline)
2014 size_t lineLength = 0;
2017 buf = Buf_Init(MAKE_BSIZE);
2023 while (((c = ParseReadc()) != '\n' || lastc == '\\')
2025 if (skip && c == '#' && lastc != '\\') {
2026 /* let a comment be terminated even by an escaped \n.
2027 * This is consistent to comment handling in ParseReadLine */
2028 while ((c = ParseReadc()) != '\n' && c != EOF)
2034 Buf_AddByte(buf, (Byte)c);
2036 Buf_ReplaceLastByte(buf, (Byte)' ');
2039 while ((c = ParseReadc()) == ' ' || c == '\t')
2046 Buf_AddByte(buf, (Byte)c);
2051 Parse_Error(PARSE_FATAL, "Unclosed conditional/for loop");
2052 Buf_Destroy(buf, TRUE);
2057 Buf_AddByte(buf, (Byte)'\0');
2058 line = (char *)Buf_GetAll(buf, &lineLength);
2059 } while (skip == 1 && line[0] != '.');
2061 Buf_Destroy(buf, FALSE);
2067 *---------------------------------------------------------------------
2069 * Read an entire line from the input file. Called only by Parse_File.
2070 * To facilitate escaped newlines and what have you, a character is
2071 * buffered in 'lastc', which is '\0' when no characters have been
2072 * read. When we break out of the loop, c holds the terminating
2073 * character and lastc holds a character that should be added to
2074 * the line (unless we don't read anything but a terminator).
2077 * A line w/o its newline
2080 * Only those associated with reading a character
2081 *---------------------------------------------------------------------
2086 Buffer *buf; /* Buffer for current line */
2087 int c; /* the current character */
2088 int lastc; /* The most-recent character */
2089 Boolean semiNL; /* treat semi-colons as newlines */
2090 Boolean ignDepOp; /* TRUE if should ignore dependency operators
2091 * for the purposes of setting semiNL */
2092 Boolean ignComment; /* TRUE if should ignore comments (in a
2094 char *line; /* Result */
2095 char *ep; /* to strip trailing blanks */
2096 size_t lineLength; /* Length of result */
2097 int lineno; /* Saved line # */
2104 * Handle special-characters at the beginning of the line. Either a
2105 * leading tab (shell command) or pound-sign (possible conditional)
2106 * forces us to ignore comments and dependency operators and treat
2107 * semi-colons as semi-colons (by leaving semiNL FALSE). This also
2108 * discards completely blank lines.
2114 ignComment = ignDepOp = TRUE;
2116 } else if (c == '\n') {
2118 } else if (c == '#') {
2123 * Anything else breaks out without doing anything
2131 buf = Buf_Init(MAKE_BSIZE);
2133 while (((c = ParseReadc()) != '\n' || (lastc == '\\')) &&
2140 * Escaped newline: read characters until a non-space or an
2141 * unescaped newline and replace them all by a single space.
2142 * This is done by storing the space over the backslash and
2143 * dropping through with the next nonspace. If it is a
2144 * semi-colon and semiNL is TRUE, it will be recognized as a
2145 * newline in the code below this...
2149 while ((c = ParseReadc()) == ' ' || c == '\t') {
2152 if (c == EOF || c == '\n') {
2156 * Check for comments, semiNL's, etc. -- easier than
2157 * ParseUnreadc(c); continue;
2166 * Semi-colon: Need to see if it should be interpreted as a
2171 * To make sure the command that may be following this
2172 * semi-colon begins with a tab, we push one back into the
2173 * input stream. This will overwrite the semi-colon in the
2174 * buffer. If there is no command following, this does no
2175 * harm, since the newline remains in the buffer and the
2176 * whole line is ignored.
2185 * Haven't seen a dependency operator before this, so this
2186 * must be a variable assignment -- don't pay attention to
2187 * dependency operators after this.
2190 } else if (lastc == ':' || lastc == '!') {
2192 * Well, we've seen a dependency operator already, but it
2193 * was the previous character, so this is really just an
2194 * expanded variable assignment. Revert semi-colons to
2195 * being just semi-colons again and ignore any more
2196 * dependency operators.
2198 * XXX: Note that a line like "foo : a:=b" will blow up,
2199 * but who'd write a line like that anyway?
2207 if (lastc != '\\') {
2209 * If the character is a hash mark and it isn't escaped
2210 * (or we're being compatible), the thing is a comment.
2211 * Skip to the end of the line.
2215 } while ((c != '\n') && (c != EOF));
2219 * Don't add the backslash. Just let the # get copied
2229 if (!ignDepOp && (c == ':' || c == '!')) {
2231 * A semi-colon is recognized as a newline only on
2232 * dependency lines. Dependency lines are lines with a
2233 * colon or an exclamation point. Ergo...
2242 * Copy in the previous character and save this one in lastc.
2244 Buf_AddByte(buf, (Byte)lastc);
2251 if (lastc != '\0') {
2252 Buf_AddByte(buf, (Byte)lastc);
2254 Buf_AddByte(buf, (Byte)'\0');
2255 line = (char *)Buf_GetAll(buf, &lineLength);
2256 Buf_Destroy(buf, FALSE);
2259 * Strip trailing blanks and tabs from the line.
2260 * Do not strip a blank or tab that is preceded by
2266 while (ep > line + 1 && (ep[-1] == ' ' || ep[-1] == '\t')) {
2267 if (ep > line + 1 && ep[-2] == '\\')
2273 if (line[0] == '.') {
2275 * The line might be a conditional. Ask the conditional module
2276 * about it and act accordingly
2278 switch (Cond_Eval(line)) {
2281 * Skip to next conditional that evaluates to COND_PARSE.
2285 line = ParseSkipLine(1, 0);
2286 } while (line && Cond_Eval(line) != COND_PARSE);
2292 line = ParseReadLine();
2295 if (For_Eval(line)) {
2298 lineno = curFile.lineno;
2301 * Skip after the matching end
2303 line = ParseSkipLine(0, 1);
2305 Parse_Error(PARSE_FATAL,
2306 "Unexpected end of file in for loop.\n");
2309 ok = For_Eval(line);
2315 line = ParseReadLine();
2326 * Hit end-of-file, so return a NULL line to indicate this.
2333 *-----------------------------------------------------------------------
2334 * ParseFinishLine --
2335 * Handle the end of a dependency group.
2341 * inLine set FALSE. 'targets' list destroyed.
2343 *-----------------------------------------------------------------------
2346 ParseFinishLine(void)
2350 Lst_ForEach(&targets, Suff_EndTransform, NULL);
2351 Lst_Destroy(&targets, ParseHasCommands);
2357 stripvarname(char *cp)
2361 while (isspace((unsigned char)*cp))
2364 while (*cp2 && !isspace((unsigned char)*cp2))
2372 *---------------------------------------------------------------------
2374 * Parse a file into its component parts, incorporating it into the
2375 * current dependency graph. This is the main function and controls
2376 * almost every other function in this module
2382 * Loads. Nodes are added to the list of all targets, nodes and links
2383 * are added to the dependency graph. etc. etc. etc.
2384 *---------------------------------------------------------------------
2387 Parse_File(const char *name, FILE *stream)
2389 char *cp, /* pointer into the line */
2390 *line; /* the line we're working on */
2393 curFile.fname = name;
2398 Var_Append(".MAKEFILE_LIST", name, VAR_GLOBAL);
2401 while ((line = ParseReadLine()) != NULL) {
2404 * Lines that begin with the special character are either
2405 * include or undef directives.
2407 for (cp = line + 1; isspace((unsigned char)*cp); cp++) {
2410 if (strncmp(cp, "include", 7) == 0) {
2411 ParseDoInclude(cp + 7);
2413 } else if (strncmp(cp, "error", 5) == 0) {
2414 ParseDoError(cp + 5);
2416 } else if (strncmp(cp, "warning", 7) == 0) {
2417 ParseDoWarning(cp + 7);
2419 } else if (strncmp(cp, "undef", 5) == 0) {
2420 cp = stripvarname(cp + 5);
2421 cp = Var_Subst(NULL, cp, VAR_CMD, FALSE);
2422 Var_Delete(cp, VAR_GLOBAL);
2424 } else if (strncmp(cp, "makeenv", 7) == 0) {
2425 cp = stripvarname(cp + 7);
2426 Var_SetEnv(cp, VAR_GLOBAL);
2431 /* If we're this far, the line must be a comment. */
2435 if (*line == '\t') {
2437 * If a line starts with a tab, it can only hope to be
2438 * a creation command.
2440 for (cp = line + 1; isspace((unsigned char)*cp); cp++) {
2446 * So long as it's not a blank line and we're actually
2447 * in a dependency spec, add the command to the list of
2448 * commands of all targets in the dependency spec
2450 Lst_ForEach(&targets, ParseAddCmd, cp);
2453 Parse_Error(PARSE_FATAL,
2454 "Unassociated shell command \"%s\"",
2459 } else if (strncmp(line, "include", 7) == 0 &&
2460 isspace((unsigned char)line[7]) &&
2461 strchr(line, ':') == NULL) {
2463 * It's an S3/S5-style "include".
2465 ParseTraditionalInclude(line + 7);
2468 } else if (Parse_IsVar(line)) {
2470 Parse_DoVar(line, VAR_GLOBAL);
2473 * We now know it's a dependency line so it needs to have all
2474 * variables expanded before being parsed. Tell the variable
2475 * module to complain if some variable is undefined...
2476 * To make life easier on novices, if the line is indented we
2477 * first make sure the line has a dependency operator in it.
2478 * If it doesn't have an operator and we're in a dependency
2479 * line's script, we assume it's actually a shell command
2480 * and add it to the current list of targets.
2483 if (isspace((unsigned char)line[0])) {
2484 while ((*cp != '\0') && isspace((unsigned char)*cp)) {
2494 cp = Var_Subst(NULL, line, VAR_CMD, TRUE);
2499 * Need a non-circular list for the target nodes
2501 Lst_Destroy(&targets, NOFREE);
2504 ParseDoDependency(line);
2512 * Reached EOF, but it may be just EOF of an include file...
2514 } while (ParseEOF(1) == CONTINUE);
2517 * Make sure conditionals are clean
2522 errx(1, "fatal errors encountered -- cannot continue");
2526 *---------------------------------------------------------------------
2528 * initialize the parsing module
2534 * the parseIncPath list is initialized...
2535 *---------------------------------------------------------------------
2545 *-----------------------------------------------------------------------
2547 * Return a Lst of the main target to create for main()'s sake. If
2548 * no such target exists, we Punt with an obnoxious error message.
2551 * A Lst of the single node to create.
2556 *-----------------------------------------------------------------------
2559 Parse_MainName(Lst *listmain)
2562 if (mainNode == NULL) {
2563 Punt("no target to make.");
2565 } else if (mainNode->type & OP_DOUBLEDEP) {
2566 Lst_AtEnd(listmain, mainNode);
2567 Lst_Concat(listmain, &mainNode->cohorts, LST_CONCNEW);
2570 Lst_AtEnd(listmain, mainNode);