/*	builtin.c
 *	(c) Copyright 1991 by Ben Eng, All Rights Reserved
 *
 */

#include <fcntl.h>
#include <clib/exec_protos.h>

#include "make.h"
#include "depend.h"

extern void NewList( struct List *);

/*	creates a new BUILTIN rule (of any type)
 *	accepts optional command lines as arguments, ending in a NULL
 *	returns the first target created
 */
static struct target *
new_targetline( char *tline, ... )
{
	va_list argptr;
	struct List cmdList;
	struct target *targ;
	struct command *cmd;
	char *nextcmd;

	NewList( &cmdList );
	va_start( argptr, tline );
	while( nextcmd = va_arg( argptr, char *)) {
		cmd = new_command( nextcmd );
		if( !cmd ) break;
		AddTail( &cmdList, &cmd->node );
	}

	targ = process_targetline( tline, &cmdList );
	if( !targ ) delete_commandlist( &cmdList );
	va_end( argptr );
	return( targ );
}

int
input_builtins( void )
{
	static char *name_list[] = {
		"builtins.make",
		"S:builtins.make",
		NULL
	};
	char *filename;
	int i = 0;

	while( filename = name_list[ i++ ] ) {
		if( !access( filename, 0 )) { /* file exists */
			if( input_makefile( filename )) return( 1 );
		}
	}
	return( 0 );
}

int
init_builtins( void )
{
	struct target *targ;

	if( Param.no_builtins ) return( 0 );

	set_macro( "CC", "dcc" );
	set_macro( "LN", "dcc" );
	set_macro( "CFLAGS", "-proto" );
	set_macro( "AS", "dcc" );
	set_macro( "CI", "ci" ); /* RCS */
	set_macro( "CO", "co" ); /* RCS */

	targ = new_targetline( "%,v:", NULL ); /* no commands for RCS files */

	targ = new_targetline( "%.a: RCS/%.a,v", "$(CO) -u $@", NULL );
	targ = new_targetline( "%.a:", NULL ); /* no commands for a files */

	targ = new_targetline( "%.c: RCS/%.c,v", "$(CO) -u $@", NULL );
	targ = new_targetline( "%.c:", NULL ); /* no commands for c files */

	targ = new_targetline( "%.h: RCS/%.h,v", "$(CO) -u $@", NULL );
	targ = new_targetline( "%.h:", NULL ); /* no commands for c headers */

	targ = new_targetline( "%.i: RCS/%.i,v", "$(CO) -u $@", NULL );
	targ = new_targetline( "%.i:", NULL ); /* no commands for asm headers */

	targ = new_targetline( "%.o: %.c", "$(CC) -c $(CFLAGS) -o $@ $<", NULL );
	targ = new_targetline( "%.o: %.a", "$(AS) -c $(AFLAGS) -o $@ $<", NULL );

	if( input_builtins()) return( 1 );

	return( 0 );
}

