#  Makefile for dwarfdump
#  This is made very simple so it should work with
#  any 'make'.
#  The Makefile does assume that libdwarf is at ../libdwarf
#  from the dwarfdump2 source directory.
#

srcdir =	@srcdir@
VPATH =		@srcdir@

prefix =	@prefix@
exec_prefix =	@exec_prefix@
bindir =	$(exec_prefix)/bin
libdir =	$(exec_prefix)/lib
mandir =        $(exec_prefix)/share/man
man1dir =       $(mandir)/man1


INSTALL =	@INSTALL@
INSTALL_PROGRAM = @INSTALL_PROGRAM@
INSTALL_DATA =	@INSTALL_DATA@
DATAROOT =      @datarootdir@
SHELL =		/bin/sh
CC =		@CC@
AR =		@AR@
ARFLAGS =	@ARFLAGS@
RM =		rm
RANLIB =	@RANLIB@
DEFS =		@DEFS@
# ../libdwarf gets us to local headers and a libdwarf 
# archive, usually, so we assume it.
dwfwall =       @dwfwall@
dwfsanitize =   @dwfsanitize@
dwfzlib =       @dwfzlib@
LIBDWARF_PATH = ../libdwarf
DIRINC =        $(srcdir)/../libdwarf
LIBS =		@LIBS@ -L../libdwarf -ldwarf -lelf $(dwfzlib)
INCLUDES =	-I. -I$(srcdir) -I../libdwarf -I$(srcdir)/../libdwarf
CFLAGS =	$(PREINCS) @CPPFLAGS@ @CFLAGS@ $(INCLUDES) $(dwfwall) $(dwfsanitize) -DCONFPREFIX=${libdir} $(POSTINCS)
LDFLAGS =	$(PRELIBS) @LDFLAGS@  $(LIBS) $(dwfsanitize) $(POSTLIBS)
#VG =            valgrind --leak-check=full  --show-leak-kinds=all
VG =


INSTALL = cp

binprefix =

FINALOBJECTS = \
	addrmap.o \
        checkutil.o \
	dwarfdump.o \
	dwconf.o \
	esb.o \
        glflags.o \
        helpertree.o \
        macrocheck.o \
	print_abbrevs.o \
	print_aranges.o \
	print_debugfission.o \
	print_die.o \
	print_dnames.o \
	print_frames.o  \
	print_gdbindex.o  \
	print_lines.o \
	print_locs.o \
	print_macro.o \
	print_macros.o \
	print_pubnames.o \
	print_ranges.o \
	print_reloc.o \
	print_sections.o  \
	print_section_groups.o  \
	print_static_funcs.o  \
	print_static_vars.o  \
	print_strings.o \
	print_types.o \
	print_weaknames.o  \
	sanitized.o \
	section_bitmaps.o \
	strstrnocase.o \
	uri.o
GEN_HFILES = common.o \
	tmp-tt-table.c \
	tmp-ta-table.c \
	tmp-ta-ext-table.c \
	tmp-tt-ext-table.c 

all: dwarfdump

HEADERS =  $(srcdir)/checkutil.h \
        $(srcdir)/common.h \
        $(srcdir)/dwconf.h \
        $(srcdir)/dwgetopt.h \
        $(srcdir)/esb.h \
        $(srcdir)/globals.h \
        $(srcdir)/macrocheck.h \
        $(srcdir)/makename.h \
        $(srcdir)/dwarf_tsearch.h \
        $(srcdir)/print_frames.h \
        $(srcdir)/section_bitmaps.h \
        $(srcdir)/uri.h

$(FINALOBJECTS): $(GEN_HFILES)  $(HEADERS) $(srcdir)/naming.c

default: $(TARGETS)

dwarfdump: $(FINALOBJECTS) dwarf_tsearchbal.o makename.o naming.o common.o dwgetopt.o
	$(CC) $(CFLAGS) -o $@ $(FINALOBJECTS) common.o makename.o naming.o dwgetopt.o dwarf_tsearchbal.o  $(LDFLAGS) 

dwgetopt.o: $(srcdir)/dwgetopt.h $(srcdir)/dwgetopt.c 
	$(CC) $(CFLAGS) -c $(srcdir)/dwgetopt.c

