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.29 2005/01/05 23:28:20 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.
92 #include "pathnames.h"
95 * These values are returned by ParseEOF to tell Parse_File whether to
96 * CONTINUE parsing, i.e. it had only reached the end of an include file,
102 /* targets we're working on */
103 static Lst targets = Lst_Initializer(targets);
105 static Boolean inLine; /* true if currently in a dependency
106 * line or its commands */
107 static int fatals = 0;
109 static GNode *mainNode; /* The main target to create. This is the
110 * first target on the first dependency
111 * line in the first makefile */
113 IFile curFile; /* current makefile */
115 /* stack of IFiles generated by * #includes */
116 static Lst includes = Lst_Initializer(includes);
118 /* list of directories for "..." includes */
119 Lst parseIncPath = Lst_Initializer(parseIncPath);
121 /* list of directories for <...> includes */
122 Lst sysIncPath = Lst_Initializer(sysIncPath);
125 * specType contains the SPECial TYPE of the current target. It is
126 * Not if the target is unspecial. If it *is* special, however, the children
127 * are linked as children of the parent but not vice versa. This variable is
128 * set in ParseDoDependency
132 Default, /* .DEFAULT */
134 Ignore, /* .IGNORE */
135 Includes, /* .INCLUDES */
136 Interrupt, /* .INTERRUPT */
138 MFlags, /* .MFLAGS or .MAKEFLAGS */
139 Main, /* .MAIN and we don't have anything user-specified to
141 NoExport, /* .NOEXPORT */
142 Not, /* Not special */
143 NotParallel, /* .NOTPARALELL */
146 Parallel, /* .PARALLEL */
150 Precious, /* .PRECIOUS */
151 ExShell, /* .SHELL */
152 Silent, /* .SILENT */
153 SingleShell, /* .SINGLESHELL */
154 Suffixes, /* .SUFFIXES */
156 Attribute /* Generic attribute */
159 static ParseSpecial specType;
163 * Predecessor node for handling .ORDER. Initialized to NULL when .ORDER
164 * seen, then set to each successive source on the line.
166 static GNode *predecessor;
169 * The parseKeywords table is searched using binary search when deciding
170 * if a target or source is special. The 'spec' field is the ParseSpecial
171 * type of the keyword ("Not" if the keyword isn't special as a target) while
172 * the 'op' field is the operator to apply to the list of targets if the
173 * keyword is used as a source ("0" if the keyword isn't special as a source)
176 char *name; /* Name of keyword */
177 ParseSpecial spec; /* Type when used as a target */
178 int op; /* Operator when used as a source */
179 } parseKeywords[] = {
180 { ".BEGIN", Begin, 0 },
181 { ".DEFAULT", Default, 0 },
183 { ".EXEC", Attribute, OP_EXEC },
184 { ".IGNORE", Ignore, OP_IGNORE },
185 { ".INCLUDES", Includes, 0 },
186 { ".INTERRUPT", Interrupt, 0 },
187 { ".INVISIBLE", Attribute, OP_INVISIBLE },
188 { ".JOIN", Attribute, OP_JOIN },
189 { ".LIBS", Libs, 0 },
190 { ".MAIN", Main, 0 },
191 { ".MAKE", Attribute, OP_MAKE },
192 { ".MAKEFLAGS", MFlags, 0 },
193 { ".MFLAGS", MFlags, 0 },
194 { ".NOTMAIN", Attribute, OP_NOTMAIN },
195 { ".NOTPARALLEL", NotParallel, 0 },
196 { ".NO_PARALLEL", NotParallel, 0 },
197 { ".NULL", Null, 0 },
198 { ".OPTIONAL", Attribute, OP_OPTIONAL },
199 { ".ORDER", Order, 0 },
200 { ".PARALLEL", Parallel, 0 },
201 { ".PATH", ExPath, 0 },
202 { ".PHONY", Phony, OP_PHONY },
203 { ".POSIX", Posix, 0 },
204 { ".PRECIOUS", Precious, OP_PRECIOUS },
205 { ".RECURSIVE", Attribute, OP_MAKE },
206 { ".SHELL", ExShell, 0 },
207 { ".SILENT", Silent, OP_SILENT },
208 { ".SINGLESHELL", SingleShell, 0 },
209 { ".SUFFIXES", Suffixes, 0 },
210 { ".USE", Attribute, OP_USE },
211 { ".WAIT", Wait, 0 },
214 static int ParseFindKeyword(char *);
215 static int ParseLinkSrc(void *, void *);
216 static int ParseDoOp(void *, void *);
217 static int ParseAddDep(void *, void *);
218 static void ParseDoSrc(int, char *, Lst *);
219 static int ParseFindMain(void *, void *);
220 static int ParseAddDir(void *, void *);
221 static int ParseClearPath(void *, void *);
222 static void ParseDoDependency(char *);
223 static int ParseAddCmd(void *, void *);
224 static int ParseReadc(void);
225 static void ParseUnreadc(int);
226 static void ParseHasCommands(void *);
227 static void ParseDoInclude(char *);
228 static void ParseDoError(char *);
229 static void ParseDoWarning(char *);
231 static void ParseTraditionalInclude(char *);
233 static int ParseEOF(int);
234 static char *ParseReadLine(void);
235 static char *ParseSkipLine(int, int);
236 static void ParseFinishLine(void);
239 *----------------------------------------------------------------------
240 * ParseFindKeyword --
241 * Look in the table of keywords for one matching the given string.
244 * The index of the keyword, or -1 if it isn't there.
248 *----------------------------------------------------------------------
251 ParseFindKeyword(char *str)
259 end = (sizeof(parseKeywords) / sizeof(parseKeywords[0])) - 1;
262 cur = start + ((end - start) / 2);
263 diff = strcmp(str, parseKeywords[cur].name);
267 } else if (diff < 0) {
272 } while (start <= end);
278 * Error message abort function for parsing. Prints out the context
279 * of the error (line number and file) as well as the message with
280 * two optional arguments.
286 * "fatals" is incremented if the level is PARSE_FATAL.
290 Parse_Error(int type, const char *fmt, ...)
295 fprintf(stderr, "\"%s\", line %d: ",
296 curFile.fname, curFile.lineno);
297 if (type == PARSE_WARNING)
298 fprintf(stderr, "warning: ");
299 vfprintf(stderr, fmt, ap);
301 fprintf(stderr, "\n");
303 if (type == PARSE_FATAL)
308 *---------------------------------------------------------------------
310 * Link the parent node to its new child. Used in a Lst_ForEach by
311 * ParseDoDependency. If the specType isn't 'Not', the parent
312 * isn't linked as a parent of the child.
318 * New elements are added to the parents list of cgn and the
319 * children list of cgn. the unmade field of pgn is updated
320 * to reflect the additional child.
321 *---------------------------------------------------------------------
324 ParseLinkSrc(void *pgnp, void *cgnp)
329 if (Lst_Member(&pgn->children, cgn) == NULL) {
330 Lst_AtEnd(&pgn->children, cgn);
331 if (specType == Not) {
332 Lst_AtEnd(&cgn->parents, pgn);
340 *---------------------------------------------------------------------
342 * Apply the parsed operator to the given target node. Used in a
343 * Lst_ForEach call by ParseDoDependency once all targets have
344 * been found and their operator parsed. If the previous and new
345 * operators are incompatible, a major error is taken.
351 * The type field of the node is altered to reflect any new bits in
353 *---------------------------------------------------------------------
356 ParseDoOp(void *gnp, void *opp)
359 int op = *(int *)opp;
362 * If the dependency mask of the operator and the node don't match and
363 * the node has actually had an operator applied to it before, and
364 * the operator actually has some dependency information in it, complain.
366 if (((op & OP_OPMASK) != (gn->type & OP_OPMASK)) &&
367 !OP_NOP(gn->type) && !OP_NOP(op))
369 Parse_Error(PARSE_FATAL, "Inconsistent operator for %s", gn->name);
373 if ((op == OP_DOUBLEDEP) && ((gn->type & OP_OPMASK) == OP_DOUBLEDEP)) {
375 * If the node was the object of a :: operator, we need to create a
376 * new instance of it for the children and commands on this dependency
377 * line. The new instance is placed on the 'cohorts' list of the
378 * initial one (note the initial one is not on its own cohorts list)
379 * and the new instance is linked to all parents of the initial
385 cohort = Targ_NewGN(gn->name);
387 * Duplicate links to parents so graph traversal is simple. Perhaps
388 * some type bits should be duplicated?
390 * Make the cohort invisible as well to avoid duplicating it into
391 * other variables. True, parents of this target won't tend to do
392 * anything with their local variables, but better safe than
395 Lst_ForEach(&gn->parents, ParseLinkSrc, cohort);
396 cohort->type = OP_DOUBLEDEP|OP_INVISIBLE;
397 Lst_AtEnd(&gn->cohorts, cohort);
400 * Replace the node in the targets list with the new copy
402 ln = Lst_Member(&targets, gn);
403 Lst_Replace(ln, cohort);
407 * We don't want to nuke any previous flags (whatever they were) so we
408 * just OR the new operator into the old
416 *---------------------------------------------------------------------
418 * Check if the pair of GNodes given needs to be synchronized.
419 * This has to be when two nodes are on different sides of a
423 * Returns 1 if the two targets need to be ordered, 0 otherwise.
424 * If it returns 1, the search can stop
427 * A dependency can be added between the two nodes.
429 *---------------------------------------------------------------------
432 ParseAddDep(void *pp, void *sp)
437 if (p->order < s->order) {
439 * XXX: This can cause loops, and loops can cause unmade targets,
440 * but checking is tedious, and the debugging output can show the
443 Lst_AtEnd(&p->successors, s);
444 Lst_AtEnd(&s->preds, p);
453 *---------------------------------------------------------------------
455 * Given the name of a source, figure out if it is an attribute
456 * and apply it to the targets if it is. Else decide if there is
457 * some attribute which should be applied *to* the source because
458 * of some special target and apply it if so. Otherwise, make the
459 * source be a child of the targets in the list 'targets'
465 * Operator bits may be added to the list of targets or to the source.
466 * The targets may have a new source added to their lists of children.
467 *---------------------------------------------------------------------
470 ParseDoSrc(int tOp, char *src, Lst *allsrc)
474 if (*src == '.' && isupper ((unsigned char) src[1])) {
475 int keywd = ParseFindKeyword(src);
477 int op = parseKeywords[keywd].op;
479 Lst_ForEach(&targets, ParseDoOp, &op);
482 if (parseKeywords[keywd].spec == Wait) {
492 * If we have noted the existence of a .MAIN, it means we need
493 * to add the sources of said target to the list of things
494 * to create. The string 'src' is likely to be free, so we
495 * must make a new copy of it. Note that this will only be
496 * invoked if the user didn't specify a target on the command
497 * line. This is to allow #ifmake's to succeed, or something...
499 Lst_AtEnd(&create, estrdup(src));
501 * Add the name to the .TARGETS variable as well, so the user cna
502 * employ that, if desired.
504 Var_Append(".TARGETS", src, VAR_GLOBAL);
509 * Create proper predecessor/successor links between the previous
510 * source and the current one.
512 gn = Targ_FindNode(src, TARG_CREATE);
513 if (predecessor != NULL) {
514 Lst_AtEnd(&predecessor->successors, gn);
515 Lst_AtEnd(&gn->preds, predecessor);
518 * The current source now becomes the predecessor for the next one.
525 * If the source is not an attribute, we need to find/create
526 * a node for it. After that we can apply any operator to it
527 * from a special target or link it to its parents, as
530 * In the case of a source that was the object of a :: operator,
531 * the attribute is applied to all of its instances (as kept in
532 * the 'cohorts' list of the node) or all the cohorts are linked
533 * to all the targets.
535 gn = Targ_FindNode(src, TARG_CREATE);
539 Lst_ForEach(&targets, ParseLinkSrc, gn);
541 if ((gn->type & OP_OPMASK) == OP_DOUBLEDEP) {
545 for (ln = Lst_First(&gn->cohorts); ln != NULL; ln = Lst_Succ(ln)) {
546 cohort = Lst_Datum(ln);
550 Lst_ForEach(&targets, ParseLinkSrc, cohort);
558 Lst_AtEnd(allsrc, gn);
560 Lst_ForEach(allsrc, ParseAddDep, gn);
565 *-----------------------------------------------------------------------
567 * Find a real target in the list and set it to be the main one.
568 * Called by ParseDoDependency when a main target hasn't been found
572 * 0 if main not found yet, 1 if it is.
575 * mainNode is changed and Targ_SetMain is called.
577 *-----------------------------------------------------------------------
580 ParseFindMain(void *gnp, void *dummy __unused)
584 if ((gn->type & (OP_NOTMAIN | OP_USE | OP_EXEC | OP_TRANSFORM)) == 0) {
594 *-----------------------------------------------------------------------
596 * Front-end for Dir_AddDir to make sure Lst_ForEach keeps going
604 *-----------------------------------------------------------------------
607 ParseAddDir(void *path, void *name)
610 Dir_AddDir(path, name);
615 *-----------------------------------------------------------------------
617 * Front-end for Dir_ClearPath to make sure Lst_ForEach keeps going
625 *-----------------------------------------------------------------------
628 ParseClearPath(void *path, void *dummy __unused)
636 *---------------------------------------------------------------------
637 * ParseDoDependency --
638 * Parse the dependency line in line.
644 * The nodes of the sources are linked as children to the nodes of the
645 * targets. Some nodes may be created.
647 * We parse a dependency line by first extracting words from the line and
648 * finding nodes in the list of all targets with that name. This is done
649 * until a character is encountered which is an operator character. Currently
650 * these are only ! and :. At this point the operator is parsed and the
651 * pointer into the line advanced until the first source is encountered.
652 * The parsed operator is applied to each node in the 'targets' list,
653 * which is where the nodes found for the targets are kept, by means of
654 * the ParseDoOp function.
655 * The sources are read in much the same way as the targets were except
656 * that now they are expanded using the wildcarding scheme of the C-Shell
657 * and all instances of the resulting words in the list of all targets
658 * are found. Each of the resulting nodes is then linked to each of the
659 * targets as one of its children.
660 * Certain targets are handled specially. These are the ones detailed
661 * by the specType variable.
662 * The storing of transformation rules is also taken care of here.
663 * A target is recognized as a transformation rule by calling
664 * Suff_IsTransform. If it is a transformation rule, its node is gotten
665 * from the suffix module via Suff_AddTransform rather than the standard
666 * Targ_FindNode in the target module.
667 *---------------------------------------------------------------------
670 ParseDoDependency (char *line)
672 char *cp; /* our current position */
673 GNode *gn; /* a general purpose temporary node */
674 int op; /* the operator on the line */
675 char savec; /* a place to save a character */
676 Lst paths; /* Search paths to alter when parsing a list of .PATH targets */
677 int tOp; /* operator from special target */
687 *cp && !isspace((unsigned char)*cp) && *cp != '(';
692 * Must be a dynamic source (would have been expanded
693 * otherwise), so call the Var module to parse the puppy
694 * so we can safely advance beyond it...There should be
695 * no errors in this, as they would have been discovered
696 * in the initial Var_Subst and we wouldn't be here.
702 result = Var_Parse(cp, VAR_CMD, TRUE, &length, &freeIt);
708 } else if (*cp == '!' || *cp == ':') {
710 * We don't want to end a word on ':' or '!' if there is a
711 * better match later on in the string (greedy matching).
712 * This allows the user to have targets like:
715 * where "fie::fi:fo" and "foo::bar" are the targets. In
716 * real life this is used for perl5 library man pages where
717 * "::" separates an object from its class.
718 * Ie: "File::Spec::Unix". This behaviour is also consistent
719 * with other versions of make.
723 if (*cp == ':' && *p == ':')
726 /* Found the best match already. */
727 if (*p == '\0' || isspace(*p))
730 p += strcspn(p, "!:");
732 /* No better match later on... */
740 * Archives must be handled specially to make sure the OP_ARCHV
741 * flag is set in their 'type' field, for one thing, and because
742 * things like "archive(file1.o file2.o file3.o)" are permissible.
743 * Arch_ParseArchive will set 'line' to be the first non-blank
744 * after the archive-spec. It creates/finds nodes for the members
745 * and places them on the given list, returning SUCCESS if all
746 * went well and FAILURE if there was an error in the
747 * specification. On error, line should remain untouched.
749 if (Arch_ParseArchive(&line, &targets, VAR_CMD) != SUCCESS) {
750 Parse_Error(PARSE_FATAL,
751 "Error in archive specification: \"%s\"", line);
761 * Ending a dependency line without an operator is a Bozo
762 * no-no. As a heuristic, this is also often triggered by
763 * undetected conflicts from cvs/rcs merges.
765 if ((strncmp(line, "<<<<<<", 6) == 0) ||
766 (strncmp(line, "======", 6) == 0) ||
767 (strncmp(line, ">>>>>>", 6) == 0))
768 Parse_Error(PARSE_FATAL,
769 "Makefile appears to contain unresolved cvs/rcs/??? merge conflicts");
771 Parse_Error(PARSE_FATAL, "Need an operator");
776 * Have a word in line. See if it's a special target and set
777 * specType to match it.
779 if (*line == '.' && isupper((unsigned char)line[1])) {
781 * See if the target is a special target that must have it
782 * or its sources handled specially.
784 int keywd = ParseFindKeyword(line);
786 if (specType == ExPath && parseKeywords[keywd].spec != ExPath) {
787 Parse_Error(PARSE_FATAL, "Mismatched special targets");
791 specType = parseKeywords[keywd].spec;
792 tOp = parseKeywords[keywd].op;
795 * Certain special targets have special semantics:
796 * .PATH Have to set the dirSearchPath
798 * .MAIN Its sources are only used if
799 * nothing has been specified to
801 * .DEFAULT Need to create a node to hang
802 * commands on, but we don't want
803 * it in the graph, nor do we want
804 * it to be the Main Target, so we
805 * create it, set OP_NOTMAIN and
806 * add it to the list, setting
807 * DEFAULT to the new node for
808 * later use. We claim the node is
809 * A transformation rule to make
810 * life easier later, when we'll
811 * use Make_HandleUse to actually
812 * apply the .DEFAULT commands.
813 * .PHONY The list of targets
816 * .INTERRUPT Are not to be considered the
818 * .NOTPARALLEL Make only one target at a time.
819 * .SINGLESHELL Create a shell for each command.
820 * .ORDER Must set initial predecessor to NULL
824 Lst_AtEnd(&paths, &dirSearchPath);
827 if (!Lst_IsEmpty(&create)) {
834 gn = Targ_FindNode(line, TARG_CREATE);
835 gn->type |= OP_NOTMAIN;
836 Lst_AtEnd(&targets, gn);
839 gn = Targ_NewGN(".DEFAULT");
840 gn->type |= (OP_NOTMAIN|OP_TRANSFORM);
841 Lst_AtEnd(&targets, gn);
858 } else if (strncmp(line, ".PATH", 5) == 0) {
860 * .PATH<suffix> has to be handled specially.
861 * Call on the suffix module to give us a path to
867 path = Suff_GetPath(&line[5]);
869 Parse_Error(PARSE_FATAL,
870 "Suffix '%s' not defined (yet)",
874 Lst_AtEnd(&paths, path);
879 * Have word in line. Get or create its node and stick it at
880 * the end of the targets list
882 if ((specType == Not) && (*line != '\0')) {
883 /* target names to be found and added to targets list */
884 Lst curTargs = Lst_Initializer(curTargs);
886 if (Dir_HasWildcards(line)) {
888 * Targets are to be sought only in the current directory,
889 * so create an empty path for the thing. Note we need to
890 * use Dir_Destroy in the destruction of the path as the
891 * Dir module could have added a directory to the path...
893 Lst emptyPath = Lst_Initializer(emptyPath);
895 Dir_Expand(line, &emptyPath, &curTargs);
897 Lst_Destroy(&emptyPath, Dir_Destroy);
900 * No wildcards, but we want to avoid code duplication,
901 * so create a list with the word on it.
903 Lst_AtEnd(&curTargs, line);
906 while (!Lst_IsEmpty(&curTargs)) {
907 char *targName = Lst_DeQueue(&curTargs);
909 if (!Suff_IsTransform (targName)) {
910 gn = Targ_FindNode(targName, TARG_CREATE);
912 gn = Suff_AddTransform(targName);
915 Lst_AtEnd(&targets, gn);
917 } else if (specType == ExPath && *line != '.' && *line != '\0') {
918 Parse_Error(PARSE_WARNING, "Extra target (%s) ignored", line);
923 * If it is a special type and not .PATH, it's the only target we
924 * allow on this line...
926 if (specType != Not && specType != ExPath) {
927 Boolean warn = FALSE;
929 while ((*cp != '!') && (*cp != ':') && *cp) {
930 if (*cp != ' ' && *cp != '\t') {
936 Parse_Error(PARSE_WARNING, "Extra target ignored");
939 while (*cp && isspace((unsigned char)*cp)) {
944 } while ((*line != '!') && (*line != ':') && *line);
946 if (!Lst_IsEmpty(&targets)) {
949 Parse_Error(PARSE_WARNING, "Special and mundane targets don't mix. Mundane ones ignored");
956 * These four create nodes on which to hang commands, so
957 * targets shouldn't be empty...
961 * Nothing special here -- targets can be empty if it wants.
968 * Have now parsed all the target names. Must parse the operator next. The
969 * result is left in op .
973 } else if (*cp == ':') {
981 Parse_Error(PARSE_FATAL, "Missing dependency operator");
985 cp++; /* Advance beyond operator */
987 Lst_ForEach(&targets, ParseDoOp, &op);
990 * Get to the first source
992 while (*cp && isspace((unsigned char)*cp)) {
998 * Several special targets take different actions if present with no
1000 * a .SUFFIXES line with no sources clears out all old suffixes
1001 * a .PRECIOUS line makes all targets precious
1002 * a .IGNORE line ignores errors for all targets
1003 * a .SILENT line creates silence when making all targets
1004 * a .PATH removes all directories from the search path(s).
1009 Suff_ClearSuffixes();
1015 ignoreErrors = TRUE;
1021 Lst_ForEach(&paths, ParseClearPath, NULL);
1024 Var_Set("%POSIX", "1003.2", VAR_GLOBAL);
1029 } else if (specType == MFlags) {
1031 * Call on functions in main.c to deal with these arguments and
1032 * set the initial character to a null-character so the loop to
1033 * get sources won't get anything
1035 Main_ParseArgLine(line);
1037 } else if (specType == ExShell) {
1038 if (Job_ParseShell(line) != SUCCESS) {
1039 Parse_Error(PARSE_FATAL, "improper shell specification");
1043 } else if ((specType == NotParallel) || (specType == SingleShell)) {
1048 * NOW GO FOR THE SOURCES
1050 if ((specType == Suffixes) || (specType == ExPath) ||
1051 (specType == Includes) || (specType == Libs) ||
1056 * If the target was one that doesn't take files as its sources
1057 * but takes something like suffixes, we take each
1058 * space-separated word on the line as a something and deal
1059 * with it accordingly.
1061 * If the target was .SUFFIXES, we take each source as a
1062 * suffix and add it to the list of suffixes maintained by the
1065 * If the target was a .PATH, we add the source as a directory
1066 * to search on the search path.
1068 * If it was .INCLUDES, the source is taken to be the suffix of
1069 * files which will be #included and whose search path should
1070 * be present in the .INCLUDES variable.
1072 * If it was .LIBS, the source is taken to be the suffix of
1073 * files which are considered libraries and whose search path
1074 * should be present in the .LIBS variable.
1076 * If it was .NULL, the source is the suffix to use when a file
1077 * has no valid suffix.
1080 while (*cp && !isspace((unsigned char)*cp)) {
1087 Suff_AddSuffix(line);
1090 Lst_ForEach(&paths, ParseAddDir, line);
1093 Suff_AddInclude(line);
1105 if (savech != '\0') {
1108 while (*cp && isspace((unsigned char)*cp)) {
1113 Lst_Destroy(&paths, NOFREE);
1116 Lst curSrcs = Lst_Initializer(curSrc); /* list of sources in order */
1120 * The targets take real sources, so we must beware of archive
1121 * specifications (i.e. things with left parentheses in them)
1122 * and handle them accordingly.
1124 while (*cp && !isspace((unsigned char)*cp)) {
1125 if ((*cp == '(') && (cp > line) && (cp[-1] != '$')) {
1127 * Only stop for a left parenthesis if it isn't at the
1128 * start of a word (that'll be for variable changes
1129 * later) and isn't preceded by a dollar sign (a dynamic
1140 /* list of archive source names after expansion */
1141 Lst sources = Lst_Initializer(sources);
1143 if (Arch_ParseArchive(&line, &sources, VAR_CMD) != SUCCESS) {
1144 Parse_Error(PARSE_FATAL,
1145 "Error in source archive spec \"%s\"", line);
1149 while (!Lst_IsEmpty(&sources)) {
1150 gnp = Lst_DeQueue(&sources);
1151 ParseDoSrc(tOp, gnp->name, &curSrcs);
1160 ParseDoSrc(tOp, line, &curSrcs);
1162 while (*cp && isspace((unsigned char)*cp)) {
1167 Lst_Destroy(&curSrcs, NOFREE);
1170 if (mainNode == NULL) {
1172 * If we have yet to decide on a main target to make, in the
1173 * absence of any user input, we want the first target on
1174 * the first dependency line that is actually a real target
1175 * (i.e. isn't a .USE or .EXEC rule) to be made.
1177 Lst_ForEach(&targets, ParseFindMain, NULL);
1182 *---------------------------------------------------------------------
1184 * Return TRUE if the passed line is a variable assignment. A variable
1185 * assignment consists of a single word followed by optional whitespace
1186 * followed by either a += or an = operator.
1187 * This function is used both by the Parse_File function and main when
1188 * parsing the command-line arguments.
1191 * TRUE if it is. FALSE if it ain't
1195 *---------------------------------------------------------------------
1198 Parse_IsVar(char *line)
1200 Boolean wasSpace = FALSE; /* set TRUE if found a space */
1201 Boolean haveName = FALSE; /* Set TRUE if have a variable name */
1204 #define ISEQOPERATOR(c) \
1205 (((c) == '+') || ((c) == ':') || ((c) == '?') || ((c) == '!'))
1208 * Skip to variable name
1210 for (; (*line == ' ') || (*line == '\t'); line++)
1213 for (; *line != '=' || level != 0; line++)
1217 * end-of-line -- can't be a variable assignment.
1224 * there can be as much white space as desired so long as there is
1225 * only one word before the operator
1241 if (wasSpace && haveName) {
1242 if (ISEQOPERATOR(*line)) {
1244 * We must have a finished word
1250 * When an = operator [+?!:] is found, the next
1251 * character must be an = or it ain't a valid
1258 * This is a shell command
1260 if (strncmp(line, ":sh", 3) == 0)
1265 * This is the start of another word, so not assignment.
1280 *---------------------------------------------------------------------
1282 * Take the variable assignment in the passed line and do it in the
1285 * Note: There is a lexical ambiguity with assignment modifier characters
1286 * in variable names. This routine interprets the character before the =
1287 * as a modifier. Therefore, an assignment like
1289 * is interpreted as "C+ +=" instead of "C++ =".
1295 * the variable structure of the given variable name is altered in the
1297 *---------------------------------------------------------------------
1300 Parse_DoVar(char *line, GNode *ctxt)
1302 char *cp; /* pointer into line */
1304 VAR_SUBST, VAR_APPEND, VAR_SHELL, VAR_NORMAL
1305 } type; /* Type of assignment */
1306 char *opc; /* ptr to operator character to
1307 * null-terminate the variable name */
1309 * Avoid clobbered variable warnings by forcing the compiler
1310 * to ``unregister'' variables
1318 * Skip to variable name
1320 while ((*line == ' ') || (*line == '\t')) {
1325 * Skip to operator character, nulling out whitespace as we go
1327 for (cp = line + 1; *cp != '='; cp++) {
1328 if (isspace((unsigned char)*cp)) {
1332 opc = cp - 1; /* operator is the previous character */
1333 *cp++ = '\0'; /* nuke the = */
1336 * Check operator type
1346 * If the variable already has a value, we don't do anything.
1349 if (Var_Exists(line, ctxt)) {
1374 if (strncmp(opc, ":sh", 3) == 0) {
1384 while (isspace((unsigned char)*cp)) {
1388 if (type == VAR_APPEND) {
1389 Var_Append(line, cp, ctxt);
1390 } else if (type == VAR_SUBST) {
1392 * Allow variables in the old value to be undefined, but leave their
1393 * invocation alone -- this is done by forcing oldVars to be false.
1394 * XXX: This can cause recursive variables, but that's not hard to do,
1395 * and this allows someone to do something like
1397 * CFLAGS = $(.INCLUDES)
1398 * CFLAGS := -I.. $(CFLAGS)
1400 * And not get an error.
1402 Boolean oldOldVars = oldVars;
1407 * make sure that we set the variable the first time to nothing
1408 * so that it gets substituted!
1410 if (!Var_Exists(line, ctxt))
1411 Var_Set(line, "", ctxt);
1413 cp = Var_Subst(NULL, cp, ctxt, FALSE);
1414 oldVars = oldOldVars;
1416 Var_Set(line, cp, ctxt);
1418 } else if (type == VAR_SHELL) {
1419 Boolean freeCmd = FALSE; /* TRUE if the command needs to be freed, i.e.
1420 * if any variable expansion was performed */
1423 if (strchr(cp, '$') != NULL) {
1425 * There's a dollar sign in the command, so perform variable
1426 * expansion on the whole thing. The resulting string will need
1427 * freeing when we're done, so set freeCmd to TRUE.
1429 cp = Var_Subst(NULL, cp, VAR_CMD, TRUE);
1433 res = Cmd_Exec(cp, &error);
1434 Var_Set(line, res, ctxt);
1438 Parse_Error(PARSE_WARNING, error, cp);
1444 * Normal assignment -- just do it.
1446 Var_Set(line, cp, ctxt);
1452 * Lst_ForEach function to add a command line to all targets
1458 * A new element is added to the commands list of the node.
1461 ParseAddCmd(void *gnp, void *cmd)
1465 /* if target already supplied, ignore commands */
1466 if (!(gn->type & OP_HAS_COMMANDS))
1467 Lst_AtEnd(&gn->commands, cmd);
1469 Parse_Error(PARSE_WARNING,
1470 "duplicate script for target \"%s\" ignored",
1476 *-----------------------------------------------------------------------
1477 * ParseHasCommands --
1478 * Callback procedure for Parse_File when destroying the list of
1479 * targets on the last dependency line. Marks a target as already
1480 * having commands if it does, to keep from having shell commands
1481 * on multiple dependency lines.
1487 * OP_HAS_COMMANDS may be set for the target.
1489 *-----------------------------------------------------------------------
1492 ParseHasCommands(void *gnp)
1496 if (!Lst_IsEmpty(&gn->commands)) {
1497 gn->type |= OP_HAS_COMMANDS;
1502 *-----------------------------------------------------------------------
1503 * Parse_AddIncludeDir --
1504 * Add a directory to the path searched for included makefiles
1505 * bracketed by double-quotes. Used by functions in main.c
1511 * The directory is appended to the list.
1513 *-----------------------------------------------------------------------
1516 Parse_AddIncludeDir(char *dir)
1519 Dir_AddDir(&parseIncPath, dir);
1522 /*---------------------------------------------------------------------
1524 * Handle error directive
1526 * The input is the line minus the ".error". We substitute variables,
1527 * print the message and exit(1) or just print a warning if the ".error"
1528 * directive is malformed.
1530 *---------------------------------------------------------------------
1533 ParseDoError(char *errmsg)
1536 if (!isspace((unsigned char)*errmsg)) {
1537 Parse_Error(PARSE_WARNING, "invalid syntax: .error%s", errmsg);
1541 while (isspace((unsigned char)*errmsg))
1544 errmsg = Var_Subst(NULL, errmsg, VAR_GLOBAL, FALSE);
1546 Parse_Error(PARSE_FATAL, "%s", errmsg);
1547 /* Terminate immediately. */
1551 /*---------------------------------------------------------------------
1553 * Handle warning directive
1555 * The input is the line minus the ".warning". We substitute variables
1556 * and print the message or just print a warning if the ".warning"
1557 * directive is malformed.
1559 *---------------------------------------------------------------------
1562 ParseDoWarning(char *warnmsg)
1565 if (!isspace((unsigned char)*warnmsg)) {
1566 Parse_Error(PARSE_WARNING, "invalid syntax: .warning%s",
1571 while (isspace((unsigned char)*warnmsg))
1574 warnmsg = Var_Subst(NULL, warnmsg, VAR_GLOBAL, FALSE);
1576 Parse_Error(PARSE_WARNING, "%s", warnmsg);
1580 *---------------------------------------------------------------------
1582 * Push to another file.
1584 * The input is the line minus the #include. A file spec is a string
1585 * enclosed in <> or "". The former is looked for only in sysIncPath.
1586 * The latter in . and the directories specified by -I command line
1593 * A structure is added to the includes Lst and readProc, curFile.lineno,
1594 * curFile.fname and curFile.F are altered for the new file
1595 *---------------------------------------------------------------------
1598 ParseDoInclude (char *file)
1600 char *fullname; /* full pathname of file */
1601 IFile *oldFile; /* state associated with current file */
1602 char endc; /* the character which ends the file spec */
1603 char *cp; /* current position in file spec */
1604 Boolean isSystem; /* TRUE if makefile is a system makefile */
1607 * Skip to delimiter character so we know where to look
1609 while ((*file == ' ') || (*file == '\t')) {
1613 if ((*file != '"') && (*file != '<')) {
1614 Parse_Error(PARSE_FATAL,
1615 ".include filename must be delimited by '\"' or '<'");
1620 * Set the search path on which to find the include file based on the
1621 * characters which bracket its name. Angle-brackets imply it's
1622 * a system Makefile while double-quotes imply it's a user makefile
1633 * Skip to matching delimiter
1635 for (cp = ++file; *cp && *cp != endc; cp++) {
1640 Parse_Error(PARSE_FATAL,
1641 "Unclosed %cinclude filename. '%c' expected",
1648 * Substitute for any variables in the file name before trying to
1651 file = Var_Subst(NULL, file, VAR_CMD, FALSE);
1654 * Now we know the file's name and its search path, we attempt to
1655 * find the durn thing. A return of NULL indicates the file don't
1660 * Include files contained in double-quotes are first searched for
1661 * relative to the including file's location. We don't want to
1662 * cd there, of course, so we just tack on the old file's
1663 * leading path components and call Dir_FindFile to see if
1664 * we can locate the beast.
1666 char *prefEnd, *Fname;
1668 /* Make a temporary copy of this, to be safe. */
1669 Fname = estrdup(curFile.fname);
1671 prefEnd = strrchr(Fname, '/');
1672 if (prefEnd != (char *)NULL) {
1677 newName = estrdup(file);
1679 newName = str_concat(Fname, file, STR_ADDSLASH);
1680 fullname = Dir_FindFile(newName, &parseIncPath);
1681 if (fullname == NULL) {
1682 fullname = Dir_FindFile(newName, &dirSearchPath);
1694 if (fullname == NULL) {
1696 * System makefile or makefile wasn't found in same directory as
1697 * included makefile. Search for it first on the -I search path,
1698 * then on the .PATH search path, if not found in a -I directory.
1699 * XXX: Suffix specific?
1701 fullname = Dir_FindFile(file, &parseIncPath);
1702 if (fullname == NULL) {
1703 fullname = Dir_FindFile(file, &dirSearchPath);
1707 if (fullname == NULL) {
1709 * Still haven't found the makefile. Look for it on the system
1710 * path as a last resort.
1712 fullname = Dir_FindFile(file, &sysIncPath);
1715 if (fullname == NULL) {
1717 Parse_Error(PARSE_FATAL, "Could not find %s", file);
1724 * Once we find the absolute path to the file, we get to save all the
1725 * state from the current file before we can start reading this
1726 * include file. The state is stored in an IFile structure which
1727 * is placed on a list with other IFile structures. The list makes
1728 * a very nice stack to track how we got here...
1730 oldFile = emalloc(sizeof (IFile));
1731 memcpy(oldFile, &curFile, sizeof(IFile));
1733 Lst_AtFront(&includes, oldFile);
1736 * Once the previous state has been saved, we can get down to reading
1737 * the new file. We set up the name of the file to be the absolute
1738 * name of the include file so error messages refer to the right
1739 * place. Naturally enough, we start reading at line number 0.
1741 curFile.fname = fullname;
1744 curFile.F = fopen(fullname, "r");
1746 if (curFile.F == NULL) {
1747 Parse_Error(PARSE_FATAL, "Cannot open %s", fullname);
1749 * Pop to previous file
1753 Var_Append(".MAKEFILE_LIST", fullname, VAR_GLOBAL);
1758 *---------------------------------------------------------------------
1759 * Parse_FromString --
1760 * Start Parsing from the given string
1766 * A structure is added to the includes Lst and readProc, curFile.lineno,
1767 * curFile.fname and curFile.F are altered for the new file
1768 *---------------------------------------------------------------------
1771 Parse_FromString(char *str, int lineno)
1773 IFile *oldFile; /* state associated with this file */
1775 DEBUGF(FOR, ("%s\n---- at line %d\n", str, lineno));
1777 oldFile = emalloc(sizeof(IFile));
1778 memcpy(oldFile, &curFile, sizeof(IFile));
1780 Lst_AtFront(&includes, oldFile);
1783 curFile.p = emalloc(sizeof (PTR));
1784 curFile.p->str = curFile.p->ptr = str;
1785 curFile.lineno = lineno;
1786 curFile.fname = estrdup(curFile.fname);
1791 *---------------------------------------------------------------------
1792 * ParseTraditionalInclude --
1793 * Push to another file.
1795 * The input is the line minus the "include". The file name is
1796 * the string following the "include".
1802 * A structure is added to the includes Lst and readProc, curFile.lineno,
1803 * curFile.fname and curFile.F are altered for the new file
1804 *---------------------------------------------------------------------
1807 ParseTraditionalInclude (char *file)
1809 char *fullname; /* full pathname of file */
1810 IFile *oldFile; /* state associated with current file */
1811 char *cp; /* current position in file spec */
1814 * Skip over whitespace
1816 while ((*file == ' ') || (*file == '\t')) {
1820 if (*file == '\0') {
1821 Parse_Error(PARSE_FATAL,
1822 "Filename missing from \"include\"");
1827 * Skip to end of line or next whitespace
1829 for (cp = file; *cp && *cp != '\n' && *cp != '\t' && *cp != ' '; cp++) {
1836 * Substitute for any variables in the file name before trying to
1839 file = Var_Subst(NULL, file, VAR_CMD, FALSE);
1842 * Now we know the file's name, we attempt to find the durn thing.
1843 * Search for it first on the -I search path, then on the .PATH
1844 * search path, if not found in a -I directory.
1846 fullname = Dir_FindFile(file, &parseIncPath);
1847 if (fullname == NULL) {
1848 fullname = Dir_FindFile(file, &dirSearchPath);
1851 if (fullname == NULL) {
1853 * Still haven't found the makefile. Look for it on the system
1854 * path as a last resort.
1856 fullname = Dir_FindFile(file, &sysIncPath);
1859 if (fullname == NULL) {
1860 Parse_Error(PARSE_FATAL, "Could not find %s", file);
1865 * Once we find the absolute path to the file, we get to save all the
1866 * state from the current file before we can start reading this
1867 * include file. The state is stored in an IFile structure which
1868 * is placed on a list with other IFile structures. The list makes
1869 * a very nice stack to track how we got here...
1871 oldFile = emalloc(sizeof(IFile));
1872 memcpy(oldFile, &curFile, sizeof(IFile));
1874 Lst_AtFront(&includes, oldFile);
1877 * Once the previous state has been saved, we can get down to reading
1878 * the new file. We set up the name of the file to be the absolute
1879 * name of the include file so error messages refer to the right
1880 * place. Naturally enough, we start reading at line number 0.
1882 curFile.fname = fullname;
1885 curFile.F = fopen(fullname, "r");
1887 if (curFile.F == NULL) {
1888 Parse_Error(PARSE_FATAL, "Cannot open %s", fullname);
1890 * Pop to previous file
1894 Var_Append(".MAKEFILE_LIST", fullname, VAR_GLOBAL);
1900 *---------------------------------------------------------------------
1902 * Called when EOF is reached in the current file. If we were reading
1903 * an include file, the includes stack is popped and things set up
1904 * to go back to reading the previous file at the previous location.
1907 * CONTINUE if there's more to do. DONE if not.
1910 * The old curFile.F is closed. The includes list is shortened.
1911 * curFile.lineno, curFile.F, and curFile.fname are changed if
1912 * CONTINUE is returned.
1913 *---------------------------------------------------------------------
1916 ParseEOF(int opened)
1918 IFile *ifile; /* the state on the top of the includes stack */
1920 if (Lst_IsEmpty(&includes)) {
1921 Var_Append(".MAKEFILE_LIST", "..", VAR_GLOBAL);
1925 ifile = Lst_DeQueue(&includes);
1926 free(curFile.fname);
1927 if (opened && curFile.F) {
1929 Var_Append(".MAKEFILE_LIST", "..", VAR_GLOBAL);
1932 free(curFile.p->str);
1935 memcpy(&curFile, ifile, sizeof(IFile));
1941 *---------------------------------------------------------------------
1943 * Read a character from the current file
1946 * The character that was read
1949 *---------------------------------------------------------------------
1956 return (fgetc(curFile.F));
1958 if (curFile.p && *curFile.p->ptr)
1959 return (*curFile.p->ptr++);
1965 *---------------------------------------------------------------------
1967 * Put back a character to the current file
1973 *---------------------------------------------------------------------
1980 ungetc(c, curFile.F);
1984 *--(curFile.p->ptr) = c;
1991 * Grab the next line unless it begins with a dot (`.') and we're told to
1992 * ignore such lines.
1995 ParseSkipLine(int skip, int keep_newline)
1999 size_t lineLength = 0;
2002 buf = Buf_Init(MAKE_BSIZE);
2005 Buf_Discard(buf, lineLength);
2008 while (((c = ParseReadc()) != '\n' || lastc == '\\')
2010 if (skip && c == '#' && lastc != '\\') {
2011 /* let a comment be terminated even by an escaped \n.
2012 * This is consistent to comment handling in ParseReadLine */
2013 while ((c = ParseReadc()) != '\n' && c != EOF)
2019 Buf_AddByte(buf, (Byte)c);
2021 Buf_ReplaceLastByte(buf, (Byte)' ');
2024 while ((c = ParseReadc()) == ' ' || c == '\t')
2031 Buf_AddByte(buf, (Byte)c);
2036 Parse_Error(PARSE_FATAL, "Unclosed conditional/for loop");
2037 Buf_Destroy(buf, TRUE);
2042 Buf_AddByte(buf, (Byte)'\0');
2043 line = (char *)Buf_GetAll(buf, &lineLength);
2044 } while (skip == 1 && line[0] != '.');
2046 Buf_Destroy(buf, FALSE);
2052 *---------------------------------------------------------------------
2054 * Read an entire line from the input file. Called only by Parse_File.
2055 * To facilitate escaped newlines and what have you, a character is
2056 * buffered in 'lastc', which is '\0' when no characters have been
2057 * read. When we break out of the loop, c holds the terminating
2058 * character and lastc holds a character that should be added to
2059 * the line (unless we don't read anything but a terminator).
2062 * A line w/o its newline
2065 * Only those associated with reading a character
2066 *---------------------------------------------------------------------
2071 Buffer buf; /* Buffer for current line */
2072 int c; /* the current character */
2073 int lastc; /* The most-recent character */
2074 Boolean semiNL; /* treat semi-colons as newlines */
2075 Boolean ignDepOp; /* TRUE if should ignore dependency operators
2076 * for the purposes of setting semiNL */
2077 Boolean ignComment; /* TRUE if should ignore comments (in a
2079 char *line; /* Result */
2080 char *ep; /* to strip trailing blanks */
2081 size_t lineLength; /* Length of result */
2082 int lineno; /* Saved line # */
2089 * Handle special-characters at the beginning of the line. Either a
2090 * leading tab (shell command) or pound-sign (possible conditional)
2091 * forces us to ignore comments and dependency operators and treat
2092 * semi-colons as semi-colons (by leaving semiNL FALSE). This also
2093 * discards completely blank lines.
2099 ignComment = ignDepOp = TRUE;
2101 } else if (c == '\n') {
2103 } else if (c == '#') {
2108 * Anything else breaks out without doing anything
2116 buf = Buf_Init(MAKE_BSIZE);
2118 while (((c = ParseReadc()) != '\n' || (lastc == '\\')) &&
2125 * Escaped newline: read characters until a non-space or an
2126 * unescaped newline and replace them all by a single space.
2127 * This is done by storing the space over the backslash and
2128 * dropping through with the next nonspace. If it is a
2129 * semi-colon and semiNL is TRUE, it will be recognized as a
2130 * newline in the code below this...
2134 while ((c = ParseReadc()) == ' ' || c == '\t') {
2137 if (c == EOF || c == '\n') {
2141 * Check for comments, semiNL's, etc. -- easier than
2142 * ParseUnreadc(c); continue;
2151 * Semi-colon: Need to see if it should be interpreted as a
2156 * To make sure the command that may be following this
2157 * semi-colon begins with a tab, we push one back into the
2158 * input stream. This will overwrite the semi-colon in the
2159 * buffer. If there is no command following, this does no
2160 * harm, since the newline remains in the buffer and the
2161 * whole line is ignored.
2170 * Haven't seen a dependency operator before this, so this
2171 * must be a variable assignment -- don't pay attention to
2172 * dependency operators after this.
2175 } else if (lastc == ':' || lastc == '!') {
2177 * Well, we've seen a dependency operator already, but it
2178 * was the previous character, so this is really just an
2179 * expanded variable assignment. Revert semi-colons to
2180 * being just semi-colons again and ignore any more
2181 * dependency operators.
2183 * XXX: Note that a line like "foo : a:=b" will blow up,
2184 * but who'd write a line like that anyway?
2192 if (lastc != '\\') {
2194 * If the character is a hash mark and it isn't escaped
2195 * (or we're being compatible), the thing is a comment.
2196 * Skip to the end of the line.
2200 } while ((c != '\n') && (c != EOF));
2204 * Don't add the backslash. Just let the # get copied
2214 if (!ignDepOp && (c == ':' || c == '!')) {
2216 * A semi-colon is recognized as a newline only on
2217 * dependency lines. Dependency lines are lines with a
2218 * colon or an exclamation point. Ergo...
2227 * Copy in the previous character and save this one in lastc.
2229 Buf_AddByte(buf, (Byte)lastc);
2236 if (lastc != '\0') {
2237 Buf_AddByte(buf, (Byte)lastc);
2239 Buf_AddByte(buf, (Byte)'\0');
2240 line = (char *)Buf_GetAll(buf, &lineLength);
2241 Buf_Destroy(buf, FALSE);
2244 * Strip trailing blanks and tabs from the line.
2245 * Do not strip a blank or tab that is preceded by
2251 while (ep > line + 1 && (ep[-1] == ' ' || ep[-1] == '\t')) {
2252 if (ep > line + 1 && ep[-2] == '\\')
2258 if (line[0] == '.') {
2260 * The line might be a conditional. Ask the conditional module
2261 * about it and act accordingly
2263 switch (Cond_Eval(line)) {
2266 * Skip to next conditional that evaluates to COND_PARSE.
2270 line = ParseSkipLine(1, 0);
2271 } while (line && Cond_Eval(line) != COND_PARSE);
2277 line = ParseReadLine();
2280 if (For_Eval(line)) {
2283 lineno = curFile.lineno;
2286 * Skip after the matching end
2288 line = ParseSkipLine(0, 1);
2290 Parse_Error(PARSE_FATAL,
2291 "Unexpected end of file in for loop.\n");
2294 ok = For_Eval(line);
2300 line = ParseReadLine();
2311 * Hit end-of-file, so return a NULL line to indicate this.
2318 *-----------------------------------------------------------------------
2319 * ParseFinishLine --
2320 * Handle the end of a dependency group.
2326 * inLine set FALSE. 'targets' list destroyed.
2328 *-----------------------------------------------------------------------
2331 ParseFinishLine(void)
2335 Lst_ForEach(&targets, Suff_EndTransform, NULL);
2336 Lst_Destroy(&targets, ParseHasCommands);
2342 stripvarname(char *cp)
2346 while (isspace((unsigned char)*cp))
2349 while (*cp2 && !isspace((unsigned char)*cp2))
2357 *---------------------------------------------------------------------
2359 * Parse a file into its component parts, incorporating it into the
2360 * current dependency graph. This is the main function and controls
2361 * almost every other function in this module
2367 * Loads. Nodes are added to the list of all targets, nodes and links
2368 * are added to the dependency graph. etc. etc. etc.
2369 *---------------------------------------------------------------------
2372 Parse_File(char *name, FILE *stream)
2374 char *cp, /* pointer into the line */
2375 *line; /* the line we're working on */
2378 curFile.fname = name;
2383 Var_Append(".MAKEFILE_LIST", name, VAR_GLOBAL);
2386 while ((line = ParseReadLine()) != NULL) {
2389 * Lines that begin with the special character are either
2390 * include or undef directives.
2392 for (cp = line + 1; isspace((unsigned char)*cp); cp++) {
2395 if (strncmp(cp, "include", 7) == 0) {
2396 ParseDoInclude (cp + 7);
2398 } else if (strncmp(cp, "error", 5) == 0) {
2399 ParseDoError(cp + 5);
2401 } else if (strncmp(cp, "warning", 7) == 0) {
2402 ParseDoWarning(cp + 7);
2404 } else if (strncmp(cp, "undef", 5) == 0) {
2405 cp = stripvarname(cp + 5);
2406 cp = Var_Subst(NULL, cp, VAR_CMD, FALSE);
2407 Var_Delete(cp, VAR_GLOBAL);
2409 } else if (strncmp(cp, "makeenv", 7) == 0) {
2410 cp = stripvarname(cp + 7);
2411 Var_SetEnv(cp, VAR_GLOBAL);
2416 /* If we're this far, the line must be a comment. */
2420 if (*line == '\t') {
2422 * If a line starts with a tab, it can only hope to be
2423 * a creation command.
2425 for (cp = line + 1; isspace((unsigned char)*cp); cp++) {
2431 * So long as it's not a blank line and we're actually
2432 * in a dependency spec, add the command to the list of
2433 * commands of all targets in the dependency spec
2435 Lst_ForEach(&targets, ParseAddCmd, cp);
2438 Parse_Error(PARSE_FATAL,
2439 "Unassociated shell command \"%s\"",
2444 } else if (strncmp(line, "include", 7) == 0 &&
2445 isspace((unsigned char)line[7]) &&
2446 strchr(line, ':') == NULL) {
2448 * It's an S3/S5-style "include".
2450 ParseTraditionalInclude(line + 7);
2453 } else if (Parse_IsVar(line)) {
2455 Parse_DoVar(line, VAR_GLOBAL);
2458 * We now know it's a dependency line so it needs to have all
2459 * variables expanded before being parsed. Tell the variable
2460 * module to complain if some variable is undefined...
2461 * To make life easier on novices, if the line is indented we
2462 * first make sure the line has a dependency operator in it.
2463 * If it doesn't have an operator and we're in a dependency
2464 * line's script, we assume it's actually a shell command
2465 * and add it to the current list of targets.
2468 if (isspace((unsigned char)line[0])) {
2469 while ((*cp != '\0') && isspace((unsigned char)*cp)) {
2479 cp = Var_Subst(NULL, line, VAR_CMD, TRUE);
2484 * Need a non-circular list for the target nodes
2486 Lst_Destroy(&targets, NOFREE);
2489 ParseDoDependency (line);
2497 * Reached EOF, but it may be just EOF of an include file...
2499 } while (ParseEOF(1) == CONTINUE);
2502 * Make sure conditionals are clean
2507 errx(1, "fatal errors encountered -- cannot continue");
2511 *---------------------------------------------------------------------
2513 * initialize the parsing module
2519 * the parseIncPath list is initialized...
2520 *---------------------------------------------------------------------
2530 *-----------------------------------------------------------------------
2532 * Return a Lst of the main target to create for main()'s sake. If
2533 * no such target exists, we Punt with an obnoxious error message.
2536 * A Lst of the single node to create.
2541 *-----------------------------------------------------------------------
2544 Parse_MainName(Lst *listmain)
2547 if (mainNode == NULL) {
2548 Punt("no target to make.");
2550 } else if (mainNode->type & OP_DOUBLEDEP) {
2551 Lst_AtEnd(listmain, mainNode);
2552 Lst_Concat(listmain, &mainNode->cohorts, LST_CONCNEW);
2555 Lst_AtEnd(listmain, mainNode);