first in 2.5.10 series
[k8jam.git] / src / make1.c
blob13829be686ce4b0a27c61d69980c707c35ca9575
1 /*
2 * Copyright 1993-2002 Christopher Seiwald and Perforce Software, Inc.
4 * This file is part of Jam - see jam.c for Copyright information.
5 */
7 /*
8 * make1.c - execute command to bring targets up to date
10 * This module contains make1(), the entry point called by make() to
11 * recursively decend the dependency graph executing update actions as
12 * marked by make0().
14 * External routines:
16 * make1() - execute commands to update a TARGET and all its dependents
18 * Internal routines, the recursive/asynchronous command executors:
20 * make1a() - recursively traverse target tree, calling make1b()
21 * make1b() - dependents of target built, now build target with make1c()
22 * make1c() - launch target's next command, call make1b() when done
23 * make1d() - handle command execution completion and call back make1c()
25 * Internal support routines:
27 * make1cmds() - turn ACTIONS into CMDs, grouping, splitting, etc
28 * make1list() - turn a list of targets into a LIST, for $(<) and $(>)
29 * make1settings() - for vars that get bound, build up replacement lists
30 * make1bind() - bind targets that weren't bound in dependency analysis
32 * 04/16/94 (seiwald) - Split from make.c.
33 * 04/21/94 (seiwald) - Handle empty "updated" actions.
34 * 05/04/94 (seiwald) - async multiprocess (-j) support
35 * 06/01/94 (seiwald) - new 'actions existing' does existing sources
36 * 12/20/94 (seiwald) - NOTIME renamed NOTFILE.
37 * 01/19/95 (seiwald) - distinguish between CANTFIND/CANTMAKE targets.
38 * 01/22/94 (seiwald) - pass per-target JAMSHELL down to execcmd().
39 * 02/28/95 (seiwald) - Handle empty "existing" actions.
40 * 03/10/95 (seiwald) - Fancy counts.
41 * 02/07/01 (seiwald) - Fix jam -d0 return status.
42 * 01/21/02 (seiwald) - new -q to quit quickly on build failure
43 * 02/28/02 (seiwald) - don't delete 'actions updated' targets on failure
44 * 02/28/02 (seiwald) - merge EXEC_xxx flags in with RULE_xxx
45 * 07/17/02 (seiwald) - TEMPORARY sources for headers now get built
46 * 09/23/02 (seiwald) - "...using temp..." only displayed on -da now.
47 * 10/22/02 (seiwald) - list_new() now does its own newstr()/copystr()
48 * 11/04/02 (seiwald) - const-ing for string literals
49 * 12/03/02 (seiwald) - fix odd includes support by grafting them onto depends
51 #include <unistd.h>
53 #include "jam.h"
55 #include "lists.h"
56 #include "parse.h"
57 #include "variable.h"
58 #include "rules.h"
60 #include "search.h"
61 #include "newstr.h"
62 #include "make.h"
63 #include "command.h"
64 #include "execcmd.h"
67 static void make1a (TARGET *t, TARGET *parent);
68 static void make1b (TARGET *t);
69 static void make1c (TARGET *t);
70 static void make1d (void *closure, int status);
72 static CMD *make1cmds (ACTIONS *a0);
73 static LIST *make1list (LIST *l, TARGETS *targets, int flags);
74 static SETTINGS *make1settings (LIST *vars);
75 static void make1bind (TARGET *t, int warn);
77 /* Ugly static - it's too hard to carry it through the callbacks. */
79 static struct {
80 int failed;
81 int skipped;
82 int total;
83 int made;
84 } counts[1];
88 * make1() - execute commands to update a TARGET and all its dependents
90 static int intr = 0;
91 int make1 (TARGET *t) {
92 memset((char *)counts, 0, sizeof(*counts));
93 /* Recursively make the target and its dependents */
94 make1a(t, (TARGET *)0);
95 /* Wait for any outstanding commands to finish running. */
96 while (execwait()) ;
97 /* Talk about it */
98 if (DEBUG_MAKE && counts->failed) printf("...failed updating %d target%s...\n", counts->failed, multiFormSfx(counts->failed));
99 if (DEBUG_MAKE && counts->skipped) printf("...skipped %d target%s...\n", counts->skipped, multiFormSfx(counts->skipped));
100 if (DEBUG_MAKE && counts->made) printf("...updated %d target%s...\n", counts->made, multiFormSfx(counts->made));
101 return counts->total != counts->made;
106 * make1a() - recursively traverse target tree, calling make1b()
108 static void make1a (TARGET *t, TARGET *parent) {
109 TARGETS *c;
110 /* If the parent is the first to try to build this target */
111 /* or this target is in the make1c() quagmire, arrange for the */
112 /* parent to be notified when this target is built. */
113 if (parent) {
114 switch (t->progress) {
115 case T_MAKE_INIT: case T_MAKE_ACTIVE: case T_MAKE_RUNNING:
116 t->parents = targetentry(t->parents, parent);
117 parent->asynccnt++;
121 if (t->progress != T_MAKE_INIT) return;
122 /* Asynccnt counts the dependents preventing this target from */
123 /* proceeding to make1b() for actual building. We start off with */
124 /* a count of 1 to prevent anything from happening until we can */
125 /* call all dependents. This 1 is accounted for when we call */
126 /* make1b() ourselves, below. */
127 t->asynccnt = 1;
128 /* Recurse on our dependents, manipulating progress to guard */
129 /* against circular dependency. */
130 t->progress = T_MAKE_ONSTACK;
131 for (c = t->depends; c && !intr; c = c->next) make1a(c->target, t);
132 t->progress = T_MAKE_ACTIVE;
133 /* Now that all dependents have bumped asynccnt, we now allow */
134 /* decrement our reference to asynccnt. */
135 make1b(t);
140 * make1b() - dependents of target built, now build target with make1c()
142 static void make1b (TARGET *t) {
143 TARGETS *c;
144 const char *failed = "dependents";
145 /* if any dependents are still outstanding, wait until they */
146 /* call make1b() to signal their completion */
147 if (--t->asynccnt) return;
148 #ifdef JAM_OPT_SEMAPHORE
149 if (t->semaphore && t->semaphore->asynccnt) {
150 /* append 't' to the list of targets waiting on semaphore. */
151 t->semaphore->parents = targetentry(t->semaphore->parents, t);
152 ++t->asynccnt;
153 if (DEBUG_EXECCMD) printf( "SEM: %s is busy, delaying launch of %s\n", t->semaphore->name, t->name );
154 //pop_state(&state_stack);
155 return;
157 #endif
158 /* now ready to build target 't'... if dependents built ok */
159 /* collect status from dependents */
160 for (c = t->depends; c; c = c->next) {
161 if (c->target->status > t->status) {
162 failed = c->target->name;
163 t->status = c->target->status;
166 /* If actions on deps have failed, bail. */
167 /* Otherwise, execute all actions to make target */
168 if (t->status == EXEC_CMD_FAIL && t->actions) {
169 ++counts->skipped;
170 printf("...skipped %s for lack of %s...\n", t->name, failed);
172 if (t->status == EXEC_CMD_OK) {
173 switch(t->fate) {
174 case T_FATE_INIT: case T_FATE_MAKING:
175 /* shouldn't happen */
176 case T_FATE_STABLE: case T_FATE_NEWER:
177 break;
178 case T_FATE_CANTFIND: case T_FATE_CANTMAKE:
179 t->status = EXEC_CMD_FAIL;
180 break;
181 case T_FATE_ISTMP:
182 if (DEBUG_MAKEQ) printf("...using %s...\n", t->name);
183 break;
184 case T_FATE_TOUCHED: case T_FATE_MISSING: case T_FATE_NEEDTMP:
185 case T_FATE_OUTDATED: case T_FATE_UPDATE:
186 /* Set "on target" vars, build actions, unset vars */
187 /* Set "progress" so that make1c() counts this target among */
188 /* the successes/failures. */
189 if (t->actions) {
190 ++counts->total;
191 if (DEBUG_MAKE && !(counts->total % 100)) printf("...on %dth target...\n", counts->total);
192 pushsettings(t->settings);
193 t->cmds = (char *)make1cmds(t->actions);
194 popsettings(t->settings);
196 t->progress = T_MAKE_RUNNING;
198 break;
201 /* Call make1c() to begin the execution of the chain of commands */
202 /* needed to build target. If we're not going to build target */
203 /* (because of dependency failures or because no commands need to */
204 /* be run) the chain will be empty and make1c() will directly */
205 /* signal the completion of target. */
206 #ifdef JAM_OPT_SEMAPHORE
207 /* If there is a semaphore, indicate that it is in use. */
208 if (pState->t->semaphore) {
209 ++pState->t->semaphore->asynccnt;
210 if (DEBUG_EXECCMD) printf( "SEM: %s now used by %s\n", pState->t->semaphore->name, pState->t->name);
212 #endif
213 make1c(t); //pState->curstate = T_STATE_MAKE1C;
218 * make1c() - launch target's next command, call make1b() when done
220 static void make1c (TARGET *t) {
221 CMD *cmd = (CMD *)t->cmds;
222 /* if there are (more) commands to run to build this target */
223 /* (and we haven't hit an error running earlier comands) we */
224 /* launch the command with execcmd() */
225 /* if there are no more commands to run, we collect the status */
226 /* from all the actions then report our completion to all the */
227 /* parents */
228 if (cmd && t->status == EXEC_CMD_OK) {
229 if (DEBUG_MAKE) {
230 if (DEBUG_MAKEQ || ! (cmd->rule->flags & RULE_QUIETLY)) {
231 printf("%s ", cmd->rule->name);
232 list_print(lol_get(&cmd->args, 0));
233 printf("\n");
236 if (DEBUG_EXEC) printf("%s\n", cmd->buf);
237 if (globs.cmdout) fprintf(globs.cmdout, "%s", cmd->buf);
238 if (globs.noexec) make1d(t, EXEC_CMD_OK);
239 else {
240 fflush(stdout);
241 execcmd(cmd->buf, make1d, t, cmd->shell);
243 } else {
244 TARGETS *c;
245 ACTIONS *actions;
246 /* collect status from actions, and distribute it as well */
247 for (actions = t->actions; actions; actions = actions->next) {
248 if (actions->action->status > t->status) t->status = actions->action->status;
250 for (actions = t->actions; actions; actions = actions->next) {
251 if (t->status > actions->action->status) actions->action->status = t->status;
253 /* tally success/failure for those we tried to update */
254 if (t->progress == T_MAKE_RUNNING) {
255 switch(t->status) {
256 case EXEC_CMD_OK: ++counts->made; break;
257 case EXEC_CMD_FAIL: ++counts->failed; break;
260 /* tell parents dependent has been built */
261 t->progress = T_MAKE_DONE;
262 for (c = t->parents; c; c = c->next) make1b(c->target);
263 #ifdef JAM_OPT_SEMAPHORE
264 /* If there is a semaphore, it is now free. */
265 if (t->semaphore) {
266 assert(t->semaphore->asynccnt == 1);
267 --t->semaphore->asynccnt;
268 if (DEBUG_EXECCMD) printf( "SEM: %s is now free\n", t->semaphore->name );
269 /* If anything is waiting, notify the next target */
270 /* there is no point in notifying waiting targets, since they will be notified again */
271 if (t->semaphore->parents) {
272 TARGETS *first = t->semaphore->parents;
273 if (first->next) first->next->tail = first->tail;
274 t->semaphore->parents = first->next;
275 if (DEBUG_EXECCMD) printf( "SEM: placing %s on stack\n", first->target->name );
276 push_state(&temp_stack, first->target, NULL, T_STATE_MAKE1B);
277 BJAM_FREE(first);
280 #endif
286 * make1d() - handle command execution completion and call back make1c()
288 static void make1d (void *closure, int status) {
289 TARGET *t = (TARGET *)closure;
290 CMD *cmd = (CMD *)t->cmds;
292 /* Execcmd() has completed. All we need to do is fiddle with the */
293 /* status and signal our completion so make1c() can run the next */
294 /* command. On interrupts, we bail heavily. */
295 if (status == EXEC_CMD_FAIL && (cmd->rule->flags & RULE_IGNORE)) status = EXEC_CMD_OK;
296 /* On interrupt, set intr so _everything_ fails */
297 if (status == EXEC_CMD_INTR) ++intr;
298 if (status == EXEC_CMD_FAIL && DEBUG_MAKE) {
299 /* Print command text on failure */
300 if (!DEBUG_EXEC) printf("%s\n", cmd->buf);
301 printf("...failed %s ", cmd->rule->name);
302 list_print(lol_get(&cmd->args, 0));
303 printf("...\n");
304 if (globs.quitquick) ++intr;
306 /* If the command was interrupted or failed and the target */
307 /* is not "precious", remove the targets. */
308 /* Precious == 'actions updated' -- the target maintains state. */
309 if (status != EXEC_CMD_OK && !(cmd->rule->flags & RULE_UPDATED)) {
310 LIST *targets = lol_get(&cmd->args, 0);
311 for (; targets; targets = list_next(targets)) {
312 if (!unlink(targets->string)) printf("...removing %s\n", targets->string);
315 /* Free this command and call make1c() to move onto next command. */
316 t->status = status;
317 t->cmds = (char *)cmd_next(cmd);
318 cmd_free(cmd);
319 make1c(t);
324 * make1cmds() - turn ACTIONS into CMDs, grouping, splitting, etc
326 * Essentially copies a chain of ACTIONs to a chain of CMDs,
327 * grouping RULE_TOGETHER actions, splitting RULE_PIECEMEAL actions,
328 * and handling RULE_UPDATED actions. The result is a chain of
329 * CMDs which can be expanded by var_string() and executed with
330 * execcmd().
332 static CMD *make1cmds (ACTIONS *a0) {
333 CMD *cmds = 0;
334 LIST *shell = var_get("JAMSHELL"); /* shell is per-target */
336 /* Step through actions */
337 /* Actions may be shared with other targets or grouped with */
338 /* RULE_TOGETHER, so actions already seen are skipped. */
339 for (; a0; a0 = a0->next) {
340 RULE *rule = a0->action->rule;
341 SETTINGS *boundvars;
342 LIST *nt, *ns;
343 ACTIONS *a1;
344 int start, chunk, length, maxline;
345 /* Only do rules with commands to execute. */
346 /* If this action has already been executed, use saved status */
347 if (!rule->actions || a0->action->running) continue;
348 a0->action->running = 1;
349 /* Make LISTS of targets and sources */
350 /* If `execute together` has been specified for this rule, tack */
351 /* on sources from each instance of this rule for this target. */
352 nt = make1list(L0, a0->action->targets, 0);
353 ns = make1list(L0, a0->action->sources, rule->flags);
354 if (rule->flags & RULE_TOGETHER) {
355 for (a1 = a0->next; a1; a1 = a1->next) {
356 if (a1->action->rule == rule && !a1->action->running) {
357 ns = make1list(ns, a1->action->sources, rule->flags);
358 a1->action->running = 1;
362 /* If doing only updated (or existing) sources, but none have */
363 /* been updated (or exist), skip this action. */
364 if (!ns && (rule->flags & (RULE_UPDATED | RULE_EXISTING))) {
365 list_free(nt);
366 continue;
368 /* If we had 'actions xxx bind vars' we bind the vars now */
369 boundvars = make1settings(rule->bindlist);
370 pushsettings(boundvars);
372 * Build command, starting with all source args.
374 * If cmd_new returns 0, it's because the resulting command
375 * length is > MAXLINE. In this case, we'll slowly reduce
376 * the number of source arguments presented until it does
377 * fit. This only applies to actions that allow PIECEMEAL
378 * commands.
380 * While reducing slowly takes a bit of compute time to get
381 * things just right, it's worth it to get as close to MAXLINE
382 * as possible, because launching the commands we're executing
383 * is likely to be much more compute intensive!
385 * Note we loop through at least once, for sourceless actions.
387 * Max line length is the action specific maxline or, if not
388 * given or bigger than MAXLINE, MAXLINE.
390 start = 0;
391 chunk = length = list_length(ns);
392 maxline = rule->flags / RULE_MAXLINE;
393 maxline = maxline && maxline < MAXLINE ? maxline : MAXLINE;
394 do {
395 /* Build cmd: cmd_new consumes its lists. */
396 CMD *cmd = cmd_new(rule,
397 list_copy(L0, nt),
398 list_sublist(ns, start, chunk),
399 list_copy(L0, shell),
400 maxline);
401 if (cmd) {
402 /* It fit: chain it up. */
403 if (!cmds) cmds = cmd; else cmds->tail->next = cmd;
404 cmds->tail = cmd;
405 start += chunk;
406 } else if ((rule->flags & RULE_PIECEMEAL) && chunk > 1) {
407 /* Reduce chunk size slowly. */
408 chunk = chunk*9/10;
409 } else {
410 /* Too long and not splittable. */
411 printf("%s actions too long (max %d)!\n", rule->name, maxline);
412 exit(EXITBAD);
414 } while (start < length);
415 /* These were always copied when used. */
416 list_free(nt);
417 list_free(ns);
418 /* Free the variables whose values were bound by */
419 /* 'actions xxx bind vars' */
420 popsettings(boundvars);
421 freesettings(boundvars);
423 return cmds;
428 * make1list() - turn a list of targets into a LIST, for $(<) and $(>)
430 static LIST *make1list (LIST *l, TARGETS *targets, int flags) {
431 for (; targets; targets = targets->next) {
432 TARGET *t = targets->target;
433 /* Sources to 'actions existing' are never in the dependency */
434 /* graph (if they were, they'd get built and 'existing' would */
435 /* be superfluous, so throttle warning message about independent */
436 /* targets. */
437 if (t->binding == T_BIND_UNBOUND) make1bind(t, !(flags & RULE_EXISTING));
438 if ((flags & RULE_EXISTING) && t->binding != T_BIND_EXISTS) continue;
439 if ((flags & RULE_UPDATED) && t->fate <= T_FATE_STABLE) continue;
440 /* Prohibit duplicates for RULE_TOGETHER */
441 if (flags & RULE_TOGETHER) {
442 LIST *m;
444 for (m = l; m; m = m->next) {
445 if (!strcmp(m->string, t->boundname)) break;
447 if (m) continue;
449 /* Build new list */
450 l = list_new(l, t->boundname, 1);
452 return l;
457 * make1settings() - for vars that get bound values, build up replacement lists
459 static SETTINGS *make1settings (LIST *vars) {
460 SETTINGS *settings = 0;
462 for (; vars; vars = list_next(vars)) {
463 LIST *l = var_get(vars->string);
464 LIST *nl = 0;
466 for (; l; l = list_next(l)) {
467 TARGET *t = bindtarget(l->string);
468 /* Make sure the target is bound, warning if it is not in the */
469 /* dependency graph. */
470 if (t->binding == T_BIND_UNBOUND) make1bind(t, 1);
471 /* Build new list */
472 nl = list_new(nl, t->boundname, 1);
474 /* Add to settings chain */
475 settings = addsettings(settings, 0, vars->string, nl);
477 return settings;
482 * make1bind() - bind targets that weren't bound in dependency analysis
484 * Spot the kludge! If a target is not in the dependency tree, it didn't
485 * get bound by make0(), so we have to do it here. Ugly.
487 static void make1bind (TARGET *t, int warn) {
488 if (t->flags & T_FLAG_NOTFILE) return;
489 /* Sources to 'actions existing' are never in the dependency */
490 /* graph (if they were, they'd get built and 'existing' would */
491 /* be superfluous, so throttle warning message about independent */
492 /* targets. */
493 if (warn) printf("warning: using independent target %s\n", t->name);
494 pushsettings(t->settings);
495 t->boundname = search(t->name, &t->time);
496 t->binding = t->time ? T_BIND_EXISTS : T_BIND_MISSING;
497 popsettings(t->settings);