This patch adds `ppif` function at `configure` which can test arbitrary
compiler preprocessor condition even when cross compiling.

Currently it's only used to detect x86-64 and i386 CPUs, but hopefully
it will be used to replace some config guesses or uname-related tests.

If there are no objections, I'll push it to mob soon.
From d4382ecedce5ae6f5160c342f088313b16600fd5 Mon Sep 17 00:00:00 2001
From: "Avi Halachmi (:avih)" <avih...@yahoo.com>
Date: Mon, 24 Jun 2019 12:29:05 +0300
Subject: [PATCH] configure: allow cc tests even if cross compiling, test
 i386/x86-64

Adds a tool `ppif` at configure which can test preprocessor conditions
even when $cc is a cross compiler to any foreign platform.

Currently used only to identify i386 or x86_64 (including when cross
compiling) as a mini-demonstration.

Hopefully will be used in the future to test more compiler features
and/or replace uname-related tests with more accurate results.
---
 configure | 43 +++++++++++++++++++++++++++++++++++++------
 1 file changed, 37 insertions(+), 6 deletions(-)

diff --git a/configure b/configure
index c35686a..4a40405 100755
--- a/configure
+++ b/configure
@@ -150,11 +150,46 @@ for opt do
   esac
 done
 
+cc="${cross_prefix}${cc}"
+ar="${cross_prefix}${ar}"
+strip="${cross_prefix}${strip}"
+
+
+PPIF_TEMPLATE="
+int ppif(void) {
+#if %s
+    return 0;
+#else
+    PPIF_FALSE;
+#endif
+}
+"
+
+# Succeeds when preprocessor condition `#if $1` is true.
+# if $2 is not empty, prints to stderr `checking whether $2... <yes/no>`
+# Works also when $cc is a cross compiler to any foreign platform.
+# E.g. ppif "defined(__GNUC__) && (GCC_MAJOR >= 3)"
+#   or ppif "defined(_WIN32)" "target is Windows"
+ppif() {
+    [ -z "${2-}" ] || printf "checking whether %s... " "$2" >&2
+    printf "$PPIF_TEMPLATE" "$1" > ppif.c \
+    && $cc -o ppif.o -c ppif.c 2>/dev/null
+    ppif_rv=$?
+    rm ppif.c ppif.o 2>/dev/null
+    [ -z "${2-}" ] || { [ 0 = $ppif_rv ] && echo yes || echo no; } >&2
+    return $ppif_rv
+}
+
+
 if test -z "$cpu" ; then
     if test -n "$ARCH" ; then
-	cpu="$ARCH"
+        cpu="$ARCH"
+    elif ppif "defined(__x86_64__)"; then
+        cpu="x86_64"
+    elif ppif "defined(__i386__)"; then
+        cpu="i386"
     else
-	cpu=`uname -m`
+        cpu=`uname -m`
     fi
 fi
 
@@ -303,10 +338,6 @@ EOF
 exit 1
 fi
 
-cc="${cross_prefix}${cc}"
-ar="${cross_prefix}${ar}"
-strip="${cross_prefix}${strip}"
-
 if test -z "$cross_prefix" ; then
   CONFTEST=./conftest$EXESUF
   if ! $cc -o $CONFTEST $source_path/conftest.c 2>/dev/null ; then
-- 
2.22.0

_______________________________________________
Tinycc-devel mailing list
Tinycc-devel@nongnu.org
https://lists.nongnu.org/mailman/listinfo/tinycc-devel

Reply via email to