xref: /openbsd-src/usr.bin/make/make.c (revision 850e275390052b330d93020bf619a739a3c277ac)
1 /*	$OpenPackages$ */
2 /*	$OpenBSD: make.c,v 1.57 2008/01/12 13:08:59 espie Exp $	*/
3 /*	$NetBSD: make.c,v 1.10 1996/11/06 17:59:15 christos Exp $	*/
4 
5 /*
6  * Copyright (c) 1988, 1989, 1990, 1993
7  *	The Regents of the University of California.  All rights reserved.
8  * Copyright (c) 1989 by Berkeley Softworks
9  * All rights reserved.
10  *
11  * This code is derived from software contributed to Berkeley by
12  * Adam de Boor.
13  *
14  * Redistribution and use in source and binary forms, with or without
15  * modification, are permitted provided that the following conditions
16  * are met:
17  * 1. Redistributions of source code must retain the above copyright
18  *    notice, this list of conditions and the following disclaimer.
19  * 2. Redistributions in binary form must reproduce the above copyright
20  *    notice, this list of conditions and the following disclaimer in the
21  *    documentation and/or other materials provided with the distribution.
22  * 3. Neither the name of the University nor the names of its contributors
23  *    may be used to endorse or promote products derived from this software
24  *    without specific prior written permission.
25  *
26  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
27  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
28  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
29  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
30  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
31  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
32  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
33  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
34  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
35  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
36  * SUCH DAMAGE.
37  */
38 
39 /*-
40  * make.c --
41  *	The functions which perform the examination of targets and
42  *	their suitability for creation
43  *
44  * Interface:
45  *	Make_Run		Initialize things for the module and recreate
46  *				whatever needs recreating. Returns true if
47  *				work was (or would have been) done and
48  *				false
49  *				otherwise.
50  *
51  *	Make_Update		Update all parents of a given child. Performs
52  *				various bookkeeping chores like the updating
53  *				of the cmtime field of the parent, filling
54  *				of the IMPSRC context variable, etc. It will
55  *				place the parent on the toBeMade queue if it
56  *				should be.
57  *
58  */
59 
60 #include <limits.h>
61 #include <stdio.h>
62 #include <signal.h>
63 #include <stddef.h>
64 #include <stdlib.h>
65 #include <string.h>
66 #include <ohash.h>
67 #include "config.h"
68 #include "defines.h"
69 #include "dir.h"
70 #include "job.h"
71 #include "suff.h"
72 #include "var.h"
73 #include "error.h"
74 #include "make.h"
75 #include "gnode.h"
76 #include "extern.h"
77 #include "timestamp.h"
78 #include "engine.h"
79 #include "lst.h"
80 #include "targ.h"
81 #include "garray.h"
82 #include "memory.h"
83 
84 /* what gets added each time. Kept as one static array so that it doesn't
85  * get resized every time.
86  */
87 static struct growableArray examine;
88 /* The current fringe of the graph. These are nodes which await examination by
89  * MakeOODate. It is added to by Make_Update and subtracted from by
90  * MakeStartJobs */
91 static struct growableArray toBeMade;
92 
93 static struct ohash targets;	/* stuff we must build */
94 
95 static void MakeAddChild(void *, void *);
96 static void MakeHandleUse(void *, void *);
97 static bool MakeStartJobs(void);
98 static void MakePrintStatus(void *, void *);
99 static bool try_to_make_node(GNode *);
100 static void add_targets_to_make(Lst);
101 
102 static bool has_unmade_predecessor(GNode *);
103 static void requeue_successors(GNode *);
104 static void random_setup(void);
105 
106 static bool randomize_queue;
107 long random_delay = 0;
108 
109 bool
110 no_jobs_left()
111 {
112 	return Array_IsEmpty(&toBeMade);
113 }
114 
115 static void
116 random_setup()
117 {
118 	randomize_queue = Var_Definedi("RANDOM_ORDER", NULL);
119 
120 	if (Var_Definedi("RANDOM_DELAY", NULL))
121 		random_delay = strtonum(Var_Value("RANDOM_DELAY"), 0, 1000,
122 		    NULL) * 1000000;
123 
124 	if (randomize_queue || random_delay) {
125 		unsigned int random_seed;
126 		char *t;
127 
128 		t = Var_Value("RANDOM_SEED");
129 		if (t != NULL)
130 			random_seed = strtonum(t, 0, UINT_MAX, NULL);
131 		else
132 			random_seed = time(NULL);
133 		fprintf(stderr, "RANDOM_SEED=%u\n", random_seed);
134 		srandom(random_seed);
135 	}
136 }
137 
138 static void
139 randomize_garray(struct growableArray *g)
140 {
141 	/* This is a fairly standard algorithm to randomize an array. */
142 	unsigned int i, v;
143 	GNode *e;
144 
145 	for (i = g->n; i > 0; i--) {
146 		v = random() % i;
147 		if (v == i-1)
148 			continue;
149 		else {
150 			e = g->a[i-1];
151 			g->a[i-1] = g->a[v];
152 			g->a[v] = e;
153 		}
154 	}
155 }
156 
157 static bool
158 has_unmade_predecessor(GNode *gn)
159 {
160 	LstNode ln;
161 
162 	if (Lst_IsEmpty(&gn->preds))
163 		return false;
164 
165 
166 	for (ln = Lst_First(&gn->preds); ln != NULL; ln = Lst_Adv(ln)) {
167 		GNode	*pgn = (GNode *)Lst_Datum(ln);
168 
169 		if (pgn->must_make && pgn->built_status == UNKNOWN) {
170 			if (DEBUG(MAKE))
171 				printf("predecessor %s not made yet.\n",
172 				    pgn->name);
173 			return true;
174 		}
175 	}
176 	return false;
177 }
178 
179 static void
180 requeue_successors(GNode *gn)
181 {
182 	LstNode ln;
183 	/* Deal with successor nodes. If any is marked for making and has an
184 	 * unmade count of 0, has not been made and isn't in the examination
185 	 * queue, it means we need to place it in the queue as it restrained
186 	 * itself before.	*/
187 	for (ln = Lst_First(&gn->successors); ln != NULL; ln = Lst_Adv(ln)) {
188 		GNode	*succ = (GNode *)Lst_Datum(ln);
189 
190 		if (succ->must_make && succ->unmade == 0
191 		    && succ->built_status == UNKNOWN)
192 			Array_PushNew(&toBeMade, succ);
193 	}
194 }
195 
196 /*-
197  *-----------------------------------------------------------------------
198  * Make_Update	--
199  *	Perform update on the parents of a node. Used by JobFinish once
200  *	a node has been dealt with and by MakeStartJobs if it finds an
201  *	up-to-date node.
202  *
203  * Results:
204  *	Always returns 0
205  *
206  * Side Effects:
207  *	The unmade field of pgn is decremented and pgn may be placed on
208  *	the toBeMade queue if this field becomes 0.
209  *
210  *	If the child was made, the parent's childMade field will be set true
211  *	and its cmtime set to now.
212  *
213  *	If the child wasn't made, the cmtime field of the parent will be
214  *	altered if the child's mtime is big enough.
215  *
216  *-----------------------------------------------------------------------
217  */
218 void
219 Make_Update(GNode *cgn)	/* the child node */
220 {
221 	GNode	*pgn;	/* the parent node */
222 	char	*cname; /* the child's name */
223 	LstNode	ln;	/* Element in parents list */
224 
225 	cname = Var(TARGET_INDEX, cgn);
226 
227 	/*
228 	 * If the child was actually made, see what its modification time is
229 	 * now -- some rules won't actually update the file. If the file still
230 	 * doesn't exist, make its mtime now.
231 	 */
232 	if (cgn->built_status != UPTODATE) {
233 		/*
234 		 * This is what Make does and it's actually a good thing, as it
235 		 * allows rules like
236 		 *
237 		 *	cmp -s y.tab.h parse.h || cp y.tab.h parse.h
238 		 *
239 		 * to function as intended. Unfortunately, thanks to the
240 		 * stateless nature of NFS, there are times when the
241 		 * modification time of a file created on a remote machine
242 		 * will not be modified before the local stat() implied by
243 		 * the Dir_MTime occurs, thus leading us to believe that the
244 		 * file is unchanged, wreaking havoc with files that depend
245 		 * on this one.
246 		 */
247 		if (noExecute || is_out_of_date(Dir_MTime(cgn)))
248 			cgn->mtime = now;
249 		if (DEBUG(MAKE))
250 			printf("update time: %s\n", time_to_string(cgn->mtime));
251 	}
252 
253 	for (ln = Lst_First(&cgn->parents); ln != NULL; ln = Lst_Adv(ln)) {
254 		pgn = (GNode *)Lst_Datum(ln);
255 		pgn->unmade--;
256 		if (pgn->must_make) {
257 			if (DEBUG(MAKE))
258 				printf("%s--=%d ",
259 				    pgn->name, pgn->unmade);
260 
261 			if ( ! (cgn->type & (OP_EXEC|OP_USE))) {
262 				if (cgn->built_status == MADE) {
263 					pgn->childMade = true;
264 					if (is_strictly_before(pgn->cmtime,
265 					    cgn->mtime))
266 						pgn->cmtime = cgn->mtime;
267 				} else {
268 					(void)Make_TimeStamp(pgn, cgn);
269 				}
270 			}
271 			if (pgn->unmade == 0) {
272 				/*
273 				 * Queue the node up -- any unmade
274 				 * predecessors will be dealt with in
275 				 * MakeStartJobs.
276 				 */
277 				if (DEBUG(MAKE))
278 					printf("QUEUING ");
279 				Array_Push(&toBeMade, pgn);
280 			} else if (pgn->unmade < 0) {
281 				Error("Child %s discovered graph cycles through %s", cgn->name, pgn->name);
282 			}
283 		}
284 	}
285 	if (DEBUG(MAKE))
286 		printf("\n");
287 	requeue_successors(cgn);
288 }
289 
290 static bool
291 try_to_make_node(GNode *gn)
292 {
293 	if (DEBUG(MAKE))
294 		printf("Examining %s...", gn->name);
295 
296 	if (gn->unmade != 0) {
297 		if (DEBUG(MAKE))
298 			printf(" Requeuing (%d)\n", gn->unmade);
299 		add_targets_to_make(&gn->children);
300 		Array_Push(&toBeMade, gn);
301 		return false;
302 	}
303 	if (has_been_built(gn)) {
304 		if (DEBUG(MAKE))
305 			printf(" already made\n");
306 			return false;
307 	}
308 	if (has_unmade_predecessor(gn)) {
309 		if (DEBUG(MAKE))
310 			printf(" Dropping for now\n");
311 		return false;
312 	}
313 
314 	Suff_FindDeps(gn);
315 	if (gn->unmade != 0) {
316 		if (DEBUG(MAKE))
317 			printf(" Requeuing (after deps: %d)\n", gn->unmade);
318 		add_targets_to_make(&gn->children);
319 		return false;
320 	}
321 	if (Make_OODate(gn)) {
322 		if (DEBUG(MAKE))
323 			printf("out-of-date\n");
324 		if (queryFlag)
325 			return true;
326 		Make_DoAllVar(gn);
327 		Job_Make(gn);
328 	} else {
329 		if (DEBUG(MAKE))
330 			printf("up-to-date\n");
331 		gn->built_status = UPTODATE;
332 		if (gn->type & OP_JOIN) {
333 			/*
334 			 * Even for an up-to-date .JOIN node, we need it
335 			 * to have its context variables so references
336 			 * to it get the correct value for .TARGET when
337 			 * building up the context variables of its
338 			 * parent(s)...
339 			 */
340 			Make_DoAllVar(gn);
341 		}
342 
343 		Make_Update(gn);
344 	}
345 	return false;
346 }
347 
348 /*
349  *-----------------------------------------------------------------------
350  * MakeStartJobs --
351  *	Start as many jobs as possible.
352  *
353  * Results:
354  *	If the query flag was given to pmake, no job will be started,
355  *	but as soon as an out-of-date target is found, this function
356  *	returns true. At all other times, this function returns false.
357  *
358  * Side Effects:
359  *	Nodes are removed from the toBeMade queue and job table slots
360  *	are filled.
361  *-----------------------------------------------------------------------
362  */
363 static bool
364 MakeStartJobs(void)
365 {
366 	GNode	*gn;
367 
368 	while (!Job_Full() && (gn = Array_Pop(&toBeMade)) != NULL) {
369 		if (try_to_make_node(gn))
370 			return true;
371 	}
372 	return false;
373 }
374 
375 /*-
376  *-----------------------------------------------------------------------
377  * MakePrintStatus --
378  *	Print the status of a top-level node, viz. it being up-to-date
379  *	already or not created due to an error in a lower level.
380  *	Callback function for Make_Run via Lst_ForEach.
381  *
382  * Side Effects:
383  *	A message may be printed.
384  *-----------------------------------------------------------------------
385  */
386 static void
387 MakePrintStatus(
388     void *gnp,		    /* Node to examine */
389     void *cyclep)	    /* True if gn->unmade being non-zero implies
390 			     * a cycle in the graph, not an error in an
391 			     * inferior */
392 {
393 	GNode	*gn = (GNode *)gnp;
394 	bool	cycle = *(bool *)cyclep;
395 	if (gn->built_status == UPTODATE) {
396 		printf("`%s' is up to date.\n", gn->name);
397 	} else if (gn->unmade != 0) {
398 		if (cycle) {
399 			bool t = true;
400 			/*
401 			 * If printing cycles and came to one that has unmade
402 			 * children, print out the cycle by recursing on its
403 			 * children. Note a cycle like:
404 			 *	a : b
405 			 *	b : c
406 			 *	c : b
407 			 * will cause this to erroneously complain about a
408 			 * being in the cycle, but this is a good approximation.
409 			 */
410 			if (gn->built_status == CYCLE) {
411 				Error("Graph cycles through `%s'", gn->name);
412 				gn->built_status = ENDCYCLE;
413 				Lst_ForEach(&gn->children, MakePrintStatus, &t);
414 				gn->built_status = UNKNOWN;
415 			} else if (gn->built_status != ENDCYCLE) {
416 				gn->built_status = CYCLE;
417 				Lst_ForEach(&gn->children, MakePrintStatus, &t);
418 			}
419 		} else {
420 			printf("`%s' not remade because of errors.\n",
421 			    gn->name);
422 		}
423 	}
424 }
425 
426 
427 static void
428 MakeAddChild(void *to_addp, void *ap)
429 {
430 	GNode *gn = (GNode *)to_addp;
431 
432 	if (!gn->must_make && !(gn->type & OP_USE))
433 		Array_Push((struct growableArray *)ap, gn);
434 }
435 
436 static void
437 MakeHandleUse(void *pgn, void *cgn)
438 {
439 	Make_HandleUse((GNode *)pgn, (GNode *)cgn);
440 }
441 
442 /* Add stuff to the toBeMade queue. we try to sort things so that stuff
443  * that can be done directly is done right away.  This won't be perfect,
444  * since some dependencies are only discovered later (e.g., SuffFindDeps).
445  */
446 static void
447 add_targets_to_make(Lst todo)
448 {
449 	GNode *gn;
450 
451 	unsigned int slot;
452 
453 	AppendList2Array(todo, &examine);
454 
455 	while ((gn = Array_Pop(&examine)) != NULL) {
456 		if (gn->must_make) 	/* already known */
457 			continue;
458 		gn->must_make = true;
459 
460 		slot = ohash_qlookup(&targets, gn->name);
461 		if (!ohash_find(&targets, slot))
462 			ohash_insert(&targets, slot, gn);
463 
464 
465 		look_harder_for_target(gn);
466 		/*
467 		 * Apply any .USE rules before looking for implicit
468 		 * dependencies to make sure everything that should have
469 		 * commands has commands ...
470 		 */
471 		Lst_ForEach(&gn->children, MakeHandleUse, gn);
472 		expand_all_children(gn);
473 
474 		if (gn->unmade != 0) {
475 			if (DEBUG(MAKE))
476 				printf("%s: not queuing (%d unmade children)\n",
477 				    gn->name, gn->unmade);
478 			Lst_ForEach(&gn->children, MakeAddChild,
479 			    &examine);
480 		} else {
481 			if (DEBUG(MAKE))
482 				printf("%s: queuing\n", gn->name);
483 			Array_Push(&toBeMade, gn);
484 		}
485 	}
486 	if (randomize_queue)
487 		randomize_garray(&toBeMade);
488 }
489 
490 /*-
491  *-----------------------------------------------------------------------
492  * Make_Run --
493  *	Initialize the nodes to remake and the list of nodes which are
494  *	ready to be made by doing a breadth-first traversal of the graph
495  *	starting from the nodes in the given list. Once this traversal
496  *	is finished, all the 'leaves' of the graph are in the toBeMade
497  *	queue.
498  *	Using this queue and the Job module, work back up the graph,
499  *	calling on MakeStartJobs to keep the job table as full as
500  *	possible.
501  *
502  * Results:
503  *	true if work was done. false otherwise.
504  *
505  * Side Effects:
506  *	The must_make field of all nodes involved in the creation of the given
507  *	targets is set to 1. The toBeMade list is set to contain all the
508  *	'leaves' of these subgraphs.
509  *-----------------------------------------------------------------------
510  */
511 bool
512 Make_Run(Lst targs)		/* the initial list of targets */
513 {
514 	int errors;	/* Number of errors the Job module reports */
515 	GNode *gn;
516 	unsigned int i;
517 	bool cycle;
518 
519 	/* wild guess at initial sizes */
520 	Array_Init(&toBeMade, 500);
521 	Array_Init(&examine, 150);
522 	ohash_init(&targets, 10, &gnode_info);
523 	if (DEBUG(PARALLEL))
524 		random_setup();
525 
526 	add_targets_to_make(targs);
527 	if (queryFlag) {
528 		/*
529 		 * We wouldn't do any work unless we could start some jobs in
530 		 * the next loop... (we won't actually start any, of course,
531 		 * this is just to see if any of the targets was out of date)
532 		 */
533 		return MakeStartJobs();
534 	} else {
535 		/*
536 		 * Initialization. At the moment, no jobs are running and until
537 		 * some get started, nothing will happen since the remaining
538 		 * upward traversal of the graph is performed by the routines
539 		 * in job.c upon the finishing of a job. So we fill the Job
540 		 * table as much as we can before going into our loop.
541 		 */
542 		(void)MakeStartJobs();
543 	}
544 
545 	/*
546 	 * Main Loop: The idea here is that the ending of jobs will take
547 	 * care of the maintenance of data structures and the waiting for output
548 	 * will cause us to be idle most of the time while our children run as
549 	 * much as possible. Because the job table is kept as full as possible,
550 	 * the only time when it will be empty is when all the jobs which need
551 	 * running have been run, so that is the end condition of this loop.
552 	 * Note that the Job module will exit if there were any errors unless
553 	 * the keepgoing flag was given.
554 	 */
555 	while (!Job_Empty()) {
556 		handle_running_jobs();
557 		(void)MakeStartJobs();
558 	}
559 
560 	errors = Job_Finish();
561 	cycle = false;
562 
563 	for (gn = ohash_first(&targets, &i); gn != NULL;
564 	    gn = ohash_next(&targets, &i)) {
565 	    	if (has_been_built(gn))
566 			continue;
567 		cycle = true;
568 		errors++;
569 	    	printf("Error: target %s unaccounted for (%s)\n",
570 		    gn->name, status_to_string(gn));
571 	}
572 	/*
573 	 * Print the final status of each target. E.g. if it wasn't made
574 	 * because some inferior reported an error.
575 	 */
576 	Lst_ForEach(targs, MakePrintStatus, &cycle);
577 	if (errors)
578 		Fatal("Errors while building");
579 
580 	return true;
581 }
582