dwarf_tsearchbal.o: $(srcdir)/dwarf_tsearch.h  $(srcdir)/dwarf_tsearchbal.c
	$(CC) $(CFLAGS) -c $(srcdir)/dwarf_tsearchbal.c

makename.o: $(srcdir)/makename.h $(srcdir)/makename.c 
	$(CC) $(CFLAGS) -c $(srcdir)/makename.c

common.o:  $(srcdir)/common.c $(srcdir)/common.h
	$(CC) $(CFLAGS) -c $(srcdir)/common.c

naming.o:  $(srcdir)/naming.c $(srcdir)/naming.h
	$(CC) $(CFLAGS) -c $(srcdir)/naming.c

# We need this as naming.o has external references we cannot have
# in the tree builds.
trivial_naming.o: $(srcdir)/naming.c
	$(CC) $(CFLAGS) -DTRIVIAL_NAMING  -c $(srcdir)/naming.c -o trivial_naming.o 

tag_tree_build:  $(srcdir)/tag_tree.c $(DIRINC)/dwarf.h $(HEADERS) tag_common.o makename.o common.o trivial_naming.o dwgetopt.o dwarf_tsearchbal.o
	LD_LIBRARY_PATH=$$LD_LIBRARY_PATH:$(LIBDWARF_PATH) $(CC) $(CFLAGS) $(srcdir)/tag_tree.c tag_common.o common.o makename.o  dwarf_tsearchbal.o trivial_naming.o dwgetopt.o  $(LDFLAGS) -o tag_tree_build 

tag_attr_build:  $(srcdir)/tag_attr.c $(DIRINC)/dwarf.h $(HEADERS) tag_common.o makename.o  common.o trivial_naming.o dwgetopt.o dwarf_tsearchbal.o
	$(CC) $(CFLAGS) $(srcdir)/tag_attr.c tag_common.o common.o makename.o trivial_naming.o  dwarf_tsearchbal.o dwgetopt.o $(LDFLAGS) -o tag_attr_build 

# Plain GNU C dash E does not work on a .list, 
# so copy to a .c name to run
# the following four table creations.
tmp-tt-table.c: $(srcdir)/tag_tree.list tag_tree_build
	-rm -f  tmp-t1.c
	cp $(srcdir)/tag_tree.list tmp-t1.c
	$(CC) $(CFLAGS) -E tmp-t1.c  > ./tmp-tag-tree-build1.tmp
	LD_LIBRARY_PATH=$$LD_LIBRARY_PATH:$(LIBDWARF_PATH) ./tag_tree_build -s  -i tmp-tag-tree-build1.tmp  -o tmp-tt-table.c

tmp-tt-ext-table.c: $(srcdir)/tag_tree_ext.list tag_tree_build
	-rm -f tmp-t4.c
	cp $(srcdir)/tag_tree_ext.list tmp-t4.c
	$(CC) $(CFLAGS) -E tmp-t4.c  > ./tmp-tag-tree-build4.tmp
	LD_LIBRARY_PATH=$$LD_LIBRARY_PATH:$(LIBDWARF_PATH) ./tag_tree_build -e  -i tmp-tag-tree-build4.tmp -o tmp-tt-ext-table.c

tmp-ta-table.c: $(srcdir)/tag_attr.list tag_attr_build
	-rm -f tmp-t2.c
	cp $(srcdir)/tag_attr.list tmp-t2.c
	$(CC) $(CFLAGS)  -E tmp-t2.c  > ./tmp-tag-attr-build2.tmp
	LD_LIBRARY_PATH=$$LD_LIBRARY_PATH:$(LIBDWARF_PATH) ./tag_attr_build -s -i tmp-tag-attr-build2.tmp -o tmp-ta-table.c

tmp-ta-ext-table.c: $(srcdir)/tag_attr_ext.list tag_attr_build
	-rm -f tmp-t3.c
	cp $(srcdir)/tag_attr_ext.list tmp-t3.c
	$(CC) $(CFLAGS)  -E tmp-t3.c  > ./tmp-tag-attr-build3.tmp
	LD_LIBRARY_PATH=$$LD_LIBRARY_PATH:$(LIBDWARF_PATH) ./tag_attr_build -e -i tmp-tag-attr-build3.tmp -o tmp-ta-ext-table.c


