CC ?= gcc DEBUG ?= 0 CFLAGS = -Wall -Werror -Wextra -Wpedantic -std=c99 -Ofast \ -D_POSIX_C_SOURCE=200112L ifeq ($(DEBUG), 1) CFLAGS += -g -DDEBUG endif SRCDIR = ./src INCDIR = ./include OBJDIR = ./obj BINDIR = ./bin TESTDIR_SAMPLES = ./test-files TESTDIR = $(TESTDIR_SAMPLES)/corrupted SRC = $(wildcard $(SRCDIR)/*.c) OBJ = $(SRC:$(SRCDIR)/%.c=$(OBJDIR)/%.o) DEP = $(OBJ:.o=.d) PROGNAME_BASE = polonium PROGNAME_SUFFIX_DEBUG = debug EXECUTABLE = $(PROGNAME_BASE) ifeq ($(DEBUG), 1) EXECUTABLE := $(EXECUTABLE)_$(PROGNAME_SUFFIX_DEBUG) endif EXECPATH = $(BINDIR)/$(EXECUTABLE) all: $(EXECPATH) $(EXECPATH): $(OBJ) @mkdir -p $(@D) $(CC) $(CFLAGS) $^ -o $@ ifeq ($(DEBUG), 0) strip $@* endif -include $(DEP) $(OBJDIR)/%.o: $(SRCDIR)/%.c @mkdir -p $(@D) $(CC) $(CFLAGS) -I$(INCDIR) -MMD -MP -c $< -o $@ define rmfiles_if_exist @for file in $1; do \ if [ -e $$file ]; then \ printf -- "Removing file $$file\n"; \ rm $$file; \ fi; \ done endef define rmdirs @for dir in $1; do \ if [ -d "$$dir" ]; then \ if [ -z "$$(find "$$dir" -mindepth 1 -maxdepth 1 -print -quit)" ]; \ then \ printf -- "Removing empty directory $$dir\n"; \ else \ printf -- "Removing non-empty directory $$dir\n"; \ fi; \ rm -r $$dir; \ fi; \ done endef define rmdirs_if_empty @for dir in $1; do \ if [ -d "$$dir" ]; then \ if [ -z "$$(find "$$dir" -mindepth 1 -maxdepth 1 -print -quit)" ]; \ then \ printf -- "Removing empty directory $$dir\n"; \ rm -r $$dir; \ fi; \ fi; \ done endef FILES_TO_REMOVE = $(EXECPATH) $(DEP) $(OBJ) DIRECTORIES_TO_REMOVE = $(BINDIR) $(OBJDIR) clean: $(call rmfiles_if_exist,$(FILES_TO_REMOVE)) $(call rmdirs_if_empty,$(DIRECTORIES_TO_REMOVE)) distclean: testclean $(call rmdirs,$(DIRECTORIES_TO_REMOVE)) # Variables for testing EXEC_LOG = ./exec.log PROGRAM_ARGS := -probability 60 -threshold 2 -passes 2 -noconfirm -contents \ -seed 999999999 test: @if [ ! -e "$(EXECPATH)" ]; then \ printf -- "[!] Polonium executable (\`$(EXECUTABLE)\`) is not \ available, attempting to build\n"; \ $(MAKE) all; \ exit 0; \ fi @printf -- "[*] Generating corrupted versions of test files\n" @mkdir -p $(TESTDIR) @find $(TESTDIR_SAMPLES) -maxdepth 1 -type f -name 'test.*' | while IFS= \ read -r file; do \ basename=$$(basename "$$file"); \ workfile="$(TESTDIR)/$$basename"; \ cp "$$file" "$$workfile" || exit 1; \ printf -- "--- [*] $(EXECUTABLE) $(basename "$2")$(PROGRAM_ARGS)\n"; \ $(EXECPATH) "$$workfile" $(PROGRAM_ARGS) > $(EXEC_LOG) 2>&1; \ if [ "$$?" -ne "0" ]; then \ printf -- "[!] Polonium failed:\n"; \ cat $(EXEC_LOG); \ rm $(EXEC_LOG); \ exit 1; \ fi; \ rm $(EXEC_LOG); \ done @printf "[>] All done!\n" testclean: $(call rmdirs,$(TESTDIR)) .PHONY: all clean distclean test testclean