## Use the R_HOME indirection to support installations of multiple R version

# Use C++11 if available
CXX_STD=CXX11

UNAME := $(shell uname)

ifeq ($(UNAME), Darwin)
FRAMEWORK = -framework CoreServices
endif

PKG_LIBS = ./libuv/.libs/libuv.a ./http-parser/http_parser.o ./sha1/sha1.o ./base64/base64.o $(FRAMEWORK)
ifeq ($(UNAME), SunOS)
PKG_LIBS += -lkstat -lsendfile -lsocket -lxnet
endif
ifeq ($(UNAME), FreeBSD)
PKG_LIBS += -lkvm
endif
ifeq ($(UNAME), OpenBSD)
PKG_LIBS += -lkvm
endif

PKG_CPPFLAGS = $(C_VISIBILITY)

# To avoid spurious warnings from `R CMD check --as-cran`, about compiler
# warning flags like -Werror.
ifdef _R_CHECK_COMPILATION_FLAGS_
CONFIGURE_FLAGS="--quiet"
endif

#### Debugging flags ####
# Uncomment to enable thread assertions
# PKG_CPPFLAGS += -DDEBUG_THREAD -UNDEBUG
# Uncomment to enable printing of trace() messages
# PKG_CPPFLAGS += -DDEBUG_TRACE


$(SHLIB): libuv/.libs/libuv.a http-parser/http_parser.o sha1/sha1.o base64/base64.o

# We needed to rename lt~obsolete.m4 because the name causes problems with R
# CMD check. Here we rename it back.
libuv/m4/lt~obsolete.m4: libuv/m4/lt_obsolete.m4
	cp -p -f libuv/m4/lt_obsolete.m4 libuv/m4/lt~obsolete.m4

# Run ./configure. We need to touch various autotools-related files to avoid
# it trying to run autotools programs again.
libuv/Makefile: libuv/m4/lt~obsolete.m4
	(cd libuv \
		&& touch Makefile.in aclocal.m4 configure m4/lt~obsolete.m4 \
		&& CC="$(CC)" CFLAGS="$(CFLAGS) $(CPICFLAGS) $(C_VISIBILITY)" AR="$(AR)" RANLIB="$(RANLIB)" LDFLAGS="$(LDFLAGS)" ./configure $(CONFIGURE_FLAGS))

libuv/.libs/libuv.a: libuv/Makefile
	$(MAKE) --directory=libuv \
		HAVE_DTRACE=0

clean:
	$(MAKE) --directory=libuv distclean
