mtarget.c

来自「开放源码的编译器open watcom 1.6.0版的源代码」· C语言 代码 · 共 860 行 · 第 1/2 页

C
860
字号
/****************************************************************************
*
*                            Open Watcom Project
*
*    Portions Copyright (c) 1983-2002 Sybase, Inc. All Rights Reserved.
*
*  ========================================================================
*
*    This file contains Original Code and/or Modifications of Original
*    Code as defined in and that are subject to the Sybase Open Watcom
*    Public License version 1.0 (the 'License'). You may not use this file
*    except in compliance with the License. BY USING THIS FILE YOU AGREE TO
*    ALL TERMS AND CONDITIONS OF THE LICENSE. A copy of the License is
*    provided with the Original Code and Modifications, and is also
*    available at www.sybase.com/developer/opensource.
*
*    The Original Code and all software distributed under the License are
*    distributed on an 'AS IS' basis, WITHOUT WARRANTY OF ANY KIND, EITHER
*    EXPRESS OR IMPLIED, AND SYBASE AND ALL CONTRIBUTORS HEREBY DISCLAIM
*    ALL SUCH WARRANTIES, INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF
*    MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE, QUIET ENJOYMENT OR
*    NON-INFRINGEMENT. Please see the License for the specific language
*    governing rights and limitations under the License.
*
*  ========================================================================
*
* Description:  Target management routines.
*
****************************************************************************/


#include <string.h>

#include "massert.h"
#include "mtypes.h"
#include "make.h"
#include "mmemory.h"
#include "mhash.h"
#include "mmisc.h"
#include "mlex.h"
#include "mrcmsg.h"
#include "msg.h"
#include "mtarget.h"


/* just for people to copy in */
const TATTR FalseAttr = { FALSE, FALSE, FALSE, FALSE, FALSE, FALSE, FALSE };

#define HASH_PRIME    211
#define CASESENSITIVE FALSE  // Is Target Name case sensitive

STATIC HASHTAB    *targTab;
STATIC DEPEND     *freeDepends;
STATIC TLIST      *freeTLists;
STATIC CLIST      *freeCLists;
STATIC FLIST      *freeFLists;
STATIC NKLIST     *freeNKLists;
STATIC SLIST      *freeSLists;


FLIST *NewFList( void )
/**********************
 * allocate a FLIST, fill in default values
 */
{
    FLIST   *f;

    if( freeFLists != NULL ) {
        f = freeFLists;
        freeFLists = f->next;
        memset( f, 0, sizeof( *f ) );
        return( f );
    }
    return( (FLIST *)CallocSafe( sizeof( FLIST ) ) );
}


NKLIST *NewNKList( void )
/************************
 * allocate a NKLIST, fill in default values
 */
{
    NKLIST  *nk;

    if( freeNKLists != NULL ) {
        nk = freeNKLists;
        freeNKLists = nk->next;
        memset( nk, 0, sizeof( *nk ) );
        return( nk );
    }
    return( (NKLIST *)CallocSafe( sizeof( NKLIST ) ) );
}


SLIST *NewSList( void )
/**********************
 * allocate a NKLIST, fill in default values
 */
{
    SLIST   *s;

    if( freeSLists != NULL ) {
        s = freeSLists;
        freeSLists = s->next;
        memset( s, 0, sizeof( *s ) );
        return( s );
    }
    return( (SLIST *)CallocSafe( sizeof( SLIST ) ) );
}


TLIST *NewTList( void )
/**********************
 * allocate a TLIST, fill in default values
 */
{
    TLIST   *t;

    if( freeTLists != NULL ) {
        t = freeTLists;
        freeTLists = t->next;
        memset( t, 0, sizeof( *t ) );
        return( t );
    }
    return( (TLIST *)CallocSafe( sizeof( TLIST ) ) );
}


void RenameTarget( TARGET *targ, const char *newname )
/****************************************************/
{
    (void)RemHashNode( targTab, targ->node.name, CASESENSITIVE );
    if( targ->node.name != NULL ) {
        FreeSafe( targ->node.name );
    }
    targ->node.name = FixName( StrDupSafe( newname ) );
    AddHashNode( targTab, (HASHNODE *)targ );
}


TARGET *NewTarget( const char *name )
/************************************
 * allocate a newtarget with name, and default values
 */
{
    TARGET  *new;

    new = CallocSafe( sizeof( *new ) );
    new->executed = TRUE;
    new->date = OLDEST_DATE;
    new->node.name = FixName( StrDupSafe( name ) );
    AddHashNode( targTab, (HASHNODE *)new );

    return( new );
}


TARGET *FindTarget( const char *name )
/*************************************
 * be sure that name has been FixName'd!
 */
{
    assert( name != NULL );

    return( (TARGET *)FindHashNode( targTab, name, CASESENSITIVE ) );
}


#ifdef __WATCOMC__
#pragma on (check_stack);
#endif
CLIST *DotCList( enum DotNames dot )
/***********************************
 * find clist associated with dotname
 */
{
    char                name[MAX_DOT_NAME];
    TARGET const        *cur;

    name[0] = DOT;
    FixName( strcpy( name + 1, DotNames[dot] ) );

    cur = FindTarget( name );

    if( cur == NULL || cur->depend == NULL ) {
        return( NULL );
    }
    return( cur->depend->clist );
}
#ifdef __WATCOMC__
#pragma off(check_stack);
#endif


