#
# $Id$
#

include $(TOP)$(ROOT)config/$(HB_ARCHITECTURE)/global.cf

ifeq ($(HB_COMPILER),gpp)
   HB_CMP = g++
else
   HB_CMP = $(HB_COMPILER)
endif

OBJ_EXT = .o
EXE_EXT =
LIB_PREF = lib
LIB_EXT = .a

CC = $(HB_CCACHE) $(HB_CMP)
CC_IN = -c
CC_OUT = -o
CPPFLAGS = -I. -I$(HB_INC_COMPILE)

# We are under linux
CFLAGS = -DHB_OS_LINUX -Wall -W

# uncomment this if you want to force relocateable code for .so libs
# it's necessary on some platforms but can reduce performance
#CFLAGS += -fPIC

LD = $(HB_CCACHE) $(HB_CMP)
LD_OUT = -o

# Add all libraries specified in CONTRIBS and LIBS.
ifeq ($(HB_LIB_COMPILE),)
LINKPATHS += $(foreach lib, $(CONTRIBS), -L$(TOP)$(ROOT)contrib/$(lib)/$(HB_ARCH))
LINKPATHS += $(foreach lib, $(LIBS), -L$(TOP)$(ROOT)source/$(lib)/$(HB_ARCH))
else
LINKPATHS += -L$(HB_LIB_COMPILE)
endif

# our libs have a lot of cross referenced now and we have to group them
# untill we don't clean them
LINKLIBS += -Wl,--start-group

LINKLIBS += $(foreach lib, $(CONTRIBS), -l$(subst lib,,$(lib)))
LINKLIBS += $(foreach lib, $(LIBS), -l$(lib))

# If LIBS specifies the rdd library, add all DB drivers.
ifeq ($(findstring rdd,$(LIBS)),rdd)
LINKPATHS += $(foreach drv, $(HB_DB_DRIVERS), -L$(TOP)$(ROOT)source/rdd/$(drv)/$(HB_ARCH))
LINKLIBS += $(foreach drv, $(HB_DB_DRIVERS), -l$(drv))
endif

# Add the specified GT driver library and other RTLs
ifeq ($(findstring rtl,$(LIBS)),rtl)

LINKPATHS += $(foreach gt, $(HB_GT_LIBS), -L$(TOP)$(ROOT)source/rtl/$(gt)/$(HB_ARCH))
LINKLIBS += $(foreach gt, $(HB_GT_LIBS), -l$(gt))

# screen driver libraries
ifneq ($(findstring gtcrs, $(HB_GT_LIBS)),)
LINKLIBS += -lncurses
endif
ifneq ($(findstring gtsln, $(HB_GT_LIBS)),)
LINKLIBS += -lslang
endif
ifneq ($(findstring gtalleg, $(HB_GT_LIBS)),)
LINKLIBS += `allegro-config --static`
endif
ifneq ($(findstring gtxwc, $(HB_GT_LIBS)),)
LINKLIBS += -lX11
#LINKPATHS += -L/usr/X11R6/lib64
LINKPATHS += -L/usr/X11R6/lib
endif

# HB_GPM_MOUSE: use gpm mouse driver
ifeq ($(HB_GPM_MOUSE),yes)
LINKLIBS += -lgpm
endif

endif

ifneq ($(findstring -DHB_PCRE_REGEX, $(C_USR)),)
LINKLIBS += -lpcre
endif

LINKLIBS += -lm -ldl -Wl,--end-group

LDFLAGS = $(LINKPATHS)

AR = ar
ARFLAGS = $(A_USR)
AR_RULE = $(AR) $(ARFLAGS) cr $@ $(^F) && $(RANLIB) $@ || ( $(RM) $@ && false )

include $(TOP)$(ROOT)config/rules.cf
