From b64c21f7eb3a8b844ec4c827275dc17590d653fa Mon Sep 17 00:00:00 2001
From: Maksim Melnikov <m.melnikov@postgrespro.ru>
Date: Thu, 27 Aug 2026 13:17:20 +0300
Subject: [PATCH v3] Fix race with LLVM and bison.

Ensure that each .bc files for MODULES depends on the corresponding .o file,
to ensure the dependencies required for it to be built are present.

Author: Andres Freund <andres@anarazel.de>
Discussion: https://www.postgresql.org/message-id/f070a61e-1dd3-4fd9-bbc9-18d100b6722a%40postgrespro.ru
---
 src/backend/common.mk | 3 +++
 src/makefiles/pgxs.mk | 5 +++++
 2 files changed, 8 insertions(+)

diff --git a/src/backend/common.mk b/src/backend/common.mk
index 61861f5c7eb..85bb4ee2527 100644
--- a/src/backend/common.mk
+++ b/src/backend/common.mk
@@ -22,7 +22,10 @@ objfiles.txt: Makefile $(SUBDIROBJS) $(OBJS)
 	$(if $(filter-out $(OBJS),$?),( $(if $(SUBDIROBJS),cat $(SUBDIROBJS); )echo $(addprefix $(subdir)/,$(OBJS)) ) >$@,touch $@)
 
 ifeq ($(with_llvm), yes)
+# Ensure that .bc files get built when building .o files
 objfiles.txt: $(patsubst %.o,%.bc, $(OBJS))
+# Ensure that each .bc file depends on the corresponding .o file, to ensure
+# the dependencies required for it to be built are present.
 $(patsubst %.o,%.bc, $(OBJS)): $(OBJS)
 endif
 
diff --git a/src/makefiles/pgxs.mk b/src/makefiles/pgxs.mk
index 039cee3dfe5..d7732024b15 100644
--- a/src/makefiles/pgxs.mk
+++ b/src/makefiles/pgxs.mk
@@ -219,7 +219,12 @@ endef
 all: $(PROGRAM) $(DATA_built) $(HEADER_allbuilt) $(SCRIPTS_built) $(addsuffix $(DLSUFFIX), $(MODULES)) $(addsuffix .control, $(EXTENSION))
 
 ifeq ($(with_llvm), yes)
+# Ensure that .bc files for MODULES and OBJS get built with all
 all: $(addsuffix .bc, $(MODULES)) $(patsubst %.o,%.bc, $(OBJS))
+# Ensure that each .bc file depends on the corresponding .o file, to ensure
+# the dependencies required for it to be built are present.
+$(patsubst %.o,%.bc, $(OBJS)): $(OBJS)
+$(addsuffix .bc, $(MODULES)): $(addsuffix .o, $(MODULES))
 endif
 
 ifdef MODULE_big
-- 
2.43.0

