#*************************************************************************
# 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.
#*************************************************************************
#
#  RULES_TARGET
#
#  This file is to be maintained by the community.
#
#-----------------------------------------------------------------------

define TARGET_template
$(1)_$(2) += $$(if $$(strip $$($(1)_$(2)_$$(OS_CLASS))), \
             $$(subst -nil-,,$$($(1)_$(2)_$$(OS_CLASS))), \
             $$($(1)_$(2)_DEFAULT))
endef

$(foreach type, SRCS RCS OBJS LDFLAGS OBJLIBS LDOBJS SYS_LIBS , \
$(foreach target, $(PROD) $(TESTPROD) $(LIBRARY) $(LOADABLE_LIBRARY) , \
    $(eval $(call TARGET_template,$(strip $(target)),$(type)))))

#-----------------------------------------------------------------------

# This define block requires GNU make 3.81
define PROD_template
ifeq ($$(strip $$($(1)_OBJS) $$($(1)_SRCS) $$(PRODUCT_OBJS)),)
$(1)_OBJS = $(1)$$(OBJ)
endif
endef

$(foreach target, $(PROD) $(TESTPROD), \
    $(eval $(call PROD_template,$(strip $(target)))))

#-----------------------------------------------------------------------

define TARGET2_template
$(1)_LDLIBS += $$($(1)_LIBS)
$(1)_LDLIBS += $$(if $$(strip $$($(1)_LIBS_$$(OS_CLASS))), \
             $$(subst -nil-,,$$($(1)_LIBS_$$(OS_CLASS))), \
             $$($(1)_LIBS_DEFAULT))

$(1)_RESS = $$(if $$(RES),$$(addsuffix $$(RES),$$(basename $$($(1)_RCS))),)
$(1)_OBJSNAME = $$(addsuffix $$(OBJ),$$(basename $$($(1)_OBJS) $$($(1)_SRCS) ))
$(1)_DEPLIBS = $$(foreach lib, $$($(1)_LDLIBS),\
 $$(firstword $$(wildcard $$(addsuffix /$$(LIB_PREFIX)$$(lib).*,\
 $$($$(lib)_DIR) $$(SHRLIB_SEARCH_DIRS)))\
 $$(addsuffix /$$(LIB_PREFIX)$$(lib)$$(LIB_SUFFIX),\
 $$(firstword $$($$(lib)_DIR) $$(SHRLIB_SEARCH_DIRS)))))

endef

$(foreach target, $(PROD) $(TESTPROD) $(LIBRARY) $(LOADABLE_LIBRARY) , \
    $(eval $(call TARGET2_template,$(strip $(target)))))

#-----------------------------------------------------------------------

define PROD2_template

$(1)$$(EXE): $$($(1)_OBJSNAME) $$($(1)_RESS) $$($(1)_DEPLIBS)
endef

$(foreach target, $(PROD) $(TESTPROD), \
    $(eval $(call PROD2_template,$(strip $(target)))))

#-----------------------------------------------------------------------

define LIBRARY_template
BUILD_LIBRARY += $$(if $$(strip $$($(1)_OBJSNAME) $$(LIBRARY_OBJS)),$(1),)

$(1)_DLL_DEPLIBS=$$(foreach lib, $$($(1)_DLL_LIBS),\
 $$(firstword $$(wildcard $$(addsuffix /$$(LIB_PREFIX)$$(lib).*,\
 $$($$(lib)_DIR) $$(SHRLIB_SEARCH_DIRS)))\
 $$(addsuffix /$$(LIB_PREFIX)$$(lib)$$(LIB_SUFFIX),\
 $$(firstword $$($$(lib)_DIR) $$(SHRLIB_SEARCH_DIRS)))))

$$(LIB_PREFIX)$(1)$$(LIB_SUFFIX):$$($(1)_OBJSNAME) $$($(1)_RESS)
$$(LIB_PREFIX)$(1)$$(LIB_SUFFIX):$$($(1)_DEPLIBS)
$$(SHRLIB_PREFIX)$(1)$$(SHRLIB_SUFFIX):$$($(1)_OBJSNAME) $$($(1)_RESS)
$$(SHRLIB_PREFIX)$(1)$$(SHRLIB_SUFFIX):$$($(1)_DEPLIBS)
$$(SHRLIB_PREFIX)$(1)$$(SHRLIB_SUFFIX):$$($(1)_DLL_DEPLIBS)
endef

$(foreach target, $(LIBRARY), \
    $(eval $(call LIBRARY_template,$(strip $(target)))))

#-----------------------------------------------------------------------

define LOADABLE_LIBRARY_template
LOADABLE_BUILD_LIBRARY += $$(if $$(strip $$($(1)_OBJSNAME) $$(LIBRARY_OBJS)),$(1),)

$(1)_DLL_DEPLIBS=$$(foreach lib, $$($(1)_DLL_LIBS),\
 $$(firstword $$(wildcard $$(addsuffix /$$(LIB_PREFIX)$$(lib).\*,\
 $$($$(lib)_DIR) $$(SHRLIB_SEARCH_DIRS)))\
 $$(addsuffix /$$(LIB_PREFIX)$$(lib)$$(LIB_SUFFIX),\
 $$(firstword $$($$(lib)_DIR) $$(SHRLIB_SEARCH_DIRS)))))

$$(LOADABLE_SHRLIB_PREFIX)$(1)$$(LOADABLE_SHRLIB_SUFFIX):$$($(1)_OBJSNAME) $$($(1)_RESS)
$$(LOADABLE_SHRLIB_PREFIX)$(1)$$(LOADABLE_SHRLIB_SUFFIX):$$($(1)_DEPLIBS)
$$(LOADABLE_SHRLIB_PREFIX)$(1)$$(LOADABLE_SHRLIB_SUFFIX):$$($(1)_DLL_DEPLIBS)
endef

$(foreach target, $(LOADABLE_LIBRARY), \
    $(eval $(call LOADABLE_LIBRARY_template,$(strip $(target)))))

