cosmetix in 'maxline'
[k8jam.git] / src / make1.c
blob43b0084dd1fbe095dd86a27c4eb40dddcdf78c54
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 */
6 /*
7 * make1.c - execute command to bring targets up to date
9 * This module contains make1(), the entry point called by make() to
10 * recursively decend the dependency graph executing update actions as
11 * marked by make0().
13 * External routines:
15 * make1() - execute commands to update a TARGET and all its dependents
17 * Internal routines, the recursive/asynchronous command executors:
19 * make1a() - recursively traverse target tree, calling make1b()
20 * make1b() - dependents of target built, now build target with make1c()
21 * make1c() - launch target's next command, call make1b() when done
22 * make1d() - handle command execution completion and call back make1c()
24 * Internal support routines:
26 * make1cmds() - turn ACTIONS into CMDs, grouping, splitting, etc
27 * make1list() - turn a list of targets into a LIST, for $(<) and $(>)
28 * make1settings() - for vars that get bound, build up replacement lists
29 * make1bind() - bind targets that weren't bound in dependency analysis
31 * 04/16/94 (seiwald) - Split from make.c.
32 * 04/21/94 (seiwald) - Handle empty "updated" actions.
33 * 05/04/94 (seiwald) - async multiprocess (-j) support
34 * 06/01/94 (seiwald) - new 'actions existing' does existing sources
35 * 12/20/94 (seiwald) - NOTIME renamed NOTFILE.
36 * 01/19/95 (seiwald) - distinguish between CANTFIND/CANTMAKE targets.
37 * 01/22/94 (seiwald) - pass per-target JAMSHELL down to execcmd().
38 * 02/28/95 (seiwald) - Handle empty "existing" actions.
39 * 03/10/95 (seiwald) - Fancy counts.
40 * 02/07/01 (seiwald) - Fix jam -d0 return status.
41 * 01/21/02 (seiwald) - new -q to quit quickly on build failure
42 * 02/28/02 (seiwald) - don't delete 'actions updated' targets on failure
43 * 02/28/02 (seiwald) - merge EXEC_xxx flags in with RULE_xxx
44 * 07/17/02 (seiwald) - TEMPORARY sources for headers now get built
45 * 09/23/02 (seiwald) - "...using temp..." only displayed on -da now.
46 * 10/22/02 (seiwald) - list_new() now does its own newstr()/copystr()
47 * 11/04/02 (seiwald) - const-ing for string literals
48 * 12/03/02 (seiwald) - fix odd includes support by grafting them onto depends
50 #include <unistd.h>
52 #include "jam.h"
54 #include "lists.h"
55 #include "parse.h"
56 #include "variable.h"
57 #include "rules.h"
59 #include "search.h"
60 #include "newstr.h"
61 #include "make.h"
62 #include "command.h"
63 #include "execcmd.h"
65 //#ifdef OPT_IMPROVED_PROGRESS_EXT
66 #include "progress.h"
67 //#endif
70 static void make1a (TARGET *t, TARGET *parent);
71 static void make1b (TARGET *t);
72 static void make1c (TARGET *t);
73 static void make1d (void *closure, int status);
75 static CMD *make1cmds (ACTIONS *a0);
76 static LIST *make1list (LIST *l, TARGETS *targets, int flags);
77 static SETTINGS *make1settings (LIST *vars);
78 static void make1bind (TARGET *t, int warn);
81 /* Ugly static - it's too hard to carry it through the callbacks. */
82 static struct {
83 int failed;
84 int skipped;
85 int total;
86 int made;
87 } counts[1];
91 * make1() - execute commands to update a TARGET and all its dependents
93 static int intr = 0;
96 int make1 (TARGET *t) {
97 memset((char *)counts, 0, sizeof(*counts));
98 /* recursively make the target and its dependents */
99 make1a(t, (TARGET *)0);
100 /* wait for any outstanding commands to finish running */
101 while (execwait()) ;
102 /* talk about it */
103 if (DEBUG_MAKE && counts->failed) printf("...failed updating %d target%s...\n", counts->failed, multiform_suffix(counts->failed));
104 if (DEBUG_MAKE && counts->skipped) printf("...skipped %d target%s...\n", counts->skipped, multiform_suffix(counts->skipped));
105 if (DEBUG_MAKE && counts->made && !is_make_silent()) printf("...updated %d target%s...\n", counts->made, multiform_suffix(counts->made));
106 return counts->total != counts->made;
111 * make1a() - recursively traverse target tree, calling make1b()
113 static void make1a (TARGET *t, TARGET *parent) {
114 TARGETS *c;
115 /* if the parent is the first to try to build this target */
116 /* or this target is in the make1c() quagmire, arrange for the */
117 /* parent to be notified when this target is built */
118 if (parent) {
119 switch (t->progress) {
120 case T_MAKE_INIT:
121 case T_MAKE_ACTIVE:
122 case T_MAKE_RUNNING:
123 t->parents = targetentry(t->parents, parent);
124 ++parent->asynccnt;
125 break;
128 if (t->progress != T_MAKE_INIT) return;
129 /* asynccnt counts the dependents preventing this target from proceeding to make1b() for actual building */
130 /* we start off with a count of 1 to prevent anything from happening until we can call all dependents */
131 /* this 1 is accounted for when we call make1b() ourselves, below */
132 t->asynccnt = 1;
133 /* recurse on our dependents, manipulating progress to guard against circular dependency */
134 t->progress = T_MAKE_ONSTACK;
135 for (c = t->depends; c && !intr; c = c->next) make1a(c->target, t);
136 t->progress = T_MAKE_ACTIVE;
137 /* now that all dependents have bumped asynccnt, we now allow decrement our reference to asynccnt */
138 make1b(t);
143 * make1b() - dependents of target built, now build target with make1c()
145 static void make1b (TARGET *t) {
146 TARGETS *c;
147 const char *failed = "dependents";
148 /* if any dependents are still outstanding, wait until they call make1b() to signal their completion */
149 if (--t->asynccnt) return;
150 #ifdef JAM_OPT_SEMAPHORE
151 /* try to aquire a semaphore. If it is locked, wait until the target that locked it is built and signal completition */
152 if (t->semaphore && t->semaphore->asynccnt) {
153 /* append 't' to the list of targets waiting on semaphore. */
154 t->semaphore->parents = targetentry(t->semaphore->parents, t);
155 ++t->asynccnt;
156 if (DEBUG_EXECCMD) printf("SEM: %s is busy, delaying launch of %s\n", t->semaphore->name, t->name);
157 //pop_state(&state_stack);
158 return;
160 #endif
161 /* now ready to build target 't'... if dependents built ok */
162 /* collect status from dependents */
163 for (c = t->depends; c; c = c->next) {
164 if (c->target->status > t->status) {
165 failed = c->target->name;
166 t->status = c->target->status;
169 /* if actions on deps have failed, bail, otherwise, execute all actions to make target */
170 if (t->status == EXEC_CMD_FAIL && t->actions) {
171 ++counts->skipped;
172 printf("...skipped %s for lack of %s...\n", t->name, failed);
174 if (t->status == EXEC_CMD_OK) {
175 switch(t->fate) {
176 case T_FATE_INIT:
177 case T_FATE_MAKING:
178 /* shouldn't happen */
179 case T_FATE_STABLE:
180 case T_FATE_NEWER:
181 break;
182 case T_FATE_CANTFIND:
183 case T_FATE_CANTMAKE:
184 t->status = EXEC_CMD_FAIL;
185 break;
186 case T_FATE_ISTMP:
187 if (DEBUG_MAKEQ) printf("...using %s...\n", t->name);
188 break;
189 case T_FATE_TOUCHED:
190 case T_FATE_MISSING:
191 case T_FATE_NEEDTMP:
192 case T_FATE_OUTDATED:
193 case T_FATE_UPDATE:
194 /* set "on target" vars, build actions, unset vars */
195 /* set "progress" so that make1c() counts this target among the successes/failures. */
196 if (t->actions) {
197 ++counts->total;
198 //if (DEBUG_MAKE && !(counts->total % 100)) printf("...on %dth target...\n", counts->total);
200 double est_remaining = progress_update(globs.progress, counts->total);
201 if (est_remaining > 0.0) {
202 int minutes = (int)est_remaining/60;
203 int seconds = (int)est_remaining%60;
204 if (minutes > 0 || seconds > 0) {
205 printf("*** completed %.0f%% (", ((double)counts->total*100/globs.updating));
206 if (minutes > 0) printf("%d min ", minutes);
207 if (seconds >= 0) printf("%d sec ", seconds);
208 printf("remaining)...\n");
212 pushsettings(t->settings);
213 t->cmds = (char *)make1cmds(t->actions);
214 popsettings(t->settings);
215 t->progress = T_MAKE_RUNNING;
217 break;
220 /* call make1c() to begin the execution of the chain of commands needed to build target */
221 /* if we're not going to build target (because of dependency failures or because no commands
222 * need to be run) the chain will be empty and make1c() will directly
223 * signal the completion of target */
224 #ifdef JAM_OPT_SEMAPHORE
225 /* if there is a semaphore, indicate that it is in use */
226 if (t->semaphore) {
227 ++t->semaphore->asynccnt;
228 if (DEBUG_EXECCMD) printf( "SEM: %s now used by %s\n", t->semaphore->name, t->name);
230 #endif
231 make1c(t); //pState->curstate = T_STATE_MAKE1C;
236 * make1c() - launch target's next command, call make1b() when done
238 static void make1c (TARGET *t) {
239 CMD *cmd = (CMD *)t->cmds;
240 /* if there are (more) commands to run to build this target
241 * (and we haven't hit an error running earlier comands) we
242 * launch the command with execcmd() */
243 /* if there are no more commands to run, we collect the status
244 * from all the actions then report our completion to all the
245 * parents */
246 if (cmd && t->status == EXEC_CMD_OK) {
247 if (DEBUG_MAKE) {
248 if (DEBUG_MAKEQ || !(cmd->rule->flags&RULE_QUIETLY)) {
249 printf("(%3d%%) ", counts->total*100/globs.updating);
250 printf("%s ", cmd->rule->name);
251 list_print_ex(stdout, lol_get(&cmd->args, 0), LPFLAG_NO_TRSPACE);
252 printf("\n");
255 if (DEBUG_EXEC) printf("%s\n", dstr_cstr(&cmd->buf));
256 if (globs.cmdout) fprintf(globs.cmdout, "%s", dstr_cstr(&cmd->buf));
257 if (globs.noexec) make1d(t, EXEC_CMD_OK);
258 else {
259 fflush(stdout);
260 execcmd(dstr_cstr(&cmd->buf), make1d, t, cmd->shell);
262 } else {
263 TARGETS *c;
264 ACTIONS *actions;
265 /* collect status from actions, and distribute it as well */
266 for (actions = t->actions; actions; actions = actions->next) {
267 if (actions->action->status > t->status) t->status = actions->action->status;
269 for (actions = t->actions; actions; actions = actions->next) {
270 if (t->status > actions->action->status) actions->action->status = t->status;
272 /* tally success/failure for those we tried to update */
273 if (t->progress == T_MAKE_RUNNING) {
274 switch(t->status) {
275 case EXEC_CMD_OK: ++counts->made; break;
276 case EXEC_CMD_FAIL: ++counts->failed; break;
279 /* tell parents dependent has been built */
280 t->progress = T_MAKE_DONE;
281 for (c = t->parents; c; c = c->next) make1b(c->target);
282 #ifdef JAM_OPT_SEMAPHORE
283 /* if there is a semaphore, it is now free */
284 if (t->semaphore) {
285 /*assert(t->semaphore->asynccnt == 1);*/
286 if (t->semaphore->asynccnt != 1) { fprintf(stderr, "FATAL: internal error: t->semaphore->asynccnt != 1!\n"); abort(); }
287 --t->semaphore->asynccnt;
288 if (DEBUG_EXECCMD) printf( "SEM: %s is now free\n", t->semaphore->name);
289 /* If anything is waiting, notify the next target */
290 /* there is no point in notifying waiting targets, since they will be notified again */
291 if (t->semaphore->parents) {
292 TARGETS *first = t->semaphore->parents;
293 if (first->next) first->next->tail = first->tail;
294 t->semaphore->parents = first->next;
295 if (DEBUG_EXECCMD) printf( "SEM: placing %s on stack\n", first->target->name);
296 //push_state(&temp_stack, first->target, NULL, T_STATE_MAKE1B);
297 /*k8: memory leak, but it's safe: free(first);*/
300 #endif
306 * make1d() - handle command execution completion and call back make1c()
308 static void make1d (void *closure, int status) {
309 TARGET *t = (TARGET *)closure;
310 CMD *cmd = (CMD *)t->cmds;
311 /* execcmd() has completed */
312 /* all we need to do is fiddle with the status and signal our completion so make1c() can run the next command */
313 /* in interrupts, we bail heavily */
314 if (status == EXEC_CMD_FAIL && (cmd->rule->flags & RULE_IGNORE)) status = EXEC_CMD_OK;
315 /* on interrupt, set intr so _everything_ fails */
316 if (status == EXEC_CMD_INTR) ++intr;
317 if (status == EXEC_CMD_FAIL && DEBUG_MAKE) {
318 /* print command text on failure */
319 if (!DEBUG_EXEC) printf("%s\n", dstr_cstr(&cmd->buf));
320 printf("...failed %s ", cmd->rule->name);
321 list_print(lol_get(&cmd->args, 0));
322 printf("...\n");
323 if (globs.quitquick) ++intr;
325 /* if the command was interrupted or failed and the target is not "precious", remove the targets */
326 /* precious == 'actions updated' -- the target maintains state */
327 if (status != EXEC_CMD_OK && !(cmd->rule->flags & RULE_UPDATED)) {
328 LIST *targets = lol_get(&cmd->args, 0);
329 for (; targets; targets = list_next(targets)) {
330 if (!unlink(targets->string)) printf("...removing %s\n", targets->string);
333 /* free this command and call make1c() to move onto next command */
334 t->status = status;
335 t->cmds = (char *)cmd_next(cmd);
336 cmd_free(cmd);
337 make1c(t);
342 * make1cmds() - turn ACTIONS into CMDs, grouping, splitting, etc
344 * Essentially copies a chain of ACTIONs to a chain of CMDs,
345 * grouping RULE_TOGETHER actions, splitting RULE_PIECEMEAL actions,
346 * and handling RULE_UPDATED actions. The result is a chain of
347 * CMDs which can be expanded by var_string() and executed with
348 * execcmd().
350 static CMD *make1cmds (ACTIONS *a0) {
351 CMD *cmds = 0;
352 LIST *shell = var_get("JAMSHELL"); /* shell is per-target */
353 /* step through actions */
354 /* actions may be shared with other targets or grouped with RULE_TOGETHER, so actions already seen are skipped */
355 for (; a0; a0 = a0->next) {
356 RULE *rule = a0->action->rule;
357 SETTINGS *boundvars;
358 LIST *nt, *ns;
359 ACTIONS *a1;
360 int start, chunk, length, maxline;
361 /* only do rules with commands to execute */
362 /* if this action has already been executed, use saved status */
363 if (!rule->actions || a0->action->running) continue;
364 a0->action->running = 1;
365 /* make LISTS of targets and sources */
366 /* if `execute together` has been specified for this rule, tack
367 * on sources from each instance of this rule for this target */
368 nt = make1list(L0, a0->action->targets, 0);
369 ns = make1list(L0, a0->action->sources, rule->flags);
370 if (rule->flags & RULE_TOGETHER) {
371 for (a1 = a0->next; a1; a1 = a1->next) {
372 if (a1->action->rule == rule && !a1->action->running) {
373 ns = make1list(ns, a1->action->sources, rule->flags);
374 a1->action->running = 1;
378 /* if doing only updated (or existing) sources, but none have
379 * been updated (or exist), skip this action */
380 if (!ns && (rule->flags & (RULE_UPDATED | RULE_EXISTING))) {
381 list_free(nt);
382 continue;
384 /* if we had 'actions xxx bind vars' we bind the vars now */
385 boundvars = make1settings(rule->bindlist);
386 pushsettings(boundvars);
388 * Build command, starting with all source args.
390 * If cmd_new returns 0, it's because the resulting command
391 * length is > MAXLINE. In this case, we'll slowly reduce
392 * the number of source arguments presented until it does
393 * fit. This only applies to actions that allow PIECEMEAL
394 * commands.
396 * While reducing slowly takes a bit of compute time to get
397 * things just right, it's worth it to get as close to MAXLINE
398 * as possible, because launching the commands we're executing
399 * is likely to be much more compute intensive!
401 * Note we loop through at least once, for sourceless actions.
403 * Max line length is the action specific maxline or, if not
404 * given or bigger than MAXLINE, MAXLINE.
406 start = 0;
407 chunk = length = list_length(ns);
408 maxline = (rule->flags&RULE_MAXLINE ? rule->flags>>8 : 0);
409 maxline = (maxline && maxline < MAXLINE ? maxline : MAXLINE);
410 do {
411 /* build cmd: cmd_new consumes its lists */
412 CMD *cmd = cmd_new(rule,
413 list_copy(L0, nt),
414 list_sublist(ns, start, chunk),
415 list_copy(L0, shell),
416 maxline);
417 if (cmd) {
418 /* it fit: chain it up */
419 if (!cmds) cmds = cmd; else cmds->tail->next = cmd;
420 cmds->tail = cmd;
421 start += chunk;
422 } else if ((rule->flags & RULE_PIECEMEAL) && chunk > 1) {
423 /* reduce chunk size slowly */
424 chunk = chunk*9/10;
425 } else {
426 /* too long and not splittable */
427 printf("%s actions too long (max %d)!\n", rule->name, maxline);
428 exit(EXITBAD);
430 } while (start < length);
431 /* these were always copied when used */
432 list_free(nt);
433 list_free(ns);
434 /* free the variables whose values were bound by 'actions xxx bind vars' */
435 popsettings(boundvars);
436 freesettings(boundvars);
438 return cmds;
443 * make1list() - turn a list of targets into a LIST, for $(<) and $(>)
445 static LIST *make1list (LIST *l, TARGETS *targets, int flags) {
446 for (; targets; targets = targets->next) {
447 TARGET *t = targets->target;
448 /* sources to 'actions existing' are never in the dependency
449 * graph (if they were, they'd get built and 'existing' would
450 * be superfluous, so throttle warning message about independent
451 * targets */
452 if (t->binding == T_BIND_UNBOUND) make1bind(t, !(flags & RULE_EXISTING));
453 if ((flags & RULE_EXISTING) && t->binding != T_BIND_EXISTS) continue;
454 if ((flags & RULE_UPDATED) && t->fate <= T_FATE_STABLE) continue;
455 /* prohibit duplicates for RULE_TOGETHER */
456 if (flags & RULE_TOGETHER) {
457 LIST *m;
459 for (m = l; m; m = m->next) if (!strcmp(m->string, t->boundname)) break;
460 if (m) continue;
462 /* build new list */
463 l = list_new(l, t->boundname, 1);
465 return l;
470 * make1settings() - for vars that get bound values, build up replacement lists
472 static SETTINGS *make1settings (LIST *vars) {
473 SETTINGS *settings = NULL;
474 for (; vars; vars = list_next(vars)) {
475 LIST *l = var_get(vars->string), *nl = NULL;
476 for (; l; l = list_next(l)) {
477 TARGET *t = bindtarget(l->string);
478 /* make sure the target is bound, warning if it is not in the dependency graph */
479 if (t->binding == T_BIND_UNBOUND) make1bind(t, 1);
480 /* build new list */
481 nl = list_new(nl, t->boundname, 1);
483 /* add to settings chain */
484 settings = addsettings(settings, 0, vars->string, nl);
486 return settings;
491 * make1bind() - bind targets that weren't bound in dependency analysis
493 * Spot the kludge! If a target is not in the dependency tree, it didn't
494 * get bound by make0(), so we have to do it here. Ugly.
496 static void make1bind (TARGET *t, int warn) {
497 if (t->flags & T_FLAG_NOTFILE) return;
498 /* sources to 'actions existing' are never in the dependency
499 * graph (if they were, they'd get built and 'existing' would
500 * be superfluous, so throttle warning message about independent
501 * targets */
502 if (warn) printf("warning: using independent target %s\n", t->name);
503 pushsettings(t->settings);
504 t->boundname = search(t->name, &t->time);
505 t->binding = t->time?T_BIND_EXISTS:T_BIND_MISSING;
506 popsettings(t->settings);