From 5c02a00d440b90ead12658ce6ec9f4eee95dd0a3 Mon Sep 17 00:00:00 2001
From: Tom Lane <tgl@sss.pgh.pa.us>
Date: Tue, 28 Feb 2012 19:53:39 -0500
Subject: [PATCH] Move CRC tables to libpgport, and provide them in a separate
 include file.

This makes it much more convenient to build tools for Postgres that are
separately compiled and require a matching CRC implementation.

To prevent multiple copies of the CRC polynomial tables being introduced
into the postgres binaries, they are now included in the static library
libpgport that is mainly meant for replacement system functions.  That
seems like a bit of a kludge, but there's no better place.

This cleans up building of the tools pg_controldata and pg_resetxlog,
which previously had to build their own copies of pg_crc.o.

In the future, external programs that need access to the CRC tables can
include the tables directly from the new header file pg_crc_tables.h.

Daniel Farina, reviewed by Abhijit Menon-Sen and Tom Lane
---
 src/backend/utils/hash/Makefile               |  2 +-
 src/bin/pg_controldata/.gitignore             |  2 --
 src/bin/pg_controldata/Makefile               |  7 ++-----
 src/bin/pg_resetxlog/.gitignore               |  2 --
 src/bin/pg_resetxlog/Makefile                 |  7 ++-----
 .../utils/pg_crc_tables.h}                    | 20 ++++++++++--------
 src/port/Makefile                             |  4 ++--
 src/port/pg_crc.c                             | 21 +++++++++++++++++++
 src/tools/msvc/Project.pm                     |  4 ++--
 9 files changed, 41 insertions(+), 28 deletions(-)
 rename src/{backend/utils/hash/pg_crc.c => include/utils/pg_crc_tables.h} (98%)
 create mode 100644 src/port/pg_crc.c

diff --git a/src/backend/utils/hash/Makefile b/src/backend/utils/hash/Makefile
index 64eebd1d996..05d347c8563 100644
--- a/src/backend/utils/hash/Makefile
+++ b/src/backend/utils/hash/Makefile
@@ -12,6 +12,6 @@ subdir = src/backend/utils/hash
 top_builddir = ../../../..
 include $(top_builddir)/src/Makefile.global
 
-OBJS = dynahash.o hashfn.o pg_crc.o
+OBJS = dynahash.o hashfn.o
 
 include $(top_srcdir)/src/backend/common.mk
diff --git a/src/bin/pg_controldata/.gitignore b/src/bin/pg_controldata/.gitignore
index 32ea40181f5..eab0c28a8bb 100644
--- a/src/bin/pg_controldata/.gitignore
+++ b/src/bin/pg_controldata/.gitignore
@@ -1,3 +1 @@
-/pg_crc.c
-
 /pg_controldata
diff --git a/src/bin/pg_controldata/Makefile b/src/bin/pg_controldata/Makefile
index 0eff84666da..b8a39dc1cdb 100644
--- a/src/bin/pg_controldata/Makefile
+++ b/src/bin/pg_controldata/Makefile
@@ -15,16 +15,13 @@ subdir = src/bin/pg_controldata
 top_builddir = ../../..
 include $(top_builddir)/src/Makefile.global
 
-OBJS= pg_controldata.o pg_crc.o $(WIN32RES)
+OBJS= pg_controldata.o $(WIN32RES)
 
 all: pg_controldata
 
 pg_controldata: $(OBJS) | submake-libpgport
 	$(CC) $(CFLAGS) $^ $(LDFLAGS) $(LDFLAGS_EX) $(LIBS) -o $@$(X)
 
-pg_crc.c: $(top_srcdir)/src/backend/utils/hash/pg_crc.c
-	rm -f $@ && $(LN_S) $< .
-
 install: all installdirs
 	$(INSTALL_PROGRAM) pg_controldata$(X) '$(DESTDIR)$(bindir)/pg_controldata$(X)'
 
@@ -35,4 +32,4 @@ uninstall:
 	rm -f '$(DESTDIR)$(bindir)/pg_controldata$(X)'
 
 clean distclean maintainer-clean:
-	rm -f pg_controldata$(X) $(OBJS) pg_crc.c
+	rm -f pg_controldata$(X) $(OBJS)
diff --git a/src/bin/pg_resetxlog/.gitignore b/src/bin/pg_resetxlog/.gitignore
index 584590951fc..6b84208ee0c 100644
--- a/src/bin/pg_resetxlog/.gitignore
+++ b/src/bin/pg_resetxlog/.gitignore
@@ -1,3 +1 @@
-/pg_crc.c
-
 /pg_resetxlog
diff --git a/src/bin/pg_resetxlog/Makefile b/src/bin/pg_resetxlog/Makefile
index eb03b8a0b91..0e260355869 100644
--- a/src/bin/pg_resetxlog/Makefile
+++ b/src/bin/pg_resetxlog/Makefile
@@ -15,16 +15,13 @@ subdir = src/bin/pg_resetxlog
 top_builddir = ../../..
 include $(top_builddir)/src/Makefile.global
 
-OBJS= pg_resetxlog.o pg_crc.o $(WIN32RES)
+OBJS= pg_resetxlog.o $(WIN32RES)
 
 all: pg_resetxlog
 
 pg_resetxlog: $(OBJS) | submake-libpgport
 	$(CC) $(CFLAGS) $^ $(LDFLAGS) $(LDFLAGS_EX) $(LIBS) -o $@$(X)
 
