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>
67 #include <sys/queue.h>
68 #include <sys/resource.h>
69 #include <sys/utsname.h>
86 #include "pathnames.h"
94 extern char **environ; /* XXX what header declares this variable? */
96 #define WANT_ENV_MKLVL 1
97 #define MKLVL_MAXVAL 500
98 #define MKLVL_ENVVAR "__MKLVL__"
100 /* ordered list of makefiles to read */
101 static Lst makefiles = Lst_Initializer(makefiles);
103 /* ordered list of source makefiles */
104 static Lst source_makefiles = Lst_Initializer(source_makefiles);
106 /* list of variables to print */
107 static Lst variables = Lst_Initializer(variables);
109 static Boolean expandVars; /* fully expand printed variables */
110 static Boolean noBuiltins; /* -r flag */
111 static Boolean forceJobs; /* -j argument given */
112 static char *curdir; /* startup directory */
113 static char *objdir; /* where we chdir'ed to */
114 static char **save_argv; /* saved argv */
115 static char *save_makeflags;/* saved MAKEFLAGS */
117 /* (-E) vars to override from env */
118 Lst envFirstVars = Lst_Initializer(envFirstVars);
120 /* Targets to be made */
121 Lst create = Lst_Initializer(create);
123 Boolean allPrecious; /* .PRECIOUS given on line by itself */
124 Boolean is_posix; /* .POSIX target seen */
125 Boolean mfAutoDeps; /* .MAKEFILEDEPS target seen */
126 Boolean remakingMakefiles; /* True if remaking makefiles is in progress */
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;
370 char found_dir[MAXPATHLEN + 1]; /* for searching for sys.mk */
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);
397 if (getcwd(curdir, MAXPATHLEN) == NULL)
401 Var_SetGlobal(optarg, "1");
402 MFLAGS_append("-D", optarg);
405 char *modules = optarg;
407 for (; *modules; ++modules)
425 if (modules[1] == '1') {
426 debug |= DEBUG_GRAPH1;
429 else if (modules[1] == '2') {
430 debug |= DEBUG_GRAPH2;
453 warnx("illegal argument to d option "
457 MFLAGS_append("-d", optarg);
461 Lst_AtEnd(&envFirstVars, estrdup(optarg));
462 MFLAGS_append("-E", optarg);
465 checkEnvFirst = TRUE;
466 MFLAGS_append("-e", NULL);
469 Lst_AtEnd(&makefiles, estrdup(optarg));
472 Parse_AddIncludeDir(optarg);
473 MFLAGS_append("-I", optarg);
477 MFLAGS_append("-i", NULL);
483 jobLimit = strtol(optarg, &endptr, 10);
484 if (jobLimit <= 0 || *endptr != '\0') {
485 warnx("illegal number, -j argument -- %s",
489 MFLAGS_append("-j", optarg);
494 MFLAGS_append("-k", NULL);
497 /* look for magic parent directory search string */
498 if (strncmp(".../", optarg, 4) == 0) {
499 if (!Dir_FindHereOrAbove(curdir, optarg + 4,
500 found_dir, sizeof(found_dir)))
501 break; /* nothing doing */
502 Path_AddDir(&sysIncPath, found_dir);
504 Path_AddDir(&sysIncPath, optarg);
506 MFLAGS_append("-m", optarg);
510 MFLAGS_append("-n", NULL);
514 MFLAGS_append("-P", NULL);
517 printGraphOnly = TRUE;
518 debug |= DEBUG_GRAPH1;
523 MFLAGS_append("-Q", NULL);
527 /* Kind of nonsensical, wot? */
528 MFLAGS_append("-q", NULL);
532 MFLAGS_append("-r", NULL);
536 MFLAGS_append("-S", NULL);
540 MFLAGS_append("-s", NULL);
544 MFLAGS_append("-t", NULL);
547 Lst_AtEnd(&variables, estrdup(optarg));
548 MFLAGS_append("-V", optarg);
553 MFLAGS_append("-v", NULL);
559 if (Main_ParseWarn(optarg, 1) != -1)
560 MFLAGS_append("-x", optarg);
574 * Parse the rest of the arguments.
575 * o Check for variable assignments and perform them if so.
576 * o Check for more flags and restart getopt if so.
577 * o Anything else is taken to be a target and added
578 * to the end of the "create" list.
580 for (; *argv != NULL; ++argv, --argc) {
581 if (Parse_IsVar(*argv)) {
582 char *ptr = MAKEFLAGS_quote(*argv);
583 char *v = estrdup(*argv);
585 Var_Append(".MAKEFLAGS", ptr, VAR_GLOBAL);
586 Parse_DoVar(v, VAR_CMD);
590 } else if ((*argv)[0] == '-') {
591 if ((*argv)[1] == '\0') {
593 * (*argv) is a single dash, so we
596 } else if (found_dd) {
598 * Double dash has been found, ignore
599 * any more options. But what do we do
600 * with it? For now treat it like a target.
602 Lst_AtEnd(&create, estrdup(*argv));
605 * (*argv) is a -flag, so backup argv and
606 * argc. getopt() expects options to start
607 * in the 2nd position.
614 } else if ((*argv)[0] == '\0') {
615 Punt("illegal (null) argument.");
618 Lst_AtEnd(&create, estrdup(*argv));
625 * Used by the parse module when a .MFLAGS or .MAKEFLAGS target
626 * is encountered and by main() when reading the .MAKEFLAGS envariable.
627 * Takes a line of arguments and breaks it into its
628 * component words and passes those words and the number of them to the
629 * MainParseArgs function.
630 * The line should have all its leading whitespace removed.
633 * Only those that come from the various arguments.
636 Main_ParseArgLine(char *line, int mflags)
642 for (; *line == ' '; ++line)
648 MAKEFLAGS_break(&aa, line);
650 brk_string(&aa, line, TRUE);
652 MainParseArgs(aa.argc, aa.argv);
657 chdir_verify_path(const char *path, char *obpath)
661 if (stat(path, &sb) == 0 && S_ISDIR(sb.st_mode)) {
662 if (chdir(path) == -1 || getcwd(obpath, MAXPATHLEN) == NULL) {
663 warn("warning: %s", path);
673 * In lieu of a good way to prevent every possible looping in make(1), stop
674 * there from being more than MKLVL_MAXVAL processes forked by make(1), to
675 * prevent a forkbomb from happening, in a dumb and mechanical way.
678 * Creates or modifies enviornment variable MKLVL_ENVVAR via setenv().
681 check_make_level(void)
683 #ifdef WANT_ENV_MKLVL
684 char *value = getenv(MKLVL_ENVVAR);
685 int level = (value == NULL) ? 0 : atoi(value);
688 errx(2, "Invalid value for recursion level (%d).", level);
689 } else if (level > MKLVL_MAXVAL) {
690 errx(2, "Max recursion level (%d) exceeded.", MKLVL_MAXVAL);
693 sprintf(new_value, "%d", level + 1);
694 setenv(MKLVL_ENVVAR, new_value, 1);
696 #endif /* WANT_ENV_MKLVL */
700 * Main_AddSourceMakefile
701 * Add a file to the list of source makefiles
704 Main_AddSourceMakefile(const char *name)
707 Lst_AtEnd(&source_makefiles, estrdup(name));
712 * Remake all the makefiles
715 Remake_Makefiles(void)
722 Compat_InstallSignalHandlers();
723 if (curdir != objdir) {
724 if (chdir(curdir) < 0)
725 Fatal("Failed to change directory to %s.", curdir);
729 LST_FOREACH(ln, &source_makefiles) {
732 const char *name = Lst_Datum(ln);
733 Boolean saveTouchFlag = touchFlag;
734 Boolean saveQueryFlag = queryFlag;
735 Boolean saveNoExecute = noExecute;
741 gn = Targ_FindNode(name, TARG_CREATE);
742 DEBUGF(MAKE, ("Checking %s...", gn->name));
746 * -t, -q and -n has no effect unless the makefile is
747 * specified as one of the targets explicitly in the
750 LST_FOREACH(ln2, &create) {
751 if (!strcmp(gn->name, Lst_Datum(ln2))) {
752 /* found as a target */
763 * Check and remake the makefile
765 mtime = Dir_MTime(gn);
766 remakingMakefiles = TRUE;
768 remakingMakefiles = FALSE;
771 * Restore -t, -q and -n behaviour
773 touchFlag = saveTouchFlag;
774 queryFlag = saveQueryFlag;
775 noExecute = saveNoExecute;
778 * Compat_Make will leave the 'made' field of gn
779 * in one of the following states:
780 * UPTODATE gn was already up-to-date
781 * MADE gn was recreated successfully
782 * ERROR An error occurred while gn was being created
783 * ABORTED gn was not remade because one of its inferiors
784 * could not be made due to errors.
786 if (gn->made == MADE) {
787 if (mtime != Dir_MTime(gn)) {
789 ("%s updated (%d -> %d).\n",
790 gn->name, mtime, gn->mtime));
794 ("%s not updated: skipping restart.\n",
797 } else if (gn->made == ERROR)
799 else if (gn->made == ABORTED) {
800 printf("`%s' not remade because of errors.\n",
803 } else if (gn->made == UPTODATE) {
804 Lst_EnQueue(&cleanup, gn);
809 Fatal("Failed to remake Makefiles.");
810 if (remade_cnt > 0) {
811 DEBUGF(MAKE, ("Restarting `%s'.\n", save_argv[0]));
814 * Some of makefiles were remade -- restart from clean state
816 if (save_makeflags != NULL)
817 setenv("MAKEFLAGS", save_makeflags, 1);
819 unsetenv("MAKEFLAGS");
820 if (execvp(save_argv[0], save_argv) < 0) {
821 Fatal("Can't restart `%s': %s.",
822 save_argv[0], strerror(errno));
826 while (!Lst_IsEmpty(&cleanup)) {
827 GNode *gn = Lst_DeQueue(&cleanup);
832 gn->childMade = FALSE;
833 gn->mtime = gn->cmtime = 0;
834 gn->cmtime_gn = NULL;
836 LST_FOREACH(ln, &gn->children) {
837 GNode *cgn = Lst_Datum(ln);
840 Lst_EnQueue(&cleanup, cgn);
844 if (curdir != objdir) {
845 if (chdir(objdir) < 0)
846 Fatal("Failed to change directory to %s.", objdir);
852 * The main function, for obvious reasons. Initializes variables
853 * and a few modules, then parses the arguments give it in the
854 * environment and on the command line. Reads the system makefile
855 * followed by either Makefile, makefile or the file given by the
856 * -f argument. Sets the .MAKEFLAGS PMake variable based on all the
857 * flags it has received by then uses either the Make or the Compat
858 * module to create the initial list of targets.
861 * If -q was given, exits -1 if anything was out-of-date. Else it exits
865 * The program exits when done. Targets are created. etc. etc. etc.
868 main(int argc, char **argv)
871 const char *machine_arch;
872 const char *machine_cpu;
873 Boolean outOfDate = TRUE; /* FALSE if all targets up to date */
877 char mdpath[MAXPATHLEN];
878 char obpath[MAXPATHLEN];
879 char cdpath[MAXPATHLEN];
880 char found_dir[MAXPATHLEN + 1]; /* for searching for sys.mk */
881 char *cp = NULL, *start;
884 save_makeflags = getenv("MAKEFLAGS");
885 if (save_makeflags != NULL)
886 save_makeflags = estrdup(save_makeflags);
889 * Initialize file global variables.
892 noBuiltins = FALSE; /* Read the built-in rules */
893 forceJobs = FALSE; /* No -j flag */
897 * Initialize program global variables.
899 beSilent = FALSE; /* Print commands as executed */
900 ignoreErrors = FALSE; /* Pay attention to non-zero returns */
901 noExecute = FALSE; /* Execute all commands */
902 printGraphOnly = FALSE; /* Don't stop after printing graph */
903 keepgoing = FALSE; /* Stop on error */
904 allPrecious = FALSE; /* Remove targets when interrupted */
905 queryFlag = FALSE; /* This is not just a check-run */
906 touchFlag = FALSE; /* Actually update targets */
907 usePipes = TRUE; /* Catch child output in pipes */
908 debug = 0; /* No debug verbosity, please. */
911 jobLimit = DEFMAXJOBS;
912 compatMake = FALSE; /* No compat mode */
918 * get rid of resource limit on file descriptors
922 if (getrlimit(RLIMIT_NOFILE, &rl) == -1) {
925 rl.rlim_cur = rl.rlim_max;
926 if (setrlimit(RLIMIT_NOFILE, &rl) == -1) {
933 * Get the name of this type of MACHINE from utsname
934 * so we can share an executable for similar machines.
935 * (i.e. m68k: amiga hp300, mac68k, sun3, ...)
937 * Note that both MACHINE and MACHINE_ARCH are decided at
940 if ((machine = getenv("MACHINE")) == NULL) {
941 static struct utsname utsname;
943 if (uname(&utsname) == -1)
945 machine = utsname.machine;
948 if ((machine_arch = getenv("MACHINE_ARCH")) == NULL) {
950 machine_arch = MACHINE_ARCH;
952 machine_arch = "unknown";
957 * Set machine_cpu to the minumum supported CPU revision based
958 * on the target architecture, if not already set.
960 if ((machine_cpu = getenv("MACHINE_CPU")) == NULL) {
961 if (!strcmp(machine_arch, "i386"))
962 machine_cpu = "i386";
964 machine_cpu = "unknown";
968 * Initialize the parsing, directory and variable modules to prepare
969 * for the reading of inclusion paths and variable settings on the
974 Dir_Init(); /* Initialize directory structures so -I flags
975 * can be processed correctly */
976 Var_Init(environ); /* As well as the lists of variables for
977 * parsing arguments */
980 * Initialize the Shell so that we have a shell for != assignments
981 * on the command line.
986 * Initialize various variables.
987 * MAKE also gets this name, for compatibility
988 * .MAKEFLAGS gets set to the empty string just in case.
989 * MFLAGS also gets initialized empty, for compatibility.
991 Var_SetGlobal("MAKE", argv[0]);
992 Var_SetGlobal(".MAKEFLAGS", "");
993 Var_SetGlobal("MFLAGS", "");
994 Var_SetGlobal("MACHINE", machine);
995 Var_SetGlobal("MACHINE_ARCH", machine_arch);
996 Var_SetGlobal("MACHINE_CPU", machine_cpu);
998 Var_SetGlobal("MAKE_VERSION", MAKE_VERSION);
1000 Var_SetGlobal(".newline", "\n"); /* handy for :@ loops */
1004 snprintf(tmp, sizeof(tmp), "%u", getpid());
1005 Var_SetGlobal(".MAKE.PID", tmp);
1006 snprintf(tmp, sizeof(tmp), "%u", getppid());
1007 Var_SetGlobal(".MAKE.PPID", tmp);
1012 * Find where we are...
1014 if (getcwd(curdir, MAXPATHLEN) == NULL)
1018 * First snag things out of the MAKEFLAGS environment
1019 * variable. Then parse the command line arguments.
1021 Main_ParseArgLine(getenv("MAKEFLAGS"), 1);
1023 MainParseArgs(argc, argv);
1026 * Verify that cwd is sane (after -C may have changed it).
1031 if (stat(curdir, &sa) == -1)
1032 err(2, "%s", curdir);
1036 * The object directory location is determined using the
1037 * following order of preference:
1039 * 1. MAKEOBJDIRPREFIX`cwd`
1041 * 3. PATH_OBJDIR.${MACHINE}
1043 * 5. PATH_OBJDIRPREFIX`cwd`
1045 * If one of the first two fails, use the current directory.
1046 * If the remaining three all fail, use the current directory.
1048 * Once things are initted,
1049 * have to add the original directory to the search path,
1050 * and modify the paths for the Makefiles apropriately. The
1051 * current directory is also placed as a variable for make scripts.
1053 if (!(pathp = getenv("MAKEOBJDIRPREFIX"))) {
1054 if (!(path = getenv("MAKEOBJDIR"))) {
1056 pathp = PATH_OBJDIRPREFIX;
1057 snprintf(mdpath, MAXPATHLEN, "%s.%s", path, machine);
1058 if (!(objdir = chdir_verify_path(mdpath, obpath)))
1059 if (!(objdir=chdir_verify_path(path, obpath))) {
1060 snprintf(mdpath, MAXPATHLEN,
1061 "%s%s", pathp, curdir);
1062 if (!(objdir=chdir_verify_path(mdpath,
1067 else if (!(objdir = chdir_verify_path(path, obpath)))
1071 snprintf(mdpath, MAXPATHLEN, "%s%s", pathp, curdir);
1072 if (!(objdir = chdir_verify_path(mdpath, obpath)))
1075 Dir_InitDot(); /* Initialize the "." directory */
1076 if (objdir != curdir)
1077 Path_AddDir(&dirSearchPath, curdir);
1078 Var_SetGlobal(".ST_EXPORTVAR", "YES");
1079 Var_SetGlobal(".CURDIR", curdir);
1080 Var_SetGlobal(".OBJDIR", objdir);
1082 if (getenv("MAKE_JOBS_FIFO") != NULL)
1085 * Be compatible if user did not specify -j and did not explicitly
1086 * turned compatibility on
1088 if (!compatMake && !forceJobs)
1092 * Initialize target and suffix modules in preparation for
1093 * parsing the makefile(s)
1102 * Set up the .TARGETS variable to contain the list of targets to be
1103 * created. If none specified, make the variable empty -- the parser
1104 * will fill the thing in with the default or .MAIN target.
1106 if (Lst_IsEmpty(&create)) {
1107 Var_SetGlobal(".TARGETS", "");
1111 for (ln = Lst_First(&create); ln != NULL; ln = Lst_Succ(ln)) {
1112 char *name = Lst_Datum(ln);
1114 Var_Append(".TARGETS", name, VAR_GLOBAL);
1120 * If no user-supplied system path was given (through the -m option)
1121 * add the directories from the DEFSYSPATH (more than one may be given
1122 * as dir1:...:dirn) to the system include path.
1124 if (TAILQ_EMPTY(&sysIncPath)) {
1125 char defsyspath[] = PATH_DEFSYSPATH;
1126 char *syspath = getenv("MAKESYSPATH");
1129 * If no user-supplied system path was given (thru -m option)
1130 * add the directories from the DEFSYSPATH (more than one may
1131 * be given as dir1:...:dirn) to the system include path.
1133 if (syspath == NULL || *syspath == '\0')
1134 syspath = defsyspath;
1136 syspath = estrdup(syspath);
1138 for (start = syspath; *start != '\0'; start = cp) {
1139 for (cp = start; *cp != '\0' && *cp != ':'; cp++)
1144 /* look for magic parent directory search string */
1145 if (strncmp(".../", start, 4) == 0) {
1146 if (Dir_FindHereOrAbove(curdir, start + 4,
1147 found_dir, sizeof(found_dir))) {
1148 Path_AddDir(&sysIncPath, found_dir);
1151 Path_AddDir(&sysIncPath, start);
1154 if (syspath != defsyspath)
1159 * Read in the built-in rules first, followed by the specified
1160 * makefile, if it was (makefile != (char *) NULL), or the default
1161 * Makefile and makefile, in that order, if it wasn't.
1164 /* Path of sys.mk */
1165 Lst sysMkPath = Lst_Initializer(sysMkPath);
1167 char defsysmk[] = PATH_DEFSYSMK;
1169 Path_Expand(defsysmk, &sysIncPath, &sysMkPath);
1170 if (Lst_IsEmpty(&sysMkPath))
1171 Fatal("make: no system rules (%s).", PATH_DEFSYSMK);
1172 LST_FOREACH(ln, &sysMkPath) {
1173 if (!ReadMakefile(Lst_Datum(ln)))
1177 Fatal("make: cannot open %s.", (char *)Lst_Datum(ln));
1178 Lst_Destroy(&sysMkPath, free);
1181 if (!Lst_IsEmpty(&makefiles)) {
1184 LST_FOREACH(ln, &makefiles) {
1185 if (!TryReadMakefile(Lst_Datum(ln)))
1189 Fatal("make: cannot open %s.", (char *)Lst_Datum(ln));
1190 } else if (!TryReadMakefile("BSDmakefile"))
1191 if (!TryReadMakefile("makefile"))
1192 TryReadMakefile("Makefile");
1194 ReadMakefile(".depend");
1196 /* Install all the flags into the MAKEFLAGS envariable. */
1197 if (((p = Var_Value(".MAKEFLAGS", VAR_GLOBAL)) != NULL) && *p)
1198 setenv("MAKEFLAGS", p, 1);
1200 setenv("MAKEFLAGS", "", 1);
1203 * For compatibility, look at the directories in the VPATH variable
1204 * and add them to the search path, if the variable is defined. The
1205 * variable's value is in the same format as the PATH envariable, i.e.
1206 * <directory>:<directory>:<directory>...
1208 if (Var_Exists("VPATH", VAR_CMD)) {
1210 * GCC stores string constants in read-only memory, but
1211 * Var_Subst will want to write this thing, so store it
1214 static char VPATH[] = "${VPATH}";
1220 buf = Var_Subst(VPATH, VAR_CMD, FALSE);
1222 vpath = Buf_Data(buf);
1224 /* skip to end of directory */
1225 for (ptr = vpath; *ptr != ':' && *ptr != '\0'; ptr++)
1228 /* Save terminator character so know when to stop */
1232 /* Add directory to search path */
1233 Path_AddDir(&dirSearchPath, vpath);
1236 } while (savec != '\0');
1238 Buf_Destroy(buf, TRUE);
1242 * Now that all search paths have been read for suffixes et al, it's
1243 * time to add the default search path to their lists...
1247 /* print the initial graph, if the user requested it */
1251 /* print the values of any variables requested by the user */
1252 if (Lst_IsEmpty(&variables) && !printGraphOnly) {
1254 * Since the user has not requested that any variables
1255 * be printed, we can build targets.
1257 * Have read the entire graph and need to make a list of targets
1258 * to create. If none was given on the command line, we consult
1259 * the parsing module to find the main target(s) to create.
1261 Lst targs = Lst_Initializer(targs);
1263 if (!is_posix && mfAutoDeps) {
1265 * Check if any of the makefiles are out-of-date.
1270 if (Lst_IsEmpty(&create))
1271 Parse_MainName(&targs);
1273 Targ_FindList(&targs, &create, TARG_CREATE);
1277 * Compat_Init will take care of creating
1278 * all the targets as well as initializing
1285 * Initialize job module before traversing
1286 * the graph, now that any .BEGIN and .END
1287 * targets have been read. This is done
1288 * only if the -q flag wasn't given (to
1289 * prevent the .BEGIN from being executed
1297 /* Traverse the graph, checking on all the targets */
1298 outOfDate = Make_Run(&targs);
1300 Lst_Destroy(&targs, NOFREE);
1303 Var_Print(&variables, expandVars);
1306 Lst_Destroy(&variables, free);
1307 Lst_Destroy(&makefiles, free);
1308 Lst_Destroy(&source_makefiles, free);
1309 Lst_Destroy(&create, free);
1311 /* print the graph now it's been processed if the user requested it */
1318 if (makeErrors != 0)