1/*
2 * Copyright 1993, 1995 Christopher Seiwald.
3 *
4 * This file is part of Jam - see jam.c for Copyright information.
5 */
6
7/*
8 * make.c - bring a target up to date, once rules are in place
9 *
10 * This modules controls the execution of rules to bring a target and
11 * its dependencies up to date.  It is invoked after the targets, rules,
12 * et. al. described in rules.h are created by the interpreting of the
13 * jam files.
14 *
15 * This file contains the main make() entry point and the first pass
16 * make0().  The second pass, make1(), which actually does the command
17 * execution, is in make1.c.
18 *
19 * External routines:
20 *	make() - make a target, given its name
21 *
22 * Internal routines:
23 * 	make0() - bind and scan everything to make a TARGET
24 * 	make0sort() - reorder TARGETS chain by their time (newest to oldest)
25 *
26 * 12/26/93 (seiwald) - allow NOTIME targets to be expanded via $(<), $(>)
27 * 01/04/94 (seiwald) - print all targets, bounded, when tracing commands
28 * 04/08/94 (seiwald) - progress report now reflects only targets with actions
29 * 04/11/94 (seiwald) - Combined deps & headers into deps[2] in TARGET.
30 * 12/20/94 (seiwald) - NOTIME renamed NOTFILE.
31 * 12/20/94 (seiwald) - make0() headers after determining fate of target, so
32 *			that headers aren't seen as dependents on themselves.
33 * 01/19/95 (seiwald) - distinguish between CANTFIND/CANTMAKE targets.
34 * 02/02/95 (seiwald) - propagate leaf source time for new LEAVES rule.
35 * 02/14/95 (seiwald) - NOUPDATE rule means don't update existing target.
36 * 08/22/95 (seiwald) - NOUPDATE targets immune to anyhow (-a) flag.
37 * 09/06/00 (seiwald) - NOCARE affects targets with sources/actions.
38 * 03/02/01 (seiwald) - reverse NOCARE change.
39 * 03/14/02 (seiwald) - TEMPORARY targets no longer take on parents age
40 * 03/16/02 (seiwald) - support for -g (reorder builds by source time)
41 * 07/17/02 (seiwald) - TEMPORARY sources for headers now get built
42 * 09/19/02 (seiwald) - new -d displays
43 * 09/23/02 (seiwald) - suppress "...using temp..." in default output
44 * 09/28/02 (seiwald) - make0() takes parent pointer; new -dc display
45 * 11/04/02 (seiwald) - const-ing for string literals
46 * 12/03/02 (seiwald) - fix odd includes support by grafting them onto depends
47 * 12/17/02 (seiwald) - new copysettings() to protect target-specific vars
48 * 01/03/03 (seiwald) - T_FATE_NEWER once again gets set with missing parent
49 * 01/14/03 (seiwald) - fix includes fix with new internal includes TARGET
50 * 04/04/03 (seiwald) - fix INTERNAL node binding to avoid T_BIND_PARENTS
51 */
52
53# include "jam.h"
54
55# include "lists.h"
56# include "parse.h"
57# include "variable.h"
58# include "rules.h"
59
60# include "search.h"
61# include "newstr.h"
62# include "make.h"
63# include "headers.h"
64# include "command.h"
65
66# ifndef max
67# define max( a,b ) ((a)>(b)?(a):(b))
68# endif
69
70typedef struct {
71	int	temp;
72	int	updating;
73	int	cantfind;
74	int	cantmake;
75	int	targets;
76	int	made;
77} COUNTS ;
78
79static void make0( TARGET *t, TARGET *p, int depth,
80		COUNTS *counts, int anyhow );
81
82static TARGETS *make0sort( TARGETS *c );
83
84static const char *target_fate[] =
85{
86	"init",		/* T_FATE_INIT */
87	"making", 	/* T_FATE_MAKING */
88	"stable", 	/* T_FATE_STABLE */
89	"newer",	/* T_FATE_NEWER */
90	"temp", 	/* T_FATE_ISTMP */
91	"touched", 	/* T_FATE_TOUCHED */
92	"missing", 	/* T_FATE_MISSING */
93	"needtmp", 	/* T_FATE_NEEDTMP */
94	"old", 		/* T_FATE_OUTDATED */
95	"update", 	/* T_FATE_UPDATE */
96	"nofind", 	/* T_FATE_CANTFIND */
97	"nomake" 	/* T_FATE_CANTMAKE */
98} ;
99
100static const char *target_bind[] =
101{
102	"unbound",
103	"missing",
104	"parents",
105	"exists",
106} ;
107
108# define spaces(x) ( "                " + 16 - ( x > 16 ? 16 : x ) )
109
110/*
111 * make() - make a target, given its name
112 */
113
114int
115make(
116	int		n_targets,
117	const char	**targets,
118	int		anyhow )
119{
120	int i;
121	COUNTS counts[1];
122	int status = 0;		/* 1 if anything fails */
123
124#ifdef OPT_HEADER_CACHE_EXT
125	hcache_init();
126#endif
127
128	memset( (char *)counts, 0, sizeof( *counts ) );
129
130	for( i = 0; i < n_targets; i++ )
131	{
132	    TARGET *t = bindtarget( targets[i] );
133
134	    make0( t, 0, 0, counts, anyhow );
135	}
136
137	if( DEBUG_MAKE )
138	{
139	    if( counts->targets )
140		printf( "...found %d target(s)...\n", counts->targets );
141	    if( counts->temp )
142		printf( "...using %d temp target(s)...\n", counts->temp );
143	    if( counts->updating )
144		printf( "...updating %d target(s)...\n", counts->updating );
145	    if( counts->cantfind )
146		printf( "...can't find %d target(s)...\n", counts->cantfind );
147	    if( counts->cantmake )
148		printf( "...can't make %d target(s)...\n", counts->cantmake );
149	}
150
151#ifdef OPT_HEADER_CACHE_EXT
152	hcache_done();
153#endif
154
155	status = counts->cantfind || counts->cantmake;
156
157	for( i = 0; i < n_targets; i++ )
158	    status |= make1( bindtarget( targets[i] ) );
159
160	return status;
161}
162
163/*
164 * make0() - bind and scan everything to make a TARGET
165 *
166 * Make0() recursively binds a target, searches for #included headers,
167 * calls itself on those headers, and calls itself on any dependents.
168 */
169
170static void
171make0(
172	TARGET	*t,
173	TARGET  *p,		/* parent */
174	int	depth,		/* for display purposes */
175	COUNTS	*counts,	/* for reporting */
176	int	anyhow )	/* forcibly touch all (real) targets */
177{
178	TARGETS	*c, *d, *incs;
179	TARGET 	*ptime = t;
180	time_t	last, leaf, hlast;
181	int	fate;
182	const char *flag = "";
183	SETTINGS *s;
184
185	/*
186	 * Step 1: initialize
187	 */
188
189	if( DEBUG_MAKEPROG )
190	    printf( "make\t--\t%s%s\n", spaces( depth ), t->name );
191
192	t->fate = T_FATE_MAKING;
193
194	/*
195	 * Step 2: under the influence of "on target" variables,
196	 * bind the target and search for headers.
197	 */
198
199	/* Step 2a: set "on target" variables. */
200
201	s = copysettings( t->settings );
202	pushsettings( s );
203
204	/* Step 2b: find and timestamp the target file (if it's a file). */
205
206	if( t->binding == T_BIND_UNBOUND && !( t->flags & T_FLAG_NOTFILE ) )
207	{
208	    t->boundname = search( t->name, &t->time );
209	    t->binding = t->time ? T_BIND_EXISTS : T_BIND_MISSING;
210	}
211
212	/* INTERNAL, NOTFILE header nodes have the time of their parents */
213
214	if( p && t->flags & T_FLAG_INTERNAL )
215	    ptime = p;
216
217	/* If temp file doesn't exist but parent does, use parent */
218
219	if( p && t->flags & T_FLAG_TEMP &&
220	    t->binding == T_BIND_MISSING &&
221	    p->binding != T_BIND_MISSING )
222	{
223	    t->binding = T_BIND_PARENTS;
224	    ptime = p;
225	}
226
227	/* Step 2c: If its a file, search for headers. */
228
229	if( t->binding == T_BIND_EXISTS )
230	    headers( t );
231
232	/* Step 2d: reset "on target" variables */
233
234	popsettings( s );
235	freesettings( s );
236
237	/*
238	 * Pause for a little progress reporting
239	 */
240
241	if( DEBUG_MAKEPROG )
242	{
243	    if( strcmp( t->name, t->boundname ) )
244	    {
245		printf( "bind\t--\t%s%s: %s\n",
246			spaces( depth ), t->name, t->boundname );
247	    }
248
249	    switch( t->binding )
250	    {
251	    case T_BIND_UNBOUND:
252	    case T_BIND_MISSING:
253	    case T_BIND_PARENTS:
254		printf( "time\t--\t%s%s: %s\n",
255			spaces( depth ), t->name, target_bind[ t->binding ] );
256		break;
257
258	    case T_BIND_EXISTS:
259		printf( "time\t--\t%s%s: %s",
260			spaces( depth ), t->name, ctime( &t->time ) );
261		break;
262	    }
263	}
264
265	/*
266	 * Step 3: recursively make0() dependents & headers
267	 */
268
269	/* Step 3a: recursively make0() dependents */
270
271	for( c = t->depends; c; c = c->next )
272	{
273	    int internal = t->flags & T_FLAG_INTERNAL;
274
275	    if( DEBUG_DEPENDS )
276		printf( "%s \"%s\" : \"%s\" ;\n",
277		    internal ? "Includes" : "Depends",
278		    t->name, c->target->name );
279
280	    /* Warn about circular deps, except for includes, */
281	    /* which include each other alot. */
282
283	    if( c->target->fate == T_FATE_INIT )
284		make0( c->target, ptime, depth + 1, counts, anyhow );
285	    else if( c->target->fate == T_FATE_MAKING && !internal )
286		printf( "warning: %s depends on itself\n", c->target->name );
287	}
288
289	/* Step 3b: recursively make0() internal includes node */
290
291	if( t->includes )
292	    make0( t->includes, p, depth + 1, counts, anyhow );
293
294	/* Step 3c: add dependents' includes to our direct dependencies */
295
296	incs = 0;
297
298	for( c = t->depends; c; c = c->next )
299	    if( c->target->includes )
300		incs = targetentry( incs, c->target->includes );
301
302	t->depends = targetchain( t->depends, incs );
303
304	/*
305	 * Step 4: compute time & fate
306	 */
307
308	/* Step 4a: pick up dependents' time and fate */
309
310	last = 0;
311	leaf = 0;
312	fate = T_FATE_STABLE;
313
314	for( c = t->depends; c; c = c->next )
315	{
316	    /* If LEAVES has been applied, we only heed the timestamps of */
317	    /* the leaf source nodes. */
318
319	    leaf = max( leaf, c->target->leaf );
320
321	    if( t->flags & T_FLAG_LEAVES )
322	    {
323		last = leaf;
324		continue;
325	    }
326
327	    last = max( last, c->target->time );
328	    fate = max( fate, c->target->fate );
329	}
330
331	/* Step 4b: pick up included headers time */
332
333	/*
334	 * If a header is newer than a temp source that includes it,
335	 * the temp source will need building.
336	 */
337
338	hlast = t->includes ? t->includes->time : 0;
339
340	/* Step 4c: handle NOUPDATE oddity */
341
342	/*
343	 * If a NOUPDATE file exists, make dependents eternally old.
344	 * Don't inherit our fate from our dependents.  Decide fate
345	 * based only upon other flags and our binding (done later).
346	 */
347
348	if( t->flags & T_FLAG_NOUPDATE )
349	{
350	    last = 0;
351	    t->time = 0;
352	    fate = T_FATE_STABLE;
353	}
354
355	/* Step 4d: determine fate: rebuild target or what? */
356
357	/*
358	    In English:
359		If can't find or make child, can't make target.
360		If children changed, make target.
361		If target missing, make it.
362		If children newer, make target.
363		If temp's children newer than parent, make temp.
364		If temp's headers newer than parent, make temp.
365		If deliberately touched, make it.
366		If up-to-date temp file present, use it.
367		If target newer than non-notfile parent, mark target newer.
368		Otherwise, stable!
369
370		Note this block runs from least to most stable:
371		as we make it further down the list, the target's
372		fate is getting stabler.
373	*/
374
375	if( fate >= T_FATE_BROKEN )
376	{
377	    fate = T_FATE_CANTMAKE;
378	}
379	else if( fate >= T_FATE_SPOIL )
380	{
381	    fate = T_FATE_UPDATE;
382	}
383	else if( t->binding == T_BIND_MISSING )
384	{
385	    fate = T_FATE_MISSING;
386	}
387	else if( t->binding == T_BIND_EXISTS && last > t->time )
388	{
389	    fate = T_FATE_OUTDATED;
390	}
391	else if( t->binding == T_BIND_PARENTS && last > p->time )
392	{
393	    fate = T_FATE_NEEDTMP;
394	}
395	else if( t->binding == T_BIND_PARENTS && hlast > p->time )
396	{
397	    fate = T_FATE_NEEDTMP;
398	}
399	else if( t->flags & T_FLAG_TOUCHED )
400	{
401	    fate = T_FATE_TOUCHED;
402	}
403	else if( anyhow && !( t->flags & T_FLAG_NOUPDATE ) )
404	{
405	    fate = T_FATE_TOUCHED;
406	}
407	else if( t->binding == T_BIND_EXISTS && t->flags & T_FLAG_TEMP )
408	{
409	    fate = T_FATE_ISTMP;
410	}
411	else if( t->binding == T_BIND_EXISTS && p &&
412		 p->binding != T_BIND_UNBOUND && t->time > p->time )
413	{
414	    fate = T_FATE_NEWER;
415	}
416	else
417	{
418	    fate = T_FATE_STABLE;
419	}
420
421	/* Step 4e: handle missing files */
422	/* If it's missing and there are no actions to create it, boom. */
423	/* If we can't make a target we don't care about, 'sokay */
424	/* We could insist that there are updating actions for all missing */
425	/* files, but if they have dependents we just pretend it's NOTFILE. */
426
427	if( fate == T_FATE_MISSING && !t->actions && !t->depends )
428	{
429	    if( t->flags & T_FLAG_NOCARE )
430	    {
431		fate = T_FATE_STABLE;
432	    }
433	    else
434	    {
435		printf( "don't know how to make %s\n", t->name );
436
437		fate = T_FATE_CANTFIND;
438	    }
439	}
440
441	/* Step 4f: propagate dependents' time & fate. */
442	/* Set leaf time to be our time only if this is a leaf. */
443
444	t->time = max( t->time, last );
445	t->leaf = leaf ? leaf : t->time ;
446	t->fate = fate;
447
448	/*
449	 * Step 5: sort dependents by their update time.
450	 */
451
452	if( globs.newestfirst )
453	    t->depends = make0sort( t->depends );
454
455	/*
456	 * Step 6: a little harmless tabulating for tracing purposes
457	 */
458
459	/* Don't count or report interal includes nodes. */
460
461	if( t->flags & T_FLAG_INTERNAL )
462	    return;
463
464	if( !( counts->targets++ % 10000 ) && DEBUG_MAKE )
465	    printf( "...patience...\n" );
466
467	if( fate == T_FATE_ISTMP )
468	    counts->temp++;
469	else if( fate == T_FATE_CANTFIND )
470	    counts->cantfind++;
471	else if( fate == T_FATE_CANTMAKE && t->actions )
472	    counts->cantmake++;
473	else if( fate >= T_FATE_BUILD && fate < T_FATE_BROKEN && t->actions )
474	    counts->updating++;
475
476	if( !( t->flags & T_FLAG_NOTFILE ) && fate >= T_FATE_SPOIL )
477	    flag = "+";
478	else if( t->binding == T_BIND_EXISTS && p && t->time > p->time )
479	    flag = "*";
480
481	if( DEBUG_MAKEPROG )
482	    printf( "made%s\t%s\t%s%s\n",
483		flag, target_fate[ t->fate ],
484		spaces( depth ), t->name );
485
486	if( DEBUG_CAUSES &&
487	    t->fate >= T_FATE_NEWER &&
488	    t->fate <= T_FATE_MISSING )
489		printf( "%s %s\n", target_fate[ t->fate ], t->name );
490}
491
492/*
493 * make0sort() - reorder TARGETS chain by their time (newest to oldest)
494 */
495
496static TARGETS *
497make0sort( TARGETS *chain )
498{
499	TARGETS *result = 0;
500
501	/* We walk chain, taking each item and inserting it on the */
502	/* sorted result, with newest items at the front.  This involves */
503	/* updating each TARGETS' c->next and c->tail.  Note that we */
504	/* make c->tail a valid prev pointer for every entry.  Normally, */
505	/* it is only valid at the head, where prev == tail.  Note also */
506	/* that while tail is a loop, next ends at the end of the chain. */
507
508	/* Walk current target list */
509
510	while( chain )
511	{
512	    TARGETS *c = chain;
513	    TARGETS *s = result;
514
515	    chain = chain->next;
516
517	    /* Find point s in result for c */
518
519	    while( s && s->target->time > c->target->time )
520		s = s->next;
521
522	    /* Insert c in front of s (might be 0). */
523	    /* Don't even think of deciphering this. */
524
525	    c->next = s;			/* good even if s = 0 */
526	    if( result == s ) result = c;	/* new head of chain? */
527	    if( !s ) s = result;		/* wrap to ensure a next */
528	    if( result != c ) s->tail->next = c; /* not head? be prev's next */
529	    c->tail = s->tail;			/* take on next's prev */
530	    s->tail = c;			/* make next's prev us */
531	}
532
533	return result;
534}
535