builtins.c #2

  • //
  • guest/
  • perforce_software/
  • jam/
  • src/
  • builtins.c
  • View
  • Commits
  • Open Download .zip Download (6 KB)
/*
 * Copyright 1993-2002 Christopher Seiwald and Perforce Software, Inc.
 *
 * This file is part of Jam - see jam.c for Copyright information.
 */

# include "jam.h"

# include "lists.h"
# include "parse.h"
# include "builtins.h"
# include "rules.h"
# include "filesys.h"
# include "newstr.h"
# include "regexp.h"

/*
 * builtins.c - builtin jam rules
 *
 * External routines:
 *
 * 	load_builtin() - define builtin rules
 *
 * Internal routines:
 *
 *	builtin_depends() - DEPENDS/INCLUDES rule
 *	builtin_echo() - ECHO rule
 *	builtin_exit() - EXIT rule
 *	builtin_flags() - NOCARE, NOTFILE, TEMPORARY rule
 *	builtin_glob() - GLOB rule
 *	builtin_match() - MATCH rule
 *
 * 01/10/01 (seiwald) - split from compile.c
 */

/*
 * compile_builtin() - define builtin rules
 */

# define P0 (PARSE *)0
# define C0 (char *)0

LIST *builtin_depends( PARSE *parse, LOL *args );
LIST *builtin_echo( PARSE *parse, LOL *args );
LIST *builtin_exit( PARSE *parse, LOL *args );
LIST *builtin_flags( PARSE *parse, LOL *args );
LIST *builtin_glob( PARSE *parse, LOL *args );
LIST *builtin_match( PARSE *parse, LOL *args );

int glob( char *s, char *c );

void
load_builtins()
{
    bindrule( "Always" )->procedure = 
    bindrule( "ALWAYS" )->procedure = 
	parse_make( builtin_flags, P0, P0, P0, C0, C0, T_FLAG_TOUCHED );

    bindrule( "Depends" )->procedure = 
    bindrule( "DEPENDS" )->procedure = 
	parse_make( builtin_depends, P0, P0, P0, C0, C0, T_DEPS_DEPENDS );

    bindrule( "echo" )->procedure = 
    bindrule( "Echo" )->procedure = 
    bindrule( "ECHO" )->procedure = 
	parse_make( builtin_echo, P0, P0, P0, C0, C0, 0 );

    bindrule( "exit" )->procedure = 
    bindrule( "Exit" )->procedure = 
    bindrule( "EXIT" )->procedure = 
	parse_make( builtin_exit, P0, P0, P0, C0, C0, 0 );

    bindrule( "Glob" )->procedure = 
    bindrule( "GLOB" )->procedure = 
	parse_make( builtin_glob, P0, P0, P0, C0, C0, 0 );

    bindrule( "Includes" )->procedure = 
    bindrule( "INCLUDES" )->procedure = 
	parse_make( builtin_depends, P0, P0, P0, C0, C0, T_DEPS_INCLUDES );

    bindrule( "Leaves" )->procedure = 
    bindrule( "LEAVES" )->procedure = 
	parse_make( builtin_flags, P0, P0, P0, C0, C0, T_FLAG_LEAVES );

    bindrule( "Match" )->procedure = 
    bindrule( "MATCH" )->procedure = 
	parse_make( builtin_match, P0, P0, P0, C0, C0, 0 );

    bindrule( "NoCare" )->procedure = 
    bindrule( "NOCARE" )->procedure = 
	parse_make( builtin_flags, P0, P0, P0, C0, C0, T_FLAG_NOCARE );

    bindrule( "NOTIME" )->procedure = 
    bindrule( "NotFile" )->procedure = 
    bindrule( "NOTFILE" )->procedure = 
	parse_make( builtin_flags, P0, P0, P0, C0, C0, T_FLAG_NOTFILE );

    bindrule( "NoUpdate" )->procedure = 
    bindrule( "NOUPDATE" )->procedure = 
	parse_make( builtin_flags, P0, P0, P0, C0, C0, T_FLAG_NOUPDATE );

    bindrule( "Temporary" )->procedure = 
    bindrule( "TEMPORARY" )->procedure = 
	parse_make( builtin_flags, P0, P0, P0, C0, C0, T_FLAG_TEMP );
}

