#
# $Id$
#

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

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

CC = $(CCPREFIX)gcc
CC_IN = -c
CC_OUT = -o
CPPFLAGS = -I. -I$(HB_INC_COMPILE)
CFLAGS = -Wall -W

LD = $(CCPREFIX)gcc
LD_OUT = -o
LINKPATHS =
LDLIBS =

# 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

LDLIBS += $(foreach lib, $(CONTRIBS), -l$(subst lib,,$(lib)))
LDLIBS += $(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))
LDLIBS += $(foreach drv, $(HB_DB_DRIVERS), -l$(drv))
endif

# Add the specified GT driver library
ifeq ($(findstring rtl,$(LIBS)),rtl)
LINKPATHS += $(foreach gt, $(HB_GT_LIBS), -L$(TOP)$(ROOT)source/rtl/$(gt)/$(HB_ARCH))
LDLIBS += $(foreach gt, $(HB_GT_LIBS), -l$(gt))
endif

# HB_SCREEN_LIB: empty, or one of ncurses, slang
# HB_SCREEN_LIB=ncurses
# HB_SCREEN_LIB=slang

ifneq ($(HB_SCREEN_LIB),)
LDLIBS += -l$(HB_SCREEN_LIB)
endif

# Add the optional user path(s)
ifneq ($(LNK_USR_PATH),)
LINKPATHS += $(foreach path, $(LNK_USR_PATH), -L$(path))
endif

# Add the optional user libarary (or libraries)
ifneq ($(LNK_USR_LIB),)
LDLIBS += $(foreach lib, $(LNK_USR_LIB), -l$(lib))
endif

#SYSLIBS = -luser32 -lwinspool -lgdi32 -lcomctl32 -lcomdlg32 \
#          -lole32 -loleaut32 -luuid -lmpr -lwsock32 -lws2_32 -lmapi32
SYSLIBS = -lwininet -lws2

# NOTE: The empty line directly before 'endef' HAVE TO exist!
# It causes that every echo command will be separated by LF
define lib_object
echo. ADDMOD $(file) >> __lib__.tmp

endef

# We have to use script to overcome the DOS limit of max 128 characters
# in commmand line
define create_library
echo. CREATE $@ > __lib__.tmp
$(foreach file, $(^F), $(lib_object))
echo. SAVE >> __lib__.tmp
echo. END >> __lib__.tmp
$(AR) $(ARFLAGS) -M < __lib__.tmp
endef

# NOTE: The empty line below HAVE TO exist!
define link_file
echo. $(file) >> __link__.tmp

endef

define search_dir
echo. SEARCH_DIR($(file)) >> __link__.tmp

endef

ifneq ($(strip $(LDLIBS)),)
define libs_list
echo. GROUP( >> __link__.tmp
$(foreach file, $(LDLIBS), $(link_file))
echo. ) >> __link__.tmp
endef
else
libs_list=
endif

define link_exe_file
echo. INPUT( >> __link__.tmp
$(foreach file, $(^F), $(link_file))
echo. ) >> __link__.tmp
$(libs_list)
$(foreach file, $(subst -L,,$(LINKPATHS)), $(search_dir))
echo. GROUP( >> __link__.tmp
$(foreach file, $(SYSLIBS), $(link_file))
echo. ) >> __link__.tmp
-$(LD) $(LDFLAGS) $(L_USR) $(LD_OUT)$@ __link__.tmp
endef

LDFLAGS =

AR = $(CCPREFIX)ar
ARFLAGS = $(A_USR)

RANLIB = $(CCPREFIX)ranlib

AR = $(CCPREFIX)ar
ARFLAGS = $(A_USR)

ifeq ($(SHLVL),) # COMMAND.COM - length of command line is limited
ifneq ($(OS),Windows_NT)
AR_RULE = $(create_library)
LD_RULE = $(link_exe_file)
endif
endif

ifeq ($(AR_RULE),)
AR_RULE = $(AR) $(ARFLAGS) cr $@ $(^F) && $(RANLIB) $@ || $(RM) $@
# our libs have a lot of cross referenced now and we have to group them
# until we won't have cleaned them
LINKLIBS = $(LINKPATHS) -Wl,--start-group $(LDLIBS) -Wl,--end-group $(SYSLIBS)
endif

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