DEPEND *NewDepend( void )
/***********************/
{
    DEPEND  *dep;

    if( freeDepends != NULL ) {
        dep = freeDepends;
        freeDepends = dep->next;
        memset( dep, 0, sizeof( *dep ) );
        return( dep );
    }
    return( (DEPEND *)CallocSafe( sizeof( DEPEND ) ) );
}


CLIST *NewCList( void )
/*********************/
{
    CLIST   *c;

    if( freeCLists != NULL ) {
        c = freeCLists;
        freeCLists = c->next;
        memset( c, 0, sizeof( *c ) );
        return( c );
    }
    return( (CLIST *)CallocSafe( sizeof( CLIST ) ) );
}


STATIC FLIST *DupFList( const FLIST *old )
/*****************************************
 * Duplicate the inline file information of the CLIST
 */
{
    FLIST   *new;
    FLIST   *cur;
    FLIST   *head;  // resulting FLIST

    if( old == NULL ) {
        return( NULL );
    }

    head = NewFList();
    head->fileName = StrDupSafe( old->fileName );
    if( old->body != NULL ) {
        head->body = StrDupSafe( old->body );
    } else {
        head->body = NULL;
    }
    head->keep     = old->keep;

    cur = head;
    old = old->next;
    while( old != NULL ) {
        new = NewFList();
        new->fileName = StrDupSafe( old->fileName );
        if( old->body != NULL ) {
            new->body = StrDupSafe( old->body );
        } else {
            new->body = NULL;
        }
        new->keep     = old->keep;
        cur->next     = new;

        cur = new;
        old = old->next;
    }

    return( head );
}


STATIC SLIST *DupSList( const SLIST *old )
/****************************************/
{
    SLIST   *new;
    SLIST   *cur;
    SLIST   *head;

    if( old == NULL ) {
        return( NULL );
    }

    head = NewSList();
    head->targ_path = StrDupSafe( old->targ_path );
    head->dep_path  = StrDupSafe( old->dep_path );
    head->clist     = DupCList  ( old->clist );

    cur = head;
    old = old->next;
    while( old != NULL ) {
        new = NewSList();
        new->targ_path  = StrDupSafe( old->targ_path );
        new->dep_path   = StrDupSafe( old->dep_path );
        new->clist      = DupCList  ( old->clist );
        cur->next = new;
        cur = new;
        old = old->next;
    }

    return( head );
}


CLIST *DupCList( const CLIST *old )
/*********************************/
{
    CLIST   *new;
    CLIST   *cur;
    CLIST   *head;

    if( old == NULL ) {
        return( NULL );
    }

    head = NewCList();
    head->text       = StrDupSafe( old->text );
    head->inlineHead = DupFList  ( old->inlineHead );

    cur = head;
    old = old->next;
    while( old != NULL ) {
        new = NewCList();
        new->text       = StrDupSafe( old->text );
        new->inlineHead = DupFList  ( old->inlineHead );
        cur->next = new;

        cur = new;
        old = old->next;
    }

    return( head );
}


TLIST *DupTList( const TLIST * old )
/***********************************
 *  duplicate the tlist
 */
{
    TLIST              *new;
    TLIST              *currentNew;
    TLIST const        *currentOld;

    new = NULL;
    if( old != NULL ) {
        new = NewTList();
        new ->target = old->target;
        currentNew = new;
        currentOld = old->next;
        while( currentOld != NULL ) {
            currentNew->next = NewTList();
            currentNew->next->target = currentOld->target;
            currentNew = currentNew->next;
            currentOld = currentOld->next;

        }
    }
    return( new );
}


DEPEND *DupDepend( const DEPEND *old )
/*************************************
 * doesn't recursively descend old->next, or old->targs->target->depend
 */
{
    DEPEND  *new;

    if( old == NULL ) {
        return( NULL );                 /* no need to dup */
    }

    new = NewDepend();
    new->targs    = DupTList( old->targs );
    new->clist    = DupCList( old->clist );
    new->slist    = DupSList( old->slist );
    new->slistCmd = old->slistCmd;

    return( new );
}


void FreeTList( TLIST *tlist )   /* non-recursive */
/****************************/
{
    TLIST   *cur;

    while( tlist != NULL ) {
        cur = tlist;
        tlist = tlist->next;
        cur->next = freeTLists;
        freeTLists = cur;
    }
}


/* frees the no keep list */
void FreeNKList( NKLIST *nklist )   /* non-recursive */
/*******************************/
{
    NKLIST  *cur;

    while( nklist != NULL ) {
        cur = nklist;
        nklist = nklist->next;
        FreeSafe( cur->fileName );
        cur->next = freeNKLists;
        freeNKLists = cur;
    }
}


/* frees the sufsuf list */
void FreeSList( SLIST *slist )   /* non-recursive */
/****************************/
{
    SLIST   *cur;

    while( slist != NULL ) {
        cur = slist;
        FreeSafe( cur->targ_path );
        FreeSafe( cur->dep_path );
        FreeCList( cur->clist );
        slist = slist->next;
        cur->next = freeSLists;
        freeSLists = cur;
    }
}


void FreeFList( FLIST *flist )   /* non-recursive */
/*****************************
 * frees the inline file information for the clist
 */

⌨️ 快捷键说明

复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?