From d13367462e761a55be1a984b6b8744676bf85dfb Mon Sep 17 00:00:00 2001
From: Takashi Menjo <takashi.menjou.vg@hco.ntt.co.jp>
Date: Tue, 23 Mar 2021 13:32:27 +0900
Subject: [PATCH v8 01/11] Add --with-libpmem option for PMEM support

Fixes introduced in patchset v8:
- Support build with MSVC on Windows
---
 configure                        | 99 ++++++++++++++++++++++++++++++++
 configure.ac                     | 17 ++++++
 src/include/pg_config.h.in       |  6 ++
 src/tools/msvc/Solution.pm       | 13 +++++
 src/tools/msvc/config_default.pl |  3 +-
 5 files changed, 137 insertions(+), 1 deletion(-)

diff --git a/configure b/configure
index 3f2aea0d7d..3529005e9b 100755
--- a/configure
+++ b/configure
@@ -699,6 +699,7 @@ with_gnu_ld
 LD
 LDFLAGS_SL
 LDFLAGS_EX
+with_libpmem
 LZ4_LIBS
 LZ4_CFLAGS
 with_lz4
@@ -868,6 +869,7 @@ with_libxslt
 with_system_tzdata
 with_zlib
 with_lz4
+with_libpmem
 with_gnu_ld
 with_ssl
 with_openssl
@@ -1576,6 +1578,7 @@ Optional Packages:
                           use system time zone data in DIR
   --without-zlib          do not use Zlib
   --with-lz4              build with LZ4 support
+  --with-libpmem          build with PMEM support
   --with-gnu-ld           assume the C compiler uses GNU ld [default=no]
   --with-ssl=LIB          use LIB for SSL/TLS support (openssl)
   --with-openssl          obsolete spelling of --with-ssl=openssl
@@ -9033,6 +9036,41 @@ fi
   done
 fi
 
+#
+# libpmem
+#
+{ $as_echo "$as_me:${as_lineno-$LINENO}: checking whether to build with PMEM support" >&5
+$as_echo_n "checking whether to build with PMEM support... " >&6; }
+
+
+
+# Check whether --with-libpmem was given.
+if test "${with_libpmem+set}" = set; then :
+  withval=$with_libpmem;
+  case $withval in
+    yes)
+
+$as_echo "#define USE_LIBPMEM 1" >>confdefs.h
+
+      ;;
+    no)
+      :
+      ;;
+    *)
+      as_fn_error $? "no argument expected for --with-libpmem option" "$LINENO" 5
+      ;;
+  esac
+
+else
+  with_libpmem=no
+
+fi
+
+
+{ $as_echo "$as_me:${as_lineno-$LINENO}: result: $with_libpmem" >&5
+$as_echo "$with_libpmem" >&6; }
+
+
 #
 # Assignments
 #
@@ -13504,6 +13542,56 @@ fi
 fi
 
 
+if test "$with_libpmem" = yes; then
+  { $as_echo "$as_me:${as_lineno-$LINENO}: checking for pmem_memcpy in -lpmem" >&5
+$as_echo_n "checking for pmem_memcpy in -lpmem... " >&6; }
+if ${ac_cv_lib_pmem_pmem_memcpy+:} false; then :
+  $as_echo_n "(cached) " >&6
+else
+  ac_check_lib_save_LIBS=$LIBS
+LIBS="-lpmem  $LIBS"
+cat confdefs.h - <<_ACEOF >conftest.$ac_ext
+/* end confdefs.h.  */
+
+/* Override any GCC internal prototype to avoid an error.
+   Use char because int might match the return type of a GCC
+   builtin and then its argument prototype would still apply.  */
+#ifdef __cplusplus
+extern "C"
+#endif
+char pmem_memcpy ();
+int
+main ()
+{
+return pmem_memcpy ();
+  ;
+  return 0;
+}
+_ACEOF
+if ac_fn_c_try_link "$LINENO"; then :
+  ac_cv_lib_pmem_pmem_memcpy=yes
+else
+  ac_cv_lib_pmem_pmem_memcpy=no
+fi
+rm -f core conftest.err conftest.$ac_objext \
+    conftest$ac_exeext conftest.$ac_ext
+LIBS=$ac_check_lib_save_LIBS
+fi
+{ $as_echo "$as_me:${as_lineno-$LINENO}: result: $ac_cv_lib_pmem_pmem_memcpy" >&5
+$as_echo "$ac_cv_lib_pmem_pmem_memcpy" >&6; }
+if test "x$ac_cv_lib_pmem_pmem_memcpy" = xyes; then :
+  cat >>confdefs.h <<_ACEOF
+#define HAVE_LIBPMEM 1
+_ACEOF
+
+  LIBS="-lpmem $LIBS"
+
+else
+  as_fn_error $? "library 'libpmem' (version >= 1.5) is required for PMEM support" "$LINENO" 5
+fi
+
+fi
+
 
 ##
 ## Header files
@@ -14215,6 +14303,17 @@ fi
 
 done
 
+fi
+
+if test "$with_libpmem" = yes ; then
+  ac_fn_c_check_header_mongrel "$LINENO" "libpmem.h" "ac_cv_header_libpmem_h" "$ac_includes_default"
+if test "x$ac_cv_header_libpmem_h" = xyes; then :
+
+else
+  as_fn_error $? "header file <libpmem.h> is required for PMEM support" "$LINENO" 5
+fi
+
+
 fi
 
 ##
diff --git a/configure.ac b/configure.ac
index 95287705f6..a7e3fc8a3b 100644
--- a/configure.ac
+++ b/configure.ac
@@ -1056,6 +1056,15 @@ if test "$with_lz4" = yes; then
   done
 fi
 
