[BACK]Return to targ.c CVS log [TXT][DIR] Up to [local] / src / usr.bin / make

Annotation of src/usr.bin/make/targ.c, Revision 1.86

1.86    ! espie       1: /*     $OpenBSD: targ.c,v 1.85 2020/01/26 12:41:21 espie Exp $ */
1.6       millert     2: /*     $NetBSD: targ.c,v 1.11 1997/02/20 16:51:50 christos Exp $       */
1.1       deraadt     3:
                      4: /*
1.31      espie       5:  * Copyright (c) 1999 Marc Espie.
                      6:  *
                      7:  * Extensive code changes for the OpenBSD project.
                      8:  *
                      9:  * Redistribution and use in source and binary forms, with or without
                     10:  * modification, are permitted provided that the following conditions
                     11:  * are met:
                     12:  * 1. Redistributions of source code must retain the above copyright
                     13:  *    notice, this list of conditions and the following disclaimer.
                     14:  * 2. Redistributions in binary form must reproduce the above copyright
                     15:  *    notice, this list of conditions and the following disclaimer in the
                     16:  *    documentation and/or other materials provided with the distribution.
                     17:  *
                     18:  * THIS SOFTWARE IS PROVIDED BY THE OPENBSD PROJECT AND CONTRIBUTORS
                     19:  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
                     20:  * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
                     21:  * A PARTICULAR PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE OPENBSD
                     22:  * PROJECT OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
                     23:  * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
                     24:  * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
                     25:  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
                     26:  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
                     27:  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
                     28:  * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
                     29:  */
                     30: /*
1.5       millert    31:  * Copyright (c) 1988, 1989, 1990, 1993
                     32:  *     The Regents of the University of California.  All rights reserved.
1.1       deraadt    33:  * Copyright (c) 1989 by Berkeley Softworks
                     34:  * All rights reserved.
                     35:  *
                     36:  * This code is derived from software contributed to Berkeley by
                     37:  * Adam de Boor.
                     38:  *
                     39:  * Redistribution and use in source and binary forms, with or without
                     40:  * modification, are permitted provided that the following conditions
                     41:  * are met:
                     42:  * 1. Redistributions of source code must retain the above copyright
                     43:  *    notice, this list of conditions and the following disclaimer.
                     44:  * 2. Redistributions in binary form must reproduce the above copyright
                     45:  *    notice, this list of conditions and the following disclaimer in the
                     46:  *    documentation and/or other materials provided with the distribution.
1.38      millert    47:  * 3. Neither the name of the University nor the names of its contributors
1.1       deraadt    48:  *    may be used to endorse or promote products derived from this software
                     49:  *    without specific prior written permission.
                     50:  *
                     51:  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
                     52:  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
                     53:  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
                     54:  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
                     55:  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
                     56:  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
                     57:  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
                     58:  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
                     59:  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
                     60:  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
                     61:  * SUCH DAMAGE.
                     62:  */
                     63:
                     64: /*-
                     65:  * targ.c --
1.27      espie      66:  *             Target nodes are kept into a hash table.
1.1       deraadt    67:  *
                     68:  * Interface:
1.31      espie      69:  *     Targ_Init               Initialization procedure.
1.1       deraadt    70:  *
1.31      espie      71:  *     Targ_NewGN              Create a new GNode for the passed target
                     72:  *                             (string). The node is *not* placed in the
                     73:  *                             hash table, though all its fields are
                     74:  *                             initialized.
1.1       deraadt    75:  *
1.31      espie      76:  *     Targ_FindNode           Find the node for a given target, creating
                     77:  *                             and storing it if it doesn't exist and the
                     78:  *                             flags are right (TARG_CREATE)
1.1       deraadt    79:  *
1.31      espie      80:  *     Targ_FindList           Given a list of names, find nodes for all
                     81:  *                             of them, creating nodes if needed.
1.1       deraadt    82:  *
1.32      espie      83:  *     Targ_Ignore             Return true if errors should be ignored when
1.31      espie      84:  *                             creating the given target.
1.1       deraadt    85:  *
1.32      espie      86:  *     Targ_Silent             Return true if we should be silent when
1.31      espie      87:  *                             creating the given target.
1.1       deraadt    88:  *
1.32      espie      89:  *     Targ_Precious           Return true if the target is precious and
1.31      espie      90:  *                             should not be removed if we are interrupted.
1.1       deraadt    91:  *
                     92:  * Debugging:
1.31      espie      93:  *     Targ_PrintGraph         Print out the entire graphm all variables
                     94:  *                             and statistics for the directory cache. Should
                     95:  *                             print something for suffixes, too, but...
1.1       deraadt    96:  */
                     97:
