#
# $Id$
#

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

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

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

LD = gcc
LD_OUT = -o

# Add all libraries specified in LIBS.
ifeq ($(HB_LIB_COMPILE),)
LINKPATHS += $(foreach lib, $(LIBS), -L$(TOP)$(ROOT)source/$(lib)/$(ARCH))
else
LINKPATHS += -L$(HB_LIB_COMPILE)
endif
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)/$(ARCH))
LINKLIBS += $(foreach drv, $(HB_DB_DRIVERS), -l$(drv))
endif

# Add the specified GT driver library
ifeq ($(findstring rtl,$(LIBS)),rtl)
LINKPATHS += -L$(TOP)$(ROOT)source/rtl/$(HB_GT_LIB)/$(ARCH)
LINKLIBS += -l$(HB_GT_LIB)
endif

# 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, $^, $(lib_object))
echo. SAVE >> __lib__.tmp
echo. END >> __lib__.tmp
$(AR) -M < __lib__.tmp
endef

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

endef

define link_exe_file
echo. $(LDFLAGS) $(LD_OUT)$@ > __link__.tmp
$(foreach file, $^, $(link_file))
$(foreach file, $(LINKPATHS), $(link_file))
$(foreach file, $(LINKLIBS), $(link_file))
-$(LD) @__link__.tmp
endef

AR = ar
ARFLAGS =
AR_RULE = $(create_library)

LD_RULE = $(link_exe_file)

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