/*
 * builtin_depends() - DEPENDS/INCLUDES rule
 *
 * The DEPENDS builtin rule appends each of the listed sources on the 
 * dependency list of each of the listed targets.  It binds both the 
 * targets and sources as TARGETs.
 */

LIST *
builtin_depends(
	PARSE	*parse,
	LOL	*args )
{
	LIST *targets = lol_get( args, 0 );
	LIST *sources = lol_get( args, 1 );
	int which = parse->num;
	LIST *l;

	for( l = targets; l; l = list_next( l ) )
	{
	    TARGET *t = bindtarget( l->string );
	    t->deps[ which ] = targetlist( t->deps[ which ], sources );
	}

	return L0;
}

/*
 * builtin_echo() - ECHO rule
 *
 * The ECHO builtin rule echoes the targets to the user.  No other 
 * actions are taken.
 */

LIST *
builtin_echo(
	PARSE	*parse,
	LOL	*args )
{
	list_print( lol_get( args, 0 ) );
	printf( "\n" );
	return L0;
}

/*
 * builtin_exit() - EXIT rule
 *
 * The EXIT builtin rule echoes the targets to the user and exits
 * the program with a failure status.
 */

LIST *
builtin_exit(
	PARSE	*parse,
	LOL	*args )
{
	list_print( lol_get( args, 0 ) );
	printf( "\n" );
	exit( EXITBAD ); /* yeech */
	return L0;
}

/*
 * builtin_flags() - NOCARE, NOTFILE, TEMPORARY rule
 *
 * Builtin_flags() marks the target with the appropriate flag, for use
 * by make0().  It binds each target as a TARGET.
 */

LIST *
builtin_flags(
	PARSE	*parse,
	LOL	*args )
{
	LIST *l = lol_get( args, 0 );

	for( ; l; l = list_next( l ) )
	    bindtarget( l->string )->flags |= parse->num;

	return L0;
}

/*
 * builtin_globbing() - GLOB rule
 */

struct globbing {
	LIST	*patterns;
	LIST	*results;
} ;

static void
builtin_glob_back(
	void	*closure,
	char	*file,
	int	status,
	time_t	time )
{
	struct globbing *globbing = (struct globbing *)closure;
	LIST		*l;

	for( l = globbing->patterns; l; l = l->next )
	    if( !glob( l->string, file ) )
	{
	    globbing->results = list_new( globbing->results, newstr( file ) );
	    break;
	}
}

LIST *
builtin_glob(
	PARSE	*parse,
	LOL	*args )
{
	LIST *l = lol_get( args, 0 );
	LIST *r = lol_get( args, 1 );

	struct globbing globbing;

	globbing.results = L0;
	globbing.patterns = r;

	for( ; l; l = list_next( l ) )
	    file_dirscan( l->string, builtin_glob_back, &globbing );

	return globbing.results;
}

/*
 * builtin_match() - MATCH rule, regexp matching
 */

LIST *
builtin_match(
	PARSE	*parse,
	LOL	*args )
{
	LIST *l = lol_get( args, 0 );
	LIST *r = lol_get( args, 1 );
	LIST *result = 0;
	regexp *re;

	/* No pattern or string?  No results. */

	if( !l || !r )
	    return L0;

	/* Just use first arg of each list. */

	re = regcomp( l->string );

	if( regexec( re, r->string ) )
	{
	    int i, top;

	    /* Find highest parameter */

	    for( top = NSUBEXP; top-- > 1; )
		if( re->startp[top] != re->endp[top] )
		    break;

	    /* And add all parameters up to highest onto list. */
	    /* Must have parameters to have results! */

	    for( i = 1; i <= top; i++ )
	    {
		char buf[ MAXSYM ];
		int l = re->endp[i] - re->startp[i];
		memcpy( buf, re->startp[i], l );
		buf[ l ] = 0;
		result = list_new( result, newstr( buf ) );
	    }
	}

	free( (char *)re );

	return result;
}
# Change User Description Committed
#11 2614 rmg Fix to includes of includes not being considered, broken by 2499.

