From 8124eec4e59222f1c3f3dc4d7e6706e13c33755c Mon Sep 17 00:00:00 2001
From: Ola x Nilsson <olani@axis.com>
Date: Wed, 6 Mar 2024 18:40:16 +0100
Subject: [PATCH] build: Find lua with pkg-config

ax_lua.m4 does not support cross compilation.

Upstream-Status: Inappropriate (Embedded specific)

---
 configure.ac | 26 +++++++++++++++++++++++++-
 1 file changed, 25 insertions(+), 1 deletion(-)

Index: diod-1.1.0+git/configure.ac
===================================================================
--- diod-1.1.0+git.orig/configure.ac
+++ diod-1.1.0+git/configure.ac
@@ -138,7 +138,31 @@ AC_ARG_ENABLE([config],
   [AS_HELP_STRING([--disable-config], [disable lua config file support])])
 
 AS_IF([test "x$enable_config" != "xno"], [
-  AX_PROG_LUA([5.1],[5.5])
+  dnl Use pkg_config to check for lua settings.  This is not standard
+  dnl as lua typically does not distribute a pc file.  The
+  dnl OpenEmbedded recipe adds one that can be used.  Reuse/replicate
+  dnl as much of AX_PROG_LUA as possible so AX_LUA_HEADERS and
+  dnl AX_LUA_LIBS are still usable.
+  PKG_CHECK_EXISTS([lua], [
+    AS_IF([
+      $PKG_CONFIG --atleast-version=5.1 lua && $PKG_CONFIG --max-version=5.6 lua
+    ],[
+      AC_ARG_VAR([LUA], [The LUA interpreter, e.g. /usr/bin/lua5.1])
+	  dnl should really check that the file exists ...
+      AC_SUBST([LUA], [${bindir}/lua])
+	  AC_CACHE_CHECK([for $LUA version], [ax_cv_lua_version], [
+	    ax_cv_lua_version=[`$PKG_CONFIG --modversion lua | $SED 's/^\([0-9]\+\.[0-9]\+\).*/\1/'`]
+      ])
+	  AS_IF([test "x$ax_cv_lua_version" = 'x'],
+       [AC_MSG_ERROR([invalid Lua version number])])
+      AC_SUBST([LUA_VERSION], [$ax_cv_lua_version])
+      AC_SUBST([LUA_SHORT_VERSION], [`echo "$LUA_VERSION" | $SED 's|\.||'`])
+    ],[
+	  AC_MSG_ERROR([Incorrect lua version])
+	])
+  ],[
+    AX_PROG_LUA([5.1],[5.5])
+  ])
   AX_LUA_HEADERS([],[
     AC_MSG_ERROR([Install lua headers or configure with --disable-config])
   ])