test: esb.o dwgetopt.o 
	echo "ESB test"
	$(CC) $(CFLAGS) -o test $(srcdir)/testesb.c esb.o
	$(VG) ./test
	-rm -f ./test
	echo "dwgetopt test"
	$(CC) $(CFLAGS) -o getopttest $(srcdir)/getopttest.c dwgetopt.o
	$(VG) ./getopttest
	-rm -f ./getopttest
	echo "Now use system getopt to validate our tests"
	$(CC) $(CFLAGS) -DGETOPT_FROM_SYSTEM -o getopttestnat $(srcdir)/getopttest.c dwgetopt.o
	./getopttestnat -c 1
	./getopttestnat -c 2
	./getopttestnat -c 3
	./getopttestnat -c 5
	./getopttestnat -c 6
	./getopttestnat -c 7
	./getopttestnat -c 8
	-rm -f ./getopttestnat
	-rm -f ./selfesb
	-rm -f ./selfmc
	-rm -f ./selfmakename
	-rm -f ./selfsection_bitmaps
	-rm -f ./selfprint_reloc
	$(CC) -g -c $(INCLUDES) $(srcdir)/esb.c
	$(CC) -g -c $(INCLUDES) $(srcdir)/dwarf_tsearchbal.c
	$(CC) -g -DSELFTEST  $(INCLUDES) $(srcdir)/makename.c dwarf_tsearchbal.o -o selfmakename
	$(CC) -DSELFTEST $(INCLUDES) -g $(srcdir)/helpertree.c esb.o dwarf_tsearchbal.o -o selfhelpertree
	./selfhelpertree
	rm -f ./selfhelpertree
	./selfmakename
	rm -f ./selfmakename
	$(CC) -DSELFTEST $(INCLUDES) -g $(srcdir)/macrocheck.c esb.o dwarf_tsearchbal.o -o selfmc
	./selfmc
	rm -f ./selfmc
	$(CC) -DSELFTEST -g $(srcdir)/esb.c -o selfesb
	./selfesb
	rm -f ./selfesb
	$(CC) -DSELFTEST -I../libdwarf -g section_bitmaps.c -o selfsection_bitmaps
	./selfsection_bitmaps
	rm -f ./selfsection_bitmaps
	$(CC) -DSELFTEST -I../libdwarf -g print_reloc.c esb.o -o selfprint_reloc
	./selfprint_reloc
	rm -f ./selfprint_reloc

# This simply assumes that a default INSTALL (cp) command
# will work and leave sensible permissions on the resulting files.
# Some adjustment might be required, see README.
install: all
	$(INSTALL) dwarfdump $(bindir)/dwarfdump
	$(INSTALL) $(srcdir)/dwarfdump.conf $(libdir)/dwarfdump.conf
	$(INSTALL) $(srcdir)/dwarfdump.1 $(man1dir)/dwarfdump.1

uninstall:
	-rm -f $(bindir)/dwarfdump
	-rm -f $(man1dir)/dwarfdump.1
	-rm -f $(libdir)/dwarfdump.conf

clean:
	rm -f *.o dwarfdump 
	rm -f _tag_attr_table.c 
	rm -f _tag_attr_ext_table.c 
	rm -f _tag_tree_table.c 
	rm -f _tag_tree_ext_table.c 
	-rm -f tag_attr_build*.tmp
	-rm -f tag_tree_build*.tmp
	rm -f tag_tree_build
	rm -f tag_attr_build
	-rm -f _*.c _*.h
	-rm -f tmp-*.c tmp-*.h tmp-*.tmp
	rm -f gennames
	rm -f dwarf_names_new.c
	rm -f dwarf_names_new.h
	rm -f dwarf_names_enum.h
	rm -f dwarf_names.h 
	rm -f dwarf_names.c
	rm -f a.out
	rm -f getopttest
	rm -f getopttestnat
	rm -f selfhelpertree
	rm -f selfmc
	rm -f *~

distclean: clean
	rm -f config.log config.h config.cache config.status 
	rm -rf autom4te.cache
	rm -rf Makefile

shar:
	@echo "shar not set up yet"
dist:
	@echo "dist not set up yet"
