From 5ec5e08edc059ed0c0d430dc8e02cd64bebc8d1c Mon Sep 17 00:00:00 2001
From: Khem Raj <raj.khem@gmail.com>
Date: Thu, 28 Aug 2025 17:02:49 -0700
Subject: [PATCH] meson: use a runtime-safe interpreter string
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bit

In cross builds, python.find_installation() (what
PYTHON usually comes from) must be a native
interpreter (since Meson runs it during configure),
but you don’t want that absolute native path baked
into target files. You want a runtime/target path
such as /usr/bin/env python3 (portable) or
/usr/bin/python3

Upstream-Status: Submitted [https://gitlab.isc.org/isc-projects/kea/-/issues/4087]
Signed-off-by: Khem Raj <raj.khem@gmail.com>
---
 doc/sphinx/meson.build          | 8 +++++++-
 meson.build                     | 8 +++++++-
 src/bin/shell/tests/meson.build | 8 +++++++-
 src/lib/util/python/meson.build | 8 +++++++-
 4 files changed, 28 insertions(+), 4 deletions(-)

--- a/doc/sphinx/meson.build
+++ b/doc/sphinx/meson.build
@@ -70,7 +70,13 @@ doc_conf.set('builddir', meson.current_b
 doc_conf.set('srcdir', meson.current_source_dir())
 doc_conf.set('sphinxbuilddir', sphinxbuilddir)
 doc_conf.set('abs_sphinxbuilddir', abs_sphinxbuilddir)
-doc_conf.set('PYTHON', PYTHON.full_path())
+# During cross builds, avoid embedding the native Python path into target artifacts.
+# Use a runtime-safe interpreter path for the target.
+py_for_runtime = '/usr/bin/env python3'
+if not meson.is_cross_build()
+  py_for_runtime = PYTHON.full_path()
+endif
+doc_conf.set('PYTHON', py_for_runtime)
 doc_conf.set('TOP_SOURCE_DIR', TOP_SOURCE_DIR)
 if PDFLATEX.found()
     doc_conf.set('HAVE_PDFLATEX', 'yes')
--- a/meson.build
+++ b/meson.build
@@ -638,9 +638,13 @@ link_args = []
 # Also, Meson might use it by default, but might not use it on all systems, so lots of variables...
 # EXECUTABLE_RPATH = f'$ORIGIN/../@LIBDIR@'
 # HOOK_RPATH = '$ORIGIN/../..'
-
+if not meson.is_cross_build()
 BUILD_RPATH = TOP_BUILD_DIR / 'src/lib'
 INSTALL_RPATH = LIBDIR_INSTALLED
+else
+BUILD_RPATH = ''
+INSTALL_RPATH = ''
+endif
 
 # Add rpaths for NETCONF dependencies.
 if NETCONF_DEP.found()
@@ -759,7 +763,13 @@ report_conf_data.set('CXX_ARGS', ' '.joi
 report_conf_data.set('LD_ID', cpp.get_linker_id())
 link_args += get_option('cpp_link_args')
 report_conf_data.set('LD_ARGS', ' '.join(link_args))
-report_conf_data.set('PYTHON_PATH', PYTHON.full_path())
+# During cross builds, avoid embedding the native Python path into target artifacts.
+# Use a runtime-safe interpreter path for the target.
+py_for_runtime = '/usr/bin/env python3'
+if not meson.is_cross_build()
+  py_for_runtime = PYTHON.full_path()
+endif
+report_conf_data.set('PYTHON_PATH', py_for_runtime)
 report_conf_data.set('PYTHON_VERSION', PYTHON.version())
 report_conf_data.set('PKGPYTHONDIR', PKGPYTHONDIR)
 result = cpp.run(
--- a/src/bin/shell/tests/meson.build
+++ b/src/bin/shell/tests/meson.build
@@ -3,7 +3,13 @@ if not TESTS_OPT.enabled()
 endif
 
 shell_tests_conf_data = configuration_data()
-shell_tests_conf_data.set('PYTHON', PYTHON.full_path())
+# During cross builds, avoid embedding the native Python path into target artifacts.
+# Use a runtime-safe interpreter path for the target.
+py_for_runtime = '/usr/bin/env python3'
+if not meson.is_cross_build()
+  py_for_runtime = PYTHON.full_path()
+endif
+shell_tests_conf_data.set('PYTHON', py_for_runtime)
 shell_tests_conf_data.set('abs_top_builddir', TOP_BUILD_DIR)
 shell_tests_conf_data.set('abs_top_srcdir', TOP_SOURCE_DIR)
 shell_unittest = configure_file(
--- a/src/lib/util/python/meson.build
+++ b/src/lib/util/python/meson.build
@@ -4,7 +4,13 @@ endif
 
 configure_file(input: 'const2hdr.py', output: 'const2hdr.py', copy: true)
 util_python_conf_data = configuration_data()
-util_python_conf_data.set('PYTHON', PYTHON.full_path())
+# During cross builds, avoid embedding the native Python path into target artifacts.
+# Use a runtime-safe interpreter path for the target.
+py_for_runtime = '/usr/bin/env python3'
+if not meson.is_cross_build()
+  py_for_runtime = PYTHON.full_path()
+endif
+util_python_conf_data.set('PYTHON', py_for_runtime)
 configure_file(
     input: 'gen_wiredata.py.in',
     output: 'gen_wiredata.py',
--- a/src/bin/shell/meson.build
+++ b/src/bin/shell/meson.build
@@ -1,5 +1,11 @@
 kea_shell_conf_data = configuration_data()
-kea_shell_conf_data.set('PYTHON', PYTHON.full_path())
+# During cross builds, avoid embedding the native Python path into target artifacts.
+# Use a runtime-safe interpreter path for the target.
+py_for_runtime = '/usr/bin/env python3'
+if not meson.is_cross_build()
+  py_for_runtime = PYTHON.full_path()
+endif
+kea_shell_conf_data.set('PYTHON', py_for_runtime)
 kea_shell_conf_data.set('PACKAGE_VERSION', PROJECT_VERSION)
 kea_shell_conf_data.set(
     'EXTENDED_VERSION',
