2 * Copyright (c) 1988, 1989, 1990, 1993
3 * The Regents of the University of California. All rights reserved.
4 * Copyright (c) 1989 by Berkeley Softworks
7 * This code is derived from software contributed to Berkeley by
10 * Redistribution and use in source and binary forms, with or without
11 * modification, are permitted provided that the following conditions
13 * 1. Redistributions of source code must retain the above copyright
14 * notice, this list of conditions and the following disclaimer.
15 * 2. Redistributions in binary form must reproduce the above copyright
16 * notice, this list of conditions and the following disclaimer in the
17 * documentation and/or other materials provided with the distribution.
18 * 3. All advertising materials mentioning features or use of this software
19 * must display the following acknowledgement:
20 * This product includes software developed by the University of
21 * California, Berkeley and its contributors.
22 * 4. Neither the name of the University nor the names of its contributors
23 * may be used to endorse or promote products derived from this software
24 * without specific prior written permission.
26 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
27 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
28 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
29 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
30 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
31 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
32 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
33 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
34 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
35 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
38 * @(#)main.c 8.3 (Berkeley) 3/19/94
43 static char copyright[] =
44 "@(#) Copyright (c) 1988, 1989, 1990, 1993\n\
45 The Regents of the University of California. All rights reserved.\n";
48 #include <sys/cdefs.h>
49 __FBSDID("$FreeBSD$");
53 * The main file for this entire program. Exit routines etc
56 * Utility functions defined in this file:
58 * Takes a line of arguments, breaks them and
59 * treats them as if they were given when first
60 * invoked. Used by the parse module to implement
64 #include <sys/param.h>
66 #include <sys/sysctl.h>
68 #include <sys/queue.h>
69 #include <sys/resource.h>
70 #include <sys/utsname.h>
87 #include "pathnames.h"
95 extern char **environ; /* XXX what header declares this variable? */
97 #define WANT_ENV_MKLVL 1
98 #define MKLVL_MAXVAL 500
99 #define MKLVL_ENVVAR "__MKLVL__"
101 /* ordered list of makefiles to read */
102 static Lst makefiles = Lst_Initializer(makefiles);
104 /* ordered list of source makefiles */
105 static Lst source_makefiles = Lst_Initializer(source_makefiles);
107 /* list of variables to print */
108 static Lst variables = Lst_Initializer(variables);
110 static Boolean expandVars; /* fully expand printed variables */
111 static Boolean noBuiltins; /* -r flag */
112 static Boolean forceJobs; /* -j argument given */
113 static char *curdir; /* startup directory */
114 static char *objdir; /* where we chdir'ed to */
115 static char **save_argv; /* saved argv */
116 static char *save_makeflags;/* saved MAKEFLAGS */
118 /* (-E) vars to override from env */
119 Lst envFirstVars = Lst_Initializer(envFirstVars);
121 /* Targets to be made */
122 Lst create = Lst_Initializer(create);
124 Boolean allPrecious; /* .PRECIOUS given on line by itself */
125 Boolean is_posix; /* .POSIX target seen */
126 Boolean mfAutoDeps; /* .MAKEFILEDEPS target seen */
127 Boolean beSilent; /* -s flag */
128 Boolean beVerbose; /* -v flag */
129 Boolean beQuiet; /* -Q flag */
130 Boolean compatMake; /* -B argument */
131 int debug; /* -d flag */
132 Boolean ignoreErrors; /* -i flag */
133 int jobLimit; /* -j argument */
134 int makeErrors; /* Number of targets not remade due to errors */
135 Boolean jobsRunning; /* TRUE if the jobs might be running */
136 Boolean keepgoing; /* -k flag */
137 Boolean noExecute; /* -n flag */
138 Boolean printGraphOnly; /* -p flag */
139 Boolean queryFlag; /* -q flag */
140 Boolean touchFlag; /* -t flag */
141 Boolean usePipes; /* !-P flag */
142 uint32_t warn_cmd; /* command line warning flags */
143 uint32_t warn_flags; /* actual warning flags */
144 uint32_t warn_nocmd; /* command line no-warning flags */
146 time_t now; /* Time at start of make */
147 struct GNode *DEFAULT; /* .DEFAULT node */
150 * Exit with usage message.
156 "usage: make [-BPSXeiknpqrstv] [-C directory] [-D variable]\n"
157 "\t[-d flags] [-E variable] [-f makefile] [-I directory]\n"
158 "\t[-j max_jobs] [-m directory] [-V variable]\n"
159 "\t[variable=value] [target ...]\n");
165 * Append a flag with an optional argument to MAKEFLAGS and MFLAGS
168 MFLAGS_append(const char *flag, char *arg)
172 Var_Append(".MAKEFLAGS", flag, VAR_GLOBAL);
174 str = MAKEFLAGS_quote(arg);
175 Var_Append(".MAKEFLAGS", str, VAR_GLOBAL);
179 Var_Append("MFLAGS", flag, VAR_GLOBAL);
181 str = MAKEFLAGS_quote(arg);
182 Var_Append("MFLAGS", str, VAR_GLOBAL);
190 * Handle argument to warning option.
193 Main_ParseWarn(const char *arg, int iscmd)
197 static const struct {
201 { "dirsyntax", WARN_DIRSYNTAX },
206 if (arg[0] == 'n' && arg[1] == 'o') {
211 for (i = 0; options[i].option != NULL; i++)
212 if (strcmp(arg, options[i].option) == 0)
215 if (options[i].option == NULL)
221 warn_cmd |= options[i].flag;
222 warn_nocmd &= ~options[i].flag;
223 warn_flags |= options[i].flag;
225 warn_nocmd |= options[i].flag;
226 warn_cmd &= ~options[i].flag;
227 warn_flags &= ~options[i].flag;
231 warn_flags |= (options[i].flag & ~warn_nocmd);
233 warn_flags &= ~(options[i].flag | warn_cmd);
240 * Open and parse the given makefile.
243 * TRUE if ok. FALSE if couldn't open file.
246 ReadMakefile(const char p[])
248 char *fname, *fnamesave; /* makefile to read */
250 char *name, path[MAXPATHLEN];
254 /* XXX - remove this once constification is done */
255 fnamesave = fname = estrdup(p);
257 if (!strcmp(fname, "-")) {
258 Parse_File("(stdin)", stdin);
259 Var_SetGlobal("MAKEFILE", "");
261 setMAKEFILE = strcmp(fname, ".depend");
263 /* if we've chdir'd, rebuild the path name */
264 if (curdir != objdir && *fname != '/') {
265 snprintf(path, MAXPATHLEN, "%s/%s", curdir, fname);
267 * XXX The realpath stuff breaks relative includes
268 * XXX in some cases. The problem likely is in
269 * XXX parse.c where it does special things in
270 * XXX ParseDoInclude if the file is relateive
271 * XXX or absolute and not a system file. There
272 * XXX it assumes that if the current file that's
273 * XXX being included is absolute, that any files
274 * XXX that it includes shouldn't do the -I path
275 * XXX stuff, which is inconsistant with historical
276 * XXX behavior. However, I can't pentrate the mists
277 * XXX further, so I'm putting this workaround in
278 * XXX here until such time as the underlying bug
281 #if THIS_BREAKS_THINGS
282 if (realpath(path, path) != NULL &&
283 (stream = fopen(path, "r")) != NULL) {
288 } else if (realpath(fname, path) != NULL) {
291 if ((stream = fopen(fname, "r")) != NULL)
295 if ((stream = fopen(path, "r")) != NULL) {
302 if ((stream = fopen(fname, "r")) != NULL)
306 /* look in -I and system include directories. */
307 name = Path_FindFile(fname, &parseIncPath);
309 name = Path_FindFile(fname, &sysIncPath);
310 if (!name || !(stream = fopen(name, "r"))) {
314 MAKEFILE = fname = name;
316 * set the MAKEFILE variable desired by System V fans -- the
317 * placement of the setting here means it gets set to the last
318 * makefile specified, as it is set by SysV make.
322 Var_SetGlobal("MAKEFILE", MAKEFILE);
323 Parse_File(fname, stream);
330 * Open and parse the given makefile.
331 * If open is successful add it to the list of makefiles.
334 * TRUE if ok. FALSE if couldn't open file.
337 TryReadMakefile(const char p[])
340 LstNode *last = Lst_Last(&source_makefiles);
342 if (!ReadMakefile(p))
347 LstNode *first = Lst_First(&source_makefiles);
348 Lst_Insert(&source_makefiles, first, data);
350 Lst_Append(&source_makefiles, last, estrdup(p));
356 * Parse a given argument vector. Called from main() and from
357 * Main_ParseArgLine() when the .MAKEFLAGS target is used.
359 * XXX: Deal with command line overriding .MAKEFLAGS in makefile
362 * Various global and local flags will be set depending on the flags
366 MainParseArgs(int argc, char **argv)
369 Boolean found_dd = FALSE;
372 optind = 1; /* since we're called more than once */
374 #define OPTFLAGS "ABC:D:d:E:ef:I:ij:km:nPpQqrSstV:vXx:"
376 if ((optind < argc) && strcmp(argv[optind], "--") == 0) {
379 if ((c = getopt(argc, argv, OPTFLAGS)) == -1) {
386 MFLAGS_append("-A", NULL);
390 MFLAGS_append("-B", NULL);
391 unsetenv("MAKE_JOBS_FIFO");
394 if (chdir(optarg) == -1)
395 err(1, "chdir %s", optarg);
398 Var_SetGlobal(optarg, "1");
399 MFLAGS_append("-D", optarg);
402 char *modules = optarg;
404 for (; *modules; ++modules)
422 if (modules[1] == '1') {
423 debug |= DEBUG_GRAPH1;
426 else if (modules[1] == '2') {
427 debug |= DEBUG_GRAPH2;
450 warnx("illegal argument to d option "
454 MFLAGS_append("-d", optarg);
458 Lst_AtEnd(&envFirstVars, estrdup(optarg));
459 MFLAGS_append("-E", optarg);
462 checkEnvFirst = TRUE;
463 MFLAGS_append("-e", NULL);
466 Lst_AtEnd(&makefiles, estrdup(optarg));
469 Parse_AddIncludeDir(optarg);
470 MFLAGS_append("-I", optarg);
474 MFLAGS_append("-i", NULL);
480 jobLimit = strtol(optarg, &endptr, 10);
481 if (jobLimit <= 0 || *endptr != '\0') {
482 warnx("illegal number, -j argument -- %s",
486 MFLAGS_append("-j", optarg);
491 MFLAGS_append("-k", NULL);
494 Path_AddDir(&sysIncPath, optarg);
495 MFLAGS_append("-m", optarg);
499 MFLAGS_append("-n", NULL);
503 MFLAGS_append("-P", NULL);
506 printGraphOnly = TRUE;
507 debug |= DEBUG_GRAPH1;
512 MFLAGS_append("-Q", NULL);
516 /* Kind of nonsensical, wot? */
517 MFLAGS_append("-q", NULL);
521 MFLAGS_append("-r", NULL);
525 MFLAGS_append("-S", NULL);
529 MFLAGS_append("-s", NULL);
533 MFLAGS_append("-t", NULL);
536 Lst_AtEnd(&variables, estrdup(optarg));
537 MFLAGS_append("-V", optarg);
542 MFLAGS_append("-v", NULL);
548 if (Main_ParseWarn(optarg, 1) != -1)
549 MFLAGS_append("-x", optarg);
563 * Parse the rest of the arguments.
564 * o Check for variable assignments and perform them if so.
565 * o Check for more flags and restart getopt if so.
566 * o Anything else is taken to be a target and added
567 * to the end of the "create" list.
569 for (; *argv != NULL; ++argv, --argc) {
570 if (Parse_IsVar(*argv)) {
571 char *ptr = MAKEFLAGS_quote(*argv);
572 char *v = estrdup(*argv);
574 Var_Append(".MAKEFLAGS", ptr, VAR_GLOBAL);
575 Parse_DoVar(v, VAR_CMD);
579 } else if ((*argv)[0] == '-') {
580 if ((*argv)[1] == '\0') {
582 * (*argv) is a single dash, so we
585 } else if (found_dd) {
587 * Double dash has been found, ignore
588 * any more options. But what do we do
589 * with it? For now treat it like a target.
591 Lst_AtEnd(&create, estrdup(*argv));
594 * (*argv) is a -flag, so backup argv and
595 * argc. getopt() expects options to start
596 * in the 2nd position.
603 } else if ((*argv)[0] == '\0') {
604 Punt("illegal (null) argument.");
607 Lst_AtEnd(&create, estrdup(*argv));
614 * Used by the parse module when a .MFLAGS or .MAKEFLAGS target
615 * is encountered and by main() when reading the .MAKEFLAGS envariable.
616 * Takes a line of arguments and breaks it into its
617 * component words and passes those words and the number of them to the
618 * MainParseArgs function.
619 * The line should have all its leading whitespace removed.
622 * Only those that come from the various arguments.
625 Main_ParseArgLine(char *line, int mflags)
631 for (; *line == ' '; ++line)
637 MAKEFLAGS_break(&aa, line);
639 brk_string(&aa, line, TRUE);
641 MainParseArgs(aa.argc, aa.argv);
646 chdir_verify_path(const char *path, char *obpath)
650 if (stat(path, &sb) == 0 && S_ISDIR(sb.st_mode)) {
651 if (chdir(path) == -1 || getcwd(obpath, MAXPATHLEN) == NULL) {
652 warn("warning: %s", path);
662 * In lieu of a good way to prevent every possible looping in make(1), stop
663 * there from being more than MKLVL_MAXVAL processes forked by make(1), to
664 * prevent a forkbomb from happening, in a dumb and mechanical way.
667 * Creates or modifies enviornment variable MKLVL_ENVVAR via setenv().
670 check_make_level(void)
672 #ifdef WANT_ENV_MKLVL
673 char *value = getenv(MKLVL_ENVVAR);
674 int level = (value == NULL) ? 0 : atoi(value);
677 errc(2, EAGAIN, "Invalid value for recursion level (%d).",
679 } else if (level > MKLVL_MAXVAL) {
680 errc(2, EAGAIN, "Max recursion level (%d) exceeded.",
684 sprintf(new_value, "%d", level + 1);
685 setenv(MKLVL_ENVVAR, new_value, 1);
687 #endif /* WANT_ENV_MKLVL */
691 * Main_AddSourceMakefile
692 * Add a file to the list of source makefiles
695 Main_AddSourceMakefile(const char *name)
698 Lst_AtEnd(&source_makefiles, estrdup(name));
703 * Remake all the makefiles
706 Remake_Makefiles(void)
712 Compat_InstallSignalHandlers();
713 if (curdir != objdir) {
714 if (chdir(curdir) < 0)
715 Fatal("Failed to change directory to %s.", curdir);
718 LST_FOREACH(ln, &source_makefiles) {
721 const char *name = Lst_Datum(ln);
722 Boolean saveTouchFlag = touchFlag;
723 Boolean saveQueryFlag = queryFlag;
724 Boolean saveNoExecute = noExecute;
730 gn = Targ_FindNode(name, TARG_CREATE);
731 DEBUGF(MAKE, ("Checking %s...", gn->name));
735 * ! dependencies as well as
736 * dependencies with .FORCE, .EXEC and .PHONY attributes
737 * are skipped to prevent infinite loops
739 if (gn->type & (OP_FORCE | OP_EXEC | OP_PHONY)) {
740 DEBUGF(MAKE, ("skipping (force, exec or phony).\n",
746 * Skip :: targets that have commands and no children
747 * because such targets are always out-of-date
749 if ((gn->type & OP_DOUBLEDEP) &&
750 !Lst_IsEmpty(&gn->commands) &&
751 Lst_IsEmpty(&gn->children)) {
752 DEBUGF(MAKE, ("skipping (doubledep, no sources "
753 "and has commands).\n"));
758 * Skip targets without sources and without commands
760 if (Lst_IsEmpty(&gn->commands) &&
761 Lst_IsEmpty(&gn->children)) {
763 ("skipping (no sources and no commands).\n"));
767 DEBUGF(MAKE, ("\n"));
770 * -t, -q and -n has no effect unless the makefile is
771 * specified as one of the targets explicitly in the
774 LST_FOREACH(ln2, &create) {
775 if (!strcmp(gn->name, Lst_Datum(ln2))) {
776 /* found as a target */
787 * Check and remake the makefile
789 mtime = Dir_MTime(gn);
793 * Restore -t, -q and -n behaviour
795 touchFlag = saveTouchFlag;
796 queryFlag = saveQueryFlag;
797 noExecute = saveNoExecute;
800 * Compat_Make will leave the 'made' field of gn
801 * in one of the following states:
802 * UPTODATE gn was already up-to-date
803 * MADE gn was recreated successfully
804 * ERROR An error occurred while gn was being created
805 * ABORTED gn was not remade because one of its inferiors
806 * could not be made due to errors.
808 if (gn->made == MADE) {
809 if (mtime != Dir_MTime(gn)) {
811 ("%s updated (%d -> %d).\n",
812 gn->name, mtime, gn->mtime));
816 ("%s not updated: skipping restart.\n",
819 } else if (gn->made == ERROR)
821 else if (gn->made == ABORTED) {
822 printf("`%s' not remade because of errors.\n",
825 } else if (gn->made == UPTODATE) {
829 Lst_EnQueue(&examine, gn);
830 while (!Lst_IsEmpty(&examine)) {
832 GNode *egn = Lst_DeQueue(&examine);
835 LST_FOREACH(eln, &egn->children) {
836 GNode *cgn = Lst_Datum(eln);
838 Lst_EnQueue(&examine, cgn);
845 Fatal("Failed to remake Makefiles.");
846 if (remade_cnt > 0) {
847 DEBUGF(MAKE, ("Restarting `%s'.\n", save_argv[0]));
850 * Some of makefiles were remade -- restart from clean state
852 if (save_makeflags != NULL)
853 setenv("MAKEFLAGS", save_makeflags, 1);
855 unsetenv("MAKEFLAGS");
856 if (execvp(save_argv[0], save_argv) < 0) {
857 Fatal("Can't restart `%s': %s.",
858 save_argv[0], strerror(errno));
862 if (curdir != objdir) {
863 if (chdir(objdir) < 0)
864 Fatal("Failed to change directory to %s.", objdir);
870 * The main function, for obvious reasons. Initializes variables
871 * and a few modules, then parses the arguments give it in the
872 * environment and on the command line. Reads the system makefile
873 * followed by either Makefile, makefile or the file given by the
874 * -f argument. Sets the .MAKEFLAGS PMake variable based on all the
875 * flags it has received by then uses either the Make or the Compat
876 * module to create the initial list of targets.
879 * If -q was given, exits -1 if anything was out-of-date. Else it exits
883 * The program exits when done. Targets are created. etc. etc. etc.
886 main(int argc, char **argv)
889 const char *machine_arch;
890 const char *machine_cpu;
891 Boolean outOfDate = TRUE; /* FALSE if all targets up to date */
895 char mdpath[MAXPATHLEN];
896 char obpath[MAXPATHLEN];
897 char cdpath[MAXPATHLEN];
898 char *cp = NULL, *start;
901 save_makeflags = getenv("MAKEFLAGS");
902 if (save_makeflags != NULL)
903 save_makeflags = estrdup(save_makeflags);
906 * Initialize file global variables.
909 noBuiltins = FALSE; /* Read the built-in rules */
910 forceJobs = FALSE; /* No -j flag */
914 * Initialize program global variables.
916 beSilent = FALSE; /* Print commands as executed */
917 ignoreErrors = FALSE; /* Pay attention to non-zero returns */
918 noExecute = FALSE; /* Execute all commands */
919 printGraphOnly = FALSE; /* Don't stop after printing graph */
920 keepgoing = FALSE; /* Stop on error */
921 allPrecious = FALSE; /* Remove targets when interrupted */
922 queryFlag = FALSE; /* This is not just a check-run */
923 touchFlag = FALSE; /* Actually update targets */
924 usePipes = TRUE; /* Catch child output in pipes */
925 debug = 0; /* No debug verbosity, please. */
928 jobLimit = DEFMAXJOBS;
929 compatMake = FALSE; /* No compat mode */
935 * get rid of resource limit on file descriptors
939 if (getrlimit(RLIMIT_NOFILE, &rl) == -1) {
942 rl.rlim_cur = rl.rlim_max;
943 if (setrlimit(RLIMIT_NOFILE, &rl) == -1) {
950 * Prior to 7.0, FreeBSD/pc98 kernel used to set the
951 * utsname.machine to "i386", and MACHINE was defined as
952 * "i386", so it could not be distinguished from FreeBSD/i386.
953 * Therefore, we had to check machine.ispc98 and adjust the
954 * MACHINE variable. NOTE: The code is still here to be able
955 * to compile new make binary on old FreeBSD/pc98 systems, and
956 * have the MACHINE variable set properly.
958 if ((machine = getenv("MACHINE")) == NULL) {
962 len = sizeof(ispc98);
963 if (!sysctlbyname("machdep.ispc98", &ispc98, &len, NULL, 0)) {
970 * Get the name of this type of MACHINE from utsname
971 * so we can share an executable for similar machines.
972 * (i.e. m68k: amiga hp300, mac68k, sun3, ...)
974 * Note that both MACHINE and MACHINE_ARCH are decided at
977 if (machine == NULL) {
978 static struct utsname utsname;
980 if (uname(&utsname) == -1)
982 machine = utsname.machine;
985 if ((machine_arch = getenv("MACHINE_ARCH")) == NULL) {
987 machine_arch = MACHINE_ARCH;
989 machine_arch = "unknown";
994 * Set machine_cpu to the minumum supported CPU revision based
995 * on the target architecture, if not already set.
997 if ((machine_cpu = getenv("MACHINE_CPU")) == NULL) {
998 if (!strcmp(machine_arch, "i386"))
999 machine_cpu = "i386";
1000 else if (!strcmp(machine_arch, "alpha"))
1001 machine_cpu = "ev4";
1003 machine_cpu = "unknown";
1007 * Initialize the parsing, directory and variable modules to prepare
1008 * for the reading of inclusion paths and variable settings on the
1013 Dir_Init(); /* Initialize directory structures so -I flags
1014 * can be processed correctly */
1015 Var_Init(environ); /* As well as the lists of variables for
1016 * parsing arguments */
1019 * Initialize the Shell so that we have a shell for != assignments
1020 * on the command line.
1025 * Initialize various variables.
1026 * MAKE also gets this name, for compatibility
1027 * .MAKEFLAGS gets set to the empty string just in case.
1028 * MFLAGS also gets initialized empty, for compatibility.
1030 Var_SetGlobal("MAKE", argv[0]);
1031 Var_SetGlobal(".MAKEFLAGS", "");
1032 Var_SetGlobal("MFLAGS", "");
1033 Var_SetGlobal("MACHINE", machine);
1034 Var_SetGlobal("MACHINE_ARCH", machine_arch);
1035 Var_SetGlobal("MACHINE_CPU", machine_cpu);
1037 Var_SetGlobal("MAKE_VERSION", MAKE_VERSION);
1039 Var_SetGlobal(".newline", "\n"); /* handy for :@ loops */
1043 snprintf(tmp, sizeof(tmp), "%u", getpid());
1044 Var_SetGlobal(".MAKE.PID", tmp);
1045 snprintf(tmp, sizeof(tmp), "%u", getppid());
1046 Var_SetGlobal(".MAKE.PPID", tmp);
1051 * First snag things out of the MAKEFLAGS environment
1052 * variable. Then parse the command line arguments.
1054 Main_ParseArgLine(getenv("MAKEFLAGS"), 1);
1056 MainParseArgs(argc, argv);
1059 * Find where we are...
1061 if (getcwd(curdir, MAXPATHLEN) == NULL)
1067 if (stat(curdir, &sa) == -1)
1068 err(2, "%s", curdir);
1072 * The object directory location is determined using the
1073 * following order of preference:
1075 * 1. MAKEOBJDIRPREFIX`cwd`
1077 * 3. PATH_OBJDIR.${MACHINE}
1079 * 5. PATH_OBJDIRPREFIX`cwd`
1081 * If one of the first two fails, use the current directory.
1082 * If the remaining three all fail, use the current directory.
1084 * Once things are initted,
1085 * have to add the original directory to the search path,
1086 * and modify the paths for the Makefiles apropriately. The
1087 * current directory is also placed as a variable for make scripts.
1089 if (!(pathp = getenv("MAKEOBJDIRPREFIX"))) {
1090 if (!(path = getenv("MAKEOBJDIR"))) {
1092 pathp = PATH_OBJDIRPREFIX;
1093 snprintf(mdpath, MAXPATHLEN, "%s.%s", path, machine);
1094 if (!(objdir = chdir_verify_path(mdpath, obpath)))
1095 if (!(objdir=chdir_verify_path(path, obpath))) {
1096 snprintf(mdpath, MAXPATHLEN,
1097 "%s%s", pathp, curdir);
1098 if (!(objdir=chdir_verify_path(mdpath,
1103 else if (!(objdir = chdir_verify_path(path, obpath)))
1107 snprintf(mdpath, MAXPATHLEN, "%s%s", pathp, curdir);
1108 if (!(objdir = chdir_verify_path(mdpath, obpath)))
1111 Dir_InitDot(); /* Initialize the "." directory */
1112 if (objdir != curdir)
1113 Path_AddDir(&dirSearchPath, curdir);
1114 Var_SetGlobal(".ST_EXPORTVAR", "YES");
1115 Var_SetGlobal(".CURDIR", curdir);
1116 Var_SetGlobal(".OBJDIR", objdir);
1118 if (getenv("MAKE_JOBS_FIFO") != NULL)
1121 * Be compatible if user did not specify -j and did not explicitly
1122 * turned compatibility on
1124 if (!compatMake && !forceJobs)
1128 * Initialize target and suffix modules in preparation for
1129 * parsing the makefile(s)
1138 * Set up the .TARGETS variable to contain the list of targets to be
1139 * created. If none specified, make the variable empty -- the parser
1140 * will fill the thing in with the default or .MAIN target.
1142 if (Lst_IsEmpty(&create)) {
1143 Var_SetGlobal(".TARGETS", "");
1147 for (ln = Lst_First(&create); ln != NULL; ln = Lst_Succ(ln)) {
1148 char *name = Lst_Datum(ln);
1150 Var_Append(".TARGETS", name, VAR_GLOBAL);
1156 * If no user-supplied system path was given (through the -m option)
1157 * add the directories from the DEFSYSPATH (more than one may be given
1158 * as dir1:...:dirn) to the system include path.
1160 if (TAILQ_EMPTY(&sysIncPath)) {
1161 char syspath[] = PATH_DEFSYSPATH;
1163 for (start = syspath; *start != '\0'; start = cp) {
1164 for (cp = start; *cp != '\0' && *cp != ':'; cp++)
1167 Path_AddDir(&sysIncPath, start);
1170 Path_AddDir(&sysIncPath, start);
1176 * Read in the built-in rules first, followed by the specified
1177 * makefile, if it was (makefile != (char *) NULL), or the default
1178 * Makefile and makefile, in that order, if it wasn't.
1181 /* Path of sys.mk */
1182 Lst sysMkPath = Lst_Initializer(sysMkPath);
1184 char defsysmk[] = PATH_DEFSYSMK;
1186 Path_Expand(defsysmk, &sysIncPath, &sysMkPath);
1187 if (Lst_IsEmpty(&sysMkPath))
1188 Fatal("make: no system rules (%s).", PATH_DEFSYSMK);
1189 LST_FOREACH(ln, &sysMkPath) {
1190 if (!ReadMakefile(Lst_Datum(ln)))
1194 Fatal("make: cannot open %s.", (char *)Lst_Datum(ln));
1195 Lst_Destroy(&sysMkPath, free);
1198 if (!Lst_IsEmpty(&makefiles)) {
1201 LST_FOREACH(ln, &makefiles) {
1202 if (!TryReadMakefile(Lst_Datum(ln)))
1206 Fatal("make: cannot open %s.", (char *)Lst_Datum(ln));
1207 } else if (!TryReadMakefile("BSDmakefile"))
1208 if (!TryReadMakefile("makefile"))
1209 TryReadMakefile("Makefile");
1211 ReadMakefile(".depend");
1213 /* Install all the flags into the MAKEFLAGS envariable. */
1214 if (((p = Var_Value(".MAKEFLAGS", VAR_GLOBAL)) != NULL) && *p)
1215 setenv("MAKEFLAGS", p, 1);
1217 setenv("MAKEFLAGS", "", 1);
1220 * For compatibility, look at the directories in the VPATH variable
1221 * and add them to the search path, if the variable is defined. The
1222 * variable's value is in the same format as the PATH envariable, i.e.
1223 * <directory>:<directory>:<directory>...
1225 if (Var_Exists("VPATH", VAR_CMD)) {
1227 * GCC stores string constants in read-only memory, but
1228 * Var_Subst will want to write this thing, so store it
1231 static char VPATH[] = "${VPATH}";
1237 buf = Var_Subst(VPATH, VAR_CMD, FALSE);
1239 vpath = Buf_Data(buf);
1241 /* skip to end of directory */
1242 for (ptr = vpath; *ptr != ':' && *ptr != '\0'; ptr++)
1245 /* Save terminator character so know when to stop */
1249 /* Add directory to search path */
1250 Path_AddDir(&dirSearchPath, vpath);
1253 } while (savec != '\0');
1255 Buf_Destroy(buf, TRUE);
1259 * Now that all search paths have been read for suffixes et al, it's
1260 * time to add the default search path to their lists...
1264 /* print the initial graph, if the user requested it */
1268 /* print the values of any variables requested by the user */
1269 if (Lst_IsEmpty(&variables) && !printGraphOnly) {
1271 * Since the user has not requested that any variables
1272 * be printed, we can build targets.
1274 * Have read the entire graph and need to make a list of targets
1275 * to create. If none was given on the command line, we consult
1276 * the parsing module to find the main target(s) to create.
1278 Lst targs = Lst_Initializer(targs);
1280 if (!is_posix && mfAutoDeps) {
1282 * Check if any of the makefiles are out-of-date.
1287 if (Lst_IsEmpty(&create))
1288 Parse_MainName(&targs);
1290 Targ_FindList(&targs, &create, TARG_CREATE);
1294 * Compat_Init will take care of creating
1295 * all the targets as well as initializing
1302 * Initialize job module before traversing
1303 * the graph, now that any .BEGIN and .END
1304 * targets have been read. This is done
1305 * only if the -q flag wasn't given (to
1306 * prevent the .BEGIN from being executed
1314 /* Traverse the graph, checking on all the targets */
1315 outOfDate = Make_Run(&targs);
1317 Lst_Destroy(&targs, NOFREE);
1320 Var_Print(&variables, expandVars);
1323 Lst_Destroy(&variables, free);
1324 Lst_Destroy(&makefiles, free);
1325 Lst_Destroy(&source_makefiles, free);
1326 Lst_Destroy(&create, free);
1328 /* print the graph now it's been processed if the user requested it */
1335 if (makeErrors != 0)