TOP=..
include $(TOP)/mk/boilerplate.mk
-include config.mk

ifneq "$(findstring clean, $(MAKECMDGOALS))" ""
# if we're cleaning, then config.mk might have been cleaned already
READLINE_BUILD_PACKAGE=yes
PACKAGE=readline
endif

ifeq "$(READLINE_BUILD_PACKAGE)" "yes"

SUBDIRS      = include

ALL_DIRS     = System/Console
PACKAGE_DEPS = base

SRC_HC_OPTS += -Iinclude
SRC_CC_OPTS += -Iinclude
SRC_HSC2HS_OPTS += -Iinclude

SRC_HADDOCK_OPTS += -t "Haskell Hierarchical Libraries ($(PACKAGE) package)"

# yeuch, have to get the vars in through CPP to package.conf.in, and put
# quotes around each element.
empty =
space = $(empty) $(empty)
comma = ,
PACKAGE_CPP_OPTS += -DREADLINE_LIBS='$(subst $(space),$(comma),$(patsubst %,"%",$(strip $(READLINE_LIBS))))'
PACKAGE_CPP_OPTS += -DREADLINE_INCLUDE_DIRS='$(subst $(space),$(comma),$(patsubst %,"%",$(strip $(READLINE_INCLUDE_DIRS))))'
PACKAGE_CPP_OPTS += -DREADLINE_LIB_DIRS='$(subst $(space),$(comma),$(patsubst %,"%",$(strip $(READLINE_LIB_DIRS))))'

ifneq "$(READLINE_INCLUDE_DIRS)" ""
    SRC_HC_OPTS += -I$(READLINE_INCLUDE_DIRS)
    SRC_CC_OPTS += -I$(READLINE_INCLUDE_DIRS)
    SRC_HSC2HS_OPTS += -I$(READLINE_INCLUDE_DIRS)
endif
ifneq "$(READLINE_LIB_DIRS)" ""
    SRC_LD_OPTS += -L$(READLINE_LIB_DIRS)
endif

ifneq "$(BootingFromHc)" "YES"
  STUBOBJS += System/Console/Readline_stub.$(way_)o
endif
  CLEAN_FILES += System/Console/Readline_stub.[ch] $(STUBOBJS)

ifeq "$(TARGETPLATFORM)" "i386-unknown-mingw32"
#
# The 'readline' library is only supplied in static form with mingw (libreadline.a).
# The GHCi linker isn't capable of slurping in archives, so we're forced to convert
# that library into a relocatable .o (=> and install&bundle it.)
#
readline.o:
	$(word 1,$(AR)) x `$(CC) -print-file-name=libmoldname.a` strcasecmp.o strncasecmp.o
	$(LD) -r -o readline.o --whole-archive `$(CC) -print-file-name=libreadline.a` strcasecmp.o strncasecmp.o
	$(RM) strcasecmp.o strncasecmp.o

INSTALL_LIBS=readline.o
CLEAN_FILES=readline.o
endif

endif

include $(TOP)/mk/target.mk
