Skip to content

Commit cb65cb3

Browse files
committed
Add automatic header-dependency tracking to the Makefile
AGE lists OBJS explicitly and relies on PGXS, whose built-in dependency tracking only runs when the server was built with --enable-depend (often disabled). Consequently, editing a header did not recompile the .c files that include it, leaving stale .o files. This is especially dangerous for node/struct headers: a stale ag_nodes.o keeps an outdated node_size, so _readExtensibleNode under-allocates and readNode corrupts the heap ("unrecognized node type"). Emit a .d file beside each object via -MMD -MP and -include them, deriving DEPFILES from OBJS. The mechanism is self-contained (independent of --enable-depend): -MMD skips system headers and -MP tolerates deleted headers. On servers built with --enable-depend, PGXS appends its own -MF after CFLAGS (last -MF wins), so this degrades cleanly to PGXS's tracking. Add DEPFILES to EXTRA_CLEAN and *.d to .gitignore. Co-authored-by: Copilot <copilot@github.com> modified: .gitignore modified: Makefile
1 parent c1617a2 commit cb65cb3

2 files changed

Lines changed: 24 additions & 1 deletion

File tree

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
*.o
2+
*.d
23
*.so
34
build.sh
45
.idea

Makefile

Lines changed: 23 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -162,6 +162,10 @@ OBJS = src/backend/age.o \
162162
src/backend/utils/name_validation.o \
163163
src/backend/utils/ag_guc.o
164164

165+
# Per-object header-dependency files (see "Automatic header-dependency
166+
# tracking" below the PGXS include). One .d is generated beside each .o.
167+
DEPFILES = $(OBJS:.o=.d)
168+
165169
# ===== Extension SQL & data files =====
166170
EXTENSION = age
167171

@@ -258,7 +262,8 @@ EXTRA_CLEAN = $(addprefix $(ag_regress_dir)/, $(ag_regress_out)) \
258262
$(all_age_sql) \
259263
$(age_init_sql) \
260264
$(age_upgrade_test_sql) \
261-
$(ag_regress_dir)/age_upgrade_cleanup.sh
265+
$(ag_regress_dir)/age_upgrade_cleanup.sh \
266+
$(DEPFILES)
262267

263268
GEN_KEYWORDLIST = $(PERL) -I ./tools/ ./tools/gen_keywordlist.pl
264269
GEN_KEYWORDLIST_DEPS = ./tools/gen_keywordlist.pl tools/PerfectHash.pm
@@ -271,6 +276,23 @@ PG_CONFIG ?= pg_config
271276
PGXS := $(shell $(PG_CONFIG) --pgxs)
272277
include $(PGXS)
273278

279+
# ===== Automatic header-dependency tracking =====
280+
#
281+
# AGE lists OBJS explicitly, and PGXS's built-in .deps tracking only runs when
282+
# the *server* was built with --enable-depend (often off). Without the lines
283+
# below, editing a header does NOT rebuild the .c files that include it, leaving
284+
# STALE .o files. This is especially dangerous for node/struct headers: a stale
285+
# ag_nodes.o keeps an old node_size, so _readExtensibleNode under-allocates and
286+
# readNode corrupts the heap ("unrecognized node type: <garbage>").
287+
#
288+
# The compiler emits a .d file next to each object (-MMD = user headers only;
289+
# -MP adds phony targets so deleting a header does not break the build). With
290+
# "-o foo.o", -MMD writes "foo.d" automatically (no -MF, no basename clashes).
291+
# On servers that DO set --enable-depend, PGXS appends its own "-MF .deps/*.Po"
292+
# after $(CFLAGS) (last -MF wins), so this degrades cleanly to that mechanism.
293+
override CFLAGS += -MMD -MP
294+
-include $(DEPFILES)
295+
274296
# ===== Build rules =====
275297

276298
# 32-bit platform support: pass SIZEOF_DATUM=4 to enable (e.g., make SIZEOF_DATUM=4)

0 commit comments

Comments
 (0)