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