BINUTILS  = $(HOME)/src/binutils-2.21
ZLIB      = $(HOME)/src/zlib-1.2.5
LIBIBERTY = $(BINUTILS)/libiberty
BINUTIL   = $(BINUTILS)/binutils
OPCODES   = $(BINUTILS)/opcodes
BFD       = $(BINUTILS)/bfd
BIN       = ~/bin

ARGS    = \
 -I. -I$(BINUTILS)/include -I$(BINUTIL) -I$(BFD) \
 -DHAVE_CONFIG_H \
 -D_GNU_SOURCE  \
 -DLOCALEDIR="\"/usr/local/share/locale\"" \
 -Dbin_dummy_emulation=bin_vanilla_emulation \
 -D_LARGEFILE64_SOURCE \
 -DDISASSEMBLER_NEEDS_RELOCS 

CFLAGS  = -g -I. -I$(BINUTILS)/include -Wall

BIN     =~/bin

DEMANGLE_SRC = \
 $(LIBIBERTY)/xexit.c \
 $(LIBIBERTY)/xmalloc.c \
 $(LIBIBERTY)/xstrdup.c \
 $(LIBIBERTY)/safe-ctype.c \
 $(LIBIBERTY)/dyn-string.c \
 $(LIBIBERTY)/cp-demangle.c \
 $(LIBIBERTY)/cplus-dem.c 

DEMANGLE_OBJECTS = \
 $(LIBIBERTY)/xexit.o \
 $(LIBIBERTY)/xmalloc.o \
 $(LIBIBERTY)/xstrdup.o \
 $(LIBIBERTY)/safe-ctype.o \
 $(LIBIBERTY)/dyn-string.o \
 $(LIBIBERTY)/cp-demangle.o \
 $(LIBIBERTY)/cplus-dem.o 

ASXO_SOURCES = \
 asxo/asxo.c \
 $(BINUTIL)/debug.c \
 $(BINUTIL)/stabs.c \
 $(BINUTIL)/ieee.c \
 $(BINUTIL)/rdcoff.c \
 $(BINUTIL)/bucomm.c \
 $(BINUTIL)/filemode.c \
 $(OPCODES)/disassemble.c \
 $(OPCODES)/dis-buf.c  \
 $(OPCODES)/dis-init.c \
 $(OPCODES)/i386-dis.c

ASXO_OBJECTS = \
 $(BINUTIL)/debug.o \
 $(BINUTIL)/stabs.o \
 $(BINUTIL)/ieee.o \
 $(BINUTIL)/rdcoff.o \
 $(BINUTIL)/bucomm.o \
 $(BINUTIL)/filemode.o \
 $(OPCODES)/disassemble.o \
 $(OPCODES)/dis-buf.o  \
 $(OPCODES)/dis-init.o \
 $(OPCODES)/i386-dis.o \
 asxo/asxo.o

CX_SOURCES = \
 cx/cx.c

CX_OBJECTS = \
 cx/cx.o

# N.B. Early versions of binutils don't need zlib

LIBRARIES = \
 $(BFD)/libbfd.a \
 $(LIBIBERTY)/libiberty.a \
 $(ZLIB)/libz.a 


SOURCES = xmalloc.cpp util.cpp label.cpp dwarf.cpp sanity.cpp entity.cpp directory.cpp collection.cpp source.cpp file.cpp template.cpp class.cpp classmember.cpp variable.cpp function.cpp edge.cpp signature_buffer.cpp signature.cpp asx.cpp

OBJECTS = xmalloc.o util.o label.o dwarf.o sanity.o entity.o archive.o directory.o source.o collection.o file.o template.o class.o classmember.o variable.o function.o edge.o signature_buffer.o signature.o asx.o

all : asx test_demangler

asx : $(DEMANGLE_OBJECTS) $(ASXO_OBJECTS) $(CX_OBJECTS) $(OBJECTS) $(LIBRARIES)
	g++ -o asx $(CFLAGS) $(DEMANGLE_OBJECTS) $(ASXO_OBJECTS) $(CX_OBJECTS) $(OBJECTS) $(LIBRARIES)

