autoconf: Don’t search for X11 when cross compiling (#110345)

From ecb8af04b1cd91bb44adfb9956229794c734b91a Mon Sep 17 00:00:00 2001
From: Zack Weinberg <[EMAIL REDACTED]>
Date: Sun, 1 Nov 2020 19:23:12 -0500
Subject: [PATCH] =?UTF-8?q?Don=E2=80=99t=20search=20for=20X11=20when=20cro?=
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bit

This is undesirable because X11 development headers and libraries
found by searching /usr are much more likely to belong to the build
operating system than the host operating system (being cross-compiled
for).  A particularly problematic case, from the original bug report,
is “using a sysroot where the target is binary compatible with the
host.  In this case AC_PATH_X will happily look at /usr and say that
yes, X is available, even if the sysroot doesn't have X.”

To cross-compile X client applications, the recommended procedure is
to put X11 headers and libraries for the host system in the cross
compiler’s default search path; alternatively, --x-includes and
--x-libraries can be used.

Fixes bug #110345.  Problem reported by Ross Burton.

 * lib/autoconf/libs.m4 (_AC_PATH_X): Before doing anything else,
   see whether a test compilation with no special options (just -lX11)
   will work.  If it doesn’t, only invoke _AC_PATH_X_XMKMF and
   _AC_PATH_X_DIRECT when not cross compiling.

(cherry-picked from commit 33c3a47c04ab70a4dd54963fe433a171bc03747f)
 lib/autoconf/libs.m4 | 36 ++++++++++++++++++++++++------------
 1 file changed, 24 insertions(+), 12 deletions(-)

diff --git a/lib/autoconf/libs.m4 b/lib/autoconf/libs.m4
index 390c1eb1..7a8f2b67 100644
--- a/lib/autoconf/libs.m4
+++ b/lib/autoconf/libs.m4
@@ -295,22 +295,34 @@ fi # $ac_x_libraries = no
 # ----------
 # Compute ac_cv_have_x.
+[AC_REQUIRE([AC_PROG_CC])]dnl To ensure that $cross_compiling is finalized.
 [# One or both of the vars are not set, and there is no cached value.
-ac_x_includes=no ac_x_libraries=no
-case $ac_x_includes,$ac_x_libraries in #(
-  no,* | *,no | *\'*)
-    # Didn't find X, or a directory has "'" in its name.
-    ac_cv_have_x="have_x=no";; #(
-  *)
-    # Record where we found X for the cache.
+# Do we need to do anything special at all?
+LIBS="-lX11 $LIBS"
+AC_LINK_IFELSE([AC_LANG_PROGRAM([@%:@include <X11/Xlib.h>],
+				[XrmInitialize ()])],
+  [# We can compile and link X programs with no special options.
+  ac_x_includes=
+  ac_x_libraries=])
+# If that didn't work, only try xmkmf and filesystem searches
+# for native compilation.
+AS_IF([test x"$ac_x_includes" = xno && test "$cross_compiling" = no],
+# Record the results.
+  [no,* | *,no | *\'*],
+    [# Didn't find X, or a directory has "'" in its name.
+    ac_cv_have_x="have_x=no"],
+    [# Record where we found X for the cache.
-	ac_x_libraries='$ac_x_libraries'"
+	ac_x_libraries='$ac_x_libraries'"])])])