From ae20140e651f14721cf870b3910421135dbcc3d5 Mon Sep 17 00:00:00 2001
From: Tavian Barnes <tavianator@tavianator.com>
Date: Mon, 18 Apr 2022 22:13:03 -0400
Subject: Makefile: Use a recipe to update .flags rather than $(shell)

This means we don't need to generate .flags unless we're actually
building a target that needs it, which is important for thing like

    $ sudo make install

that used to inconveniently create a root-owned .flags file.
---
 Makefile | 12 ++++--------
 1 file changed, 4 insertions(+), 8 deletions(-)

(limited to 'Makefile')

diff --git a/Makefile b/Makefile
index d2c0258..8fe13a7 100644
--- a/Makefile
+++ b/Makefile
@@ -169,10 +169,6 @@ ALL_CFLAGS = $(ALL_CPPFLAGS) $(LOCAL_CFLAGS) $(CFLAGS) $(EXTRA_CFLAGS) $(DEPFLAG
 ALL_LDFLAGS = $(ALL_CFLAGS) $(LOCAL_LDFLAGS) $(LDFLAGS) $(EXTRA_LDFLAGS)
 ALL_LDLIBS = $(LOCAL_LDLIBS) $(LDLIBS) $(EXTRA_LDLIBS)
 
-# Save the full set of flags to rebuild everything when they change
-ALL_FLAGS := $(CC) : $(ALL_CFLAGS) : $(ALL_LDFLAGS) : $(ALL_LDLIBS)
-$(shell ./flags.sh $(ALL_FLAGS))
-
 # Goals that make binaries
 BIN_GOALS := bfs build/tests/mksock build/tests/trie build/tests/xtimegm
 
@@ -241,9 +237,9 @@ build/%.o: src/%.c .flags | build
 build/tests/%.o: tests/%.c .flags | build/tests
 	$(CC) $(ALL_CFLAGS) -c $< -o $@
 
-# Need a rule for .flags to convince make to apply the above pattern rule if
-# .flags didn't exist when make was run
-.flags:
+# Save the full set of flags to rebuild everything when they change
+.flags: FORCE
+	@./flags.sh $(CC) : $(ALL_CFLAGS) : $(ALL_LDFLAGS) : $(ALL_LDLIBS)
 
 # Make sure that "make release" builds everything, but "make release main.o" doesn't
 $(FLAG_GOALS): $(FLAG_PREREQS)
@@ -284,7 +280,7 @@ uninstall:
 	$(RM) $(DESTDIR)$(MANDIR)/man1/bfs.1
 	$(RM) $(DESTDIR)$(PREFIX)/bin/bfs
 
-.PHONY: default all $(FLAG_GOALS) check $(CHECKS) distcheck clean install uninstall
+.PHONY: default all $(FLAG_GOALS) check $(CHECKS) distcheck clean install uninstall FORCE
 
 .SUFFIXES:
 
-- 
cgit v1.2.3