+#
+# libpmem
+#
+AC_MSG_CHECKING([whether to build with PMEM support])
+PGAC_ARG_BOOL(with, libpmem, no, [build with PMEM support],
+              [AC_DEFINE([USE_LIBPMEM], 1, [Define to 1 to build with PMEM support. (--with-libpmem)])])
+AC_MSG_RESULT([$with_libpmem])
+AC_SUBST(with_libpmem)
+
 #
 # Assignments
 #
@@ -1385,6 +1394,10 @@ elif test "$with_uuid" = ossp ; then
 fi
 AC_SUBST(UUID_LIBS)
 
+if test "$with_libpmem" = yes; then
+  AC_CHECK_LIB(pmem, pmem_memcpy, [], [AC_MSG_ERROR([library 'libpmem' (version >= 1.5) is required for PMEM support])])
+fi
+
 
 ##
 ## Header files
@@ -1572,6 +1585,10 @@ if test "$PORTNAME" = "win32" ; then
    AC_CHECK_HEADERS(crtdefs.h)
 fi
 
+if test "$with_libpmem" = yes ; then
+  AC_CHECK_HEADER(libpmem.h, [], [AC_MSG_ERROR([header file <libpmem.h> is required for PMEM support])])
+fi
+
 ##
 ## Types, structures, compiler characteristics
 ##
diff --git a/src/include/pg_config.h.in b/src/include/pg_config.h.in
index 9d9bd6b9ef..2b40b208bc 100644
--- a/src/include/pg_config.h.in
+++ b/src/include/pg_config.h.in
@@ -331,6 +331,9 @@
 /* Define to 1 if you have the `pam' library (-lpam). */
 #undef HAVE_LIBPAM
 
+/* Define to 1 if you have the `pmem' library (-lpmem). */
+#undef HAVE_LIBPMEM
+
 /* Define if you have a function readline library */
 #undef HAVE_LIBREADLINE
 
@@ -901,6 +904,9 @@
 /* Define to 1 to build with LDAP support. (--with-ldap) */
 #undef USE_LDAP
 
+/* Define to 1 to build with PMEM support. (--with-libpmem) */
+#undef USE_LIBPMEM
+
 /* Define to 1 to build with XML support. (--with-libxml) */
 #undef USE_LIBXML
 
diff --git a/src/tools/msvc/Solution.pm b/src/tools/msvc/Solution.pm
index e47c2d648c..3be3332799 100644
--- a/src/tools/msvc/Solution.pm
+++ b/src/tools/msvc/Solution.pm
@@ -304,6 +304,7 @@ sub GenerateFiles
 		HAVE_LIBLZ4                                 => undef,
 		HAVE_LIBM                                   => undef,
 		HAVE_LIBPAM                                 => undef,
+		HAVE_LIBPMEM                                => undef,
 		HAVE_LIBREADLINE                            => undef,
 		HAVE_LIBSELINUX                             => undef,
 		HAVE_LIBSSL                                 => undef,
@@ -489,6 +490,7 @@ sub GenerateFiles
 		USE_BONJOUR         => undef,
 		USE_BSD_AUTH        => undef,
 		USE_ICU => $self->{options}->{icu} ? 1 : undef,
+		USE_LIBPMEM                => undef,
 		USE_LIBXML                 => undef,
 		USE_LIBXSLT                => undef,
 		USE_LZ4                    => undef,
@@ -539,6 +541,11 @@ sub GenerateFiles
 		$define{HAVE_LZ4_H}  = 1;
 		$define{USE_LZ4}     = 1;
 	}
+	if ($self->{options}->{pmem})
+	{
+		$define{HAVE_LIBPMEM} = 1;
+		$define{USE_LIBPMEM}  = 1;
+	}
 	if ($self->{options}->{openssl})
 	{
 		$define{USE_OPENSSL} = 1;
@@ -1086,6 +1093,11 @@ sub AddProject
 		$proj->AddIncludeDir($self->{options}->{uuid} . '\include');
 		$proj->AddLibrary($self->{options}->{uuid} . '\lib\uuid.lib');
 	}
+	if ($self->{options}->{pmem})
+	{
+		$proj->AddIncludeDir($self->{options}->{pmem} . '\include');
+		$proj->AddLibrary($self->{options}->{pmem} . '\lib\libpmem.lib');
+	}
 	return $proj;
 }
 
@@ -1198,6 +1210,7 @@ sub GetFakeConfigure
 	$cfg .= ' --with-tcl'           if ($self->{options}->{tcl});
 	$cfg .= ' --with-perl'          if ($self->{options}->{perl});
 	$cfg .= ' --with-python'        if ($self->{options}->{python});
+	$cfg .= ' --with-libpmem'       if ($self->{options}->{pmem});
 	my $port = $self->{options}->{'--with-pgport'};
 	$cfg .= " --with-pgport=$port" if defined($port);
 
diff --git a/src/tools/msvc/config_default.pl b/src/tools/msvc/config_default.pl
index 7a9b00be72..7b887bc68f 100644
--- a/src/tools/msvc/config_default.pl
+++ b/src/tools/msvc/config_default.pl
@@ -25,7 +25,8 @@ our $config = {
 	xml       => undef,    # --with-libxml=<path>
 	xslt      => undef,    # --with-libxslt=<path>
 	iconv     => undef,    # (not in configure, path to iconv)
-	zlib      => undef     # --with-zlib=<path>
+	zlib      => undef,    # --with-zlib=<path>
+	pmem      => undef     # --with-libpmem=<path>
 };
 
 1;
-- 
2.25.1

