From 1b3ba19b381876bd57ad733955b25236223cab4e Mon Sep 17 00:00:00 2001
From: Eli Schwartz <eschwartz93@gmail.com>
Date: Sun, 26 Nov 2023 18:05:48 -0500
Subject: [PATCH 4/4] ax_have_qt: migrate from hand-rolled path lookups with
 `which` to AC_PATH_PROG

The "which" utility is not guaranteed to be installed either, and if it
is, its behavior is not portable either. This means that when Qt is
installed, the `which` check will report a fatal error because the which
tool did not exist and the shell returned a nonzero status when
attempting to fork+exec. If it did exist, it might not be an
implementation of `which` that returns nonzero when commands do not
exist.

The general scripting suggestion is to use the "command -v" shell
builtin is required to exist in all POSIX 2008 compliant shells, and is
thus guaranteed to work everywhere.

For some in-depth discussions on the topic, see:
- https://mywiki.wooledge.org/BashFAQ/081
- https://unix.stackexchange.com/questions/85249/why-not-use-which-what-to-use-then/85250#85250

Examples of open-source shells likely to be installed as /bin/sh on
Linux, which implement the 15-year-old standard: ash, bash, busybox,
dash, ksh, mksh and zsh.

Since this is an autoconf extension, however, autoconf provides its own
guarantees: you are always supposed to use AC_PATH_PROG, which is
guaranteed to generate code which does the right thing, and in
particular tends to work even on systems which predate POSIX. It
officially becomes autoconf's job to make sure path detection works.

This has some convenient knock-on effects. In particular, we now permit
users to specify the path via $QT_* variables, which are documented
inline as being set and are now documented as publicly settable too.
This was even already the case with qmake.
---
 m4/ax_have_qt.m4 | 23 +++++++++++------------
 1 file changed, 11 insertions(+), 12 deletions(-)

diff --git a/m4/ax_have_qt.m4 b/m4/ax_have_qt.m4
index 9da2499..f713032 100644
--- a/m4/ax_have_qt.m4
+++ b/m4/ax_have_qt.m4
@@ -55,7 +55,7 @@
 #   and this notice are preserved. This file is offered as-is, without any
 #   warranty.
 
-#serial 26
+#serial 27
 
 AU_ALIAS([BNV_HAVE_QT], [AX_HAVE_QT])
 AC_DEFUN([AX_HAVE_QT],
@@ -127,11 +127,16 @@ EOF
     rmdir $am_have_qt_dir
 
     # Look for specific tools in $PATH
-    QT_MOC=`which moc$am_have_qt_qmexe_suff`
-    QT_UIC=`which uic$am_have_qt_qmexe_suff`
-    QT_RCC=`which rcc$am_have_qt_qmexe_suff`
-    QT_LRELEASE=`which lrelease$am_have_qt_qmexe_suff`
-    QT_LUPDATE=`which lupdate$am_have_qt_qmexe_suff`
+    AC_ARG_VAR([QT_MOC],[Qt moc tool])
+    AC_PATH_PROG([QT_MOC],[moc$am_have_qt_qmexe_suff])
+    AC_ARG_VAR([QT_UIC],[Qt uic tool])
+    AC_PATH_PROG([QT_UIC],[uic$am_have_qt_qmexe_suff])
+    AC_ARG_VAR([QT_RCC],[Qt rcc tool])
+    AC_PATH_PROG([QT_RCC],[rcc$am_have_qt_qmexe_suff])
+    AC_ARG_VAR([QT_LRELEASE],[Qt lrelease tool])
+    AC_PATH_PROG([QT_LRELEASE],[lreleasemoc$am_have_qt_qmexe_suff])
+    AC_ARG_VAR([QT_LUPDATE],[Qt lupdate tool])
+    AC_PATH_PROG([QT_LUPDATE],[lupdate$am_have_qt_qmexe_suff])
 
     # Get Qt version from qmake
     QT_DIR=`$QMAKE --version | grep -o -E /.+`
@@ -162,12 +167,6 @@ EOF
   AC_SUBST(QT_CXXFLAGS)
   AC_SUBST(QT_DIR)
   AC_SUBST(QT_LIBS)
-  AC_SUBST(QT_UIC)
-  AC_SUBST(QT_MOC)
-  AC_SUBST(QT_RCC)
-  AC_SUBST(QT_LRELEASE)
-  AC_SUBST(QT_LUPDATE)
-  AC_SUBST(QMAKE)
 
   #### Being paranoid:
   if test x"$have_qt" = xyes; then
-- 
2.41.0

