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

all: libelf++.a libelf++.pc

SRCS := elf.cc mmap_loader.cc to_string.cc
HDRS := elf++.hh data.hh common.hh to_hex.hh
CLEAN :=

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

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

to_string.cc: enum-print.py 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 "data.hh"' >> to_string.cc
	@echo '#include "to_hex.hh"' >> to_string.cc
	@echo >> to_string.cc
	@echo 'ELFPP_BEGIN_NAMESPACE' >> to_string.cc
	@echo >> to_string.cc
	python2 enum-print.py -u --hex --no-type --mask shf --mask pf \
		-x loos -x hios -x loproc -x hiproc < data.hh >> to_string.cc
	@echo 'ELFPP_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.
libelf++.pc: always
	@(VER=$$(git describe --match 'v*' | sed -e s/^v//); \
	  echo "prefix=$$PWD"; \
	  echo "Name: libelf++"; \
	  echo "Description: C++11 ELF library"; \
	  echo "Version: $$VER"; \
	  echo "Libs: -L\$${prefix} -lelf++"; \
	  echo "Cflags: -I\$${prefix}") > $@
CLEAN += libelf++.pc

.PHONY: always

PREFIX?=/usr/local

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

clean:
	rm -f $(CLEAN)

.DELETE_ON_ERROR:
