From 2f851743e0cf21509e9bde08b22a9ffa5853eeaf Mon Sep 17 00:00:00 2001 From: Ivan Shapovalov Date: Tue, 3 Sep 2024 11:06:37 +0200 Subject: [PATCH] miniupnpd: Makefile: generate dep-files during compilation Implement the "Auto-Dependency Generation"[1] writeup by Paul D. Smith and generate dep-files at the compilation time instead of requiring a separate preprocessor pass. This removes the `depend` target as it is not needed anymore. [1]: https://make.mad-scientist.net/papers/advanced-auto-dependency-generation/ --- miniupnpd/Makefile.linux | 35 ++++++++++++++++------------------- miniupnpd/Makefile.linux_nft | 34 ++++++++++++++++------------------ 2 files changed, 32 insertions(+), 37 deletions(-) diff --git a/miniupnpd/Makefile.linux b/miniupnpd/Makefile.linux index b35e25f..b291c48 100644 --- a/miniupnpd/Makefile.linux +++ b/miniupnpd/Makefile.linux @@ -49,7 +49,8 @@ PKG_CONFIG ?= pkg-config CP = cp DOXYGEN ?= doxygen -DEPFLAGS = -MM -MG -MT $(patsubst %.d,%.o,$@) -MT $@ +DEPDIR := .depend +DEPFLAGS = -MMD -MP -MF $(@:%.o=$(DEPDIR)/%.d) -MT $@ # -M : with system headers, -MM : without INSTALLPREFIX ?= $(PREFIX)/usr @@ -66,8 +67,6 @@ NETFILTEROBJS = iptcrdr.o iptpinhole.o nfct_get.o ALLOBJS = $(BASEOBJS) $(LNXOBJS) $(NETFILTEROBJS) $(OTHEROBJS) -DEP = $(ALLOBJS:.o=.d) - NETFILTER_SCRIPTS = $(addprefix $(SRCDIR)/netfilter/, \ iptables_init.sh iptables_removeall.sh \ ip6tables_init.sh ip6tables_removeall.sh \ @@ -118,14 +117,14 @@ EXECUTABLES = miniupnpd testupnpdescgen testgetifstats \ testgetroute testasyncsendto testportinuse \ testssdppktgen testminissdp -.PHONY: all clean install depend dox +.PHONY: all clean install dox all: $(EXECUTABLES) clean: $(RM) config.h $(RM) $(ALLOBJS) - $(RM) $(DEP) + $(RM) -r $(DEPDIR) $(RM) $(EXECUTABLES) $(RM) testupnpdescgen.o testgetifstats.o $(RM) testupnppermissions.o testgetifaddr.o @@ -198,26 +197,24 @@ miniupnpdctl: miniupnpdctl.o config.mk config.h: $(SRCDIR)/configure $(SRCDIR)/VERSION $(SHELL) $< $(CONFIG_OPTIONS) -depend: $(DEP) - -%.d: $(SRCDIR)/%.c - $(CC) $(CPPFLAGS) $(DEPFLAGS) -o $@ $< - dox: $(SRCDIR)/miniupnpd.doxyconf (cat $< ; echo "INPUT=$(SRCDIR)" ) | $(DOXYGEN) - -%.o: $(SRCDIR)/%.c - $(CC) -c $(CFLAGS) $(CPPFLAGS) $< -o $@ +%.o: $(SRCDIR)/%.c $(DEPDIR)/%.d | $(DEPDIR) + $(CC) -c $(DEPFLAGS) $(CFLAGS) $(CPPFLAGS) $< -o $@ -%.o: $(SRCDIR)/linux/%.c - $(CC) -c $(CFLAGS) $(CPPFLAGS) $< -o $@ +%.o: $(SRCDIR)/linux/%.c $(DEPDIR)/%.d | $(DEPDIR) + $(CC) -c $(DEPFLAGS) $(CFLAGS) $(CPPFLAGS) $< -o $@ -%.o: $(SRCDIR)/netfilter/%.c - $(CC) -c $(CFLAGS) $(CPPFLAGS) $< -o $@ +%.o: $(SRCDIR)/netfilter/%.c $(DEPDIR)/%.d | $(DEPDIR) + $(CC) -c $(DEPFLAGS) $(CFLAGS) $(CPPFLAGS) $< -o $@ print-%: @echo "$* = $($*)" -ifneq ($(MAKECMDGOALS),clean) --include $(DEP) -endif + +DEPFILES := $(ALLOBJS:%.o=$(DEPDIR)/%.d) +$(DEPDIR): ; @mkdir -p $@ +$(DEPFILES): + +-include $(wildcard $(DEPFILES)) diff --git a/miniupnpd/Makefile.linux_nft b/miniupnpd/Makefile.linux_nft index 30b35e8..2043ede 100644 --- a/miniupnpd/Makefile.linux_nft +++ b/miniupnpd/Makefile.linux_nft @@ -44,7 +44,8 @@ PKG_CONFIG ?= pkg-config CP = cp DOXYGEN ?= doxygen -DEPFLAGS = -MM -MG -MT $(patsubst %.d,%.o,$@) -MT $@ +DEPDIR := .depend +DEPFLAGS = -MMD -MP -MF $(@:%.o=$(DEPDIR)/%.d) -MT $@ # -M : with system headers, -MM : without INSTALLPREFIX ?= $(PREFIX)/usr @@ -61,8 +62,6 @@ NETFILTEROBJS = nftnlrdr.o nftpinhole.o nfct_get.o nftnlrdr_misc.o ALLOBJS = $(BASEOBJS) $(LNXOBJS) $(NETFILTEROBJS) $(OTHEROBJS) -DEP = $(ALLOBJS:.o=.d) - NFT_SCRIPTS = $(addprefix $(SRCDIR)/netfilter_nft/scripts/, \ nft_init.sh nft_removeall.sh nft_flush.sh nft_delete_chain.sh) @@ -109,13 +108,14 @@ EXECUTABLES = miniupnpd miniupnpdctl \ testupnppermissions testgetifaddr \ testgetroute testasyncsendto testportinuse -.PHONY: all clean install depend dox +.PHONY: all clean install dox all: $(EXECUTABLES) clean: $(RM) config.h $(RM) $(ALLOBJS) + $(RM) -r $(DEPDIR) $(RM) $(EXECUTABLES) $(RM) testupnpdescgen.o testgetifstats.o $(RM) testupnppermissions.o testgetifaddr.o @@ -169,26 +169,24 @@ miniupnpdctl: miniupnpdctl.o config.mk config.h: $(SRCDIR)/configure $(SRCDIR)/VERSION $(SHELL) $< $(CONFIG_OPTIONS) -depend: $(DEP) - -%.d: $(SRCDIR)/%.c - $(CC) $(CPPFLAGS) $(DEPFLAGS) -o $@ $< - dox: $(SRCDIR)/miniupnpd.doxyconf (cat $< ; echo "INPUT=$(SRCDIR)" ) | $(DOXYGEN) - -%.o: $(SRCDIR)/%.c - $(CC) -c $(CFLAGS) $(CPPFLAGS) $< -o $@ +%.o: $(SRCDIR)/%.c $(DEPDIR)/%.d | $(DEPDIR) + $(CC) -c $(DEPFLAGS) $(CFLAGS) $(CPPFLAGS) $< -o $@ -%.o: $(SRCDIR)/linux/%.c - $(CC) -c $(CFLAGS) $(CPPFLAGS) $< -o $@ +%.o: $(SRCDIR)/linux/%.c $(DEPDIR)/%.d | $(DEPDIR) + $(CC) -c $(DEPFLAGS) $(CFLAGS) $(CPPFLAGS) $< -o $@ -%.o: $(SRCDIR)/netfilter_nft/%.c - $(CC) -c $(CFLAGS) $(CPPFLAGS) $< -o $@ +%.o: $(SRCDIR)/netfilter_nft/%.c $(DEPDIR)/%.d | $(DEPDIR) + $(CC) -c $(DEPFLAGS) $(CFLAGS) $(CPPFLAGS) $< -o $@ print-%: @echo "$* = $($*)" -ifneq ($(MAKECMDGOALS),clean) --include $(DEP) -endif + +DEPFILES := $(ALLOBJS:%.o=$(DEPDIR)/%.d) +$(DEPDIR): ; @mkdir -p $@ +$(DEPFILES): + +-include $(wildcard $(DEPFILES))