#ifndef lint
#if 0
static char copyright[] =
"@(#) Copyright (c) 1988, 1989, 1990, 1993\n\
The Regents of the University of California. All rights reserved.\n";
#endif
#endif
#include <sys/cdefs.h>
#include <sys/param.h>
#include <sys/signal.h>
#include <sys/stat.h>
#if defined(__i386__)
#include <sys/sysctl.h>
#endif
#include <sys/time.h>
#include <sys/resource.h>
#ifndef MACHINE
#include <sys/utsname.h>
#endif
#include <sys/wait.h>
#include <err.h>
#include <errno.h>
#include <fcntl.h>
#include <signal.h>
#include <stdarg.h>
#include <stdio.h>
#include <stdlib.h>
#include <sysexits.h>
#include <unistd.h>
#include "make.h"
#include "hash.h"
#include "dir.h"
#include "job.h"
#include "pathnames.h"
#define WANT_ENV_MKLVL 1
#ifndef DEFMAXLOCAL
#define DEFMAXLOCAL DEFMAXJOBS
#endif
#define MAKEFLAGS ".MAKEFLAGS"
Lst create;
time_t now;
GNode *DEFAULT;
Boolean allPrecious;
static Boolean noBuiltins;
static Lst makefiles;
static Boolean expandVars;
static Lst variables;
int maxJobs;
static Boolean forceJobs;
static int maxLocal;
Boolean compatMake;
Boolean debug;
Boolean noExecute;
Boolean keepgoing;
Boolean queryFlag;
Boolean touchFlag;
Boolean usePipes;
Boolean ignoreErrors;
Boolean beSilent;
Boolean beVerbose;
Boolean oldVars;
Boolean checkEnvFirst;
Lst envFirstVars;
Boolean jobsRunning;
static void MainParseArgs(int, char **);
char * chdir_verify_path(char *, char *);
static int ReadMakefile(void *, void *);
static void usage(void);
static char *curdir;
static char *objdir;
static void
MFLAGS_append(char *flag, char *arg)
{
Var_Append(MAKEFLAGS, flag, VAR_GLOBAL);
if (arg != NULL)
Var_Append(MAKEFLAGS, arg, VAR_GLOBAL);
Var_Append("MFLAGS", flag, VAR_GLOBAL);
if (arg != NULL)
Var_Append("MFLAGS", arg, VAR_GLOBAL);
}
static void
MainParseArgs(int argc, char **argv)
{
char *p;
int c;
optind = 1;
#ifdef REMOTE
# define OPTFLAGS "BC:D:E:I:L:PSV:Xd:ef:ij:km:nqrstv"
#else
# define OPTFLAGS "BC:D:E:I:PSV:Xd:ef:ij:km:nqrstv"
#endif
rearg: while((c = getopt(argc, argv, OPTFLAGS)) != -1) {
switch(c) {
case 'C':
if (chdir(optarg) == -1)
err(1, "chdir %s", optarg);
break;
case 'D':
Var_Set(optarg, "1", VAR_GLOBAL);
MFLAGS_append("-D", optarg);
break;
case 'I':
Parse_AddIncludeDir(optarg);
MFLAGS_append("-I", optarg);
break;
case 'V':
(void)Lst_AtEnd(variables, (void *)optarg);
MFLAGS_append("-V", optarg);
break;
case 'X':
expandVars = FALSE;
break;
case 'B':
compatMake = TRUE;
MFLAGS_append("-B", NULL);
break;
#ifdef REMOTE
case 'L': {
char *endptr;
maxLocal = strtol(optarg, &endptr, 10);
if (maxLocal < 0 || *endptr != '\0') {
warnx("illegal number, -L argument -- %s",
optarg);
usage();
}
MFLAGS_append("-L", optarg);
break;
}
#endif
case 'P':
usePipes = FALSE;
MFLAGS_append("-P", NULL);
break;
case 'S':
keepgoing = FALSE;
MFLAGS_append("-S", NULL);
break;
case 'd': {
char *modules = optarg;
for (; *modules; ++modules)
switch (*modules) {
case 'A':
debug = ~0;
break;
case 'a':
debug |= DEBUG_ARCH;
break;
case 'c':
debug |= DEBUG_COND;
break;
case 'd':
debug |= DEBUG_DIR;
break;
case 'f':
debug |= DEBUG_FOR;
break;
case 'g':
if (modules[1] == '1') {
debug |= DEBUG_GRAPH1;
++modules;
}
else if (modules[1] == '2') {
debug |= DEBUG_GRAPH2;
++modules;
}
break;
case 'j':
debug |= DEBUG_JOB;
break;
case 'l':
debug |= DEBUG_LOUD;
break;
case 'm':
debug |= DEBUG_MAKE;
break;
case 's':
debug |= DEBUG_SUFF;
break;
case 't':
debug |= DEBUG_TARG;
break;
case 'v':
debug |= DEBUG_VAR;
break;
default:
warnx("illegal argument to d option -- %c", *modules);
usage();
}
MFLAGS_append("-d", optarg);
break;
}
case 'E':
p = emalloc(strlen(optarg) + 1);
(void)strcpy(p, optarg);
(void)Lst_AtEnd(envFirstVars, (void *)p);
MFLAGS_append("-E", optarg);
break;
case 'e':
checkEnvFirst = TRUE;
MFLAGS_append("-e", NULL);
break;
case 'f':
(void)Lst_AtEnd(makefiles, (void *)optarg);
break;
case 'i':
ignoreErrors = TRUE;
MFLAGS_append("-i", NULL);
break;
case 'j': {
char *endptr;
forceJobs = TRUE;
maxJobs = strtol(optarg, &endptr, 10);
if (maxJobs <= 0 || *endptr != '\0') {
warnx("illegal number, -j argument -- %s",
optarg);
usage();
}
#ifndef REMOTE
maxLocal = maxJobs;
#endif
MFLAGS_append("-j", optarg);
break;
}
case 'k':
keepgoing = TRUE;
MFLAGS_append("-k", NULL);
break;
case 'm':
Dir_AddDir(sysIncPath, optarg);
MFLAGS_append("-m", optarg);
break;
case 'n':
noExecute = TRUE;
MFLAGS_append("-n", NULL);
break;
case 'q':
queryFlag = TRUE;
MFLAGS_append("-q", NULL);
break;
case 'r':
noBuiltins = TRUE;
MFLAGS_append("-r", NULL);
break;
case 's':
beSilent = TRUE;
MFLAGS_append("-s", NULL);
break;
case 't':
touchFlag = TRUE;
MFLAGS_append("-t", NULL);
break;
case 'v':
beVerbose = TRUE;
MFLAGS_append("-v", NULL);
break;
default:
case '?':
usage();
}
}
oldVars = TRUE;
for (argv += optind, argc -= optind; *argv; ++argv, --argc)
if (Parse_IsVar(*argv)) {
char *ptr = Var_Quote(*argv);
Var_Append(MAKEFLAGS, ptr, VAR_GLOBAL);
free(ptr);
Parse_DoVar(*argv, VAR_CMD);
} else {
if (!**argv)
Punt("illegal (null) argument.");
if (**argv == '-') {
if ((*argv)[1])
optind = 0;
else
optind = 1;
goto rearg;
}
(void)Lst_AtEnd(create, (void *)estrdup(*argv));
}
}
void
Main_ParseArgLine(char *line)
{
char **argv;
int argc;
if (line == NULL)
return;
for (; *line == ' '; ++line)
continue;
if (!*line)
return;
argv = brk_string(line, &argc, TRUE);
MainParseArgs(argc, argv);
}
char *
chdir_verify_path(char *path, char *obpath)
{
struct stat sb;
if (stat(path, &sb) == 0 && S_ISDIR(sb.st_mode)) {
if (chdir(path) == -1 || getcwd(obpath, MAXPATHLEN) == NULL) {
warn("warning: %s", path);
return 0;
}
return obpath;
}
return 0;
}
static void
catch_child(int sig __unused)
{
}
int
main(int argc, char **argv)
{
Lst targs;
Boolean outOfDate = TRUE;
struct stat sa;
char *p, *p1, *path, *pathp;
#ifdef WANT_ENV_MKLVL
#define MKLVL_MAXVAL 500
#define MKLVL_ENVVAR "__MKLVL__"
int iMkLvl = 0;
char *szMkLvl = getenv(MKLVL_ENVVAR);
#endif
char mdpath[MAXPATHLEN];
char obpath[MAXPATHLEN];
char cdpath[MAXPATHLEN];
char *machine = getenv("MACHINE");
char *machine_arch = getenv("MACHINE_ARCH");
char *machine_cpu = getenv("MACHINE_CPU");
Lst sysMkPath;
char *cp = NULL, *start;
static char syspath[] = _PATH_DEFSYSPATH;
{
struct sigaction sa;
sigemptyset(&sa.sa_mask);
sa.sa_flags = SA_RESTART | SA_NOCLDSTOP;
sa.sa_handler = catch_child;
sigaction(SIGCHLD, &sa, NULL);
}
#ifdef WANT_ENV_MKLVL
if ((iMkLvl = szMkLvl ? atoi(szMkLvl) : 0) < 0) {
iMkLvl = 0;
}
if (iMkLvl++ > MKLVL_MAXVAL) {
errc(2, EAGAIN,
"Max recursion level (%d) exceeded.", MKLVL_MAXVAL);
}
bzero(szMkLvl = emalloc(32), 32);
sprintf(szMkLvl, "%d", iMkLvl);
setenv(MKLVL_ENVVAR, szMkLvl, 1);
#endif
#if DEFSHELL == 2
unsetenv("ENV");
#endif
#ifdef RLIMIT_NOFILE
{
struct rlimit rl;
if (getrlimit(RLIMIT_NOFILE, &rl) != -1 &&
rl.rlim_cur != rl.rlim_max) {
rl.rlim_cur = rl.rlim_max;
(void) setrlimit(RLIMIT_NOFILE, &rl);
}
}
#endif
if (!machine) {
int ispc98;
size_t len;
len = sizeof(ispc98);
if (!sysctlbyname("machdep.ispc98", &ispc98, &len, NULL, 0)) {
if (ispc98)
machine = "pc98";
}
}
if (!machine) {
#ifndef MACHINE
struct utsname utsname;
if (uname(&utsname) == -1)
err(2, "uname");
machine = utsname.machine;
#else
machine = MACHINE;
#endif
}
if (!machine_arch) {
#ifndef MACHINE_ARCH
machine_arch = "unknown";
#else
machine_arch = MACHINE_ARCH;
#endif
}
if (!machine_cpu) {
if (!strcmp(machine_arch, "i386"))
machine_cpu = "i386";
else if (!strcmp(machine_arch, "alpha"))
machine_cpu = "ev4";
else
machine_cpu = "unknown";
}
create = Lst_Init(FALSE);
makefiles = Lst_Init(FALSE);
envFirstVars = Lst_Init(FALSE);
expandVars = TRUE;
variables = Lst_Init(FALSE);
beSilent = FALSE;
ignoreErrors = FALSE;
noExecute = FALSE;
keepgoing = FALSE;
allPrecious = FALSE;
queryFlag = FALSE;
noBuiltins = FALSE;
touchFlag = FALSE;
usePipes = TRUE;
debug = 0;
jobsRunning = FALSE;
maxLocal = DEFMAXLOCAL;
#ifdef REMOTE
maxJobs = DEFMAXJOBS;
#else
maxJobs = maxLocal;
#endif
forceJobs = FALSE;
compatMake = FALSE;
Dir_Init();
Parse_Init();
Var_Init();
str_init();
Var_Set("MAKE", argv[0], VAR_GLOBAL);
Var_Set(MAKEFLAGS, "", VAR_GLOBAL);
Var_Set("MFLAGS", "", VAR_GLOBAL);
Var_Set("MACHINE", machine, VAR_GLOBAL);
Var_Set("MACHINE_ARCH", machine_arch, VAR_GLOBAL);
Var_Set("MACHINE_CPU", machine_cpu, VAR_GLOBAL);
#ifdef MAKE_VERSION
Var_Set("MAKE_VERSION", MAKE_VERSION, VAR_GLOBAL);
#endif
#ifdef POSIX
Main_ParseArgLine(getenv("MAKEFLAGS"));
#else
Main_ParseArgLine(getenv("MAKE"));
#endif
MainParseArgs(argc, argv);
curdir = cdpath;
if (getcwd(curdir, MAXPATHLEN) == NULL)
err(2, NULL);
if (stat(curdir, &sa) == -1)
err(2, "%s", curdir);
if (!(pathp = getenv("MAKEOBJDIRPREFIX"))) {
if (!(path = getenv("MAKEOBJDIR"))) {
path = _PATH_OBJDIR;
pathp = _PATH_OBJDIRPREFIX;
(void) snprintf(mdpath, MAXPATHLEN, "%s.%s",
path, machine);
if (!(objdir = chdir_verify_path(mdpath, obpath)))
if (!(objdir=chdir_verify_path(path, obpath))) {
(void) snprintf(mdpath, MAXPATHLEN,
"%s%s", pathp, curdir);
if (!(objdir=chdir_verify_path(mdpath,
obpath)))
objdir = curdir;
}
}
else if (!(objdir = chdir_verify_path(path, obpath)))
objdir = curdir;
}
else {
(void) snprintf(mdpath, MAXPATHLEN, "%s%s", pathp, curdir);
if (!(objdir = chdir_verify_path(mdpath, obpath)))
objdir = curdir;
}
Dir_InitDot();
if (objdir != curdir)
Dir_AddDir(dirSearchPath, curdir);
Var_Set(".CURDIR", curdir, VAR_GLOBAL);
Var_Set(".OBJDIR", objdir, VAR_GLOBAL);
if (!compatMake && !forceJobs)
compatMake = TRUE;
Arch_Init();
Targ_Init();
Suff_Init();
DEFAULT = NULL;
(void)time(&now);
if (!Lst_IsEmpty(create)) {
LstNode ln;
for (ln = Lst_First(create); ln != NULL;
ln = Lst_Succ(ln)) {
char *name = (char *)Lst_Datum(ln);
Var_Append(".TARGETS", name, VAR_GLOBAL);
}
} else
Var_Set(".TARGETS", "", VAR_GLOBAL);
if (Lst_IsEmpty(sysIncPath)) {
for (start = syspath; *start != '\0'; start = cp) {
for (cp = start; *cp != '\0' && *cp != ':'; cp++)
continue;
if (*cp == '\0') {
Dir_AddDir(sysIncPath, start);
} else {
*cp++ = '\0';
Dir_AddDir(sysIncPath, start);
}
}
}
if (!noBuiltins) {
LstNode ln;
sysMkPath = Lst_Init (FALSE);
Dir_Expand (_PATH_DEFSYSMK, sysIncPath, sysMkPath);
if (Lst_IsEmpty(sysMkPath))
Fatal("make: no system rules (%s).", _PATH_DEFSYSMK);
ln = Lst_Find(sysMkPath, (void *)NULL, ReadMakefile);
if (ln != NULL)
Fatal("make: cannot open %s.", (char *)Lst_Datum(ln));
}
if (!Lst_IsEmpty(makefiles)) {
LstNode ln;
ln = Lst_Find(makefiles, (void *)NULL, ReadMakefile);
if (ln != NULL)
Fatal("make: cannot open %s.", (char *)Lst_Datum(ln));
} else if (!ReadMakefile("BSDmakefile", NULL))
if (!ReadMakefile("makefile", NULL))
(void)ReadMakefile("Makefile", NULL);
(void)ReadMakefile(".depend", NULL);
if (((p = Var_Value(MAKEFLAGS, VAR_GLOBAL, &p1)) != NULL) && *p)
#ifdef POSIX
setenv("MAKEFLAGS", p, 1);
#else
setenv("MAKE", p, 1);
#endif
free(p1);
if (Var_Exists("VPATH", VAR_CMD)) {
char *vpath, savec;
static char VPATH[] = "${VPATH}";
vpath = Var_Subst(NULL, VPATH, VAR_CMD, FALSE);
path = vpath;
do {
for (cp = path; *cp != ':' && *cp != '\0'; cp++)
continue;
savec = *cp;
*cp = '\0';
Dir_AddDir(dirSearchPath, path);
*cp = savec;
path = cp + 1;
} while (savec == ':');
(void)free(vpath);
}
Suff_DoPaths();
if (DEBUG(GRAPH1))
Targ_PrintGraph(1);
if (!Lst_IsEmpty(variables)) {
LstNode ln;
for (ln = Lst_First(variables); ln != NULL;
ln = Lst_Succ(ln)) {
char *value;
if (expandVars) {
p1 = emalloc(strlen((char *)Lst_Datum(ln)) + 1 + 3);
(void)sprintf(p1, "${%s}", (char *)Lst_Datum(ln));
value = Var_Subst(NULL, p1, VAR_GLOBAL, FALSE);
} else {
value = Var_Value((char *)Lst_Datum(ln),
VAR_GLOBAL, &p1);
}
printf("%s\n", value ? value : "");
if (p1)
free(p1);
}
} else {
if (Lst_IsEmpty(create))
targs = Parse_MainName();
else
targs = Targ_FindList(create, TARG_CREATE);
if (!compatMake) {
if (!queryFlag) {
if (maxLocal == -1)
maxLocal = maxJobs;
Job_Init(maxJobs, maxLocal);
jobsRunning = TRUE;
}
outOfDate = Make_Run(targs);
} else {
Compat_Run(targs);
outOfDate = 0;
}
Lst_Destroy(targs, NOFREE);
}
Lst_Destroy(variables, NOFREE);
Lst_Destroy(makefiles, NOFREE);
Lst_Destroy(create, (void (*)(void *)) free);
if (DEBUG(GRAPH2))
Targ_PrintGraph(2);
Suff_End();
Targ_End();
Arch_End();
str_end();
Var_End();
Parse_End();
Dir_End();
if (queryFlag && outOfDate)
return(1);
else
return(0);
}
static Boolean
ReadMakefile(void *p, void *q __unused)
{
char *fname;
FILE *stream;
char *name, path[MAXPATHLEN];
char *MAKEFILE;
int setMAKEFILE;
fname = p;
if (!strcmp(fname, "-")) {
Parse_File("(stdin)", stdin);
Var_Set("MAKEFILE", "", VAR_GLOBAL);
} else {
setMAKEFILE = strcmp(fname, ".depend");
if (curdir != objdir && *fname != '/') {
(void)snprintf(path, MAXPATHLEN, "%s/%s", curdir, fname);
#if THIS_BREAKS_THINGS
if (realpath(path, path) != NULL &&
(stream = fopen(path, "r")) != NULL) {
MAKEFILE = fname;
fname = path;
goto found;
}
} else if (realpath(fname, path) != NULL) {
MAKEFILE = fname;
fname = path;
if ((stream = fopen(fname, "r")) != NULL)
goto found;
}
#else
if ((stream = fopen(path, "r")) != NULL) {
MAKEFILE = fname;
fname = path;
goto found;
}
} else {
MAKEFILE = fname;
if ((stream = fopen(fname, "r")) != NULL)
goto found;
}
#endif
name = Dir_FindFile(fname, parseIncPath);
if (!name)
name = Dir_FindFile(fname, sysIncPath);
if (!name || !(stream = fopen(name, "r")))
return(FALSE);
MAKEFILE = fname = name;
found:
if (setMAKEFILE)
Var_Set("MAKEFILE", MAKEFILE, VAR_GLOBAL);
Parse_File(fname, stream);
(void)fclose(stream);
}
return(TRUE);
}
char *
Cmd_Exec(char *cmd, char **error)
{
char *args[4];
int fds[2];
int cpid;
int pid;
char *res;
int status;
Buffer buf;
char *cp;
int cc;
*error = NULL;
args[0] = "sh";
args[1] = "-c";
args[2] = cmd;
args[3] = NULL;
if (pipe(fds) == -1) {
*error = "Couldn't create pipe for \"%s\"";
goto bad;
}
switch (cpid = vfork()) {
case 0:
(void) close(fds[0]);
(void) dup2(fds[1], 1);
(void) close(fds[1]);
#if defined(DEFSHELL) && DEFSHELL == 0
(void) execv("/bin/csh", args);
#elif DEFSHELL == 1
(void) execv("/bin/sh", args);
#elif DEFSHELL == 2
(void) execv("/bin/ksh", args);
#else
#error "DEFSHELL must be 1 or 2."
#endif
_exit(1);
case -1:
*error = "Couldn't exec \"%s\"";
goto bad;
default:
(void) close(fds[1]);
buf = Buf_Init (MAKE_BSIZE);
do {
char result[BUFSIZ];
cc = read(fds[0], result, sizeof(result));
if (cc > 0)
Buf_AddBytes(buf, cc, (Byte *) result);
}
while (cc > 0 || (cc == -1 && errno == EINTR));
(void) close(fds[0]);
while(((pid = wait(&status)) != cpid) && (pid >= 0))
continue;
if (cc == -1)
*error = "Error reading shell's output for \"%s\"";
res = (char *)Buf_GetAll (buf, &cc);
Buf_Destroy (buf, FALSE);
if (status)
*error = "\"%s\" returned non-zero status";
res[cc] = '\0';
cp = &res[cc] - 1;
if (*cp == '\n') {
*cp-- = '\0';
}
while (cp >= res) {
if (*cp == '\n') {
*cp = ' ';
}
cp--;
}
break;
}
return res;
bad:
res = emalloc(1);
*res = '\0';
return res;
}
static void
usage(void)
{
(void)fprintf(stderr, "%s\n%s\n%s\n",
"usage: make [-BPSXeiknqrstv] [-C directory] [-D variable] [-d flags]",
" [-E variable] [-f makefile] [-I directory] [-j max_jobs]",
" [-m directory] [-V variable] [variable=value] [target ...]");
exit(2);
}