# $Source: /usr/local/kcs/sys.DAVIS_800/bld_tools/RCS/Makefile,v $
# $Revision: 1.5.98.1 $	$Author: root $
# $State: Exp $   	$Locker:  $
# $Date: 95/07/20 10:20:11 $

# Kernel Build Tools Makefile

all: qdep mpp betools scc

##################################################################
# Common 

# For use over NFS, make executables generic (archived, PA1.0)
CFLAGS= +DA1.0
LDFLAGS= -s -Wl,-a,archive

.SUFFIXES: .o .c .l

WARN=@[ -f $@ ] && echo \
  "WARNING: $@ tool changed!  You may need to re-build." || exit 0

##################################################################
# Quick Dependencies Tool
#
# Creates makefile-syntax dependency lists based on #include directives
#

qdep: qdep.c
	$(WARN)
	rm -f $@ 
	${CC} ${CFLAGS} qdep.c -o qdep ${LDFLAGS}


##################################################################
# SCSI Scripts Compiler
#
# Creates header file (.h) from scsi script file (.ss)
#

scc_obj= scc_src/main.o scc_src/scsi_lex.o scc_src/ytab.o scc_src/error.o

# link rule
scc: $(scc_obj)
	$(WARN)
	rm -f $@ 
	${CC} ${CFLAGS} -o $@ $(scc_obj) ${LDFLAGS}

# include dependencies
scc_src/error.o: scc_src/scsi.h scc_src/portable.h scc_src/error.h
scc_src/main.o: scc_src/pass2.h scc_src/scsi.h scc_src/ytab.h \
	scc_src/portable.h scc_src/error.h
scc_src/scsi_lex.o: scc_src/keywords.h scc_src/scsi.h scc_src/ytab.h \
	scc_src/portable.h scc_src/error.h
scc_src/ytab.o: scc_src/scsi.h scc_src/portable.h scc_src/error.h

# objects rule
$(scc_obj): $$(@:.o=.c)
	rm -f $@ 
	${CC} ${CFLAGS} -I scc_src -c -o $@ $(@:.o=.c)

##################################################################
# Make Pre-Processor
#
# Filters template file(s), expanding macros based on given macro definitions.
# Normally used to create a makefile from a generic template and data files
# listing specific files and libraries.
#

mpp: mpp.o mpp_parse.o
	$(WARN)
	rm -f $@ 
	${CC} ${CFLAGS} mpp.o mpp_parse.o -o $@ -ll

mpp.o mpp_parse.o: $$(@:.o=.c)
	rm -f $@ 
	${CC} ${CFLAGS} -c $(@:.o=.c)

mpp_parse.c: mpp_parse.l
	rm -f $@ 
	$(LEX) $(LFLAGS) mpp_parse.l
	mv lex.yy.c $@

mpp_parse.o mpp.o: mpp.h

##################################################################
# Build Environment
#
# Creates local wrapper functions for compilers and related tools to
# use non-default paths (unless using chroot).
#

# Default Pseudo-Root
BEROOT=

BECMDS= be_ar be_cc config.internl be_cpp be_as be_ld be_fdp be_m4 be_lint

betools: $(BECMDS)
	rm -f $@
	touch $@


# root_check target must always execute, but we don't want it 
# to always trigger re-build of bescript, hence we use :: syntax
# Anything depending on bescript must also depend on root_check in 
# this way.

$(BECMDS):: root_check
$(BECMDS):: bescript
	$(WARN)
	rm -f $@ 
	cp bescript $@
	chmod a+x $@

bescript: be_env be_root
	rm -f $@ current_root
	echo "$(BEROOT)" >> current_root
	@if [ -d "$(BEROOT)" ] ; then \
	  echo "\nUsing Build Environment = $(BEROOT)\n" ; \
	  echo "#! /bin/ksh" > $@ ; \
	  echo "ROOT=$(BEROOT)" >> $@ ; \
	  cat be_env >> $@ ; \
	else \
	  echo "\nUsing Build Environment = /\n" ; \
	  echo "#! /usr/bin/ksh" > $@ ; \
	  cat be_root >> $@ ; \
	fi


# Check to see if bescript is out of date w.r.t. definition of BEROOT

root_check:
	@if [ -f current_root ] ; then \
		CURR=`cat current_root` ; \
	    	if [ "$$CURR" != "$(BEROOT)" ] ; then \
			rm -f bescript ; \
		fi ; \
	 else \
		rm -f bescript ; \
	 fi 

	

##################################################################
# Clean-Up Stuff

clean:
	rm -f *.o mpp_parse.c bescript current_root scc_src/*.o

clobber: clean
	rm -f bconfig qdep mpp scc $(BECMDS)

