#Standard boilerplate
prefix = /usr/local
exec_prefix = ${prefix}
mandir = ${datarootdir}/man
includedir = ${prefix}/include
datarootdir = ${prefix}/share
pkgconfig = ${exec_prefix}/lib/pkgconfig
srcdir = .
libdir=${exec_prefix}/lib
LOADLIBES = -lbluetooth 

vpath %.cc $(srcdir)

ifneq "$(DESTDIR)" ""
DESTDIR+=/
endif

CXX=g++
LD=g++
CXXFLAGS=-g -O2 -Wall -Wextra -W -O3 -ggdb -fPIC -I$(srcdir)
LDFLAGS=

hdr = $(DESTDIR)$(includedir)/
lib = $(DESTDIR)$(libdir)/

archive=libble++.a
soname=libble++.so
soname1=libble++.so.0
soname2=libble++.so.0.5
set_soname=-Wl,-soname,libble++.so.0

LIBOBJS=src/att.o src/uuid.o src/bledevice.o src/att_pdu.o src/pretty_printers.o src/blestatemachine.o src/float.o src/logging.o src/lescan.o

PROGS=examples/lescan examples/blelogger examples/bluetooth examples/lescan_simple examples/temperature examples/read_device_name examples/write

.PHONY: all clean testclean install lib progs test doc install-so install-a install-hdr install-pkgconfig

all: lib progs test doc

lib: $(soname) $(archive)
progs:$(PROGS)


distclean: clean
	rm -f Makefile config.log config.status libblepp.pc
clean: testclean
	rm -f $(PROGS) *.o */*.o *.so.* *.so *.d */*.d $(soname) $(soname1) $(soname2) $(archive)
testclean:
	rm -f tests/*.result tests/*.test tests/*.result_ tests/results



doc:


$(soname2): $(LIBOBJS)
	$(LD) -shared -o $(soname2) $(LIBOBJS) $(LDFLAGS) $(LOADLIBES)

$(soname1): $(soname2)
	rm -f $(soname1)
	ln -s $(soname2) $(soname1)

$(soname): $(soname1)
	rm -f $(soname)
	ln -s $(soname1) $(soname)

$(archive): $(LIBOBJS)
	ar crvs $(archive) $(LIBOBJS)
	ranlib $(archive)

$(PROGS):|$(soname)

$(PROGS:%=%.o): | examples
$(PROGS): % : %.o | examples
	$(LD) -o $@ $<  -L. -lble++

install: install-so install-a install-hdr install-pkgconfig


install-a: $(archive) | $(lib)
	cp $(archive) $(lib)

install-so: $(soname) $(soname1) $(soname2) | $(lib)
	cp $(soname) $(soname1) $(soname2) $(lib)

install-hdr: | $(hdr)
	cp -r $(srcdir)/blepp $(hdr)

install-pkgconfig:
	[ "$(pkgconfig)" = "" ] || mkdir -p $(DESTDIR)$(pkgconfig)
	[ "$(pkgconfig)" = "" ] || cp libblepp.pc $(DESTDIR)$(pkgconfig)/
	


docs:
	doxygen 


#Every .cc file in the tests directory is a test
TESTS=$(notdir $(basename $(wildcard $(srcdir)/tests/*.cc)))


#Get the intermediate file names from the list of tests.
TEST_RESULT=$(TESTS:%=tests/%.result)


# Don't delete the intermediate files, since these can take a
# long time to regenerate
.PRECIOUS: tests/%.result_ tests/%.test

test:tests/results

#We don't want this file hanging around on failure since we 
#want the build depend on it. If we leave it behing then typing make
#twice in a row will suceed, since make will find the file and not try
#to rebuild it.
.DELETE_ON_ERROR: tests/results 

tests/results:$(TEST_RESULT)
	cat $(TEST_RESULT) > tests/results
	@echo -------------- Test Results ---------------
	@cat tests/results
	@echo -------------------------------------------
	@ ! grep -qv OK tests/results 


#Build a test executable from a test program. On compile error,
#create an executable which declares the error.
tests/%.test: tests/%.cc $(LIBOBJS) | tests
	$(CXX) $(CXXFLAGS) $^ -o $@ -I . $(LDFLAGS) $(LOADLIBES) || {  echo "echo 'Compile error!' ; return 126" > $@ ; chmod +x $@; }

#Run the program and either use it's output (it should just say OK)
#or a failure message
tests/%.result_: tests/%.test | tests
	$< > $@ 2>&1 ; \
	a=$$? ;\
	if [ $$a != 0 ]; \
	then \
	   if [ $$a -ge 128 ] ; \
	   then \
	       echo Crash!! > $@ ; \
	   elif [ $$a -ne 126 ] ;\
	   then \
	       echo Failed > $@ ; \
	   fi;\
	else\
	    echo OK >> $@;\
	fi
	
tests/%.result: tests/%.result_
	echo $*: `tail -1 $<` > $@

#Get the C style dependencies working. Note we need to massage the test dependencies
#to make the filenames correct
.deps:
	rm -f .deps .sourcefiles
	find . -name "*.cc" | xargs -IQQQ $(CXX) $(CXXFLAGS) -MM -MG QQQ | sed -e'/test/s!\(.*\)\.o:!tests/\1.test:!'  > .deps

#Make compilation generate dependencies
%.o %d: %.cc
	$(CXX) -o $@ -c $< $(CXXFLAGS) -MMD -MP -MF $*.d

include $(wildcard *.d */*.d)

$(LIBOBJS): | $(sort $(dir $(LIBOBJS)))
tests/results: | $(if $(wildcard tests),,tests)

examples tests $(sort $(dir $(LIBOBJS))) $(lib) $(hdr):
	mkdir -p $@


