#
# Alterations to remove linking across archives:
# Alterations Author: J. Curry
TESTDIR = TEST
COPYARCHS = jcopy $(TESTDIR)
NONARCEND = `find $(TESTDIR) -name "*.o"` \
       $(LOCALLIBS) $(SHAREDSTATICLIBS) $(OTHERLIBS) $(SHAREDLIBSTUBS) \
       $(SYSLIBSTUBS) > $(MAPFILE) 
#
# Pathname MACROs
#
RCSDIR = ./RCS
#INCLUDES = $(ODE_ROOT)/inc
#INCLUDES = 

#
# File MACROs
#
MAPFILE = $(PFX)$(PRODUCT)_MAP
#SYSLIBSTUBS = $(ODE_ROOT)/lib/dl_stubs64.a
SYSLIBSTUBS = 
TMCRT0 = $(ODE_ROOT)/lib/tm_crt064.o
MAKEFILE = Makefile
OFILES = $(SFILES:.s=.o) $(CFILES:.c=.o)
SRCS = $(MAKEFILE) $(HFILES) $(CFILES) $(SFILES) $(LNKCMDFILE)

#
# Tool MACROs 
#
GCC = /usr/cygnus/hppa-990918/H-hppa2.0w-hp-hpux11.00/bin/gcc
CC = /usr/bin/cc
LD = /usr/bin/ld
MK = /bin/make
CO = /usr/bin/co
RM = /bin/rm 
AR = /bin/ar
#FPTR_CHECK = /source/ODE/tools/fptr_check
SHELL = /bin/ksh
ENV =                # makes sure ksh is non-interactive


#
# Flags MACROs                                
#
ANSI = -Aa
CFLAGS = $(ANSI) +ESdev +ESrel +DCemb +DA2.0W $(OTHERCFLAGS) $(DEF) -c $(INCLUDEPATHS)
#GCCFLAGS = $(OTHERCFLAGS) $(DEF) -c $(INCLUDEPATHS)
GCCFLAGS = $(OTHERCFLAGS) $(DEF) -c $(INCLUDEPATHS) -O1
LDFLAGS = +nodefaultmap -e \$$START\$$ -k $(LNKCMDFILE) -m -a archive -o
COFLAGS = -r$(LABEL)
ARFLAGS = ru

#
# Other Misc MACROs
#
LDCMDS = $(LDFLAGS) $(PFX)$(PRODUCT) $(TMCRT0) 
COCMD = $(CO) $(COFLAGS)
MAKEARGS = "ODE_ROOT=$(ODE_ROOT)" "COCMD=$(COCMD)" "DEF=$(DEF)" "PFX=$(PFX)" \
           "TMCRT0=$(TMCRT0)" "SYSLIBSTUBS=$(SYSLIBSTUBS)"

RCSDEP  = $$(@D)/RCS/$$(@F),v

ARC = $(AR) $(ARFLAGS) $(PFX)$(PRODUCT)
BEGIN  = $(LD) $(LDCMDS) 
END    = $$(echo $$(cat -s libfiles)) \
       $(LOCALLIBS) $(SHAREDSTATICLIBS) $(OTHERLIBS) $(SHAREDLIBSTUBS) \
       $(SYSLIBSTUBS) > $(MAPFILE) 
#BUG_CHECK = $(FPTR_CHECK) $(PRODUCT)
BUG_CHECK = echo
NO_CHECK = echo

#############################################################################
#
#                                Build Rules                                 
#
#############################################################################

.SUFFIXES: .o .c .s

.DEFAULT:


#
# This rule makes ensures sources are checked out and then recursively       
# calls 'make.'  The recursive call ensures that the  most current 'Makefile'
# is used. It also enables macros to be defined dynamically based on the
# the product being built      
#
# The first part of this rule builds a list of include directories. This list
# is used for the 'VPATH' macro definition, which tells 'make' where to look
# for source files.
#
# The rule then builds a list of prefixed o-files.  If 'PFX' is null, then
# this list is no different from 'OFILES.' However, if the user specifies
# a prefix for 'PFX,' this created o-file list is used for linking and
# archiving.  Note, the dummy file 'ofile.' This is necessary so that the
# script won't crash when 'OFILES' is null (e.g. no c-files or s-files).
#
# The last part of the rule determines whether we are building an archive
# library, an actual SOM, or no product.  It then does a recursive call to
# 'make' with the appropiate macro definitions.

