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 remakingMakefiles; /* True if remaking makefiles is in progress */
128 Boolean beSilent; /* -s flag */
129 Boolean beVerbose; /* -v flag */
130 Boolean beQuiet; /* -Q flag */
131 Boolean compatMake; /* -B argument */
132 int debug; /* -d flag */
133 Boolean ignoreErrors; /* -i flag */
134 int jobLimit; /* -j argument */
135 int makeErrors; /* Number of targets not remade due to errors */
136 Boolean jobsRunning; /* TRUE if the jobs might be running */
137 Boolean keepgoing; /* -k flag */
138 Boolean noExecute; /* -n flag */
139 Boolean printGraphOnly; /* -p flag */
140 Boolean queryFlag; /* -q flag */
141 Boolean touchFlag; /* -t flag */
142 Boolean usePipes; /* !-P flag */
143 uint32_t warn_cmd; /* command line warning flags */
144 uint32_t warn_flags; /* actual warning flags */
145 uint32_t warn_nocmd; /* command line no-warning flags */
147 time_t now; /* Time at start of make */
148 struct GNode *DEFAULT; /* .DEFAULT node */
151 * Exit with usage message.
157 "usage: make [-BPSXeiknpqrstv] [-C directory] [-D variable]\n"
158 "\t[-d flags] [-E variable] [-f makefile] [-I directory]\n"
159 "\t[-j max_jobs] [-m directory] [-V variable]\n"
160 "\t[variable=value] [target ...]\n");
166 * Append a flag with an optional argument to MAKEFLAGS and MFLAGS
169 MFLAGS_append(const char *flag, char *arg)
173 Var_Append(".MAKEFLAGS", flag, VAR_GLOBAL);
175 str = MAKEFLAGS_quote(arg);
176 Var_Append(".MAKEFLAGS", str, VAR_GLOBAL);
180 Var_Append("MFLAGS", flag, VAR_GLOBAL);
182 str = MAKEFLAGS_quote(arg);
183 Var_Append("MFLAGS", str, VAR_GLOBAL);
191 * Handle argument to warning option.
194 Main_ParseWarn(const char *arg, int iscmd)
198 static const struct {
202 { "dirsyntax", WARN_DIRSYNTAX },
207 if (arg[0] == 'n' && arg[1] == 'o') {
212 for (i = 0; options[i].option != NULL; i++)
213 if (strcmp(arg, options[i].option) == 0)
216 if (options[i].option == NULL)
222 warn_cmd |= options[i].flag;
223 warn_nocmd &= ~options[i].flag;
224 warn_flags |= options[i].flag;
226 warn_nocmd |= options[i].flag;
227 warn_cmd &= ~options[i].flag;
228 warn_flags &= ~options[i].flag;
232 warn_flags |= (options[i].flag & ~warn_nocmd);
234 warn_flags &= ~(options[i].flag | warn_cmd);
241 * Open and parse the given makefile.
244 * TRUE if ok. FALSE if couldn't open file.
247 ReadMakefile(const char p[])
249 char *fname, *fnamesave; /* makefile to read */
251 char *name, path[MAXPATHLEN];
255 /* XXX - remove this once constification is done */
256 fnamesave = fname = estrdup(p);
258 if (!strcmp(fname, "-")) {
259 Parse_File("(stdin)", stdin);
260 Var_SetGlobal("MAKEFILE", "");
262 setMAKEFILE = strcmp(fname, ".depend");
264 /* if we've chdir'd, rebuild the path name */
265 if (curdir != objdir && *fname != '/') {
266 snprintf(path, MAXPATHLEN, "%s/%s", curdir, fname);
268 * XXX The realpath stuff breaks relative includes
269 * XXX in some cases. The problem likely is in
270 * XXX parse.c where it does special things in
271 * XXX ParseDoInclude if the file is relateive
272 * XXX or absolute and not a system file. There
273 * XXX it assumes that if the current file that's
274 * XXX being included is absolute, that any files
275 * XXX that it includes shouldn't do the -I path
276 * XXX stuff, which is inconsistant with historical
277 * XXX behavior. However, I can't pentrate the mists
278 * XXX further, so I'm putting this workaround in
279 * XXX here until such time as the underlying bug
282 #if THIS_BREAKS_THINGS
283 if (realpath(path, path) != NULL &&
284 (stream = fopen(path, "r")) != NULL) {
289 } else if (realpath(fname, path) != NULL) {
292 if ((stream = fopen(fname, "r")) != NULL)
296 if ((stream = fopen(path, "r")) != NULL) {
303 if ((stream = fopen(fname, "r")) != NULL)
307 /* look in -I and system include directories. */
308 name = Path_FindFile(fname, &parseIncPath);
310 name = Path_FindFile(fname, &sysIncPath);
311 if (!name || !(stream = fopen(name, "r"))) {
315 MAKEFILE = fname = name;
317 * set the MAKEFILE variable desired by System V fans -- the
318 * placement of the setting here means it gets set to the last
319 * makefile specified, as it is set by SysV make.
323 Var_SetGlobal("MAKEFILE", MAKEFILE);
324 Parse_File(fname, stream);
331 * Open and parse the given makefile.
332 * If open is successful add it to the list of makefiles.
335 * TRUE if ok. FALSE if couldn't open file.
338 TryReadMakefile(const char p[])
341 LstNode *last = Lst_Last(&source_makefiles);
343 if (!ReadMakefile(p))
348 LstNode *first = Lst_First(&source_makefiles);
349 Lst_Insert(&source_makefiles, first, data);
351 Lst_Append(&source_makefiles, last, estrdup(p));
357 * Parse a given argument vector. Called from main() and from
358 * Main_ParseArgLine() when the .MAKEFLAGS target is used.
360 * XXX: Deal with command line overriding .MAKEFLAGS in makefile
363 * Various global and local flags will be set depending on the flags
367 MainParseArgs(int argc, char **argv)
370 Boolean found_dd = FALSE;
373 optind = 1; /* since we're called more than once */
375 #define OPTFLAGS "ABC:D:d:E:ef:I:ij:km:nPpQqrSstV:vXx:"
377 if ((optind < argc) && strcmp(argv[optind], "--") == 0) {
380 if ((c = getopt(argc, argv, OPTFLAGS)) == -1) {
387 MFLAGS_append("-A", NULL);
391 MFLAGS_append("-B", NULL);
392 unsetenv("MAKE_JOBS_FIFO");
395 if (chdir(optarg) == -1)
396 err(1, "chdir %s", optarg);
399 Var_SetGlobal(optarg, "1");
400 MFLAGS_append("-D", optarg);
403 char *modules = optarg;
405 for (; *modules; ++modules)
423 if (modules[1] == '1') {
424 debug |= DEBUG_GRAPH1;
427 else if (modules[1] == '2') {
428 debug |= DEBUG_GRAPH2;
451 warnx("illegal argument to d option "
455 MFLAGS_append("-d", optarg);
459 Lst_AtEnd(&envFirstVars, estrdup(optarg));
460 MFLAGS_append("-E", optarg);
463 checkEnvFirst = TRUE;
464 MFLAGS_append("-e", NULL);
467 Lst_AtEnd(&makefiles, estrdup(optarg));
470 Parse_AddIncludeDir(optarg);
471 MFLAGS_append("-I", optarg);
475 MFLAGS_append("-i", NULL);
481 jobLimit = strtol(optarg, &endptr, 10);
482 if (jobLimit <= 0 || *endptr != '\0') {
483 warnx("illegal number, -j argument -- %s",
487 MFLAGS_append("-j", optarg);
492 MFLAGS_append("-k", NULL);
495 Path_AddDir(&sysIncPath, optarg);
496 MFLAGS_append("-m", optarg);
500 MFLAGS_append("-n", NULL);
504 MFLAGS_append("-P", NULL);
507 printGraphOnly = TRUE;
508 debug |= DEBUG_GRAPH1;
513 MFLAGS_append("-Q", NULL);
517 /* Kind of nonsensical, wot? */
518 MFLAGS_append("-q", NULL);
522 MFLAGS_append("-r", NULL);
526 MFLAGS_append("-S", NULL);
530 MFLAGS_append("-s", NULL);
534 MFLAGS_append("-t", NULL);
537 Lst_AtEnd(&variables, estrdup(optarg));
538 MFLAGS_append("-V", optarg);
543 MFLAGS_append("-v", NULL);
549 if (Main_ParseWarn(optarg, 1) != -1)
550 MFLAGS_append("-x", optarg);
564 * Parse the rest of the arguments.
565 * o Check for variable assignments and perform them if so.
566 * o Check for more flags and restart getopt if so.
567 * o Anything else is taken to be a target and added
568 * to the end of the "create" list.
570 for (; *argv != NULL; ++argv, --argc) {
571 if (Parse_IsVar(*argv)) {
572 char *ptr = MAKEFLAGS_quote(*argv);
573 char *v = estrdup(*argv);
575 Var_Append(".MAKEFLAGS", ptr, VAR_GLOBAL);
576 Parse_DoVar(v, VAR_CMD);
580 } else if ((*argv)[0] == '-') {
581 if ((*argv)[1] == '\0') {
583 * (*argv) is a single dash, so we
586 } else if (found_dd) {
588 * Double dash has been found, ignore
589 * any more options. But what do we do
590 * with it? For now treat it like a target.
592 Lst_AtEnd(&create, estrdup(*argv));
595 * (*argv) is a -flag, so backup argv and
596 * argc. getopt() expects options to start
597 * in the 2nd position.
604 } else if ((*argv)[0] == '\0') {
605 Punt("illegal (null) argument.");
608 Lst_AtEnd(&create, estrdup(*argv));
615 * Used by the parse module when a .MFLAGS or .MAKEFLAGS target
616 * is encountered and by main() when reading the .MAKEFLAGS envariable.
617 * Takes a line of arguments and breaks it into its
618 * component words and passes those words and the number of them to the
619 * MainParseArgs function.
620 * The line should have all its leading whitespace removed.
623 * Only those that come from the various arguments.
626 Main_ParseArgLine(char *line, int mflags)
632 for (; *line == ' '; ++line)
638 MAKEFLAGS_break(&aa, line);
640 brk_string(&aa, line, TRUE);
642 MainParseArgs(aa.argc, aa.argv);
647 chdir_verify_path(const char *path, char *obpath)
651 if (stat(path, &sb) == 0 && S_ISDIR(sb.st_mode)) {
652 if (chdir(path) == -1 || getcwd(obpath, MAXPATHLEN) == NULL) {
653 warn("warning: %s", path);
663 * In lieu of a good way to prevent every possible looping in make(1), stop
664 * there from being more than MKLVL_MAXVAL processes forked by make(1), to
665 * prevent a forkbomb from happening, in a dumb and mechanical way.
668 * Creates or modifies enviornment variable MKLVL_ENVVAR via setenv().
671 check_make_level(void)
673 #ifdef WANT_ENV_MKLVL
674 char *value = getenv(MKLVL_ENVVAR);
675 int level = (value == NULL) ? 0 : atoi(value);
678 errc(2, EAGAIN, "Invalid value for recursion level (%d).",
680 } else if (level > MKLVL_MAXVAL) {
681 errc(2, EAGAIN, "Max recursion level (%d) exceeded.",
685 sprintf(new_value, "%d", level + 1);
686 setenv(MKLVL_ENVVAR, new_value, 1);
688 #endif /* WANT_ENV_MKLVL */
692 * Main_AddSourceMakefile
693 * Add a file to the list of source makefiles
696 Main_AddSourceMakefile(const char *name)
699 Lst_AtEnd(&source_makefiles, estrdup(name));
704 * Remake all the makefiles
707 Remake_Makefiles(void)
713 Compat_InstallSignalHandlers();
714 if (curdir != objdir) {
715 if (chdir(curdir) < 0)
716 Fatal("Failed to change directory to %s.", curdir);
719 LST_FOREACH(ln, &source_makefiles) {
722 const char *name = Lst_Datum(ln);
723 Boolean saveTouchFlag = touchFlag;
724 Boolean saveQueryFlag = queryFlag;
725 Boolean saveNoExecute = noExecute;
731 gn = Targ_FindNode(name, TARG_CREATE);
732 DEBUGF(MAKE, ("Checking %s...", gn->name));
736 * -t, -q and -n has no effect unless the makefile is
737 * specified as one of the targets explicitly in the
740 LST_FOREACH(ln2, &create) {
741 if (!strcmp(gn->name, Lst_Datum(ln2))) {
742 /* found as a target */
753 * Check and remake the makefile
755 mtime = Dir_MTime(gn);
756 remakingMakefiles = TRUE;
758 remakingMakefiles = FALSE;
761 * Restore -t, -q and -n behaviour
763 touchFlag = saveTouchFlag;
764 queryFlag = saveQueryFlag;
765 noExecute = saveNoExecute;
768 * Compat_Make will leave the 'made' field of gn
769 * in one of the following states:
770 * UPTODATE gn was already up-to-date
771 * MADE gn was recreated successfully
772 * ERROR An error occurred while gn was being created
773 * ABORTED gn was not remade because one of its inferiors
774 * could not be made due to errors.
776 if (gn->made == MADE) {
777 if (mtime != Dir_MTime(gn)) {
779 ("%s updated (%d -> %d).\n",
780 gn->name, mtime, gn->mtime));
784 ("%s not updated: skipping restart.\n",
787 } else if (gn->made == ERROR)
789 else if (gn->made == ABORTED) {
790 printf("`%s' not remade because of errors.\n",
793 } else if (gn->made == UPTODATE) {
797 Lst_EnQueue(&examine, gn);
798 while (!Lst_IsEmpty(&examine)) {
800 GNode *egn = Lst_DeQueue(&examine);
803 LST_FOREACH(eln, &egn->children) {
804 GNode *cgn = Lst_Datum(eln);
806 Lst_EnQueue(&examine, cgn);
813 Fatal("Failed to remake Makefiles.");
814 if (remade_cnt > 0) {
815 DEBUGF(MAKE, ("Restarting `%s'.\n", save_argv[0]));
818 * Some of makefiles were remade -- restart from clean state
820 if (save_makeflags != NULL)
821 setenv("MAKEFLAGS", save_makeflags, 1);
823 unsetenv("MAKEFLAGS");
824 if (execvp(save_argv[0], save_argv) < 0) {
825 Fatal("Can't restart `%s': %s.",
826 save_argv[0], strerror(errno));
830 if (curdir != objdir) {
831 if (chdir(objdir) < 0)
832 Fatal("Failed to change directory to %s.", objdir);
838 * The main function, for obvious reasons. Initializes variables
839 * and a few modules, then parses the arguments give it in the
840 * environment and on the command line. Reads the system makefile
841 * followed by either Makefile, makefile or the file given by the
842 * -f argument. Sets the .MAKEFLAGS PMake variable based on all the
843 * flags it has received by then uses either the Make or the Compat
844 * module to create the initial list of targets.
847 * If -q was given, exits -1 if anything was out-of-date. Else it exits
851 * The program exits when done. Targets are created. etc. etc. etc.
854 main(int argc, char **argv)
857 const char *machine_arch;
858 const char *machine_cpu;
859 Boolean outOfDate = TRUE; /* FALSE if all targets up to date */
863 char mdpath[MAXPATHLEN];
864 char obpath[MAXPATHLEN];
865 char cdpath[MAXPATHLEN];
866 char *cp = NULL, *start;
869 save_makeflags = getenv("MAKEFLAGS");
870 if (save_makeflags != NULL)
871 save_makeflags = estrdup(save_makeflags);
874 * Initialize file global variables.
877 noBuiltins = FALSE; /* Read the built-in rules */
878 forceJobs = FALSE; /* No -j flag */
882 * Initialize program global variables.
884 beSilent = FALSE; /* Print commands as executed */
885 ignoreErrors = FALSE; /* Pay attention to non-zero returns */
886 noExecute = FALSE; /* Execute all commands */
887 printGraphOnly = FALSE; /* Don't stop after printing graph */
888 keepgoing = FALSE; /* Stop on error */
889 allPrecious = FALSE; /* Remove targets when interrupted */
890 queryFlag = FALSE; /* This is not just a check-run */
891 touchFlag = FALSE; /* Actually update targets */
892 usePipes = TRUE; /* Catch child output in pipes */
893 debug = 0; /* No debug verbosity, please. */
896 jobLimit = DEFMAXJOBS;
897 compatMake = FALSE; /* No compat mode */
903 * get rid of resource limit on file descriptors
907 if (getrlimit(RLIMIT_NOFILE, &rl) == -1) {
910 rl.rlim_cur = rl.rlim_max;
911 if (setrlimit(RLIMIT_NOFILE, &rl) == -1) {
918 * Prior to 7.0, FreeBSD/pc98 kernel used to set the
919 * utsname.machine to "i386", and MACHINE was defined as
920 * "i386", so it could not be distinguished from FreeBSD/i386.
921 * Therefore, we had to check machine.ispc98 and adjust the
922 * MACHINE variable. NOTE: The code is still here to be able
923 * to compile new make binary on old FreeBSD/pc98 systems, and
924 * have the MACHINE variable set properly.
926 if ((machine = getenv("MACHINE")) == NULL) {
930 len = sizeof(ispc98);
931 if (!sysctlbyname("machdep.ispc98", &ispc98, &len, NULL, 0)) {
938 * Get the name of this type of MACHINE from utsname
939 * so we can share an executable for similar machines.
940 * (i.e. m68k: amiga hp300, mac68k, sun3, ...)
942 * Note that both MACHINE and MACHINE_ARCH are decided at
945 if (machine == NULL) {
946 static struct utsname utsname;
948 if (uname(&utsname) == -1)
950 machine = utsname.machine;
953 if ((machine_arch = getenv("MACHINE_ARCH")) == NULL) {
955 machine_arch = MACHINE_ARCH;
957 machine_arch = "unknown";
962 * Set machine_cpu to the minumum supported CPU revision based
963 * on the target architecture, if not already set.
965 if ((machine_cpu = getenv("MACHINE_CPU")) == NULL) {
966 if (!strcmp(machine_arch, "i386"))
967 machine_cpu = "i386";
968 else if (!strcmp(machine_arch, "alpha"))
971 machine_cpu = "unknown";
975 * Initialize the parsing, directory and variable modules to prepare
976 * for the reading of inclusion paths and variable settings on the
981 Dir_Init(); /* Initialize directory structures so -I flags
982 * can be processed correctly */
983 Var_Init(environ); /* As well as the lists of variables for
984 * parsing arguments */
987 * Initialize the Shell so that we have a shell for != assignments
988 * on the command line.
993 * Initialize various variables.
994 * MAKE also gets this name, for compatibility
995 * .MAKEFLAGS gets set to the empty string just in case.
996 * MFLAGS also gets initialized empty, for compatibility.
998 Var_SetGlobal("MAKE", argv[0]);
999 Var_SetGlobal(".MAKEFLAGS", "");
1000 Var_SetGlobal("MFLAGS", "");
1001 Var_SetGlobal("MACHINE", machine);
1002 Var_SetGlobal("MACHINE_ARCH", machine_arch);
1003 Var_SetGlobal("MACHINE_CPU", machine_cpu);
1005 Var_SetGlobal("MAKE_VERSION", MAKE_VERSION);
1007 Var_SetGlobal(".newline", "\n"); /* handy for :@ loops */
1011 snprintf(tmp, sizeof(tmp), "%u", getpid());
1012 Var_SetGlobal(".MAKE.PID", tmp);
1013 snprintf(tmp, sizeof(tmp), "%u", getppid());
1014 Var_SetGlobal(".MAKE.PPID", tmp);
1019 * First snag things out of the MAKEFLAGS environment
1020 * variable. Then parse the command line arguments.
1022 Main_ParseArgLine(getenv("MAKEFLAGS"), 1);
1024 MainParseArgs(argc, argv);
1027 * Find where we are...
1029 if (getcwd(curdir, MAXPATHLEN) == NULL)
1035 if (stat(curdir, &sa) == -1)
1036 err(2, "%s", curdir);
1040 * The object directory location is determined using the
1041 * following order of preference:
1043 * 1. MAKEOBJDIRPREFIX`cwd`
1045 * 3. PATH_OBJDIR.${MACHINE}
1047 * 5. PATH_OBJDIRPREFIX`cwd`
1049 * If one of the first two fails, use the current directory.
1050 * If the remaining three all fail, use the current directory.
1052 * Once things are initted,
1053 * have to add the original directory to the search path,
1054 * and modify the paths for the Makefiles apropriately. The
1055 * current directory is also placed as a variable for make scripts.
1057 if (!(pathp = getenv("MAKEOBJDIRPREFIX"))) {
1058 if (!(path = getenv("MAKEOBJDIR"))) {
1060 pathp = PATH_OBJDIRPREFIX;
1061 snprintf(mdpath, MAXPATHLEN, "%s.%s", path, machine);
1062 if (!(objdir = chdir_verify_path(mdpath, obpath)))
1063 if (!(objdir=chdir_verify_path(path, obpath))) {
1064 snprintf(mdpath, MAXPATHLEN,
1065 "%s%s", pathp, curdir);
1066 if (!(objdir=chdir_verify_path(mdpath,
1071 else if (!(objdir = chdir_verify_path(path, obpath)))
1075 snprintf(mdpath, MAXPATHLEN, "%s%s", pathp, curdir);
1076 if (!(objdir = chdir_verify_path(mdpath, obpath)))
1079 Dir_InitDot(); /* Initialize the "." directory */
1080 if (objdir != curdir)
1081 Path_AddDir(&dirSearchPath, curdir);
1082 Var_SetGlobal(".ST_EXPORTVAR", "YES");
1083 Var_SetGlobal(".CURDIR", curdir);
1084 Var_SetGlobal(".OBJDIR", objdir);
1086 if (getenv("MAKE_JOBS_FIFO") != NULL)
1089 * Be compatible if user did not specify -j and did not explicitly
1090 * turned compatibility on
1092 if (!compatMake && !forceJobs)
1096 * Initialize target and suffix modules in preparation for
1097 * parsing the makefile(s)
1106 * Set up the .TARGETS variable to contain the list of targets to be
1107 * created. If none specified, make the variable empty -- the parser
1108 * will fill the thing in with the default or .MAIN target.
1110 if (Lst_IsEmpty(&create)) {
1111 Var_SetGlobal(".TARGETS", "");
1115 for (ln = Lst_First(&create); ln != NULL; ln = Lst_Succ(ln)) {
1116 char *name = Lst_Datum(ln);
1118 Var_Append(".TARGETS", name, VAR_GLOBAL);
1124 * If no user-supplied system path was given (through the -m option)
1125 * add the directories from the DEFSYSPATH (more than one may be given
1126 * as dir1:...:dirn) to the system include path.
1128 if (TAILQ_EMPTY(&sysIncPath)) {
1129 char syspath[] = PATH_DEFSYSPATH;
1131 for (start = syspath; *start != '\0'; start = cp) {
1132 for (cp = start; *cp != '\0' && *cp != ':'; cp++)
1135 Path_AddDir(&sysIncPath, start);
1138 Path_AddDir(&sysIncPath, start);
1144 * Read in the built-in rules first, followed by the specified
1145 * makefile, if it was (makefile != (char *) NULL), or the default
1146 * Makefile and makefile, in that order, if it wasn't.
1149 /* Path of sys.mk */
1150 Lst sysMkPath = Lst_Initializer(sysMkPath);
1152 char defsysmk[] = PATH_DEFSYSMK;
1154 Path_Expand(defsysmk, &sysIncPath, &sysMkPath);
1155 if (Lst_IsEmpty(&sysMkPath))
1156 Fatal("make: no system rules (%s).", PATH_DEFSYSMK);
1157 LST_FOREACH(ln, &sysMkPath) {
1158 if (!ReadMakefile(Lst_Datum(ln)))
1162 Fatal("make: cannot open %s.", (char *)Lst_Datum(ln));
1163 Lst_Destroy(&sysMkPath, free);
1166 if (!Lst_IsEmpty(&makefiles)) {
1169 LST_FOREACH(ln, &makefiles) {
1170 if (!TryReadMakefile(Lst_Datum(ln)))
1174 Fatal("make: cannot open %s.", (char *)Lst_Datum(ln));
1175 } else if (!TryReadMakefile("BSDmakefile"))
1176 if (!TryReadMakefile("makefile"))
1177 TryReadMakefile("Makefile");
1179 ReadMakefile(".depend");
1181 /* Install all the flags into the MAKEFLAGS envariable. */
1182 if (((p = Var_Value(".MAKEFLAGS", VAR_GLOBAL)) != NULL) && *p)
1183 setenv("MAKEFLAGS", p, 1);
1185 setenv("MAKEFLAGS", "", 1);
1188 * For compatibility, look at the directories in the VPATH variable
1189 * and add them to the search path, if the variable is defined. The
1190 * variable's value is in the same format as the PATH envariable, i.e.
1191 * <directory>:<directory>:<directory>...
1193 if (Var_Exists("VPATH", VAR_CMD)) {
1195 * GCC stores string constants in read-only memory, but
1196 * Var_Subst will want to write this thing, so store it
1199 static char VPATH[] = "${VPATH}";
1205 buf = Var_Subst(VPATH, VAR_CMD, FALSE);
1207 vpath = Buf_Data(buf);
1209 /* skip to end of directory */
1210 for (ptr = vpath; *ptr != ':' && *ptr != '\0'; ptr++)
1213 /* Save terminator character so know when to stop */
1217 /* Add directory to search path */
1218 Path_AddDir(&dirSearchPath, vpath);
1221 } while (savec != '\0');
1223 Buf_Destroy(buf, TRUE);
1227 * Now that all search paths have been read for suffixes et al, it's
1228 * time to add the default search path to their lists...
1232 /* print the initial graph, if the user requested it */
1236 /* print the values of any variables requested by the user */
1237 if (Lst_IsEmpty(&variables) && !printGraphOnly) {
1239 * Since the user has not requested that any variables
1240 * be printed, we can build targets.
1242 * Have read the entire graph and need to make a list of targets
1243 * to create. If none was given on the command line, we consult
1244 * the parsing module to find the main target(s) to create.
1246 Lst targs = Lst_Initializer(targs);
1248 if (!is_posix && mfAutoDeps) {
1250 * Check if any of the makefiles are out-of-date.
1255 if (Lst_IsEmpty(&create))
1256 Parse_MainName(&targs);
1258 Targ_FindList(&targs, &create, TARG_CREATE);
1262 * Compat_Init will take care of creating
1263 * all the targets as well as initializing
1270 * Initialize job module before traversing
1271 * the graph, now that any .BEGIN and .END
1272 * targets have been read. This is done
1273 * only if the -q flag wasn't given (to
1274 * prevent the .BEGIN from being executed
1282 /* Traverse the graph, checking on all the targets */
1283 outOfDate = Make_Run(&targs);
1285 Lst_Destroy(&targs, NOFREE);
1288 Var_Print(&variables, expandVars);
1291 Lst_Destroy(&variables, free);
1292 Lst_Destroy(&makefiles, free);
1293 Lst_Destroy(&source_makefiles, free);
1294 Lst_Destroy(&create, free);
1296 /* print the graph now it's been processed if the user requested it */
1303 if (makeErrors != 0)