asx.o : asx.cpp
	g++ -c $(CFLAGS) asx.cpp

signature.o : signature.cpp
	g++ -c $(CFLAGS) signature.cpp

signature_buffer.o : signature_buffer.cpp
	g++ -c $(CFLAGS) signature_buffer.cpp

edge.o : edge.cpp
	g++ -c $(CFLAGS) edge.cpp

function.o : function.cpp
	g++ -c $(CFLAGS) function.cpp

variable.o : variable.cpp
	g++ -c $(CFLAGS) variable.cpp

file.o : file.cpp
	g++ -c $(CFLAGS) file.cpp

source.o : source.cpp
	g++ -c $(CFLAGS) source.cpp

collection.o : collection.cpp
	g++ -c $(CFLAGS) collection.cpp

directory.o : directory.cpp
	g++ -c $(CFLAGS) directory.cpp

archive.o : archive.cpp
	g++ -c $(CFLAGS) archive.cpp

entity.o : entity.cpp
	g++ -c $(CFLAGS) entity.cpp

classmember.o : classmember.cpp
	g++ -c $(CFLAGS) classmember.cpp

class.o : class.cpp
	g++ -c $(CFLAGS) class.cpp

template.o : template.cpp
	g++ -c $(CFLAGS) template.cpp

util.o : util.cpp
	g++ -c $(CFLAGS) util.cpp

xmalloc.o : xmalloc.cpp
	g++ -c $(CFLAGS) xmalloc.cpp

label.o : label.cpp
	g++ -c $(CFLAGS) label.cpp

dwarf.o : dwarf.cpp
	g++ -c $(CFLAGS) dwarf.cpp

sanity.o : sanity.cpp
	g++ -c $(CFLAGS) sanity.cpp

cx/cx.o : cx/cx.c
	cd cx; make cx.o

$(LIBIBERTY)/cplus-dem.o : $(LIBIBERTY)/cplus-dem.c
	cd $(LIBIBERTY); make cplus-dem.o

$(LIBIBERTY)/cp-demangle.o : $(LIBIBERTY)/cp-demangle.c
	cd $(LIBIBERTY); make cp-demangle.o

$(LIBIBERTY)/dyn-string.o : $(LIBIBERTY)/dyn-string.c
	cd $(LIBIBERTY); make dyn-string.o

$(LIBIBERTY)/safe-ctype.o : $(LIBIBERTY)/safe-ctype.c
	cd $(LIBIBERTY); make safe-ctype.o

$(LIBIBERTY)/xmalloc.o : $(LIBIBERTY)/xmalloc.c
	cd $(LIBIBERTY); make xmalloc.o

$(LIBIBERTY)/xexit.o : $(LIBIBERTY)/xexit.c
	cd $(LIBIBERTY); make xexit.o

$(LIBIBERTY)/xstrdup.o : $(LIBIBERTY)/xstrdup.c
	cd $(LIBIBERTY); make xstrdup.o

asxo/asxo.o : asxo/asxo.c
	cd asxo; make asxo.o

$(BINUTIL)/budemang.o : $(BINUTIL)/budemang.c
	cd $(BINUTIL); make budemang.o

$(BINUTIL)/prdbg.o : $(BINUTIL)/prdbg.c
	cd $(BINUTIL); make prdbg.o

$(BINUTIL)/rddbg.o : $(BINUTIL)/rddbg.c
	cd $(BINUTIL); make rddbg.o

$(BINUTIL)/debug.o : $(BINUTIL)/debug.c
	cd $(BINUTIL); make debug.o

$(BINUTIL)/stabs.o : $(BINUTIL)/stabs.c
	cd $(BINUTIL); make stabs.o

$(BINUTIL)/ieee.o : $(BINUTIL)/ieee.c
	cd $(BINUTIL); make ieee.o

$(BINUTIL)/rdcoff.o : $(BINUTIL)/rdcoff.c
	cd $(BINUTIL); make rdcoff.o

$(BINUTIL)/bucomm.o : $(BINUTIL)/bucomm.c
	cd $(BINUTIL); make bucomm.o

