#include <limits.h>
#include <signal.h>
#include <stddef.h>
#include <stdint.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <ohash.h>
#include "defines.h"
#include "dir.h"
#include "job.h"
#include "suff.h"
#include "var.h"
#include "error.h"
#include "expandchildren.h"
#include "make.h"
#include "gnode.h"
#include "extern.h"
#include "timestamp.h"
#include "engine.h"
#include "lst.h"
#include "targ.h"
#include "targequiv.h"
#include "garray.h"
#include "memory.h"
static struct growableArray examine;
static struct growableArray to_build;
static struct growableArray heldBack;
static struct ohash targets;
static void MakeAddChild(void *, void *);
static void MakeHandleUse(void *, void *);
static bool MakeStartJobs(void);
static void MakePrintStatus(void *);
static bool targets_contain_cycles(void);
static void print_unlink_cycle(struct growableArray *, GNode *);
static void break_and_print_cycles(Lst);
static GNode *find_cycle(Lst, struct growableArray *);
static bool try_to_make_node(GNode *);
static void add_targets_to_make(Lst);
static bool has_predecessor_left_to_build(GNode *);
static void requeue_successors(GNode *);
static void random_setup(void);
static bool randomize_queue;
long random_delay = 0;
bool
nothing_left_to_build(void)
{
return Array_IsEmpty(&to_build);
}
static void
random_setup(void)
{
randomize_queue = Var_Definedi("RANDOM_ORDER", NULL);
#if 0
if (Var_Definedi("RANDOM_DELAY", NULL))
random_delay = strtonum(Var_Value("RANDOM_DELAY"), 0, 1000,
NULL) * 1000000;
#endif
}
static void
randomize_garray(struct growableArray *g)
{
unsigned int i, v;
GNode *e;
for (i = g->n; i > 0; i--) {
v = arc4random_uniform(i);
if (v == i-1)
continue;
else {
e = g->a[i-1];
g->a[i-1] = g->a[v];
g->a[v] = e;
}
}
}
static bool
has_predecessor_left_to_build(GNode *gn)
{
LstNode ln;
if (Lst_IsEmpty(&gn->predecessors))
return false;
for (ln = Lst_First(&gn->predecessors); ln != NULL; ln = Lst_Adv(ln)) {
GNode *pgn = Lst_Datum(ln);
if (pgn->must_make && pgn->built_status == UNKNOWN) {
if (DEBUG(MAKE))
printf("predecessor %s not made yet.\n",
pgn->name);
return true;
}
}
return false;
}
static void
requeue_successors(GNode *gn)
{
LstNode ln;
for (ln = Lst_First(&gn->successors); ln != NULL; ln = Lst_Adv(ln)) {
GNode *succ = Lst_Datum(ln);
if (succ->must_make && succ->children_left == 0
&& succ->built_status == UNKNOWN)
Array_PushNew(&to_build, succ);
}
}
static void
requeue(GNode *gn)
{
unsigned int i, j;
for (i = 0, j = 0; i < heldBack.n; i++, j++) {
if (heldBack.a[i]->watched == gn) {
j--;
heldBack.a[i]->built_status = UNKNOWN;
if (DEBUG(HELDJOBS))
printf("%s finished, releasing: %s\n",
gn->name, heldBack.a[i]->name);
Array_Push(&to_build, heldBack.a[i]);
continue;
}
heldBack.a[j] = heldBack.a[i];
}
heldBack.n = j;
}
void
Make_Update(GNode *cgn)
{
GNode *pgn;
LstNode ln;
if (cgn->built_status != UPTODATE) {
if (noExecute || is_out_of_date(Dir_MTime(cgn)))
clock_gettime(CLOCK_REALTIME, &cgn->mtime);
if (DEBUG(MAKE))
printf("update time: %s\n",
time_to_string(&cgn->mtime));
}
requeue(cgn);
for (ln = Lst_First(&cgn->parents); ln != NULL; ln = Lst_Adv(ln)) {
pgn = Lst_Datum(ln);
pgn->children_left--;
if (pgn->must_make) {
if (DEBUG(MAKE))
printf("%s--=%d ",
pgn->name, pgn->children_left);
if ( ! (cgn->type & OP_USE)) {
if (cgn->built_status == REBUILT)
pgn->child_rebuilt = true;
(void)Make_TimeStamp(pgn, cgn);
}
if (pgn->children_left == 0) {
if (DEBUG(MAKE))
printf("QUEUING ");
Array_Push(&to_build, pgn);
} else if (pgn->children_left < 0) {
Error("Child %s discovered graph cycles through %s", cgn->name, pgn->name);
}
}
}
if (DEBUG(MAKE))
printf("\n");
requeue_successors(cgn);
}
static bool
try_to_make_node(GNode *gn)
{
if (DEBUG(MAKE))
printf("Examining %s...", gn->name);
if (gn->built_status == HELDBACK) {
if (DEBUG(HELDJOBS))
printf("%s already held back ???\n", gn->name);
return false;
}
if (gn->children_left != 0) {
if (DEBUG(MAKE))
printf(" Requeuing (%d)\n", gn->children_left);
add_targets_to_make(&gn->children);
Array_Push(&to_build, gn);
return false;
}
if (has_been_built(gn)) {
if (DEBUG(MAKE))
printf(" already made\n");
return false;
}
if (has_predecessor_left_to_build(gn)) {
if (DEBUG(MAKE))
printf(" Dropping for now\n");
return false;
}
if (gn->children_left != 0) {
if (DEBUG(MAKE))
printf(" Requeuing (after deps: %d)\n",
gn->children_left);
add_targets_to_make(&gn->children);
return false;
}
if (gn->groupling != NULL) {
GNode *gn2;
for (gn2 = gn->groupling; gn2 != gn; gn2 = gn2->groupling)
if (gn2->built_status == BUILDING) {
gn->watched = gn2;
gn->built_status = HELDBACK;
if (DEBUG(HELDJOBS))
printf("Holding back job %s, "
"groupling to %s\n",
gn->name, gn2->name);
Array_Push(&heldBack, gn);
return false;
}
}
if (gn->sibling != gn) {
GNode *gn2;
for (gn2 = gn->sibling; gn2 != gn; gn2 = gn2->sibling)
if (gn2->built_status == BUILDING) {
gn->watched = gn2;
gn->built_status = HELDBACK;
if (DEBUG(HELDJOBS))
printf("Holding back job %s, "
"sibling to %s\n",
gn->name, gn2->name);
Array_Push(&heldBack, gn);
return false;
}
}
if (Make_OODate(gn)) {
if (DEBUG(MAKE))
printf("out-of-date\n");
if (queryFlag)
return true;
Make_DoAllVar(gn);
if (node_find_valid_commands(gn)) {
if (touchFlag)
Job_Touch(gn);
else
Job_Make(gn);
} else
node_failure(gn);
} else {
if (DEBUG(MAKE))
printf("up-to-date\n");
gn->built_status = UPTODATE;
Make_Update(gn);
}
return false;
}
static bool
MakeStartJobs(void)
{
GNode *gn;
while (can_start_job() && (gn = Array_Pop(&to_build)) != NULL) {
if (try_to_make_node(gn))
return true;
}
return false;
}
static void
MakePrintStatus(void *gnp)
{
GNode *gn = gnp;
if (gn->built_status == UPTODATE) {
printf("`%s' is up to date.\n", gn->name);
} else if (gn->children_left != 0) {
printf("`%s' not remade because of errors.\n", gn->name);
}
}
static void
MakeAddChild(void *to_addp, void *ap)
{
GNode *gn = to_addp;
struct growableArray *a = ap;
if (!gn->must_make && !(gn->type & OP_USE))
Array_Push(a, gn);
}
static void
MakeHandleUse(void *cgnp, void *pgnp)
{
GNode *cgn = cgnp;
GNode *pgn = pgnp;
if (cgn->type & OP_USE)
Make_HandleUse(cgn, pgn);
}
static void
add_targets_to_make(Lst todo)
{
GNode *gn;
unsigned int slot;
AppendList2Array(todo, &examine);
while ((gn = Array_Pop(&examine)) != NULL) {
if (gn->must_make)
continue;
gn->must_make = true;
slot = ohash_qlookup(&targets, gn->name);
if (!ohash_find(&targets, slot))
ohash_insert(&targets, slot, gn);
look_harder_for_target(gn);
kludge_look_harder_for_target(gn);
Lst_ForEach(&gn->children, MakeHandleUse, gn);
Suff_FindDeps(gn);
expand_all_children(gn);
if (gn->children_left != 0) {
if (DEBUG(MAKE))
printf("%s: not queuing (%d child%s left to build)\n",
gn->name, gn->children_left,
gn->children_left > 1 ? "ren" : "");
Lst_ForEach(&gn->children, MakeAddChild,
&examine);
} else {
if (DEBUG(MAKE))
printf("%s: queuing\n", gn->name);
Array_Push(&to_build, gn);
}
}
if (randomize_queue)
randomize_garray(&to_build);
}
void
Make_Init(void)
{
Array_Init(&to_build, 500);
Array_Init(&examine, 150);
Array_Init(&heldBack, 100);
ohash_init(&targets, 10, &gnode_info);
}
void
Make_Run(Lst targs, bool *has_errors, bool *out_of_date)
{
if (DEBUG(PARALLEL))
random_setup();
add_targets_to_make(targs);
if (queryFlag) {
if (MakeStartJobs())
*out_of_date = true;
} else {
(void)MakeStartJobs();
}
while (!Job_Empty()) {
handle_running_jobs();
(void)MakeStartJobs();
}
if (errorJobs != NULL)
*has_errors = true;
if (targets_contain_cycles()) {
break_and_print_cycles(targs);
*has_errors = true;
}
Lst_Every(targs, MakePrintStatus);
}
static bool
targets_contain_cycles(void)
{
GNode *gn;
unsigned int i;
bool cycle = false;
bool first = true;
for (gn = ohash_first(&targets, &i); gn != NULL;
gn = ohash_next(&targets, &i)) {
if (has_been_built(gn))
continue;
cycle = true;
if (first)
printf("Error target(s) unaccounted for: ");
printf("%s ", gn->name);
first = false;
}
if (!first)
printf("\n");
return cycle;
}
static void
print_unlink_cycle(struct growableArray *l, GNode *c)
{
LstNode ln;
GNode *gn = NULL;
unsigned int i;
printf("Cycle found: ");
for (i = 0; i != l->n; i++) {
gn = l->a[i];
if (gn == c)
printf("(");
printf("%s -> ", gn->name);
}
printf("%s)\n", c->name);
assert(gn);
for (ln = Lst_First(&gn->children); ln != NULL; ln = Lst_Adv(ln)) {
GNode *gn2 = Lst_Datum(ln);
if (gn2 == c) {
Lst_Remove(&gn->children, ln);
return;
}
}
assert(0);
}
static void
break_and_print_cycles(Lst t)
{
struct growableArray cycle;
Array_Init(&cycle, 16);
while (1) {
GNode *c;
Array_Reset(&cycle);
c = find_cycle(t, &cycle);
if (c)
print_unlink_cycle(&cycle, c);
else
break;
}
free(cycle.a);
}
static GNode *
find_cycle(Lst l, struct growableArray *cycle)
{
LstNode ln;
for (ln = Lst_First(l); ln != NULL; ln = Lst_Adv(ln)) {
GNode *gn = Lst_Datum(ln);
if (gn->in_cycle) {
return gn;
}
if (gn->built_status == UPTODATE)
continue;
if (gn->children_left != 0) {
GNode *c;
gn->in_cycle = true;
Array_Push(cycle, gn);
c = find_cycle(&gn->children, cycle);
gn->in_cycle = false;
if (c)
return c;
Array_Pop(cycle);
}
}
return NULL;
}