#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>
__FBSDID("$FreeBSD: src/usr.bin/make/main.c,v 1.159 2005/12/05 14:22:12 ru Exp $");
#include <sys/param.h>
#include <sys/stat.h>
#include <sys/sysctl.h>
#include <sys/time.h>
#include <sys/queue.h>
#include <sys/resource.h>
#include <sys/utsname.h>
#include <sys/wait.h>
#include <err.h>
#include <errno.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#ifdef __APPLE__
#include <dirent.h>
#include <libgen.h>
#include <mach-o/dyld.h>
#endif
#include "arch.h"
#include "buf.h"
#include "config.h"
#include "dir.h"
#include "globals.h"
#include "job.h"
#include "make.h"
#include "parse.h"
#include "pathnames.h"
#include "shell.h"
#include "str.h"
#include "suff.h"
#include "targ.h"
#include "util.h"
#include "var.h"
extern char **environ;
#define WANT_ENV_MKLVL 1
#define MKLVL_MAXVAL 500
#define MKLVL_ENVVAR "__MKLVL__"
static Lst makefiles = Lst_Initializer(makefiles);
static Lst variables = Lst_Initializer(variables);
static Boolean expandVars;
static Boolean noBuiltins;
static Boolean forceJobs;
static char *curdir;
static char *objdir;
Lst envFirstVars = Lst_Initializer(envFirstVars);
Lst create = Lst_Initializer(create);
Boolean allPrecious;
Boolean beSilent;
Boolean beVerbose;
Boolean compatMake;
int debug;
Boolean ignoreErrors;
int jobLimit;
Boolean jobsRunning;
Boolean keepgoing;
Boolean noExecute;
Boolean queryFlag;
Boolean touchFlag;
Boolean usePipes;
uint32_t warn_cmd;
uint32_t warn_flags;
uint32_t warn_nocmd;
time_t now;
struct GNode *DEFAULT;
static void
usage(void)
{
fprintf(stderr,
"usage: make [-BPSXeiknqrstv] [-C directory] [-D variable]\n"
"\t[-d flags] [-E variable] [-f makefile] [-I directory]\n"
"\t[-j max_jobs] [-m directory] [-V variable]\n"
"\t[variable=value] [target ...]\n");
exit(2);
}
static void
MFLAGS_append(const char *flag, char *arg)
{
char *str;
Var_Append(".MAKEFLAGS", flag, VAR_GLOBAL);
if (arg != NULL) {
str = MAKEFLAGS_quote(arg);
Var_Append(".MAKEFLAGS", str, VAR_GLOBAL);
free(str);
}
Var_Append("MFLAGS", flag, VAR_GLOBAL);
if (arg != NULL) {
str = MAKEFLAGS_quote(arg);
Var_Append("MFLAGS", str, VAR_GLOBAL);
free(str);
}
}
int
Main_ParseWarn(const char *arg, int iscmd)
{
int i, neg;
static const struct {
const char *option;
uint32_t flag;
} options[] = {
{ "dirsyntax", WARN_DIRSYNTAX },
{ NULL, 0 }
};
neg = 0;
if (arg[0] == 'n' && arg[1] == 'o') {
neg = 1;
arg += 2;
}
for (i = 0; options[i].option != NULL; i++)
if (strcmp(arg, options[i].option) == 0)
break;
if (options[i].option == NULL)
return (-1);
if (iscmd) {
if (!neg) {
warn_cmd |= options[i].flag;
warn_nocmd &= ~options[i].flag;
warn_flags |= options[i].flag;
} else {
warn_nocmd |= options[i].flag;
warn_cmd &= ~options[i].flag;
warn_flags &= ~options[i].flag;
}
} else {
if (!neg) {
warn_flags |= (options[i].flag & ~warn_nocmd);
} else {
warn_flags &= ~(options[i].flag | warn_cmd);
}
}
return (0);
}
static Boolean
ReadMakefile(const char p[])
{
char *fname, *fnamesave;
FILE *stream;
char *name, path[MAXPATHLEN];
char *MAKEFILE;
int setMAKEFILE;
#ifdef __APPLE__
DIR *dir;
int status;
struct dirent entry;
struct dirent *result;
#endif
fnamesave = fname = estrdup(p);
if (!strcmp(fname, "-")) {
Parse_File("(stdin)", stdin);
Var_SetGlobal("MAKEFILE", "");
} else {
setMAKEFILE = strcmp(fname, ".depend");
if (curdir != objdir && *fname != '/') {
snprintf(path, MAXPATHLEN, "%s/%s", curdir, fname);
#ifdef __APPLE__
dir = opendir(curdir);
} else {
snprintf(path, MAXPATHLEN, "%s", fname);
dir = opendir(dirname(fname));
}
if (dir != NULL) {
for (status = readdir_r(dir, &entry, &result); status == 0 && result != NULL; status = readdir_r(dir, &entry, &result)) {
if (!strcmp(entry.d_name, fname)) {
if ((stream = fopen(path, "r")) != NULL) {
MAKEFILE = fname;
fname = path;
closedir(dir);
goto found;
}
}
}
closedir(dir);
}
#else
#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
#endif
name = Path_FindFile(fname, &parseIncPath);
if (!name)
name = Path_FindFile(fname, &sysIncPath);
if (!name || !(stream = fopen(name, "r"))) {
free(fnamesave);
return (FALSE);
}
MAKEFILE = fname = name;
found:
if (setMAKEFILE)
Var_SetGlobal("MAKEFILE", MAKEFILE);
Parse_File(fname, stream);
}
free(fnamesave);
return (TRUE);
}
static void
MainParseArgs(int argc, char **argv)
{
int c;
Boolean found_dd = FALSE;
rearg:
optind = 1;
optreset = 1;
#define OPTFLAGS "ABC:D:E:I:PSV:Xd:ef:ij:km:nqrstvx:"
for (;;) {
if ((optind < argc) && strcmp(argv[optind], "--") == 0) {
found_dd = TRUE;
}
if ((c = getopt(argc, argv, OPTFLAGS)) == -1) {
break;
}
switch(c) {
case 'A':
arch_fatal = FALSE;
MFLAGS_append("-A", NULL);
break;
case 'C':
if (chdir(optarg) == -1)
err(1, "chdir %s", optarg);
break;
case 'D':
Var_SetGlobal(optarg, "1");
MFLAGS_append("-D", optarg);
break;
case 'I':
Parse_AddIncludeDir(optarg);
MFLAGS_append("-I", optarg);
break;
case 'V':
Lst_AtEnd(&variables, estrdup(optarg));
MFLAGS_append("-V", optarg);
break;
case 'X':
expandVars = FALSE;
break;
case 'B':
compatMake = TRUE;
MFLAGS_append("-B", NULL);
unsetenv("MAKE_JOBS_FIFO");
break;
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':
Lst_AtEnd(&envFirstVars, estrdup(optarg));
MFLAGS_append("-E", optarg);
break;
case 'e':
checkEnvFirst = TRUE;
MFLAGS_append("-e", NULL);
break;
case 'f':
Lst_AtEnd(&makefiles, estrdup(optarg));
break;
case 'i':
ignoreErrors = TRUE;
MFLAGS_append("-i", NULL);
break;
case 'j': {
char *endptr;
forceJobs = TRUE;
jobLimit = strtol(optarg, &endptr, 10);
if (jobLimit <= 0 || *endptr != '\0') {
warnx("illegal number, -j argument -- %s",
optarg);
usage();
}
MFLAGS_append("-j", optarg);
break;
}
case 'k':
keepgoing = TRUE;
MFLAGS_append("-k", NULL);
break;
case 'm':
Path_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;
case 'x':
if (Main_ParseWarn(optarg, 1) != -1)
MFLAGS_append("-x", optarg);
break;
default:
case '?':
usage();
}
}
argv += optind;
argc -= optind;
oldVars = TRUE;
for (; *argv != NULL; ++argv, --argc) {
if (Parse_IsVar(*argv)) {
char *ptr = MAKEFLAGS_quote(*argv);
Var_Append(".MAKEFLAGS", ptr, VAR_GLOBAL);
Parse_DoVar(*argv, VAR_CMD);
free(ptr);
} else if ((*argv)[0] == '-') {
if ((*argv)[1] == '\0') {
} else if (found_dd) {
Lst_AtEnd(&create, estrdup(*argv));
} else {
argc++;
argv--;
goto rearg;
}
} else if ((*argv)[0] == '\0') {
Punt("illegal (null) argument.");
} else {
Lst_AtEnd(&create, estrdup(*argv));
}
}
}
void
Main_ParseArgLine(char *line, int mflags)
{
ArgArray aa;
if (line == NULL)
return;
for (; *line == ' '; ++line)
continue;
if (!*line)
return;
if (mflags)
MAKEFLAGS_break(&aa, line);
else
brk_string(&aa, line, TRUE);
MainParseArgs(aa.argc, aa.argv);
ArgArray_Done(&aa);
}
static char *
chdir_verify_path(const 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 (NULL);
}
return (obpath);
}
return (NULL);
}
static void
check_make_level(void)
{
#ifdef WANT_ENV_MKLVL
char *value = getenv(MKLVL_ENVVAR);
int level = (value == NULL) ? 0 : atoi(value);
if (level < 0) {
errc(2, EAGAIN, "Invalid value for recursion level (%d).",
level);
} else if (level > MKLVL_MAXVAL) {
errc(2, EAGAIN, "Max recursion level (%d) exceeded.",
MKLVL_MAXVAL);
} else {
char new_value[32];
sprintf(new_value, "%d", level + 1);
setenv(MKLVL_ENVVAR, new_value, 1);
}
#endif
}
int
main(int argc, char **argv)
{
const char *machine;
const char *machine_arch;
const char *machine_cpu;
Boolean outOfDate = TRUE;
const char *p;
const char *pathp;
const char *path;
char mdpath[MAXPATHLEN];
char obpath[MAXPATHLEN];
char cdpath[MAXPATHLEN];
char *cp = NULL, *start;
expandVars = TRUE;
noBuiltins = FALSE;
forceJobs = FALSE;
curdir = cdpath;
beSilent = FALSE;
ignoreErrors = FALSE;
noExecute = FALSE;
keepgoing = FALSE;
allPrecious = FALSE;
queryFlag = FALSE;
touchFlag = FALSE;
usePipes = TRUE;
debug = 0;
jobsRunning = FALSE;
jobLimit = DEFMAXJOBS;
compatMake = FALSE;
check_make_level();
#ifdef RLIMIT_NOFILE
{
struct rlimit rl;
if (getrlimit(RLIMIT_NOFILE, &rl) == -1) {
err(2, "getrlimit");
}
#ifdef __APPLE__
if (rl.rlim_max != RLIM_INFINITY)
rl.rlim_cur = rl.rlim_max;
else
rl.rlim_cur = OPEN_MAX;
#else
rl.rlim_cur = rl.rlim_max;
#endif
if (setrlimit(RLIMIT_NOFILE, &rl) == -1) {
err(2, "setrlimit");
}
}
#endif
if ((machine = getenv("MACHINE")) == NULL) {
int ispc98;
size_t len;
len = sizeof(ispc98);
if (!sysctlbyname("machdep.ispc98", &ispc98, &len, NULL, 0)) {
if (ispc98)
machine = "pc98";
}
}
if (machine == NULL) {
static struct utsname utsname;
if (uname(&utsname) == -1)
err(2, "uname");
machine = utsname.machine;
}
if ((machine_arch = getenv("MACHINE_ARCH")) == NULL) {
#ifdef MACHINE_ARCH
machine_arch = MACHINE_ARCH;
#else
machine_arch = "unknown";
#endif
}
if ((machine_cpu = getenv("MACHINE_CPU")) == NULL) {
if (!strcmp(machine_arch, "i386"))
machine_cpu = "i386";
else if (!strcmp(machine_arch, "alpha"))
machine_cpu = "ev4";
else
machine_cpu = "unknown";
}
Proc_Init();
Dir_Init();
Var_Init(environ);
Shell_Init();
Var_SetGlobal("MAKE", argv[0]);
Var_SetGlobal(".MAKEFLAGS", "");
Var_SetGlobal("MFLAGS", "");
Var_SetGlobal("MACHINE", machine);
Var_SetGlobal("MACHINE_ARCH", machine_arch);
Var_SetGlobal("MACHINE_CPU", machine_cpu);
#ifdef MAKE_VERSION
Var_SetGlobal("MAKE_VERSION", MAKE_VERSION);
#endif
Main_ParseArgLine(getenv("MAKEFLAGS"), 1);
MainParseArgs(argc, argv);
if (getcwd(curdir, MAXPATHLEN) == NULL)
err(2, NULL);
{
struct stat sa;
if (stat(curdir, &sa) == -1)
err(2, "%s", curdir);
}
if (!(pathp = getenv("MAKEOBJDIRPREFIX"))) {
if (!(path = getenv("MAKEOBJDIR"))) {
path = PATH_OBJDIR;
pathp = PATH_OBJDIRPREFIX;
snprintf(mdpath, MAXPATHLEN, "%s.%s", path, machine);
if (!(objdir = chdir_verify_path(mdpath, obpath)))
if (!(objdir=chdir_verify_path(path, obpath))) {
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 {
snprintf(mdpath, MAXPATHLEN, "%s%s", pathp, curdir);
if (!(objdir = chdir_verify_path(mdpath, obpath)))
objdir = curdir;
}
Dir_InitDot();
if (objdir != curdir)
Path_AddDir(&dirSearchPath, curdir);
Var_SetGlobal(".ST_EXPORTVAR", "YES");
Var_SetGlobal(".CURDIR", curdir);
Var_SetGlobal(".OBJDIR", objdir);
if (getenv("MAKE_JOBS_FIFO") != NULL)
forceJobs = TRUE;
if (!compatMake && !forceJobs)
compatMake = TRUE;
Targ_Init();
Suff_Init();
DEFAULT = NULL;
time(&now);
if (Lst_IsEmpty(&create)) {
Var_SetGlobal(".TARGETS", "");
} else {
LstNode *ln;
for (ln = Lst_First(&create); ln != NULL; ln = Lst_Succ(ln)) {
char *name = Lst_Datum(ln);
Var_Append(".TARGETS", name, VAR_GLOBAL);
}
}
if (TAILQ_EMPTY(&sysIncPath)) {
char syspath[] = PATH_DEFSYSPATH;
#ifdef __APPLE__
char *prefixes[2] = { NULL, NULL };
char *devdir = getenv("DEVELOPER_DIR");
char **pf;
char execpath[MAXPATHLEN];
uint32_t execpathsize = sizeof(execpath);
char *rel;
if (devdir && *devdir != '/')
devdir = NULL;
prefixes[0] = devdir;
if (_NSGetExecutablePath(execpath, &execpathsize) != 0)
strcpy(execpath, "/usr/bin/bsdmake");
if ((cp = strrchr(execpath, '/')) != NULL)
*cp = 0;
if ((cp = strrchr(execpath, '/')) != NULL)
*cp = 0;
if ((cp = strrchr(execpath, '/')) != NULL)
*cp = 0;
rel = (cp > execpath) ? execpath : NULL;
pf = prefixes;
do {
#endif
for (start = syspath; *start != '\0'; start = cp) {
for (cp = start; *cp != '\0' && *cp != ':'; cp++)
continue;
if (*cp == '\0') {
#ifdef __APPLE__
Path_AddDirPrefix(&sysIncPath, *pf ? *pf : rel, start);
#else
Path_AddDir(&sysIncPath, start);
#endif
} else {
#ifdef __APPLE__
*cp = '\0';
Path_AddDirPrefix(&sysIncPath, *pf ? *pf : rel, start);
*cp++ = ':';
#else
*cp++ = '\0';
Path_AddDir(&sysIncPath, start);
#endif
}
}
#ifdef __APPLE__
} while(*pf++);
#endif
}
if (!noBuiltins) {
Lst sysMkPath = Lst_Initializer(sysMkPath);
LstNode *ln;
char defsysmk[] = PATH_DEFSYSMK;
Path_Expand(defsysmk, &sysIncPath, &sysMkPath);
if (Lst_IsEmpty(&sysMkPath))
Fatal("make: no system rules (%s).", PATH_DEFSYSMK);
LST_FOREACH(ln, &sysMkPath) {
if (!ReadMakefile(Lst_Datum(ln)))
break;
}
if (ln != NULL)
Fatal("make: cannot open %s.", (char *)Lst_Datum(ln));
Lst_Destroy(&sysMkPath, free);
}
if (!Lst_IsEmpty(&makefiles)) {
LstNode *ln;
LST_FOREACH(ln, &makefiles) {
if (!ReadMakefile(Lst_Datum(ln)))
break;
}
if (ln != NULL)
Fatal("make: cannot open %s.", (char *)Lst_Datum(ln));
} else if (!ReadMakefile("BSDmakefile"))
if (!ReadMakefile("makefile"))
ReadMakefile("Makefile");
ReadMakefile(".depend");
if (((p = Var_Value(".MAKEFLAGS", VAR_GLOBAL)) != NULL) && *p)
setenv("MAKEFLAGS", p, 1);
if (Var_Exists("VPATH", VAR_CMD)) {
static char VPATH[] = "${VPATH}";
Buffer *buf;
char *vpath;
char *ptr;
char savec;
buf = Var_Subst(VPATH, VAR_CMD, FALSE);
vpath = Buf_Data(buf);
do {
for (ptr = vpath; *ptr != ':' && *ptr != '\0'; ptr++)
;
savec = *ptr;
*ptr = '\0';
Path_AddDir(&dirSearchPath, vpath);
vpath = ptr + 1;
} while (savec != '\0');
Buf_Destroy(buf, TRUE);
}
Suff_DoPaths();
if (DEBUG(GRAPH1))
Targ_PrintGraph(1);
if (Lst_IsEmpty(&variables)) {
Lst targs = Lst_Initializer(targs);
if (Lst_IsEmpty(&create))
Parse_MainName(&targs);
else
Targ_FindList(&targs, &create, TARG_CREATE);
if (compatMake) {
Compat_Run(&targs);
outOfDate = 0;
} else {
if (!queryFlag) {
Job_Init(jobLimit);
jobsRunning = TRUE;
}
outOfDate = Make_Run(&targs);
}
Lst_Destroy(&targs, NOFREE);
} else {
Var_Print(&variables, expandVars);
}
Lst_Destroy(&variables, free);
Lst_Destroy(&makefiles, free);
Lst_Destroy(&create, free);
if (DEBUG(GRAPH2))
Targ_PrintGraph(2);
if (queryFlag && outOfDate)
return (1);
else
return (0);
}