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

# Executable
APP_NAME = csa

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

# Stripping tool
STRIP = strip

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

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

# Define C source files
SOURCES = $(wildcard src/*.c)

# Objects
OBJECTS = $(addprefix obj/,$(notdir $(SOURCES:%.c=%.o)))

# Dependencies
DEPS = $(OBJECTS:%.o=%.d)

.PHONY: clean directories

all: directories $(APP_NAME)

directories: obj

obj:
	mkdir -p obj

csa: $(OBJECTS)
	$(CC) $(CFLAGS) $(OBJECTS) -o $@ $(LDFLAGS)
	$(STRIP) $(APP_NAME)

-include $(DEPS)

obj/%.o: src/%.c
	$(CC) $(CFLAGS) -MMD -c $< -o $@

clean:
	$(RM) obj/*.o obj/*.d src/*~ $(APP_NAME)