Details taken from email to jamming:

       The challenge is to make included includes appear as direct
       includes, so that they get considered.  This used to work by
       recursion, because each TARGET node computed the summary of its
       includes -- the hfate and htime -- along with the summary of
       its dependents -- the fate and time.  Alas, that previous
       arrangement confused make1() into treating headers as direct
       dependencies during the build phase.

               A.o
               |
               A.c -- A.h

               (Read depends down, includes across)
               (Failed build of A.h aborts build of A.c)

       The fix to the confused make1() problem was to consolidate the
       special handling of includes by having make0() tack onto a
       target's list of dependendies any of the target's dependents'
       includes.  Unfortunately, this fix did not recurse: if the
       target's dependents' includes included other files, those files
       were not added to the target's dependencies.

               A.o
               |
               A.c -- A.h -- B.h -- C.h

               is rewritten to:

               A.o
               |  \
               A.c A.h -- B.h -- C.h

               (A.o depends on A.h, but not B.h or C.h.  This is
               the current, broken state of jam 2.5rc1.)

       Matt's bugfix added some recursion at this point, by transitively
       appending includes' includes onto the includes chain.  But, as
       he found out (and I did before), this can slow make0() down
       considerably, as typically header files all include each other
       and you wind up with lots of really long chains.

               A.o
               |
               A.c -- A.h -- B.h -- C.h

               is rewritten to:

               A.o
               |
               A.c -- A.h -- B.h -- C.h
                    - B.h  - C.h
                    - C.h

               (Matt's fix: if the .h files include each other, the
                includes chains get very long.)

       The final(?) fix I have is relatively simple, but is an extra
       step:  to have make0() replace a target's includes chain with
       a single pseudo-target whose dependencies are the original
       target's includes.  That pseudo-target gets passed to make0(),
       which then recursively consolidates its fate and time.  This
       then makes a target's includes fate and time available in a
       single target hanging off the original target.

               A.o
               |
               A.c -- A.h -- B.h -- C.h

               is rewritten to:

               A.o
               |   \
               A.c  A.c-includes
                    |    \
                    A.h  A.h-includes
                         |    \
                         B.h  B.h-includes
                              |
                              C.h

               (New pseudo-target xxx-includes recursively consolidates
               fate and time of all included targets.)

       While this new scheme does add a node for every include file,
       it is linear, rather than exponential, and the time is pretty
       much neglible.

User-visible bugfix not documented, because there is no place
in RELNOTES for release-candidate fixes.

Bumped patchlevel to 2.5rc2.
#10 2499 rmg Fix 'includes' support so that included files aren't treated as
direct dependencies during the command execution phase.  If an
included file failed to build, make1() would bypass the including
file.

Now make0() appends each child's 'includes' onto its own 'depends'
list, eliminating 'includes'-specific code in make0() and make1().
This not only fixes the bug, but removes some complexity as well.

Bug fix documented in RELNOTES.

=== computer:1666: Change 38399 by seiwald@play-seiwald on 2002/12/03 16:00:40
#9 2493 rmg Rewrite the past: update all jam's source with comments to
reflect changes since about 2.3, very early 2001.

Whitespace only change.

=== computer:1666: Change 37660 by seiwald@play-seiwald on 2002/11/06 22:41:35

Note: I regenerated jamgram.c on my linux 7.3 system prior to
the submit, since patch was so unhappy trying to lay down the
changes from Christopher's change. Presumably this is just due to
different yacc/bison/whatever particulars on the system where
Christopher made the changes originally. - rmg
#8 2491 rmg Some consting in jam to make it more compilable by C++ compilers.

No functional change.

=== computer:1666: Change 37433 by perforce@perforce on 2002/10/30 16:08:51

Recreational const-ing of jam, for compilers that don't allow
"string" to be passed as a non-const char *.

This included a few places where we were modifying what could
possibly have been read-only storage, oddly enough.

No functional change.

