# CONFIG.Common.UnixCommon
#
#  Contains definitions common to all Unix target archs
#
#  This file is maintained by the build community.
#  Sites may override definitions in CONFIG_SITE.Common.UnixCommon
#  or CONFIG_SITE.<host>.UnixCommon
#-------------------------------------------------------

# Unix valid build types
VALID_BUILDS = Host Ioc Command

#-------------------------------------------------------
# Unix prefix and suffix definitions
EXE =
OBJ = .o
#Library prefix and suffixes
LIB_PREFIX = lib
LIB_SUFFIX = .a
SHRLIB_SUFFIX_BASE = .so
SHRLIB_SUFFIX = $(SHRLIB_SUFFIX_BASE)$(addprefix .,$(SHRLIB_VERSION))
LOADABLE_SHRLIB_SUFFIX = $(SHRLIB_SUFFIX_BASE)$(addprefix .,$(LOADABLE_SHRLIB_VERSION))
LOADABLE_SHRLIB_PREFIX = lib

#-------------------------------------------------------
#	names of libraries to build 
# <lib> ->  lib<lib>.a
LIBNAME = $(BUILD_LIBRARY:%=$(LIB_PREFIX)%$(LIB_SUFFIX))
TESTLIBNAME = $(TESTBUILD_LIBRARY:%=$(LIB_PREFIX)%$(LIB_SUFFIX))
# <lib> ->  lib<lib>.so.<version>
SHRLIBNAME_YES = $(BUILD_LIBRARY:%=$(SHRLIB_PREFIX)%$(SHRLIB_SUFFIX))
TESTSHRLIBNAME_YES = $(TESTBUILD_LIBRARY:%=$(SHRLIB_PREFIX)%$(SHRLIB_SUFFIX))
LOADABLE_SHRLIBNAME = $(LOADABLE_BUILD_LIBRARY:%=$(LOADABLE_SHRLIB_PREFIX)%$(LOADABLE_SHRLIB_SUFFIX))

#-------------------------------------------------------
# shrlib: SHRLIB_DEPLIBS, SHRLIB_LDLIBS and SHRLIBDIR_LDFLAGS definitions

# SHRLIB_LIBS deprecated
LIB_LIBS += $(SHRLIB_LIBS)

SHRLIB_DEPLIBS = $(foreach lib, $(LIB_LIBS) $(USR_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), \
    $(if $(filter $(lib),$(TESTLIBRARY)),.,$(INSTALL_LIB)))))

SHRLIB_LDLIBS = $(addprefix -l, $($*_LDLIBS) $(LIB_LIBS) $(USR_LIBS)) \
    $(STATIC_LDLIBS) \
    $(addprefix -l, $($*_SYS_LIBS) $(LIB_SYS_LIBS) $(USR_SYS_LIBS)) \
    $(LDLIBS)

SHRLIB_DEPLIB_DIRS = $(foreach word, \
    $(sort $(INSTALL_LIB)/ $(dir $($*_DEPLIBS) $(SHRLIB_DEPLIBS))), \
    $(abspath $(word)))

SHRLIBDIR_LDFLAGS += $(SHRLIB_DEPLIB_DIRS:%=-L%)

#-------------------------------------------------------
# Prod: PROD_DEPLIBS, PROD_LDLIBS and PRODDIR_LDFLAGS definitions

PROD_DEPLIBS = $(foreach lib, $(PROD_LIBS) $(USR_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), \
    $(if $(filter $(lib),$(TESTLIBRARY)),.,$(INSTALL_LIB)))))

PROD_LDLIBS = $(addprefix -l, $($*_LDLIBS) $(PROD_LIBS) $(USR_LIBS)) \
    $(STATIC_LDLIBS) \
    $(addprefix -l, $($*_SYS_LIBS) $(PROD_SYS_LIBS) $(USR_SYS_LIBS))

LDLIBS_STATIC_YES = LDLIBS
LDLIBS_SHARED_NO = LDLIBS
PROD_LDLIBS += $($(firstword $(LDLIBS_STATIC_$(STATIC_BUILD)) \
               $(LDLIBS_SHARED_$(SHARED_LIBRARIES))))

PROD_DEPLIB_DIRS = $(foreach word, \
    $(sort $(INSTALL_LIB)/ $(dir $($*_DEPLIBS) $(PROD_DEPLIBS))), \
    $(abspath $(word)))

PRODDIR_LDFLAGS += $(PROD_DEPLIB_DIRS:%=-L%)

#--------------------------------------------------
# Link definitions
LINK.cpp = $(CCC) -o $@ $(STATIC_LDFLAGS) $(PRODDIR_LDFLAGS) $(LDFLAGS)
LINK.cpp += $(PROD_LDFLAGS) $(PROD_LD_OBJS) $(PROD_LD_RESS) $(PROD_LDLIBS)
LINK.shrlib = $(CCC) -o $@ $(TARGET_LIB_LDFLAGS) $(SHRLIBDIR_LDFLAGS) $(LDFLAGS)
LINK.shrlib += $(LIB_LDFLAGS) $(LIBRARY_LD_OBJS) $(LIBRARY_LD_RESS) $(SHRLIB_LDLIBS)

#--------------------------------------------------
# Operating system definitions
OP_SYS_CPPFLAGS += -DUNIX 
OP_SYS_LDLIBS += -lm

#--------------------------------------------------
# Allow site overrides
-include $(CONFIG)/os/CONFIG_SITE.Common.UnixCommon
-include $(CONFIG)/os/CONFIG_SITE.$(EPICS_HOST_ARCH).UnixCommon
