From 64045f2f8fa73b3b1c3150a9efae6ef55fe1ead9 Mon Sep 17 00:00:00 2001
From: Eli Schwartz <eschwartz93@gmail.com>
Date: Sun, 26 Nov 2023 17:46:13 -0500
Subject: [PATCH 2/4] ax_llvm: 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
llvm-config 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 $LLVM_CONFIG, which is a somewhat standard
approach, and we document it as well.
---
 m4/ax_llvm.m4 | 33 ++++++++++++++-------------------
 1 file changed, 14 insertions(+), 19 deletions(-)

diff --git a/m4/ax_llvm.m4 b/m4/ax_llvm.m4
index 712f86d..fb1f6ba 100644
--- a/m4/ax_llvm.m4
+++ b/m4/ax_llvm.m4
@@ -24,34 +24,29 @@
 #   and this notice are preserved. This file is offered as-is, without any
 #   warranty.
 
-#serial 17
+#serial 18
 
 AC_DEFUN([AX_LLVM],
 [
+AC_ARG_VAR([LLVM_CONFIG], [llvm-config program])
 AC_ARG_WITH([llvm],
 	AS_HELP_STRING([--with-llvm@<:@=ARG@:>@], [use llvm (default is yes) - it is possible to specify the root directory for llvm (optional)]),
-	[
-    if test "$withval" = "no"; then
-		want_llvm="no"
-    elif test "$withval" = "yes"; then
-        want_llvm="yes"
-        ac_llvm_config_path=`which llvm-config`
-    else
-	    want_llvm="yes"
-        ac_llvm_config_path="$withval"
-	fi
-    ],
-    [want_llvm="yes"])
+	[],
+	[want_llvm=yes])
+
+	AS_IF([test "$withval" = "no"], [want_llvm=no],
+		[test "$withval" = "yes"], [want_llvm=yes; AC_PATH_PROG([LLVM_CONFIG], [llvm-config])],
+		[want_llvm=yes; if test -n "$withval"; then LLVM_CONFIG="$withval"; fi])
 
 	succeeded=no
-	if test -z "$ac_llvm_config_path"; then
-		ac_llvm_config_path=`which llvm-config`
-	fi
 
 	if test "x$want_llvm" = "xyes"; then
-		if test -e "$ac_llvm_config_path"; then
-			LLVM_CPPFLAGS=`$ac_llvm_config_path --cxxflags`
-			LLVM_LDFLAGS="$($ac_llvm_config_path --ldflags) $($ac_llvm_config_path --libs $1)"
+		if test -z "$LLVM_CONFIG"; then
+			AC_PATH_PROG([LLVM_CONFIG], [llvm-config])
+		fi
+		if test -e "$LLVM_CONFIG"; then
+			LLVM_CPPFLAGS=`$LLVM_CONFIG --cxxflags`
+			LLVM_LDFLAGS="$($LLVM_CONFIG --ldflags) $($LLVM_CONFIG --libs $1)"
 
 			AC_REQUIRE([AC_PROG_CXX])
 			CPPFLAGS_SAVED="$CPPFLAGS"
-- 
2.41.0