1.34      espie      98: #include <limits.h>
1.32      espie      99: #include <stddef.h>
1.70      espie     100: #include <stdint.h>
1.32      espie     101: #include <stdio.h>
1.67      espie     102: #include <stdlib.h>
1.33      espie     103: #include <string.h>
1.70      espie     104: #include <ohash.h>
1.32      espie     105: #include "defines.h"
                    106: #include "stats.h"
                    107: #include "suff.h"
                    108: #include "var.h"
                    109: #include "targ.h"
                    110: #include "memory.h"
                    111: #include "gnode.h"
                    112: #include "extern.h"
                    113: #include "timestamp.h"
                    114: #include "lst.h"
1.46      espie     115: #include "node_int.h"
                    116: #include "nodehashconsts.h"
1.67      espie     117: #include "dump.h"
1.31      espie     118:
1.57      espie     119: static struct ohash targets;   /* hash table of targets */
1.46      espie     120: struct ohash_info gnode_info = {
1.75      espie     121:        offsetof(GNode, name), NULL, hash_calloc, hash_free, element_alloc
1.42      espie     122: };
1.1       deraadt   123:
1.83      espie     124: static GNode *Targ_mk_node(const char *, const char *, unsigned int,
                    125:     unsigned char, unsigned int);
1.48      espie     126:
1.83      espie     127: #define Targ_mk_constant(n, type) \
                    128:     Targ_mk_special_node(n, sizeof(n), K_##n, type, SPECIAL_NONE, 0)
1.1       deraadt   129:
1.46      espie     130: GNode *begin_node, *end_node, *interrupt_node, *DEFAULT;
                    131:
1.1       deraadt   132: void
1.39      espie     133: Targ_Init(void)
1.1       deraadt   134: {
1.42      espie     135:        /* A small make file already creates 200 targets.  */
                    136:        ohash_init(&targets, 10, &gnode_info);
1.83      espie     137:        begin_node = Targ_mk_constant(NODE_BEGIN,
                    138:            OP_DUMMY | OP_NOTMAIN | OP_NODEFAULT);
                    139:        end_node = Targ_mk_constant(NODE_END,
                    140:            OP_DUMMY | OP_NOTMAIN | OP_NODEFAULT);
                    141:        interrupt_node = Targ_mk_constant(NODE_INTERRUPT,
                    142:            OP_DUMMY | OP_NOTMAIN | OP_NODEFAULT);
                    143:        DEFAULT = Targ_mk_constant(NODE_DEFAULT,
                    144:            OP_DUMMY | OP_NOTMAIN| OP_TRANSFORM | OP_NODEFAULT);
1.46      espie     145:
1.1       deraadt   146: }
                    147:
1.83      espie     148: static GNode *
                    149: Targ_mk_node(const char *name, const char *ename,
                    150:     unsigned int type, unsigned char special, unsigned int special_op)
1.1       deraadt   151: {
1.42      espie     152:        GNode *gn;
1.1       deraadt   153:
1.42      espie     154:        gn = ohash_create_entry(&gnode_info, name, &ename);
                    155:        gn->path = NULL;
1.83      espie     156:        gn->type = type;
                    157:        gn->special = special;
                    158:        gn->special_op = special_op;
1.80      espie     159:        gn->children_left = 0;
1.51      espie     160:        gn->must_make = false;
1.53      espie     161:        gn->built_status = UNKNOWN;
1.78      espie     162:        gn->in_cycle = false;
1.80      espie     163:        gn->child_rebuilt = false;
1.46      espie     164:        gn->order = 0;
1.42      espie     165:        ts_set_out_of_date(gn->mtime);
1.71      espie     166:        gn->youngest = gn;
1.42      espie     167:        Lst_Init(&gn->cohorts);
                    168:        Lst_Init(&gn->parents);
                    169:        Lst_Init(&gn->children);
1.80      espie     170:        Lst_Init(&gn->predecessors);
1.42      espie     171:        Lst_Init(&gn->successors);
1.81      espie     172:        SymTable_Init(&gn->localvars);
1.50      espie     173:        gn->impliedsrc = NULL;
1.42      espie     174:        Lst_Init(&gn->commands);
1.56      espie     175:        gn->suffix = NULL;
1.57      espie     176:        gn->next = NULL;
                    177:        gn->basename = NULL;
                    178:        gn->sibling = gn;
1.69      espie     179:        gn->groupling = NULL;
1.1       deraadt   180:
1.31      espie     181: #ifdef STATS_GN_CREATION
1.42      espie     182:        STAT_GN_COUNT++;
1.10      espie     183: #endif
1.1       deraadt   184:
1.42      espie     185:        return gn;
1.1       deraadt   186: }
                    187:
1.46      espie     188: GNode *
1.83      espie     189: Targ_NewGNi(const char *name, const char *ename)
1.46      espie     190: {
1.83      espie     191:        return Targ_mk_node(name, ename, OP_ZERO, SPECIAL_NONE, 0);
1.46      espie     192: }
1.1       deraadt   193:
                    194: GNode *
1.83      espie     195: Targ_FindNodei(const char *name, const char *ename, int flags)
1.1       deraadt   196: {
1.83      espie     197:        uint32_t hv;
1.46      espie     198:        GNode *gn;
1.42      espie     199:        unsigned int slot;
1.27      espie     200:
1.83      espie     201:        hv = ohash_interval(name, &ename);
                    202:
1.46      espie     203:        slot = ohash_lookup_interval(&targets, name, ename, hv);
1.27      espie     204:
1.42      espie     205:        gn = ohash_find(&targets, slot);
1.31      espie     206:
1.42      espie     207:        if (gn == NULL && (flags & TARG_CREATE)) {
                    208:                gn = Targ_NewGNi(name, ename);
                    209:                ohash_insert(&targets, slot, gn);
                    210:        }
1.1       deraadt   211:
1.42      espie     212:        return gn;
1.1       deraadt   213: }
                    214:
1.83      espie     215: GNode *
                    216: Targ_mk_special_node(const char *name, size_t n, uint32_t hv,
                    217:     unsigned int type, unsigned char special, unsigned int special_op)
                    218: {
                    219:        GNode *gn;
                    220:        unsigned int slot;
                    221:        const char *ename = name + n - 1;
                    222:
                    223:        slot = ohash_lookup_interval(&targets, name, ename, hv);
                    224:
                    225:        assert(ohash_find(&targets, slot) == NULL);
                    226:
                    227:        gn = Targ_mk_node(name, ename, type, special, special_op);
                    228:        ohash_insert(&targets, slot, gn);
                    229:
                    230:        return gn;
                    231: }
                    232:
1.20      espie     233: void
1.46      espie     234: Targ_FindList(Lst nodes, Lst names)
1.1       deraadt   235: {
1.46      espie     236:        LstNode ln;
                    237:        GNode *gn;
1.42      espie     238:        char *name;
                    239:
                    240:        for (ln = Lst_First(names); ln != NULL; ln = Lst_Adv(ln)) {
1.77      espie     241:                name = Lst_Datum(ln);
1.42      espie     242:                gn = Targ_FindNode(name, TARG_CREATE);
                    243:                /* Note: Lst_AtEnd must come before the Lst_Concat so the nodes
                    244:                 * are added to the list in the order in which they were
                    245:                 * encountered in the makefile.  */
                    246:                Lst_AtEnd(nodes, gn);
                    247:                if (gn->type & OP_DOUBLEDEP)
                    248:                        Lst_Concat(nodes, &gn->cohorts);
                    249:        }
1.1       deraadt   250: }
                    251:
1.32      espie     252: bool
1.39      espie     253: Targ_Ignore(GNode *gn)
1.1       deraadt   254: {
1.42      espie     255:        if (ignoreErrors || gn->type & OP_IGNORE)
                    256:                return true;
                    257:        else
                    258:                return false;
1.1       deraadt   259: }
                    260:
1.32      espie     261: bool
1.39      espie     262: Targ_Silent(GNode *gn)
1.1       deraadt   263: {
1.42      espie     264:        if (beSilent || gn->type & OP_SILENT)
                    265:                return true;
                    266:        else
                    267:                return false;
1.1       deraadt   268: }
                    269:
1.32      espie     270: bool
1.39      espie     271: Targ_Precious(GNode *gn)
1.1       deraadt   272: {
1.58      espie     273:        if (allPrecious || (gn->type & (OP_PRECIOUS|OP_DOUBLEDEP|OP_PHONY)))
1.42      espie     274:                return true;
                    275:        else
                    276:                return false;
1.84      espie     277: }
                    278:
                    279: bool
                    280: node_is_real(GNode *gn)
                    281: {
                    282:        return (gn->type & OP_DUMMY) == 0;
1.1       deraadt   283: }
                    284:
1.17      espie     285: void
1.66      espie     286: Targ_PrintCmd(void *p)
1.1       deraadt   287: {
1.76      espie     288:        const struct command *cmd = p;
1.66      espie     289:        printf("\t%s\n", cmd->string);
1.1       deraadt   290: }
                    291:
                    292: void
1.39      espie     293: Targ_PrintType(int type)
1.1       deraadt   294: {
1.42      espie     295:        int    tbit;
1.5       millert   296:
1.46      espie     297: #define PRINTBIT(attr) case CONCAT(OP_,attr): printf("." #attr " "); break
                    298: #define PRINTDBIT(attr) case CONCAT(OP_,attr): if (DEBUG(TARG)) printf("." #attr " "); break
1.42      espie     299:
                    300:        type &= ~OP_OPMASK;
                    301:
                    302:        while (type) {
                    303:                tbit = 1 << (ffs(type) - 1);
                    304:                type &= ~tbit;
                    305:
                    306:                switch (tbit) {
                    307:                PRINTBIT(OPTIONAL);
                    308:                PRINTBIT(USE);
                    309:                PRINTBIT(IGNORE);
                    310:                PRINTBIT(PRECIOUS);
                    311:                PRINTBIT(SILENT);
                    312:                PRINTBIT(MAKE);
                    313:                PRINTBIT(INVISIBLE);
                    314:                PRINTBIT(NOTMAIN);
                    315:                /*XXX: MEMBER is defined, so CONCAT(OP_,MEMBER) gives OP_"%" */
1.45      espie     316:                case OP_MEMBER:
                    317:                        if (DEBUG(TARG))
                    318:                                printf(".MEMBER ");
1.42      espie     319:                        break;
                    320:                PRINTDBIT(ARCHV);
                    321:                }
1.46      espie     322:     }
1.1       deraadt   323: }
1.67      espie     324:
1.54      espie     325: const char *
1.52      espie     326: status_to_string(GNode *gn)
                    327: {
                    328:        switch (gn->built_status) {
1.53      espie     329:        case UNKNOWN:
                    330:                return "unknown";
1.79      espie     331:        case REBUILT:
1.52      espie     332:                return "made";
                    333:        case UPTODATE:
                    334:                return "up-to-date";
                    335:        case ERROR:
                    336:                return "error when made";
                    337:        case ABORTED:
                    338:                return "aborted";
                    339:        default:
                    340:                return "other status";
                    341:        }
1.49      espie     342: }
                    343:
1.57      espie     344: struct ohash *
                    345: targets_hash()
1.49      espie     346: {
1.57      espie     347:        return &targets;
1.65      espie     348: }