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 const char *foreign_name;
151 const char *freebsd_name;
153 { "x86_64", "amd64" },
154 { "mipsel", "mips" },
158 * Exit with usage message.
164 "usage: make [-BPSXeiknpqrstv] [-C directory] [-D variable]\n"
165 "\t[-d flags] [-E variable] [-f makefile] [-I directory]\n"
166 "\t[-j max_jobs] [-m directory] [-V variable]\n"
167 "\t[variable=value] [target ...]\n");
173 * Append a flag with an optional argument to MAKEFLAGS and MFLAGS
176 MFLAGS_append(const char *flag, char *arg)
180 Var_Append(".MAKEFLAGS", flag, VAR_GLOBAL);
182 str = MAKEFLAGS_quote(arg);
183 Var_Append(".MAKEFLAGS", str, VAR_GLOBAL);
187 Var_Append("MFLAGS", flag, VAR_GLOBAL);
189 str = MAKEFLAGS_quote(arg);
190 Var_Append("MFLAGS", str, VAR_GLOBAL);
198 * Handle argument to warning option.
201 Main_ParseWarn(const char *arg, int iscmd)
205 static const struct {
209 { "dirsyntax", WARN_DIRSYNTAX },
214 if (arg[0] == 'n' && arg[1] == 'o') {
219 for (i = 0; options[i].option != NULL; i++)
220 if (strcmp(arg, options[i].option) == 0)
223 if (options[i].option == NULL)
229 warn_cmd |= options[i].flag;
230 warn_nocmd &= ~options[i].flag;
231 warn_flags |= options[i].flag;
233 warn_nocmd |= options[i].flag;
234 warn_cmd &= ~options[i].flag;
235 warn_flags &= ~options[i].flag;
239 warn_flags |= (options[i].flag & ~warn_nocmd);
241 warn_flags &= ~(options[i].flag | warn_cmd);
248 * Open and parse the given makefile.
251 * TRUE if ok. FALSE if couldn't open file.
254 ReadMakefile(const char p[])
256 char *fname, *fnamesave; /* makefile to read */
258 char *name, path[MAXPATHLEN];
262 /* XXX - remove this once constification is done */
263 fnamesave = fname = estrdup(p);
265 if (!strcmp(fname, "-")) {
266 Parse_File("(stdin)", stdin);
267 Var_SetGlobal("MAKEFILE", "");
269 setMAKEFILE = strcmp(fname, ".depend");
271 /* if we've chdir'd, rebuild the path name */
272 if (curdir != objdir && *fname != '/') {
273 snprintf(path, MAXPATHLEN, "%s/%s", curdir, fname);
275 * XXX The realpath stuff breaks relative includes
276 * XXX in some cases. The problem likely is in
277 * XXX parse.c where it does special things in
278 * XXX ParseDoInclude if the file is relative
279 * XXX or absolute and not a system file. There
280 * XXX it assumes that if the current file that's
281 * XXX being included is absolute, that any files
282 * XXX that it includes shouldn't do the -I path
283 * XXX stuff, which is inconsistent with historical
284 * XXX behavior. However, I can't penetrate the mists
285 * XXX further, so I'm putting this workaround in
286 * XXX here until such time as the underlying bug
289 #if THIS_BREAKS_THINGS
290 if (realpath(path, path) != NULL &&
291 (stream = fopen(path, "r")) != NULL) {
296 } else if (realpath(fname, path) != NULL) {
299 if ((stream = fopen(fname, "r")) != NULL)
303 if ((stream = fopen(path, "r")) != NULL) {
310 if ((stream = fopen(fname, "r")) != NULL)
314 /* look in -I and system include directories. */
315 name = Path_FindFile(fname, &parseIncPath);
317 name = Path_FindFile(fname, &sysIncPath);
318 if (!name || !(stream = fopen(name, "r"))) {
322 MAKEFILE = fname = name;
324 * set the MAKEFILE variable desired by System V fans -- the
325 * placement of the setting here means it gets set to the last
326 * makefile specified, as it is set by SysV make.
330 Var_SetGlobal("MAKEFILE", MAKEFILE);
331 Parse_File(fname, stream);
338 * Open and parse the given makefile.
339 * If open is successful add it to the list of makefiles.
342 * TRUE if ok. FALSE if couldn't open file.
345 TryReadMakefile(const char p[])
348 LstNode *last = Lst_Last(&source_makefiles);
350 if (!ReadMakefile(p))
355 LstNode *first = Lst_First(&source_makefiles);
356 Lst_Insert(&source_makefiles, first, data);
358 Lst_Append(&source_makefiles, last, estrdup(p));
364 * Parse a given argument vector. Called from main() and from
365 * Main_ParseArgLine() when the .MAKEFLAGS target is used.
367 * XXX: Deal with command line overriding .MAKEFLAGS in makefile
370 * Various global and local flags will be set depending on the flags
374 MainParseArgs(int argc, char **argv)
377 Boolean found_dd = FALSE;
378 char found_dir[MAXPATHLEN + 1]; /* for searching for sys.mk */
381 optind = 1; /* since we're called more than once */
383 #define OPTFLAGS "ABC:D:d:E:ef:I:ij:km:nPpQqrSstV:vXx:"
385 if ((optind < argc) && strcmp(argv[optind], "--") == 0) {
388 if ((c = getopt(argc, argv, OPTFLAGS)) == -1) {
395 MFLAGS_append("-A", NULL);
399 MFLAGS_append("-B", NULL);
400 unsetenv("MAKE_JOBS_FIFO");
403 if (chdir(optarg) == -1)
404 err(1, "chdir %s", optarg);
405 if (getcwd(curdir, MAXPATHLEN) == NULL)
409 Var_SetGlobal(optarg, "1");
410 MFLAGS_append("-D", optarg);
413 char *modules = optarg;
415 for (; *modules; ++modules)
433 if (modules[1] == '1') {
434 debug |= DEBUG_GRAPH1;
437 else if (modules[1] == '2') {
438 debug |= DEBUG_GRAPH2;
461 warnx("illegal argument to d option "
465 MFLAGS_append("-d", optarg);
469 Lst_AtEnd(&envFirstVars, estrdup(optarg));
470 MFLAGS_append("-E", optarg);
473 checkEnvFirst = TRUE;
474 MFLAGS_append("-e", NULL);
477 Lst_AtEnd(&makefiles, estrdup(optarg));
480 Parse_AddIncludeDir(optarg);
481 MFLAGS_append("-I", optarg);
485 MFLAGS_append("-i", NULL);
491 jobLimit = strtol(optarg, &endptr, 10);
492 if (jobLimit <= 0 || *endptr != '\0') {
493 warnx("illegal number, -j argument -- %s",
497 MFLAGS_append("-j", optarg);
502 MFLAGS_append("-k", NULL);
505 /* look for magic parent directory search string */
506 if (strncmp(".../", optarg, 4) == 0) {
507 if (!Dir_FindHereOrAbove(curdir, optarg + 4,
508 found_dir, sizeof(found_dir)))
509 break; /* nothing doing */
510 Path_AddDir(&sysIncPath, found_dir);
512 Path_AddDir(&sysIncPath, optarg);
514 MFLAGS_append("-m", optarg);
518 MFLAGS_append("-n", NULL);
522 MFLAGS_append("-P", NULL);
525 printGraphOnly = TRUE;
526 debug |= DEBUG_GRAPH1;
531 MFLAGS_append("-Q", NULL);
535 /* Kind of nonsensical, wot? */
536 MFLAGS_append("-q", NULL);
540 MFLAGS_append("-r", NULL);
544 MFLAGS_append("-S", NULL);
548 MFLAGS_append("-s", NULL);
552 MFLAGS_append("-t", NULL);
555 Lst_AtEnd(&variables, estrdup(optarg));
556 MFLAGS_append("-V", optarg);
561 MFLAGS_append("-v", NULL);
567 if (Main_ParseWarn(optarg, 1) != -1)
568 MFLAGS_append("-x", optarg);
582 * Parse the rest of the arguments.
583 * o Check for variable assignments and perform them if so.
584 * o Check for more flags and restart getopt if so.
585 * o Anything else is taken to be a target and added
586 * to the end of the "create" list.
588 for (; *argv != NULL; ++argv, --argc) {
589 if (Parse_IsVar(*argv)) {
590 char *ptr = MAKEFLAGS_quote(*argv);
591 char *v = estrdup(*argv);
593 Var_Append(".MAKEFLAGS", ptr, VAR_GLOBAL);
594 Parse_DoVar(v, VAR_CMD);
598 } else if ((*argv)[0] == '-') {
599 if ((*argv)[1] == '\0') {
601 * (*argv) is a single dash, so we
604 } else if (found_dd) {
606 * Double dash has been found, ignore
607 * any more options. But what do we do
608 * with it? For now treat it like a target.
610 Lst_AtEnd(&create, estrdup(*argv));
613 * (*argv) is a -flag, so backup argv and
614 * argc. getopt() expects options to start
615 * in the 2nd position.
622 } else if ((*argv)[0] == '\0') {
623 Punt("illegal (null) argument.");
626 Lst_AtEnd(&create, estrdup(*argv));
633 * Used by the parse module when a .MFLAGS or .MAKEFLAGS target
634 * is encountered and by main() when reading the .MAKEFLAGS envariable.
635 * Takes a line of arguments and breaks it into its
636 * component words and passes those words and the number of them to the
637 * MainParseArgs function.
638 * The line should have all its leading whitespace removed.
641 * Only those that come from the various arguments.
644 Main_ParseArgLine(char *line, int mflags)
650 for (; *line == ' '; ++line)
656 MAKEFLAGS_break(&aa, line);
658 brk_string(&aa, line, TRUE);
660 MainParseArgs(aa.argc, aa.argv);
665 chdir_verify_path(const char *path, char *obpath)
669 if (stat(path, &sb) == 0 && S_ISDIR(sb.st_mode)) {
670 if (chdir(path) == -1 || getcwd(obpath, MAXPATHLEN) == NULL) {
671 warn("warning: %s", path);
681 * In lieu of a good way to prevent every possible looping in make(1), stop
682 * there from being more than MKLVL_MAXVAL processes forked by make(1), to
683 * prevent a forkbomb from happening, in a dumb and mechanical way.
686 * Creates or modifies environment variable MKLVL_ENVVAR via setenv().
689 check_make_level(void)
691 #ifdef WANT_ENV_MKLVL
692 char *value = getenv(MKLVL_ENVVAR);
693 int level = (value == NULL) ? 0 : atoi(value);
696 errx(2, "Invalid value for recursion level (%d).", level);
697 } else if (level > MKLVL_MAXVAL) {
698 errx(2, "Max recursion level (%d) exceeded.", MKLVL_MAXVAL);
701 sprintf(new_value, "%d", level + 1);
702 setenv(MKLVL_ENVVAR, new_value, 1);
704 #endif /* WANT_ENV_MKLVL */
708 * Main_AddSourceMakefile
709 * Add a file to the list of source makefiles
712 Main_AddSourceMakefile(const char *name)
715 Lst_AtEnd(&source_makefiles, estrdup(name));
720 * Remake all the makefiles
723 Remake_Makefiles(void)
730 Compat_InstallSignalHandlers();
731 if (curdir != objdir) {
732 if (chdir(curdir) < 0)
733 Fatal("Failed to change directory to %s.", curdir);
737 LST_FOREACH(ln, &source_makefiles) {
740 const char *name = Lst_Datum(ln);
741 Boolean saveTouchFlag = touchFlag;
742 Boolean saveQueryFlag = queryFlag;
743 Boolean saveNoExecute = noExecute;
749 gn = Targ_FindNode(name, TARG_CREATE);
750 DEBUGF(MAKE, ("Checking %s...", gn->name));
754 * -t, -q and -n has no effect unless the makefile is
755 * specified as one of the targets explicitly in the
758 LST_FOREACH(ln2, &create) {
759 if (!strcmp(gn->name, Lst_Datum(ln2))) {
760 /* found as a target */
771 * Check and remake the makefile
773 mtime = Dir_MTime(gn);
774 remakingMakefiles = TRUE;
776 remakingMakefiles = FALSE;
779 * Restore -t, -q and -n behaviour
781 touchFlag = saveTouchFlag;
782 queryFlag = saveQueryFlag;
783 noExecute = saveNoExecute;
786 * Compat_Make will leave the 'made' field of gn
787 * in one of the following states:
788 * UPTODATE gn was already up-to-date
789 * MADE gn was recreated successfully
790 * ERROR An error occurred while gn was being created
791 * ABORTED gn was not remade because one of its inferiors
792 * could not be made due to errors.
794 if (gn->made == MADE) {
795 if (mtime != Dir_MTime(gn)) {
797 ("%s updated (%d -> %d).\n",
798 gn->name, mtime, gn->mtime));
802 ("%s not updated: skipping restart.\n",
805 } else if (gn->made == ERROR)
807 else if (gn->made == ABORTED) {
808 printf("`%s' not remade because of errors.\n",
811 } else if (gn->made == UPTODATE) {
812 Lst_EnQueue(&cleanup, gn);
817 Fatal("Failed to remake Makefiles.");
818 if (remade_cnt > 0) {
819 DEBUGF(MAKE, ("Restarting `%s'.\n", save_argv[0]));
822 * Some of makefiles were remade -- restart from clean state
824 if (save_makeflags != NULL)
825 setenv("MAKEFLAGS", save_makeflags, 1);
827 unsetenv("MAKEFLAGS");
828 if (execvp(save_argv[0], save_argv) < 0) {
829 Fatal("Can't restart `%s': %s.",
830 save_argv[0], strerror(errno));
834 while (!Lst_IsEmpty(&cleanup)) {
835 GNode *gn = Lst_DeQueue(&cleanup);
840 gn->childMade = FALSE;
841 gn->mtime = gn->cmtime = 0;
842 gn->cmtime_gn = NULL;
844 LST_FOREACH(ln, &gn->children) {
845 GNode *cgn = Lst_Datum(ln);
848 Lst_EnQueue(&cleanup, cgn);
852 if (curdir != objdir) {
853 if (chdir(objdir) < 0)
854 Fatal("Failed to change directory to %s.", objdir);
860 * The main function, for obvious reasons. Initializes variables
861 * and a few modules, then parses the arguments give it in the
862 * environment and on the command line. Reads the system makefile
863 * followed by either Makefile, makefile or the file given by the
864 * -f argument. Sets the .MAKEFLAGS PMake variable based on all the
865 * flags it has received by then uses either the Make or the Compat
866 * module to create the initial list of targets.
869 * If -q was given, exits -1 if anything was out-of-date. Else it exits
873 * The program exits when done. Targets are created. etc. etc. etc.
876 main(int argc, char **argv)
879 const char *machine_arch;
880 const char *machine_cpu;
881 Boolean outOfDate = TRUE; /* FALSE if all targets up to date */
885 char mdpath[MAXPATHLEN];
886 char obpath[MAXPATHLEN];
887 char cdpath[MAXPATHLEN];
888 char found_dir[MAXPATHLEN + 1]; /* for searching for sys.mk */
889 char *cp = NULL, *start;
892 save_makeflags = getenv("MAKEFLAGS");
893 if (save_makeflags != NULL)
894 save_makeflags = estrdup(save_makeflags);
897 * Initialize file global variables.
900 noBuiltins = FALSE; /* Read the built-in rules */
901 forceJobs = FALSE; /* No -j flag */
905 * Initialize program global variables.
907 beSilent = FALSE; /* Print commands as executed */
908 ignoreErrors = FALSE; /* Pay attention to non-zero returns */
909 noExecute = FALSE; /* Execute all commands */
910 printGraphOnly = FALSE; /* Don't stop after printing graph */
911 keepgoing = FALSE; /* Stop on error */
912 allPrecious = FALSE; /* Remove targets when interrupted */
913 queryFlag = FALSE; /* This is not just a check-run */
914 touchFlag = FALSE; /* Actually update targets */
915 usePipes = TRUE; /* Catch child output in pipes */
916 debug = 0; /* No debug verbosity, please. */
919 jobLimit = DEFMAXJOBS;
920 compatMake = FALSE; /* No compat mode */
926 * get rid of resource limit on file descriptors
930 if (getrlimit(RLIMIT_NOFILE, &rl) == -1) {
933 rl.rlim_cur = rl.rlim_max;
934 if (setrlimit(RLIMIT_NOFILE, &rl) == -1) {
941 * Get the name of this type of MACHINE from utsname
942 * so we can share an executable for similar machines.
943 * (i.e. m68k: amiga hp300, mac68k, sun3, ...)
945 * Note that both MACHINE and MACHINE_ARCH are decided at
948 if ((machine = getenv("MACHINE")) == NULL) {
949 static struct utsname utsname;
952 if (uname(&utsname) == -1)
954 machine = utsname.machine;
956 /* Canonicalize non-FreeBSD naming conventions */
957 for (i = 0; i < sizeof(arch_aliases)
958 / sizeof(arch_aliases[0]); i++)
959 if (!strcmp(machine, arch_aliases[i].foreign_name)) {
960 machine = arch_aliases[i].freebsd_name;
965 if ((machine_arch = getenv("MACHINE_ARCH")) == NULL) {
967 machine_arch = MACHINE_ARCH;
969 machine_arch = "unknown";
974 * Set machine_cpu to the minimum supported CPU revision based
975 * on the target architecture, if not already set.
977 if ((machine_cpu = getenv("MACHINE_CPU")) == NULL) {
978 if (!strcmp(machine_arch, "i386"))
979 machine_cpu = "i486";
981 machine_cpu = "unknown";
985 * Initialize the parsing, directory and variable modules to prepare
986 * for the reading of inclusion paths and variable settings on the
991 Dir_Init(); /* Initialize directory structures so -I flags
992 * can be processed correctly */
993 Var_Init(environ); /* As well as the lists of variables for
994 * parsing arguments */
997 * Initialize the Shell so that we have a shell for != assignments
998 * on the command line.
1003 * Initialize various variables.
1004 * MAKE also gets this name, for compatibility
1005 * .MAKEFLAGS gets set to the empty string just in case.
1006 * MFLAGS also gets initialized empty, for compatibility.
1008 Var_SetGlobal("MAKE", argv[0]);
1009 Var_SetGlobal(".MAKEFLAGS", "");
1010 Var_SetGlobal("MFLAGS", "");
1011 Var_SetGlobal("MACHINE", machine);
1012 Var_SetGlobal("MACHINE_ARCH", machine_arch);
1013 Var_SetGlobal("MACHINE_CPU", machine_cpu);
1015 Var_SetGlobal("MAKE_VERSION", MAKE_VERSION);
1017 Var_SetGlobal(".newline", "\n"); /* handy for :@ loops */
1021 snprintf(tmp, sizeof(tmp), "%u", getpid());
1022 Var_SetGlobal(".MAKE.PID", tmp);
1023 snprintf(tmp, sizeof(tmp), "%u", getppid());
1024 Var_SetGlobal(".MAKE.PPID", tmp);
1029 * Find where we are...
1031 if (getcwd(curdir, MAXPATHLEN) == NULL)
1035 * First snag things out of the MAKEFLAGS environment
1036 * variable. Then parse the command line arguments.
1038 Main_ParseArgLine(getenv("MAKEFLAGS"), 1);
1040 MainParseArgs(argc, argv);
1043 * Verify that cwd is sane (after -C may have changed it).
1048 if (stat(curdir, &sa) == -1)
1049 err(2, "%s", curdir);
1053 * The object directory location is determined using the
1054 * following order of preference:
1056 * 1. MAKEOBJDIRPREFIX`cwd`
1058 * 3. PATH_OBJDIR.${MACHINE}
1060 * 5. PATH_OBJDIRPREFIX`cwd`
1062 * If one of the first two fails, use the current directory.
1063 * If the remaining three all fail, use the current directory.
1065 * Once things are initted,
1066 * have to add the original directory to the search path,
1067 * and modify the paths for the Makefiles appropriately. The
1068 * current directory is also placed as a variable for make scripts.
1070 if (!(pathp = getenv("MAKEOBJDIRPREFIX"))) {
1071 if (!(path = getenv("MAKEOBJDIR"))) {
1073 pathp = PATH_OBJDIRPREFIX;
1074 snprintf(mdpath, MAXPATHLEN, "%s.%s", path, machine);
1075 if (!(objdir = chdir_verify_path(mdpath, obpath)))
1076 if (!(objdir=chdir_verify_path(path, obpath))) {
1077 snprintf(mdpath, MAXPATHLEN,
1078 "%s%s", pathp, curdir);
1079 if (!(objdir=chdir_verify_path(mdpath,
1084 else if (!(objdir = chdir_verify_path(path, obpath)))
1088 snprintf(mdpath, MAXPATHLEN, "%s%s", pathp, curdir);
1089 if (!(objdir = chdir_verify_path(mdpath, obpath)))
1092 Dir_InitDot(); /* Initialize the "." directory */
1093 if (objdir != curdir)
1094 Path_AddDir(&dirSearchPath, curdir);
1095 Var_SetGlobal(".ST_EXPORTVAR", "YES");
1096 Var_SetGlobal(".CURDIR", curdir);
1097 Var_SetGlobal(".OBJDIR", objdir);
1099 if (getenv("MAKE_JOBS_FIFO") != NULL)
1102 * Be compatible if user did not specify -j and did not explicitly
1103 * turned compatibility on
1105 if (!compatMake && !forceJobs)
1109 * Initialize target and suffix modules in preparation for
1110 * parsing the makefile(s)
1119 * Set up the .TARGETS variable to contain the list of targets to be
1120 * created. If none specified, make the variable empty -- the parser
1121 * will fill the thing in with the default or .MAIN target.
1123 if (Lst_IsEmpty(&create)) {
1124 Var_SetGlobal(".TARGETS", "");
1128 for (ln = Lst_First(&create); ln != NULL; ln = Lst_Succ(ln)) {
1129 char *name = Lst_Datum(ln);
1131 Var_Append(".TARGETS", name, VAR_GLOBAL);
1137 * If no user-supplied system path was given (through the -m option)
1138 * add the directories from the DEFSYSPATH (more than one may be given
1139 * as dir1:...:dirn) to the system include path.
1141 if (TAILQ_EMPTY(&sysIncPath)) {
1142 char defsyspath[] = PATH_DEFSYSPATH;
1143 char *syspath = getenv("MAKESYSPATH");
1146 * If no user-supplied system path was given (thru -m option)
1147 * add the directories from the DEFSYSPATH (more than one may
1148 * be given as dir1:...:dirn) to the system include path.
1150 if (syspath == NULL || *syspath == '\0')
1151 syspath = defsyspath;
1153 syspath = estrdup(syspath);
1155 for (start = syspath; *start != '\0'; start = cp) {
1156 for (cp = start; *cp != '\0' && *cp != ':'; cp++)
1161 /* look for magic parent directory search string */
1162 if (strncmp(".../", start, 4) == 0) {
1163 if (Dir_FindHereOrAbove(curdir, start + 4,
1164 found_dir, sizeof(found_dir))) {
1165 Path_AddDir(&sysIncPath, found_dir);
1168 Path_AddDir(&sysIncPath, start);
1171 if (syspath != defsyspath)
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)