include ../../librz/config.mk
include ../../mk/platform.mk
include ../../mk/$(COMPILER).mk
include ../../shlr/zip/deps.mk
EDITOR?=vim
CC?=gcc
CC_AR?=ar
AR?=$(CC_AR)
RANLIB?=ranlib
LIBNAME=libgdbr
LIBFILE=$(LIBNAME).$(EXT_SO)
CFLAGS+=-I$(LIBRZ)/include $(PIC_CFLAGS)
CFLAGS+=-Iinclude -I${(LIBRZ)}/include
MAJOR=0
MINOR=1
LD=$(CC)
ifeq ($(LD),cccl)
LDFLAGS+=-L${(LIBRZ)}/socket -llibrz_socket
LDFLAGS+=-L${(LIBRZ)}/util -llibrz_util
EXT_AR?=lib
else
LDFLAGS+=-L${(LIBRZ)}/socket -lrz_socket
LDFLAGS+=-L${(LIBRZ)}/util -lrz_util
EXT_AR?=a
endif

ifeq (${OSTYPE},darwin)
LDFLAGS_SHARED=-dynamiclib
else
LDFLAGS_SHARED=-shared
endif

#OSTYPE=windows
include ../../librz/socket/deps.mk
include ../../shlr/sdb.mk

# Test variables
TEST_D= $(PWD)/test
BIN=$(PWD)/bin
UNIT_TEST=$(TEST_D)/unit.c
CLIENT=$(TEST_D)/client.c

PWD=$(shell pwd)
TEST=test
LIB=lib

TEST_INCLUDES += $(INCLUDES) -I/usr/include/glib-2.0 -I/usr/lib64/glib-2.0/include

SRC_D=src
SRC_C=$(wildcard $(SRC_D)/*.c) $(wildcard $(SRC_D)/*/*.c)
SRC_O=$(SRC_C:.c=.o)

TEST_C=$(wildcard $(TEST_D)/*.c)
TEST_O=$(TEST_C:.c=.o)

all: $(LIB)/$(LIBNAME).${EXT_AR}
#	make

default: make

prepare:
	mkdir -p $(LIB)

$(LIB):
	mkdir -p $(LIB)

$(LIB)/$(LIBNAME).${EXT_AR}: $(LIB) $(SRC_O)
ifeq ($(WITHPIC),1)
	$(LD) $(PIC_CFLAGS) $(LDFLAGS_SHARED) -o $(LIB)/$(LIBFILE) $(CFLAGS) $(SRC_O) $(LDFLAGS) $(LINK)
endif
	$(AR) rvs $(LIB)/$(LIBNAME).${EXT_AR} $(SRC_O)
	# required for solaris and w32
	$(RANLIB) $(LIB)/$(LIBNAME).${EXT_AR}

clean:
	-rm -f $(SRC_O)
	-rm -rf lib

unit: lib
	$(CC) $(CFLAGS) $(TEST_INCLUDES) -c $(UNIT_TEST) -o $(TEST_D)/unit.o
	$(LD) $(TEST_D)/unit.o -o $(TEST_D)/unit -L$(LIB) -lgdbr -lglib-2.0

run_unit: unit
	LD_(LIBRZ)ARY_PATH=./lib ./test/unit

client: lib
	$(CC) $(CFLAGS) $(INCLUDES) -c $(CLIENT) -o $(TEST_D)/client.o
	$(LD) $(TEST_D)/client.o -o $(TEST_D)/client -L$(LIB) -lgdbr

run_test: client
	rz_run libpath=./lib program=./test/client

gdb_test: client
	rz_run libpath=./lib system="gdb ./test/client"

valgrind_test: client
	rz_run libpath=./lib system="valgrind --track-origins=yes -v --leak-check=full ./test/client"

edit:
	$(EDITOR) -c "args **/*.h **/*.c"