$(BINUTIL)/version.o : $(BINUTIL)/version.c
	cd $(BINUTIL); make version.o

$(BINUTIL)/filemode.o : $(BINUTIL)/filemode.c
	cd $(BINUTIL); make filemode.o

$(OPCODES)/disassemble.o : $(OPCODES)/disassemble.c
	cd $(OPCODES); make disassemble.o

$(OPCODES)/dis-buf.o : $(OPCODES)/dis-buf.c
	cd $(OPCODES); make dis-buf.o

$(OPCODES)/dis-init.o : $(OPCODES)/dis-init.c
	cd $(OPCODES); make dis-init.o

$(OPCODES)/i386-dis.o : $(OPCODES)/i386-dis.c
	cd $(OPCODES); make i386-dis.o

$(BFD)/libbfd.a :
	cd $(BFD); make libbfd.a

$(LIBIBERTY)/libiberty.a :
	cd $(LIBIBERTY); make libiberty.a

test_demangler : xmalloc.o util.o signature_buffer.o signature.o test_demangler.cpp
	g++ -o test_demangler $(CFLAGS) $(DEMANGLE_OBJECTS) xmalloc.o util.o signature_buffer.o signature.o test_demangler.cpp $(LIBIBERTY)/libiberty.a

tests : asx.ta asx1.ta asx2.ta asx3.ta asx4.ta asx5.ta

# Produce ta from a history file

asx.ta: asx asx.history
	/bin/rm -f asx.ta
	./asx "--ASX_FORCE=v" < asx.history > asx.ta

# Produce ta as if doing a compile

asx1.ta: asx
	/bin/rm -f asx1.ta
	./asx $(CFLAGS) ./*.c* $(ARGS) $(CX_SOURCES) $(ASXO_SOURCES) $(DEMANGLE_SRC) > asx1.ta

asx2.ta: asx
	/bin/rm -f asx2.ta
	./asx $(DEMANGLE_OBJECTS) $(ASXO_OBJECTS) $(OBJECTS) $(LIBRARIES) > asx2.ta 

# Extract ta from an executable

asx3.ta: asx
	/bin/rm -f asx3.ta
	./asx asx > asx3.ta 

# Consider the libraries

asx4.ta: asx
	/bin/rm -f asx4.ta
	./asx $(OPCODES)/.libs/libopcodes.a > asx4.ta

asx5.ta: asx
	/bin/rm -f asx5.ta
	./asx $(CFLAGS) $(SOURCES) $(ARGS) $(DEMANGLE_SRC) > asx5.ta

# Simple extraction from source files
# Obviously you don't have the source files to perform this test
# but you can find similar simple code that compiles without
# special compiler options.

asx6.ta: asx
	./asx ~ijdavis/c488/*.c > asx6.ta

install: asx
	cp asx $(BIN)/asx
	chmod 755 $(BIN)/asx

clean:
	/bin/rm -f *.o asxo/*.o cx/*.o *.s asx test_demangler

# The following are commands for executing other products that perform
# similar functionality to asx. They may be of interest to the viewer.

# You will need to install tags to run this
# Doesn't handle function calls and variable usage

tags: Makefile
	cpp  -I. -I/home/ijdavis/src/asx/binutils-2.16.1/include asx.cpp > asx.ii
	ctags-5.6/ctags --options=NONE -f - -n -u --line-directives=yes --fields=afikmsS --c-kinds=cflnv --c++-kinds=cflnv --langmap=c:+.i,c++:+.ii asx.ii > tags

# You will need to install cscope-15.5 to run this
# Not sure what its usefulness is

tags1: Makefile
	cscope-15.5/src/cscope -I. -I/home/ijdavis/src/asx/binutils-2.16.1/include -b -c -f tags1 asx.cpp

# You will need to install cflow to run this
# Doesn't handle C++

tags2: Makefile
	cflow-1.1/src/cflow --include=_x --cpp -I. -I/home/ijdavis/src/asx/binutils-2.16.1/include asx.cpp > tags2
	
# You will need to install global-5.2 to run this

tags3: Makefile
	global-5.2/global/global asx.cpp

