.PHONY:all clean
MKDIR = mkdir
RM = rm
RMFLAGS = -rf
CC = gcc
DIR_OBJS = objs
DIR_EXES = exes
DIR_DEPS = deps
DIRS = $(DIR_OBJS) $(DIR_EXES) $(DIR_DEPS)
EXE = exe
EXE := $(addprefix $(DIR_EXES)/,$(EXE))
SRCS = $(wildcard *.c)
OBJS = $(SRCS:.c=.o)
OBJS := $(addprefix $(DIR_OBJS)/,$(OBJS))
DEPS = $(SRCS:.c=.dep)
DEPS := $(addprefix $(DIR_DEPS)/, $(DEPS))
all: $(EXE)
ifneq ($(MAKECMDGOALS),clean)
-include $(DEPS)
endif
$(DIRS):
$(MKDIR) $@
$(EXE):$(DIR_EXES) $(OBJS)
$(CC) -o $@ $(filter %.o,$^)
$(DIR_OBJS)/%.o: $(DIR_OBJS) %.c
$(CC) -o $@ -c $(filter %.c,$^)
$(DIR_DEPS)/%.dep: $(DIR_DEPS) %.c
@echo "making $@..."
@set -e; \
$(RM) $(RMFLAGS) $@.tmp; \
$(CC) -E -MM $(filter %.c,$^) > $@.tmp; \
sed 's,\(.*\)\.o[:]*,objs/\1.o $@:,g' < $@.tmp > $@; \
$(RM) $(RMFLAGS) $@.tmp
clean:
$(RM) $(RMFLAGS) $(DIRS)