From 1054a9443fa420f7e3c15a118b6bb3dcf9a214d4 Mon Sep 17 00:00:00 2001
From: Andrew Dunstan <andrew@dunslane.net>
Date: Sat, 22 Aug 2026 17:21:02 -0400
Subject: [PATCH v2] meson: report the GNU host triplet in PG_VERSION_STR

The meson build composed the platform part of PG_VERSION_STR from
host_machine.cpu_family() and host_system, producing strings like
"aarch64-linux", where configure substitutes the GNU host triplet,
"aarch64-unknown-linux-gnu".  Two regression tests match against that
string, and both have quietly been doing the wrong thing on meson builds
ever since meson support arrived in 16. Those have been fixed now, but
the differences remain.

Fix by asking the compiler for its own target triplet with -dumpmachine
where it supports that, falling back to meson's idea of the host
otherwise.

Discussion: https://postgr.es/m/a40b19da-9a02-47b4-8afd-2bbbde8db1e8@dunslane.net
---
 meson.build | 21 +++++++++++++++++++--
 1 file changed, 19 insertions(+), 2 deletions(-)

diff --git a/meson.build b/meson.build
index f4cde249242..779881c8880 100644
--- a/meson.build
+++ b/meson.build
@@ -3293,10 +3293,27 @@ cdata.set('MEMSET_LOOP_LIMIT', memset_loop_limit)
 cdata.set_quoted('DLSUFFIX', dlsuffix)


+# configure substitutes the GNU host triplet into PG_VERSION_STR, and some
+# regression tests match against it: collate.linux.utf8 looks for "linux-gnu",
+# and infinite_recurse looks for "powerpc64[^,]*-linux-gnu".  Meson's own idea
+# of the host is just cpu_family-system, which never contains the ABI suffix,
+# so ask the compiler for the triplet when it can tell us.
+host_tuple = '@0@-@1@'.format(host_machine.cpu_family(), host_system)
+if cc.get_id() in ['gcc', 'clang']
+  # Include c_args: a --target passed there (clang cross-builds) changes
+  # what the compiler actually targets, and -dumpmachine needs to see it
+  # too, or it reports the untargeted default instead.
+  dumpmachine = run_command(cc.cmd_array() + get_option('c_args'),
+                             '-dumpmachine', check: false)
+  if dumpmachine.returncode() == 0 and dumpmachine.stdout().strip() != ''
+    host_tuple = dumpmachine.stdout().strip()
+  endif
+endif
+
 # built later than the rest of the version metadata, we need SIZEOF_VOID_P
 cdata.set_quoted('PG_VERSION_STR',
-  'PostgreSQL @0@ on @1@-@2@, compiled by @3@-@4@, @5@-bit'.format(
-    pg_version, host_machine.cpu_family(), host_system,
+  'PostgreSQL @0@ on @1@, compiled by @2@-@3@, @4@-bit'.format(
+    pg_version, host_tuple,
     cc.get_id(), cc.version(), cdata.get('SIZEOF_VOID_P') * 8,
   )
 )
--
2.43.0
