8000 Fix make rules that generate multiple output files. · tomdcc/postgres@3bd5009 · GitHub
[go: up one dir, main page]

Skip to content

Commit 3bd5009

Browse files
committed
Fix make rules that generate multiple output files.
For years, our makefiles have correctly observed that "there is no correct way to write a rule that generates two files". However, what we did is to provide empty rules that "generate" the secondary output files from the primary one, and that's not right either. Depending on the details of the creating process, the primary file might end up timestamped later than one or more secondary files, causing subsequent make runs to consider the secondary file(s) out of date. That's harmless in a plain build, since make will just re-execute the empty rule and nothing happens. But it's fatal in a VPATH build, since make will expect the secondary file to be rebuilt in the build directory. This would manifest as "file not found" failures during VPATH builds from tarballs, if we were ever unlucky enough to ship a tarball with apparently out-of-date secondary files. (It's not clear whether that has ever actually happened, but it definitely could.) To ensure that secondary output files have timestamps >= their primary's, change our makefile convention to be that we provide a "touch $@" action not an empty rule. Also, make sure that this rule actually gets invoked during a distprep run, else the hazard remains. It's been like this a long time, so back-patch to all supported branches. In HEAD, I skipped the changes in src/backend/catalog/Makefile, because those rules are due to get replaced soon in the bootstrap data format patch, and there seems no need to create a merge issue for that patch. If for some reason we fail to land that patch in v11, we'll need to back-fill the changes in that one makefile from v10. Discussion: https://postgr.es/m/18556.1521668179@sss.pgh.pa.us
1 parent 46f8080 commit 3bd5009

File tree

9 files changed

+43
-33
lines changed

9 files changed

+43
-33
lines changed

src/Makefile.shlib

Lines changed: 3 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -358,13 +358,9 @@ else # PORTNAME == aix
358358

359359
# AIX case
360360

361-
# There is no correct way to write a rule that generates two files.
362-
# Rules with two targets don't have that meaning, they are merely
363-
# shorthand for two otherwise separate rules. To be safe for parallel
364-
# make, we must chain the dependencies like this. The semicolon is
365-
# important, otherwise make will choose some built-in rule.
366-
367-
$(stlib): $(shlib) ;
361+
# See notes in src/backend/parser/Makefile about the following two rules
362+
$(stlib): $(shlib)
363+
touch $@
368364

369365
$(shlib): $(OBJS) | $(SHLIB_PREREQS)
370366
rm -f $(stlib)

src/backend/Makefile

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -141,6 +141,12 @@ postgres.o: $(OBJS)
141141
# The following targets are specified in make commands that appear in
142142
# the make files in our subdirectories. Note that it's important we
143143
# match the dependencies shown in the subdirectory makefiles!
144+
# Also, in cases where a subdirectory makefile generates two files in
145+
# what's really one step, such as bison producing both gram.h and gram.c,
146+
# we must request making the one that is shown as the secondary (dependent)
147+
# output, else the timestamp on it might be wrong. By project convention,
148+
# the .h file is the dependent one for bison output, so we need only request
149+
# that; but in other cases, request both for safety.
144150

145151
parser/gram.h: parser/gram.y
146152
$(MAKE) -C parser gram.h

src/backend/catalog/Makefile

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ BKIFILES = postgres.bki postgres.description postgres.shdescription
2121

2222
include $(top_srcdir)/src/backend/common.mk
2323

24-
all: $(BKIFILES) schemapg.h
24+
all: $(BKIFILES) schemapg.h postgres.description postgres.shdescription
2525

2626
# Note: there are some undocumented dependencies on the ordering in which
2727
# the catalog header files are assembled into postgres.bki. In particular,
@@ -50,12 +50,15 @@ catalogdir = $(top_srcdir)/src/backend/catalog
5050
# locations of headers that genbki.pl needs to read
5151
pg_includes = -I$(top_srcdir)/src/include/catalog -I$(top_builddir)/src/include/catalog
5252

53-
# see explanation in ../parser/Makefile
54-
postgres.description: postgres.bki ;
53+
# see notes in src/backend/parser/Makefile about multiple output files
54+
postgres.description: postgres.bki
55+
touch $@
5556

56-
postgres.shdescription: postgres.bki ;
57+
postgres.shdescription: postgres.bki
58+
touch $@
5759

58-
schemapg.h: postgres.bki ;
60+
schemapg.h: postgres.bki
61+
touch $@
5962

6063
# Technically, this should depend on Makefile.global, but then
6164
# postgres.bki would need to be rebuilt after every configure run,

src/backend/parser/Makefile

Lines changed: 11 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -28,12 +28,17 @@ endif
2828

