CXXFLAGS+=-g -O2 -Werror
override CXXFLAGS+=-std=c++0x -Wall -fPIC

all: libdwarf++.a libdwarf++.pc

SRCS := dwarf.cc cursor.cc die.cc value.cc abbrev.cc \
	expr.cc rangelist.cc line.cc attrs.cc \
	die_str_map.cc elf.cc to_string.cc
HDRS := dwarf++.hh data.hh internal.hh small_vector.hh ../elf/to_hex.hh
CLEAN :=

libdwarf++.a: $(SRCS:.cc=.o)
	ar rcs $@ $^
CLEAN += libdwarf++.a $(SRCS:.cc=.o)

$(SRCS:.cc=.o): $(HDRS)

to_string.cc: ../elf/enum-print.py dwarf++.hh data.hh Makefile
	@echo "// Automatically generated by make at $$(date)" > to_string.cc
	@echo "// DO NOT EDIT" >> to_string.cc
	@echo >> to_string.cc
	@echo '#include "internal.hh"' >> to_string.cc
	@echo >> to_string.cc
	@echo 'DWARFPP_BEGIN_NAMESPACE' >> to_string.cc
	@echo >> to_string.cc
	python2 ../elf/enum-print.py < dwarf++.hh >> to_string.cc
	python2 ../elf/enum-print.py -s _ -u --hex -x hi_user -x lo_user < data.hh >> to_string.cc
	@echo 'DWARFPP_END_NAMESPACE' >> to_string.cc
CLEAN += to_string.cc

# Create pkg-config for local library and headers. This will be
# transformed in to the correct global pkg-config by install.
libdwarf++.pc: always
	@(VER=$$(git describe --match 'v*' | sed -e s/^v//); \
	  echo "prefix=$$PWD"; \
	  echo "Name: libdwarf++"; \
	  echo "Description: C++11 DWARF library"; \
	  echo "Version: $$VER"; \
	  echo "Requires: libelf++ = $$VER"; \
	  echo "Libs: -L\$${prefix} -ldwarf++"; \
	  echo "Cflags: -I\$${prefix}") > $@
CLEAN += libdwarf++.pc

.PHONY: always

PREFIX?=/usr/local

install: libdwarf++.a libdwarf++.pc
	install -d $(PREFIX)/lib/pkgconfig
	install -t $(PREFIX)/lib libdwarf++.a
	install -d $(PREFIX)/include/libelfin/dwarf
	install -t $(PREFIX)/include/libelfin/dwarf data.hh dwarf++.hh small_vector.hh
	sed 's,^prefix=.*,prefix=$(PREFIX),;s,-L${prefix},&/lib,;s,-I${prefix},&/include,' libdwarf++.pc \
		> $(PREFIX)/lib/pkgconfig/libdwarf++.pc

clean:
	rm -f $(CLEAN)

.DELETE_ON_ERROR:
