#*************************************************************************
# Copyright (c) 2002 The University of Chicago, as Operator of Argonne
#     National Laboratory.
# Copyright (c) 2002 The Regents of the University of California, as
#     Operator of Los Alamos National Laboratory.
# EPICS BASE Versions 3.13.7
# and higher are distributed subject to a Software License Agreement found
# in file LICENSE that is included with this distribution. 
#*************************************************************************
#
# $Revision-Id$
#
#  EPICS RULES_ARCH
#       by Matthew Needes and Mike Bordua and Janet Anderson and Jeff Hill
#

all:: install

ACTIONS += inc
ACTIONS += build
ACTIONS += install
ACTIONS += buildInstall 
ACTIONS += depends 
ACTIONS += rebuild 

actionPart = $(word 1, $(subst $(DIVIDER), ,$@))
archPart = $(word 2, $(subst $(DIVIDER), ,$@))

#
# hostActionArchTargets
#
hostArchs = $(HOST_ARCH)
hostActionArchTargets = $(foreach x, $(ACTIONS),\
		$(foreach arch,$(hostArchs), $(x)$(DIVIDER)$(arch)))
ifeq (Makefile.Host, $(wildcard Makefile.Host))
hostDirs = $(addprefix O.,$(hostArchs))
$(hostActionArchTargets) : $(hostDirs)
	$(MAKE) -C O.$(archPart) -f ../Makefile.Host T_A=$(archPart) BUILD_TYPE=Host $(actionPart)
$(hostArchs) : % : O.% 
	$(MAKE) -C O.$@ -f ../Makefile.Host T_A=$@ BUILD_TYPE=Host
else
$(hostActionArchTargets) :
$(hostArchs) :
endif

#
# crossActionArchTargets
#
crossArchs = $(filter-out $(hostArchs),$(BUILD_ARCHS))
crossActionArchTargets = $(foreach x, $(ACTIONS), \
	$(foreach arch, $(CROSS_COMPILER_TARGET_ARCHS), $(x)$(DIVIDER)$(arch)))
ifeq (Makefile.Vx, $(wildcard Makefile.Vx))
crossDirs = $(addprefix O.,$(crossArchs))
$(crossActionArchTargets) : $(crossDirs)
	$(MAKE) -C O.$(archPart) -f ../Makefile.Vx T_A=$(archPart) BUILD_TYPE=Vx $(actionPart)
$(crossArchs) : % : O.%
	$(MAKE) -C O.$@ -f ../Makefile.Vx T_A=$@ BUILD_TYPE=Vx
else
$(crossActionArchTargets) :
$(crossArchs) : 
endif

$(hostDirs) : $(EPICS_BASE_TOOLS)/makeMakefile.pl
	$(PERL) $(EPICS_BASE_TOOLS)/makeMakefile.pl $@ Host

$(crossDirs) : $(EPICS_BASE_TOOLS)/makeMakefile.pl
	$(PERL) $(EPICS_BASE_TOOLS)/makeMakefile.pl $@ Vx

#
# host/cross action targets
#
$(ACTIONS) :: % : %$(DIVIDER)host %$(DIVIDER)cross 
HostActionTargets = $(foreach x, $(ACTIONS) clean, $(x)$(DIVIDER)host)
CrossActionTargets = $(foreach x, $(ACTIONS) clean, $(x)$(DIVIDER)cross)
$(HostActionTargets) : %$(DIVIDER)host : $(addprefix %$(DIVIDER), $(hostArchs))
$(CrossActionTargets) : %$(DIVIDER)cross : $(addprefix %$(DIVIDER), $(crossArchs))


#
# arch targets
#
host : $(hostArchs)
cross : $(crossArchs)

#
# special clean rule
#
clean ::
	$(RMDIR) $(hostDirs) $(crossDirs)
clean$(DIVIDER)% : 
	$(RMDIR) O.$*

.PHONY :: $(HostActionTargets)
.PHONY :: $(CrossActionTargets)
.PHONY :: $(crossActionArchTargets)
.PHONY :: $(hostActionArchTargets)
.PHONY :: $(hostArchs) $(crossArchs)
.PHONY :: $(ACTIONS) clean all host cross