=== computer:1666: Change 37602 by seiwald@play-seiwald on 2002/11/04 17:25:40
#7 2490 rmg Jam langauge work: make 'return' actually return from the rule,
rather than just setting the return value.  Introduce new
break/continue statements for managing loops.

User visible change to be documented in Jam.html.

=== computer:1666: Change 37200 by seiwald@play-seiwald on 2002/10/22 15:41:28

Gross rework of Jam.html documentation, including:

- the description of parameters for rules
- description of -g flag
- a new description of targets
- more about rules and their return values
- better separation of rules and updating actions
- putting borders around the tables

(Undocumented) change to documentation.

=== computer:1666: Change 37551 by seiwald@waffle-cyg-seiwald on 2002/11/03 23:17:12

Document jam's new and working break/continue/return statements.

=== computer:1666: Change 37574 by seiwald@play-seiwald on 2002/11/04 13:13:01
#6 2489 rmg Jam tinkering: since all calls to list_new() must either newstr()
or copystr() the added string, instead just pass a flag and let
list_new() do the newstr/copystr.

No functional change.

=== computer:1666: Change 37164 by seiwald@spice on 2002/10/22 01:21:58
#5 1613 Perforce staff Make MATCH generate empty strings for () subexpressions that
match nothing, rather than generating nothing at all.  The logic
that finds the count of () subexpressions was checking the
length of the match, rather than the presence of the match.

Thanks to David Abrahams.
#4 1612 Perforce staff GLOB now applies the pattern to the directory-less filename,
rather than the whole path.  The directory is part of the
arguments to GLOB, so there's no reason to require it to be
part of the pattern.  This makes

    echo [ Glob .  : jam*.c ] ;

match and output

    ./jam.c ./jambase.c ./jamgram.c

instead of nothing at all. 

Thanks to Niklaus Giger.
#3 1601 Perforce staff Fix lame Match rule to do productized results, rather than
using just $(1[1]) as pattern and $(2[1]) as the string.  Now
each pattern is applied to each list element, and the results
are concatenated and returned.

To be documented in Jam.html.
#2 1498 Perforce staff Support for (primitive) regexp "Match" command, which
takes a regexp and a string and returns a list of the
(matched) parameters.
#1 1319 rmg Jam 2.3 + Perforce's internal changes.

This change is a drop of the Perforce internal Jam changes
since the 2.3 public release. The individual changes
represented herein are preserved in the
//guest/richard_geiger/intjam/ branch.

The intent of this drop is to provide a base, from which other
contributors' Jam branches may be integrated into. It is not
intended to become a packaged release in this state. We will
be integrating changes from other users prior to creating the
next packaged release.

Please refer to the src/RELNOTES file for an overview of the
changes present in this integration.

  - Richard Geiger
  Open Source Engineer at Perforce
//guest/richard_geiger/intjam/src/builtins.c
#5 1317 Richard Geiger Update the copyright notices in all files touched in the upcoming
drop into //public/jam/
#4 1305 Richard Geiger Port new jam to run with just cxx compiler, 'cause we're
too cheap to buy the C compiler.

=== computer.perforce.com:1666: Change 21156 by PERFORCE@vulgar on 2001/03/12 16:17:08

This is a VMS portability change, but does touch code compiled on
other platforms.

Added to RELNOTES - rmg
#3 1244 Richard Geiger New 'Glob' builtin that returns a list of files in a list of
directories, given a list of patterns.

=== computer.perforce.com:1666: Change 19941 by seiwald@spice on 2001/01/08 23:32:00

Also, document the existence of rule values, the [ rule args ... ]
syntax for accessing them, and the 'return' statement.
#2 1242 Richard Geiger Jam aliases 'echo' and 'exit' for 'Echo' and 'Exit', since
they seem more part of the language than the other built-in
rules.

=== computer.perforce.com:1666: Change 19940 by seiwald@spice2 on 2001/01/08 23:29:35

Added a note about this to Jam.html - rmg
#1 1241 Richard Geiger Split jam's built-in rules out to builtins.c from compile.c,
so that compile.c only deals with the language.

=== computer.perforce.com:1666: Change 19939 by seiwald@spice2 on 2001/01/08 22:55:10