]> CyberLeo.Net >> Repos - FreeBSD/releng/8.1.git/blob - usr.bin/make/targ.c
Copy stable/8 to releng/8.1 in preparation for 8.1-RC1.
[FreeBSD/releng/8.1.git] / usr.bin / make / targ.c
1 /*-
2  * Copyright (c) 1988, 1989, 1990, 1993
3  *      The Regents of the University of California.  All rights reserved.
4  * Copyright (c) 1989 by Berkeley Softworks
5  * All rights reserved.
6  *
7  * This code is derived from software contributed to Berkeley by
8  * Adam de Boor.
9  *
10  * Redistribution and use in source and binary forms, with or without
11  * modification, are permitted provided that the following conditions
12  * are met:
13  * 1. Redistributions of source code must retain the above copyright
14  *    notice, this list of conditions and the following disclaimer.
15  * 2. Redistributions in binary form must reproduce the above copyright
16  *    notice, this list of conditions and the following disclaimer in the
17  *    documentation and/or other materials provided with the distribution.
18  * 3. All advertising materials mentioning features or use of this software
19  *    must display the following acknowledgement:
20  *      This product includes software developed by the University of
21  *      California, Berkeley and its contributors.
22  * 4. 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  * @(#)targ.c   8.2 (Berkeley) 3/19/94
39  */
40
41 #include <sys/cdefs.h>
42 __FBSDID("$FreeBSD$");
43
44 /*
45  * Functions for maintaining the Lst allTargets. Target nodes are
46  * kept in two structures: a Lst, maintained by the list library, and a
47  * hash table, maintained by the hash library.
48  *
49  * Interface:
50  *      Targ_Init       Initialization procedure.
51  *
52  *      Targ_NewGN      Create a new GNode for the passed target (string).
53  *                      The node is *not* placed in the hash table, though all
54  *                      its fields are initialized.
55  *
56  *      Targ_FindNode   Find the node for a given target, creating and storing
57  *                      it if it doesn't exist and the flags are right
58  *                      (TARG_CREATE)
59  *
60  *      Targ_FindList   Given a list of names, find nodes for all of them. If a
61  *                      name doesn't exist and the TARG_NOCREATE flag was given,
62  *                      an error message is printed. Else, if a name doesn't
63  *                      exist, its node is created.
64  *
65  *      Targ_Ignore     Return TRUE if errors should be ignored when creating
66  *                      the given target.
67  *
68  *      Targ_Silent     Return TRUE if we should be silent when creating the
69  *                      given target.
70  *
71  *      Targ_Precious   Return TRUE if the target is precious and should not
72  *                      be removed if we are interrupted.
73  *
74  * Debugging:
75  *      Targ_PrintGraph Print out the entire graphm all variables and statistics
76  *                      for the directory cache. Should print something for
77  *                      suffixes, too, but...
78  */
79
80 #include <stdio.h>
81 #include <string.h>
82
83 #include "dir.h"
84 #include "globals.h"
85 #include "GNode.h"
86 #include "hash.h"
87 #include "make.h"
88 #include "suff.h"
89 #include "targ.h"
90 #include "util.h"
91 #include "var.h"
92
93 /* the list of all targets found so far */
94 static Lst allTargets = Lst_Initializer(allTargets);
95
96 static Hash_Table targets;      /* a hash table of same */
97
98 #define HTSIZE  191             /* initial size of hash table */
99
100 /**
101  * Targ_Init
102  *      Initialize this module
103  *
104  * Side Effects:
105  *      The allTargets list and the targets hash table are initialized
106  */
107 void
108 Targ_Init(void)
109 {
110
111         Hash_InitTable(&targets, HTSIZE);
112 }
113
114 /**
115  * Targ_NewGN
116  *      Create and initialize a new graph node
117  *
118  * Results:
119  *      An initialized graph node with the name field filled with a copy
120  *      of the passed name
121  *
122  * Side Effects:
123  *      The gnode is added to the list of all gnodes.
124  */
125 GNode *
126 Targ_NewGN(const char *name)
127 {
128         GNode *gn;
129
130         gn = emalloc(sizeof(GNode));
131         gn->name = estrdup(name);
132         gn->path = NULL;
133         if (name[0] == '-' && name[1] == 'l') {
134                 gn->type = OP_LIB;
135         } else {
136                 gn->type = 0;
137         }
138         gn->unmade = 0;
139         gn->make = FALSE;
140         gn->made = UNMADE;
141         gn->childMade = FALSE;
142         gn->order = 0;
143         gn->mtime = gn->cmtime = 0;
144         gn->cmtime_gn = NULL;
145         Lst_Init(&gn->iParents);
146         Lst_Init(&gn->cohorts);
147         Lst_Init(&gn->parents);
148         Lst_Init(&gn->children);
149         Lst_Init(&gn->successors);
150         Lst_Init(&gn->preds);
151         Lst_Init(&gn->context);
152         Lst_Init(&gn->commands);
153         gn->suffix = NULL;
154
155         return (gn);
156 }
157
158 /**
159  * Targ_FindNode
160  *      Find a node in the list using the given name for matching
161  *
162  * Results:
163  *      The node in the list if it was. If it wasn't, return NULL of
164  *      flags was TARG_NOCREATE or the newly created and initialized node
165  *      if it was TARG_CREATE
166  *
167  * Side Effects:
168  *      Sometimes a node is created and added to the list
169  */
170 GNode *
171 Targ_FindNode(const char *name, int flags)
172 {
173         GNode           *gn;    /* node in that element */
174         Hash_Entry      *he;    /* New or used hash entry for node */
175         Boolean         isNew;  /* Set TRUE if Hash_CreateEntry had to create */
176                                 /* an entry for the node */
177
178         if (flags & TARG_CREATE) {
179                 he = Hash_CreateEntry(&targets, name, &isNew);
180                 if (isNew) {
181                         gn = Targ_NewGN(name);
182                         Hash_SetValue(he, gn);
183                         Lst_AtEnd(&allTargets, gn);
184                 }
185         } else {
186                 he = Hash_FindEntry(&targets, name);
187         }
188
189         if (he == NULL) {
190                 return (NULL);
191         } else {
192                 return (Hash_GetValue(he));
193         }
194 }
195
196 /**
197  * Targ_FindList
198  *      Make a complete list of GNodes from the given list of names
199  *
200  * Results:
201  *      A complete list of graph nodes corresponding to all instances of all
202  *      the names in names.
203  *
204  * Side Effects:
205  *      If flags is TARG_CREATE, nodes will be created for all names in
206  *      names which do not yet have graph nodes. If flags is TARG_NOCREATE,
207  *      an error message will be printed for each name which can't be found.
208  */
209 void
210 Targ_FindList(Lst *nodes, Lst *names, int flags)
211 {
212         LstNode *ln;    /* name list element */
213         GNode   *gn;    /* node in tLn */
214         char    *name;
215
216         for (ln = Lst_First(names); ln != NULL; ln = Lst_Succ(ln)) {
217                 name = Lst_Datum(ln);
218                 gn = Targ_FindNode(name, flags);
219                 if (gn != NULL) {
220                         /*
221                          * Note: Lst_AtEnd must come before the Lst_Concat so
222                          * the nodes are added to the list in the order in which
223                          * they were encountered in the makefile.
224                          */
225                         Lst_AtEnd(nodes, gn);
226                         if (gn->type & OP_DOUBLEDEP) {
227                                 Lst_Concat(nodes, &gn->cohorts, LST_CONCNEW);
228                         }
229
230                 } else if (flags == TARG_NOCREATE) {
231                         Error("\"%s\" -- target unknown.", name);
232                 }
233         }
234 }
235
236 /**
237  * Targ_Ignore
238  *      Return true if should ignore errors when creating gn
239  *
240  * Results:
241  *      TRUE if should ignore errors
242  */
243 Boolean
244 Targ_Ignore(GNode *gn)
245 {
246
247         if (ignoreErrors || (gn->type & OP_IGNORE)) {
248                 return (TRUE);
249         } else {
250                 return (FALSE);
251         }
252 }
253
254 /**
255  * Targ_Silent
256  *      Return true if be silent when creating gn
257  *
258  * Results:
259  *      TRUE if should be silent
260  */
261 Boolean
262 Targ_Silent(GNode *gn)
263 {
264
265         if (beSilent || (gn->type & OP_SILENT)) {
266                 return (TRUE);
267         } else {
268                 return (FALSE);
269         }
270 }
271
272 /**
273  * Targ_Precious
274  *      See if the given target is precious
275  *
276  * Results:
277  *      TRUE if it is precious. FALSE otherwise
278  */
279 Boolean
280 Targ_Precious(GNode *gn)
281 {
282
283         if (allPrecious || (gn->type & (OP_PRECIOUS | OP_DOUBLEDEP))) {
284                 return (TRUE);
285         } else {
286                 return (FALSE);
287         }
288 }
289
290 static GNode    *mainTarg;      /* the main target, as set by Targ_SetMain */
291
292 /**
293  * Targ_SetMain
294  *      Set our idea of the main target we'll be creating. Used for
295  *      debugging output.
296  *
297  * Side Effects:
298  *      "mainTarg" is set to the main target's node.
299  */
300 void
301 Targ_SetMain(GNode *gn)
302 {
303
304         mainTarg = gn;
305 }
306
307 /**
308  * Targ_FmtTime
309  *      Format a modification time in some reasonable way and return it.
310  *
311  * Results:
312  *      The time reformatted.
313  *
314  * Side Effects:
315  *      The time is placed in a static area, so it is overwritten
316  *      with each call.
317  */
318 char *
319 Targ_FmtTime(time_t modtime)
320 {
321         struct tm       *parts;
322         static char     buf[128];
323
324         parts = localtime(&modtime);
325
326         strftime(buf, sizeof(buf), "%H:%M:%S %b %d, %Y", parts);
327         buf[sizeof(buf) - 1] = '\0';
328         return (buf);
329 }
330
331 /**
332  * Targ_PrintType
333  *      Print out a type field giving only those attributes the user can
334  *      set.
335  */
336 void
337 Targ_PrintType(int type)
338 {
339         static const struct flag2str type2str[] = {
340                 { OP_OPTIONAL,  ".OPTIONAL" },
341                 { OP_USE,       ".USE" },
342                 { OP_EXEC,      ".EXEC" },
343                 { OP_IGNORE,    ".IGNORE" },
344                 { OP_PRECIOUS,  ".PRECIOUS" },
345                 { OP_SILENT,    ".SILENT" },
346                 { OP_MAKE,      ".MAKE" },
347                 { OP_JOIN,      ".JOIN" },
348                 { OP_INVISIBLE, ".INVISIBLE" },
349                 { OP_NOTMAIN,   ".NOTMAIN" },
350                 { OP_PHONY,     ".PHONY" },
351                 { OP_LIB,       ".LIB" },
352                 { OP_MEMBER,    ".MEMBER" },
353                 { OP_ARCHV,     ".ARCHV" },
354                 { 0,            NULL }
355         };
356
357         type &= ~OP_OPMASK;
358         if (!DEBUG(TARG))
359                 type &= ~(OP_ARCHV | OP_LIB | OP_MEMBER);
360         print_flags(stdout, type2str, type, 0);
361 }
362
363 /**
364  * TargPrintNode
365  *      print the contents of a node
366  */
367 static int
368 TargPrintNode(const GNode *gn, int pass)
369 {
370         const LstNode   *tln;
371
372         if (!OP_NOP(gn->type)) {
373                 printf("#\n");
374                 if (gn == mainTarg) {
375                         printf("# *** MAIN TARGET ***\n");
376                 }
377                 if (pass == 2) {
378                         if (gn->unmade) {
379                                 printf("# %d unmade children\n", gn->unmade);
380                         } else {
381                                 printf("# No unmade children\n");
382                         }
383                         if (!(gn->type & (OP_JOIN | OP_USE | OP_EXEC))) {
384                                 if (gn->mtime != 0) {
385                                         printf("# last modified %s: %s\n",
386                                             Targ_FmtTime(gn->mtime),
387                                             gn->made == UNMADE ? "unmade" :
388                                             gn->made == MADE ? "made" :
389                                             gn->made == UPTODATE ? "up-to-date":
390                                             "error when made");
391                                 } else if (gn->made != UNMADE) {
392                                         printf("# non-existent (maybe): %s\n",
393                                             gn->made == MADE ? "made" :
394                                             gn->made == UPTODATE ? "up-to-date":
395                                             gn->made == ERROR?"error when made":
396                                             "aborted");
397                                 } else {
398                                         printf("# unmade\n");
399                                 }
400                         }
401                         if (!Lst_IsEmpty(&gn->iParents)) {
402                                 printf("# implicit parents: ");
403                                 LST_FOREACH(tln, &gn->iParents)
404                                         printf("%s ", ((const GNode *)
405                                             Lst_Datum(tln))->name);
406                                 printf("\n");
407                         }
408                 }
409                 if (!Lst_IsEmpty(&gn->parents)) {
410                         printf("# parents: ");
411                         LST_FOREACH(tln, &gn->parents)
412                                 printf("%s ", ((const GNode *)
413                                     Lst_Datum(tln))->name);
414                         printf("\n");
415                 }
416
417                 printf("%-16s", gn->name);
418                 switch (gn->type & OP_OPMASK) {
419                   case OP_DEPENDS:
420                         printf(": ");
421                         break;
422                   case OP_FORCE:
423                         printf("! ");
424                         break;
425                   case OP_DOUBLEDEP:
426                         printf(":: ");
427                         break;
428                   default:
429                         break;
430                 }
431                 Targ_PrintType(gn->type);
432                 LST_FOREACH(tln, &gn->children)
433                         printf("%s ", ((const GNode *)Lst_Datum(tln))->name);
434                 printf("\n");
435                 LST_FOREACH(tln, &gn->commands)
436                         printf("\t%s\n", (const char *)Lst_Datum(tln));
437                 printf("\n\n");
438                 if (gn->type & OP_DOUBLEDEP) {
439                         LST_FOREACH(tln, &gn->cohorts)
440                                 TargPrintNode((const GNode *)Lst_Datum(tln),
441                                     pass);
442                 }
443         }
444         return (0);
445 }
446
447 /**
448  * Targ_PrintGraph
449  *      Print the entire graph.
450  */
451 void
452 Targ_PrintGraph(int pass)
453 {
454         const GNode     *gn;
455         const LstNode   *tln;
456
457         printf("#*** Input graph:\n");
458         LST_FOREACH(tln, &allTargets)
459                 TargPrintNode((const GNode *)Lst_Datum(tln), pass);
460         printf("\n\n");
461
462         printf("#\n#   Files that are only sources:\n");
463         LST_FOREACH(tln, &allTargets) {
464                 gn = Lst_Datum(tln);
465                 if (OP_NOP(gn->type))
466                         printf("#\t%s [%s]\n", gn->name,
467                             gn->path ? gn->path : gn->name);
468         }
469         Var_Dump();
470         printf("\n");
471         Dir_PrintDirectories();
472         printf("\n");
473         Suff_PrintAll();
474 }