
CC=gcc
CFLAGS=-std=c99 -pedantic -Wall -D_DEFAULT_SOURCE -D_BSD_SOURCE -D_SVID_SOURCE -D_POSIX_C_SOURCE=200809L

.PHONY: all clean
all: default
default: bin intercept.so main_intercept

bin:
	mkdir -p bin/

bin/main.o: ../test1/src/main.c
	$(CC) -c -o $@ $^ $(CFLAGS)

bin/intercept_preload.o: src/intercept.c
	$(CC) -fPIC -c -o $@ $^ $(CFLAGS) -DINTERCEPT_PRELOAD

intercept.so: bin/intercept_preload.o
	$(CC) -shared -o $@ $^ $(CFLAGS) -lc -ldl

main_intercept: bin/main.o src/intercept.c
	$(CC) -o $@ $^ $(CFLAGS) -lc -Wl,--wrap=malloc,--wrap=free,--wrap=calloc,--wrap=realloc,--wrap=reallocarray,--wrap=getopt,--wrap=exit,\
--wrap=read,--wrap=pread,--wrap=write,--wrap=pwrite,--wrap=close,--wrap=sigaction,\
--wrap=sem_init,--wrap=sem_open,--wrap=sem_post,--wrap=sem_wait,--wrap=sem_trywait,--wrap=sem_timedwait,--wrap=sem_getvalue,--wrap=sem_close,--wrap=sem_unlink,--wrap=sem_destroy,\
--wrap=shm_open,--wrap=shm_unlink,--wrap=mmap,--wrap=munmap,\
--wrap=ftruncate,--wrap=fork,--wrap=wait,--wrap=waitpid,--wrap=pipe,--wrap=dup,--wrap=dup2,--wrap=dup3,\
--wrap=execl,--wrap=execlp,--wrap=execle,--wrap=execv,--wrap=execvp,--wrap=execvpe,--wrap=execve,--wrap=fexecve,\
--wrap=socket,--wrap=bind,--wrap=listen,--wrap=accept,--wrap=connect,--wrap=getaddrinfo,--wrap=freeaddrinfo,\
--wrap=send,--wrap=sendto,--wrap=sendmsg,--wrap=recv,--wrap=recvfrom,--wrap=recvmsg,\
--wrap=getline,--wrap=getdelim

clean:
	rm -rf main_intercept bin/* *.so *.ko *.o
