# Makefile for Checker
# 
# If you already have morecore(), remove morecore.o from OBJS
#

# Your compiler
CC = gcc
AS = as

# Flags for your compiler.
# Those are for Gcc
# O2 means high optimization and Wall enables (nearly) all warnings.
# please, do not add -fomit-frame-pointer
CPPFLAGS = -I. -I./chkrlib
CFLAGS = -O2 -Wall -g

# LIB is the name of the library file
# LIBOBJ is an object file, that contains all the library for safety link.
LIB= libchecker.a
LIBOBJ= libchecker.o
LIBMCHECK = libmcheck.a
LIBCHKRLIB = chkrlib/libchkrlib.o

OBJS  = malloc.o free.o realloc.o calloc.o valloc.o memalign.o \
	mstats.o cfree.o garbage.o checker.o errlist.o find_exec.o \
	sysdep.o symtab.o maccess.o addrextract.o morecore.o

OBJS1 = mcheck.o mtrace.o

TARGET=$(LIB) $(LIBMCHECK)

all: $(TARGET) try_checker try_detector try_maccess

find_exec.o: find_exec.c
	$(CC) $(subst -O2,,$(CFLAGS)) $(CPPFLAGS) -c find_exec.c

$(LIB): .depend $(OBJS) $(LIBCHKRLIB)
	$(AR) usvc $(LIB) $(OBJS) $(LIBCHKRLIB)
#	ranlib $(LIB)

$(LIBOBJ): .depend $(OBJS) $(LIBCHKRLIB) export checker.export
	$(LD) -r -o $(LIBOBJ) $(OBJS) $(LIBCHKRLIB) -static
	./export -v $(LIBOBJ) checker.export

$(LIBMCHECK): mcheck-init.o
	$(AR) uvc $(LIBMCHECK) mcheck-init.o

$(LIBCHKRLIB):
	(cd chkrlib && $(MAKE))

try_checker: try_checker.o $(LIBOBJ)
	gcc -o try_checker try_checker.o $(LIBOBJ) libtermcap.a

try_detector: try_detector.o $(LIBOBJ)
	gcc -o try_detector try_detector.o $(LIBOBJ) -static

try_maccess.o: try_maccess.c
	gcc -checker -c try_maccess.c -o try_maccess.o

try_maccess: try_maccess.o $(LIBOBJ)
	gcc -checker try_maccess.o -o try_maccess $(LIBOBJ) -static

export.o: export.c
	gcc -c export.c -O -Wall -g

export: export.o
	gcc -o export export.o


.depend: Makefile
	@echo "You have not make the dependency file"	# have a beep
	@echo "Try 'make dep'"

obj: $(OBJS)

clean:
	(cd chkrlib && $(MAKE) clean)
	$(RM) -f core *.o a.out try_checker try_detector try_maccess export
	$(RM) -f $(LIBMCHECK) $(LIB) 
	$(RM) -f .depend *~ sysdep/*~ sysdep/a.out example

dep:
	$(CPP) -M *.c $(CPPFLAGS) > .depend
	(cd chkrlib && $(MAKE) dep)

#
# include a dependency file if one exists
#
ifeq (.depend,$(wildcard .depend))
include .depend
endif
