TARGET = cmsg
OBJECTS = main.o harness.o net.o util.o relay.o hashtable.o dataq.o sexp.o sexpio.o node.o \
	queue.o direct.o fanout.o subscription.o meta.o messages.o

UUID_CFLAGS:=$(shell uuid-config --cflags)
UUID_LDFLAGS:=$(shell uuid-config --ldflags)

ifeq ($(shell pkg-config --exists libevent && echo yes),yes)
LIBEVENT_CFLAGS:=$(shell pkg-config --cflags libevent)
LIBEVENT_LDFLAGS:=$(shell pkg-config --libs libevent)
else
LIBEVENT_CFLAGS:=
LIBEVENT_LDFLAGS:=-levent
endif

# grr
ifeq ($(shell uname -s),Darwin)
UUID_LIB=uuid
else
UUID_LIB=ossp-uuid
endif

#CFLAGS = -D_XOPEN_SOURCE=600 -Wall -O0 -g $(UUID_CFLAGS) $(LIBEVENT_CFLAGS)
CFLAGS = -D_XOPEN_SOURCE=600 -Wall -O3 $(UUID_CFLAGS) $(LIBEVENT_CFLAGS)
#CFLAGS = -D_XOPEN_SOURCE=600 -Wall -O3 -static $(UUID_CFLAGS) $(LIBEVENT_CFLAGS)

all: $(TARGET)

$(TARGET): $(OBJECTS)
	$(CC) $(CFLAGS) $(UUID_LDFLAGS) -o $@ $(OBJECTS) -l$(UUID_LIB) $(LIBEVENT_LDFLAGS)
#	$(CC) $(CFLAGS) $(UUID_LDFLAGS) -o $@ $(OBJECTS) -l$(UUID_LIB) $(LIBEVENT_LDFLAGS) -lrt

%.o: %.c
	$(CC) $(CFLAGS) -c $<

messages.c: ../../protocol/messages.json codegen.py
	python codegen.py body > $@

messages.h: ../../protocol/messages.json codegen.py
	python codegen.py header > $@

clean:
	rm -f $(TARGET)
	rm -f $(OBJECTS)
	rm -rf *.dSYM
	rm -f depend.mk messages.c messages.h

depend.mk:
	touch messages.h
	gcc $(CFLAGS) -M *.c > $@
	rm messages.h
	echo "depend.mk:" Makefile *.c >> $@
-include depend.mk
