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 * @(#) Copyright (c) 1988, 1989, 1990, 1993 The Regents of the University of California. All rights reserved.
39 * @(#)main.c 8.3 (Berkeley) 3/19/94
40 * $FreeBSD: src/usr.bin/make/main.c,v 1.35.2.10 2003/12/16 08:34:11 des Exp $
41 * $DragonFly: src/usr.bin/make/main.c,v 1.14 2004/11/13 07:25:17 dillon Exp $
46 * The main file for this entire program. Exit routines etc
49 * Utility functions defined in this file:
50 * Main_ParseArgLine Takes a line of arguments, breaks them and
51 * treats them as if they were given when first
52 * invoked. Used by the parse module to implement
56 #include <sys/types.h>
58 #include <sys/param.h>
59 #include <sys/resource.h>
60 #include <sys/signal.h>
63 #include <sys/sysctl.h>
66 #include <sys/utsname.h>
85 #include "pathnames.h"
88 #define DEFMAXLOCAL DEFMAXJOBS
89 #endif /* DEFMAXLOCAL */
91 #define MAKEFLAGS ".MAKEFLAGS"
93 Lst create; /* Targets to be made */
94 time_t now; /* Time at start of make */
95 GNode *DEFAULT; /* .DEFAULT node */
96 Boolean allPrecious; /* .PRECIOUS given on line by itself */
98 static Boolean noBuiltins; /* -r flag */
99 static Lst makefiles; /* ordered list of makefiles to read */
100 static Boolean printVars; /* print value of one or more vars */
101 static Boolean expandVars; /* fully expand printed variables */
102 static Lst variables; /* list of variables to print */
103 int maxJobs; /* -j argument */
104 static Boolean forceJobs; /* -j argument given */
105 static int maxLocal; /* -L argument */
106 Boolean compatMake; /* -B argument */
107 Boolean debug; /* -d flag */
108 Boolean noExecute; /* -n flag */
109 Boolean keepgoing; /* -k flag */
110 Boolean queryFlag; /* -q flag */
111 Boolean touchFlag; /* -t flag */
112 Boolean usePipes; /* !-P flag */
113 Boolean ignoreErrors; /* -i flag */
114 Boolean beSilent; /* -s flag */
115 Boolean beVerbose; /* -v flag */
116 Boolean oldVars; /* variable substitution style */
117 Boolean checkEnvFirst; /* -e flag */
118 Lst envFirstVars; /* (-E) vars to override from env */
119 Boolean jobsRunning; /* TRUE if the jobs might be running */
121 static void MainParseArgs(int, char **);
122 char * chdir_verify_path(char *, char *);
123 static int ReadMakefile(void *, void *);
124 static void usage(void);
125 static void catch_child(int sig __unused);
127 static char *curdir; /* startup directory */
128 static char *objdir; /* where we chdir'ed to */
133 * Parse a given argument vector. Called from main() and from
134 * Main_ParseArgLine() when the .MAKEFLAGS target is used.
136 * XXX: Deal with command line overriding .MAKEFLAGS in makefile
142 * Various global and local flags will be set depending on the flags
146 MainParseArgs(int argc, char **argv)
153 optind = 1; /* since we're called more than once */
155 # define OPTFLAGS "BC:D:E:I:L:PSV:Xd:ef:ij:km:nqrstv"
157 # define OPTFLAGS "BC:D:E:I:PSV:Xd:ef:ij:km:nqrstv"
159 rearg: while((c = getopt(argc, argv, OPTFLAGS)) != -1) {
162 if (chdir(optarg) == -1)
163 err(1, "chdir %s", optarg);
166 Var_Set(optarg, "1", VAR_GLOBAL);
167 Var_Append(MAKEFLAGS, "-D", VAR_GLOBAL);
168 Var_Append(MAKEFLAGS, optarg, VAR_GLOBAL);
171 Parse_AddIncludeDir(optarg);
172 Var_Append(MAKEFLAGS, "-I", VAR_GLOBAL);
173 Var_Append(MAKEFLAGS, optarg, VAR_GLOBAL);
177 (void)Lst_AtEnd(variables, (void *)optarg);
178 Var_Append(MAKEFLAGS, "-V", VAR_GLOBAL);
179 Var_Append(MAKEFLAGS, optarg, VAR_GLOBAL);
186 Var_Append(MAKEFLAGS, "-B", VAR_GLOBAL);
192 maxLocal = strtol(optarg, &endptr, 10);
193 if (maxLocal < 0 || *endptr != '\0') {
194 warnx("illegal number, -L argument -- %s",
198 Var_Append(MAKEFLAGS, "-L", VAR_GLOBAL);
199 Var_Append(MAKEFLAGS, optarg, VAR_GLOBAL);
205 Var_Append(MAKEFLAGS, "-P", VAR_GLOBAL);
209 Var_Append(MAKEFLAGS, "-S", VAR_GLOBAL);
212 char *modules = optarg;
214 for (; *modules; ++modules)
232 if (modules[1] == '1') {
233 debug |= DEBUG_GRAPH1;
236 else if (modules[1] == '2') {
237 debug |= DEBUG_GRAPH2;
260 warnx("illegal argument to d option -- %c", *modules);
263 Var_Append(MAKEFLAGS, "-d", VAR_GLOBAL);
264 Var_Append(MAKEFLAGS, optarg, VAR_GLOBAL);
268 p = malloc(strlen(optarg) + 1);
270 Punt("make: cannot allocate memory.");
271 (void)strcpy(p, optarg);
272 (void)Lst_AtEnd(envFirstVars, (void *)p);
273 Var_Append(MAKEFLAGS, "-E", VAR_GLOBAL);
274 Var_Append(MAKEFLAGS, optarg, VAR_GLOBAL);
277 checkEnvFirst = TRUE;
278 Var_Append(MAKEFLAGS, "-e", VAR_GLOBAL);
281 (void)Lst_AtEnd(makefiles, (void *)optarg);
285 Var_Append(MAKEFLAGS, "-i", VAR_GLOBAL);
291 maxJobs = strtol(optarg, &endptr, 10);
292 if (maxJobs <= 0 || *endptr != '\0') {
293 warnx("illegal number, -j argument -- %s",
300 Var_Append(MAKEFLAGS, "-j", VAR_GLOBAL);
301 Var_Append(MAKEFLAGS, optarg, VAR_GLOBAL);
306 Var_Append(MAKEFLAGS, "-k", VAR_GLOBAL);
309 Dir_AddDir(sysIncPath, optarg);
310 Var_Append(MAKEFLAGS, "-m", VAR_GLOBAL);
311 Var_Append(MAKEFLAGS, optarg, VAR_GLOBAL);
315 Var_Append(MAKEFLAGS, "-n", VAR_GLOBAL);
319 /* Kind of nonsensical, wot? */
320 Var_Append(MAKEFLAGS, "-q", VAR_GLOBAL);
324 Var_Append(MAKEFLAGS, "-r", VAR_GLOBAL);
328 Var_Append(MAKEFLAGS, "-s", VAR_GLOBAL);
332 Var_Append(MAKEFLAGS, "-t", VAR_GLOBAL);
336 Var_Append(MAKEFLAGS, "-v", VAR_GLOBAL);
347 * See if the rest of the arguments are variable assignments and
348 * perform them if so. Else take them to be targets and stuff them
349 * on the end of the "create" list.
351 for (argv += optind, argc -= optind; *argv; ++argv, --argc)
352 if (Parse_IsVar(*argv))
353 Parse_DoVar(*argv, VAR_CMD);
356 Punt("illegal (null) argument.");
359 optind = 0; /* -flag... */
364 (void)Lst_AtEnd(create, (void *)estrdup(*argv));
369 * Main_ParseArgLine --
370 * Used by the parse module when a .MFLAGS or .MAKEFLAGS target
371 * is encountered and by main() when reading the .MAKEFLAGS envariable.
372 * Takes a line of arguments and breaks it into its
373 * component words and passes those words and the number of them to the
374 * MainParseArgs function.
375 * The line should have all its leading whitespace removed.
381 * Only those that come from the various arguments.
384 Main_ParseArgLine(char *line)
386 char **argv; /* Manufactured argument vector */
387 int argc; /* Number of arguments in argv */
391 for (; *line == ' '; ++line)
396 argv = brk_string(line, &argc, TRUE);
397 MainParseArgs(argc, argv);
401 chdir_verify_path(char *path, char *obpath)
405 if (stat(path, &sb) == 0 && S_ISDIR(sb.st_mode)) {
407 warn("warning: %s", path);
411 if (path[0] != '/') {
412 (void) snprintf(obpath, MAXPATHLEN, "%s/%s",
427 * The main function, for obvious reasons. Initializes variables
428 * and a few modules, then parses the arguments give it in the
429 * environment and on the command line. Reads the system makefile
430 * followed by either Makefile, makefile or the file given by the
431 * -f argument. Sets the .MAKEFLAGS PMake variable based on all the
432 * flags it has received by then uses either the Make or the Compat
433 * module to create the initial list of targets.
436 * If -q was given, exits -1 if anything was out-of-date. Else it exits
440 * The program exits when done. Targets are created. etc. etc. etc.
443 main(int argc, char **argv)
445 Lst targs; /* target nodes to create -- passed to Make_Init */
446 Boolean outOfDate = TRUE; /* FALSE if all targets up to date */
448 char *p, *p1, *path, *pathp;
453 char mdpath[MAXPATHLEN + 1];
454 char obpath[MAXPATHLEN + 1];
455 char cdpath[MAXPATHLEN + 1];
456 char *machine = getenv("MACHINE");
457 char *machine_arch = getenv("MACHINE_ARCH");
458 char *machine_cpu = getenv("MACHINE_CPU");
459 Lst sysMkPath; /* Path of sys.mk */
460 char *cp = NULL, *start;
461 /* avoid faults on read-only strings */
462 static char syspath[] = _PATH_DEFSYSPATH;
466 * Catch SIGCHLD so that we get kicked out of select() when we
467 * need to look at a child. This is only known to matter for the
468 * -j case (perhaps without -P).
470 * XXX this is intentionally misplaced.
474 sigemptyset(&sa.sa_mask);
475 sa.sa_flags = SA_RESTART | SA_NOCLDSTOP;
476 sa.sa_handler = catch_child;
477 sigaction(SIGCHLD, &sa, NULL);
482 * get rid of resource limit on file descriptors
486 if (getrlimit(RLIMIT_NOFILE, &rl) != -1 &&
487 rl.rlim_cur != rl.rlim_max) {
488 rl.rlim_cur = rl.rlim_max;
489 (void) setrlimit(RLIMIT_NOFILE, &rl);
494 * Find where we are and take care of PWD for the automounter...
495 * All this code is so that we know where we are when we start up
496 * on a different machine with pmake.
499 if (getcwd(curdir, MAXPATHLEN) == NULL)
502 if (stat(curdir, &sa) == -1)
503 err(2, "%s", curdir);
506 if ((pwd = getenv("PWD")) != NULL) {
507 if (stat(pwd, &sb) == 0 && sa.st_ino == sb.st_ino &&
508 sa.st_dev == sb.st_dev)
509 (void) strcpy(curdir, pwd);
513 #if defined(__i386__) && defined(__DragonFly_version)
515 * PC-98 kernel sets the `i386' string to the utsname.machine and
516 * it cannot be distinguished from IBM-PC by uname(3). Therefore,
517 * we check machine.ispc98 and adjust the machine variable before
518 * using usname(3) below.
519 * NOTE: machdep.ispc98 was defined on 1998/8/31.
525 len = sizeof(ispc98);
526 if (!sysctlbyname("machdep.ispc98", &ispc98, &len, NULL, 0)) {
534 * Get the name of this type of MACHINE from utsname
535 * so we can share an executable for similar machines.
536 * (i.e. m68k: amiga hp300, mac68k, sun3, ...)
538 * Note that while MACHINE is decided at run-time,
539 * MACHINE_ARCH is always known at compile time.
543 struct utsname utsname;
545 if (uname(&utsname) == -1)
547 machine = utsname.machine;
555 machine_arch = "unknown";
557 machine_arch = MACHINE_ARCH;
562 * Set machine_cpu to the minumum supported CPU revision based
563 * on the target architecture, if not already set.
566 if (!strcmp(machine_arch, "i386"))
567 machine_cpu = "i386";
568 else if (!strcmp(machine_arch, "alpha"))
571 machine_cpu = "unknown";
575 * The object directory location is determined using the
576 * following order of preference:
578 * 1. MAKEOBJDIRPREFIX`cwd`
580 * 3. _PATH_OBJDIR.${MACHINE}
582 * 5. _PATH_OBJDIRPREFIX`cwd`
584 * If one of the first two fails, use the current directory.
585 * If the remaining three all fail, use the current directory.
587 * Once things are initted,
588 * have to add the original directory to the search path,
589 * and modify the paths for the Makefiles apropriately. The
590 * current directory is also placed as a variable for make scripts.
592 if (!(pathp = getenv("MAKEOBJDIRPREFIX"))) {
593 if (!(path = getenv("MAKEOBJDIR"))) {
595 pathp = _PATH_OBJDIRPREFIX;
596 (void) snprintf(mdpath, MAXPATHLEN, "%s.%s",
598 if (!(objdir = chdir_verify_path(mdpath, obpath)))
599 if (!(objdir=chdir_verify_path(path, obpath))) {
600 (void) snprintf(mdpath, MAXPATHLEN,
601 "%s%s", pathp, curdir);
602 if (!(objdir=chdir_verify_path(mdpath,
607 else if (!(objdir = chdir_verify_path(path, obpath)))
611 (void) snprintf(mdpath, MAXPATHLEN, "%s%s", pathp, curdir);
612 if (!(objdir = chdir_verify_path(mdpath, obpath)))
617 setenv("PWD", objdir, 1);
620 create = Lst_Init(FALSE);
621 makefiles = Lst_Init(FALSE);
622 envFirstVars = Lst_Init(FALSE);
625 variables = Lst_Init(FALSE);
626 beSilent = FALSE; /* Print commands as executed */
627 ignoreErrors = FALSE; /* Pay attention to non-zero returns */
628 noExecute = FALSE; /* Execute all commands */
629 keepgoing = FALSE; /* Stop on error */
630 allPrecious = FALSE; /* Remove targets when interrupted */
631 queryFlag = FALSE; /* This is not just a check-run */
632 noBuiltins = FALSE; /* Read the built-in rules */
633 touchFlag = FALSE; /* Actually update targets */
634 usePipes = TRUE; /* Catch child output in pipes */
635 debug = 0; /* No debug verbosity, please. */
638 maxLocal = DEFMAXLOCAL; /* Set default local max concurrency */
640 maxJobs = DEFMAXJOBS; /* Set default max concurrency */
644 forceJobs = FALSE; /* No -j flag */
645 compatMake = FALSE; /* No compat mode */
649 * Initialize the parsing, directory and variable modules to prepare
650 * for the reading of inclusion paths and variable settings on the
653 Dir_Init(); /* Initialize directory structures so -I flags
654 * can be processed correctly */
655 Parse_Init(); /* Need to initialize the paths of #include
657 Var_Init(); /* As well as the lists of variables for
658 * parsing arguments */
660 if (objdir != curdir)
661 Dir_AddDir(dirSearchPath, curdir);
662 Var_Set(".DIRECTIVE_MAKEENV", "YES", VAR_GLOBAL);
663 Var_Set(".CURDIR", curdir, VAR_GLOBAL);
664 Var_Set(".OBJDIR", objdir, VAR_GLOBAL);
667 * Initialize various variables.
668 * MAKE also gets this name, for compatibility
669 * .MAKEFLAGS gets set to the empty string just in case.
670 * MFLAGS also gets initialized empty, for compatibility.
672 Var_Set("MAKE", argv[0], VAR_GLOBAL);
673 Var_Set(MAKEFLAGS, "", VAR_GLOBAL);
674 Var_Set("MFLAGS", "", VAR_GLOBAL);
675 Var_Set("MACHINE", machine, VAR_GLOBAL);
676 Var_Set("MACHINE_ARCH", machine_arch, VAR_GLOBAL);
677 Var_Set("MACHINE_CPU", machine_cpu, VAR_GLOBAL);
680 * First snag any flags out of the MAKE environment variable.
681 * (Note this is *not* MAKEFLAGS since /bin/make uses that and it's
682 * in a different format).
685 Main_ParseArgLine(getenv("MAKEFLAGS"));
687 Main_ParseArgLine(getenv("MAKE"));
690 MainParseArgs(argc, argv);
693 * Be compatible if user did not specify -j and did not explicitly
694 * turned compatibility on
696 if (!compatMake && !forceJobs)
700 * Initialize archive, target and suffix modules in preparation for
701 * parsing the makefile(s)
711 * Set up the .TARGETS variable to contain the list of targets to be
712 * created. If none specified, make the variable empty -- the parser
713 * will fill the thing in with the default or .MAIN target.
715 if (!Lst_IsEmpty(create)) {
718 for (ln = Lst_First(create); ln != NULL;
720 char *name = (char *)Lst_Datum(ln);
722 Var_Append(".TARGETS", name, VAR_GLOBAL);
725 Var_Set(".TARGETS", "", VAR_GLOBAL);
729 * If no user-supplied system path was given (through the -m option)
730 * add the directories from the DEFSYSPATH (more than one may be given
731 * as dir1:...:dirn) to the system include path.
733 if (Lst_IsEmpty(sysIncPath)) {
734 for (start = syspath; *start != '\0'; start = cp) {
735 for (cp = start; *cp != '\0' && *cp != ':'; cp++)
738 Dir_AddDir(sysIncPath, start);
741 Dir_AddDir(sysIncPath, start);
747 * Read in the built-in rules first, followed by the specified
748 * makefile, if it was (makefile != (char *) NULL), or the default
749 * Makefile and makefile, in that order, if it wasn't.
754 sysMkPath = Lst_Init (FALSE);
755 Dir_Expand (_PATH_DEFSYSMK, sysIncPath, sysMkPath);
756 if (Lst_IsEmpty(sysMkPath))
757 Fatal("make: no system rules (%s).", _PATH_DEFSYSMK);
758 ln = Lst_Find(sysMkPath, (void *)NULL, ReadMakefile);
760 Fatal("make: cannot open %s.", (char *)Lst_Datum(ln));
763 if (!Lst_IsEmpty(makefiles)) {
766 ln = Lst_Find(makefiles, (void *)NULL, ReadMakefile);
768 Fatal("make: cannot open %s.", (char *)Lst_Datum(ln));
769 } else if (!ReadMakefile("BSDmakefile", NULL))
770 if (!ReadMakefile("makefile", NULL))
771 (void)ReadMakefile("Makefile", NULL);
773 (void)ReadMakefile(".depend", NULL);
775 Var_Append("MFLAGS", Var_Value(MAKEFLAGS, VAR_GLOBAL, &p1), VAR_GLOBAL);
778 /* Install all the flags into the MAKE envariable. */
779 if (((p = Var_Value(MAKEFLAGS, VAR_GLOBAL, &p1)) != NULL) && *p)
781 setenv("MAKEFLAGS", p, 1);
783 setenv("MAKE", p, 1);
788 * For compatibility, look at the directories in the VPATH variable
789 * and add them to the search path, if the variable is defined. The
790 * variable's value is in the same format as the PATH envariable, i.e.
791 * <directory>:<directory>:<directory>...
793 if (Var_Exists("VPATH", VAR_CMD)) {
796 * GCC stores string constants in read-only memory, but
797 * Var_Subst will want to write this thing, so store it
800 static char VPATH[] = "${VPATH}";
802 vpath = Var_Subst(NULL, VPATH, VAR_CMD, FALSE);
805 /* skip to end of directory */
806 for (cp = path; *cp != ':' && *cp != '\0'; cp++)
808 /* Save terminator character so know when to stop */
811 /* Add directory to search path */
812 Dir_AddDir(dirSearchPath, path);
815 } while (savec == ':');
820 * Now that all search paths have been read for suffixes et al, it's
821 * time to add the default search path to their lists...
825 /* print the initial graph, if the user requested it */
829 /* print the values of any variables requested by the user */
833 for (ln = Lst_First(variables); ln != NULL;
837 p1 = malloc(strlen((char *)Lst_Datum(ln)) + 1 + 3);
839 Punt("make: cannot allocate memory.");
840 /* This sprintf is safe, because of the malloc above */
841 (void)sprintf(p1, "${%s}", (char *)Lst_Datum(ln));
842 value = Var_Subst(NULL, p1, VAR_GLOBAL, FALSE);
844 value = Var_Value((char *)Lst_Datum(ln),
847 printf("%s\n", value ? value : "");
854 * Have now read the entire graph and need to make a list of targets
855 * to create. If none was given on the command line, we consult the
856 * parsing module to find the main target(s) to create.
858 if (Lst_IsEmpty(create))
859 targs = Parse_MainName();
861 targs = Targ_FindList(create, TARG_CREATE);
863 if (!compatMake && !printVars) {
865 * Initialize job module before traversing the graph, now that
866 * any .BEGIN and .END targets have been read. This is done
867 * only if the -q flag wasn't given (to prevent the .BEGIN from
868 * being executed should it exist).
873 Job_Init(maxJobs, maxLocal);
877 /* Traverse the graph, checking on all the targets */
878 outOfDate = Make_Run(targs);
879 } else if (!printVars) {
881 * Compat_Init will take care of creating all the targets as
882 * well as initializing the module.
887 Lst_Destroy(targs, NOFREE);
888 Lst_Destroy(variables, NOFREE);
889 Lst_Destroy(makefiles, NOFREE);
890 Lst_Destroy(create, (void (*) (void *)) free);
892 /* print the graph now it's been processed if the user requested it */
904 if (queryFlag && outOfDate)
912 * Open and parse the given makefile.
915 * TRUE if ok. FALSE if couldn't open file.
921 ReadMakefile(void *p, void *q __unused)
923 char *fname; /* makefile to read */
925 char *name, path[MAXPATHLEN + 1];
931 if (!strcmp(fname, "-")) {
932 Parse_File("(stdin)", stdin);
933 Var_Set("MAKEFILE", "", VAR_GLOBAL);
935 setMAKEFILE = strcmp(fname, ".depend");
937 /* if we've chdir'd, rebuild the path name */
938 if (curdir != objdir && *fname != '/') {
939 (void)snprintf(path, MAXPATHLEN, "%s/%s", curdir, fname);
941 * XXX The realpath stuff breaks relative includes
942 * XXX in some cases. The problem likely is in
943 * XXX parse.c where it does special things in
944 * XXX ParseDoInclude if the file is relateive
945 * XXX or absolute and not a system file. There
946 * XXX it assumes that if the current file that's
947 * XXX being included is absolute, that any files
948 * XXX that it includes shouldn't do the -I path
949 * XXX stuff, which is inconsistant with historical
950 * XXX behavior. However, I can't pentrate the mists
951 * XXX further, so I'm putting this workaround in
952 * XXX here until such time as the underlying bug
955 #if THIS_BREAKS_THINGS
956 if (realpath(path, path) != NULL &&
957 (stream = fopen(path, "r")) != NULL) {
962 } else if (realpath(fname, path) != NULL) {
965 if ((stream = fopen(fname, "r")) != NULL)
969 if ((stream = fopen(path, "r")) != NULL) {
976 if ((stream = fopen(fname, "r")) != NULL)
980 /* look in -I and system include directories. */
981 name = Dir_FindFile(fname, parseIncPath);
983 name = Dir_FindFile(fname, sysIncPath);
984 if (!name || !(stream = fopen(name, "r")))
986 MAKEFILE = fname = name;
988 * set the MAKEFILE variable desired by System V fans -- the
989 * placement of the setting here means it gets set to the last
990 * makefile specified, as it is set by SysV make.
994 Var_Set("MAKEFILE", MAKEFILE, VAR_GLOBAL);
995 Parse_File(fname, stream);
996 (void)fclose(stream);
1003 * Execute the command in cmd, and return the output of that command
1007 * A string containing the output of the command, or the empty string
1008 * If err is not NULL, it contains the reason for the command failure
1011 * The string must be freed by the caller.
1014 Cmd_Exec(char *cmd, char **error)
1016 char *args[4]; /* Args for invoking the shell */
1017 int fds[2]; /* Pipe streams */
1018 int cpid; /* Child PID */
1019 int pid; /* PID from wait() */
1020 char *res; /* result */
1021 int status; /* command exit status */
1022 Buffer buf; /* buffer to store the result */
1029 * Set up arguments for shell
1037 * Open a pipe for fetching its output
1039 if (pipe(fds) == -1) {
1040 *error = "Couldn't create pipe for \"%s\"";
1047 switch (cpid = vfork()) {
1050 * Close input side of pipe
1052 (void) close(fds[0]);
1055 * Duplicate the output stream to the shell's output, then
1056 * shut the extra thing down. Note we don't fetch the error
1057 * stream...why not? Why?
1059 (void) dup2(fds[1], 1);
1060 (void) close(fds[1]);
1062 (void) execv("/bin/sh", args);
1067 *error = "Couldn't exec \"%s\"";
1072 * No need for the writing half
1074 (void) close(fds[1]);
1076 buf = Buf_Init (MAKE_BSIZE);
1079 char result[BUFSIZ];
1080 cc = read(fds[0], result, sizeof(result));
1082 Buf_AddBytes(buf, cc, (Byte *) result);
1084 while (cc > 0 || (cc == -1 && errno == EINTR));
1087 * Close the input side of the pipe.
1089 (void) close(fds[0]);
1092 * Wait for the process to exit.
1094 while(((pid = wait(&status)) != cpid) && (pid >= 0))
1098 *error = "Error reading shell's output for \"%s\"";
1100 res = (char *)Buf_GetAll (buf, &cc);
1101 Buf_Destroy (buf, FALSE);
1104 *error = "\"%s\" returned non-zero status";
1107 * Null-terminate the result, convert newlines to spaces and
1108 * install it in the variable.
1115 * A final newline is just stripped
1136 * exit with usage message
1141 (void)fprintf(stderr, "%s\n%s\n%s\n",
1142 "usage: make [-BPSXeiknqrstv] [-C directory] [-D variable] [-d flags]",
1143 " [-E variable] [-f makefile] [-I directory] [-j max_jobs]",
1144 " [-m directory] [-V variable] [variable=value] [target ...]");
1150 catch_child(int sig __unused)