default1: $(MAKEFILE)
	@(PROD=$(PFX)$(PRODUCT); \
       	for i in -I $(INCLUDEPATHS); do \
          if [[ $${i} != "-I" ]]; then \
            INPATH="$${INPATH}:$${i##*-I}"; \
          fi; \
        done; \
	INPATH="$${INPATH}:$(INCLUDES)"; \
	if [ $(PFX) ] || [ $(FRC) ]; then \
	  FORCE=force; \
	fi; \
	if [ $(PRODUCT) ]; then \
	  for i in ofile $(OFILES); do \
            if [[ $${i} != "ofile" ]]; then \
               POFILES="$${POFILES} $(PFX)$${i}"; \
            fi; \
          done; \
	  if [[ $${PROD##*.} != "a" ]]; then \
	     rm -f libfiles; \
             $(MAKE) default4 $(MAKEARGS) "LIBFILE=$${PWD}/libfile" \
	     "FRC=$${FORCE}" "MID=$${POFILES}" "VPATH=$${INPATH}" \
	     "CHECK=$(BUG_CHECK)"; \
	  else \
	     $(MAKE) default2 $(MAKEARGS) "BEGIN=$(ARC)" "MID=$${POFILES}" \
             "END=" "LIBFILE=$(LIBFILE)" "FRC=$${FORCE}" "VPATH=$${INPATH}" \
	     "CHECK=$(NO_CHECK)";\
	  fi; \
	else \
	  $(MAKE) default3; \
	fi)

#
# This rule is the target of the first recursive call and launches the        
# the product build.  After the product build is complete, the full pathname  
# of the product is appended to a file in the directory where the final link  
# will occur                                                                  
#
# To support a distributed or hierarchical directory structure, a file is
# needed to keep track of the location of all built archive libraries.  The
# path to this file is passed down to all prerequisite builds.

default2: appendLibFiles $(PFX)$(PRODUCT)
	@(if [ $(PRODUCT) ] ; then \
	     echo \`$(PFX)$(PRODUCT)\' Build Complete; \
        fi; \
	rm -f s)

appendLibFiles:
	@(echo $${PWD}/$(PFX)$(PRODUCT) | cat >> $(LIBFILE)s) 


default4: $(PFX)$(PRODUCT)
	@(if [ $(PRODUCT) ] ; then \
	     echo \`$(PFX)$(PRODUCT)\' Build Complete; \
        fi; \
	rm -f s)

#
# This rule just makes sure that all sources are checkedout out            
#

default3: $(SUBLIBS) $(SRCS)

#
# This rule indicates the dependencies on the product build.                  
# 'BEGIN,' 'MID,' and 'END' combine to form either a link command-line or
# an archive command-line depending on our current product
#

product $(PFX)$(PRODUCT): $(INC) $(SUBLIBS) $(OFILES) $(TMCRT0) \
                          $(SYSLIBSTUBS) $(SHAREDLIBSTUBS) $(LOCALLIBS) \
			  $(SHAREDSTATICLIBS) $(LNKCMDFILE) archivecopy
	if [[ $(PRODUCT) = "RAGE" ]] ; then \
	$(BEGIN) $(MID) $(NONARCEND) ; \
	else \
	$(BEGIN) $(MID) $(END) ; \
	fi;
	@(touch -c $@)
	@(if [[ $$(uname -n) != "hpdst41" ]]; then \
		$(CHECK); \
	fi) 

#
archivecopy: 
	@ (if [[ $(PRODUCT) = "RAGE" ]] ; then \
	   echo "Copying archives for extraction!"; \
	   $(COPYARCHS); \
	   fi)
#
# This rule builds all the sublibs and ensures that any include directories   
# are up to date. Note that the targets depend on the empty prerequisite
# 'force.'  This is required so that the commands will always get executed. 
# The first command ensures the target directory has a 'Makefile'.  If not, 
# it checks one out.  The next command then transfers control to the 
# 'Makefile' in the subdirectory.                                              
#

sublibsAndIncs $(INC) $(SUBLIBS): force	
	@(cd $(@D); \
	if [[ ! -r $(MAKEFILE) ]]; then \
	$(CO) $(MAKEFILE); \
	fi;\
	$(MAKE) $(MAKEARGS) "LABEL=$(LABEL)" "FRC=$(FRC)" \
	        "LIBFILE=$(LIBFILE)" "PFX=$(PFX)")


#
# This rule makes sure all srcs are up to date with respect to RCS.           
# Checking out of up-to-date sources can be nullified by using the            
# 'noCheckOut' rule                                                           
#

$(SRCS) $(LOCALLIBS): $(RCSDEP)
	@(if [[ -r $(@D)/RCS/$(@F),v ]]; then cd $(@D); $(COCMD) $@; fi)

#
# This rule allows one to force a build of the object files
#

ofiles $(OFILES): $(FRC)

#
# Suffix rules for .o, .c and .s files                                        
#

.c.o: 
#	$(CC) $(CFLAGS) -I $(INCLUDES) -o $(PFX)$*.o $<
	$(GCC) $(GCCFLAGS) -c $<
.s.o: 
	$(CC) $(CFLAGS) -I $(INCLUDES) -o $(PFX)$*.o $<


#
# dummy target--used as a dependency to force target commands to execute    
#
force:

############################################################################
#
#                             Utility Rules                                   
#
############################################################################

#
# Removes all ofiles with prefix 'PFX' in current directory 
#
clean:
	@(for i in ofile $(OFILES); do \
           if [[ $${i} != "ofile" ]]; then \
            echo Removing $(PFX)$${i}; \
            $(RM) -f $(PFX)$${i}; \
           fi; \
         done)


#
# Performs a 'clean' and removes the 'product' from the current directory   
#
clobber: clean
	@echo Removing $(PFX)$(PRODUCT)
	@$(RM) -f $(PFX)$(PRODUCT) 
	@echo Removing $(MAPFILE)
	@$(RM) -f $(MAPFILE)

#
# Remove all tmp files in current directory                                   
#
cleanTmp:
	$(RM) -f $(TMPFILES)

#
# Echos sources                                                               
#
echo:
	@echo $(SRCS) 

#
# Echos all sources                                                           
#
echoAll:
	@echo $$($(MAKE) echo $(SUBLIBS) $(INC) "MAKEARGS = $@" \
	"ODE_ROOT=$(ODE_ROOT)")

#
# Echos sources with absolute path names                                      
#
echoPath:
	@echo $$(for src in $(SRCS); do echo $${PWD}/$${src}; done)

#
# Echos all sources with absolute path names                                  
#
echoPathAll:
	@echo $$($(MAKE) echoPath $(SUBLIBS) $(INC) "MAKEARGS = $@" \
	"ODE_ROOT=$(ODE_ROOT)")

#
# Checks out sources in current directory                                     
#
checkOut: 
	@$(MAKE) checkOutHelper "RCSDEP = force"

checkOutHelper: $(SRCS) $(LOCALLIBS)

#
# Performs a 'make' but doesn't check out current  sources from RCS           
#
noCheckOut:
	@$(MAKE) "COCMD=echo \* Not Checking Out " "DEF=$(DEF)" "PFX=$(PFX)"\
        "ODE_ROOT=$(ODE_ROOT)"

#
# Checks out sources in current directory and all subdirectories              
#
checkOutAll:
	@$(MAKE) checkOut $(SUBLIBS) $(INC) "MAKEARGS = $@" "LABEL=$(LABEL)"\
        "ODE_ROOT=$(ODE_ROOT)"

#
# Labels sources in current directory with 'LABEL'
#
label:
	rcs -N$(LABEL): $(SRCS) $(LOCALLIBS)

#
# Label all sources in current directory and subdirectories                   
#
labelAll:
	@$(MAKE) label $(SUBLIBS) $(INC) "MAKEARGS = $@" "LABEL=$(LABEL)"\
        "ODE_ROOT=$(ODE_ROOT)"

#
# Performs a 'clobber' in current directory and all subdirectories            
#
nuke : 
	@$(MAKE) clobber $(SUBLIBS) $(INC) "MAKEARGS = $@" "PFX=$(PFX)"\
        "ODE_ROOT=$(ODE_ROOT)"

#
# Performs a 'cleanTmp' in current directory and all subdirectories           
#
nukeTmp: 
	@$(MAKE) cleanTmp $(SUBLIBS) $(INC) "MAKEARGS = $@" "PFX=$(PFX)"\
        "ODE_ROOT=$(ODE_ROOT)"

# Performs a make without checking for any dependencies. Recommended after
# a "make checkOutAll" is done.
nodep:
	@$(MAKE) -e RCSDEP=
