/*
* Copyright (c) 1988, 1989, 1990, 1993
* The Regents of the University of California. All rights reserved.
*
* This code is derived from software contributed to Berkeley by
* Adam de Boor.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
* 1. Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* 2. Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
* 3. Neither the name of the University nor the names of its contributors
* may be used to endorse or promote products derived from this software
* without specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
* ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
* OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
* OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
* SUCH DAMAGE.
*/
/*
* Copyright (c) 1989 by Berkeley Softworks
* All rights reserved.
*
* This code is derived from software contributed to Berkeley by
* Adam de Boor.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
* 1. Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* 2. Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
* 3. All advertising materials mentioning features or use of this software
* must display the following acknowledgement:
* This product includes software developed by the University of
* California, Berkeley and its contributors.
* 4. Neither the name of the University nor the names of its contributors
* may be used to endorse or promote products derived from this software
* without specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
* ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
* OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
* OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
* SUCH DAMAGE.
*/
/*
* Maintaining the targets and sources, which are both implemented as GNode.
*
* Interface:
* Targ_Init Initialize the module.
*
* Targ_End Clean up the module.
*
* Targ_List Return the list of all targets so far.
*
* GNode_New Create a new GNode with the given name, don't add it
* to allNodes.
*
* Targ_FindNode Find the node, or return NULL.
*
* Targ_GetNode Find the node, or create it.
*
* Targ_NewInternalNode
* Create an internal node.
*
* Targ_FindList Given a list of names, find nodes for all
* of them, creating them as necessary.
*
* Targ_Propagate Propagate information between related nodes.
* Should be called after the makefiles are parsed
* but before any action is taken.
*
* Debugging:
* Targ_PrintGraph
* Print out the entire graph, all variables and
* statistics for the directory cache.
*/
/*
* All target nodes that appeared on the left-hand side of one of the
* dependency operators ':', '::', '!'.
*/
static GNodeList allTargets = LST_INIT;
static HashTable allTargetsByName;
/*
* Return the list of all targets, which are all nodes that appear on the
* left-hand side of a dependency declaration such as "target: source".
* The returned list does not contain pure sources.
*/
GNodeList *
Targ_List(void)
{
return &allTargets;
}
/*
* Create a new graph node, but don't register it anywhere.
*
* Graph nodes that occur on the left-hand side of a dependency line such
* as "target: source" are called targets. XXX: In some cases (like the
* .ALLTARGETS variable), other nodes are called targets as well, even if
* they never occur on the left-hand side of a dependency line.
*
* Typical names for graph nodes are:
* "src.c" an ordinary file
* "clean" a .PHONY target
* ".END" a special hook target
* "-lm" a library
* "libm.a(sin.o)" an archive member
*/
GNode *
GNode_New(const char *name)
{
GNode *gn;
/* Don't free gn->youngestChild since it is not owned by this node. */
/*
* In the following lists, only free the list nodes, but not the
* GNodes in them since these are not owned by this node.
*/
Lst_Done(&gn->implicitParents);
Lst_Done(&gn->parents);
Lst_Done(&gn->children);
Lst_Done(&gn->order_pred);
Lst_Done(&gn->order_succ);
Lst_Done(&gn->cohorts);
HashTable_Done(&gn->vars);
/*
* Do not free the commands themselves, as they may be shared with
* other nodes.
*/
Lst_Done(&gn->commands);
/*
* gn->suffix is not owned by this node.
*
* XXX: gn->suffix should be unreferenced here. This requires a
* thorough check that the reference counting is done correctly in
* all places, otherwise a suffix might be freed too early.
*/
free(gn);
}
#endif
/* Get the existing global node, or return NULL. */
GNode *
Targ_FindNode(const char *name)
{
return HashTable_FindValue(&allTargetsByName, name);
}
/* Get the existing global node, or create it. */
GNode *
Targ_GetNode(const char *name)
{
bool isNew;
HashEntry *he = HashTable_CreateEntry(&allTargetsByName, name, &isNew);
if (!isNew)
return HashEntry_Get(he);
/*
* Create a node, register it in .ALLTARGETS but don't store it in the
* table of global nodes. This means it cannot be found by name.
*
* This is used for internal nodes, such as cohorts or .WAIT nodes.
*/
GNode *
Targ_NewInternalNode(const char *name)
{
GNode *gn = GNode_New(name);
Global_Append(".ALLTARGETS", name);
Lst_Append(&allTargets, gn);
DEBUG1(TARG, "Adding \"%s\" to all targets.\n", gn->name);
if (doing_depend)
gn->flags.fromDepend = true;
return gn;
}
/*
* Return the .END node, which contains the commands to be run when
* everything else has been made.
*/
GNode *
Targ_GetEndNode(void)
{
/*
* Save the node locally to avoid having to search for it all
* the time.
*/
static GNode *endNode = NULL;
/*
* Format a modification time in some reasonable way and return it.
* The formatted time is placed in a static area, so it is overwritten
* with each call.
*/
const char *
Targ_FmtTime(time_t tm)
{
static char buf[128];
Suff_PrintAll();
debug_printf("#*** End input graph for pass %d in %s:\n",
pass, curdir);
}
/*
* Propagate some type information to cohort nodes (those from the '::'
* dependency operator).
*
* Should be called after the makefiles are parsed but before any action is
* taken.
*/
void
Targ_Propagate(void)
{
GNodeListNode *ln, *cln;
for (ln = allTargets.first; ln != NULL; ln = ln->next) {
GNode *gn = ln->datum;
GNodeType type = gn->type;