#
# 'make'        build executable file
# 'make clean'  removes all .o and executable files
#

# Executables
CLIENT_APP_NAME = src
SERVER_APP_NAME = ips

# C compiler to use
#CC = clang
CC = gcc

# Compile-time flags
CFLAGS  = -std=gnu11 -W -Wall -O2 -pipe -fomit-frame-pointer
CFLAGS += -Wno-missing-field-initializers
#CFLAGS += -Wno-unused-parameter
#CFLAGS += -Wno-unused-variable
#CFLAGS += -Wno-unused-function

# Define any libraries to link into executable:
LDFLAGS  = -lm -lrt
LDFLAGS += -lpthread
LDFLAGS += -s

# Objects
COMMON_OBJS = \
	objects/net_utils.o \
	objects/linked_list.o
CLIENT_OBJS = \
	objects/src.o
SERVER_OBJS = \
	objects/ips.o

# Dependencies
DEPS = $(COMMON_OBJS:%.o=%.d) $(CLIENT_OBJS:%.o=%.d) $(SERVER_OBJS:%.o=%.d)

.PHONY: clean directories

all: directories executables

directories: objects

objects:
	mkdir -p objects

executables: $(CLIENT_APP_NAME) $(SERVER_APP_NAME)

$(CLIENT_APP_NAME): $(COMMON_OBJS) $(CLIENT_OBJS)
	$(CC) $(CFLAGS) $(COMMON_OBJS) $(CLIENT_OBJS) -o $@ $(LDFLAGS)

$(SERVER_APP_NAME): $(COMMON_OBJS) $(SERVER_OBJS)
	$(CC) $(CFLAGS) $(COMMON_OBJS) $(SERVER_OBJS) -o $@ $(LDFLAGS)

-include $(DEPS)

objects/%.o: sources/%.c
	$(CC) $(CFLAGS) -MMD -c $< -o $@

clean:
	$(RM) objects/*.o objects/*.d sources/*~ $(CLIENT_APP_NAME) $(SERVER_APP_NAME)
