# Makefile for Checker
# 
#

# 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 \
	machine.o symtab.o maccess.o addrextract.o chkr-syscall.o \
	parse-args.o

OBJS1 = mcheck.o mtrace.o

TARGET=$(LIBOBJ) $(LIBMCHECK)

all: $(TARGET) testsuit

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))

testsuit: $(LIBOBJ)
	cd testsuit; $(MAKE)

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
	cd testsuit && $(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
