Index: head/usr.bin/make/main.c =================================================================== --- head/usr.bin/make/main.c (revision 49331) +++ head/usr.bin/make/main.c (revision 49332) @@ -1,1312 +1,1323 @@ /* * Copyright (c) 1988, 1989, 1990, 1993 * The Regents of the University of California. All rights reserved. * Copyright (c) 1989 by Berkeley Softworks * All rights reserved. * * This code is derived from software contributed to Berkeley by * Adam de Boor. * * Redistribution and use in source and binary forms, with or without * modification, are permitted provided that the following conditions * are met: * 1. Redistributions of source code must retain the above copyright * notice, this list of conditions and the following disclaimer. * 2. Redistributions in binary form must reproduce the above copyright * notice, this list of conditions and the following disclaimer in the * documentation and/or other materials provided with the distribution. * 3. All advertising materials mentioning features or use of this software * must display the following acknowledgement: * This product includes software developed by the University of * California, Berkeley and its contributors. * 4. Neither the name of the University nor the names of its contributors * may be used to endorse or promote products derived from this software * without specific prior written permission. * * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF * SUCH DAMAGE. */ #ifndef lint static const char copyright[] = "@(#) Copyright (c) 1988, 1989, 1990, 1993\n\ The Regents of the University of California. All rights reserved.\n"; #endif /* not lint */ #ifndef lint #if 0 static char sccsid[] = "@(#)main.c 8.3 (Berkeley) 3/19/94"; #endif static const char rcsid[] = - "$Id: main.c,v 1.30 1999/03/01 06:01:05 imp Exp $"; + "$Id: main.c,v 1.31 1999/07/31 20:40:23 hoek Exp $"; #endif /* not lint */ /*- * main.c -- * The main file for this entire program. Exit routines etc * reside here. * * Utility functions defined in this file: * Main_ParseArgLine Takes a line of arguments, breaks them and * treats them as if they were given when first * invoked. Used by the parse module to implement * the .MFLAGS target. * * Error Print a tagged error message. The global * MAKE variable must have been defined. This * takes a format string and two optional * arguments for it. * * Fatal Print an error message and exit. Also takes * a format string and two arguments. * * Punt Aborts all jobs and exits with a message. Also * takes a format string and two arguments. * * Finish Finish things up by printing the number of * errors which occured, as passed to it, and * exiting. */ #include #include #include #include #include #include #if defined(__i386__) #include #endif #ifndef MACHINE #include #endif #include #include #include #include #include #include #include #if __STDC__ #include #else #include #endif #include "make.h" #include "hash.h" #include "dir.h" #include "job.h" #include "pathnames.h" #ifndef DEFMAXLOCAL #define DEFMAXLOCAL DEFMAXJOBS #endif /* DEFMAXLOCAL */ #define MAKEFLAGS ".MAKEFLAGS" Lst create; /* Targets to be made */ time_t now; /* Time at start of make */ GNode *DEFAULT; /* .DEFAULT node */ Boolean allPrecious; /* .PRECIOUS given on line by itself */ static Boolean noBuiltins; /* -r flag */ static Lst makefiles; /* ordered list of makefiles to read */ static Boolean printVars; /* print value of one or more vars */ static Lst variables; /* list of variables to print */ int maxJobs; /* -j argument */ static Boolean forceJobs; /* -j argument given */ static int maxLocal; /* -L argument */ Boolean compatMake; /* -B argument */ Boolean debug; /* -d flag */ Boolean noExecute; /* -n flag */ Boolean keepgoing; /* -k flag */ Boolean queryFlag; /* -q flag */ Boolean touchFlag; /* -t flag */ Boolean usePipes; /* !-P flag */ Boolean ignoreErrors; /* -i flag */ Boolean beSilent; /* -s flag */ Boolean beVerbose; /* -v flag */ Boolean oldVars; /* variable substitution style */ Boolean checkEnvFirst; /* -e flag */ +Lst envFirstVars; /* (-E) vars to override from env */ static Boolean jobsRunning; /* TRUE if the jobs might be running */ static void MainParseArgs __P((int, char **)); char * chdir_verify_path __P((char *, char *)); static int ReadMakefile __P((ClientData, ClientData)); static void usage __P((void)); static char *curdir; /* startup directory */ static char *objdir; /* where we chdir'ed to */ /*- * MainParseArgs -- * Parse a given argument vector. Called from main() and from * Main_ParseArgLine() when the .MAKEFLAGS target is used. * * XXX: Deal with command line overriding .MAKEFLAGS in makefile * * Results: * None * * Side Effects: * Various global and local flags will be set depending on the flags * given */ static void MainParseArgs(argc, argv) int argc; char **argv; { extern int optind; extern char *optarg; char *p; int c; optind = 1; /* since we're called more than once */ #ifdef REMOTE -# define OPTFLAGS "BD:I:L:PSV:d:ef:ij:km:nqrstv" +# define OPTFLAGS "BD:E:I:L:PSV:d:ef:ij:km:nqrstv" #else -# define OPTFLAGS "BD:I:PSV:d:ef:ij:km:nqrstv" +# define OPTFLAGS "BD:E:I:PSV:d:ef:ij:km:nqrstv" #endif rearg: while((c = getopt(argc, argv, OPTFLAGS)) != -1) { switch(c) { case 'D': Var_Set(optarg, "1", VAR_GLOBAL); Var_Append(MAKEFLAGS, "-D", VAR_GLOBAL); Var_Append(MAKEFLAGS, optarg, VAR_GLOBAL); break; case 'I': Parse_AddIncludeDir(optarg); Var_Append(MAKEFLAGS, "-I", VAR_GLOBAL); Var_Append(MAKEFLAGS, optarg, VAR_GLOBAL); break; case 'V': printVars = TRUE; p = malloc(strlen(optarg) + 1 + 3); if (!p) Punt("make: cannot allocate memory."); /* This sprintf is safe, because of the malloc above */ (void)sprintf(p, "${%s}", optarg); (void)Lst_AtEnd(variables, (ClientData)p); Var_Append(MAKEFLAGS, "-V", VAR_GLOBAL); Var_Append(MAKEFLAGS, optarg, VAR_GLOBAL); break; case 'B': compatMake = TRUE; Var_Append(MAKEFLAGS, "-B", VAR_GLOBAL); break; #ifdef REMOTE case 'L': { char *endptr; maxLocal = strtol(optarg, &endptr, 10); if (maxLocal < 0 || *endptr != '\0') { errx(EX_USAGE, "illegal argument to -L -- %s", optarg); } Var_Append(MAKEFLAGS, "-L", VAR_GLOBAL); Var_Append(MAKEFLAGS, optarg, VAR_GLOBAL); break; } #endif case 'P': usePipes = FALSE; Var_Append(MAKEFLAGS, "-P", VAR_GLOBAL); break; case 'S': keepgoing = FALSE; Var_Append(MAKEFLAGS, "-S", VAR_GLOBAL); break; case 'd': { char *modules = optarg; for (; *modules; ++modules) switch (*modules) { case 'A': debug = ~0; break; case 'a': debug |= DEBUG_ARCH; break; case 'c': debug |= DEBUG_COND; break; case 'd': debug |= DEBUG_DIR; break; case 'f': debug |= DEBUG_FOR; break; case 'g': if (modules[1] == '1') { debug |= DEBUG_GRAPH1; ++modules; } else if (modules[1] == '2') { debug |= DEBUG_GRAPH2; ++modules; } break; case 'j': debug |= DEBUG_JOB; break; case 'm': debug |= DEBUG_MAKE; break; case 's': debug |= DEBUG_SUFF; break; case 't': debug |= DEBUG_TARG; break; case 'v': debug |= DEBUG_VAR; break; default: warnx("illegal argument to d option -- %c", *modules); usage(); } Var_Append(MAKEFLAGS, "-d", VAR_GLOBAL); Var_Append(MAKEFLAGS, optarg, VAR_GLOBAL); break; } + case 'E': + p = malloc(strlen(optarg) + 1); + if (!p) + Punt("make: cannot allocate memory."); + (void)strcpy(p, optarg); + (void)Lst_AtEnd(envFirstVars, (ClientData)p); + Var_Append(MAKEFLAGS, "-E", VAR_GLOBAL); + Var_Append(MAKEFLAGS, optarg, VAR_GLOBAL); + break; case 'e': checkEnvFirst = TRUE; Var_Append(MAKEFLAGS, "-e", VAR_GLOBAL); break; case 'f': (void)Lst_AtEnd(makefiles, (ClientData)optarg); break; case 'i': ignoreErrors = TRUE; Var_Append(MAKEFLAGS, "-i", VAR_GLOBAL); break; case 'j': { char *endptr; forceJobs = TRUE; maxJobs = strtol(optarg, &endptr, 10); if (maxJobs <= 0 || *endptr != '\0') { errx(EX_USAGE, "illegal argument to -j -- %s", optarg); } #ifndef REMOTE maxLocal = maxJobs; #endif Var_Append(MAKEFLAGS, "-j", VAR_GLOBAL); Var_Append(MAKEFLAGS, optarg, VAR_GLOBAL); break; } case 'k': keepgoing = TRUE; Var_Append(MAKEFLAGS, "-k", VAR_GLOBAL); break; case 'm': Dir_AddDir(sysIncPath, optarg); Var_Append(MAKEFLAGS, "-m", VAR_GLOBAL); Var_Append(MAKEFLAGS, optarg, VAR_GLOBAL); break; case 'n': noExecute = TRUE; Var_Append(MAKEFLAGS, "-n", VAR_GLOBAL); break; case 'q': queryFlag = TRUE; /* Kind of nonsensical, wot? */ Var_Append(MAKEFLAGS, "-q", VAR_GLOBAL); break; case 'r': noBuiltins = TRUE; Var_Append(MAKEFLAGS, "-r", VAR_GLOBAL); break; case 's': beSilent = TRUE; Var_Append(MAKEFLAGS, "-s", VAR_GLOBAL); break; case 't': touchFlag = TRUE; Var_Append(MAKEFLAGS, "-t", VAR_GLOBAL); break; case 'v': beVerbose = TRUE; Var_Append(MAKEFLAGS, "-v", VAR_GLOBAL); break; default: case '?': usage(); } } oldVars = TRUE; /* * See if the rest of the arguments are variable assignments and * perform them if so. Else take them to be targets and stuff them * on the end of the "create" list. */ for (argv += optind, argc -= optind; *argv; ++argv, --argc) if (Parse_IsVar(*argv)) Parse_DoVar(*argv, VAR_CMD); else { if (!**argv) Punt("illegal (null) argument."); if (**argv == '-') { if ((*argv)[1]) optind = 0; /* -flag... */ else optind = 1; /* - */ goto rearg; } (void)Lst_AtEnd(create, (ClientData)estrdup(*argv)); } } /*- * Main_ParseArgLine -- * Used by the parse module when a .MFLAGS or .MAKEFLAGS target * is encountered and by main() when reading the .MAKEFLAGS envariable. * Takes a line of arguments and breaks it into its * component words and passes those words and the number of them to the * MainParseArgs function. * The line should have all its leading whitespace removed. * * Results: * None * * Side Effects: * Only those that come from the various arguments. */ void Main_ParseArgLine(line) char *line; /* Line to fracture */ { char **argv; /* Manufactured argument vector */ int argc; /* Number of arguments in argv */ if (line == NULL) return; for (; *line == ' '; ++line) continue; if (!*line) return; argv = brk_string(line, &argc, TRUE); MainParseArgs(argc, argv); } char * chdir_verify_path(path, obpath) char *path; char *obpath; { struct stat sb; if (stat(path, &sb) == 0 && S_ISDIR(sb.st_mode)) { if (chdir(path)) { warn("warning: %s", path); return 0; } else { if (path[0] != '/') { (void) snprintf(obpath, MAXPATHLEN, "%s/%s", curdir, path); return obpath; } else return path; } } return 0; } /*- * main -- * The main function, for obvious reasons. Initializes variables * and a few modules, then parses the arguments give it in the * environment and on the command line. Reads the system makefile * followed by either Makefile, makefile or the file given by the * -f argument. Sets the .MAKEFLAGS PMake variable based on all the * flags it has received by then uses either the Make or the Compat * module to create the initial list of targets. * * Results: * If -q was given, exits -1 if anything was out-of-date. Else it exits * 0. * * Side Effects: * The program exits when done. Targets are created. etc. etc. etc. */ int main(argc, argv) int argc; char **argv; { Lst targs; /* target nodes to create -- passed to Make_Init */ Boolean outOfDate = TRUE; /* FALSE if all targets up to date */ struct stat sa; char *p, *p1, *path, *pathp; #ifdef WANT_ENV_PWD struct stat sb; char *pwd; #endif char mdpath[MAXPATHLEN + 1]; char obpath[MAXPATHLEN + 1]; char cdpath[MAXPATHLEN + 1]; char *machine = getenv("MACHINE"); char *machine_arch = getenv("MACHINE_ARCH"); Lst sysMkPath; /* Path of sys.mk */ char *cp = NULL, *start; /* avoid faults on read-only strings */ static char syspath[] = _PATH_DEFSYSPATH; #ifdef RLIMIT_NOFILE /* * get rid of resource limit on file descriptors */ { struct rlimit rl; if (getrlimit(RLIMIT_NOFILE, &rl) != -1 && rl.rlim_cur != rl.rlim_max) { rl.rlim_cur = rl.rlim_max; (void) setrlimit(RLIMIT_NOFILE, &rl); } } #endif /* * Find where we are and take care of PWD for the automounter... * All this code is so that we know where we are when we start up * on a different machine with pmake. */ curdir = cdpath; if (getcwd(curdir, MAXPATHLEN) == NULL) err(2, NULL); if (stat(curdir, &sa) == -1) err(2, "%s", curdir); #ifdef WANT_ENV_PWD if ((pwd = getenv("PWD")) != NULL) { if (stat(pwd, &sb) == 0 && sa.st_ino == sb.st_ino && sa.st_dev == sb.st_dev) (void) strcpy(curdir, pwd); } #endif #if defined(__i386__) /* * PC-98 kernel sets the `i386' string to the utsname.machine and * it cannot be distinguished from IBM-PC by uname(3). Therefore, * we check machine.ispc98 and adjust the machine variable before * using usname(3) below. */ if (!machine) { int ispc98; size_t len; len = sizeof(ispc98); if (!sysctlbyname("machdep.ispc98", &ispc98, &len, NULL, 0)) { if (ispc98) machine = "pc98"; } } #endif /* * Get the name of this type of MACHINE from utsname * so we can share an executable for similar machines. * (i.e. m68k: amiga hp300, mac68k, sun3, ...) * * Note that while MACHINE is decided at run-time, * MACHINE_ARCH is always known at compile time. */ if (!machine) { #ifndef MACHINE struct utsname utsname; if (uname(&utsname) == -1) { perror("make: uname"); exit(2); } machine = utsname.machine; #else machine = MACHINE; #endif } if (!machine_arch) { #ifndef MACHINE_ARCH machine_arch = "unknown"; #else machine_arch = MACHINE_ARCH; #endif } /* * The object directory location is determined using the * following order of preference: * * 1. MAKEOBJDIRPREFIX`cwd` * 2. MAKEOBJDIR * 3. _PATH_OBJDIR.${MACHINE} * 4. _PATH_OBJDIR * 5. _PATH_OBJDIRPREFIX${MACHINE} * * If all fails, use the current directory to build. * * Once things are initted, * have to add the original directory to the search path, * and modify the paths for the Makefiles apropriately. The * current directory is also placed as a variable for make scripts. */ if (!(pathp = getenv("MAKEOBJDIRPREFIX"))) { if (!(path = getenv("MAKEOBJDIR"))) { path = _PATH_OBJDIR; pathp = _PATH_OBJDIRPREFIX; (void) snprintf(mdpath, MAXPATHLEN, "%s.%s", path, machine); if (!(objdir = chdir_verify_path(mdpath, obpath))) if (!(objdir=chdir_verify_path(path, obpath))) { (void) snprintf(mdpath, MAXPATHLEN, "%s%s", pathp, curdir); if (!(objdir=chdir_verify_path(mdpath, obpath))) objdir = curdir; } } else if (!(objdir = chdir_verify_path(path, obpath))) objdir = curdir; } else { (void) snprintf(mdpath, MAXPATHLEN, "%s%s", pathp, curdir); if (!(objdir = chdir_verify_path(mdpath, obpath))) objdir = curdir; } #ifdef WANT_ENV_PWD setenv("PWD", objdir, 1); #endif create = Lst_Init(FALSE); makefiles = Lst_Init(FALSE); + envFirstVars = Lst_Init(FALSE); printVars = FALSE; variables = Lst_Init(FALSE); beSilent = FALSE; /* Print commands as executed */ ignoreErrors = FALSE; /* Pay attention to non-zero returns */ noExecute = FALSE; /* Execute all commands */ keepgoing = FALSE; /* Stop on error */ allPrecious = FALSE; /* Remove targets when interrupted */ queryFlag = FALSE; /* This is not just a check-run */ noBuiltins = FALSE; /* Read the built-in rules */ touchFlag = FALSE; /* Actually update targets */ usePipes = TRUE; /* Catch child output in pipes */ debug = 0; /* No debug verbosity, please. */ jobsRunning = FALSE; maxLocal = DEFMAXLOCAL; /* Set default local max concurrency */ #ifdef REMOTE maxJobs = DEFMAXJOBS; /* Set default max concurrency */ #else maxJobs = maxLocal; #endif forceJobs = FALSE; /* No -j flag */ compatMake = FALSE; /* No compat mode */ /* * Initialize the parsing, directory and variable modules to prepare * for the reading of inclusion paths and variable settings on the * command line */ Dir_Init(); /* Initialize directory structures so -I flags * can be processed correctly */ Parse_Init(); /* Need to initialize the paths of #include * directories */ Var_Init(); /* As well as the lists of variables for * parsing arguments */ str_init(); if (objdir != curdir) Dir_AddDir(dirSearchPath, curdir); Var_Set(".CURDIR", curdir, VAR_GLOBAL); Var_Set(".OBJDIR", objdir, VAR_GLOBAL); /* * Initialize various variables. * MAKE also gets this name, for compatibility * .MAKEFLAGS gets set to the empty string just in case. * MFLAGS also gets initialized empty, for compatibility. */ Var_Set("MAKE", argv[0], VAR_GLOBAL); Var_Set(MAKEFLAGS, "", VAR_GLOBAL); Var_Set("MFLAGS", "", VAR_GLOBAL); Var_Set("MACHINE", machine, VAR_GLOBAL); Var_Set("MACHINE_ARCH", machine_arch, VAR_GLOBAL); /* * First snag any flags out of the MAKE environment variable. * (Note this is *not* MAKEFLAGS since /bin/make uses that and it's * in a different format). */ #ifdef POSIX Main_ParseArgLine(getenv("MAKEFLAGS")); #else Main_ParseArgLine(getenv("MAKE")); #endif MainParseArgs(argc, argv); /* * Be compatible if user did not specify -j and did not explicitly * turned compatibility on */ if (!compatMake && !forceJobs) compatMake = TRUE; /* * Initialize archive, target and suffix modules in preparation for * parsing the makefile(s) */ Arch_Init(); Targ_Init(); Suff_Init(); DEFAULT = NILGNODE; (void)time(&now); /* * Set up the .TARGETS variable to contain the list of targets to be * created. If none specified, make the variable empty -- the parser * will fill the thing in with the default or .MAIN target. */ if (!Lst_IsEmpty(create)) { LstNode ln; for (ln = Lst_First(create); ln != NILLNODE; ln = Lst_Succ(ln)) { char *name = (char *)Lst_Datum(ln); Var_Append(".TARGETS", name, VAR_GLOBAL); } } else Var_Set(".TARGETS", "", VAR_GLOBAL); /* * If no user-supplied system path was given (through the -m option) * add the directories from the DEFSYSPATH (more than one may be given * as dir1:...:dirn) to the system include path. */ if (Lst_IsEmpty(sysIncPath)) { for (start = syspath; *start != '\0'; start = cp) { for (cp = start; *cp != '\0' && *cp != ':'; cp++) continue; if (*cp == '\0') { Dir_AddDir(sysIncPath, start); } else { *cp++ = '\0'; Dir_AddDir(sysIncPath, start); } } } /* * Read in the built-in rules first, followed by the specified * makefile, if it was (makefile != (char *) NULL), or the default * Makefile and makefile, in that order, if it wasn't. */ if (!noBuiltins) { LstNode ln; sysMkPath = Lst_Init (FALSE); Dir_Expand (_PATH_DEFSYSMK, sysIncPath, sysMkPath); if (Lst_IsEmpty(sysMkPath)) Fatal("make: no system rules (%s).", _PATH_DEFSYSMK); ln = Lst_Find(sysMkPath, (ClientData)NULL, ReadMakefile); if (ln != NILLNODE) Fatal("make: cannot open %s.", (char *)Lst_Datum(ln)); } if (!Lst_IsEmpty(makefiles)) { LstNode ln; ln = Lst_Find(makefiles, (ClientData)NULL, ReadMakefile); if (ln != NILLNODE) Fatal("make: cannot open %s.", (char *)Lst_Datum(ln)); } else if (!ReadMakefile("makefile", NULL)) (void)ReadMakefile("Makefile", NULL); (void)ReadMakefile(".depend", NULL); Var_Append("MFLAGS", Var_Value(MAKEFLAGS, VAR_GLOBAL, &p1), VAR_GLOBAL); if (p1) free(p1); /* Install all the flags into the MAKE envariable. */ if (((p = Var_Value(MAKEFLAGS, VAR_GLOBAL, &p1)) != NULL) && *p) #ifdef POSIX setenv("MAKEFLAGS", p, 1); #else setenv("MAKE", p, 1); #endif if (p1) free(p1); /* * For compatibility, look at the directories in the VPATH variable * and add them to the search path, if the variable is defined. The * variable's value is in the same format as the PATH envariable, i.e. * ::... */ if (Var_Exists("VPATH", VAR_CMD)) { char *vpath, *path, *cp, savec; /* * GCC stores string constants in read-only memory, but * Var_Subst will want to write this thing, so store it * in an array */ static char VPATH[] = "${VPATH}"; vpath = Var_Subst(NULL, VPATH, VAR_CMD, FALSE); path = vpath; do { /* skip to end of directory */ for (cp = path; *cp != ':' && *cp != '\0'; cp++) continue; /* Save terminator character so know when to stop */ savec = *cp; *cp = '\0'; /* Add directory to search path */ Dir_AddDir(dirSearchPath, path); *cp = savec; path = cp + 1; } while (savec == ':'); (void)free((Address)vpath); } /* * Now that all search paths have been read for suffixes et al, it's * time to add the default search path to their lists... */ Suff_DoPaths(); /* print the initial graph, if the user requested it */ if (DEBUG(GRAPH1)) Targ_PrintGraph(1); /* print the values of any variables requested by the user */ if (printVars) { LstNode ln; for (ln = Lst_First(variables); ln != NILLNODE; ln = Lst_Succ(ln)) { char *value = Var_Subst(NULL, (char *)Lst_Datum(ln), VAR_GLOBAL, FALSE); printf("%s\n", value ? value : ""); } } /* * Have now read the entire graph and need to make a list of targets * to create. If none was given on the command line, we consult the * parsing module to find the main target(s) to create. */ if (Lst_IsEmpty(create)) targs = Parse_MainName(); else targs = Targ_FindList(create, TARG_CREATE); if (!compatMake && !printVars) { /* * Initialize job module before traversing the graph, now that * any .BEGIN and .END targets have been read. This is done * only if the -q flag wasn't given (to prevent the .BEGIN from * being executed should it exist). */ if (!queryFlag) { if (maxLocal == -1) maxLocal = maxJobs; Job_Init(maxJobs, maxLocal); jobsRunning = TRUE; } /* Traverse the graph, checking on all the targets */ outOfDate = Make_Run(targs); } else if (!printVars) { /* * Compat_Init will take care of creating all the targets as * well as initializing the module. */ Compat_Run(targs); } Lst_Destroy(targs, NOFREE); Lst_Destroy(variables, NOFREE); Lst_Destroy(makefiles, NOFREE); Lst_Destroy(create, (void (*) __P((ClientData))) free); /* print the graph now it's been processed if the user requested it */ if (DEBUG(GRAPH2)) Targ_PrintGraph(2); Suff_End(); Targ_End(); Arch_End(); str_end(); Var_End(); Parse_End(); Dir_End(); if (queryFlag && outOfDate) return(1); else return(0); } /*- * ReadMakefile -- * Open and parse the given makefile. * * Results: * TRUE if ok. FALSE if couldn't open file. * * Side Effects: * lots */ static Boolean ReadMakefile(p, q) ClientData p, q; { char *fname = p; /* makefile to read */ extern Lst parseIncPath; FILE *stream; char *name, path[MAXPATHLEN + 1]; if (!strcmp(fname, "-")) { Parse_File("(stdin)", stdin); Var_Set("MAKEFILE", "", VAR_GLOBAL); } else { /* if we've chdir'd, rebuild the path name */ if (curdir != objdir && *fname != '/') { (void)sprintf(path, "%s/%s", curdir, fname); if ((stream = fopen(path, "r")) != NULL) { fname = path; goto found; } } else if ((stream = fopen(fname, "r")) != NULL) goto found; /* look in -I and system include directories. */ name = Dir_FindFile(fname, parseIncPath); if (!name) name = Dir_FindFile(fname, sysIncPath); if (!name || !(stream = fopen(name, "r"))) return(FALSE); fname = name; /* * set the MAKEFILE variable desired by System V fans -- the * placement of the setting here means it gets set to the last * makefile specified, as it is set by SysV make. */ found: Var_Set("MAKEFILE", fname, VAR_GLOBAL); Parse_File(fname, stream); (void)fclose(stream); } return(TRUE); } /*- * Cmd_Exec -- * Execute the command in cmd, and return the output of that command * in a string. * * Results: * A string containing the output of the command, or the empty string * If err is not NULL, it contains the reason for the command failure * * Side Effects: * The string must be freed by the caller. */ char * Cmd_Exec(cmd, err) char *cmd; char **err; { char *args[4]; /* Args for invoking the shell */ int fds[2]; /* Pipe streams */ int cpid; /* Child PID */ int pid; /* PID from wait() */ char *res; /* result */ int status; /* command exit status */ Buffer buf; /* buffer to store the result */ char *cp; int cc; *err = NULL; /* * Set up arguments for shell */ args[0] = "sh"; args[1] = "-c"; args[2] = cmd; args[3] = NULL; /* * Open a pipe for fetching its output */ if (pipe(fds) == -1) { *err = "Couldn't create pipe for \"%s\""; goto bad; } /* * Fork */ switch (cpid = vfork()) { case 0: /* * Close input side of pipe */ (void) close(fds[0]); /* * Duplicate the output stream to the shell's output, then * shut the extra thing down. Note we don't fetch the error * stream...why not? Why? */ (void) dup2(fds[1], 1); (void) close(fds[1]); (void) execv("/bin/sh", args); _exit(1); /*NOTREACHED*/ case -1: *err = "Couldn't exec \"%s\""; goto bad; default: /* * No need for the writing half */ (void) close(fds[1]); buf = Buf_Init (MAKE_BSIZE); do { char result[BUFSIZ]; cc = read(fds[0], result, sizeof(result)); if (cc > 0) Buf_AddBytes(buf, cc, (Byte *) result); } while (cc > 0 || (cc == -1 && errno == EINTR)); /* * Close the input side of the pipe. */ (void) close(fds[0]); /* * Wait for the process to exit. */ while(((pid = wait(&status)) != cpid) && (pid >= 0)) continue; if (cc == -1) *err = "Error reading shell's output for \"%s\""; res = (char *)Buf_GetAll (buf, &cc); Buf_Destroy (buf, FALSE); if (status) *err = "\"%s\" returned non-zero status"; /* * Null-terminate the result, convert newlines to spaces and * install it in the variable. */ res[cc] = '\0'; cp = &res[cc] - 1; if (*cp == '\n') { /* * A final newline is just stripped */ *cp-- = '\0'; } while (cp >= res) { if (*cp == '\n') { *cp = ' '; } cp--; } break; } return res; bad: res = emalloc(1); *res = '\0'; return res; } /*- * Error -- * Print an error message given its format. * * Results: * None. * * Side Effects: * The message is printed. */ /* VARARGS */ void #if __STDC__ Error(char *fmt, ...) #else Error(va_alist) va_dcl #endif { va_list ap; #if __STDC__ va_start(ap, fmt); #else char *fmt; va_start(ap); fmt = va_arg(ap, char *); #endif (void)vfprintf(stderr, fmt, ap); va_end(ap); (void)fprintf(stderr, "\n"); (void)fflush(stderr); } /*- * Fatal -- * Produce a Fatal error message. If jobs are running, waits for them * to finish. * * Results: * None * * Side Effects: * The program exits */ /* VARARGS */ void #if __STDC__ Fatal(char *fmt, ...) #else Fatal(va_alist) va_dcl #endif { va_list ap; #if __STDC__ va_start(ap, fmt); #else char *fmt; va_start(ap); fmt = va_arg(ap, char *); #endif if (jobsRunning) Job_Wait(); (void)vfprintf(stderr, fmt, ap); va_end(ap); (void)fprintf(stderr, "\n"); (void)fflush(stderr); if (DEBUG(GRAPH2)) Targ_PrintGraph(2); exit(2); /* Not 1 so -q can distinguish error */ } /* * Punt -- * Major exception once jobs are being created. Kills all jobs, prints * a message and exits. * * Results: * None * * Side Effects: * All children are killed indiscriminately and the program Lib_Exits */ /* VARARGS */ void #if __STDC__ Punt(char *fmt, ...) #else Punt(va_alist) va_dcl #endif { va_list ap; #if __STDC__ va_start(ap, fmt); #else char *fmt; va_start(ap); fmt = va_arg(ap, char *); #endif (void)fprintf(stderr, "make: "); (void)vfprintf(stderr, fmt, ap); va_end(ap); (void)fprintf(stderr, "\n"); (void)fflush(stderr); DieHorribly(); } /*- * DieHorribly -- * Exit without giving a message. * * Results: * None * * Side Effects: * A big one... */ void DieHorribly() { if (jobsRunning) Job_AbortAll(); if (DEBUG(GRAPH2)) Targ_PrintGraph(2); exit(2); /* Not 1, so -q can distinguish error */ } /* * Finish -- * Called when aborting due to errors in child shell to signal * abnormal exit. * * Results: * None * * Side Effects: * The program exits */ void Finish(errors) int errors; /* number of errors encountered in Make_Make */ { Fatal("%d error%s", errors, errors == 1 ? "" : "s"); } /* * emalloc -- * malloc, but die on error. */ void * emalloc(len) size_t len; { void *p; if ((p = malloc(len)) == NULL) enomem(); return(p); } /* * estrdup -- * strdup, but die on error. */ char * estrdup(str) const char *str; { char *p; if ((p = strdup(str)) == NULL) enomem(); return(p); } /* * erealloc -- * realloc, but die on error. */ void * erealloc(ptr, size) void *ptr; size_t size; { if ((ptr = realloc(ptr, size)) == NULL) enomem(); return(ptr); } /* * enomem -- * die when out of memory. */ void enomem() { err(2, NULL); } /* * enunlink -- * Remove a file carefully, avoiding directories. */ int eunlink(file) const char *file; { struct stat st; if (lstat(file, &st) == -1) return -1; if (S_ISDIR(st.st_mode)) { errno = EISDIR; return -1; } return unlink(file); } /* * usage -- * exit with usage message */ static void usage() { (void)fprintf(stderr, "%s\n%s\n%s\n", -"usage: make [-Beiknqrstv] [-D variable] [-d flags] [-f makefile]", +"usage: make [-Beiknqrstv] [-D variable] [-d flags] [-E variable] [-f makefile]", " [-I directory] [-j max_jobs] [-m directory] [-V variable]", " [variable=value] [target ...]"); exit(2); } int PrintAddr(a, b) ClientData a; ClientData b; { printf("%lx ", (unsigned long) a); return b ? 0 : 0; } Index: head/usr.bin/make/make.1 =================================================================== --- head/usr.bin/make/make.1 (revision 49331) +++ head/usr.bin/make/make.1 (revision 49332) @@ -1,1012 +1,1016 @@ .\" Copyright (c) 1990, 1993 .\" The Regents of the University of California. All rights reserved. .\" .\" Redistribution and use in source and binary forms, with or without .\" modification, are permitted provided that the following conditions .\" are met: .\" 1. Redistributions of source code must retain the above copyright .\" notice, this list of conditions and the following disclaimer. .\" 2. Redistributions in binary form must reproduce the above copyright .\" notice, this list of conditions and the following disclaimer in the .\" documentation and/or other materials provided with the distribution. .\" 3. All advertising materials mentioning features or use of this software .\" must display the following acknowledgement: .\" This product includes software developed by the University of .\" California, Berkeley and its contributors. .\" 4. Neither the name of the University nor the names of its contributors .\" may be used to endorse or promote products derived from this software .\" without specific prior written permission. .\" .\" THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND .\" ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE .\" IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE .\" ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE .\" FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL .\" DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS .\" OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) .\" HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT .\" LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY .\" OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF .\" SUCH DAMAGE. .\" .\" from: @(#)make.1 8.4 (Berkeley) 3/19/94 -.\" $Id: make.1,v 1.16 1998/11/29 13:46:39 pds Exp $ +.\" $Id: make.1,v 1.17 1999/04/19 07:30:03 imp Exp $ .\" .Dd March 19, 1994 .Dt MAKE 1 .Os .Sh NAME .Nm make .Nd maintain program dependencies .Sh SYNOPSIS .Nm make .Op Fl Beiknqrstv .Op Fl D Ar variable .Op Fl d Ar flags +.Op Fl E Ar variable .Op Fl f Ar makefile .Op Fl I Ar directory .Bk -words .Op Fl j Ar max_jobs .Op Fl m Ar directory .Ek .Op Fl V Ar variable .Op Ar variable=value .Op Ar target ... .Sh DESCRIPTION .Nm Make is a program designed to simplify the maintenance of other programs. Its input is a list of specifications as to the files upon which programs and other files depend. .Nm Make searches a built-in path for this list. If the file .Ql Pa makefile exists along this path, it is read for this list of specifications. If it does not exist, the file .Ql Pa Makefile is used. If the file .Ql Pa .depend exists, it is also read (see .Xr mkdep 1) . .Pp This manual page is intended as a reference document only. For a more thorough description of .Nm make and makefiles, please refer to .%T "Make \- A Tutorial" . .Pp The options are as follows: .Bl -tag -width Ds .It Fl B Try to be backwards compatible by executing a single shell per command and by executing the commands to make the sources of a dependency line in sequence. .It Fl D Ar variable Define .Ar variable to be 1, in the global context. .It Fl d Ar flags Turn on debugging, and specify which portions of .Nm make are to print debugging information. .Ar Flags is one or more of the following: .Bl -tag -width Ds .It Ar A Print all possible debugging information; equivalent to specifying all of the debugging flags. .It Ar a Print debugging information about archive searching and caching. .It Ar c Print debugging information about conditional evaluation. .It Ar d Print debugging information about directory searching and caching. .It Ar "g1" Print the input graph before making anything. .It Ar "g2" Print the input graph after making everything, or before exiting on error. .It Ar j Print debugging information about running multiple shells. .It Ar m Print debugging information about making targets, including modification dates. .It Ar s Print debugging information about suffix-transformation rules. .It Ar t Print debugging information about target list maintenance. .It Ar v Print debugging information about variable assignment. .El +.It Fl E Ar variable +Specify a variable whose environment value (if any) will override +macro assignments within makefiles. .It Fl e -Specify that environment variables override macro assignments within -makefiles. +Specify that environment values override macro assignments within +makefiles for all variables. .It Fl f Ar makefile Specify a makefile to read instead of the default .Ql Pa makefile and .Ql Pa Makefile . If .Ar makefile is .Ql Fl , standard input is read. Multiple makefile's may be specified, and are read in the order specified. .It Fl I Ar directory Specify a directory in which to search for makefiles and included makefiles. The system makefile directory (or directories, see the .Fl m option) is automatically included as part of this list. .It Fl i Ignore non-zero exit of shell commands in the makefile. Equivalent to specifying .Ql Fl before each command line in the makefile. .It Fl j Ar max_jobs Specify the maximum number of jobs that .Nm make may have running at any one time. Turns compatibility mode off, unless the .Ar B flag is also specified. .It Fl k Continue processing after errors are encountered, but only on those targets that do not depend on the target whose creation caused the error. .It Fl m Ar directory Specify a directory in which to search for sys.mk and makefiles included via the <...> style. Multiple directories can be added to form a search path. This path will override the default system include path: /usr/share/mk. Furthermore the system include path will be appended to the search path used for "..."-style inclusions (see the .Fl I option). .It Fl n Display the commands that would have been executed, but do not actually execute them. .It Fl q Do not execute any commands, but exit 0 if the specified targets are up-to-date and 1, otherwise. .It Fl r Do not use the built-in rules specified in the system makefile. .It Fl s Do not echo any commands as they are executed. Equivalent to specifying .Ql Ic @ before each command line in the makefile. .It Fl t Rather than re-building a target as specified in the makefile, create it or update its modification time to make it appear up-to-date. .It Fl V Ar variable Print .Nm make Ns 's idea of the value of .Ar variable , in the global context. Do not build any targets. Multiple instances of this option may be specified; the variables will be printed one per line, with a blank line for each null or undefined variable. .It Fl v Be extra verbose. For multi-job makes, this will cause file banners to be generated. .It Ar variable=value Set the value of the variable .Ar variable to .Ar value . .El .Pp There are seven different types of lines in a makefile: file dependency specifications, shell commands, variable assignments, include statements, conditional directives, for loops, and comments. .Pp In general, lines may be continued from one line to the next by ending them with a backslash .Pq Ql \e . The trailing newline character and initial whitespace on the following line are compressed into a single space. .Sh FILE DEPENDENCY SPECIFICATIONS Dependency lines consist of one or more targets, an operator, and zero or more sources. This creates a relationship where the targets ``depend'' on the sources and are usually created from them. The exact relationship between the target and the source is determined by the operator that separates them. The three operators are as follows: .Bl -tag -width flag .It Ic \&: A target is considered out-of-date if its modification time is less than those of any of its sources. Sources for a target accumulate over dependency lines when this operator is used. The target is removed if .Nm make is interrupted. .It Ic \&! Targets are always re-created, but not until all sources have been examined and re-created as necessary. Sources for a target accumulate over dependency lines when this operator is used. The target is removed if .Nm make is interrupted. .It Ic \&:: If no sources are specified, the target is always re-created. Otherwise, a target is considered out-of-date if any of its sources has been modified more recently than the target. Sources for a target do not accumulate over dependency lines when this operator is used. The target will not be removed if .Nm make is interrupted. .El .Pp Targets and sources may contain the shell wildcard values .Ql ? , .Ql * , .Ql [] and .Ql {} . The values .Ql ? , .Ql * and .Ql [] may only be used as part of the final component of the target or source, and must be used to describe existing files. The value .Ql {} need not necessarily be used to describe existing files. Expansion is in directory order, not alphabetically as done in the shell. .Sh SHELL COMMANDS Each target may have associated with it a series of shell commands, normally used to create the target. Each of the commands in this script .Em must be preceded by a tab. While any target may appear on a dependency line, only one of these dependencies may be followed by a creation script, unless the .Ql Ic :: operator is used. .Pp If the first or first two characters of the command line are .Ql Ic @ and/or .Ql Ic \- , the command is treated specially. A .Ql Ic @ causes the command not to be echoed before it is executed. A .Ql Ic \- causes any non-zero exit status of the command line to be ignored. .Sh VARIABLE ASSIGNMENTS Variables in make are much like variables in the shell, and, by tradition, consist of all upper-case letters. The five operators that can be used to assign values to variables are as follows: .Bl -tag -width Ds .It Ic \&= Assign the value to the variable. Any previous value is overridden. .It Ic \&+= Append the value to the current value of the variable. .It Ic \&?= Assign the value to the variable if it is not already defined. .It Ic \&:= Assign with expansion, i.e. expand the value before assigning it to the variable. Normally, expansion is not done until the variable is referenced. .It Ic \&!= Expand the value and pass it to the shell for execution and assign the result to the variable. Any newlines in the result are replaced with spaces. .El .Pp Any white-space before the assigned .Ar value is removed; if the value is being appended, a single space is inserted between the previous contents of the variable and the appended value. .Pp Variables are expanded by surrounding the variable name with either curly braces .Pq Ql {} or parentheses .Pq Ql () and preceding it with a dollar sign .Pq Ql \&$ . If the variable name contains only a single letter, the surrounding braces or parentheses are not required. This shorter form is not recommended. .Pp Variable substitution occurs at two distinct times, depending on where the variable is being used. Variables in dependency lines are expanded as the line is read. Variables in shell commands are expanded when the shell command is executed. .Pp The four different classes of variables (in order of increasing precedence) are: .Bl -tag -width Ds .It Environment variables Variables defined as part of .Nm make Ns 's environment. .It Global variables Variables defined in the makefile or in included makefiles. .It Command line variables Variables defined as part of the command line. .It Local variables Variables that are defined specific to a certain target. The seven local variables are as follows: .Bl -tag -width ".ARCHIVE" .It Va .ALLSRC The list of all sources for this target; also known as .Ql Va \&> . .It Va .ARCHIVE The name of the archive file. .It Va .IMPSRC The name/path of the source from which the target is to be transformed (the ``implied'' source); also known as .Ql Va \&< . .It Va .MEMBER The name of the archive member. .It Va .OODATE The list of sources for this target that were deemed out-of-date; also known as .Ql Va \&? . .It Va .PREFIX The file prefix of the file, containing only the file portion, no suffix or preceding directory components; also known as .Ql Va * . .It Va .TARGET The name of the target; also known as .Ql Va @ . .El .Pp The shorter forms .Ql Va @ , .Ql Va ? , .Ql Va \&> and .Ql Va * are permitted for backward compatibility with historical makefiles and are not recommended. The six variables .Ql Va "@F" , .Ql Va "@D" , .Ql Va " .It Ic \&.include Ar \*qfile\*q Include the specified makefile. Variables between the angle brackets or double quotes are expanded to form the file name. If angle brackets are used, the included makefile is expected to be in the system makefile directory. If double quotes are used, the including makefile's directory and any directories specified using the .Fl I option are searched before the system makefile directory. .It Ic \&.undef Ar variable Un-define the specified global variable. Only global variables may be un-defined. .It Ic \&.error Ar message Terminate processing of the makefile immediately. The filename of the makefile, the line on which the error was encountered and the specified message are printed to standard output and .Nm make terminates with exit code 1. Variables in the message are expanded. .El .Pp Conditionals are used to determine which parts of the Makefile to process. They are used similarly to the conditionals supported by the C pre-processor. The following conditionals are supported: .Bl -tag -width Ds .It Xo .Ic \&.if .Oo \&! Oc Ns Ar expression .Op Ar operator expression ... .Xc Test the value of an expression. .It Xo .Ic .ifdef .Oo \&! Oc Ns Ar variable .Op Ar operator variable ... .Xc Test the value of a variable. .It Xo .Ic .ifndef .Oo \&! Oc Ns Ar variable .Op Ar operator variable ... .Xc Test the value of a variable. .It Xo .Ic .ifmake .Oo \&! Oc Ns Ar target .Op Ar operator target ... .Xc Test the target being built. .It Xo .Ic .ifnmake .Oo \&! Oc Ar target .Op Ar operator target ... .Xc Test the target being built. .It Ic .else Reverse the sense of the last conditional. .It Xo .Ic .elif .Oo \&! Oc Ar expression .Op Ar operator expression ... .Xc A combination of .Ql Ic .else followed by .Ql Ic .if . .It Xo .Ic .elifdef .Oo \&! Oc Ns Ar variable .Op Ar operator variable ... .Xc A combination of .Ql Ic .else followed by .Ql Ic .ifdef . .It Xo .Ic .elifndef .Oo \&! Oc Ns Ar variable .Op Ar operator variable ... .Xc A combination of .Ql Ic .else followed by .Ql Ic .ifndef . .It Xo .Ic .elifmake .Oo \&! Oc Ns Ar target .Op Ar operator target ... .Xc A combination of .Ql Ic .else followed by .Ql Ic .ifmake . .It Xo .Ic .elifnmake .Oo \&! Oc Ns Ar target .Op Ar operator target ... .Xc A combination of .Ql Ic .else followed by .Ql Ic .ifnmake . .It Ic .endif End the body of the conditional. .El .Pp The .Ar operator may be any one of the following: .Bl -tag -width "Cm XX" .It Cm \&|\&| logical OR .It Cm \&&& Logical .Tn AND ; of higher precedence than .Dq . .El .Pp As in C, .Nm make will only evaluate a conditional as far as is necessary to determine its value. Parentheses may be used to change the order of evaluation. The boolean operator .Ql Ic \&! may be used to logically negate an entire conditional. It is of higher precedence than .Ql Ic \&&& . .Pp The value of .Ar expression may be any of the following: .Bl -tag -width Ic defined .It Ic defined Takes a variable name as an argument and evaluates to true if the variable has been defined. .It Ic make Takes a target name as an argument and evaluates to true if the target was specified as part of .Nm make Ns 's command line or was declared the default target (either implicitly or explicitly, see .Va .MAIN ) before the line containing the conditional. .It Ic empty Takes a variable, with possible modifiers, and evaluates to true if the expansion of the variable would result in an empty string. .It Ic exists Takes a file name as an argument and evaluates to true if the file exists. The file is searched for on the system search path (see .Va .PATH ) . .It Ic target Takes a target name as an argument and evaluates to true if the target has been defined. .El .Pp .Ar Expression may also be an arithmetic or string comparison. Variable expansion is performed on both sides of the comparison, after which the integral values are compared. A value is interpreted as hexadecimal if it is preceded by 0x, otherwise it is decimal; octal numbers are not supported. The standard C relational operators are all supported. If after variable expansion, either the left or right hand side of a .Ql Ic == or .Ql Ic "!=" operator is not an integral value, then string comparison is performed between the expanded variables. If no relational operator is given, it is assumed that the expanded variable is being compared against 0. .Pp When .Nm make is evaluating one of these conditional expression, and it encounters a word it doesn't recognize, either the ``make'' or ``defined'' expression is applied to it, depending on the form of the conditional. If the form is .Ql Ic .ifdef or .Ql Ic .ifndef , the ``defined'' expression is applied. Similarly, if the form is .Ql Ic .ifmake or .Ql Ic .ifnmake , the ``make'' expression is applied. .Pp If the conditional evaluates to true the parsing of the makefile continues as before. If it evaluates to false, the following lines are skipped. In both cases this continues until a .Ql Ic .else or .Ql Ic .endif is found. .Pp For loops are typically used to apply a set of rules to a list of files. The syntax of a for loop is: .Bl -tag -width Ds .It Xo .Ic \&.for .Ar variable .Ic in .Ar expression .Xc .It Xo .Xc .It Xo .Ic \&.endfor .Xc .El After the for .Ic expression is evaluated, it is split into words. The iteration .Ic variable is successively set to each word, and substituted in the .Ic make-rules inside the body of the for loop. .Sh COMMENTS Comments begin with a hash .Pq Ql \&# character, anywhere but in a shell command line, and continue to the end of the line. .Sh SPECIAL SOURCES .Bl -tag -width Ic .IGNORE .It Ic .IGNORE Ignore any errors from the commands associated with this target, exactly as if they all were preceded by a dash .Pq Ql \- . .It Ic .MAKE Execute the commands associated with this target even if the .Fl n or .Fl t options were specified. Normally used to mark recursive .Nm make Ns 's . .It Ic .NOTMAIN Normally .Nm make selects the first target it encounters as the default target to be built if no target was specified. This source prevents this target from being selected. .It Ic .OPTIONAL If a target is marked with this attribute and .Nm make can't figure out how to create it, it will ignore this fact and assume the file isn't needed or already exists. .It Ic .PRECIOUS When .Nm make is interrupted, it removes any partially made targets. This source prevents the target from being removed. .It Ic .SILENT Do not echo any of the commands associated with this target, exactly as if they all were preceded by an at sign .Pq Ql @ . .It Ic .USE Turn the target into .Nm make Ns 's . version of a macro. When the target is used as a source for another target, the other target acquires the commands, sources, and attributes (except for .Ic .USE ) of the source. If the target already has commands, the .Ic .USE target's commands are appended to them. .It Ic .WAIT If special .Ic .WAIT source is appears in a dependency line, the sources that precede it are made before the sources that succeed it in the line. Loops are not being detected and targets that form loops will be silently ignored. .El .Sh "SPECIAL TARGETS" Special targets may not be included with other targets, i.e. they must be the only target specified. .Bl -tag -width Ic .BEGIN .It Ic .BEGIN Any command lines attached to this target are executed before anything else is done. .It Ic .DEFAULT This is sort of a .Ic .USE rule for any target (that was used only as a source) that .Nm make can't figure out any other way to create. Only the shell script is used. The .Ic .IMPSRC variable of a target that inherits .Ic .DEFAULT Ns 's commands is set to the target's own name. .It Ic .END Any command lines attached to this target are executed after everything else is done. .It Ic .IGNORE Mark each of the sources with the .Ic .IGNORE attribute. If no sources are specified, this is the equivalent of specifying the .Fl i option. .It Ic .INTERRUPT If .Nm make is interrupted, the commands for this target will be executed. .It Ic .MAIN If no target is specified when .Nm make is invoked, this target will be built. .It Ic .MAKEFLAGS This target provides a way to specify flags for .Nm make when the makefile is used. The flags are as if typed to the shell, though the .Fl f option will have no effect. .\" XXX: NOT YET!!!! .\" .It Ic .NOTPARALLEL .\" The named targets are executed in non parallel mode. If no targets are .\" specified, then all targets are executed in non parallel mode. .It Ic .NOTPARALLEL Disable parallel mode. .It Ic .NO_PARALLEL Same as above, for compatibility with other pmake variants. .It Ic .ORDER The named targets are made in sequence. .\" XXX: NOT YET!!!! .\" .It Ic .PARALLEL .\" The named targets are executed in parallel mode. If no targets are .\" specified, then all targets are executed in parallel mode. .It Ic .PATH The sources are directories which are to be searched for files not found in the current directory. If no sources are specified, any previously specified directories are deleted. .It Ic .PHONY Apply the .Ic .PHONY attribute to any specified sources. Targets with this attribute are always considered to be out of date. .It Ic .PRECIOUS Apply the .Ic .PRECIOUS attribute to any specified sources. If no sources are specified, the .Ic .PRECIOUS attribute is applied to every target in the file. .It Ic .SILENT Apply the .Ic .SILENT attribute to any specified sources. If no sources are specified, the .Ic .SILENT attribute is applied to every command in the file. .It Ic .SUFFIXES Each source specifies a suffix to .Nm make . If no sources are specified, any previous specified suffices are deleted. .Sh SEARCH PATH .Nm Make searches for .Ql Pa Makefile in the following directories, in order: .Pp .Bl -enum -compact .It ${MAKEOBJDIRPREFIX}`cwd` .It ${MAKEOBJDIR} .It obj.${MACHINE} .It obj .It /usr/obj${MACHINE}`cwd` .It The current directory (`cwd`). .El .Pp If .Nm make finds the .Ql Pa Makefile someplace other than the current working directory, it adds the CWD to the search path. .Sh ENVIRONMENT .Nm Make utilizes the following environment variables, if they exist: .Ev MACHINE , .Ev MAKE , .Ev MAKEFLAGS , .Ev MAKEOBJDIR , .Ev MAKEOBJDIRPREFIX , and .Ev PWD . .Sh FILES .Bl -tag -width /usr/share/doc/psd/12.make -compact .It .depend list of dependencies .It Makefile list of dependencies .It makefile list of dependencies .It obj object directory .It sys.mk system makefile .It /usr/share/mk system makefile directory .It /usr/share/doc/psd/12.make PMake tutorial .It /usr/obj system object directory prefix .El .Sh SEE ALSO .Xr mkdep 1 .Rs .%T "PMake - A Tutorial" .Re .Sh HISTORY A .Nm Make command appeared in .At v7 . Index: head/usr.bin/make/make.h =================================================================== --- head/usr.bin/make/make.h (revision 49331) +++ head/usr.bin/make/make.h (revision 49332) @@ -1,373 +1,376 @@ /* * Copyright (c) 1988, 1989, 1990, 1993 * The Regents of the University of California. All rights reserved. * Copyright (c) 1989 by Berkeley Softworks * All rights reserved. * * This code is derived from software contributed to Berkeley by * Adam de Boor. * * Redistribution and use in source and binary forms, with or without * modification, are permitted provided that the following conditions * are met: * 1. Redistributions of source code must retain the above copyright * notice, this list of conditions and the following disclaimer. * 2. Redistributions in binary form must reproduce the above copyright * notice, this list of conditions and the following disclaimer in the * documentation and/or other materials provided with the distribution. * 3. All advertising materials mentioning features or use of this software * must display the following acknowledgement: * This product includes software developed by the University of * California, Berkeley and its contributors. * 4. Neither the name of the University nor the names of its contributors * may be used to endorse or promote products derived from this software * without specific prior written permission. * * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF * SUCH DAMAGE. * * from: @(#)make.h 8.3 (Berkeley) 6/13/95 - * $Id: make.h,v 1.7 1997/02/22 19:27:16 peter Exp $ + * $Id: make.h,v 1.8 1998/11/14 16:15:04 dg Exp $ */ /*- * make.h -- * The global definitions for pmake */ #ifndef _MAKE_H_ #define _MAKE_H_ #include #include #include #include #if !defined(MAKE_BOOTSTRAP) && defined(BSD) #include #else #ifndef __P #if defined(__STDC__) || defined(__cplusplus) #define __P(protos) protos /* full-blown ANSI C */ #else #define __P(protos) () /* traditional C preprocessor */ #endif #endif #endif #if __STDC__ #include #include #endif #include "sprite.h" #include "lst.h" #include "config.h" #include "buf.h" /*- * The structure for an individual graph node. Each node has several * pieces of data associated with it. * 1) the name of the target it describes * 2) the location of the target file in the file system. * 3) the type of operator used to define its sources (qv. parse.c) * 4) whether it is involved in this invocation of make * 5) whether the target has been remade * 6) whether any of its children has been remade * 7) the number of its children that are, as yet, unmade * 8) its modification time * 9) the modification time of its youngest child (qv. make.c) * 10) a list of nodes for which this is a source * 11) a list of nodes on which this depends * 12) a list of nodes that depend on this, as gleaned from the * transformation rules. * 13) a list of nodes of the same name created by the :: operator * 14) a list of nodes that must be made (if they're made) before * this node can be, but that do no enter into the datedness of * this node. * 15) a list of nodes that must be made (if they're made) after * this node is, but that do not depend on this node, in the * normal sense. * 16) a Lst of ``local'' variables that are specific to this target * and this target only (qv. var.c [$@ $< $?, etc.]) * 17) a Lst of strings that are commands to be given to a shell * to create this target. */ typedef struct GNode { char *name; /* The target's name */ char *path; /* The full pathname of the file */ int type; /* Its type (see the OP flags, below) */ int order; /* Its wait weight */ Boolean make; /* TRUE if this target needs to be remade */ enum { UNMADE, BEINGMADE, MADE, UPTODATE, ERROR, ABORTED, CYCLE, ENDCYCLE, } made; /* Set to reflect the state of processing * on this node: * UNMADE - Not examined yet * BEINGMADE - Target is already being made. * Indicates a cycle in the graph. (compat * mode only) * MADE - Was out-of-date and has been made * UPTODATE - Was already up-to-date * ERROR - An error occured while it was being * made (used only in compat mode) * ABORTED - The target was aborted due to * an error making an inferior (compat). * CYCLE - Marked as potentially being part of * a graph cycle. If we come back to a * node marked this way, it is printed * and 'made' is changed to ENDCYCLE. * ENDCYCLE - the cycle has been completely * printed. Go back and unmark all its * members. */ Boolean childMade; /* TRUE if one of this target's children was * made */ int unmade; /* The number of unmade children */ int mtime; /* Its modification time */ int cmtime; /* The modification time of its youngest * child */ Lst iParents; /* Links to parents for which this is an * implied source, if any */ Lst cohorts; /* Other nodes for the :: operator */ Lst parents; /* Nodes that depend on this one */ Lst children; /* Nodes on which this one depends */ Lst successors; /* Nodes that must be made after this one */ Lst preds; /* Nodes that must be made before this one */ Lst context; /* The local variables */ Lst commands; /* Creation commands */ struct _Suff *suffix; /* Suffix for the node (determined by * Suff_FindDeps and opaque to everyone * but the Suff module) */ } GNode; /* * Manifest constants */ #define NILGNODE ((GNode *) NIL) /* * The OP_ constants are used when parsing a dependency line as a way of * communicating to other parts of the program the way in which a target * should be made. These constants are bitwise-OR'ed together and * placed in the 'type' field of each node. Any node that has * a 'type' field which satisfies the OP_NOP function was never never on * the lefthand side of an operator, though it may have been on the * righthand side... */ #define OP_DEPENDS 0x00000001 /* Execution of commands depends on * kids (:) */ #define OP_FORCE 0x00000002 /* Always execute commands (!) */ #define OP_DOUBLEDEP 0x00000004 /* Execution of commands depends on kids * per line (::) */ #define OP_OPMASK (OP_DEPENDS|OP_FORCE|OP_DOUBLEDEP) #define OP_OPTIONAL 0x00000008 /* Don't care if the target doesn't * exist and can't be created */ #define OP_USE 0x00000010 /* Use associated commands for parents */ #define OP_EXEC 0x00000020 /* Target is never out of date, but always * execute commands anyway. Its time * doesn't matter, so it has none...sort * of */ #define OP_IGNORE 0x00000040 /* Ignore errors when creating the node */ #define OP_PRECIOUS 0x00000080 /* Don't remove the target when * interrupted */ #define OP_SILENT 0x00000100 /* Don't echo commands when executed */ #define OP_MAKE 0x00000200 /* Target is a recurrsive make so its * commands should always be executed when * it is out of date, regardless of the * state of the -n or -t flags */ #define OP_JOIN 0x00000400 /* Target is out-of-date only if any of its * children was out-of-date */ #define OP_INVISIBLE 0x00004000 /* The node is invisible to its parents. * I.e. it doesn't show up in the parents's * local variables. */ #define OP_NOTMAIN 0x00008000 /* The node is exempt from normal 'main * target' processing in parse.c */ #define OP_PHONY 0x00010000 /* Not a file target; run always */ /* Attributes applied by PMake */ #define OP_TRANSFORM 0x80000000 /* The node is a transformation rule */ #define OP_MEMBER 0x40000000 /* Target is a member of an archive */ #define OP_LIB 0x20000000 /* Target is a library */ #define OP_ARCHV 0x10000000 /* Target is an archive construct */ #define OP_HAS_COMMANDS 0x08000000 /* Target has all the commands it should. * Used when parsing to catch multiple * commands for a target */ #define OP_SAVE_CMDS 0x04000000 /* Saving commands on .END (Compat) */ #define OP_DEPS_FOUND 0x02000000 /* Already processed by Suff_FindDeps */ /* * OP_NOP will return TRUE if the node with the given type was not the * object of a dependency operator */ #define OP_NOP(t) (((t) & OP_OPMASK) == 0x00000000) /* * The TARG_ constants are used when calling the Targ_FindNode and * Targ_FindList functions in targ.c. They simply tell the functions what to * do if the desired node(s) is (are) not found. If the TARG_CREATE constant * is given, a new, empty node will be created for the target, placed in the * table of all targets and its address returned. If TARG_NOCREATE is given, * a NIL pointer will be returned. */ #define TARG_CREATE 0x01 /* create node if not found */ #define TARG_NOCREATE 0x00 /* don't create it */ /* * There are several places where expandable buffers are used (parse.c and * var.c). This constant is merely the starting point for those buffers. If * lines tend to be much shorter than this, it would be best to reduce BSIZE. * If longer, it should be increased. Reducing it will cause more copying to * be done for longer lines, but will save space for shorter ones. In any * case, it ought to be a power of two simply because most storage allocation * schemes allocate in powers of two. */ #define MAKE_BSIZE 256 /* starting size for expandable buffers */ /* * These constants are all used by the Str_Concat function to decide how the * final string should look. If STR_ADDSPACE is given, a space will be * placed between the two strings. If STR_ADDSLASH is given, a '/' will * be used instead of a space. If neither is given, no intervening characters * will be placed between the two strings in the final output. If the * STR_DOFREE bit is set, the two input strings will be freed before * Str_Concat returns. */ #define STR_ADDSPACE 0x01 /* add a space when Str_Concat'ing */ #define STR_DOFREE 0x02 /* free source strings after concatenation */ #define STR_ADDSLASH 0x04 /* add a slash when Str_Concat'ing */ /* * Error levels for parsing. PARSE_FATAL means the process cannot continue * once the makefile has been parsed. PARSE_WARNING means it can. Passed * as the first argument to Parse_Error. */ #define PARSE_WARNING 2 #define PARSE_FATAL 1 /* * Values returned by Cond_Eval. */ #define COND_PARSE 0 /* Parse the next lines */ #define COND_SKIP 1 /* Skip the next lines */ #define COND_INVALID 2 /* Not a conditional statement */ /* * Definitions for the "local" variables. Used only for clarity. */ #define TARGET "@" /* Target of dependency */ #define OODATE "?" /* All out-of-date sources */ #define ALLSRC ">" /* All sources */ #define IMPSRC "<" /* Source implied by transformation */ #define PREFIX "*" /* Common prefix */ #define ARCHIVE "!" /* Archive in "archive(member)" syntax */ #define MEMBER "%" /* Member in "archive(member)" syntax */ #define FTARGET "@F" /* file part of TARGET */ #define DTARGET "@D" /* directory part of TARGET */ #define FIMPSRC " #include #include "make.h" #include "buf.h" /* * This is a harmless return value for Var_Parse that can be used by Var_Subst * to determine if there was an error in parsing -- easier than returning * a flag, as things outside this module don't give a hoot. */ char var_Error[] = ""; /* * Similar to var_Error, but returned when the 'err' flag for Var_Parse is * set false. Why not just use a constant? Well, gcc likes to condense * identical string instances... */ static char varNoError[] = ""; /* * Internally, variables are contained in four different contexts. * 1) the environment. They may not be changed. If an environment * variable is appended-to, the result is placed in the global * context. * 2) the global context. Variables set in the Makefile are located in * the global context. It is the penultimate context searched when * substituting. * 3) the command-line context. All variables set on the command line * are placed in this context. They are UNALTERABLE once placed here. * 4) the local context. Each target has associated with it a context * list. On this list are located the structures describing such * local variables as $(@) and $(*) * The four contexts are searched in the reverse order from which they are * listed. */ GNode *VAR_GLOBAL; /* variables from the makefile */ GNode *VAR_CMD; /* variables defined on the command-line */ static Lst allVars; /* List of all variables */ #define FIND_CMD 0x1 /* look in VAR_CMD when searching */ #define FIND_GLOBAL 0x2 /* look in VAR_GLOBAL as well */ #define FIND_ENV 0x4 /* look in the environment also */ typedef struct Var { char *name; /* the variable's name */ Buffer val; /* its value */ int flags; /* miscellaneous status flags */ #define VAR_IN_USE 1 /* Variable's value currently being used. * Used to avoid recursion */ #define VAR_FROM_ENV 2 /* Variable comes from the environment */ #define VAR_JUNK 4 /* Variable is a junk variable that * should be destroyed when done with * it. Used by Var_Parse for undefined, * modified variables */ } Var; typedef struct { char *lhs; /* String to match */ int leftLen; /* Length of string */ char *rhs; /* Replacement string (w/ &'s removed) */ int rightLen; /* Length of replacement */ int flags; #define VAR_SUB_GLOBAL 1 /* Apply substitution globally */ #define VAR_MATCH_START 2 /* Match at start of word */ #define VAR_MATCH_END 4 /* Match at end of word */ } VarPattern; static int VarCmp __P((ClientData, ClientData)); static Var *VarFind __P((char *, GNode *, int)); static void VarAdd __P((char *, char *, GNode *)); static void VarDelete __P((ClientData)); static Boolean VarHead __P((char *, Boolean, Buffer, ClientData)); static Boolean VarTail __P((char *, Boolean, Buffer, ClientData)); static Boolean VarSuffix __P((char *, Boolean, Buffer, ClientData)); static Boolean VarRoot __P((char *, Boolean, Buffer, ClientData)); static Boolean VarMatch __P((char *, Boolean, Buffer, ClientData)); #ifdef SYSVVARSUB static Boolean VarSYSVMatch __P((char *, Boolean, Buffer, ClientData)); #endif static Boolean VarNoMatch __P((char *, Boolean, Buffer, ClientData)); static Boolean VarSubstitute __P((char *, Boolean, Buffer, ClientData)); static char *VarQuote __P((char *)); static char *VarModify __P((char *, Boolean (*)(char *, Boolean, Buffer, ClientData), ClientData)); static int VarPrintVar __P((ClientData, ClientData)); /*- *----------------------------------------------------------------------- * VarCmp -- * See if the given variable matches the named one. Called from * Lst_Find when searching for a variable of a given name. * * Results: * 0 if they match. non-zero otherwise. * * Side Effects: * none *----------------------------------------------------------------------- */ static int VarCmp (v, name) ClientData v; /* VAR structure to compare */ ClientData name; /* name to look for */ { return (strcmp ((char *) name, ((Var *) v)->name)); } /*- *----------------------------------------------------------------------- * VarFind -- * Find the given variable in the given context and any other contexts * indicated. * * Results: * A pointer to the structure describing the desired variable or * NIL if the variable does not exist. * * Side Effects: * None *----------------------------------------------------------------------- */ static Var * VarFind (name, ctxt, flags) char *name; /* name to find */ GNode *ctxt; /* context in which to find it */ int flags; /* FIND_GLOBAL set means to look in the * VAR_GLOBAL context as well. * FIND_CMD set means to look in the VAR_CMD * context also. * FIND_ENV set means to look in the * environment */ { + Boolean localCheckEnvFirst; LstNode var; Var *v; /* * If the variable name begins with a '.', it could very well be one of * the local ones. We check the name against all the local variables * and substitute the short version in for 'name' if it matches one of * them. */ if (*name == '.' && isupper((unsigned char) name[1])) switch (name[1]) { case 'A': if (!strcmp(name, ".ALLSRC")) name = ALLSRC; if (!strcmp(name, ".ARCHIVE")) name = ARCHIVE; break; case 'I': if (!strcmp(name, ".IMPSRC")) name = IMPSRC; break; case 'M': if (!strcmp(name, ".MEMBER")) name = MEMBER; break; case 'O': if (!strcmp(name, ".OODATE")) name = OODATE; break; case 'P': if (!strcmp(name, ".PREFIX")) name = PREFIX; break; case 'T': if (!strcmp(name, ".TARGET")) name = TARGET; break; } + /* + * Note whether this is one of the specific variables we were told through + * the -E flag to use environment-variable-override for. + */ + if (Lst_Find (envFirstVars, (ClientData)name, + (int (*)(ClientData, ClientData)) strcmp) != NILLNODE) + { + localCheckEnvFirst = TRUE; + } + else { + localCheckEnvFirst = FALSE; + } + + /* * First look for the variable in the given context. If it's not there, * look for it in VAR_CMD, VAR_GLOBAL and the environment, in that order, * depending on the FIND_* flags in 'flags' */ var = Lst_Find (ctxt->context, (ClientData)name, VarCmp); if ((var == NILLNODE) && (flags & FIND_CMD) && (ctxt != VAR_CMD)) { var = Lst_Find (VAR_CMD->context, (ClientData)name, VarCmp); } - if (!checkEnvFirst && (var == NILLNODE) && (flags & FIND_GLOBAL) && - (ctxt != VAR_GLOBAL)) + if ((var == NILLNODE) && (flags & FIND_GLOBAL) && (ctxt != VAR_GLOBAL) && + !checkEnvFirst && !localCheckEnvFirst) { var = Lst_Find (VAR_GLOBAL->context, (ClientData)name, VarCmp); } if ((var == NILLNODE) && (flags & FIND_ENV)) { char *env; if ((env = getenv (name)) != NULL) { int len; v = (Var *) emalloc(sizeof(Var)); v->name = estrdup(name); len = strlen(env); v->val = Buf_Init(len); Buf_AddBytes(v->val, len, (Byte *)env); v->flags = VAR_FROM_ENV; return (v); - } else if (checkEnvFirst && (flags & FIND_GLOBAL) && - (ctxt != VAR_GLOBAL)) + } else if ((checkEnvFirst || localCheckEnvFirst) && + (flags & FIND_GLOBAL) && (ctxt != VAR_GLOBAL)) { var = Lst_Find (VAR_GLOBAL->context, (ClientData)name, VarCmp); if (var == NILLNODE) { return ((Var *) NIL); } else { return ((Var *)Lst_Datum(var)); } } else { return((Var *)NIL); } } else if (var == NILLNODE) { return ((Var *) NIL); } else { return ((Var *) Lst_Datum (var)); } } /*- *----------------------------------------------------------------------- * VarAdd -- * Add a new variable of name name and value val to the given context * * Results: * None * * Side Effects: * The new variable is placed at the front of the given context * The name and val arguments are duplicated so they may * safely be freed. *----------------------------------------------------------------------- */ static void VarAdd (name, val, ctxt) char *name; /* name of variable to add */ char *val; /* value to set it to */ GNode *ctxt; /* context in which to set it */ { register Var *v; int len; v = (Var *) emalloc (sizeof (Var)); v->name = estrdup (name); len = val ? strlen(val) : 0; v->val = Buf_Init(len+1); Buf_AddBytes(v->val, len, (Byte *)val); v->flags = 0; (void) Lst_AtFront (ctxt->context, (ClientData)v); (void) Lst_AtEnd (allVars, (ClientData) v); if (DEBUG(VAR)) { printf("%s:%s = %s\n", ctxt->name, name, val); } } /*- *----------------------------------------------------------------------- * VarDelete -- * Delete a variable and all the space associated with it. * * Results: * None * * Side Effects: * None *----------------------------------------------------------------------- */ static void VarDelete(vp) ClientData vp; { Var *v = (Var *) vp; free(v->name); Buf_Destroy(v->val, TRUE); free((Address) v); } /*- *----------------------------------------------------------------------- * Var_Delete -- * Remove a variable from a context. * * Results: * None. * * Side Effects: * The Var structure is removed and freed. * *----------------------------------------------------------------------- */ void Var_Delete(name, ctxt) char *name; GNode *ctxt; { LstNode ln; if (DEBUG(VAR)) { printf("%s:delete %s\n", ctxt->name, name); } ln = Lst_Find(ctxt->context, (ClientData)name, VarCmp); if (ln != NILLNODE) { register Var *v; v = (Var *)Lst_Datum(ln); Lst_Remove(ctxt->context, ln); ln = Lst_Member(allVars, v); Lst_Remove(allVars, ln); VarDelete((ClientData) v); } } /*- *----------------------------------------------------------------------- * Var_Set -- * Set the variable name to the value val in the given context. * * Results: * None. * * Side Effects: * If the variable doesn't yet exist, a new record is created for it. * Else the old value is freed and the new one stuck in its place * * Notes: * The variable is searched for only in its context before being * created in that context. I.e. if the context is VAR_GLOBAL, * only VAR_GLOBAL->context is searched. Likewise if it is VAR_CMD, only * VAR_CMD->context is searched. This is done to avoid the literally * thousands of unnecessary strcmp's that used to be done to * set, say, $(@) or $(<). *----------------------------------------------------------------------- */ void Var_Set (name, val, ctxt) char *name; /* name of variable to set */ char *val; /* value to give to the variable */ GNode *ctxt; /* context in which to set it */ { register Var *v; /* * We only look for a variable in the given context since anything set * here will override anything in a lower context, so there's not much * point in searching them all just to save a bit of memory... */ v = VarFind (name, ctxt, 0); if (v == (Var *) NIL) { VarAdd (name, val, ctxt); } else { Buf_Discard(v->val, Buf_Size(v->val)); Buf_AddBytes(v->val, strlen(val), (Byte *)val); if (DEBUG(VAR)) { printf("%s:%s = %s\n", ctxt->name, name, val); } } /* * Any variables given on the command line are automatically exported * to the environment (as per POSIX standard) */ if (ctxt == VAR_CMD) { setenv(name, val, 1); } } /*- *----------------------------------------------------------------------- * Var_Append -- * The variable of the given name has the given value appended to it in * the given context. * * Results: * None * * Side Effects: * If the variable doesn't exist, it is created. Else the strings * are concatenated (with a space in between). * * Notes: * Only if the variable is being sought in the global context is the * environment searched. * XXX: Knows its calling circumstances in that if called with ctxt * an actual target, it will only search that context since only * a local variable could be being appended to. This is actually * a big win and must be tolerated. *----------------------------------------------------------------------- */ void Var_Append (name, val, ctxt) char *name; /* Name of variable to modify */ char *val; /* String to append to it */ GNode *ctxt; /* Context in which this should occur */ { register Var *v; v = VarFind (name, ctxt, (ctxt == VAR_GLOBAL) ? FIND_ENV : 0); if (v == (Var *) NIL) { VarAdd (name, val, ctxt); } else { Buf_AddByte(v->val, (Byte)' '); Buf_AddBytes(v->val, strlen(val), (Byte *)val); if (DEBUG(VAR)) { printf("%s:%s = %s\n", ctxt->name, name, (char *) Buf_GetAll(v->val, (int *)NULL)); } if (v->flags & VAR_FROM_ENV) { /* * If the original variable came from the environment, we * have to install it in the global context (we could place * it in the environment, but then we should provide a way to * export other variables...) */ v->flags &= ~VAR_FROM_ENV; Lst_AtFront(ctxt->context, (ClientData)v); } } } /*- *----------------------------------------------------------------------- * Var_Exists -- * See if the given variable exists. * * Results: * TRUE if it does, FALSE if it doesn't * * Side Effects: * None. * *----------------------------------------------------------------------- */ Boolean Var_Exists(name, ctxt) char *name; /* Variable to find */ GNode *ctxt; /* Context in which to start search */ { Var *v; v = VarFind(name, ctxt, FIND_CMD|FIND_GLOBAL|FIND_ENV); if (v == (Var *)NIL) { return(FALSE); } else if (v->flags & VAR_FROM_ENV) { free(v->name); Buf_Destroy(v->val, TRUE); free((char *)v); } return(TRUE); } /*- *----------------------------------------------------------------------- * Var_Value -- * Return the value of the named variable in the given context * * Results: * The value if the variable exists, NULL if it doesn't * * Side Effects: * None *----------------------------------------------------------------------- */ char * Var_Value (name, ctxt, frp) char *name; /* name to find */ GNode *ctxt; /* context in which to search for it */ char **frp; { Var *v; v = VarFind (name, ctxt, FIND_ENV | FIND_GLOBAL | FIND_CMD); *frp = NULL; if (v != (Var *) NIL) { char *p = ((char *)Buf_GetAll(v->val, (int *)NULL)); if (v->flags & VAR_FROM_ENV) { Buf_Destroy(v->val, FALSE); free((Address) v); *frp = p; } return p; } else { return ((char *) NULL); } } /*- *----------------------------------------------------------------------- * VarHead -- * Remove the tail of the given word and place the result in the given * buffer. * * Results: * TRUE if characters were added to the buffer (a space needs to be * added to the buffer before the next word). * * Side Effects: * The trimmed word is added to the buffer. * *----------------------------------------------------------------------- */ static Boolean VarHead (word, addSpace, buf, dummy) char *word; /* Word to trim */ Boolean addSpace; /* True if need to add a space to the buffer * before sticking in the head */ Buffer buf; /* Buffer in which to store it */ ClientData dummy; { register char *slash; slash = strrchr (word, '/'); if (slash != (char *)NULL) { if (addSpace) { Buf_AddByte (buf, (Byte)' '); } *slash = '\0'; Buf_AddBytes (buf, strlen (word), (Byte *)word); *slash = '/'; return (TRUE); } else { /* * If no directory part, give . (q.v. the POSIX standard) */ if (addSpace) { Buf_AddBytes(buf, 2, (Byte *)" ."); } else { Buf_AddByte(buf, (Byte)'.'); } } return(dummy ? TRUE : TRUE); } /*- *----------------------------------------------------------------------- * VarTail -- * Remove the head of the given word and place the result in the given * buffer. * * Results: * TRUE if characters were added to the buffer (a space needs to be * added to the buffer before the next word). * * Side Effects: * The trimmed word is added to the buffer. * *----------------------------------------------------------------------- */ static Boolean VarTail (word, addSpace, buf, dummy) char *word; /* Word to trim */ Boolean addSpace; /* TRUE if need to stick a space in the * buffer before adding the tail */ Buffer buf; /* Buffer in which to store it */ ClientData dummy; { register char *slash; if (addSpace) { Buf_AddByte (buf, (Byte)' '); } slash = strrchr (word, '/'); if (slash != (char *)NULL) { *slash++ = '\0'; Buf_AddBytes (buf, strlen(slash), (Byte *)slash); slash[-1] = '/'; } else { Buf_AddBytes (buf, strlen(word), (Byte *)word); } return (dummy ? TRUE : TRUE); } /*- *----------------------------------------------------------------------- * VarSuffix -- * Place the suffix of the given word in the given buffer. * * Results: * TRUE if characters were added to the buffer (a space needs to be * added to the buffer before the next word). * * Side Effects: * The suffix from the word is placed in the buffer. * *----------------------------------------------------------------------- */ static Boolean VarSuffix (word, addSpace, buf, dummy) char *word; /* Word to trim */ Boolean addSpace; /* TRUE if need to add a space before placing * the suffix in the buffer */ Buffer buf; /* Buffer in which to store it */ ClientData dummy; { register char *dot; dot = strrchr (word, '.'); if (dot != (char *)NULL) { if (addSpace) { Buf_AddByte (buf, (Byte)' '); } *dot++ = '\0'; Buf_AddBytes (buf, strlen (dot), (Byte *)dot); dot[-1] = '.'; addSpace = TRUE; } return (dummy ? addSpace : addSpace); } /*- *----------------------------------------------------------------------- * VarRoot -- * Remove the suffix of the given word and place the result in the * buffer. * * Results: * TRUE if characters were added to the buffer (a space needs to be * added to the buffer before the next word). * * Side Effects: * The trimmed word is added to the buffer. * *----------------------------------------------------------------------- */ static Boolean VarRoot (word, addSpace, buf, dummy) char *word; /* Word to trim */ Boolean addSpace; /* TRUE if need to add a space to the buffer * before placing the root in it */ Buffer buf; /* Buffer in which to store it */ ClientData dummy; { register char *dot; if (addSpace) { Buf_AddByte (buf, (Byte)' '); } dot = strrchr (word, '.'); if (dot != (char *)NULL) { *dot = '\0'; Buf_AddBytes (buf, strlen (word), (Byte *)word); *dot = '.'; } else { Buf_AddBytes (buf, strlen(word), (Byte *)word); } return (dummy ? TRUE : TRUE); } /*- *----------------------------------------------------------------------- * VarMatch -- * Place the word in the buffer if it matches the given pattern. * Callback function for VarModify to implement the :M modifier. * * Results: * TRUE if a space should be placed in the buffer before the next * word. * * Side Effects: * The word may be copied to the buffer. * *----------------------------------------------------------------------- */ static Boolean VarMatch (word, addSpace, buf, pattern) char *word; /* Word to examine */ Boolean addSpace; /* TRUE if need to add a space to the * buffer before adding the word, if it * matches */ Buffer buf; /* Buffer in which to store it */ ClientData pattern; /* Pattern the word must match */ { if (Str_Match(word, (char *) pattern)) { if (addSpace) { Buf_AddByte(buf, (Byte)' '); } addSpace = TRUE; Buf_AddBytes(buf, strlen(word), (Byte *)word); } return(addSpace); } #ifdef SYSVVARSUB /*- *----------------------------------------------------------------------- * VarSYSVMatch -- * Place the word in the buffer if it matches the given pattern. * Callback function for VarModify to implement the System V % * modifiers. * * Results: * TRUE if a space should be placed in the buffer before the next * word. * * Side Effects: * The word may be copied to the buffer. * *----------------------------------------------------------------------- */ static Boolean VarSYSVMatch (word, addSpace, buf, patp) char *word; /* Word to examine */ Boolean addSpace; /* TRUE if need to add a space to the * buffer before adding the word, if it * matches */ Buffer buf; /* Buffer in which to store it */ ClientData patp; /* Pattern the word must match */ { int len; char *ptr; VarPattern *pat = (VarPattern *) patp; if (addSpace) Buf_AddByte(buf, (Byte)' '); addSpace = TRUE; if ((ptr = Str_SYSVMatch(word, pat->lhs, &len)) != NULL) Str_SYSVSubst(buf, pat->rhs, ptr, len); else Buf_AddBytes(buf, strlen(word), (Byte *) word); return(addSpace); } #endif /*- *----------------------------------------------------------------------- * VarNoMatch -- * Place the word in the buffer if it doesn't match the given pattern. * Callback function for VarModify to implement the :N modifier. * * Results: * TRUE if a space should be placed in the buffer before the next * word. * * Side Effects: * The word may be copied to the buffer. * *----------------------------------------------------------------------- */ static Boolean VarNoMatch (word, addSpace, buf, pattern) char *word; /* Word to examine */ Boolean addSpace; /* TRUE if need to add a space to the * buffer before adding the word, if it * matches */ Buffer buf; /* Buffer in which to store it */ ClientData pattern; /* Pattern the word must match */ { if (!Str_Match(word, (char *) pattern)) { if (addSpace) { Buf_AddByte(buf, (Byte)' '); } addSpace = TRUE; Buf_AddBytes(buf, strlen(word), (Byte *)word); } return(addSpace); } /*- *----------------------------------------------------------------------- * VarSubstitute -- * Perform a string-substitution on the given word, placing the * result in the passed buffer. * * Results: * TRUE if a space is needed before more characters are added. * * Side Effects: * None. * *----------------------------------------------------------------------- */ static Boolean VarSubstitute (word, addSpace, buf, patternp) char *word; /* Word to modify */ Boolean addSpace; /* True if space should be added before * other characters */ Buffer buf; /* Buffer for result */ ClientData patternp; /* Pattern for substitution */ { register int wordLen; /* Length of word */ register char *cp; /* General pointer */ VarPattern *pattern = (VarPattern *) patternp; wordLen = strlen(word); if (1) { /* substitute in each word of the variable */ /* * Break substitution down into simple anchored cases * and if none of them fits, perform the general substitution case. */ if ((pattern->flags & VAR_MATCH_START) && (strncmp(word, pattern->lhs, pattern->leftLen) == 0)) { /* * Anchored at start and beginning of word matches pattern */ if ((pattern->flags & VAR_MATCH_END) && (wordLen == pattern->leftLen)) { /* * Also anchored at end and matches to the end (word * is same length as pattern) add space and rhs only * if rhs is non-null. */ if (pattern->rightLen != 0) { if (addSpace) { Buf_AddByte(buf, (Byte)' '); } addSpace = TRUE; Buf_AddBytes(buf, pattern->rightLen, (Byte *)pattern->rhs); } } else if (pattern->flags & VAR_MATCH_END) { /* * Doesn't match to end -- copy word wholesale */ goto nosub; } else { /* * Matches at start but need to copy in trailing characters */ if ((pattern->rightLen + wordLen - pattern->leftLen) != 0){ if (addSpace) { Buf_AddByte(buf, (Byte)' '); } addSpace = TRUE; } Buf_AddBytes(buf, pattern->rightLen, (Byte *)pattern->rhs); Buf_AddBytes(buf, wordLen - pattern->leftLen, (Byte *)(word + pattern->leftLen)); } } else if (pattern->flags & VAR_MATCH_START) { /* * Had to match at start of word and didn't -- copy whole word. */ goto nosub; } else if (pattern->flags & VAR_MATCH_END) { /* * Anchored at end, Find only place match could occur (leftLen * characters from the end of the word) and see if it does. Note * that because the $ will be left at the end of the lhs, we have * to use strncmp. */ cp = word + (wordLen - pattern->leftLen); if ((cp >= word) && (strncmp(cp, pattern->lhs, pattern->leftLen) == 0)) { /* * Match found. If we will place characters in the buffer, * add a space before hand as indicated by addSpace, then * stuff in the initial, unmatched part of the word followed * by the right-hand-side. */ if (((cp - word) + pattern->rightLen) != 0) { if (addSpace) { Buf_AddByte(buf, (Byte)' '); } addSpace = TRUE; } Buf_AddBytes(buf, cp - word, (Byte *)word); Buf_AddBytes(buf, pattern->rightLen, (Byte *)pattern->rhs); } else { /* * Had to match at end and didn't. Copy entire word. */ goto nosub; } } else { /* * Pattern is unanchored: search for the pattern in the word using * String_FindSubstring, copying unmatched portions and the * right-hand-side for each match found, handling non-global * substitutions correctly, etc. When the loop is done, any * remaining part of the word (word and wordLen are adjusted * accordingly through the loop) is copied straight into the * buffer. * addSpace is set FALSE as soon as a space is added to the * buffer. */ register Boolean done; int origSize; done = FALSE; origSize = Buf_Size(buf); while (!done) { cp = Str_FindSubstring(word, pattern->lhs); if (cp != (char *)NULL) { if (addSpace && (((cp - word) + pattern->rightLen) != 0)){ Buf_AddByte(buf, (Byte)' '); addSpace = FALSE; } Buf_AddBytes(buf, cp-word, (Byte *)word); Buf_AddBytes(buf, pattern->rightLen, (Byte *)pattern->rhs); wordLen -= (cp - word) + pattern->leftLen; word = cp + pattern->leftLen; if (wordLen == 0 || (pattern->flags & VAR_SUB_GLOBAL) == 0){ done = TRUE; } } else { done = TRUE; } } if (wordLen != 0) { if (addSpace) { Buf_AddByte(buf, (Byte)' '); } Buf_AddBytes(buf, wordLen, (Byte *)word); } /* * If added characters to the buffer, need to add a space * before we add any more. If we didn't add any, just return * the previous value of addSpace. */ return ((Buf_Size(buf) != origSize) || addSpace); } /* * Common code for anchored substitutions: * addSpace was set TRUE if characters were added to the buffer. */ return (addSpace); } nosub: if (addSpace) { Buf_AddByte(buf, (Byte)' '); } Buf_AddBytes(buf, wordLen, (Byte *)word); return(TRUE); } /*- *----------------------------------------------------------------------- * VarModify -- * Modify each of the words of the passed string using the given * function. Used to implement all modifiers. * * Results: * A string of all the words modified appropriately. * * Side Effects: * None. * *----------------------------------------------------------------------- */ static char * VarModify (str, modProc, datum) char *str; /* String whose words should be trimmed */ /* Function to use to modify them */ Boolean (*modProc) __P((char *, Boolean, Buffer, ClientData)); ClientData datum; /* Datum to pass it */ { Buffer buf; /* Buffer for the new string */ Boolean addSpace; /* TRUE if need to add a space to the * buffer before adding the trimmed * word */ char **av; /* word list [first word does not count] */ int ac, i; buf = Buf_Init (0); addSpace = FALSE; av = brk_string(str, &ac, FALSE); for (i = 1; i < ac; i++) addSpace = (*modProc)(av[i], addSpace, buf, datum); Buf_AddByte (buf, '\0'); str = (char *)Buf_GetAll (buf, (int *)NULL); Buf_Destroy (buf, FALSE); return (str); } /*- *----------------------------------------------------------------------- * VarQuote -- * Quote shell meta-characters in the string * * Results: * The quoted string * * Side Effects: * None. * *----------------------------------------------------------------------- */ static char * VarQuote(str) char *str; { Buffer buf; /* This should cover most shells :-( */ static char meta[] = "\n \t'`\";&<>()|*?{}[]\\$!#^~"; buf = Buf_Init (MAKE_BSIZE); for (; *str; str++) { if (strchr(meta, *str) != NULL) Buf_AddByte(buf, (Byte)'\\'); Buf_AddByte(buf, (Byte)*str); } Buf_AddByte(buf, (Byte) '\0'); str = (char *)Buf_GetAll (buf, (int *)NULL); Buf_Destroy (buf, FALSE); return str; } /*- *----------------------------------------------------------------------- * Var_Parse -- * Given the start of a variable invocation, extract the variable * name and find its value, then modify it according to the * specification. * * Results: * The (possibly-modified) value of the variable or var_Error if the * specification is invalid. The length of the specification is * placed in *lengthPtr (for invalid specifications, this is just * 2...?). * A Boolean in *freePtr telling whether the returned string should * be freed by the caller. * * Side Effects: * None. * *----------------------------------------------------------------------- */ char * Var_Parse (str, ctxt, err, lengthPtr, freePtr) char *str; /* The string to parse */ GNode *ctxt; /* The context for the variable */ Boolean err; /* TRUE if undefined variables are an error */ int *lengthPtr; /* OUT: The length of the specification */ Boolean *freePtr; /* OUT: TRUE if caller should free result */ { register char *tstr; /* Pointer into str */ Var *v; /* Variable in invocation */ register char *cp; /* Secondary pointer into str (place marker * for tstr) */ Boolean haveModifier;/* TRUE if have modifiers for the variable */ register char endc; /* Ending character when variable in parens * or braces */ register char startc=0; /* Starting character when variable in parens * or braces */ int cnt; /* Used to count brace pairs when variable in * in parens or braces */ char *start; Boolean dynamic; /* TRUE if the variable is local and we're * expanding it in a non-local context. This * is done to support dynamic sources. The * result is just the invocation, unaltered */ int vlen; /* length of variable name, after embedded variable * expansion */ *freePtr = FALSE; dynamic = FALSE; start = str; if (str[1] != '(' && str[1] != '{') { /* * If it's not bounded by braces of some sort, life is much simpler. * We just need to check for the first character and return the * value if it exists. */ char name[2]; name[0] = str[1]; name[1] = '\0'; v = VarFind (name, ctxt, FIND_ENV | FIND_GLOBAL | FIND_CMD); if (v == (Var *)NIL) { *lengthPtr = 2; if ((ctxt == VAR_CMD) || (ctxt == VAR_GLOBAL)) { /* * If substituting a local variable in a non-local context, * assume it's for dynamic source stuff. We have to handle * this specially and return the longhand for the variable * with the dollar sign escaped so it makes it back to the * caller. Only four of the local variables are treated * specially as they are the only four that will be set * when dynamic sources are expanded. */ switch (str[1]) { case '@': return("$(.TARGET)"); case '%': return("$(.ARCHIVE)"); case '*': return("$(.PREFIX)"); case '!': return("$(.MEMBER)"); } } /* * Error */ return (err ? var_Error : varNoError); } else { haveModifier = FALSE; tstr = &str[1]; endc = str[1]; } } else { /* build up expanded variable name in this buffer */ Buffer buf = Buf_Init(MAKE_BSIZE); startc = str[1]; endc = startc == '(' ? ')' : '}'; /* * Skip to the end character or a colon, whichever comes first, * replacing embedded variables as we go. */ for (tstr = str + 2; *tstr != '\0' && *tstr != endc && *tstr != ':'; tstr++) if (*tstr == '$') { int rlen; Boolean rfree; char* rval = Var_Parse(tstr, ctxt, err, &rlen, &rfree); if (rval == var_Error) { Fatal("Error expanding embedded variable."); } else if (rval != NULL) { Buf_AddBytes(buf, strlen(rval), (Byte *) rval); if (rfree) free(rval); } tstr += rlen - 1; } else Buf_AddByte(buf, (Byte) *tstr); if (*tstr == '\0') { /* * If we never did find the end character, return NULL * right now, setting the length to be the distance to * the end of the string, since that's what make does. */ *lengthPtr = tstr - str; return (var_Error); } haveModifier = (*tstr == ':'); *tstr = '\0'; Buf_AddByte(buf, (Byte) '\0'); str = Buf_GetAll(buf, NULL); vlen = strlen(str); v = VarFind (str, ctxt, FIND_ENV | FIND_GLOBAL | FIND_CMD); if ((v == (Var *)NIL) && (ctxt != VAR_CMD) && (ctxt != VAR_GLOBAL) && (vlen == 2) && (str[1] == 'F' || str[1] == 'D')) { /* * Check for bogus D and F forms of local variables since we're * in a local context and the name is the right length. */ switch(str[0]) { case '@': case '%': case '*': case '!': case '>': case '<': { char vname[2]; char *val; /* * Well, it's local -- go look for it. */ vname[0] = str[0]; vname[1] = '\0'; v = VarFind(vname, ctxt, 0); if (v != (Var *)NIL) { /* * No need for nested expansion or anything, as we're * the only one who sets these things and we sure don't * put nested invocations in them... */ val = (char *)Buf_GetAll(v->val, (int *)NULL); if (str[1] == 'D') { val = VarModify(val, VarHead, (ClientData)0); } else { val = VarModify(val, VarTail, (ClientData)0); } /* * Resulting string is dynamically allocated, so * tell caller to free it. */ *freePtr = TRUE; *lengthPtr = tstr-start+1; *tstr = endc; Buf_Destroy(buf, TRUE); return(val); } break; } } } if (v == (Var *)NIL) { if (((vlen == 1) || (((vlen == 2) && (str[1] == 'F' || str[1] == 'D')))) && ((ctxt == VAR_CMD) || (ctxt == VAR_GLOBAL))) { /* * If substituting a local variable in a non-local context, * assume it's for dynamic source stuff. We have to handle * this specially and return the longhand for the variable * with the dollar sign escaped so it makes it back to the * caller. Only four of the local variables are treated * specially as they are the only four that will be set * when dynamic sources are expanded. */ switch (str[0]) { case '@': case '%': case '*': case '!': dynamic = TRUE; break; } } else if ((vlen > 2) && (str[0] == '.') && isupper((unsigned char) str[1]) && ((ctxt == VAR_CMD) || (ctxt == VAR_GLOBAL))) { int len; len = vlen - 1; if ((strncmp(str, ".TARGET", len) == 0) || (strncmp(str, ".ARCHIVE", len) == 0) || (strncmp(str, ".PREFIX", len) == 0) || (strncmp(str, ".MEMBER", len) == 0)) { dynamic = TRUE; } } if (!haveModifier) { /* * No modifiers -- have specification length so we can return * now. */ *lengthPtr = tstr - start + 1; *tstr = endc; if (dynamic) { str = emalloc(*lengthPtr + 1); strncpy(str, start, *lengthPtr); str[*lengthPtr] = '\0'; *freePtr = TRUE; Buf_Destroy(buf, TRUE); return(str); } else { Buf_Destroy(buf, TRUE); return (err ? var_Error : varNoError); } } else { /* * Still need to get to the end of the variable specification, * so kludge up a Var structure for the modifications */ v = (Var *) emalloc(sizeof(Var)); v->name = &str[1]; v->val = Buf_Init(1); v->flags = VAR_JUNK; } } Buf_Destroy(buf, TRUE); } if (v->flags & VAR_IN_USE) { Fatal("Variable %s is recursive.", v->name); /*NOTREACHED*/ } else { v->flags |= VAR_IN_USE; } /* * Before doing any modification, we have to make sure the value * has been fully expanded. If it looks like recursion might be * necessary (there's a dollar sign somewhere in the variable's value) * we just call Var_Subst to do any other substitutions that are * necessary. Note that the value returned by Var_Subst will have * been dynamically-allocated, so it will need freeing when we * return. */ str = (char *)Buf_GetAll(v->val, (int *)NULL); if (strchr (str, '$') != (char *)NULL) { str = Var_Subst(NULL, str, ctxt, err); *freePtr = TRUE; } v->flags &= ~VAR_IN_USE; /* * Now we need to apply any modifiers the user wants applied. * These are: * :M words which match the given . * is of the standard file * wildcarding form. * :S[g] * Substitute for in the value * :H Substitute the head of each word * :T Substitute the tail of each word * :E Substitute the extension (minus '.') of * each word * :R Substitute the root of each word * (pathname minus the suffix). * :lhs=rhs Like :S, but the rhs goes to the end of * the invocation. */ if ((str != (char *)NULL) && haveModifier) { /* * Skip initial colon while putting it back. */ *tstr++ = ':'; while (*tstr != endc) { char *newStr; /* New value to return */ char termc; /* Character which terminated scan */ if (DEBUG(VAR)) { printf("Applying :%c to \"%s\"\n", *tstr, str); } switch (*tstr) { case 'N': case 'M': { char *pattern; char *cp2; Boolean copy; copy = FALSE; for (cp = tstr + 1; *cp != '\0' && *cp != ':' && *cp != endc; cp++) { if (*cp == '\\' && (cp[1] == ':' || cp[1] == endc)){ copy = TRUE; cp++; } } termc = *cp; *cp = '\0'; if (copy) { /* * Need to compress the \:'s out of the pattern, so * allocate enough room to hold the uncompressed * pattern (note that cp started at tstr+1, so * cp - tstr takes the null byte into account) and * compress the pattern into the space. */ pattern = emalloc(cp - tstr); for (cp2 = pattern, cp = tstr + 1; *cp != '\0'; cp++, cp2++) { if ((*cp == '\\') && (cp[1] == ':' || cp[1] == endc)) { cp++; } *cp2 = *cp; } *cp2 = '\0'; } else { pattern = &tstr[1]; } if (*tstr == 'M' || *tstr == 'm') { newStr = VarModify(str, VarMatch, (ClientData)pattern); } else { newStr = VarModify(str, VarNoMatch, (ClientData)pattern); } if (copy) { free(pattern); } break; } case 'S': { VarPattern pattern; register char delim; Buffer buf; /* Buffer for patterns */ pattern.flags = 0; delim = tstr[1]; tstr += 2; /* * If pattern begins with '^', it is anchored to the * start of the word -- skip over it and flag pattern. */ if (*tstr == '^') { pattern.flags |= VAR_MATCH_START; tstr += 1; } buf = Buf_Init(0); /* * Pass through the lhs looking for 1) escaped delimiters, * '$'s and backslashes (place the escaped character in * uninterpreted) and 2) unescaped $'s that aren't before * the delimiter (expand the variable substitution). * The result is left in the Buffer buf. */ for (cp = tstr; *cp != '\0' && *cp != delim; cp++) { if ((*cp == '\\') && ((cp[1] == delim) || (cp[1] == '$') || (cp[1] == '\\'))) { Buf_AddByte(buf, (Byte)cp[1]); cp++; } else if (*cp == '$') { if (cp[1] != delim) { /* * If unescaped dollar sign not before the * delimiter, assume it's a variable * substitution and recurse. */ char *cp2; int len; Boolean freeIt; cp2 = Var_Parse(cp, ctxt, err, &len, &freeIt); Buf_AddBytes(buf, strlen(cp2), (Byte *)cp2); if (freeIt) { free(cp2); } cp += len - 1; } else { /* * Unescaped $ at end of pattern => anchor * pattern at end. */ pattern.flags |= VAR_MATCH_END; } } else { Buf_AddByte(buf, (Byte)*cp); } } Buf_AddByte(buf, (Byte)'\0'); /* * If lhs didn't end with the delimiter, complain and * return NULL */ if (*cp != delim) { *lengthPtr = cp - start + 1; if (*freePtr) { free(str); } Buf_Destroy(buf, TRUE); Error("Unclosed substitution for %s (%c missing)", v->name, delim); return (var_Error); } /* * Fetch pattern and destroy buffer, but preserve the data * in it, since that's our lhs. Note that Buf_GetAll * will return the actual number of bytes, which includes * the null byte, so we have to decrement the length by * one. */ pattern.lhs = (char *)Buf_GetAll(buf, &pattern.leftLen); pattern.leftLen--; Buf_Destroy(buf, FALSE); /* * Now comes the replacement string. Three things need to * be done here: 1) need to compress escaped delimiters and * ampersands and 2) need to replace unescaped ampersands * with the l.h.s. (since this isn't regexp, we can do * it right here) and 3) expand any variable substitutions. */ buf = Buf_Init(0); tstr = cp + 1; for (cp = tstr; *cp != '\0' && *cp != delim; cp++) { if ((*cp == '\\') && ((cp[1] == delim) || (cp[1] == '&') || (cp[1] == '\\') || (cp[1] == '$'))) { Buf_AddByte(buf, (Byte)cp[1]); cp++; } else if ((*cp == '$') && (cp[1] != delim)) { char *cp2; int len; Boolean freeIt; cp2 = Var_Parse(cp, ctxt, err, &len, &freeIt); Buf_AddBytes(buf, strlen(cp2), (Byte *)cp2); cp += len - 1; if (freeIt) { free(cp2); } } else if (*cp == '&') { Buf_AddBytes(buf, pattern.leftLen, (Byte *)pattern.lhs); } else { Buf_AddByte(buf, (Byte)*cp); } } Buf_AddByte(buf, (Byte)'\0'); /* * If didn't end in delimiter character, complain */ if (*cp != delim) { *lengthPtr = cp - start + 1; if (*freePtr) { free(str); } Buf_Destroy(buf, TRUE); Error("Unclosed substitution for %s (%c missing)", v->name, delim); return (var_Error); } pattern.rhs = (char *)Buf_GetAll(buf, &pattern.rightLen); pattern.rightLen--; Buf_Destroy(buf, FALSE); /* * Check for global substitution. If 'g' after the final * delimiter, substitution is global and is marked that * way. */ cp++; if (*cp == 'g') { pattern.flags |= VAR_SUB_GLOBAL; cp++; } termc = *cp; newStr = VarModify(str, VarSubstitute, (ClientData)&pattern); /* * Free the two strings. */ free(pattern.lhs); free(pattern.rhs); break; } case 'Q': if (tstr[1] == endc || tstr[1] == ':') { newStr = VarQuote (str); cp = tstr + 1; termc = *cp; break; } /*FALLTHRU*/ case 'T': if (tstr[1] == endc || tstr[1] == ':') { newStr = VarModify (str, VarTail, (ClientData)0); cp = tstr + 1; termc = *cp; break; } /*FALLTHRU*/ case 'H': if (tstr[1] == endc || tstr[1] == ':') { newStr = VarModify (str, VarHead, (ClientData)0); cp = tstr + 1; termc = *cp; break; } /*FALLTHRU*/ case 'E': if (tstr[1] == endc || tstr[1] == ':') { newStr = VarModify (str, VarSuffix, (ClientData)0); cp = tstr + 1; termc = *cp; break; } /*FALLTHRU*/ case 'R': if (tstr[1] == endc || tstr[1] == ':') { newStr = VarModify (str, VarRoot, (ClientData)0); cp = tstr + 1; termc = *cp; break; } /*FALLTHRU*/ #ifdef SUNSHCMD case 's': if (tstr[1] == 'h' && (tstr[2] == endc || tstr[2] == ':')) { char *err; newStr = Cmd_Exec (str, &err); if (err) Error (err, str); cp = tstr + 2; termc = *cp; break; } /*FALLTHRU*/ #endif default: { #ifdef SYSVVARSUB /* * This can either be a bogus modifier or a System-V * substitution command. */ VarPattern pattern; Boolean eqFound; pattern.flags = 0; eqFound = FALSE; /* * First we make a pass through the string trying * to verify it is a SYSV-make-style translation: * it must be: =) */ cp = tstr; cnt = 1; while (*cp != '\0' && cnt) { if (*cp == '=') { eqFound = TRUE; /* continue looking for endc */ } else if (*cp == endc) cnt--; else if (*cp == startc) cnt++; if (cnt) cp++; } if (*cp == endc && eqFound) { /* * Now we break this sucker into the lhs and * rhs. We must null terminate them of course. */ for (cp = tstr; *cp != '='; cp++) continue; pattern.lhs = tstr; pattern.leftLen = cp - tstr; *cp++ = '\0'; pattern.rhs = cp; cnt = 1; while (cnt) { if (*cp == endc) cnt--; else if (*cp == startc) cnt++; if (cnt) cp++; } pattern.rightLen = cp - pattern.rhs; *cp = '\0'; /* * SYSV modifications happen through the whole * string. Note the pattern is anchored at the end. */ newStr = VarModify(str, VarSYSVMatch, (ClientData)&pattern); /* * Restore the nulled characters */ pattern.lhs[pattern.leftLen] = '='; pattern.rhs[pattern.rightLen] = endc; termc = endc; } else #endif { Error ("Unknown modifier '%c'\n", *tstr); for (cp = tstr+1; *cp != ':' && *cp != endc && *cp != '\0'; cp++) continue; termc = *cp; newStr = var_Error; } } } if (DEBUG(VAR)) { printf("Result is \"%s\"\n", newStr); } if (*freePtr) { free (str); } str = newStr; if (str != var_Error) { *freePtr = TRUE; } else { *freePtr = FALSE; } if (termc == '\0') { Error("Unclosed variable specification for %s", v->name); } else if (termc == ':') { *cp++ = termc; } else { *cp = termc; } tstr = cp; } *lengthPtr = tstr - start + 1; } else { *lengthPtr = tstr - start + 1; *tstr = endc; } if (v->flags & VAR_FROM_ENV) { Boolean destroy = FALSE; if (str != (char *)Buf_GetAll(v->val, (int *)NULL)) { destroy = TRUE; } else { /* * Returning the value unmodified, so tell the caller to free * the thing. */ *freePtr = TRUE; } Buf_Destroy(v->val, destroy); free((Address)v); } else if (v->flags & VAR_JUNK) { /* * Perform any free'ing needed and set *freePtr to FALSE so the caller * doesn't try to free a static pointer. */ if (*freePtr) { free(str); } *freePtr = FALSE; Buf_Destroy(v->val, TRUE); free((Address)v); if (dynamic) { str = emalloc(*lengthPtr + 1); strncpy(str, start, *lengthPtr); str[*lengthPtr] = '\0'; *freePtr = TRUE; } else { str = var_Error; } } return (str); } /*- *----------------------------------------------------------------------- * Var_Subst -- * Substitute for all variables in the given string in the given context * If undefErr is TRUE, Parse_Error will be called when an undefined * variable is encountered. * * Results: * The resulting string. * * Side Effects: * None. The old string must be freed by the caller *----------------------------------------------------------------------- */ char * Var_Subst (var, str, ctxt, undefErr) char *var; /* Named variable || NULL for all */ char *str; /* the string in which to substitute */ GNode *ctxt; /* the context wherein to find variables */ Boolean undefErr; /* TRUE if undefineds are an error */ { Buffer buf; /* Buffer for forming things */ char *val; /* Value to substitute for a variable */ int length; /* Length of the variable invocation */ Boolean doFree; /* Set true if val should be freed */ static Boolean errorReported; /* Set true if an error has already * been reported to prevent a plethora * of messages when recursing */ buf = Buf_Init (MAKE_BSIZE); errorReported = FALSE; while (*str) { if (var == NULL && (*str == '$') && (str[1] == '$')) { /* * A dollar sign may be escaped either with another dollar sign. * In such a case, we skip over the escape character and store the * dollar sign into the buffer directly. */ str++; Buf_AddByte(buf, (Byte)*str); str++; } else if (*str != '$') { /* * Skip as many characters as possible -- either to the end of * the string or to the next dollar sign (variable invocation). */ char *cp; for (cp = str++; *str != '$' && *str != '\0'; str++) continue; Buf_AddBytes(buf, str - cp, (Byte *)cp); } else { if (var != NULL) { int expand; for (;;) { if (str[1] != '(' && str[1] != '{') { if (str[1] != *var) { Buf_AddBytes(buf, 2, (Byte *) str); str += 2; expand = FALSE; } else expand = TRUE; break; } else { char *p; /* * Scan up to the end of the variable name. */ for (p = &str[2]; *p && *p != ':' && *p != ')' && *p != '}'; p++) if (*p == '$') break; /* * A variable inside the variable. We cannot expand * the external variable yet, so we try again with * the nested one */ if (*p == '$') { Buf_AddBytes(buf, p - str, (Byte *) str); str = p; continue; } if (strncmp(var, str + 2, p - str - 2) != 0 || var[p - str - 2] != '\0') { /* * Not the variable we want to expand, scan * until the next variable */ for (;*p != '$' && *p != '\0'; p++) continue; Buf_AddBytes(buf, p - str, (Byte *) str); str = p; expand = FALSE; } else expand = TRUE; break; } } if (!expand) continue; } val = Var_Parse (str, ctxt, undefErr, &length, &doFree); /* * When we come down here, val should either point to the * value of this variable, suitably modified, or be NULL. * Length should be the total length of the potential * variable invocation (from $ to end character...) */ if (val == var_Error || val == varNoError) { /* * If performing old-time variable substitution, skip over * the variable and continue with the substitution. Otherwise, * store the dollar sign and advance str so we continue with * the string... */ if (oldVars) { str += length; } else if (undefErr) { /* * If variable is undefined, complain and skip the * variable. The complaint will stop us from doing anything * when the file is parsed. */ if (!errorReported) { Parse_Error (PARSE_FATAL, "Undefined variable \"%.*s\"",length,str); } str += length; errorReported = TRUE; } else { Buf_AddByte (buf, (Byte)*str); str += 1; } } else { /* * We've now got a variable structure to store in. But first, * advance the string pointer. */ str += length; /* * Copy all the characters from the variable value straight * into the new string. */ Buf_AddBytes (buf, strlen (val), (Byte *)val); if (doFree) { free ((Address)val); } } } } Buf_AddByte (buf, '\0'); str = (char *)Buf_GetAll (buf, (int *)NULL); Buf_Destroy (buf, FALSE); return (str); } /*- *----------------------------------------------------------------------- * Var_GetTail -- * Return the tail from each of a list of words. Used to set the * System V local variables. * * Results: * The resulting string. * * Side Effects: * None. * *----------------------------------------------------------------------- */ char * Var_GetTail(file) char *file; /* Filename to modify */ { return(VarModify(file, VarTail, (ClientData)0)); } /*- *----------------------------------------------------------------------- * Var_GetHead -- * Find the leading components of a (list of) filename(s). * XXX: VarHead does not replace foo by ., as (sun) System V make * does. * * Results: * The leading components. * * Side Effects: * None. * *----------------------------------------------------------------------- */ char * Var_GetHead(file) char *file; /* Filename to manipulate */ { return(VarModify(file, VarHead, (ClientData)0)); } /*- *----------------------------------------------------------------------- * Var_Init -- * Initialize the module * * Results: * None * * Side Effects: * The VAR_CMD and VAR_GLOBAL contexts are created *----------------------------------------------------------------------- */ void Var_Init () { VAR_GLOBAL = Targ_NewGN ("Global"); VAR_CMD = Targ_NewGN ("Command"); allVars = Lst_Init(FALSE); } void Var_End () { Lst_Destroy(allVars, VarDelete); } /****************** PRINT DEBUGGING INFO *****************/ static int VarPrintVar (vp, dummy) ClientData vp; ClientData dummy; { Var *v = (Var *) vp; printf ("%-16s = %s\n", v->name, (char *) Buf_GetAll(v->val, (int *)NULL)); return (dummy ? 0 : 0); } /*- *----------------------------------------------------------------------- * Var_Dump -- * print all variables in a context *----------------------------------------------------------------------- */ void Var_Dump (ctxt) GNode *ctxt; { Lst_ForEach (ctxt->context, VarPrintVar, (ClientData) 0); }