From ea8131fae723ff4ba5d183f38116abbaba7cd663 Mon Sep 17 00:00:00 2001 From: Stefan Reinauer Date: Fri, 20 Nov 2020 17:23:09 -0800 Subject: [PATCH] Add auto-dependency generation to Makefile Add automatic dependency generation to the Makefile. Previously, changes to any of the header files would cause a re-link, but not a rebuild of the source code files that include those headers. Signed-off-by: Stefan Reinauer --- .gitignore | 1 + Makefile | 25 +++++++++++++++++++++---- 2 files changed, 22 insertions(+), 4 deletions(-) diff --git a/.gitignore b/.gitignore index ef281e1..1b5a9b7 100644 --- a/.gitignore +++ b/.gitignore @@ -1,3 +1,4 @@ *~ *.o +.deps dpcmd diff --git a/Makefile b/Makefile index 0c22e16..7b764ed 100644 --- a/Makefile +++ b/Makefile @@ -16,13 +16,30 @@ LDFLAGS ?= LDFLAGS += -lpthread LDFLAGS += $(shell $(PKG_CONFIG) --libs libusb-1.0) -PROGRAMMER_OBJS += dpcmd.o usbdriver.o FlashCommand.o SerialFlash.o parse.o board.o project.o IntelHexFile.o MotorolaFile.o +DEPDIR := .deps +DEPFLAGS = -MT $@ -MMD -MP -MF $(DEPDIR)/$*.d -$(PROGRAM): $(PROGRAMMER_OBJS) *.h Makefile - $(CC) $(CFLAGS) -o $(PROGRAM) $(PROGRAMMER_OBJS) $(LDFLAGS) +SRCS = dpcmd.c usbdriver.c FlashCommand.c SerialFlash.c parse.c board.c project.c IntelHexFile.c MotorolaFile.c + +PROGRAMMER_OBJS := $(SRCS:%.c=%.o) + +$(PROGRAM): $(PROGRAMMER_OBJS) + $(CC) $(CFLAGS) $(CPPFLAGS) -o $@ $^ $(LDFLAGS) + +%.o : %.c +%.o : %.c $(DEPDIR)/%.d | $(DEPDIR) + $(CC) $(DEPFLAGS) $(CFLAGS) $(CPPFLAGS) -c -o $@ $< + +$(DEPDIR): ; @mkdir -p $@ + +DEPFILES := $(SRCS:%.c=$(DEPDIR)/%.d) +$(DEPFILES): + +include $(wildcard $(DEPFILES)) clean: - @rm -vf $(PROGRAM) $(PROGRAM).exe *.o *.d + rm -vf $(PROGRAM) $(PROGRAM).exe *.o + rm -rvf $(DEPDIR) install: $(PROGRAM) @[ $(shell id -u) -eq 0 ] || (echo "Error: install needs root privileges" && false)