main.c revision 75973
1122207Sharti/* 2122207Sharti * Copyright (c) 1988, 1989, 1990, 1993 3122207Sharti * The Regents of the University of California. All rights reserved. 4122207Sharti * Copyright (c) 1989 by Berkeley Softworks 5122207Sharti * All rights reserved. 6122207Sharti * 7122207Sharti * This code is derived from software contributed to Berkeley by 8122207Sharti * Adam de Boor. 9122207Sharti * 10122207Sharti * Redistribution and use in source and binary forms, with or without 11122207Sharti * modification, are permitted provided that the following conditions 12122207Sharti * are met: 13122207Sharti * 1. Redistributions of source code must retain the above copyright 14122207Sharti * notice, this list of conditions and the following disclaimer. 15122207Sharti * 2. Redistributions in binary form must reproduce the above copyright 16122207Sharti * notice, this list of conditions and the following disclaimer in the 17122207Sharti * documentation and/or other materials provided with the distribution. 18122207Sharti * 3. All advertising materials mentioning features or use of this software 19122207Sharti * must display the following acknowledgement: 20122207Sharti * This product includes software developed by the University of 21122207Sharti * California, Berkeley and its contributors. 22122207Sharti * 4. Neither the name of the University nor the names of its contributors 23122207Sharti * may be used to endorse or promote products derived from this software 24122207Sharti * without specific prior written permission. 25122207Sharti * 26122207Sharti * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND 27122207Sharti * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 28122207Sharti * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 29122207Sharti * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE 30122207Sharti * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 31122207Sharti * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 32122207Sharti * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 33122207Sharti * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 34122207Sharti * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 35122207Sharti * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 36122207Sharti * SUCH DAMAGE. 37122207Sharti * 38122207Sharti * @(#)main.c 8.3 (Berkeley) 3/19/94 39122207Sharti */ 40122207Sharti 41122207Sharti#ifndef lint 42122207Sharti#include <sys/cdefs.h> 43122207Sharti__COPYRIGHT("@(#) Copyright (c) 1988, 1989, 1990, 1993 The Regents of the University of California. All rights reserved."); 44122207Sharti__RCSID("$FreeBSD: head/usr.bin/make/main.c 75973 2001-04-25 14:22:38Z ru $"); 45122207Sharti#endif /* not lint */ 46122207Sharti 47122207Sharti/*- 48122207Sharti * main.c -- 49122207Sharti * The main file for this entire program. Exit routines etc 50122207Sharti * reside here. 51122207Sharti * 52122207Sharti * Utility functions defined in this file: 53122207Sharti * Main_ParseArgLine Takes a line of arguments, breaks them and 54122207Sharti * treats them as if they were given when first 55122207Sharti * invoked. Used by the parse module to implement 56122207Sharti * the .MFLAGS target. 57122207Sharti * 58122207Sharti * Error Print a tagged error message. The global 59122207Sharti * MAKE variable must have been defined. This 60122207Sharti * takes a format string and two optional 61122207Sharti * arguments for it. 62122207Sharti * 63122207Sharti * Fatal Print an error message and exit. Also takes 64122207Sharti * a format string and two arguments. 65122207Sharti * 66122207Sharti * Punt Aborts all jobs and exits with a message. Also 67122207Sharti * takes a format string and two arguments. 68122207Sharti * 69122207Sharti * Finish Finish things up by printing the number of 70122207Sharti * errors which occured, as passed to it, and 71122207Sharti * exiting. 72122207Sharti */ 73122207Sharti 74122207Sharti#include <sys/types.h> 75122207Sharti#include <sys/time.h> 76122207Sharti#include <sys/param.h> 77122207Sharti#include <sys/resource.h> 78122207Sharti#include <sys/signal.h> 79122207Sharti#include <sys/stat.h> 80122207Sharti#if defined(__i386__) 81122207Sharti#include <sys/sysctl.h> 82122207Sharti#endif 83122207Sharti#ifndef MACHINE 84122207Sharti#include <sys/utsname.h> 85122207Sharti#endif 86122207Sharti#include <sys/wait.h> 87122207Sharti#include <err.h> 88122207Sharti#include <stdlib.h> 89122207Sharti#include <errno.h> 90122207Sharti#include <fcntl.h> 91122207Sharti#include <stdio.h> 92122207Sharti#include <sysexits.h> 93122207Sharti#ifdef __STDC__ 94122207Sharti#include <stdarg.h> 95122207Sharti#else 96122207Sharti#include <varargs.h> 97122207Sharti#endif 98122207Sharti#include <unistd.h> 99122207Sharti#include "make.h" 100122207Sharti#include "hash.h" 101122207Sharti#include "dir.h" 102122207Sharti#include "job.h" 103122207Sharti#include "pathnames.h" 104122207Sharti 105122207Sharti#ifndef DEFMAXLOCAL 106122207Sharti#define DEFMAXLOCAL DEFMAXJOBS 107122207Sharti#endif /* DEFMAXLOCAL */ 108122207Sharti 109122207Sharti#define MAKEFLAGS ".MAKEFLAGS" 110122207Sharti 111122207ShartiLst create; /* Targets to be made */ 112122207Shartitime_t now; /* Time at start of make */ 113122207ShartiGNode *DEFAULT; /* .DEFAULT node */ 114122207ShartiBoolean allPrecious; /* .PRECIOUS given on line by itself */ 115122207Sharti 116122207Shartistatic Boolean noBuiltins; /* -r flag */ 117122207Shartistatic Lst makefiles; /* ordered list of makefiles to read */ 118122207Shartistatic Boolean printVars; /* print value of one or more vars */ 119122207Shartistatic Boolean expandVars; /* fully expand printed variables */ 120122207Shartistatic Lst variables; /* list of variables to print */ 121122207Shartiint maxJobs; /* -j argument */ 122122207Shartistatic Boolean forceJobs; /* -j argument given */ 123122207Shartistatic int maxLocal; /* -L argument */ 124122207ShartiBoolean compatMake; /* -B argument */ 125122207ShartiBoolean debug; /* -d flag */ 126122207ShartiBoolean noExecute; /* -n flag */ 127122207ShartiBoolean keepgoing; /* -k flag */ 128122207ShartiBoolean queryFlag; /* -q flag */ 129122207ShartiBoolean touchFlag; /* -t flag */ 130122207ShartiBoolean usePipes; /* !-P flag */ 131122207ShartiBoolean ignoreErrors; /* -i flag */ 132122207ShartiBoolean beSilent; /* -s flag */ 133122207ShartiBoolean beVerbose; /* -v flag */ 134122207ShartiBoolean oldVars; /* variable substitution style */ 135122207ShartiBoolean checkEnvFirst; /* -e flag */ 136122207ShartiLst envFirstVars; /* (-E) vars to override from env */ 137122207Shartistatic Boolean jobsRunning; /* TRUE if the jobs might be running */ 138122207Sharti 139122207Shartistatic void MainParseArgs __P((int, char **)); 140122207Shartichar * chdir_verify_path __P((char *, char *)); 141122207Shartistatic int ReadMakefile __P((void *, void *)); 142122207Shartistatic void usage __P((void)); 143122207Sharti 144122207Shartistatic char *curdir; /* startup directory */ 145122207Shartistatic char *objdir; /* where we chdir'ed to */ 146122207Sharti 147122207Sharti/*- 148122207Sharti * MainParseArgs -- 149122207Sharti * Parse a given argument vector. Called from main() and from 150122207Sharti * Main_ParseArgLine() when the .MAKEFLAGS target is used. 151122207Sharti * 152122207Sharti * XXX: Deal with command line overriding .MAKEFLAGS in makefile 153122207Sharti * 154122207Sharti * Results: 155122207Sharti * None 156122207Sharti * 157122207Sharti * Side Effects: 158122207Sharti * Various global and local flags will be set depending on the flags 159122207Sharti * given 160122207Sharti */ 161122207Shartistatic void 162122207ShartiMainParseArgs(argc, argv) 163122207Sharti int argc; 164122207Sharti char **argv; 165122207Sharti{ 166122207Sharti char *p; 167122207Sharti int c; 168122207Sharti 169122207Sharti optind = 1; /* since we're called more than once */ 170122207Sharti#ifdef REMOTE 171122207Sharti# define OPTFLAGS "BD:E:I:L:PSV:Xd:ef:ij:km:nqrstv" 172122207Sharti#else 173122207Sharti# define OPTFLAGS "BD:E:I:PSV:Xd:ef:ij:km:nqrstv" 174122207Sharti#endif 175122207Shartirearg: while((c = getopt(argc, argv, OPTFLAGS)) != -1) { 176122207Sharti switch(c) { 177122207Sharti case 'D': 178122207Sharti Var_Set(optarg, "1", VAR_GLOBAL); 179122207Sharti Var_Append(MAKEFLAGS, "-D", VAR_GLOBAL); 180122207Sharti Var_Append(MAKEFLAGS, optarg, VAR_GLOBAL); 181122207Sharti break; 182122207Sharti case 'I': 183122207Sharti Parse_AddIncludeDir(optarg); 184122207Sharti Var_Append(MAKEFLAGS, "-I", VAR_GLOBAL); 185122207Sharti Var_Append(MAKEFLAGS, optarg, VAR_GLOBAL); 186122207Sharti break; 187122207Sharti case 'V': 188122207Sharti printVars = TRUE; 189122207Sharti (void)Lst_AtEnd(variables, (void *)optarg); 190122207Sharti Var_Append(MAKEFLAGS, "-V", VAR_GLOBAL); 191122207Sharti Var_Append(MAKEFLAGS, optarg, VAR_GLOBAL); 192122207Sharti break; 193122207Sharti case 'X': 194122207Sharti expandVars = FALSE; 195122207Sharti break; 196122207Sharti case 'B': 197122207Sharti compatMake = TRUE; 198122207Sharti Var_Append(MAKEFLAGS, "-B", VAR_GLOBAL); 199122207Sharti break; 200122207Sharti#ifdef REMOTE 201122207Sharti case 'L': { 202122207Sharti char *endptr; 203122207Sharti 204122207Sharti maxLocal = strtol(optarg, &endptr, 10); 205122207Sharti if (maxLocal < 0 || *endptr != '\0') { 206122207Sharti warnx("illegal number, -L argument -- %s", 207122207Sharti optarg); 208122207Sharti usage(); 209122207Sharti } 210122207Sharti Var_Append(MAKEFLAGS, "-L", VAR_GLOBAL); 211122207Sharti Var_Append(MAKEFLAGS, optarg, VAR_GLOBAL); 212122207Sharti break; 213122207Sharti } 214122207Sharti#endif 215122207Sharti case 'P': 216122207Sharti usePipes = FALSE; 217122207Sharti Var_Append(MAKEFLAGS, "-P", VAR_GLOBAL); 218122207Sharti break; 219122207Sharti case 'S': 220122207Sharti keepgoing = FALSE; 221122207Sharti Var_Append(MAKEFLAGS, "-S", VAR_GLOBAL); 222122207Sharti break; 223122207Sharti case 'd': { 224122207Sharti char *modules = optarg; 225122207Sharti 226122207Sharti for (; *modules; ++modules) 227122207Sharti switch (*modules) { 228122207Sharti case 'A': 229122207Sharti debug = ~0; 230122207Sharti break; 231122207Sharti case 'a': 232122207Sharti debug |= DEBUG_ARCH; 233122207Sharti break; 234122207Sharti case 'c': 235122207Sharti debug |= DEBUG_COND; 236122207Sharti break; 237122207Sharti case 'd': 238122207Sharti debug |= DEBUG_DIR; 239122207Sharti break; 240122207Sharti case 'f': 241122207Sharti debug |= DEBUG_FOR; 242122207Sharti break; 243122207Sharti case 'g': 244122207Sharti if (modules[1] == '1') { 245122207Sharti debug |= DEBUG_GRAPH1; 246122207Sharti ++modules; 247122207Sharti } 248122207Sharti else if (modules[1] == '2') { 249122207Sharti debug |= DEBUG_GRAPH2; 250122207Sharti ++modules; 251122207Sharti } 252122207Sharti break; 253122207Sharti case 'j': 254122207Sharti debug |= DEBUG_JOB; 255122207Sharti break; 256122207Sharti case 'l': 257122207Sharti debug |= DEBUG_LOUD; 258122207Sharti break; 259122207Sharti case 'm': 260122207Sharti debug |= DEBUG_MAKE; 261122207Sharti break; 262122207Sharti case 's': 263122207Sharti debug |= DEBUG_SUFF; 264122207Sharti break; 265122207Sharti case 't': 266122207Sharti debug |= DEBUG_TARG; 267122207Sharti break; 268122207Sharti case 'v': 269122207Sharti debug |= DEBUG_VAR; 270122207Sharti break; 271122207Sharti default: 272122207Sharti warnx("illegal argument to d option -- %c", *modules); 273122207Sharti usage(); 274122207Sharti } 275122207Sharti Var_Append(MAKEFLAGS, "-d", VAR_GLOBAL); 276122207Sharti Var_Append(MAKEFLAGS, optarg, VAR_GLOBAL); 277122207Sharti break; 278122207Sharti } 279122207Sharti case 'E': 280122207Sharti p = malloc(strlen(optarg) + 1); 281122207Sharti if (!p) 282122207Sharti Punt("make: cannot allocate memory."); 283122207Sharti (void)strcpy(p, optarg); 284122207Sharti (void)Lst_AtEnd(envFirstVars, (void *)p); 285122207Sharti Var_Append(MAKEFLAGS, "-E", VAR_GLOBAL); 286122207Sharti Var_Append(MAKEFLAGS, optarg, VAR_GLOBAL); 287122207Sharti break; 288122207Sharti case 'e': 289122207Sharti checkEnvFirst = TRUE; 290122207Sharti Var_Append(MAKEFLAGS, "-e", VAR_GLOBAL); 291122207Sharti break; 292122207Sharti case 'f': 293122207Sharti (void)Lst_AtEnd(makefiles, (void *)optarg); 294122207Sharti break; 295122207Sharti case 'i': 296122207Sharti ignoreErrors = TRUE; 297122207Sharti Var_Append(MAKEFLAGS, "-i", VAR_GLOBAL); 298122207Sharti break; 299122207Sharti case 'j': { 300122207Sharti char *endptr; 301122207Sharti 302122207Sharti forceJobs = TRUE; 303122207Sharti maxJobs = strtol(optarg, &endptr, 10); 304122207Sharti if (maxJobs <= 0 || *endptr != '\0') { 305122207Sharti warnx("illegal number, -j argument -- %s", 306122207Sharti optarg); 307122207Sharti usage(); 308122207Sharti } 309122207Sharti#ifndef REMOTE 310122207Sharti maxLocal = maxJobs; 311122207Sharti#endif 312122207Sharti Var_Append(MAKEFLAGS, "-j", VAR_GLOBAL); 313122207Sharti Var_Append(MAKEFLAGS, optarg, VAR_GLOBAL); 314122207Sharti break; 315122207Sharti } 316122207Sharti case 'k': 317122207Sharti keepgoing = TRUE; 318122207Sharti Var_Append(MAKEFLAGS, "-k", VAR_GLOBAL); 319122207Sharti break; 320122207Sharti case 'm': 321122207Sharti Dir_AddDir(sysIncPath, optarg); 322122207Sharti Var_Append(MAKEFLAGS, "-m", VAR_GLOBAL); 323122207Sharti Var_Append(MAKEFLAGS, optarg, VAR_GLOBAL); 324122207Sharti break; 325122207Sharti case 'n': 326122207Sharti noExecute = TRUE; 327122207Sharti Var_Append(MAKEFLAGS, "-n", VAR_GLOBAL); 328122207Sharti break; 329122207Sharti case 'q': 330122207Sharti queryFlag = TRUE; 331122207Sharti /* Kind of nonsensical, wot? */ 332122207Sharti Var_Append(MAKEFLAGS, "-q", VAR_GLOBAL); 333122207Sharti break; 334122207Sharti case 'r': 335122207Sharti noBuiltins = TRUE; 336122207Sharti Var_Append(MAKEFLAGS, "-r", VAR_GLOBAL); 337122207Sharti break; 338122207Sharti case 's': 339122207Sharti beSilent = TRUE; 340122207Sharti Var_Append(MAKEFLAGS, "-s", VAR_GLOBAL); 341122207Sharti break; 342122207Sharti case 't': 343122207Sharti touchFlag = TRUE; 344122207Sharti Var_Append(MAKEFLAGS, "-t", VAR_GLOBAL); 345122207Sharti break; 346122207Sharti case 'v': 347122207Sharti beVerbose = TRUE; 348122207Sharti Var_Append(MAKEFLAGS, "-v", VAR_GLOBAL); 349122207Sharti break; 350122207Sharti default: 351122207Sharti case '?': 352122207Sharti usage(); 353122207Sharti } 354122207Sharti } 355122207Sharti 356122207Sharti oldVars = TRUE; 357122207Sharti 358122207Sharti /* 359122207Sharti * See if the rest of the arguments are variable assignments and 360122207Sharti * perform them if so. Else take them to be targets and stuff them 361122207Sharti * on the end of the "create" list. 362122207Sharti */ 363122207Sharti for (argv += optind, argc -= optind; *argv; ++argv, --argc) 364122207Sharti if (Parse_IsVar(*argv)) 365122207Sharti Parse_DoVar(*argv, VAR_CMD); 366122207Sharti else { 367122207Sharti if (!**argv) 368122207Sharti Punt("illegal (null) argument."); 369122207Sharti if (**argv == '-') { 370122207Sharti if ((*argv)[1]) 371122207Sharti optind = 0; /* -flag... */ 372122207Sharti else 373122207Sharti optind = 1; /* - */ 374122207Sharti goto rearg; 375122207Sharti } 376122207Sharti (void)Lst_AtEnd(create, (void *)estrdup(*argv)); 377122207Sharti } 378122207Sharti} 379122207Sharti 380122207Sharti/*- 381122207Sharti * Main_ParseArgLine -- 382122207Sharti * Used by the parse module when a .MFLAGS or .MAKEFLAGS target 383122207Sharti * is encountered and by main() when reading the .MAKEFLAGS envariable. 384122207Sharti * Takes a line of arguments and breaks it into its 385122207Sharti * component words and passes those words and the number of them to the 386122207Sharti * MainParseArgs function. 387122207Sharti * The line should have all its leading whitespace removed. 388122207Sharti * 389122207Sharti * Results: 390122207Sharti * None 391122207Sharti * 392122207Sharti * Side Effects: 393122207Sharti * Only those that come from the various arguments. 394122207Sharti */ 395122207Shartivoid 396122207ShartiMain_ParseArgLine(line) 397122207Sharti char *line; /* Line to fracture */ 398122207Sharti{ 399122207Sharti char **argv; /* Manufactured argument vector */ 400122207Sharti int argc; /* Number of arguments in argv */ 401122207Sharti 402122207Sharti if (line == NULL) 403122207Sharti return; 404122207Sharti for (; *line == ' '; ++line) 405122207Sharti continue; 406122207Sharti if (!*line) 407122207Sharti return; 408122207Sharti 409122207Sharti argv = brk_string(line, &argc, TRUE); 410122207Sharti MainParseArgs(argc, argv); 411122207Sharti} 412122207Sharti 413122207Shartichar * 414122207Shartichdir_verify_path(path, obpath) 415122207Sharti char *path; 416122207Sharti char *obpath; 417122207Sharti{ 418122207Sharti struct stat sb; 419122207Sharti 420122207Sharti if (stat(path, &sb) == 0 && S_ISDIR(sb.st_mode)) { 421122207Sharti if (chdir(path) == -1 || getcwd(obpath, MAXPATHLEN) == NULL) { 422122207Sharti warn("warning: %s", path); 423122207Sharti return 0; 424122207Sharti } 425122207Sharti return obpath; 426122207Sharti } 427122207Sharti 428122207Sharti return 0; 429122207Sharti} 430122207Sharti 431122207Sharti 432122207Sharti/*- 433122207Sharti * main -- 434122207Sharti * The main function, for obvious reasons. Initializes variables 435122207Sharti * and a few modules, then parses the arguments give it in the 436122207Sharti * environment and on the command line. Reads the system makefile 437122207Sharti * followed by either Makefile, makefile or the file given by the 438122207Sharti * -f argument. Sets the .MAKEFLAGS PMake variable based on all the 439122207Sharti * flags it has received by then uses either the Make or the Compat 440122207Sharti * module to create the initial list of targets. 441122207Sharti * 442122207Sharti * Results: 443122207Sharti * If -q was given, exits -1 if anything was out-of-date. Else it exits 444122207Sharti * 0. 445122207Sharti * 446122207Sharti * Side Effects: 447122207Sharti * The program exits when done. Targets are created. etc. etc. etc. 448122207Sharti */ 449122207Shartiint 450122207Shartimain(argc, argv) 451122207Sharti int argc; 452122207Sharti char **argv; 453122207Sharti{ 454122207Sharti Lst targs; /* target nodes to create -- passed to Make_Init */ 455122207Sharti Boolean outOfDate = TRUE; /* FALSE if all targets up to date */ 456122207Sharti struct stat sa; 457122207Sharti char *p, *p1, *path, *pathp; 458122207Sharti#ifdef WANT_ENV_PWD 459122207Sharti struct stat sb; 460122207Sharti char *pwd; 461122207Sharti#endif 462122207Sharti char mdpath[MAXPATHLEN]; 463122207Sharti char obpath[MAXPATHLEN]; 464122207Sharti char cdpath[MAXPATHLEN]; 465122207Sharti char *machine = getenv("MACHINE"); 466122207Sharti char *machine_arch = getenv("MACHINE_ARCH"); 467122207Sharti char *machine_cpu = getenv("MACHINE_CPU"); 468122207Sharti Lst sysMkPath; /* Path of sys.mk */ 469122207Sharti char *cp = NULL, *start; 470122207Sharti /* avoid faults on read-only strings */ 471122207Sharti static char syspath[] = _PATH_DEFSYSPATH; 472122207Sharti 473122207Sharti#if DEFSHELL == 2 474122207Sharti /* 475122207Sharti * Turn off ENV to make ksh happier. 476122207Sharti */ 477122207Sharti unsetenv("ENV"); 478122207Sharti#endif 479122207Sharti 480122207Sharti#ifdef RLIMIT_NOFILE 481122207Sharti /* 482122207Sharti * get rid of resource limit on file descriptors 483122207Sharti */ 484122207Sharti { 485122207Sharti struct rlimit rl; 486122207Sharti if (getrlimit(RLIMIT_NOFILE, &rl) != -1 && 487122207Sharti rl.rlim_cur != rl.rlim_max) { 488122207Sharti rl.rlim_cur = rl.rlim_max; 489122207Sharti (void) setrlimit(RLIMIT_NOFILE, &rl); 490122207Sharti } 491122207Sharti } 492122207Sharti#endif 493122207Sharti /* 494122207Sharti * Find where we are and take care of PWD for the automounter... 495122207Sharti * All this code is so that we know where we are when we start up 496122207Sharti * on a different machine with pmake. 497122207Sharti */ 498122207Sharti curdir = cdpath; 499122207Sharti if (getcwd(curdir, MAXPATHLEN) == NULL) 500122207Sharti err(2, NULL); 501122207Sharti 502122207Sharti if (stat(curdir, &sa) == -1) 503122207Sharti err(2, "%s", curdir); 504122207Sharti 505122207Sharti#ifdef WANT_ENV_PWD 506122207Sharti if ((pwd = getenv("PWD")) != NULL) { 507122207Sharti if (stat(pwd, &sb) == 0 && sa.st_ino == sb.st_ino && 508122207Sharti sa.st_dev == sb.st_dev) 509122207Sharti (void) strcpy(curdir, pwd); 510122207Sharti } 511122207Sharti#endif 512122207Sharti 513122207Sharti#if defined(__i386__) && defined(__FreeBSD_version) && \ 514122207Sharti __FreeBSD_version > 300003 515122207Sharti /* 516122207Sharti * PC-98 kernel sets the `i386' string to the utsname.machine and 517122207Sharti * it cannot be distinguished from IBM-PC by uname(3). Therefore, 518122207Sharti * we check machine.ispc98 and adjust the machine variable before 519122207Sharti * using usname(3) below. 520122207Sharti * NOTE: machdep.ispc98 was defined on 1998/8/31. At that time, 521122207Sharti * __FreeBSD_version was defined as 300003. So, this check can 522122207Sharti * safely be done with any kernel with version > 300003. 523122207Sharti */ 524122207Sharti if (!machine) { 525122207Sharti int ispc98; 526122207Sharti size_t len; 527122207Sharti 528122207Sharti len = sizeof(ispc98); 529122207Sharti if (!sysctlbyname("machdep.ispc98", &ispc98, &len, NULL, 0)) { 530122207Sharti if (ispc98) 531122207Sharti machine = "pc98"; 532122207Sharti } 533122207Sharti } 534122207Sharti#endif 535122207Sharti 536122207Sharti /* 537122207Sharti * Get the name of this type of MACHINE from utsname 538122207Sharti * so we can share an executable for similar machines. 539122207Sharti * (i.e. m68k: amiga hp300, mac68k, sun3, ...) 540122207Sharti * 541122207Sharti * Note that while MACHINE is decided at run-time, 542122207Sharti * MACHINE_ARCH is always known at compile time. 543122207Sharti */ 544122207Sharti if (!machine) { 545122207Sharti#ifndef MACHINE 546122207Sharti struct utsname utsname; 547122207Sharti 548122207Sharti if (uname(&utsname) == -1) { 549122207Sharti perror("make: uname"); 550122207Sharti exit(2); 551122207Sharti } 552122207Sharti machine = utsname.machine; 553122207Sharti#else 554122207Sharti machine = MACHINE; 555122207Sharti#endif 556122207Sharti } 557122207Sharti 558122207Sharti if (!machine_arch) { 559122207Sharti#ifndef MACHINE_ARCH 560122207Sharti machine_arch = "unknown"; 561122207Sharti#else 562122207Sharti machine_arch = MACHINE_ARCH; 563122207Sharti#endif 564122207Sharti } 565122207Sharti 566122207Sharti /* 567122207Sharti * Set machine_cpu to the minumum supported CPU revision based 568122207Sharti * on the target architecture, if not already set. 569122207Sharti */ 570122207Sharti if (!machine_cpu) { 571122207Sharti if (!strcmp(machine_arch, "i386")) 572122207Sharti machine_cpu = "i386"; 573122207Sharti else if (!strcmp(machine_arch, "alpha")) 574122207Sharti machine_cpu = "ev4"; 575122207Sharti else 576122207Sharti machine_cpu = "unknown"; 577122207Sharti } 578122207Sharti 579122207Sharti /* 580122207Sharti * The object directory location is determined using the 581122207Sharti * following order of preference: 582122207Sharti * 583122207Sharti * 1. MAKEOBJDIRPREFIX`cwd` 584122207Sharti * 2. MAKEOBJDIR 585122207Sharti * 3. _PATH_OBJDIR.${MACHINE} 586122207Sharti * 4. _PATH_OBJDIR 587122207Sharti * 5. _PATH_OBJDIRPREFIX`cwd` 588122207Sharti * 589122207Sharti * If one of the first two fails, use the current directory. 590122207Sharti * If the remaining three all fail, use the current directory. 591122207Sharti * 592122207Sharti * Once things are initted, 593122207Sharti * have to add the original directory to the search path, 594122207Sharti * and modify the paths for the Makefiles apropriately. The 595 * current directory is also placed as a variable for make scripts. 596 */ 597 if (!(pathp = getenv("MAKEOBJDIRPREFIX"))) { 598 if (!(path = getenv("MAKEOBJDIR"))) { 599 path = _PATH_OBJDIR; 600 pathp = _PATH_OBJDIRPREFIX; 601 (void) snprintf(mdpath, MAXPATHLEN, "%s.%s", 602 path, machine); 603 if (!(objdir = chdir_verify_path(mdpath, obpath))) 604 if (!(objdir=chdir_verify_path(path, obpath))) { 605 (void) snprintf(mdpath, MAXPATHLEN, 606 "%s%s", pathp, curdir); 607 if (!(objdir=chdir_verify_path(mdpath, 608 obpath))) 609 objdir = curdir; 610 } 611 } 612 else if (!(objdir = chdir_verify_path(path, obpath))) 613 objdir = curdir; 614 } 615 else { 616 (void) snprintf(mdpath, MAXPATHLEN, "%s%s", pathp, curdir); 617 if (!(objdir = chdir_verify_path(mdpath, obpath))) 618 objdir = curdir; 619 } 620 621#ifdef WANT_ENV_PWD 622 setenv("PWD", objdir, 1); 623#endif 624 625 create = Lst_Init(FALSE); 626 makefiles = Lst_Init(FALSE); 627 envFirstVars = Lst_Init(FALSE); 628 printVars = FALSE; 629 expandVars = TRUE; 630 variables = Lst_Init(FALSE); 631 beSilent = FALSE; /* Print commands as executed */ 632 ignoreErrors = FALSE; /* Pay attention to non-zero returns */ 633 noExecute = FALSE; /* Execute all commands */ 634 keepgoing = FALSE; /* Stop on error */ 635 allPrecious = FALSE; /* Remove targets when interrupted */ 636 queryFlag = FALSE; /* This is not just a check-run */ 637 noBuiltins = FALSE; /* Read the built-in rules */ 638 touchFlag = FALSE; /* Actually update targets */ 639 usePipes = TRUE; /* Catch child output in pipes */ 640 debug = 0; /* No debug verbosity, please. */ 641 jobsRunning = FALSE; 642 643 maxLocal = DEFMAXLOCAL; /* Set default local max concurrency */ 644#ifdef REMOTE 645 maxJobs = DEFMAXJOBS; /* Set default max concurrency */ 646#else 647 maxJobs = maxLocal; 648#endif 649 forceJobs = FALSE; /* No -j flag */ 650 compatMake = FALSE; /* No compat mode */ 651 652 653 /* 654 * Initialize the parsing, directory and variable modules to prepare 655 * for the reading of inclusion paths and variable settings on the 656 * command line 657 */ 658 Dir_Init(); /* Initialize directory structures so -I flags 659 * can be processed correctly */ 660 Parse_Init(); /* Need to initialize the paths of #include 661 * directories */ 662 Var_Init(); /* As well as the lists of variables for 663 * parsing arguments */ 664 str_init(); 665 if (objdir != curdir) 666 Dir_AddDir(dirSearchPath, curdir); 667 Var_Set(".CURDIR", curdir, VAR_GLOBAL); 668 Var_Set(".OBJDIR", objdir, VAR_GLOBAL); 669 670 /* 671 * Initialize various variables. 672 * MAKE also gets this name, for compatibility 673 * .MAKEFLAGS gets set to the empty string just in case. 674 * MFLAGS also gets initialized empty, for compatibility. 675 */ 676 Var_Set("MAKE", argv[0], VAR_GLOBAL); 677 Var_Set(MAKEFLAGS, "", VAR_GLOBAL); 678 Var_Set("MFLAGS", "", VAR_GLOBAL); 679 Var_Set("MACHINE", machine, VAR_GLOBAL); 680 Var_Set("MACHINE_ARCH", machine_arch, VAR_GLOBAL); 681 Var_Set("MACHINE_CPU", machine_cpu, VAR_GLOBAL); 682 683 /* 684 * First snag any flags out of the MAKE environment variable. 685 * (Note this is *not* MAKEFLAGS since /bin/make uses that and it's 686 * in a different format). 687 */ 688#ifdef POSIX 689 Main_ParseArgLine(getenv("MAKEFLAGS")); 690#else 691 Main_ParseArgLine(getenv("MAKE")); 692#endif 693 694 MainParseArgs(argc, argv); 695 696 /* 697 * Be compatible if user did not specify -j and did not explicitly 698 * turned compatibility on 699 */ 700 if (!compatMake && !forceJobs) 701 compatMake = TRUE; 702 703 /* 704 * Initialize archive, target and suffix modules in preparation for 705 * parsing the makefile(s) 706 */ 707 Arch_Init(); 708 Targ_Init(); 709 Suff_Init(); 710 711 DEFAULT = NULL; 712 (void)time(&now); 713 714 /* 715 * Set up the .TARGETS variable to contain the list of targets to be 716 * created. If none specified, make the variable empty -- the parser 717 * will fill the thing in with the default or .MAIN target. 718 */ 719 if (!Lst_IsEmpty(create)) { 720 LstNode ln; 721 722 for (ln = Lst_First(create); ln != NULL; 723 ln = Lst_Succ(ln)) { 724 char *name = (char *)Lst_Datum(ln); 725 726 Var_Append(".TARGETS", name, VAR_GLOBAL); 727 } 728 } else 729 Var_Set(".TARGETS", "", VAR_GLOBAL); 730 731 732 /* 733 * If no user-supplied system path was given (through the -m option) 734 * add the directories from the DEFSYSPATH (more than one may be given 735 * as dir1:...:dirn) to the system include path. 736 */ 737 if (Lst_IsEmpty(sysIncPath)) { 738 for (start = syspath; *start != '\0'; start = cp) { 739 for (cp = start; *cp != '\0' && *cp != ':'; cp++) 740 continue; 741 if (*cp == '\0') { 742 Dir_AddDir(sysIncPath, start); 743 } else { 744 *cp++ = '\0'; 745 Dir_AddDir(sysIncPath, start); 746 } 747 } 748 } 749 750 /* 751 * Read in the built-in rules first, followed by the specified 752 * makefile, if it was (makefile != (char *) NULL), or the default 753 * Makefile and makefile, in that order, if it wasn't. 754 */ 755 if (!noBuiltins) { 756 LstNode ln; 757 758 sysMkPath = Lst_Init (FALSE); 759 Dir_Expand (_PATH_DEFSYSMK, sysIncPath, sysMkPath); 760 if (Lst_IsEmpty(sysMkPath)) 761 Fatal("make: no system rules (%s).", _PATH_DEFSYSMK); 762 ln = Lst_Find(sysMkPath, (void *)NULL, ReadMakefile); 763 if (ln != NULL) 764 Fatal("make: cannot open %s.", (char *)Lst_Datum(ln)); 765 } 766 767 if (!Lst_IsEmpty(makefiles)) { 768 LstNode ln; 769 770 ln = Lst_Find(makefiles, (void *)NULL, ReadMakefile); 771 if (ln != NULL) 772 Fatal("make: cannot open %s.", (char *)Lst_Datum(ln)); 773 } else if (!ReadMakefile("makefile", NULL)) 774 (void)ReadMakefile("Makefile", NULL); 775 776 (void)ReadMakefile(".depend", NULL); 777 778 Var_Append("MFLAGS", Var_Value(MAKEFLAGS, VAR_GLOBAL, &p1), VAR_GLOBAL); 779 efree(p1); 780 781 /* Install all the flags into the MAKE envariable. */ 782 if (((p = Var_Value(MAKEFLAGS, VAR_GLOBAL, &p1)) != NULL) && *p) 783#ifdef POSIX 784 setenv("MAKEFLAGS", p, 1); 785#else 786 setenv("MAKE", p, 1); 787#endif 788 efree(p1); 789 790 /* 791 * For compatibility, look at the directories in the VPATH variable 792 * and add them to the search path, if the variable is defined. The 793 * variable's value is in the same format as the PATH envariable, i.e. 794 * <directory>:<directory>:<directory>... 795 */ 796 if (Var_Exists("VPATH", VAR_CMD)) { 797 char *vpath, *path, *cp, savec; 798 /* 799 * GCC stores string constants in read-only memory, but 800 * Var_Subst will want to write this thing, so store it 801 * in an array 802 */ 803 static char VPATH[] = "${VPATH}"; 804 805 vpath = Var_Subst(NULL, VPATH, VAR_CMD, FALSE); 806 path = vpath; 807 do { 808 /* skip to end of directory */ 809 for (cp = path; *cp != ':' && *cp != '\0'; cp++) 810 continue; 811 /* Save terminator character so know when to stop */ 812 savec = *cp; 813 *cp = '\0'; 814 /* Add directory to search path */ 815 Dir_AddDir(dirSearchPath, path); 816 *cp = savec; 817 path = cp + 1; 818 } while (savec == ':'); 819 (void)free(vpath); 820 } 821 822 /* 823 * Now that all search paths have been read for suffixes et al, it's 824 * time to add the default search path to their lists... 825 */ 826 Suff_DoPaths(); 827 828 /* print the initial graph, if the user requested it */ 829 if (DEBUG(GRAPH1)) 830 Targ_PrintGraph(1); 831 832 /* print the values of any variables requested by the user */ 833 if (printVars) { 834 LstNode ln; 835 836 for (ln = Lst_First(variables); ln != NULL; 837 ln = Lst_Succ(ln)) { 838 char *value; 839 if (expandVars) { 840 p1 = malloc(strlen((char *)Lst_Datum(ln)) + 1 + 3); 841 if (!p1) 842 Punt("make: cannot allocate memory."); 843 /* This sprintf is safe, because of the malloc above */ 844 (void)sprintf(p1, "${%s}", (char *)Lst_Datum(ln)); 845 value = Var_Subst(NULL, p1, VAR_GLOBAL, FALSE); 846 } else { 847 value = Var_Value((char *)Lst_Datum(ln), 848 VAR_GLOBAL, &p1); 849 } 850 printf("%s\n", value ? value : ""); 851 if (p1) 852 free(p1); 853 } 854 } 855 856 /* 857 * Have now read the entire graph and need to make a list of targets 858 * to create. If none was given on the command line, we consult the 859 * parsing module to find the main target(s) to create. 860 */ 861 if (Lst_IsEmpty(create)) 862 targs = Parse_MainName(); 863 else 864 targs = Targ_FindList(create, TARG_CREATE); 865 866 if (!compatMake && !printVars) { 867 /* 868 * Initialize job module before traversing the graph, now that 869 * any .BEGIN and .END targets have been read. This is done 870 * only if the -q flag wasn't given (to prevent the .BEGIN from 871 * being executed should it exist). 872 */ 873 if (!queryFlag) { 874 if (maxLocal == -1) 875 maxLocal = maxJobs; 876 Job_Init(maxJobs, maxLocal); 877 jobsRunning = TRUE; 878 } 879 880 /* Traverse the graph, checking on all the targets */ 881 outOfDate = Make_Run(targs); 882 } else if (!printVars) { 883 /* 884 * Compat_Init will take care of creating all the targets as 885 * well as initializing the module. 886 */ 887 Compat_Run(targs); 888 } 889 890 Lst_Destroy(targs, NOFREE); 891 Lst_Destroy(variables, NOFREE); 892 Lst_Destroy(makefiles, NOFREE); 893 Lst_Destroy(create, (void (*) __P((void *))) free); 894 895 /* print the graph now it's been processed if the user requested it */ 896 if (DEBUG(GRAPH2)) 897 Targ_PrintGraph(2); 898 899 Suff_End(); 900 Targ_End(); 901 Arch_End(); 902 str_end(); 903 Var_End(); 904 Parse_End(); 905 Dir_End(); 906 907 if (queryFlag && outOfDate) 908 return(1); 909 else 910 return(0); 911} 912 913/*- 914 * ReadMakefile -- 915 * Open and parse the given makefile. 916 * 917 * Results: 918 * TRUE if ok. FALSE if couldn't open file. 919 * 920 * Side Effects: 921 * lots 922 */ 923static Boolean 924ReadMakefile(p, q) 925 void *p; 926 void *q; 927{ 928 char *fname = p; /* makefile to read */ 929 extern Lst parseIncPath; 930 FILE *stream; 931 char *name, path[MAXPATHLEN]; 932 933 if (!strcmp(fname, "-")) { 934 Parse_File("(stdin)", stdin); 935 Var_Set("MAKEFILE", "", VAR_GLOBAL); 936 } else { 937 /* if we've chdir'd, rebuild the path name */ 938 if (curdir != objdir && *fname != '/') { 939 (void)snprintf(path, MAXPATHLEN, "%s/%s", curdir, fname); 940 if ((stream = fopen(path, "r")) != NULL) { 941 fname = path; 942 goto found; 943 } 944 } else if ((stream = fopen(fname, "r")) != NULL) 945 goto found; 946 /* look in -I and system include directories. */ 947 name = Dir_FindFile(fname, parseIncPath); 948 if (!name) 949 name = Dir_FindFile(fname, sysIncPath); 950 if (!name || !(stream = fopen(name, "r"))) 951 return(FALSE); 952 fname = name; 953 /* 954 * set the MAKEFILE variable desired by System V fans -- the 955 * placement of the setting here means it gets set to the last 956 * makefile specified, as it is set by SysV make. 957 */ 958found: Var_Set("MAKEFILE", fname, VAR_GLOBAL); 959 Parse_File(fname, stream); 960 (void)fclose(stream); 961 } 962 return(TRUE); 963} 964 965/*- 966 * Cmd_Exec -- 967 * Execute the command in cmd, and return the output of that command 968 * in a string. 969 * 970 * Results: 971 * A string containing the output of the command, or the empty string 972 * If err is not NULL, it contains the reason for the command failure 973 * 974 * Side Effects: 975 * The string must be freed by the caller. 976 */ 977char * 978Cmd_Exec(cmd, err) 979 char *cmd; 980 char **err; 981{ 982 char *args[4]; /* Args for invoking the shell */ 983 int fds[2]; /* Pipe streams */ 984 int cpid; /* Child PID */ 985 int pid; /* PID from wait() */ 986 char *res; /* result */ 987 int status; /* command exit status */ 988 Buffer buf; /* buffer to store the result */ 989 char *cp; 990 int cc; 991 992 993 *err = NULL; 994 995 /* 996 * Set up arguments for shell 997 */ 998 args[0] = "sh"; 999 args[1] = "-c"; 1000 args[2] = cmd; 1001 args[3] = NULL; 1002 1003 /* 1004 * Open a pipe for fetching its output 1005 */ 1006 if (pipe(fds) == -1) { 1007 *err = "Couldn't create pipe for \"%s\""; 1008 goto bad; 1009 } 1010 1011 /* 1012 * Fork 1013 */ 1014 switch (cpid = vfork()) { 1015 case 0: 1016 /* 1017 * Close input side of pipe 1018 */ 1019 (void) close(fds[0]); 1020 1021 /* 1022 * Duplicate the output stream to the shell's output, then 1023 * shut the extra thing down. Note we don't fetch the error 1024 * stream...why not? Why? 1025 */ 1026 (void) dup2(fds[1], 1); 1027 (void) close(fds[1]); 1028 1029#if DEFSHELL == 1 1030 (void) execv("/bin/sh", args); 1031#elif DEFSHELL == 2 1032 (void) execv("/bin/ksh", args); 1033#else 1034#error "DEFSHELL must be 1 or 2." 1035#endif 1036 _exit(1); 1037 /*NOTREACHED*/ 1038 1039 case -1: 1040 *err = "Couldn't exec \"%s\""; 1041 goto bad; 1042 1043 default: 1044 /* 1045 * No need for the writing half 1046 */ 1047 (void) close(fds[1]); 1048 1049 buf = Buf_Init (MAKE_BSIZE); 1050 1051 do { 1052 char result[BUFSIZ]; 1053 cc = read(fds[0], result, sizeof(result)); 1054 if (cc > 0) 1055 Buf_AddBytes(buf, cc, (Byte *) result); 1056 } 1057 while (cc > 0 || (cc == -1 && errno == EINTR)); 1058 1059 /* 1060 * Close the input side of the pipe. 1061 */ 1062 (void) close(fds[0]); 1063 1064 /* 1065 * Wait for the process to exit. 1066 */ 1067 while(((pid = wait(&status)) != cpid) && (pid >= 0)) 1068 continue; 1069 1070 if (cc == -1) 1071 *err = "Error reading shell's output for \"%s\""; 1072 1073 res = (char *)Buf_GetAll (buf, &cc); 1074 Buf_Destroy (buf, FALSE); 1075 1076 if (status) 1077 *err = "\"%s\" returned non-zero status"; 1078 1079 /* 1080 * Null-terminate the result, convert newlines to spaces and 1081 * install it in the variable. 1082 */ 1083 res[cc] = '\0'; 1084 cp = &res[cc] - 1; 1085 1086 if (*cp == '\n') { 1087 /* 1088 * A final newline is just stripped 1089 */ 1090 *cp-- = '\0'; 1091 } 1092 while (cp >= res) { 1093 if (*cp == '\n') { 1094 *cp = ' '; 1095 } 1096 cp--; 1097 } 1098 break; 1099 } 1100 return res; 1101bad: 1102 res = emalloc(1); 1103 *res = '\0'; 1104 return res; 1105} 1106 1107/*- 1108 * Error -- 1109 * Print an error message given its format. 1110 * 1111 * Results: 1112 * None. 1113 * 1114 * Side Effects: 1115 * The message is printed. 1116 */ 1117/* VARARGS */ 1118void 1119#ifdef __STDC__ 1120Error(char *fmt, ...) 1121#else 1122Error(va_alist) 1123 va_dcl 1124#endif 1125{ 1126 va_list ap; 1127#ifdef __STDC__ 1128 va_start(ap, fmt); 1129#else 1130 char *fmt; 1131 1132 va_start(ap); 1133 fmt = va_arg(ap, char *); 1134#endif 1135 (void)vfprintf(stderr, fmt, ap); 1136 va_end(ap); 1137 (void)fprintf(stderr, "\n"); 1138 (void)fflush(stderr); 1139} 1140 1141/*- 1142 * Fatal -- 1143 * Produce a Fatal error message. If jobs are running, waits for them 1144 * to finish. 1145 * 1146 * Results: 1147 * None 1148 * 1149 * Side Effects: 1150 * The program exits 1151 */ 1152/* VARARGS */ 1153void 1154#ifdef __STDC__ 1155Fatal(char *fmt, ...) 1156#else 1157Fatal(va_alist) 1158 va_dcl 1159#endif 1160{ 1161 va_list ap; 1162#ifdef __STDC__ 1163 va_start(ap, fmt); 1164#else 1165 char *fmt; 1166 1167 va_start(ap); 1168 fmt = va_arg(ap, char *); 1169#endif 1170 if (jobsRunning) 1171 Job_Wait(); 1172 1173 (void)vfprintf(stderr, fmt, ap); 1174 va_end(ap); 1175 (void)fprintf(stderr, "\n"); 1176 (void)fflush(stderr); 1177 1178 if (DEBUG(GRAPH2)) 1179 Targ_PrintGraph(2); 1180 exit(2); /* Not 1 so -q can distinguish error */ 1181} 1182 1183/* 1184 * Punt -- 1185 * Major exception once jobs are being created. Kills all jobs, prints 1186 * a message and exits. 1187 * 1188 * Results: 1189 * None 1190 * 1191 * Side Effects: 1192 * All children are killed indiscriminately and the program Lib_Exits 1193 */ 1194/* VARARGS */ 1195void 1196#ifdef __STDC__ 1197Punt(char *fmt, ...) 1198#else 1199Punt(va_alist) 1200 va_dcl 1201#endif 1202{ 1203 va_list ap; 1204#if __STDC__ 1205 va_start(ap, fmt); 1206#else 1207 char *fmt; 1208 1209 va_start(ap); 1210 fmt = va_arg(ap, char *); 1211#endif 1212 1213 (void)fprintf(stderr, "make: "); 1214 (void)vfprintf(stderr, fmt, ap); 1215 va_end(ap); 1216 (void)fprintf(stderr, "\n"); 1217 (void)fflush(stderr); 1218 1219 DieHorribly(); 1220} 1221 1222/*- 1223 * DieHorribly -- 1224 * Exit without giving a message. 1225 * 1226 * Results: 1227 * None 1228 * 1229 * Side Effects: 1230 * A big one... 1231 */ 1232void 1233DieHorribly() 1234{ 1235 if (jobsRunning) 1236 Job_AbortAll(); 1237 if (DEBUG(GRAPH2)) 1238 Targ_PrintGraph(2); 1239 exit(2); /* Not 1, so -q can distinguish error */ 1240} 1241 1242/* 1243 * Finish -- 1244 * Called when aborting due to errors in child shell to signal 1245 * abnormal exit. 1246 * 1247 * Results: 1248 * None 1249 * 1250 * Side Effects: 1251 * The program exits 1252 */ 1253void 1254Finish(errors) 1255 int errors; /* number of errors encountered in Make_Make */ 1256{ 1257 Fatal("%d error%s", errors, errors == 1 ? "" : "s"); 1258} 1259 1260/* 1261 * emalloc -- 1262 * malloc, but die on error. 1263 */ 1264void * 1265emalloc(len) 1266 size_t len; 1267{ 1268 void *p; 1269 1270 if ((p = malloc(len)) == NULL) 1271 enomem(); 1272 return(p); 1273} 1274 1275/* 1276 * estrdup -- 1277 * strdup, but die on error. 1278 */ 1279char * 1280estrdup(str) 1281 const char *str; 1282{ 1283 char *p; 1284 1285 if ((p = strdup(str)) == NULL) 1286 enomem(); 1287 return(p); 1288} 1289 1290/* 1291 * erealloc -- 1292 * realloc, but die on error. 1293 */ 1294void * 1295erealloc(ptr, size) 1296 void *ptr; 1297 size_t size; 1298{ 1299 if ((ptr = realloc(ptr, size)) == NULL) 1300 enomem(); 1301 return(ptr); 1302} 1303 1304/* 1305 * enomem -- 1306 * die when out of memory. 1307 */ 1308void 1309enomem() 1310{ 1311 err(2, NULL); 1312} 1313 1314/* 1315 * enunlink -- 1316 * Remove a file carefully, avoiding directories. 1317 */ 1318int 1319eunlink(file) 1320 const char *file; 1321{ 1322 struct stat st; 1323 1324 if (lstat(file, &st) == -1) 1325 return -1; 1326 1327 if (S_ISDIR(st.st_mode)) { 1328 errno = EISDIR; 1329 return -1; 1330 } 1331 return unlink(file); 1332} 1333 1334/* 1335 * usage -- 1336 * exit with usage message 1337 */ 1338static void 1339usage() 1340{ 1341 (void)fprintf(stderr, "%s\n%s\n%s\n", 1342"usage: make [-Beiknqrstv] [-D variable] [-d flags] [-E variable] [-f makefile]", 1343" [-I directory] [-j max_jobs] [-m directory] [-V variable]", 1344" [variable=value] [target ...]"); 1345 exit(2); 1346} 1347 1348 1349int 1350PrintAddr(a, b) 1351 void * a; 1352 void * b; 1353{ 1354 printf("%lx ", (unsigned long) a); 1355 return b ? 0 : 0; 1356} 1357