# Compiler
CC = gcc 

# Debug or Release
DEBUG = -g
RELEASE = -O3 -DNDEBUG
EXEC = ${RELEASE}

#DEFINES = -D REUSEFDS

# Compiler options
CFLAGS = -pipe -fno-exceptions -fstack-protector -Wl,-z,relro -Wl,-z,now\
		-W -Wall -Wno-unused-parameter\
		-Wno-unused-function -Wno-unused-label -Wpointer-arith -Wformat\
		-Wreturn-type -Wsign-compare -Wmultichar -Wformat-nonliteral\
		-Winit-self -Wuninitialized -Wno-deprecated -Wformat-security -Werror\
		-pedantic -pedantic-errors ${EXEC} -fPIC\
		-fvisibility=hidden  ${DEFINES}

CVER = -std=c99

# FLAGS
FLAGS_VALGRIND = -DRUPIFY
LINKER_FLAGS = -lgmp -lrt

# Log name
LOG = VALGRIND_LOG

# Executeable name
NAME = TestStream

SRC = stream.c main.c f_stream.c bbs.c alloc.c b_stream.c m_stream.c\
	 stream_common.c hashmap.c

OBJ = ${SRC:.c=.o}

# what we are trying to build
all: $(NAME)

library: ${OBJ}
	gcc -shared alloc.o b_stream.o f_stream.o m_stream.o stream_common.o stream.o hashmap.o -o libstreams.so


# linkage
$(NAME): ${OBJ}
	@echo 
	@echo ================ [Linking] ================ 
	@echo
	$(CC) ${CFLAGS} ${CVER} $(FLAGS_VALGRIND) -o $@ $^ ${LINKER_FLAGS}
	@echo
	@echo ================ [${NAME} compiled succesfully] ================ 
	@echo

# compile every source file
%.o: %.c
	@echo
	@echo ================ [Building Object] ================
	@echo
	$(CC) $(CFLAGS) $(CVER) -c $< -o $@
	@echo
	@echo OK [$<] - [$@]
	@echo

valgrind: clean all
	@echo
	@echo ================ [Executing $(NAME) using Valgrind] ================
	@echo
	valgrind -v --leak-check=full --log-file="$(LOG)" --track-origins=yes \
	--show-reachable=yes ./$(NAME) -l
	@echo
	@echo ================ [Log] ================
	@echo
	less $(LOG)
	@echo

run: clean all
	@echo
	@echo ================ [Executing $(NAME)] ================
	@echo
	./$(NAME) -l

clean:
	@echo
	@echo ================ [Cleaning $(NAME)] ================
	@echo
	rm -f *.o *~ libstreams.so SERVER_ERROR SERVER_TRACE CLIENT_ERROR CLIENT_TRACE $(LOG) $(NAME)

count:
	@echo
	@echo ================ [Counting lines in $(NAME)] ================
	@echo
	sloccount .
