CC=g++ -c
LD=g++
CCFLAGS=-Wall -O3 -pedantic -Wno-long-long
LDFLAGS=
OUTNAME=pcref
INC=-Ipcre/include
LIBS=pcre/lib/libpcrecpp.a pcre/lib/libpcre.a

#-------------------------------------------------------------------------------
# OS specific
#-------------------------------------------------------------------------------
ifeq ($(shell uname), Linux)
LDFLAGS+= -static-libgcc -s
#LDFLAGS+= -static
endif
ifeq ($(shell uname), Darwin)
endif
#-------------------------------------------------------------------------------

.PHONY: all
all: $(OUTNAME) clean

.PHONY: clean
clean:
	@-rm -f *.o >/dev/null 2>&1

.PHONY: info
info: $(OUTNAME)
	@# Well let's use the program directly to format its info ...
	@file $(OUTNAME) | ./$(OUTNAME) '/^.*?:[\s]*(.*)/ - \1/ms' 's/,/\n -/ms'
	-@readelf -d $(OUTNAME) 2>/dev/null | grep -i shared | ./$(OUTNAME) \
	      's/^.*?:[\s]*\[(.*?)\].*$$/\1/m' 's/[\s]+$$//sm' 's/\n/, /sm' \
	      's/^(.*?)$$/ - dependencies: \1\n/sm'
	@du -h $(OUTNAME) | ./$(OUTNAME) '/^([\w\d\.]+)/ - size: $$1\n/smx'

.PHONY: test
test: $(OUTNAME)
	@cd test; ./test.sh

#.PHONY: $(OUTNAME)
$(OUTNAME): main.cc
	@$(CC) -o main.o main.cc $(CCFLAGS) $(INC)
	@$(LD) -o $(OUTNAME) main.o $(LDFLAGS) $(LIBS)
	-@rm -f main.o
	@echo Output binary is named \"$(OUTNAME)\"

.PHONY: get-pcre
get-pcre:
	@-rm -rf pcre
	@mkdir pcre
	@cd pcre; svn co svn://vcs.exim.org/pcre/code/trunk src
	@cd pcre/src; ./autogen.sh
	@cd pcre/src; ./configure --enable-utf --prefix=$(shell pwd)/pcre/
	@cd pcre/src; make
	@cd pcre/src; make install
	@cd pcre/src; make clean
	@cd pcre; rm -rf bin
	@cd pcre; rm -rf share
	@cd pcre/lib; rm -f *.dylib
	@cd pcre/lib; rm -f *.la
	@cd pcre/lib; rm -rf pkgconfig
	@cd pcre; rm -rf src
