# config.mk.tail

LIBRZ:=$(abspath $(dir $(lastword $(MAKEFILE_LIST))))
# neat???!? trick to support path with spaces
null:=
SPACE:=$(null) $(null)

ifeq ($(LIBRZ),)
ifeq ($(RZDIR),)
$(error ERROR: Cannot find RZDIR. Please define this variable)
else
LIBRZ=$(RZDIR)/librz
MSVC=$(RZDIR)/msvc
endif
endif

SHLR:=$(LIBRZ)/../shlr

#include $(LIBRZ)/../config-user.mk
include $(LIBRZ)/../global.mk
include $(LIBRZ)/../mk/${COMPILER}.mk

CFLAGS+=${CFLAGS_APPEND}
CFLAGS+=${CPPFLAGS}

ifeq (1,1)
#$(WITHPIC),1)
LINK+=$(subst rz_,-L$(LIBRZ)/,$(RZ_DEPS))
LINK+=$(subst rz_,-lrz_,$(RZ_DEPS))
LINK+=$(subst rz_,-L$(LIBRZ)/,$(BINDEPS))
LINK+=$(subst rz_,-lrz_,$(BINDEPS))
CFLAGS+=${PIC_CFLAGS}
LINK+=${PIC_CFLAGS}
else
ifneq ($(RZ_DEPS),)
LINK+=$(shell echo ${RZ_DEPS} | sed -e 's:rz_\([^ ]\+\):$(LIBRZ)/\1/librz_\1.${EXT_AR}:g')
endif
endif

# Compiler: see mk/gcc.mk
# CC CFLAGS CC_LIB CC_AR LINK

# Debug
CFLAGS+=-g
CFLAGS+=-Wall

# libgmp
ifeq (${HAVE_LIB_GMP},1)
CFLAGS+=-DHAVE_LIB_GMP=1
BN_LIBS=-lgmp
endif

# open-ssl
ifeq (${HAVE_LIB_SSL},1)
BN_LIBS=${SSL_LDFLAGS}
endif

#both of these need ssl includes
ifneq (,$(filter rz_socket rz_util,$(BINDEPS)))
ifeq (${HAVE_LIB_SSL},1)
CFLAGS+=${SSL_CFLAGS}
endif
endif

# XXX do it in configure stage
OSTYPE?=gnulinux
ifeq (${OSTYPE},auto)
_UNAME=$(shell uname)
ifeq ($(_UNAME),Darwin)
OSTYPE=darwin
else
OSTYPE=gnulinux
endif
endif
ifeq (${OSTYPE},openbsd)
OSTYPE=bsd
endif
ifeq (${OSTYPE},netbsd)
OSTYPE=bsd
endif
ifeq (${OSTYPE},freebsd)
OSTYPE=bsd
endif
ifeq (${OSTYPE},dragonfly)
OSTYPE=bsd
endif
ifeq (${OSTYPE},bitrig)
OSTYPE=bsd
endif
# Output
ifeq (${OSTYPE},windows)
CFLAGS+=-D__WINDOWS__=1
EXT_AR=lib
EXT_SO=dll
EXT_EXE=.exe
TH_LIBS=
endif
ifeq (${OSTYPE},bsd)
CFLAGS+=-D__UNIX__=1
EXT_AR=a
EXT_SO=so
EXT_EXE=
TH_LIBS=-lpthread
endif
ifeq (${OSTYPE},qnx)
CFLAGS+=-D__UNIX__=1
EXT_AR=a
EXT_SO=so
EXT_EXE=
TH_LIBS=
endif
ifeq (${OSTYPE},gnulinux)
CFLAGS+=-D__UNIX__=1
EXT_AR=a
EXT_SO=so
EXT_EXE=
TH_LIBS=-lpthread
endif
ifeq (${OSTYPE},haiku)
CFLAGS+=-D__UNIX__=1
EXT_AR=a
EXT_SO=so
EXT_EXE=
TH_LIBS=
endif
ifeq (${OSTYPE},darwin)
CFLAGS+=-D__UNIX__=1
EXT_AR=a
EXT_SO=dylib
EXT_EXE=
ifeq ($(shell uname -p),arm)
# iOS
TH_LIBS=
else
# OSX
TH_LIBS=-lpthread
endif
endif
ifeq (${OSTYPE},android)
CFLAGS+=-D__UNIX__=1
EXT_AR=a
EXT_SO=so
EXT_EXE=
TH_LIBS=
endif
ifeq (${OSTYPE},solaris)
CFLAGS+=-D__UNIX__=1
EXT_AR=a
EXT_SO=so
EXT_EXE=
TH_LIBS=
endif

ifeq (${EXT_SO},)
main:
	@echo
	@echo "OSTYPE: ${OSTYPE}"
	@echo ERROR: Unknown platform
	@echo
	@exit 1
endif

LIB=lib${NAME}
LIBAR=${LIB}.${EXT_AR}
LIBSO=${LIB}.${EXT_SO}
ifeq (${OSTYPE},android)
libname=-shared -o $1.${EXT_SO}
else
ifeq (${OSTYPE},windows)
libname=-shared -o $1.${EXT_SO}
else
ifeq (${OSTYPE},darwin)
ifeq (${HAVE_LIBVERSION},1)
libname=-dynamiclib -o $1.${EXT_SO} ${LDFLAGS_SONAME}$1.${LIBVERSION}.${EXT_SO}
else
libname=-dynamiclib -o $1.${EXT_SO}
endif
else
ifeq (${HAVE_LIBVERSION},1)
libname=-shared -o $1.${EXT_SO} ${LDFLAGS_SONAME}$1.${EXT_SO}.${LIBVERSION}
else
libname=-shared -o $1.${EXT_SO}
endif
endif
endif
endif

ifeq ($(OSTYPE),darwin)
libname-version = $(subst ${EXT_SO},$(2).${EXT_SO},$(1))
else
libname-version = $(1).$(2)
endif

libpath-to-name = $(word 2,$(subst /, ,$(1)))
libpath-to-name-version = $(call libname-version,$(call libpath-to-name,$(1)),$(2))

# -- singleton
endif
