#include <sys/types.h>
#include <sys/time.h>
#include <sys/wait.h>
#include <assert.h>
#include <ctype.h>
#include <errno.h>
#include <fcntl.h>
#include <limits.h>
#include <signal.h>
#include <stdint.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include "defines.h"
#include "cmd_exec.h"
#include "dir.h"
#include "engine.h"
#include "arch.h"
#include "gnode.h"
#include "targ.h"
#include "var.h"
#include "extern.h"
#include "lst.h"
#include "timestamp.h"
#include "main.h"
#include "make.h"
#include "pathnames.h"
#include "error.h"
#include "memory.h"
#include "buf.h"
#include "job.h"
#include "lowparse.h"
static void MakeTimeStamp(void *, void *);
static int rewrite_time(const char *);
static void list_parents(GNode *, FILE *);
static bool
drop_silently(const char *s)
{
size_t len;
if (s[0] == '-' && s[1] == 'l')
return true;
len = strlen(s);
if (len >=2 && s[len-2] == '.' && s[len-1] == 'a')
return true;
return false;
}
bool
node_find_valid_commands(GNode *gn)
{
if (DEBUG(DOUBLE) && (gn->type & OP_DOUBLE))
fprintf(stderr, "Warning: target %s had >1 lists of "
"shell commands (ignoring later ones)\n", gn->name);
if (OP_NOP(gn->type) && Lst_IsEmpty(&gn->commands)) {
if (drop_silently(gn->name)) {
printf("Warning: target %s", gn->name);
list_parents(gn, stdout);
printf(" does not have any command (BUG)\n");
return true;
}
if ((gn->type & OP_NODEFAULT) == 0 &&
(DEFAULT->type & OP_DUMMY) == 0 &&
!Lst_IsEmpty(&DEFAULT->commands)) {
Make_HandleUse(DEFAULT, gn);
Var(IMPSRC_INDEX, gn) = Var(TARGET_INDEX, gn);
} else if (is_out_of_date(Dir_MTime(gn))) {
return false;
}
}
return true;
}
static void
list_parents(GNode *gn, FILE *out)
{
LstNode ln;
bool first = true;
for (ln = Lst_First(&gn->parents); ln != NULL; ln = Lst_Adv(ln)) {
GNode *p = Lst_Datum(ln);
if (!p->must_make)
continue;
if (first) {
fprintf(out, " (prerequisite of:");
first = false;
}
fprintf(out, " %s", p->name);
}
if (!first)
fprintf(out, ")");
}
void
node_failure(GNode *gn)
{
const char *diag;
FILE *out;
if (gn->type & OP_OPTIONAL) {
out = stdout;
diag = "(ignored)";
} else if (keepgoing) {
out = stdout;
diag = "(continuing)";
} else {
out = stderr;
diag = "";
}
fprintf(out, "make: don't know how to make %s", gn->name);
list_parents(gn, out);
fprintf(out, "%s\n", diag);
if (out == stdout)
fflush(stdout);
else {
print_errors();
dump_unreadable();
Punt(NULL);
}
}
static int
rewrite_time(const char *name)
{
int fd;
char c;
fd = open(name, O_RDWR | O_CREAT, 0666);
if (fd < 0)
return -1;
if (read(fd, &c, 1) == 1) {
(void)lseek(fd, 0, SEEK_SET);
(void)write(fd, &c, 1);
}
(void)close(fd);
return 0;
}
void
Job_Touch(GNode *gn)
{
handle_all_signals();
if (gn->type & (OP_USE|OP_OPTIONAL|OP_PHONY)) {
return;
}
if (!Targ_Silent(gn)) {
(void)fprintf(stdout, "touch %s\n", gn->name);
(void)fflush(stdout);
}
if (noExecute) {
return;
}
if (gn->type & OP_ARCHV) {
Arch_Touch(gn);
} else {
const char *file = gn->path != NULL ? gn->path : gn->name;
if (utimes(file, NULL) == -1){
if (rewrite_time(file) == -1) {
(void)fprintf(stderr,
"*** couldn't touch %s: %s", file,
strerror(errno));
}
}
}
}
void
Make_TimeStamp(GNode *parent, GNode *child)
{
if (is_strictly_before(parent->youngest->mtime, child->mtime)) {
parent->youngest = child;
}
}
void
Make_HandleUse(GNode *cgn,
GNode *pgn)
{
GNode *gn;
LstNode ln;
assert(cgn->type & (OP_USE|OP_TRANSFORM));
if (pgn == NULL)
Fatal("Trying to apply .USE to '%s' without a parent",
cgn->name);
if ((cgn->type & OP_USE) || Lst_IsEmpty(&pgn->commands)) {
Lst_Concat(&pgn->commands, &cgn->commands);
}
for (ln = Lst_First(&cgn->children); ln != NULL;
ln = Lst_Adv(ln)) {
gn = Lst_Datum(ln);
if (Lst_AddNew(&pgn->children, gn)) {
Lst_AtEnd(&gn->parents, pgn);
pgn->children_left++;
}
}
if (DEBUG(DOUBLE) && (cgn->type & OP_DOUBLE))
fprintf(stderr,
"Warning: .USE %s expanded in %s had >1 lists of "
"shell commands (ignoring later ones)\n",
cgn->name, pgn->name);
pgn->type |= cgn->type & ~(OP_OPMASK|OP_USE|OP_TRANSFORM|OP_DOUBLE);
if (cgn->type & OP_USE)
pgn->children_left--;
}
void
Make_DoAllVar(GNode *gn)
{
GNode *child;
LstNode ln;
BUFFER allsrc, oodate;
char *target;
bool do_oodate;
int oodate_count, allsrc_count = 0;
oodate_count = 0;
allsrc_count = 0;
Var(OODATE_INDEX, gn) = "";
Var(ALLSRC_INDEX, gn) = "";
for (ln = Lst_First(&gn->children); ln != NULL; ln = Lst_Adv(ln)) {
child = Lst_Datum(ln);
if ((child->type & (OP_USE|OP_INVISIBLE)) != 0)
continue;
if (OP_NOP(child->type) ||
(target = Var(TARGET_INDEX, child)) == NULL) {
target = child->path != NULL ? child->path :
child->name;
}
do_oodate = false;
if (is_strictly_before(gn->mtime, child->mtime) ||
(!is_strictly_before(child->mtime, starttime) &&
child->built_status == REBUILT))
do_oodate = true;
if (do_oodate) {
oodate_count++;
if (oodate_count == 1)
Var(OODATE_INDEX, gn) = target;
else {
if (oodate_count == 2) {
Buf_Init(&oodate, 0);
Buf_AddString(&oodate,
Var(OODATE_INDEX, gn));
}
Buf_AddSpace(&oodate);
Buf_AddString(&oodate, target);
}
}
allsrc_count++;
if (allsrc_count == 1)
Var(ALLSRC_INDEX, gn) = target;
else {
if (allsrc_count == 2) {
Buf_Init(&allsrc, 0);
Buf_AddString(&allsrc,
Var(ALLSRC_INDEX, gn));
}
Buf_AddSpace(&allsrc);
Buf_AddString(&allsrc, target);
}
}
if (allsrc_count > 1)
Var(ALLSRC_INDEX, gn) = Buf_Retrieve(&allsrc);
if (oodate_count > 1)
Var(OODATE_INDEX, gn) = Buf_Retrieve(&oodate);
if (gn->impliedsrc)
Var(IMPSRC_INDEX, gn) = Var(TARGET_INDEX, gn->impliedsrc);
}
static void
MakeTimeStamp(void *parent, void *child)
{
Make_TimeStamp(parent, child);
}
bool
Make_OODate(GNode *gn)
{
bool oodate;
if ((gn->type & (OP_USE|OP_PHONY)) == 0) {
(void)Dir_MTime(gn);
if (DEBUG(MAKE)) {
if (!is_out_of_date(gn->mtime))
printf("modified %s...",
time_to_string(&gn->mtime));
else
printf("non-existent...");
}
}
if (gn->type & OP_USE) {
if (DEBUG(MAKE))
printf(".USE node...");
oodate = false;
} else if (gn->type & (OP_FORCE|OP_PHONY)) {
if (DEBUG(MAKE)) {
if (gn->type & OP_FORCE)
printf("! operator...");
else if (gn->type & OP_PHONY)
printf(".PHONY node...");
else
printf(".EXEC node...");
}
oodate = true;
} else if (is_strictly_before(gn->mtime, gn->youngest->mtime) ||
(gn == gn->youngest &&
(is_out_of_date(gn->mtime) || (gn->type & OP_DOUBLEDEP)))) {
if (DEBUG(MAKE)) {
if (is_strictly_before(gn->mtime, gn->youngest->mtime))
printf("modified before source(%s)...",
gn->youngest->name);
else if (is_out_of_date(gn->mtime))
printf("non-existent and no sources...");
else
printf(":: operator and no sources...");
}
oodate = true;
} else {
oodate = false;
}
if (!oodate)
Lst_ForEach(&gn->parents, MakeTimeStamp, gn);
return oodate;
}
void
job_attach_node(Job *job, GNode *node)
{
job->node = node;
job->node->built_status = BUILDING;
job->next_cmd = Lst_First(&node->commands);
job->exit_type = JOB_EXIT_OKAY;
job->location = NULL;
job->flags = 0;
}
void
handle_job_status(Job *job, int status)
{
bool silent;
int dying;
if ((job->flags & JOB_ERRCHECK) && !keepgoing && runningJobs == NULL)
silent = !DEBUG(JOB);
else
silent = false;
debug_job_printf("Process %ld (%s) exited with status %d.\n",
(long)job->pid, job->node->name, status);
if (WIFEXITED(status)) {
job->code = WEXITSTATUS(status);
if (job->code != 0) {
if (!silent && job->code > 128
&& job->code <= 128 + _NSIG) {
dying = check_dying_signal();
silent = dying && job->code == dying + 128;
}
if (!silent)
printf("*** Error %d", job->code);
job->exit_type = JOB_EXIT_BAD;
} else
job->exit_type = JOB_EXIT_OKAY;
} else {
job->exit_type = JOB_SIGNALED;
job->code = WTERMSIG(status);
if (!silent) {
dying = check_dying_signal();
silent = dying && job->code == dying;
}
if (!silent)
printf("*** Signal %d", job->code);
}
if (job->exit_type != JOB_EXIT_OKAY) {
if (!silent)
printf(" in target '%s'", job->node->name);
if (job->flags & JOB_ERRCHECK) {
job->node->built_status = ERROR;
if (!keepgoing) {
if (!silent)
printf("\n");
job->flags |= JOB_KEEPERROR;
return;
}
printf(", line %lu of %s", job->location->lineno,
job->location->fname);
if ((job->flags & JOB_SILENT) && sequential)
determine_expensive_job(job);
if ((job->flags & (JOB_SILENT | JOB_IS_EXPENSIVE))
== JOB_SILENT)
printf(": %s", job->cmd);
printf(" (continuing)\n");
} else {
printf(" (ignored)\n");
job->exit_type = JOB_EXIT_OKAY;
}
}
free(job->cmd);
}
int
run_gnode(GNode *gn)
{
if (!gn || (gn->type & OP_DUMMY))
return NOSUCHNODE;
Job_Make(gn);
loop_handle_running_jobs();
return gn->built_status;
}
static bool
do_run_command(Job *job, const char *pre)
{
bool silent;
bool doExecute;
bool errCheck;
pid_t cpid;
const char *cmd = job->cmd;
silent = Targ_Silent(job->node);
errCheck = !Targ_Ignore(job->node);
if (job->node->type & OP_MAKE)
doExecute = true;
else
doExecute = !noExecute;
if (*cmd == '\0') {
Parse_Error(PARSE_WARNING,
"'%s' expands to '' while building %s",
pre, job->node->name);
return false;
}
for (;; cmd++) {
if (*cmd == '@')
silent = DEBUG(LOUD) ? false : true;
else if (*cmd == '-')
errCheck = false;
else if (*cmd == '+')
doExecute = true;
else
break;
}
while (ISSPACE(*cmd))
cmd++;
if ( noExecute || !silent)
printf("%s\n", cmd);
if (silent)
job->flags |= JOB_SILENT;
else
job->flags &= ~JOB_SILENT;
if (!doExecute)
return false;
fflush(stdout);
if (*cmd == '#')
return false;
switch (cpid = fork()) {
case -1:
Punt("Could not fork");
case 0:
reset_signal_mask();
if (random_delay)
if (!(runningJobs == NULL && nothing_left_to_build()))
usleep(arc4random_uniform(random_delay));
run_command(cmd, errCheck);
default:
job->pid = cpid;
job->next = runningJobs;
runningJobs = job;
if (errCheck)
job->flags |= JOB_ERRCHECK;
else
job->flags &= ~JOB_ERRCHECK;
debug_job_printf("Running %ld (%s) %s\n", (long)job->pid,
job->node->name, (noExecute || !silent) ? "" : cmd);
return true;
}
}
bool
job_run_next(Job *job)
{
bool started;
GNode *gn = job->node;
while (job->next_cmd != NULL) {
struct command *command = Lst_Datum(job->next_cmd);
handle_all_signals();
job->location = &command->location;
Parse_SetLocation(job->location);
job->cmd = Var_Subst(command->string, &gn->localvars, false);
job->next_cmd = Lst_Adv(job->next_cmd);
if (fatal_errors)
Punt(NULL);
started = do_run_command(job, command->string);
if (started)
return false;
else
free(job->cmd);
}
job->exit_type = JOB_EXIT_OKAY;
return true;
}