lexer rules relaxed a little; please, don't use tokens like abc: -- use "abc:" instead
[k8jam.git] / src / make1.c
blob270fb95115836e235088d8ab486872a6d90c135f
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"
66 static void make1a (TARGET *t, TARGET *parent);
67 static void make1b (TARGET *t);
68 static void make1c (TARGET *t);
69 static void make1d (void *closure, int status);
71 static CMD *make1cmds (ACTIONS *a0);
72 static LIST *make1list (LIST *l, TARGETS *targets, int flags);
73 static SETTINGS *make1settings (LIST *vars);
74 static void make1bind (TARGET *t, int warn);
77 /* Ugly static - it's too hard to carry it through the callbacks. */
78 static struct {
79 int failed;
80 int skipped;
81 int total;
82 int made;
83 } counts[1];
87 * make1() - execute commands to update a TARGET and all its dependents
89 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:
116 case T_MAKE_ACTIVE:
117 case T_MAKE_RUNNING:
118 t->parents = targetentry(t->parents, parent);
119 ++parent->asynccnt;
122 if (t->progress != T_MAKE_INIT) return;
123 /* asynccnt counts the dependents preventing this target from proceeding to make1b() for actual building */
124 /* we start off with a count of 1 to prevent anything from happening until we can call all dependents */
125 /* this 1 is accounted for when we call make1b() ourselves, below */
126 t->asynccnt = 1;
127 /* recurse on our dependents, manipulating progress to guard against circular dependency */
128 t->progress = T_MAKE_ONSTACK;
129 for (c = t->depends; c && !intr; c = c->next) make1a(c->target, t);
130 t->progress = T_MAKE_ACTIVE;
131 /* now that all dependents have bumped asynccnt, we now allow decrement our reference to asynccnt */
132 make1b(t);
137 * make1b() - dependents of target built, now build target with make1c()
139 static void make1b (TARGET *t) {
140 TARGETS *c;
141 const char *failed = "dependents";
142 /* if any dependents are still outstanding, wait until they call make1b() to signal their completion */
143 if (--t->asynccnt) return;
144 #ifdef JAM_OPT_SEMAPHORE
145 if (t->semaphore && t->semaphore->asynccnt) {
146 /* append 't' to the list of targets waiting on semaphore. */
147 t->semaphore->parents = targetentry(t->semaphore->parents, t);
148 ++t->asynccnt;
149 if (DEBUG_EXECCMD) printf( "SEM: %s is busy, delaying launch of %s\n", t->semaphore->name, t->name );
150 //pop_state(&state_stack);
151 return;
153 #endif
154 /* now ready to build target 't'... if dependents built ok */
155 /* collect status from dependents */
156 for (c = t->depends; c; c = c->next) {
157 if (c->target->status > t->status) {
158 failed = c->target->name;
159 t->status = c->target->status;
162 /* if actions on deps have failed, bail, otherwise, execute all actions to make target */
163 if (t->status == EXEC_CMD_FAIL && t->actions) {
164 ++counts->skipped;
165 printf("...skipped %s for lack of %s...\n", t->name, failed);
167 if (t->status == EXEC_CMD_OK) {
168 switch(t->fate) {
169 case T_FATE_INIT:
170 case T_FATE_MAKING:
171 /* shouldn't happen */
172 case T_FATE_STABLE:
173 case T_FATE_NEWER:
174 break;
175 case T_FATE_CANTFIND:
176 case T_FATE_CANTMAKE:
177 t->status = EXEC_CMD_FAIL;
178 break;
179 case T_FATE_ISTMP:
180 if (DEBUG_MAKEQ) printf("...using %s...\n", t->name);
181 break;
182 case T_FATE_TOUCHED:
183 case T_FATE_MISSING:
184 case T_FATE_NEEDTMP:
185 case T_FATE_OUTDATED:
186 case T_FATE_UPDATE:
187 /* set "on target" vars, build actions, unset vars */
188 /* set "progress" so that make1c() counts this target among 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);
195 t->progress = T_MAKE_RUNNING;
197 break;
200 /* call make1c() to begin the execution of the chain of commands needed to build target */
201 /* if we're not going to build target (because of dependency failures or because no commands
202 * need to be run) the chain will be empty and make1c() will directly
203 * signal the completion of target */
204 #ifdef JAM_OPT_SEMAPHORE
205 /* if there is a semaphore, indicate that it is in use */
206 if (pState->t->semaphore) {
207 ++pState->t->semaphore->asynccnt;
208 if (DEBUG_EXECCMD) printf( "SEM: %s now used by %s\n", pState->t->semaphore->name, pState->t->name);
210 #endif
211 make1c(t); //pState->curstate = T_STATE_MAKE1C;
216 * make1c() - launch target's next command, call make1b() when done
218 static void make1c (TARGET *t) {
219 CMD *cmd = (CMD *)t->cmds;
220 /* if there are (more) commands to run to build this target
221 * (and we haven't hit an error running earlier comands) we
222 * launch the command with execcmd() */
223 /* if there are no more commands to run, we collect the status
224 * from all the actions then report our completion to all the
225 * parents */
226 if (cmd && t->status == EXEC_CMD_OK) {
227 if (DEBUG_MAKE) {
228 if (DEBUG_MAKEQ || ! (cmd->rule->flags & RULE_QUIETLY)) {
229 printf("%s ", cmd->rule->name);
230 list_print(lol_get(&cmd->args, 0));
231 printf("\n");
234 if (DEBUG_EXEC) printf("%s\n", cmd->buf);
235 if (globs.cmdout) fprintf(globs.cmdout, "%s", cmd->buf);
236 if (globs.noexec) make1d(t, EXEC_CMD_OK);
237 else {
238 fflush(stdout);
239 execcmd(cmd->buf, make1d, t, cmd->shell);
241 } else {
242 TARGETS *c;
243 ACTIONS *actions;
244 /* collect status from actions, and distribute it as well */
245 for (actions = t->actions; actions; actions = actions->next) {
246 if (actions->action->status > t->status) t->status = actions->action->status;
248 for (actions = t->actions; actions; actions = actions->next) {
249 if (t->status > actions->action->status) actions->action->status = t->status;
251 /* tally success/failure for those we tried to update */
252 if (t->progress == T_MAKE_RUNNING) {
253 switch(t->status) {
254 case EXEC_CMD_OK: ++counts->made; break;
255 case EXEC_CMD_FAIL: ++counts->failed; break;
258 /* tell parents dependent has been built */
259 t->progress = T_MAKE_DONE;
260 for (c = t->parents; c; c = c->next) make1b(c->target);
261 #ifdef JAM_OPT_SEMAPHORE
262 /* if there is a semaphore, it is now free */
263 if (t->semaphore) {
264 assert(t->semaphore->asynccnt == 1);
265 --t->semaphore->asynccnt;
266 if (DEBUG_EXECCMD) printf( "SEM: %s is now free\n", t->semaphore->name );
267 /* If anything is waiting, notify the next target */
268 /* there is no point in notifying waiting targets, since they will be notified again */
269 if (t->semaphore->parents) {
270 TARGETS *first = t->semaphore->parents;
271 if (first->next) first->next->tail = first->tail;
272 t->semaphore->parents = first->next;
273 if (DEBUG_EXECCMD) printf( "SEM: placing %s on stack\n", first->target->name );
274 push_state(&temp_stack, first->target, NULL, T_STATE_MAKE1B);
275 BJAM_FREE(first);
278 #endif
284 * make1d() - handle command execution completion and call back make1c()
286 static void make1d (void *closure, int status) {
287 TARGET *t = (TARGET *)closure;
288 CMD *cmd = (CMD *)t->cmds;
289 /* execcmd() has completed */
290 /* all we need to do is fiddle with the status and signal our completion so make1c() can run the next command */
291 /* in interrupts, we bail heavily */
292 if (status == EXEC_CMD_FAIL && (cmd->rule->flags & RULE_IGNORE)) status = EXEC_CMD_OK;
293 /* on interrupt, set intr so _everything_ fails */
294 if (status == EXEC_CMD_INTR) ++intr;
295 if (status == EXEC_CMD_FAIL && DEBUG_MAKE) {
296 /* print command text on failure */
297 if (!DEBUG_EXEC) printf("%s\n", cmd->buf);
298 printf("...failed %s ", cmd->rule->name);
299 list_print(lol_get(&cmd->args, 0));
300 printf("...\n");
301 if (globs.quitquick) ++intr;
303 /* if the command was interrupted or failed and the target is not "precious", remove the targets */
304 /* precious == 'actions updated' -- the target maintains state */
305 if (status != EXEC_CMD_OK && !(cmd->rule->flags & RULE_UPDATED)) {
306 LIST *targets = lol_get(&cmd->args, 0);
307 for (; targets; targets = list_next(targets)) {
308 if (!unlink(targets->string)) printf("...removing %s\n", targets->string);
311 /* free this command and call make1c() to move onto next command */
312 t->status = status;
313 t->cmds = (char *)cmd_next(cmd);
314 cmd_free(cmd);
315 make1c(t);
320 * make1cmds() - turn ACTIONS into CMDs, grouping, splitting, etc
322 * Essentially copies a chain of ACTIONs to a chain of CMDs,
323 * grouping RULE_TOGETHER actions, splitting RULE_PIECEMEAL actions,
324 * and handling RULE_UPDATED actions. The result is a chain of
325 * CMDs which can be expanded by var_string() and executed with
326 * execcmd().
328 static CMD *make1cmds (ACTIONS *a0) {
329 CMD *cmds = 0;
330 LIST *shell = var_get("JAMSHELL"); /* shell is per-target */
331 /* step through actions */
332 /* actions may be shared with other targets or grouped with RULE_TOGETHER, so actions already seen are skipped */
333 for (; a0; a0 = a0->next) {
334 RULE *rule = a0->action->rule;
335 SETTINGS *boundvars;
336 LIST *nt, *ns;
337 ACTIONS *a1;
338 int start, chunk, length, maxline;
339 /* only do rules with commands to execute */
340 /* if this action has already been executed, use saved status */
341 if (!rule->actions || a0->action->running) continue;
342 a0->action->running = 1;
343 /* make LISTS of targets and sources */
344 /* if `execute together` has been specified for this rule, tack
345 * on sources from each instance of this rule for this target */
346 nt = make1list(L0, a0->action->targets, 0);
347 ns = make1list(L0, a0->action->sources, rule->flags);
348 if (rule->flags & RULE_TOGETHER) {
349 for (a1 = a0->next; a1; a1 = a1->next) {
350 if (a1->action->rule == rule && !a1->action->running) {
351 ns = make1list(ns, a1->action->sources, rule->flags);
352 a1->action->running = 1;
356 /* if doing only updated (or existing) sources, but none have
357 * been updated (or exist), skip this action */
358 if (!ns && (rule->flags & (RULE_UPDATED | RULE_EXISTING))) {
359 list_free(nt);
360 continue;
362 /* if we had 'actions xxx bind vars' we bind the vars now */
363 boundvars = make1settings(rule->bindlist);
364 pushsettings(boundvars);
366 * Build command, starting with all source args.
368 * If cmd_new returns 0, it's because the resulting command
369 * length is > MAXLINE. In this case, we'll slowly reduce
370 * the number of source arguments presented until it does
371 * fit. This only applies to actions that allow PIECEMEAL
372 * commands.
374 * While reducing slowly takes a bit of compute time to get
375 * things just right, it's worth it to get as close to MAXLINE
376 * as possible, because launching the commands we're executing
377 * is likely to be much more compute intensive!
379 * Note we loop through at least once, for sourceless actions.
381 * Max line length is the action specific maxline or, if not
382 * given or bigger than MAXLINE, MAXLINE.
384 start = 0;
385 chunk = length = list_length(ns);
386 maxline = rule->flags / RULE_MAXLINE;
387 maxline = maxline && maxline < MAXLINE ? maxline : MAXLINE;
388 do {
389 /* build cmd: cmd_new consumes its lists */
390 CMD *cmd = cmd_new(rule,
391 list_copy(L0, nt),
392 list_sublist(ns, start, chunk),
393 list_copy(L0, shell),
394 maxline);
395 if (cmd) {
396 /* it fit: chain it up */
397 if (!cmds) cmds = cmd; else cmds->tail->next = cmd;
398 cmds->tail = cmd;
399 start += chunk;
400 } else if ((rule->flags & RULE_PIECEMEAL) && chunk > 1) {
401 /* reduce chunk size slowly */
402 chunk = chunk*9/10;
403 } else {
404 /* too long and not splittable */
405 printf("%s actions too long (max %d)!\n", rule->name, maxline);
406 exit(EXITBAD);
408 } while (start < length);
409 /* these were always copied when used */
410 list_free(nt);
411 list_free(ns);
412 /* free the variables whose values were bound by 'actions xxx bind vars' */
413 popsettings(boundvars);
414 freesettings(boundvars);
416 return cmds;
421 * make1list() - turn a list of targets into a LIST, for $(<) and $(>)
423 static LIST *make1list (LIST *l, TARGETS *targets, int flags) {
424 for (; targets; targets = targets->next) {
425 TARGET *t = targets->target;
426 /* sources to 'actions existing' are never in the dependency
427 * graph (if they were, they'd get built and 'existing' would
428 * be superfluous, so throttle warning message about independent
429 * targets */
430 if (t->binding == T_BIND_UNBOUND) make1bind(t, !(flags & RULE_EXISTING));
431 if ((flags & RULE_EXISTING) && t->binding != T_BIND_EXISTS) continue;
432 if ((flags & RULE_UPDATED) && t->fate <= T_FATE_STABLE) continue;
433 /* prohibit duplicates for RULE_TOGETHER */
434 if (flags & RULE_TOGETHER) {
435 LIST *m;
437 for (m = l; m; m = m->next) if (!strcmp(m->string, t->boundname)) break;
438 if (m) continue;
440 /* build new list */
441 l = list_new(l, t->boundname, 1);
443 return l;
448 * make1settings() - for vars that get bound values, build up replacement lists
450 static SETTINGS *make1settings (LIST *vars) {
451 SETTINGS *settings = NULL;
453 for (; vars; vars = list_next(vars)) {
454 LIST *l = var_get(vars->string), *nl = NULL;
456 for (; l; l = list_next(l)) {
457 TARGET *t = bindtarget(l->string);
458 /* make sure the target is bound, warning if it is not in the dependency graph */
459 if (t->binding == T_BIND_UNBOUND) make1bind(t, 1);
460 /* build new list */
461 nl = list_new(nl, t->boundname, 1);
463 /* add to settings chain */
464 settings = addsettings(settings, 0, vars->string, nl);
466 return settings;
471 * make1bind() - bind targets that weren't bound in dependency analysis
473 * Spot the kludge! If a target is not in the dependency tree, it didn't
474 * get bound by make0(), so we have to do it here. Ugly.
476 static void make1bind (TARGET *t, int warn) {
477 if (t->flags & T_FLAG_NOTFILE) return;
478 /* sources to 'actions existing' are never in the dependency
479 * graph (if they were, they'd get built and 'existing' would
480 * be superfluous, so throttle warning message about independent
481 * targets */
482 if (warn) printf("warning: using independent target %s\n", t->name);
483 pushsettings(t->settings);
484 t->boundname = search(t->name, &t->time);
485 t->binding = t->time?T_BIND_EXISTS:T_BIND_MISSING;
486 popsettings(t->settings);