Subject: bug#37852: Build failure on MSYS2 (undefined reference to _chk functions)

From: Paul Eggert <[email-masked]>
Date: Mon, 21 Oct 2019 17:40:12 -0700

> I think we should better disable GNULIB_PORTCHECK on MinGW (I can hardly
> imagine someone using that platform for those portability checks, and
> I'm not sure I understand why this gets defined by default on other
> platforms).

The intent of GNULIB_PORTCHECK is that it is enabled by developers who 
want some simple build-time portability tests. It isn't suitable for 
people who just want to build the software, as there are too many build 
failures and other false alarms that builders don't care about.

I installed the attached patch, to cause the problem to occur only if 
one configures explicitly with --enable-gcc-warnings. This should fix 
the problem that Richard reported. However, it won't fix the more 
general problem (which Richard should be able to reproduce via 
'./configure --enable-gcc-warnings ...').

Apparently MSYS2 changed recently, and this caused _FORTIFY_SOURCE 
builds to fail. See, for example:

https://github.com/msys2/MINGW-packages/issues/5803

I don't understand MSYS2 well enough to suggest fixes for Emacs, other 
than "don't configure with --enable-gcc-warnings and don't compile with 
_FORTIFY_SOURCE defined".
>From 0ccd70a99b5cbd3cf7fea65c47a724846c578a43 Mon Sep 17 00:00:00 2001
From: Paul Eggert <[email-masked]>
Date: Mon, 21 Oct 2019 17:28:02 -0700
Subject: [PATCH] Portcheck only if --enable-gcc-warnings
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bit

Problem reported by Richard Copley (Bug#37852).
This patch causes the problem to not occur if one uses plain
‘configure’.  The problem can still occur if with ‘configure
--enable-gcc-warnings’, so a further fix may be needed.
* configure.ac (GNULIB_PORTCHECK, _FORTIFY_SOURCE):
Define these only with an explicit --enable-gcc-warnings.
---
 configure.ac | 19 ++++++++++---------
 1 file changed, 10 insertions(+), 9 deletions(-)

diff --git a/configure.ac b/configure.ac
index 33d725c804..3b6a2a6d16 100644
--- a/configure.ac
+++ b/configure.ac
@@ -1114,15 +1114,16 @@ AC_DEFUN
   fi
 
   AC_DEFINE([GCC_LINT], [1], [Define to 1 if --enable-gcc-warnings.])
-  AC_DEFINE([GNULIB_PORTCHECK], [1], [enable some gnulib portability checks])
-  AH_VERBATIM([GNULIB_PORTCHECK_FORTIFY_SOURCE],
-  [/* Enable compile-time and run-time bounds-checking, and some warnings,
-      without upsetting glibc 2.15+. */
-   #if (defined GNULIB_PORTCHECK && !defined _FORTIFY_SOURCE \
-        && defined __OPTIMIZE__ && __OPTIMIZE__)
-   # define _FORTIFY_SOURCE 2
-   #endif
-  ])
+  AS_IF([test $gl_gcc_warnings = yes],
+    [AC_DEFINE([GNULIB_PORTCHECK], [1], [enable some gnulib portability checks])
+     AH_VERBATIM([GNULIB_PORTCHECK_FORTIFY_SOURCE],
+     [/* Enable compile-time and run-time bounds-checking, and some warnings,
+	 without upsetting glibc 2.15+. */
+      #if (defined GNULIB_PORTCHECK && !defined _FORTIFY_SOURCE \
+	   && defined __OPTIMIZE__ && __OPTIMIZE__)
+      # define _FORTIFY_SOURCE 2
+      #endif
+     ])])
  ])
 
 # clang is picky about these regardless of whether
-- 
2.21.0