-pg_crc.c: $(top_srcdir)/src/backend/utils/hash/pg_crc.c
-	rm -f $@ && $(LN_S) $< .
-
 install: all installdirs
 	$(INSTALL_PROGRAM) pg_resetxlog$(X) '$(DESTDIR)$(bindir)/pg_resetxlog$(X)'
 
@@ -35,4 +32,4 @@ uninstall:
 	rm -f '$(DESTDIR)$(bindir)/pg_resetxlog$(X)'
 
 clean distclean maintainer-clean:
-	rm -f pg_resetxlog$(X) $(OBJS) pg_crc.c
+	rm -f pg_resetxlog$(X) $(OBJS)
diff --git a/src/backend/utils/hash/pg_crc.c b/src/include/utils/pg_crc_tables.h
similarity index 98%
rename from src/backend/utils/hash/pg_crc.c
rename to src/include/utils/pg_crc_tables.h
index 596184b59af..524410fffdf 100644
--- a/src/backend/utils/hash/pg_crc.c
+++ b/src/include/utils/pg_crc_tables.h
@@ -1,7 +1,11 @@
 /*-------------------------------------------------------------------------
  *
- * pg_crc.c
- *	  PostgreSQL CRC support
+ * pg_crc_tables.h
+ *	  Polynomial lookup tables for CRC macros
+ *
+ * We make these tables available as a .h file so that programs not linked
+ * with libpgport can still use the macros in pg_crc.h.  They just need
+ * to #include this header as well.
  *
  * See Ross Williams' excellent introduction
  * A PAINLESS GUIDE TO CRC ERROR DETECTION ALGORITHMS, available from
@@ -17,16 +21,12 @@
  * Portions Copyright (c) 1996-2012, PostgreSQL Global Development Group
  * Portions Copyright (c) 1994, Regents of the University of California
  *
- *
- * IDENTIFICATION
- *	  src/backend/utils/hash/pg_crc.c
+ * src/include/utils/pg_crc_tables.h
  *
  *-------------------------------------------------------------------------
  */
-
-/* Use c.h so that this file can be built in either frontend or backend */
-#include "c.h"
-
+#ifndef PG_CRC_TABLES_H
+#define PG_CRC_TABLES_H
 
 /*
  * This table is based on the polynomial
@@ -513,3 +513,5 @@ const uint64 pg_crc64_table[256] = {
 #endif   /* SIZEOF_VOID_P < 8 */
 
 #endif   /* PROVIDE_64BIT_CRC */
+
+#endif   /* PG_CRC_TABLES_H */
diff --git a/src/port/Makefile b/src/port/Makefile
index 1bf0963ba78..4e3a8edd3a1 100644
--- a/src/port/Makefile
+++ b/src/port/Makefile
@@ -31,8 +31,8 @@ override CPPFLAGS := -I$(top_builddir)/src/port -DFRONTEND $(CPPFLAGS)
 LIBS += $(PTHREAD_LIBS)
 
 OBJS = $(LIBOBJS) chklocale.o dirmod.o erand48.o exec.o fls.o inet_net_ntop.o \
-	noblock.o path.o pgcheckdir.o pgmkdirp.o pgsleep.o pgstrcasecmp.o \
-	qsort.o qsort_arg.o sprompt.o thread.o
+	noblock.o path.o pgcheckdir.o pg_crc.o pgmkdirp.o pgsleep.o \
+	pgstrcasecmp.o qsort.o qsort_arg.o sprompt.o thread.o
 
 # foo_srv.o and foo.o are both built from foo.c, but only foo.o has -DFRONTEND
 OBJS_SRV = $(OBJS:%.o=%_srv.o)
diff --git a/src/port/pg_crc.c b/src/port/pg_crc.c
new file mode 100644
index 00000000000..ebf4f3a61a7
--- /dev/null
+++ b/src/port/pg_crc.c
@@ -0,0 +1,21 @@
+/*-------------------------------------------------------------------------
+ *
+ * pg_crc.c
+ *	  PostgreSQL CRC support
+ *
+ * This file simply #includes the CRC table definitions so that they are
+ * available to programs linked with libpgport.
+ *
+ * Portions Copyright (c) 1996-2012, PostgreSQL Global Development Group
+ * Portions Copyright (c) 1994, Regents of the University of California
+ *
+ *
+ * IDENTIFICATION
+ *	  src/port/pg_crc.c
+ *
+ *-------------------------------------------------------------------------
+ */
+
+#include "c.h"
+
+#include "utils/pg_crc_tables.h"
diff --git a/src/tools/msvc/Project.pm b/src/tools/msvc/Project.pm
index 9db664a190e..98db076e58c 100644
--- a/src/tools/msvc/Project.pm
+++ b/src/tools/msvc/Project.pm
@@ -261,8 +261,8 @@ sub AddDir
         $mf =~ s{OBJS[^=]*=\s*(.*)$}{}m;
     }
 
-    # Match rules that pull in source files from different directories
-    # example: pg_crc.c: $(top_srcdir)/src/backend/utils/hash/pg_crc.c
+    # Match rules that pull in source files from different directories, eg
+    # pgstrcasecmp.c rint.c snprintf.c: % : $(top_srcdir)/src/port/%
     my $replace_re = qr{^([^:\n\$]+\.c)\s*:\s*(?:%\s*: )?\$(\([^\)]+\))\/(.*)\/[^\/]+$}m;
     while ($mf =~ m{$replace_re}m)
     {
-- 
GitLab