#*************************************************************************
# Copyright (c) 2011 UChicago Argonne LLC, 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 is distributed subject to a Software License Agreement found
# in the file LICENSE that is included with this distribution.
#*************************************************************************

# RULES_TARGET

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 LDOBJS SYS_LIBS , \
$(foreach target, $(PROD) $(TESTPROD) $(LIBRARY) $(TESTLIBRARY) $(LOADABLE_LIBRARY) , \
    $(eval $(call TARGET_template,$(strip $(target)),$(type)))))

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

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)))))

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

# These must be done before PROD2_template
define TESTLIBRARY_template
$(1)_DIR = .
TESTBUILD_LIBRARY += $$(if $$(strip $$($(1)_OBJSNAME) $$(LIBRARY_OBJS)),$(1),)
endef

$(foreach target, $(TESTLIBRARY), \
    $(eval $(call TESTLIBRARY_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 /$(DLLSTUB_PREFIX)$$(lib)$(DLLSTUB_SUFFIX), \
        $$($$(lib)_DIR) $$(SHRLIB_SEARCH_DIRS)) \
      $$(addsuffix /$(SHRLIB_PREFIX)$$(lib)*$(SHRLIB_SUFFIX_BASE)*, \
        $$($$(lib)_DIR) $$(SHRLIB_SEARCH_DIRS)) \
      $$(addsuffix /$(LIB_PREFIX)$$(lib)$(LIB_SUFFIX), \
        $$($$(lib)_DIR) $$(SHRLIB_SEARCH_DIRS)) \
    ) $$(addsuffix /$(BUILDLIB_PREFIX)$$(lib)$(BUILDLIB_SUFFIX), \
      $$(firstword $$($$(lib)_DIR) $(SHRLIB_SEARCH_DIRS)))))
endef

$(foreach target, $(PROD) $(TESTPROD) $(LIBRARY) $(TESTLIBRARY) $(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

$(1)_DLL_DEPLIBS=$$(foreach lib, $$($(1)_DLL_LIBS), \
  $$(firstword $$(wildcard \
      $$(addsuffix /$(DLLSTUB_PREFIX)$$(lib)$(DLLSTUB_SUFFIX), \
        $$($$(lib)_DIR) $$(SHRLIB_SEARCH_DIRS)) \
      $$(addsuffix /$(SHRLIB_PREFIX)$$(lib)*$(SHRLIB_SUFFIX_BASE)*, \
        $$($$(lib)_DIR) $$(SHRLIB_SEARCH_DIRS)) \
      $$(addsuffix /$(LIB_PREFIX)$$(lib)$(LIB_SUFFIX), \
        $$($$(lib)_DIR) $$(SHRLIB_SEARCH_DIRS)) \
    ) $$(addsuffix /$(BUILDLIB_PREFIX)$$(lib)$(BUILDLIB_SUFFIX), \
      $$(firstword $$($$(lib)_DIR) $$(SHRLIB_SEARCH_DIRS)))))

$$(LIB_PREFIX)$(1)$$(LIB_SUFFIX):$$($(1)_OBJSNAME) $$($(1)_RESS)
$$(LIB_PREFIX)$(1)$$(LIB_SUFFIX):$$($(1)_DEPLIBS)

ifneq ($$($(1)_API),)
$$(LIB_PREFIX)$(1)$$(LIB_SUFFIX): API_CPPFLAGS += -DBUILDING_$$($(1)_API)_API
endif

ifeq ($$(SHARED_LIBRARIES),YES)

ifdef SHRLIB_SUFFIX
$$(SHRLIB_PREFIX)$(1)$$(SHRLIB_SUFFIX):$$($(1)_OBJSNAME) $$($(1)_RESS)
$$(SHRLIB_PREFIX)$(1)$$(SHRLIB_SUFFIX):$$($(1)_DEPLIBS)
$$(SHRLIB_PREFIX)$(1)$$(SHRLIB_SUFFIX):$$($(1)_DLL_DEPLIBS)

ifneq ($$($(1)_API),)
$$(SHRLIB_PREFIX)$(1)$$(SHRLIB_SUFFIX): API_CPPFLAGS += -DBUILDING_$$($(1)_API)_API
endif
endif

endif

endef

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

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

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

# Needed for -j parallel builds option
ifeq ($$(SHARED_LIBRARIES),YES)
ifdef SHRLIB_SUFFIX
$$(INSTALL_LIB)/$$(DLLSTUB_PREFIX)$(1)$$(DLLSTUB_SUFFIX): \
    $$(INSTALL_SHRLIB)/$$(SHRLIB_PREFIX)$(1)$$(SHRLIB_SUFFIX)
endif
endif
endef

$(foreach target, $(LIBRARY), \
    $(eval $(call LIBRARY2_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)

ifneq ($$($(1)_API),)
$$(LOADABLE_SHRLIB_PREFIX)$(1)$$(LOADABLE_SHRLIB_SUFFIX): \
    API_CPPFLAGS += -DBUILDING_$$($(1)_API)_API
endif
endef

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

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