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

UNAME := $(shell uname)

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

PKG_LIBS = `$(R_HOME)/bin/Rscript -e "Rcpp:::LdFlags()"` ./libuv/libuv.a ./http-parser/http_parser.o ./sha1/sha1.o ./base64/base64.o $(FRAMEWORK)
PKG_CPPFLAGS += -I./libuv/include -I./http-parser -I./sha1 -I./base64

.PHONY: all libuv.a http-parser.o

all: $(SHLIB)
$(SHLIB): libuv.a http-parser.o sha1.o base64.o

libuv.a:
	$(MAKE) --directory=libuv \
		CC="$(CC)" CFLAGS="$(CFLAGS) $(CPICFLAGS)" AR="$(AR)" RANLIB="$(RANLIB)" \
    HAVE_DTRACE=0 \
		libuv.a

http-parser.o:
	$(MAKE) --directory=http-parser \
		CC="$(CC)" CFLAGS="$(CFLAGS) $(CPICFLAGS)" AR="$(AR)" RANLIB="$(RANLIB)" \
		http_parser.o

sha1.o:
	(cd sha1 && $(CC) $(CFLAGS) $(CPICFLAGS) -c sha1.c -o sha1.o)

base64.o:
	(cd base64 && $(CXX) $(ALL_CPPFLAGS) $(ALL_CXXFLAGS) -c base64.cpp -o base64.o)