2929
# There is no correct way to write a rule that generates two files.
3030
# Rules with two targets don't have that meaning, they are merely
31-
# shorthand for two otherwise separate rules. To be safe for parallel
32-
# make, we must chain the dependencies like this. The semicolon is
33-
# important, otherwise make will choose the built-in rule for
34-
# gram.y=>gram.c.
35-
36-
gram.h: gram.c ;
31+
# shorthand for two otherwise separate rules. If we have an action
32+
# that in fact generates two or more files, we must choose one of them
33+
# as primary and show it as the action's output, then make all of the
34+
# other output files dependent on the primary, like this. Furthermore,
35+
# the "touch" action is essential, because it ensures that gram.h is
36+
# marked as newer than (or at least no older than) gram.c. Without that,
37+
# make is likely to try to rebuild gram.h in subsequent runs, which causes
38+
# failures in VPATH builds from tarballs.
39+
40+
gram.h: gram.c
41+
touch $@
3742

3843
gram.c: BISONFLAGS += -d
3944
gram.c: BISON_CHECK_CMD = $(PERL) $(srcdir)/check_keywords.pl $< $(top_srcdir)/src/include/parser/kwlist.h

src/backend/utils/Makefile

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -20,8 +20,9 @@ all: errcodes.h fmgroids.h probes.h
2020

2121
$(SUBDIRS:%=%-recursive): fmgroids.h
2222

23-
# see explanation in ../parser/Makefile
24-
fmgroids.h: fmgrtab.c ;
23+
# see notes in src/backend/parser/Makefile
24+
fmgroids.h: fmgrtab.c
25+
touch $@
2526

2627
fmgrtab.c: Gen_fmgrtab.pl $(catalogdir)/Catalog.pm $(top_srcdir)/src/include/catalog/pg_proc.h
2728
$(PERL) -I $(catalogdir) $< $(top_srcdir)/src/include/catalog/pg_proc.h

src/bin/psql/Makefile

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,10 @@ dumputils.c keywords.c: % : $(top_srcdir)/src/bin/pg_dump/%
4040
kwlookup.c: % : $(top_srcdir)/src/backend/parser/%
4141
rm -f $@ && $(LN_S) $< .
4242

43-
sql_help.c: sql_help.h ;
43+
# See notes in src/backend/parser/Makefile about the following two rules
44+
sql_help.c: sql_help.h
45+
touch $@
46+
4447
sql_help.h: create_help.pl $(wildcard $(REFDOCDIR)/*.sgml)
4548
$(PERL) $< $(REFDOCDIR) $*
4649

@@ -50,7 +53,7 @@ mainloop.o: psqlscan.c
5053
psqlscan.c: FLEXFLAGS = -Cfe -p -p
5154
psqlscan.c: FLEX_NO_BACKUP=yes
5255

53-
distprep: sql_help.h psqlscan.c
56+
distprep: sql_help.h sql_help.c psqlscan.c
5457

5558
install: all installdirs
5659
$(INSTALL_PROGRAM) psql$(X) '$(DESTDIR)$(bindir)/psql$(X)'

src/interfaces/ecpg/preproc/Makefile

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,10 @@ ecpg: $(OBJS) | submake-libpgport
4444
../ecpglib/typename.o: ../ecpglib/typename.c
4545
$(MAKE) -C $(dir $@) $(notdir $@)
4646

47-
preproc.h: preproc.c ;
47+
# See notes in src/backend/parser/Makefile about the following two rules
48+
preproc.h: preproc.c
49+
touch $@
50+
4851
preproc.c: BISONFLAGS += -d
4952

5053
preproc.y: ../../../backend/parser/gram.y parse.pl ecpg.addons ecpg.header ecpg.tokens ecpg.trailer ecpg.type

src/pl/plpgsql/src/Makefile

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -55,7 +55,9 @@ uninstall-headers:
5555
pl_gram.o pl_handler.o pl_comp.o pl_exec.o pl_funcs.o pl_scanner.o: plpgsql.h pl_gram.h plerrcodes.h
5656

5757
# See notes in src/backend/parser/Makefile about the following two rules
58-
pl_gram.h: pl_gram.c ;
58+
pl_gram.h: pl_gram.c
59+
touch $@
60+
5961
pl_gram.c: BISONFLAGS += -d
6062

6163
# generate plerrcodes.h from src/backend/utils/errcodes.txt

src/test/isolation/Makefile

Lines changed: 0 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -29,15 +29,6 @@ isolationtester$(X): $(OBJS) | submake-libpq submake-libpgport
2929

3030
distprep: specparse.c specscanner.c
3131

32-
# There is no correct way to write a rule that generates two files.
33-
# Rules with two targets don't have that meaning, they are merely
34-
# shorthand for two otherwise separate rules. To be safe for parallel
35-
# make, we must chain the dependencies like this. The semicolon is
36-
# important, otherwise make will choose the built-in rule for
37-
# gram.y=>gram.c.
38-
39-
specparse.h: specparse.c ;
40-
4132
# specscanner is compiled as part of specparse
4233
specparse.o: specscanner.c
4334

0 commit comments

Comments
 (0)
0