main.c revision 94595
11590Srgrimes/* 21590Srgrimes * Copyright (c) 1988, 1989, 1990, 1993 31590Srgrimes * The Regents of the University of California. All rights reserved. 41590Srgrimes * Copyright (c) 1989 by Berkeley Softworks 51590Srgrimes * All rights reserved. 61590Srgrimes * 71590Srgrimes * This code is derived from software contributed to Berkeley by 81590Srgrimes * Adam de Boor. 91590Srgrimes * 101590Srgrimes * Redistribution and use in source and binary forms, with or without 111590Srgrimes * modification, are permitted provided that the following conditions 121590Srgrimes * are met: 131590Srgrimes * 1. Redistributions of source code must retain the above copyright 141590Srgrimes * notice, this list of conditions and the following disclaimer. 151590Srgrimes * 2. Redistributions in binary form must reproduce the above copyright 161590Srgrimes * notice, this list of conditions and the following disclaimer in the 171590Srgrimes * documentation and/or other materials provided with the distribution. 181590Srgrimes * 3. All advertising materials mentioning features or use of this software 191590Srgrimes * must display the following acknowledgement: 201590Srgrimes * This product includes software developed by the University of 211590Srgrimes * California, Berkeley and its contributors. 221590Srgrimes * 4. Neither the name of the University nor the names of its contributors 231590Srgrimes * may be used to endorse or promote products derived from this software 241590Srgrimes * without specific prior written permission. 251590Srgrimes * 261590Srgrimes * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND 271590Srgrimes * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 281590Srgrimes * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 291590Srgrimes * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE 301590Srgrimes * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 311590Srgrimes * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 321590Srgrimes * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 331590Srgrimes * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 341590Srgrimes * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 351590Srgrimes * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 361590Srgrimes * SUCH DAMAGE. 3762833Swsanchez * 3862833Swsanchez * @(#)main.c 8.3 (Berkeley) 3/19/94 391590Srgrimes */ 401590Srgrimes 411590Srgrimes#ifndef lint 4294595Sobrien#if 0 4394589Sobrienstatic char copyright[] = 4494589Sobrien"@(#) Copyright (c) 1988, 1989, 1990, 1993\n\ 4594589Sobrien The Regents of the University of California. All rights reserved.\n"; 4694595Sobrien#endif 471590Srgrimes#endif /* not lint */ 4894589Sobrien#include <sys/cdefs.h> 4994587Sobrien__FBSDID("$FreeBSD: head/usr.bin/make/main.c 94595 2002-04-13 12:20:51Z obrien $"); 501590Srgrimes 511590Srgrimes/*- 521590Srgrimes * main.c -- 531590Srgrimes * The main file for this entire program. Exit routines etc 541590Srgrimes * reside here. 551590Srgrimes * 561590Srgrimes * Utility functions defined in this file: 571590Srgrimes * Main_ParseArgLine Takes a line of arguments, breaks them and 581590Srgrimes * treats them as if they were given when first 591590Srgrimes * invoked. Used by the parse module to implement 601590Srgrimes * the .MFLAGS target. 611590Srgrimes * 621590Srgrimes * Error Print a tagged error message. The global 631590Srgrimes * MAKE variable must have been defined. This 641590Srgrimes * takes a format string and two optional 651590Srgrimes * arguments for it. 661590Srgrimes * 671590Srgrimes * Fatal Print an error message and exit. Also takes 681590Srgrimes * a format string and two arguments. 691590Srgrimes * 701590Srgrimes * Punt Aborts all jobs and exits with a message. Also 711590Srgrimes * takes a format string and two arguments. 721590Srgrimes * 731590Srgrimes * Finish Finish things up by printing the number of 741590Srgrimes * errors which occured, as passed to it, and 751590Srgrimes * exiting. 761590Srgrimes */ 771590Srgrimes 781590Srgrimes#include <sys/types.h> 791590Srgrimes#include <sys/time.h> 801590Srgrimes#include <sys/param.h> 811590Srgrimes#include <sys/resource.h> 821590Srgrimes#include <sys/signal.h> 831590Srgrimes#include <sys/stat.h> 8439006Skato#if defined(__i386__) 8539006Skato#include <sys/sysctl.h> 8639006Skato#endif 8718730Ssteve#ifndef MACHINE 885814Sjkh#include <sys/utsname.h> 8918730Ssteve#endif 9018730Ssteve#include <sys/wait.h> 9127644Scharnier#include <err.h> 9229957Simp#include <stdlib.h> 931590Srgrimes#include <errno.h> 941590Srgrimes#include <fcntl.h> 951590Srgrimes#include <stdio.h> 9649331Shoek#include <sysexits.h> 971590Srgrimes#include <stdarg.h> 9863955Simp#include <unistd.h> 991590Srgrimes#include "make.h" 1001590Srgrimes#include "hash.h" 1011590Srgrimes#include "dir.h" 1021590Srgrimes#include "job.h" 1031590Srgrimes#include "pathnames.h" 1041590Srgrimes 1051590Srgrimes#ifndef DEFMAXLOCAL 1061590Srgrimes#define DEFMAXLOCAL DEFMAXJOBS 10718730Ssteve#endif /* DEFMAXLOCAL */ 1081590Srgrimes 1091590Srgrimes#define MAKEFLAGS ".MAKEFLAGS" 1101590Srgrimes 1111590SrgrimesLst create; /* Targets to be made */ 1121590Srgrimestime_t now; /* Time at start of make */ 1131590SrgrimesGNode *DEFAULT; /* .DEFAULT node */ 1141590SrgrimesBoolean allPrecious; /* .PRECIOUS given on line by itself */ 1151590Srgrimes 1161590Srgrimesstatic Boolean noBuiltins; /* -r flag */ 1171590Srgrimesstatic Lst makefiles; /* ordered list of makefiles to read */ 11817193Sbdestatic Boolean printVars; /* print value of one or more vars */ 11966365Speterstatic Boolean expandVars; /* fully expand printed variables */ 12017193Sbdestatic Lst variables; /* list of variables to print */ 12118730Ssteveint maxJobs; /* -j argument */ 12228228Sfsmpstatic Boolean forceJobs; /* -j argument given */ 1231590Srgrimesstatic int maxLocal; /* -L argument */ 1241590SrgrimesBoolean compatMake; /* -B argument */ 1251590SrgrimesBoolean debug; /* -d flag */ 1261590SrgrimesBoolean noExecute; /* -n flag */ 1271590SrgrimesBoolean keepgoing; /* -k flag */ 1281590SrgrimesBoolean queryFlag; /* -q flag */ 1291590SrgrimesBoolean touchFlag; /* -t flag */ 1301590SrgrimesBoolean usePipes; /* !-P flag */ 1311590SrgrimesBoolean ignoreErrors; /* -i flag */ 1321590SrgrimesBoolean beSilent; /* -s flag */ 13341151SdgBoolean beVerbose; /* -v flag */ 1341590SrgrimesBoolean oldVars; /* variable substitution style */ 1351590SrgrimesBoolean checkEnvFirst; /* -e flag */ 13649332ShoekLst envFirstVars; /* (-E) vars to override from env */ 1371590Srgrimesstatic Boolean jobsRunning; /* TRUE if the jobs might be running */ 1381590Srgrimes 13992921Simpstatic void MainParseArgs(int, char **); 14092921Simpchar * chdir_verify_path(char *, char *); 14192921Simpstatic int ReadMakefile(void *, void *); 14292921Simpstatic void usage(void); 1431590Srgrimes 1441590Srgrimesstatic char *curdir; /* startup directory */ 1451590Srgrimesstatic char *objdir; /* where we chdir'ed to */ 1461590Srgrimes 1471590Srgrimes/*- 1481590Srgrimes * MainParseArgs -- 1491590Srgrimes * Parse a given argument vector. Called from main() and from 1501590Srgrimes * Main_ParseArgLine() when the .MAKEFLAGS target is used. 1511590Srgrimes * 1521590Srgrimes * XXX: Deal with command line overriding .MAKEFLAGS in makefile 1531590Srgrimes * 1541590Srgrimes * Results: 1551590Srgrimes * None 1561590Srgrimes * 1571590Srgrimes * Side Effects: 1581590Srgrimes * Various global and local flags will be set depending on the flags 1591590Srgrimes * given 1601590Srgrimes */ 1611590Srgrimesstatic void 1621590SrgrimesMainParseArgs(argc, argv) 1631590Srgrimes int argc; 1641590Srgrimes char **argv; 1651590Srgrimes{ 16636942Speter char *p; 1675814Sjkh int c; 1681590Srgrimes 1691590Srgrimes optind = 1; /* since we're called more than once */ 17018730Ssteve#ifdef REMOTE 17166365Speter# define OPTFLAGS "BD:E:I:L:PSV:Xd:ef:ij:km:nqrstv" 1721590Srgrimes#else 17366365Speter# define OPTFLAGS "BD:E:I:PSV:Xd:ef:ij:km:nqrstv" 1741590Srgrimes#endif 17524360Simprearg: while((c = getopt(argc, argv, OPTFLAGS)) != -1) { 1761590Srgrimes switch(c) { 1771590Srgrimes case 'D': 1781590Srgrimes Var_Set(optarg, "1", VAR_GLOBAL); 1791590Srgrimes Var_Append(MAKEFLAGS, "-D", VAR_GLOBAL); 1801590Srgrimes Var_Append(MAKEFLAGS, optarg, VAR_GLOBAL); 1811590Srgrimes break; 1821590Srgrimes case 'I': 1831590Srgrimes Parse_AddIncludeDir(optarg); 1841590Srgrimes Var_Append(MAKEFLAGS, "-I", VAR_GLOBAL); 1851590Srgrimes Var_Append(MAKEFLAGS, optarg, VAR_GLOBAL); 1861590Srgrimes break; 18717193Sbde case 'V': 18817193Sbde printVars = TRUE; 18969531Swill (void)Lst_AtEnd(variables, (void *)optarg); 19017193Sbde Var_Append(MAKEFLAGS, "-V", VAR_GLOBAL); 19117193Sbde Var_Append(MAKEFLAGS, optarg, VAR_GLOBAL); 19217193Sbde break; 19366365Speter case 'X': 19466365Speter expandVars = FALSE; 19566365Speter break; 1961590Srgrimes case 'B': 1971590Srgrimes compatMake = TRUE; 19828746Sfsmp Var_Append(MAKEFLAGS, "-B", VAR_GLOBAL); 1991590Srgrimes break; 20018730Ssteve#ifdef REMOTE 20149331Shoek case 'L': { 20249331Shoek char *endptr; 20349331Shoek 20449331Shoek maxLocal = strtol(optarg, &endptr, 10); 20549331Shoek if (maxLocal < 0 || *endptr != '\0') { 20649938Shoek warnx("illegal number, -L argument -- %s", 20749938Shoek optarg); 20849938Shoek usage(); 20949331Shoek } 2101590Srgrimes Var_Append(MAKEFLAGS, "-L", VAR_GLOBAL); 2111590Srgrimes Var_Append(MAKEFLAGS, optarg, VAR_GLOBAL); 2121590Srgrimes break; 21349331Shoek } 21418730Ssteve#endif 2151590Srgrimes case 'P': 2161590Srgrimes usePipes = FALSE; 2171590Srgrimes Var_Append(MAKEFLAGS, "-P", VAR_GLOBAL); 2181590Srgrimes break; 2191590Srgrimes case 'S': 2201590Srgrimes keepgoing = FALSE; 2211590Srgrimes Var_Append(MAKEFLAGS, "-S", VAR_GLOBAL); 2221590Srgrimes break; 2231590Srgrimes case 'd': { 2241590Srgrimes char *modules = optarg; 2251590Srgrimes 2261590Srgrimes for (; *modules; ++modules) 2271590Srgrimes switch (*modules) { 2281590Srgrimes case 'A': 2291590Srgrimes debug = ~0; 2301590Srgrimes break; 2311590Srgrimes case 'a': 2321590Srgrimes debug |= DEBUG_ARCH; 2331590Srgrimes break; 2341590Srgrimes case 'c': 2351590Srgrimes debug |= DEBUG_COND; 2361590Srgrimes break; 2371590Srgrimes case 'd': 2381590Srgrimes debug |= DEBUG_DIR; 2391590Srgrimes break; 2401590Srgrimes case 'f': 2411590Srgrimes debug |= DEBUG_FOR; 2421590Srgrimes break; 2431590Srgrimes case 'g': 2441590Srgrimes if (modules[1] == '1') { 2451590Srgrimes debug |= DEBUG_GRAPH1; 2461590Srgrimes ++modules; 2471590Srgrimes } 2481590Srgrimes else if (modules[1] == '2') { 2491590Srgrimes debug |= DEBUG_GRAPH2; 2501590Srgrimes ++modules; 2511590Srgrimes } 2521590Srgrimes break; 2531590Srgrimes case 'j': 2541590Srgrimes debug |= DEBUG_JOB; 2551590Srgrimes break; 25660569Swill case 'l': 25760569Swill debug |= DEBUG_LOUD; 25860569Swill break; 2591590Srgrimes case 'm': 2601590Srgrimes debug |= DEBUG_MAKE; 2611590Srgrimes break; 2621590Srgrimes case 's': 2631590Srgrimes debug |= DEBUG_SUFF; 2641590Srgrimes break; 2651590Srgrimes case 't': 2661590Srgrimes debug |= DEBUG_TARG; 2671590Srgrimes break; 2681590Srgrimes case 'v': 2691590Srgrimes debug |= DEBUG_VAR; 2701590Srgrimes break; 2711590Srgrimes default: 27227644Scharnier warnx("illegal argument to d option -- %c", *modules); 2731590Srgrimes usage(); 2741590Srgrimes } 2751590Srgrimes Var_Append(MAKEFLAGS, "-d", VAR_GLOBAL); 2761590Srgrimes Var_Append(MAKEFLAGS, optarg, VAR_GLOBAL); 2771590Srgrimes break; 2781590Srgrimes } 27949332Shoek case 'E': 28094506Scharnier p = emalloc(strlen(optarg) + 1); 28149332Shoek (void)strcpy(p, optarg); 28269531Swill (void)Lst_AtEnd(envFirstVars, (void *)p); 28349332Shoek Var_Append(MAKEFLAGS, "-E", VAR_GLOBAL); 28449332Shoek Var_Append(MAKEFLAGS, optarg, VAR_GLOBAL); 28549332Shoek break; 2861590Srgrimes case 'e': 2871590Srgrimes checkEnvFirst = TRUE; 2881590Srgrimes Var_Append(MAKEFLAGS, "-e", VAR_GLOBAL); 2891590Srgrimes break; 2901590Srgrimes case 'f': 29169531Swill (void)Lst_AtEnd(makefiles, (void *)optarg); 2921590Srgrimes break; 2931590Srgrimes case 'i': 2941590Srgrimes ignoreErrors = TRUE; 2951590Srgrimes Var_Append(MAKEFLAGS, "-i", VAR_GLOBAL); 2961590Srgrimes break; 29749331Shoek case 'j': { 29849331Shoek char *endptr; 29949331Shoek 30018730Ssteve forceJobs = TRUE; 30149331Shoek maxJobs = strtol(optarg, &endptr, 10); 30249331Shoek if (maxJobs <= 0 || *endptr != '\0') { 30349938Shoek warnx("illegal number, -j argument -- %s", 30449938Shoek optarg); 30549938Shoek usage(); 30649331Shoek } 30718730Ssteve#ifndef REMOTE 30818730Ssteve maxLocal = maxJobs; 30918730Ssteve#endif 3101590Srgrimes Var_Append(MAKEFLAGS, "-j", VAR_GLOBAL); 3111590Srgrimes Var_Append(MAKEFLAGS, optarg, VAR_GLOBAL); 3121590Srgrimes break; 31349331Shoek } 3141590Srgrimes case 'k': 3151590Srgrimes keepgoing = TRUE; 3161590Srgrimes Var_Append(MAKEFLAGS, "-k", VAR_GLOBAL); 3171590Srgrimes break; 31818730Ssteve case 'm': 31918730Ssteve Dir_AddDir(sysIncPath, optarg); 32018730Ssteve Var_Append(MAKEFLAGS, "-m", VAR_GLOBAL); 32118730Ssteve Var_Append(MAKEFLAGS, optarg, VAR_GLOBAL); 32218730Ssteve break; 3231590Srgrimes case 'n': 3241590Srgrimes noExecute = TRUE; 3251590Srgrimes Var_Append(MAKEFLAGS, "-n", VAR_GLOBAL); 3261590Srgrimes break; 3271590Srgrimes case 'q': 3281590Srgrimes queryFlag = TRUE; 3291590Srgrimes /* Kind of nonsensical, wot? */ 3301590Srgrimes Var_Append(MAKEFLAGS, "-q", VAR_GLOBAL); 3311590Srgrimes break; 3321590Srgrimes case 'r': 3331590Srgrimes noBuiltins = TRUE; 3341590Srgrimes Var_Append(MAKEFLAGS, "-r", VAR_GLOBAL); 3351590Srgrimes break; 3361590Srgrimes case 's': 3371590Srgrimes beSilent = TRUE; 3381590Srgrimes Var_Append(MAKEFLAGS, "-s", VAR_GLOBAL); 3391590Srgrimes break; 3401590Srgrimes case 't': 3411590Srgrimes touchFlag = TRUE; 3421590Srgrimes Var_Append(MAKEFLAGS, "-t", VAR_GLOBAL); 3431590Srgrimes break; 34441151Sdg case 'v': 34541151Sdg beVerbose = TRUE; 34641151Sdg Var_Append(MAKEFLAGS, "-v", VAR_GLOBAL); 34741151Sdg break; 3481590Srgrimes default: 3491590Srgrimes case '?': 3501590Srgrimes usage(); 3511590Srgrimes } 3521590Srgrimes } 3531590Srgrimes 3541590Srgrimes oldVars = TRUE; 3551590Srgrimes 3561590Srgrimes /* 3571590Srgrimes * See if the rest of the arguments are variable assignments and 3581590Srgrimes * perform them if so. Else take them to be targets and stuff them 3591590Srgrimes * on the end of the "create" list. 3601590Srgrimes */ 3611590Srgrimes for (argv += optind, argc -= optind; *argv; ++argv, --argc) 3621590Srgrimes if (Parse_IsVar(*argv)) 3631590Srgrimes Parse_DoVar(*argv, VAR_CMD); 3641590Srgrimes else { 3651590Srgrimes if (!**argv) 3661590Srgrimes Punt("illegal (null) argument."); 3671590Srgrimes if (**argv == '-') { 3681590Srgrimes if ((*argv)[1]) 3691590Srgrimes optind = 0; /* -flag... */ 3701590Srgrimes else 3711590Srgrimes optind = 1; /* - */ 3721590Srgrimes goto rearg; 3731590Srgrimes } 37469531Swill (void)Lst_AtEnd(create, (void *)estrdup(*argv)); 3751590Srgrimes } 3761590Srgrimes} 3771590Srgrimes 3781590Srgrimes/*- 3791590Srgrimes * Main_ParseArgLine -- 3801590Srgrimes * Used by the parse module when a .MFLAGS or .MAKEFLAGS target 3811590Srgrimes * is encountered and by main() when reading the .MAKEFLAGS envariable. 3821590Srgrimes * Takes a line of arguments and breaks it into its 3831590Srgrimes * component words and passes those words and the number of them to the 3841590Srgrimes * MainParseArgs function. 3851590Srgrimes * The line should have all its leading whitespace removed. 3861590Srgrimes * 3871590Srgrimes * Results: 3881590Srgrimes * None 3891590Srgrimes * 3901590Srgrimes * Side Effects: 3911590Srgrimes * Only those that come from the various arguments. 3921590Srgrimes */ 3931590Srgrimesvoid 3941590SrgrimesMain_ParseArgLine(line) 3951590Srgrimes char *line; /* Line to fracture */ 3961590Srgrimes{ 3971590Srgrimes char **argv; /* Manufactured argument vector */ 3981590Srgrimes int argc; /* Number of arguments in argv */ 3991590Srgrimes 4001590Srgrimes if (line == NULL) 4011590Srgrimes return; 4021590Srgrimes for (; *line == ' '; ++line) 4031590Srgrimes continue; 4041590Srgrimes if (!*line) 4051590Srgrimes return; 4061590Srgrimes 4075814Sjkh argv = brk_string(line, &argc, TRUE); 4081590Srgrimes MainParseArgs(argc, argv); 4091590Srgrimes} 4101590Srgrimes 41118339Sswallacechar * 41218339Sswallacechdir_verify_path(path, obpath) 41318339Sswallace char *path; 41418339Sswallace char *obpath; 41518339Sswallace{ 41618339Sswallace struct stat sb; 41718339Sswallace 41818339Sswallace if (stat(path, &sb) == 0 && S_ISDIR(sb.st_mode)) { 41975973Sru if (chdir(path) == -1 || getcwd(obpath, MAXPATHLEN) == NULL) { 42027644Scharnier warn("warning: %s", path); 42118339Sswallace return 0; 42218339Sswallace } 42375973Sru return obpath; 42418339Sswallace } 42518339Sswallace 42618339Sswallace return 0; 42718339Sswallace} 42818339Sswallace 42918339Sswallace 4301590Srgrimes/*- 4311590Srgrimes * main -- 4321590Srgrimes * The main function, for obvious reasons. Initializes variables 4331590Srgrimes * and a few modules, then parses the arguments give it in the 4341590Srgrimes * environment and on the command line. Reads the system makefile 4351590Srgrimes * followed by either Makefile, makefile or the file given by the 4361590Srgrimes * -f argument. Sets the .MAKEFLAGS PMake variable based on all the 4371590Srgrimes * flags it has received by then uses either the Make or the Compat 4381590Srgrimes * module to create the initial list of targets. 4391590Srgrimes * 4401590Srgrimes * Results: 4411590Srgrimes * If -q was given, exits -1 if anything was out-of-date. Else it exits 4421590Srgrimes * 0. 4431590Srgrimes * 4441590Srgrimes * Side Effects: 4451590Srgrimes * The program exits when done. Targets are created. etc. etc. etc. 4461590Srgrimes */ 4471590Srgrimesint 4481590Srgrimesmain(argc, argv) 4491590Srgrimes int argc; 4501590Srgrimes char **argv; 4511590Srgrimes{ 4521590Srgrimes Lst targs; /* target nodes to create -- passed to Make_Init */ 4531590Srgrimes Boolean outOfDate = TRUE; /* FALSE if all targets up to date */ 45440500Sobrien struct stat sa; 45540500Sobrien char *p, *p1, *path, *pathp; 45673262Simp char mdpath[MAXPATHLEN]; 45773262Simp char obpath[MAXPATHLEN]; 45873262Simp char cdpath[MAXPATHLEN]; 4595814Sjkh char *machine = getenv("MACHINE"); 46044362Simp char *machine_arch = getenv("MACHINE_ARCH"); 46172679Skris char *machine_cpu = getenv("MACHINE_CPU"); 46218730Ssteve Lst sysMkPath; /* Path of sys.mk */ 46318730Ssteve char *cp = NULL, *start; 46418730Ssteve /* avoid faults on read-only strings */ 46518730Ssteve static char syspath[] = _PATH_DEFSYSPATH; 4661590Srgrimes 46764739Sgreen#if DEFSHELL == 2 46864739Sgreen /* 46964739Sgreen * Turn off ENV to make ksh happier. 47064739Sgreen */ 47164739Sgreen unsetenv("ENV"); 47264739Sgreen#endif 47364739Sgreen 47418730Ssteve#ifdef RLIMIT_NOFILE 4751590Srgrimes /* 47618730Ssteve * get rid of resource limit on file descriptors 47718730Ssteve */ 47818730Ssteve { 47918730Ssteve struct rlimit rl; 48018730Ssteve if (getrlimit(RLIMIT_NOFILE, &rl) != -1 && 48118730Ssteve rl.rlim_cur != rl.rlim_max) { 48218730Ssteve rl.rlim_cur = rl.rlim_max; 48318730Ssteve (void) setrlimit(RLIMIT_NOFILE, &rl); 48418730Ssteve } 48518730Ssteve } 48618730Ssteve#endif 48718730Ssteve /* 48875974Sru * Find where we are... 4891590Srgrimes * All this code is so that we know where we are when we start up 4901590Srgrimes * on a different machine with pmake. 4911590Srgrimes */ 4921590Srgrimes curdir = cdpath; 49327644Scharnier if (getcwd(curdir, MAXPATHLEN) == NULL) 49427644Scharnier err(2, NULL); 4951590Srgrimes 49627644Scharnier if (stat(curdir, &sa) == -1) 49727644Scharnier err(2, "%s", curdir); 4981590Srgrimes 49953631Smarcel#if defined(__i386__) && defined(__FreeBSD_version) && \ 50053631Smarcel __FreeBSD_version > 300003 5015814Sjkh /* 50239006Skato * PC-98 kernel sets the `i386' string to the utsname.machine and 50339006Skato * it cannot be distinguished from IBM-PC by uname(3). Therefore, 50439006Skato * we check machine.ispc98 and adjust the machine variable before 50539006Skato * using usname(3) below. 50653631Smarcel * NOTE: machdep.ispc98 was defined on 1998/8/31. At that time, 50753631Smarcel * __FreeBSD_version was defined as 300003. So, this check can 50853631Smarcel * safely be done with any kernel with version > 300003. 50939006Skato */ 51039006Skato if (!machine) { 51139006Skato int ispc98; 51239006Skato size_t len; 51339006Skato 51439006Skato len = sizeof(ispc98); 51539006Skato if (!sysctlbyname("machdep.ispc98", &ispc98, &len, NULL, 0)) { 51639006Skato if (ispc98) 51739006Skato machine = "pc98"; 51839006Skato } 51939006Skato } 52039006Skato#endif 52139006Skato 52239006Skato /* 5235814Sjkh * Get the name of this type of MACHINE from utsname 5245814Sjkh * so we can share an executable for similar machines. 5255814Sjkh * (i.e. m68k: amiga hp300, mac68k, sun3, ...) 5265814Sjkh * 5275814Sjkh * Note that while MACHINE is decided at run-time, 5285814Sjkh * MACHINE_ARCH is always known at compile time. 5295814Sjkh */ 53018864Ssteve if (!machine) { 53118339Sswallace#ifndef MACHINE 53218730Ssteve struct utsname utsname; 53318730Ssteve 53494506Scharnier if (uname(&utsname) == -1) 53594506Scharnier err(2, "uname"); 5365814Sjkh machine = utsname.machine; 53718339Sswallace#else 53818339Sswallace machine = MACHINE; 53918339Sswallace#endif 5405814Sjkh } 5411590Srgrimes 54244362Simp if (!machine_arch) { 54344362Simp#ifndef MACHINE_ARCH 54444362Simp machine_arch = "unknown"; 54544362Simp#else 54644362Simp machine_arch = MACHINE_ARCH; 54744362Simp#endif 54844362Simp } 54944362Simp 5501590Srgrimes /* 55172679Skris * Set machine_cpu to the minumum supported CPU revision based 55272679Skris * on the target architecture, if not already set. 55372679Skris */ 55472679Skris if (!machine_cpu) { 55572679Skris if (!strcmp(machine_arch, "i386")) 55672679Skris machine_cpu = "i386"; 55772679Skris else if (!strcmp(machine_arch, "alpha")) 55872679Skris machine_cpu = "ev4"; 55972679Skris else 56072679Skris machine_cpu = "unknown"; 56172679Skris } 56272679Skris 56372679Skris /* 56418759Ssteve * The object directory location is determined using the 56518759Ssteve * following order of preference: 56618759Ssteve * 56718759Ssteve * 1. MAKEOBJDIRPREFIX`cwd` 56818759Ssteve * 2. MAKEOBJDIR 56918759Ssteve * 3. _PATH_OBJDIR.${MACHINE} 57018759Ssteve * 4. _PATH_OBJDIR 57149938Shoek * 5. _PATH_OBJDIRPREFIX`cwd` 57218759Ssteve * 57349938Shoek * If one of the first two fails, use the current directory. 57449938Shoek * If the remaining three all fail, use the current directory. 57518339Sswallace * 57618339Sswallace * Once things are initted, 57718339Sswallace * have to add the original directory to the search path, 5781590Srgrimes * and modify the paths for the Makefiles apropriately. The 5791590Srgrimes * current directory is also placed as a variable for make scripts. 5801590Srgrimes */ 58118339Sswallace if (!(pathp = getenv("MAKEOBJDIRPREFIX"))) { 58218339Sswallace if (!(path = getenv("MAKEOBJDIR"))) { 58318339Sswallace path = _PATH_OBJDIR; 58418339Sswallace pathp = _PATH_OBJDIRPREFIX; 58518339Sswallace (void) snprintf(mdpath, MAXPATHLEN, "%s.%s", 58618339Sswallace path, machine); 58718339Sswallace if (!(objdir = chdir_verify_path(mdpath, obpath))) 58818339Sswallace if (!(objdir=chdir_verify_path(path, obpath))) { 58918339Sswallace (void) snprintf(mdpath, MAXPATHLEN, 59018339Sswallace "%s%s", pathp, curdir); 59118339Sswallace if (!(objdir=chdir_verify_path(mdpath, 59218339Sswallace obpath))) 59318339Sswallace objdir = curdir; 59418339Sswallace } 59518339Sswallace } 59618339Sswallace else if (!(objdir = chdir_verify_path(path, obpath))) 5971590Srgrimes objdir = curdir; 5981590Srgrimes } 59918339Sswallace else { 60018339Sswallace (void) snprintf(mdpath, MAXPATHLEN, "%s%s", pathp, curdir); 60118339Sswallace if (!(objdir = chdir_verify_path(mdpath, obpath))) 60218339Sswallace objdir = curdir; 60318339Sswallace } 6041590Srgrimes 6051590Srgrimes create = Lst_Init(FALSE); 6061590Srgrimes makefiles = Lst_Init(FALSE); 60749332Shoek envFirstVars = Lst_Init(FALSE); 60817193Sbde printVars = FALSE; 60966365Speter expandVars = TRUE; 61017193Sbde variables = Lst_Init(FALSE); 6111590Srgrimes beSilent = FALSE; /* Print commands as executed */ 6121590Srgrimes ignoreErrors = FALSE; /* Pay attention to non-zero returns */ 6131590Srgrimes noExecute = FALSE; /* Execute all commands */ 6141590Srgrimes keepgoing = FALSE; /* Stop on error */ 6151590Srgrimes allPrecious = FALSE; /* Remove targets when interrupted */ 6161590Srgrimes queryFlag = FALSE; /* This is not just a check-run */ 6171590Srgrimes noBuiltins = FALSE; /* Read the built-in rules */ 6181590Srgrimes touchFlag = FALSE; /* Actually update targets */ 6191590Srgrimes usePipes = TRUE; /* Catch child output in pipes */ 6201590Srgrimes debug = 0; /* No debug verbosity, please. */ 6211590Srgrimes jobsRunning = FALSE; 6221590Srgrimes 62318730Ssteve maxLocal = DEFMAXLOCAL; /* Set default local max concurrency */ 62418730Ssteve#ifdef REMOTE 6251590Srgrimes maxJobs = DEFMAXJOBS; /* Set default max concurrency */ 6261590Srgrimes#else 62718730Ssteve maxJobs = maxLocal; 6281590Srgrimes#endif 62928228Sfsmp forceJobs = FALSE; /* No -j flag */ 63018730Ssteve compatMake = FALSE; /* No compat mode */ 6311590Srgrimes 6328874Srgrimes 6331590Srgrimes /* 6341590Srgrimes * Initialize the parsing, directory and variable modules to prepare 6351590Srgrimes * for the reading of inclusion paths and variable settings on the 6361590Srgrimes * command line 6371590Srgrimes */ 6381590Srgrimes Dir_Init(); /* Initialize directory structures so -I flags 6391590Srgrimes * can be processed correctly */ 6401590Srgrimes Parse_Init(); /* Need to initialize the paths of #include 6411590Srgrimes * directories */ 6421590Srgrimes Var_Init(); /* As well as the lists of variables for 6431590Srgrimes * parsing arguments */ 6445814Sjkh str_init(); 6451590Srgrimes if (objdir != curdir) 6461590Srgrimes Dir_AddDir(dirSearchPath, curdir); 6471590Srgrimes Var_Set(".CURDIR", curdir, VAR_GLOBAL); 6481590Srgrimes Var_Set(".OBJDIR", objdir, VAR_GLOBAL); 6491590Srgrimes 6501590Srgrimes /* 6511590Srgrimes * Initialize various variables. 6521590Srgrimes * MAKE also gets this name, for compatibility 6531590Srgrimes * .MAKEFLAGS gets set to the empty string just in case. 6541590Srgrimes * MFLAGS also gets initialized empty, for compatibility. 6551590Srgrimes */ 6561590Srgrimes Var_Set("MAKE", argv[0], VAR_GLOBAL); 6571590Srgrimes Var_Set(MAKEFLAGS, "", VAR_GLOBAL); 6581590Srgrimes Var_Set("MFLAGS", "", VAR_GLOBAL); 6595814Sjkh Var_Set("MACHINE", machine, VAR_GLOBAL); 66044362Simp Var_Set("MACHINE_ARCH", machine_arch, VAR_GLOBAL); 66172679Skris Var_Set("MACHINE_CPU", machine_cpu, VAR_GLOBAL); 6621590Srgrimes 6631590Srgrimes /* 6641590Srgrimes * First snag any flags out of the MAKE environment variable. 6651590Srgrimes * (Note this is *not* MAKEFLAGS since /bin/make uses that and it's 6661590Srgrimes * in a different format). 6671590Srgrimes */ 6681590Srgrimes#ifdef POSIX 6691590Srgrimes Main_ParseArgLine(getenv("MAKEFLAGS")); 6701590Srgrimes#else 6711590Srgrimes Main_ParseArgLine(getenv("MAKE")); 6721590Srgrimes#endif 6738874Srgrimes 6741590Srgrimes MainParseArgs(argc, argv); 6751590Srgrimes 6761590Srgrimes /* 67728228Sfsmp * Be compatible if user did not specify -j and did not explicitly 67828228Sfsmp * turned compatibility on 67928228Sfsmp */ 68028228Sfsmp if (!compatMake && !forceJobs) 68128228Sfsmp compatMake = TRUE; 68228228Sfsmp 68328228Sfsmp /* 6841590Srgrimes * Initialize archive, target and suffix modules in preparation for 6851590Srgrimes * parsing the makefile(s) 6861590Srgrimes */ 6871590Srgrimes Arch_Init(); 6881590Srgrimes Targ_Init(); 6891590Srgrimes Suff_Init(); 6901590Srgrimes 69169527Swill DEFAULT = NULL; 6921590Srgrimes (void)time(&now); 6931590Srgrimes 6941590Srgrimes /* 6951590Srgrimes * Set up the .TARGETS variable to contain the list of targets to be 6961590Srgrimes * created. If none specified, make the variable empty -- the parser 6971590Srgrimes * will fill the thing in with the default or .MAIN target. 6981590Srgrimes */ 6991590Srgrimes if (!Lst_IsEmpty(create)) { 7001590Srgrimes LstNode ln; 7011590Srgrimes 70269527Swill for (ln = Lst_First(create); ln != NULL; 7031590Srgrimes ln = Lst_Succ(ln)) { 7041590Srgrimes char *name = (char *)Lst_Datum(ln); 7051590Srgrimes 7061590Srgrimes Var_Append(".TARGETS", name, VAR_GLOBAL); 7071590Srgrimes } 7081590Srgrimes } else 7091590Srgrimes Var_Set(".TARGETS", "", VAR_GLOBAL); 7101590Srgrimes 71118730Ssteve 7121590Srgrimes /* 71318730Ssteve * If no user-supplied system path was given (through the -m option) 71418730Ssteve * add the directories from the DEFSYSPATH (more than one may be given 71518730Ssteve * as dir1:...:dirn) to the system include path. 7161590Srgrimes */ 71718730Ssteve if (Lst_IsEmpty(sysIncPath)) { 71818730Ssteve for (start = syspath; *start != '\0'; start = cp) { 71918730Ssteve for (cp = start; *cp != '\0' && *cp != ':'; cp++) 72018730Ssteve continue; 72118730Ssteve if (*cp == '\0') { 72218730Ssteve Dir_AddDir(sysIncPath, start); 72318730Ssteve } else { 72418730Ssteve *cp++ = '\0'; 72518730Ssteve Dir_AddDir(sysIncPath, start); 72618730Ssteve } 72718730Ssteve } 72818730Ssteve } 7291590Srgrimes 73018730Ssteve /* 73118730Ssteve * Read in the built-in rules first, followed by the specified 73218730Ssteve * makefile, if it was (makefile != (char *) NULL), or the default 73318730Ssteve * Makefile and makefile, in that order, if it wasn't. 73418730Ssteve */ 73518730Ssteve if (!noBuiltins) { 73618730Ssteve LstNode ln; 73718730Ssteve 73818730Ssteve sysMkPath = Lst_Init (FALSE); 73918730Ssteve Dir_Expand (_PATH_DEFSYSMK, sysIncPath, sysMkPath); 74018730Ssteve if (Lst_IsEmpty(sysMkPath)) 74118730Ssteve Fatal("make: no system rules (%s).", _PATH_DEFSYSMK); 74269531Swill ln = Lst_Find(sysMkPath, (void *)NULL, ReadMakefile); 74369527Swill if (ln != NULL) 74418730Ssteve Fatal("make: cannot open %s.", (char *)Lst_Datum(ln)); 74518730Ssteve } 74618730Ssteve 7471590Srgrimes if (!Lst_IsEmpty(makefiles)) { 7481590Srgrimes LstNode ln; 7491590Srgrimes 75069531Swill ln = Lst_Find(makefiles, (void *)NULL, ReadMakefile); 75169527Swill if (ln != NULL) 7521590Srgrimes Fatal("make: cannot open %s.", (char *)Lst_Datum(ln)); 75394594Sobrien } else if (!ReadMakefile("BSDmakefile", NULL)) 75494594Sobrien if (!ReadMakefile("makefile", NULL)) 75518730Ssteve (void)ReadMakefile("Makefile", NULL); 7561590Srgrimes 75718730Ssteve (void)ReadMakefile(".depend", NULL); 7581590Srgrimes 7595814Sjkh Var_Append("MFLAGS", Var_Value(MAKEFLAGS, VAR_GLOBAL, &p1), VAR_GLOBAL); 76049938Shoek efree(p1); 7611590Srgrimes 7621590Srgrimes /* Install all the flags into the MAKE envariable. */ 7635814Sjkh if (((p = Var_Value(MAKEFLAGS, VAR_GLOBAL, &p1)) != NULL) && *p) 7641590Srgrimes#ifdef POSIX 7651590Srgrimes setenv("MAKEFLAGS", p, 1); 7661590Srgrimes#else 7671590Srgrimes setenv("MAKE", p, 1); 7681590Srgrimes#endif 76949938Shoek efree(p1); 7701590Srgrimes 7711590Srgrimes /* 7721590Srgrimes * For compatibility, look at the directories in the VPATH variable 7731590Srgrimes * and add them to the search path, if the variable is defined. The 7741590Srgrimes * variable's value is in the same format as the PATH envariable, i.e. 7751590Srgrimes * <directory>:<directory>:<directory>... 7761590Srgrimes */ 7771590Srgrimes if (Var_Exists("VPATH", VAR_CMD)) { 7781590Srgrimes char *vpath, *path, *cp, savec; 7791590Srgrimes /* 7801590Srgrimes * GCC stores string constants in read-only memory, but 7811590Srgrimes * Var_Subst will want to write this thing, so store it 7821590Srgrimes * in an array 7831590Srgrimes */ 7841590Srgrimes static char VPATH[] = "${VPATH}"; 7851590Srgrimes 7861590Srgrimes vpath = Var_Subst(NULL, VPATH, VAR_CMD, FALSE); 7871590Srgrimes path = vpath; 7881590Srgrimes do { 7891590Srgrimes /* skip to end of directory */ 7901590Srgrimes for (cp = path; *cp != ':' && *cp != '\0'; cp++) 7911590Srgrimes continue; 7921590Srgrimes /* Save terminator character so know when to stop */ 7931590Srgrimes savec = *cp; 7941590Srgrimes *cp = '\0'; 7951590Srgrimes /* Add directory to search path */ 7961590Srgrimes Dir_AddDir(dirSearchPath, path); 7971590Srgrimes *cp = savec; 7981590Srgrimes path = cp + 1; 7991590Srgrimes } while (savec == ':'); 80069531Swill (void)free(vpath); 8011590Srgrimes } 8021590Srgrimes 8031590Srgrimes /* 8041590Srgrimes * Now that all search paths have been read for suffixes et al, it's 8051590Srgrimes * time to add the default search path to their lists... 8061590Srgrimes */ 8071590Srgrimes Suff_DoPaths(); 8081590Srgrimes 8091590Srgrimes /* print the initial graph, if the user requested it */ 8101590Srgrimes if (DEBUG(GRAPH1)) 8111590Srgrimes Targ_PrintGraph(1); 8121590Srgrimes 81317193Sbde /* print the values of any variables requested by the user */ 81417193Sbde if (printVars) { 81517193Sbde LstNode ln; 81617193Sbde 81769527Swill for (ln = Lst_First(variables); ln != NULL; 81817193Sbde ln = Lst_Succ(ln)) { 81966365Speter char *value; 82066365Speter if (expandVars) { 82194506Scharnier p1 = emalloc(strlen((char *)Lst_Datum(ln)) + 1 + 3); 82266365Speter /* This sprintf is safe, because of the malloc above */ 82366365Speter (void)sprintf(p1, "${%s}", (char *)Lst_Datum(ln)); 82466365Speter value = Var_Subst(NULL, p1, VAR_GLOBAL, FALSE); 82566365Speter } else { 82666365Speter value = Var_Value((char *)Lst_Datum(ln), 82766365Speter VAR_GLOBAL, &p1); 82866365Speter } 82917193Sbde printf("%s\n", value ? value : ""); 83066365Speter if (p1) 83166365Speter free(p1); 83217193Sbde } 83317193Sbde } 83417193Sbde 8351590Srgrimes /* 8361590Srgrimes * Have now read the entire graph and need to make a list of targets 8371590Srgrimes * to create. If none was given on the command line, we consult the 8381590Srgrimes * parsing module to find the main target(s) to create. 8391590Srgrimes */ 8401590Srgrimes if (Lst_IsEmpty(create)) 8411590Srgrimes targs = Parse_MainName(); 8421590Srgrimes else 8431590Srgrimes targs = Targ_FindList(create, TARG_CREATE); 8441590Srgrimes 84517193Sbde if (!compatMake && !printVars) { 8461590Srgrimes /* 8471590Srgrimes * Initialize job module before traversing the graph, now that 8481590Srgrimes * any .BEGIN and .END targets have been read. This is done 8491590Srgrimes * only if the -q flag wasn't given (to prevent the .BEGIN from 8501590Srgrimes * being executed should it exist). 8511590Srgrimes */ 8521590Srgrimes if (!queryFlag) { 8531590Srgrimes if (maxLocal == -1) 8541590Srgrimes maxLocal = maxJobs; 8551590Srgrimes Job_Init(maxJobs, maxLocal); 8561590Srgrimes jobsRunning = TRUE; 8571590Srgrimes } 8581590Srgrimes 8591590Srgrimes /* Traverse the graph, checking on all the targets */ 8601590Srgrimes outOfDate = Make_Run(targs); 86117193Sbde } else if (!printVars) { 8621590Srgrimes /* 8631590Srgrimes * Compat_Init will take care of creating all the targets as 8641590Srgrimes * well as initializing the module. 8651590Srgrimes */ 8661590Srgrimes Compat_Run(targs); 86717193Sbde } 8688874Srgrimes 8695814Sjkh Lst_Destroy(targs, NOFREE); 87017193Sbde Lst_Destroy(variables, NOFREE); 8715814Sjkh Lst_Destroy(makefiles, NOFREE); 87292921Simp Lst_Destroy(create, (void (*)(void *)) free); 8735814Sjkh 8741590Srgrimes /* print the graph now it's been processed if the user requested it */ 8751590Srgrimes if (DEBUG(GRAPH2)) 8761590Srgrimes Targ_PrintGraph(2); 8771590Srgrimes 8785814Sjkh Suff_End(); 8795814Sjkh Targ_End(); 8805814Sjkh Arch_End(); 8815814Sjkh str_end(); 8825814Sjkh Var_End(); 8835814Sjkh Parse_End(); 8845814Sjkh Dir_End(); 8855814Sjkh 8861590Srgrimes if (queryFlag && outOfDate) 8871590Srgrimes return(1); 8881590Srgrimes else 8891590Srgrimes return(0); 8901590Srgrimes} 8911590Srgrimes 8921590Srgrimes/*- 8931590Srgrimes * ReadMakefile -- 8941590Srgrimes * Open and parse the given makefile. 8951590Srgrimes * 8961590Srgrimes * Results: 8971590Srgrimes * TRUE if ok. FALSE if couldn't open file. 8981590Srgrimes * 8991590Srgrimes * Side Effects: 9001590Srgrimes * lots 9011590Srgrimes */ 9021590Srgrimesstatic Boolean 90318730SsteveReadMakefile(p, q) 90469531Swill void *p; 90569531Swill void *q; 9061590Srgrimes{ 90718730Ssteve char *fname = p; /* makefile to read */ 90818730Ssteve extern Lst parseIncPath; 9091590Srgrimes FILE *stream; 91073262Simp char *name, path[MAXPATHLEN]; 9111590Srgrimes 9121590Srgrimes if (!strcmp(fname, "-")) { 9131590Srgrimes Parse_File("(stdin)", stdin); 9141590Srgrimes Var_Set("MAKEFILE", "", VAR_GLOBAL); 9151590Srgrimes } else { 9161590Srgrimes /* if we've chdir'd, rebuild the path name */ 9171590Srgrimes if (curdir != objdir && *fname != '/') { 91869390Swill (void)snprintf(path, MAXPATHLEN, "%s/%s", curdir, fname); 9191590Srgrimes if ((stream = fopen(path, "r")) != NULL) { 9201590Srgrimes fname = path; 9211590Srgrimes goto found; 9221590Srgrimes } 92328828Sjkh } else if ((stream = fopen(fname, "r")) != NULL) 92428828Sjkh goto found; 9251590Srgrimes /* look in -I and system include directories. */ 9261590Srgrimes name = Dir_FindFile(fname, parseIncPath); 9271590Srgrimes if (!name) 9281590Srgrimes name = Dir_FindFile(fname, sysIncPath); 9291590Srgrimes if (!name || !(stream = fopen(name, "r"))) 9301590Srgrimes return(FALSE); 9311590Srgrimes fname = name; 9321590Srgrimes /* 9331590Srgrimes * set the MAKEFILE variable desired by System V fans -- the 9341590Srgrimes * placement of the setting here means it gets set to the last 9351590Srgrimes * makefile specified, as it is set by SysV make. 9361590Srgrimes */ 9371590Srgrimesfound: Var_Set("MAKEFILE", fname, VAR_GLOBAL); 9381590Srgrimes Parse_File(fname, stream); 9391590Srgrimes (void)fclose(stream); 9401590Srgrimes } 9411590Srgrimes return(TRUE); 9421590Srgrimes} 9431590Srgrimes 9441590Srgrimes/*- 94518730Ssteve * Cmd_Exec -- 94618730Ssteve * Execute the command in cmd, and return the output of that command 94718730Ssteve * in a string. 94818730Ssteve * 94918730Ssteve * Results: 95018730Ssteve * A string containing the output of the command, or the empty string 95118730Ssteve * If err is not NULL, it contains the reason for the command failure 95218730Ssteve * 95318730Ssteve * Side Effects: 95418730Ssteve * The string must be freed by the caller. 95518730Ssteve */ 95618730Sstevechar * 95718730SsteveCmd_Exec(cmd, err) 95818730Ssteve char *cmd; 95918730Ssteve char **err; 96018730Ssteve{ 96118730Ssteve char *args[4]; /* Args for invoking the shell */ 96218730Ssteve int fds[2]; /* Pipe streams */ 96318730Ssteve int cpid; /* Child PID */ 96418730Ssteve int pid; /* PID from wait() */ 96518730Ssteve char *res; /* result */ 96618730Ssteve int status; /* command exit status */ 96718730Ssteve Buffer buf; /* buffer to store the result */ 96818730Ssteve char *cp; 96918730Ssteve int cc; 97018730Ssteve 97118730Ssteve 97218730Ssteve *err = NULL; 97318730Ssteve 97418730Ssteve /* 97518730Ssteve * Set up arguments for shell 97618730Ssteve */ 97718730Ssteve args[0] = "sh"; 97818730Ssteve args[1] = "-c"; 97918730Ssteve args[2] = cmd; 98018730Ssteve args[3] = NULL; 98118730Ssteve 98218730Ssteve /* 98318730Ssteve * Open a pipe for fetching its output 98418730Ssteve */ 98518730Ssteve if (pipe(fds) == -1) { 98618730Ssteve *err = "Couldn't create pipe for \"%s\""; 98718730Ssteve goto bad; 98818730Ssteve } 98918730Ssteve 99018730Ssteve /* 99118730Ssteve * Fork 99218730Ssteve */ 99318730Ssteve switch (cpid = vfork()) { 99418730Ssteve case 0: 99518730Ssteve /* 99618730Ssteve * Close input side of pipe 99718730Ssteve */ 99818730Ssteve (void) close(fds[0]); 99918730Ssteve 100018730Ssteve /* 100118730Ssteve * Duplicate the output stream to the shell's output, then 100218730Ssteve * shut the extra thing down. Note we don't fetch the error 100318730Ssteve * stream...why not? Why? 100418730Ssteve */ 100518730Ssteve (void) dup2(fds[1], 1); 100618730Ssteve (void) close(fds[1]); 100718730Ssteve 100864739Sgreen#if DEFSHELL == 1 100918730Ssteve (void) execv("/bin/sh", args); 101064739Sgreen#elif DEFSHELL == 2 101164739Sgreen (void) execv("/bin/ksh", args); 101264739Sgreen#else 101364739Sgreen#error "DEFSHELL must be 1 or 2." 101464739Sgreen#endif 101518730Ssteve _exit(1); 101618730Ssteve /*NOTREACHED*/ 101718730Ssteve 101818730Ssteve case -1: 101918730Ssteve *err = "Couldn't exec \"%s\""; 102018730Ssteve goto bad; 102118730Ssteve 102218730Ssteve default: 102318730Ssteve /* 102418730Ssteve * No need for the writing half 102518730Ssteve */ 102618730Ssteve (void) close(fds[1]); 102718730Ssteve 102818730Ssteve buf = Buf_Init (MAKE_BSIZE); 102918730Ssteve 103018730Ssteve do { 103118730Ssteve char result[BUFSIZ]; 103218730Ssteve cc = read(fds[0], result, sizeof(result)); 103318730Ssteve if (cc > 0) 103418730Ssteve Buf_AddBytes(buf, cc, (Byte *) result); 103518730Ssteve } 103618730Ssteve while (cc > 0 || (cc == -1 && errno == EINTR)); 103718730Ssteve 103818730Ssteve /* 103918730Ssteve * Close the input side of the pipe. 104018730Ssteve */ 104118730Ssteve (void) close(fds[0]); 104218730Ssteve 104318730Ssteve /* 104418730Ssteve * Wait for the process to exit. 104518730Ssteve */ 104618730Ssteve while(((pid = wait(&status)) != cpid) && (pid >= 0)) 104718730Ssteve continue; 104818730Ssteve 104918877Ssteve if (cc == -1) 105018877Ssteve *err = "Error reading shell's output for \"%s\""; 105118864Ssteve 105218730Ssteve res = (char *)Buf_GetAll (buf, &cc); 105318730Ssteve Buf_Destroy (buf, FALSE); 105418730Ssteve 105518730Ssteve if (status) 105618730Ssteve *err = "\"%s\" returned non-zero status"; 105718730Ssteve 105818730Ssteve /* 105918730Ssteve * Null-terminate the result, convert newlines to spaces and 106018730Ssteve * install it in the variable. 106118730Ssteve */ 106218730Ssteve res[cc] = '\0'; 106318730Ssteve cp = &res[cc] - 1; 106418730Ssteve 106518730Ssteve if (*cp == '\n') { 106618730Ssteve /* 106718730Ssteve * A final newline is just stripped 106818730Ssteve */ 106918730Ssteve *cp-- = '\0'; 107018730Ssteve } 107118730Ssteve while (cp >= res) { 107218730Ssteve if (*cp == '\n') { 107318730Ssteve *cp = ' '; 107418730Ssteve } 107518730Ssteve cp--; 107618730Ssteve } 107718730Ssteve break; 107818730Ssteve } 107918730Ssteve return res; 108018730Sstevebad: 108118730Ssteve res = emalloc(1); 108218730Ssteve *res = '\0'; 108318730Ssteve return res; 108418730Ssteve} 108518730Ssteve 108618730Ssteve/*- 10871590Srgrimes * Error -- 10881590Srgrimes * Print an error message given its format. 10891590Srgrimes * 10901590Srgrimes * Results: 10911590Srgrimes * None. 10921590Srgrimes * 10931590Srgrimes * Side Effects: 10941590Srgrimes * The message is printed. 10951590Srgrimes */ 10961590Srgrimes/* VARARGS */ 10971590Srgrimesvoid 10985814SjkhError(char *fmt, ...) 10991590Srgrimes{ 11001590Srgrimes va_list ap; 110193056Simp 11021590Srgrimes va_start(ap, fmt); 11031590Srgrimes (void)vfprintf(stderr, fmt, ap); 11041590Srgrimes va_end(ap); 11051590Srgrimes (void)fprintf(stderr, "\n"); 11061590Srgrimes (void)fflush(stderr); 11071590Srgrimes} 11081590Srgrimes 11091590Srgrimes/*- 11101590Srgrimes * Fatal -- 11111590Srgrimes * Produce a Fatal error message. If jobs are running, waits for them 11121590Srgrimes * to finish. 11131590Srgrimes * 11141590Srgrimes * Results: 11151590Srgrimes * None 11161590Srgrimes * 11171590Srgrimes * Side Effects: 11181590Srgrimes * The program exits 11191590Srgrimes */ 11201590Srgrimes/* VARARGS */ 11211590Srgrimesvoid 11225814SjkhFatal(char *fmt, ...) 11231590Srgrimes{ 11241590Srgrimes va_list ap; 112593056Simp 11261590Srgrimes va_start(ap, fmt); 11271590Srgrimes if (jobsRunning) 11281590Srgrimes Job_Wait(); 11291590Srgrimes 11301590Srgrimes (void)vfprintf(stderr, fmt, ap); 11311590Srgrimes va_end(ap); 11321590Srgrimes (void)fprintf(stderr, "\n"); 11331590Srgrimes (void)fflush(stderr); 11341590Srgrimes 11351590Srgrimes if (DEBUG(GRAPH2)) 11361590Srgrimes Targ_PrintGraph(2); 11371590Srgrimes exit(2); /* Not 1 so -q can distinguish error */ 11381590Srgrimes} 11391590Srgrimes 11401590Srgrimes/* 11411590Srgrimes * Punt -- 11421590Srgrimes * Major exception once jobs are being created. Kills all jobs, prints 11431590Srgrimes * a message and exits. 11441590Srgrimes * 11451590Srgrimes * Results: 11468874Srgrimes * None 11471590Srgrimes * 11481590Srgrimes * Side Effects: 11491590Srgrimes * All children are killed indiscriminately and the program Lib_Exits 11501590Srgrimes */ 11511590Srgrimes/* VARARGS */ 11521590Srgrimesvoid 11535814SjkhPunt(char *fmt, ...) 11541590Srgrimes{ 11551590Srgrimes va_list ap; 115693056Simp 11571590Srgrimes va_start(ap, fmt); 11581590Srgrimes (void)fprintf(stderr, "make: "); 11591590Srgrimes (void)vfprintf(stderr, fmt, ap); 11601590Srgrimes va_end(ap); 11611590Srgrimes (void)fprintf(stderr, "\n"); 11621590Srgrimes (void)fflush(stderr); 11631590Srgrimes 11641590Srgrimes DieHorribly(); 11651590Srgrimes} 11661590Srgrimes 11671590Srgrimes/*- 11681590Srgrimes * DieHorribly -- 11691590Srgrimes * Exit without giving a message. 11701590Srgrimes * 11711590Srgrimes * Results: 11721590Srgrimes * None 11731590Srgrimes * 11741590Srgrimes * Side Effects: 11751590Srgrimes * A big one... 11761590Srgrimes */ 11771590Srgrimesvoid 11781590SrgrimesDieHorribly() 11791590Srgrimes{ 11801590Srgrimes if (jobsRunning) 11811590Srgrimes Job_AbortAll(); 11821590Srgrimes if (DEBUG(GRAPH2)) 11831590Srgrimes Targ_PrintGraph(2); 11841590Srgrimes exit(2); /* Not 1, so -q can distinguish error */ 11851590Srgrimes} 11861590Srgrimes 11871590Srgrimes/* 11881590Srgrimes * Finish -- 11891590Srgrimes * Called when aborting due to errors in child shell to signal 11908874Srgrimes * abnormal exit. 11911590Srgrimes * 11921590Srgrimes * Results: 11938874Srgrimes * None 11941590Srgrimes * 11951590Srgrimes * Side Effects: 11961590Srgrimes * The program exits 11971590Srgrimes */ 11981590Srgrimesvoid 11991590SrgrimesFinish(errors) 12001590Srgrimes int errors; /* number of errors encountered in Make_Make */ 12011590Srgrimes{ 12021590Srgrimes Fatal("%d error%s", errors, errors == 1 ? "" : "s"); 12031590Srgrimes} 12041590Srgrimes 12051590Srgrimes/* 12061590Srgrimes * emalloc -- 12071590Srgrimes * malloc, but die on error. 12081590Srgrimes */ 120918730Sstevevoid * 12101590Srgrimesemalloc(len) 12115814Sjkh size_t len; 12121590Srgrimes{ 121318730Ssteve void *p; 121418730Ssteve 121518730Ssteve if ((p = malloc(len)) == NULL) 121618730Ssteve enomem(); 121718730Ssteve return(p); 121818730Ssteve} 121918730Ssteve 122018730Ssteve/* 122118730Ssteve * estrdup -- 122218730Ssteve * strdup, but die on error. 122318730Ssteve */ 122418730Sstevechar * 122518730Ssteveestrdup(str) 122618730Ssteve const char *str; 122718730Ssteve{ 12281590Srgrimes char *p; 12291590Srgrimes 123018730Ssteve if ((p = strdup(str)) == NULL) 12311590Srgrimes enomem(); 12321590Srgrimes return(p); 12331590Srgrimes} 12341590Srgrimes 12351590Srgrimes/* 123618730Ssteve * erealloc -- 123718730Ssteve * realloc, but die on error. 123818730Ssteve */ 123918730Sstevevoid * 124018730Ssteveerealloc(ptr, size) 124118730Ssteve void *ptr; 124218730Ssteve size_t size; 124318730Ssteve{ 124418730Ssteve if ((ptr = realloc(ptr, size)) == NULL) 124518730Ssteve enomem(); 124618730Ssteve return(ptr); 124718730Ssteve} 124818730Ssteve 124918730Ssteve/* 12501590Srgrimes * enomem -- 12511590Srgrimes * die when out of memory. 12521590Srgrimes */ 12531590Srgrimesvoid 12541590Srgrimesenomem() 12551590Srgrimes{ 125627644Scharnier err(2, NULL); 12571590Srgrimes} 12581590Srgrimes 12591590Srgrimes/* 126018730Ssteve * enunlink -- 126118730Ssteve * Remove a file carefully, avoiding directories. 126218730Ssteve */ 126318730Ssteveint 126418730Ssteveeunlink(file) 126518730Ssteve const char *file; 126618730Ssteve{ 126718730Ssteve struct stat st; 126818730Ssteve 126918730Ssteve if (lstat(file, &st) == -1) 127018730Ssteve return -1; 127118730Ssteve 127218730Ssteve if (S_ISDIR(st.st_mode)) { 127318730Ssteve errno = EISDIR; 127418730Ssteve return -1; 127518730Ssteve } 127618730Ssteve return unlink(file); 127718730Ssteve} 127818730Ssteve 127918730Ssteve/* 12801590Srgrimes * usage -- 12811590Srgrimes * exit with usage message 12821590Srgrimes */ 12831590Srgrimesstatic void 12841590Srgrimesusage() 12851590Srgrimes{ 128627644Scharnier (void)fprintf(stderr, "%s\n%s\n%s\n", 128749332Shoek"usage: make [-Beiknqrstv] [-D variable] [-d flags] [-E variable] [-f makefile]", 128827644Scharnier" [-I directory] [-j max_jobs] [-m directory] [-V variable]", 128927644Scharnier" [variable=value] [target ...]"); 12901590Srgrimes exit(2); 12911590Srgrimes} 12925814Sjkh 12935814Sjkh 12945814Sjkhint 12955814SjkhPrintAddr(a, b) 129669531Swill void * a; 129769531Swill void * b; 12985814Sjkh{ 12995814Sjkh printf("%lx ", (unsigned long) a); 13005814Sjkh return b ? 0 : 0; 13015814Sjkh} 1302