1290067Sbapt# ===========================================================================
2290067Sbapt#          http://www.gnu.org/software/autoconf-archive/ax_lua.html
3290067Sbapt# ===========================================================================
4290067Sbapt#
5290067Sbapt# SYNOPSIS
6290067Sbapt#
7290067Sbapt#   AX_PROG_LUA[([MINIMUM-VERSION], [TOO-BIG-VERSION], [ACTION-IF-FOUND], [ACTION-IF-NOT-FOUND])]
8290067Sbapt#   AX_LUA_HEADERS[([ACTION-IF-FOUND], [ACTION-IF-NOT-FOUND])]
9290067Sbapt#   AX_LUA_LIBS[([ACTION-IF-FOUND], [ACTION-IF-NOT-FOUND])]
10290067Sbapt#   AX_LUA_READLINE[([ACTION-IF-FOUND], [ACTION-IF-NOT-FOUND])]
11290067Sbapt#
12290067Sbapt# DESCRIPTION
13290067Sbapt#
14290067Sbapt#   Detect a Lua interpreter, optionally specifying a minimum and maximum
15290067Sbapt#   version number. Set up important Lua paths, such as the directories in
16290067Sbapt#   which to install scripts and modules (shared libraries).
17290067Sbapt#
18290067Sbapt#   Also detect Lua headers and libraries. The Lua version contained in the
19290067Sbapt#   header is checked to match the Lua interpreter version exactly. When
20290067Sbapt#   searching for Lua libraries, the version number is used as a suffix.
21290067Sbapt#   This is done with the goal of supporting multiple Lua installs (5.1,
22290067Sbapt#   5.2, and 5.3 side-by-side).
23290067Sbapt#
24290067Sbapt#   A note on compatibility with previous versions: This file has been
25290067Sbapt#   mostly rewritten for serial 18. Most developers should be able to use
26290067Sbapt#   these macros without needing to modify configure.ac. Care has been taken
27290067Sbapt#   to preserve each macro's behavior, but there are some differences:
28290067Sbapt#
29290067Sbapt#   1) AX_WITH_LUA is deprecated; it now expands to the exact same thing as
30290067Sbapt#   AX_PROG_LUA with no arguments.
31290067Sbapt#
32290067Sbapt#   2) AX_LUA_HEADERS now checks that the version number defined in lua.h
33290067Sbapt#   matches the interpreter version. AX_LUA_HEADERS_VERSION is therefore
34290067Sbapt#   unnecessary, so it is deprecated and does not expand to anything.
35290067Sbapt#
36290067Sbapt#   3) The configure flag --with-lua-suffix no longer exists; the user
37290067Sbapt#   should instead specify the LUA precious variable on the command line.
38290067Sbapt#   See the AX_PROG_LUA description for details.
39290067Sbapt#
40290067Sbapt#   Please read the macro descriptions below for more information.
41290067Sbapt#
42290067Sbapt#   This file was inspired by Andrew Dalke's and James Henstridge's
43290067Sbapt#   python.m4 and Tom Payne's, Matthieu Moy's, and Reuben Thomas's ax_lua.m4
44290067Sbapt#   (serial 17). Basically, this file is a mash-up of those two files. I
45290067Sbapt#   like to think it combines the best of the two!
46290067Sbapt#
47290067Sbapt#   AX_PROG_LUA: Search for the Lua interpreter, and set up important Lua
48290067Sbapt#   paths. Adds precious variable LUA, which may contain the path of the Lua
49290067Sbapt#   interpreter. If LUA is blank, the user's path is searched for an
50290067Sbapt#   suitable interpreter.
51290067Sbapt#
52290067Sbapt#   If MINIMUM-VERSION is supplied, then only Lua interpreters with a
53290067Sbapt#   version number greater or equal to MINIMUM-VERSION will be accepted. If
54290067Sbapt#   TOO-BIG-VERSION is also supplied, then only Lua interpreters with a
55290067Sbapt#   version number greater or equal to MINIMUM-VERSION and less than
56290067Sbapt#   TOO-BIG-VERSION will be accepted.
57290067Sbapt#
58290067Sbapt#   The Lua version number, LUA_VERSION, is found from the interpreter, and
59290067Sbapt#   substituted. LUA_PLATFORM is also found, but not currently supported (no
60290067Sbapt#   standard representation).
61290067Sbapt#
62290067Sbapt#   Finally, the macro finds four paths:
63290067Sbapt#
64290067Sbapt#     luadir             Directory to install Lua scripts.
65290067Sbapt#     pkgluadir          $luadir/$PACKAGE
66290067Sbapt#     luaexecdir         Directory to install Lua modules.
67290067Sbapt#     pkgluaexecdir      $luaexecdir/$PACKAGE
68290067Sbapt#
69290067Sbapt#   These paths are found based on $prefix, $exec_prefix, Lua's
70290067Sbapt#   package.path, and package.cpath. The first path of package.path
71290067Sbapt#   beginning with $prefix is selected as luadir. The first path of
72290067Sbapt#   package.cpath beginning with $exec_prefix is used as luaexecdir. This
73290067Sbapt#   should work on all reasonable Lua installations. If a path cannot be
74290067Sbapt#   determined, a default path is used. Of course, the user can override
75290067Sbapt#   these later when invoking make.
76290067Sbapt#
77290067Sbapt#     luadir             Default: $prefix/share/lua/$LUA_VERSION
78290067Sbapt#     luaexecdir         Default: $exec_prefix/lib/lua/$LUA_VERSION
79290067Sbapt#
80290067Sbapt#   These directories can be used by Automake as install destinations. The
81290067Sbapt#   variable name minus 'dir' needs to be used as a prefix to the
82290067Sbapt#   appropriate Automake primary, e.g. lua_SCRIPS or luaexec_LIBRARIES.
83290067Sbapt#
84290067Sbapt#   If an acceptable Lua interpreter is found, then ACTION-IF-FOUND is
85290067Sbapt#   performed, otherwise ACTION-IF-NOT-FOUND is preformed. If ACTION-IF-NOT-
86290067Sbapt#   FOUND is blank, then it will default to printing an error. To prevent
87290067Sbapt#   the default behavior, give ':' as an action.
88290067Sbapt#
89290067Sbapt#   AX_LUA_HEADERS: Search for Lua headers. Requires that AX_PROG_LUA be
90290067Sbapt#   expanded before this macro. Adds precious variable LUA_INCLUDE, which
91290067Sbapt#   may contain Lua specific include flags, e.g. -I/usr/include/lua5.1. If
92290067Sbapt#   LUA_INCLUDE is blank, then this macro will attempt to find suitable
93290067Sbapt#   flags.
94290067Sbapt#
95290067Sbapt#   LUA_INCLUDE can be used by Automake to compile Lua modules or
96290067Sbapt#   executables with embedded interpreters. The *_CPPFLAGS variables should
97290067Sbapt#   be used for this purpose, e.g. myprog_CPPFLAGS = $(LUA_INCLUDE).
98290067Sbapt#
99290067Sbapt#   This macro searches for the header lua.h (and others). The search is
100290067Sbapt#   performed with a combination of CPPFLAGS, CPATH, etc, and LUA_INCLUDE.
101290067Sbapt#   If the search is unsuccessful, then some common directories are tried.
102290067Sbapt#   If the headers are then found, then LUA_INCLUDE is set accordingly.
103290067Sbapt#
104290067Sbapt#   The paths automatically searched are:
105290067Sbapt#
106290067Sbapt#     * /usr/include/luaX.Y
107290067Sbapt#     * /usr/include/lua/X.Y
108290067Sbapt#     * /usr/include/luaXY
109290067Sbapt#     * /usr/local/include/luaX.Y
110290067Sbapt#     * /usr/local/include/lua-X.Y
111290067Sbapt#     * /usr/local/include/lua/X.Y
112290067Sbapt#     * /usr/local/include/luaXY
113290067Sbapt#
114290067Sbapt#   (Where X.Y is the Lua version number, e.g. 5.1.)
115290067Sbapt#
116290067Sbapt#   The Lua version number found in the headers is always checked to match
117290067Sbapt#   the Lua interpreter's version number. Lua headers with mismatched
118290067Sbapt#   version numbers are not accepted.
119290067Sbapt#
120290067Sbapt#   If headers are found, then ACTION-IF-FOUND is performed, otherwise
121290067Sbapt#   ACTION-IF-NOT-FOUND is performed. If ACTION-IF-NOT-FOUND is blank, then
122290067Sbapt#   it will default to printing an error. To prevent the default behavior,
123290067Sbapt#   set the action to ':'.
124290067Sbapt#
125290067Sbapt#   AX_LUA_LIBS: Search for Lua libraries. Requires that AX_PROG_LUA be
126290067Sbapt#   expanded before this macro. Adds precious variable LUA_LIB, which may
127290067Sbapt#   contain Lua specific linker flags, e.g. -llua5.1. If LUA_LIB is blank,
128290067Sbapt#   then this macro will attempt to find suitable flags.
129290067Sbapt#
130290067Sbapt#   LUA_LIB can be used by Automake to link Lua modules or executables with
131290067Sbapt#   embedded interpreters. The *_LIBADD and *_LDADD variables should be used
132290067Sbapt#   for this purpose, e.g. mymod_LIBADD = $(LUA_LIB).
133290067Sbapt#
134290067Sbapt#   This macro searches for the Lua library. More technically, it searches
135290067Sbapt#   for a library containing the function lua_load. The search is performed
136290067Sbapt#   with a combination of LIBS, LIBRARY_PATH, and LUA_LIB.
137290067Sbapt#
138290067Sbapt#   If the search determines that some linker flags are missing, then those
139290067Sbapt#   flags will be added to LUA_LIB.
140290067Sbapt#
141290067Sbapt#   If libraries are found, then ACTION-IF-FOUND is performed, otherwise
142290067Sbapt#   ACTION-IF-NOT-FOUND is performed. If ACTION-IF-NOT-FOUND is blank, then
143290067Sbapt#   it will default to printing an error. To prevent the default behavior,
144290067Sbapt#   set the action to ':'.
145290067Sbapt#
146290067Sbapt#   AX_LUA_READLINE: Search for readline headers and libraries. Requires the
147290067Sbapt#   AX_LIB_READLINE macro, which is provided by ax_lib_readline.m4 from the
148290067Sbapt#   Autoconf Archive.
149290067Sbapt#
150290067Sbapt#   If a readline compatible library is found, then ACTION-IF-FOUND is
151290067Sbapt#   performed, otherwise ACTION-IF-NOT-FOUND is performed.
152290067Sbapt#
153290067Sbapt# LICENSE
154290067Sbapt#
155290067Sbapt#   Copyright (c) 2015 Reuben Thomas <rrt@sc3d.org>
156290067Sbapt#   Copyright (c) 2014 Tim Perkins <tprk77@gmail.com>
157290067Sbapt#
158290067Sbapt#   This program is free software: you can redistribute it and/or modify it
159290067Sbapt#   under the terms of the GNU General Public License as published by the
160290067Sbapt#   Free Software Foundation, either version 3 of the License, or (at your
161290067Sbapt#   option) any later version.
162290067Sbapt#
163290067Sbapt#   This program is distributed in the hope that it will be useful, but
164290067Sbapt#   WITHOUT ANY WARRANTY; without even the implied warranty of
165290067Sbapt#   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General
166290067Sbapt#   Public License for more details.
167290067Sbapt#
168290067Sbapt#   You should have received a copy of the GNU General Public License along
169290067Sbapt#   with this program. If not, see <http://www.gnu.org/licenses/>.
170290067Sbapt#
171290067Sbapt#   As a special exception, the respective Autoconf Macro's copyright owner
172290067Sbapt#   gives unlimited permission to copy, distribute and modify the configure
173290067Sbapt#   scripts that are the output of Autoconf when processing the Macro. You
174290067Sbapt#   need not follow the terms of the GNU General Public License when using
175290067Sbapt#   or distributing such scripts, even though portions of the text of the
176290067Sbapt#   Macro appear in them. The GNU General Public License (GPL) does govern
177290067Sbapt#   all other use of the material that constitutes the Autoconf Macro.
178290067Sbapt#
179290067Sbapt#   This special exception to the GPL applies to versions of the Autoconf
180290067Sbapt#   Macro released by the Autoconf Archive. When you make and distribute a
181290067Sbapt#   modified version of the Autoconf Macro, you may extend this special
182290067Sbapt#   exception to the GPL to apply to your modified version as well.
183290067Sbapt
184290067Sbapt#serial 39
185290067Sbapt
186290067Sbaptdnl =========================================================================
187290067Sbaptdnl AX_PROG_LUA([MINIMUM-VERSION], [TOO-BIG-VERSION],
188290067Sbaptdnl             [ACTION-IF-FOUND], [ACTION-IF-NOT-FOUND])
189290067Sbaptdnl =========================================================================
190290067SbaptAC_DEFUN([AX_PROG_LUA],
191290067Sbapt[
192290067Sbapt  dnl Check for required tools.
193290067Sbapt  AC_REQUIRE([AC_PROG_GREP])
194290067Sbapt  AC_REQUIRE([AC_PROG_SED])
195290067Sbapt
196290067Sbapt  dnl Make LUA a precious variable.
197290067Sbapt  AC_ARG_VAR([LUA], [The Lua interpreter, e.g. /usr/bin/lua5.1])
198290067Sbapt
199290067Sbapt  dnl Find a Lua interpreter.
200290067Sbapt  m4_define_default([_AX_LUA_INTERPRETER_LIST],
201290067Sbapt    [lua lua5.3 lua53 lua5.2 lua52 lua5.1 lua51 lua50])
202290067Sbapt
203290067Sbapt  m4_if([$1], [],
204290067Sbapt  [ dnl No version check is needed. Find any Lua interpreter.
205290067Sbapt    AS_IF([test "x$LUA" = 'x'],
206290067Sbapt      [AC_PATH_PROGS([LUA], [_AX_LUA_INTERPRETER_LIST], [:])])
207290067Sbapt    ax_display_LUA='lua'
208290067Sbapt
209290067Sbapt    AS_IF([test "x$LUA" != 'x:'],
210290067Sbapt      [ dnl At least check if this is a Lua interpreter.
211290067Sbapt        AC_MSG_CHECKING([if $LUA is a Lua interpreter])
212290067Sbapt        _AX_LUA_CHK_IS_INTRP([$LUA],
213290067Sbapt          [AC_MSG_RESULT([yes])],
214290067Sbapt          [ AC_MSG_RESULT([no])
215290067Sbapt            AC_MSG_ERROR([not a Lua interpreter])
216290067Sbapt          ])
217290067Sbapt      ])
218290067Sbapt  ],
219290067Sbapt  [ dnl A version check is needed.
220290067Sbapt    AS_IF([test "x$LUA" != 'x'],
221290067Sbapt    [ dnl Check if this is a Lua interpreter.
222290067Sbapt      AC_MSG_CHECKING([if $LUA is a Lua interpreter])
223290067Sbapt      _AX_LUA_CHK_IS_INTRP([$LUA],
224290067Sbapt        [AC_MSG_RESULT([yes])],
225290067Sbapt        [ AC_MSG_RESULT([no])
226290067Sbapt          AC_MSG_ERROR([not a Lua interpreter])
227290067Sbapt        ])
228290067Sbapt      dnl Check the version.
229290067Sbapt      m4_if([$2], [],
230290067Sbapt        [_ax_check_text="whether $LUA version >= $1"],
231290067Sbapt        [_ax_check_text="whether $LUA version >= $1, < $2"])
232290067Sbapt      AC_MSG_CHECKING([$_ax_check_text])
233290067Sbapt      _AX_LUA_CHK_VER([$LUA], [$1], [$2],
234290067Sbapt        [AC_MSG_RESULT([yes])],
235290067Sbapt        [ AC_MSG_RESULT([no])
236290067Sbapt          AC_MSG_ERROR([version is out of range for specified LUA])])
237290067Sbapt      ax_display_LUA=$LUA
238290067Sbapt    ],
239290067Sbapt    [ dnl Try each interpreter until we find one that satisfies VERSION.
240290067Sbapt      m4_if([$2], [],
241290067Sbapt        [_ax_check_text="for a Lua interpreter with version >= $1"],
242290067Sbapt        [_ax_check_text="for a Lua interpreter with version >= $1, < $2"])
243290067Sbapt      AC_CACHE_CHECK([$_ax_check_text],
244290067Sbapt        [ax_cv_pathless_LUA],
245290067Sbapt        [ for ax_cv_pathless_LUA in _AX_LUA_INTERPRETER_LIST none; do
246290067Sbapt            test "x$ax_cv_pathless_LUA" = 'xnone' && break
247290067Sbapt            _AX_LUA_CHK_IS_INTRP([$ax_cv_pathless_LUA], [], [continue])
248290067Sbapt            _AX_LUA_CHK_VER([$ax_cv_pathless_LUA], [$1], [$2], [break])
249290067Sbapt          done
250290067Sbapt        ])
251290067Sbapt      dnl Set $LUA to the absolute path of $ax_cv_pathless_LUA.
252290067Sbapt      AS_IF([test "x$ax_cv_pathless_LUA" = 'xnone'],
253290067Sbapt        [LUA=':'],
254290067Sbapt        [AC_PATH_PROG([LUA], [$ax_cv_pathless_LUA])])
255290067Sbapt      ax_display_LUA=$ax_cv_pathless_LUA
256290067Sbapt    ])
257290067Sbapt  ])
258290067Sbapt
259290067Sbapt  AS_IF([test "x$LUA" = 'x:'],
260290067Sbapt  [ dnl Run any user-specified action, or abort.
261290067Sbapt    m4_default([$4], [AC_MSG_ERROR([cannot find suitable Lua interpreter])])
262290067Sbapt  ],
263290067Sbapt  [ dnl Query Lua for its version number.
264290067Sbapt    AC_CACHE_CHECK([for $ax_display_LUA version],
265290067Sbapt      [ax_cv_lua_version],
266290067Sbapt      [ dnl Get the interpreter version in X.Y format. This should work for
267290067Sbapt        dnl interpreters version 5.0 and beyond.
268290067Sbapt        ax_cv_lua_version=[`$LUA -e '
269290067Sbapt          -- return a version number in X.Y format
270290067Sbapt          local _, _, ver = string.find(_VERSION, "^Lua (%d+%.%d+)")
271290067Sbapt          print(ver)'`]
272290067Sbapt      ])
273290067Sbapt    AS_IF([test "x$ax_cv_lua_version" = 'x'],
274290067Sbapt      [AC_MSG_ERROR([invalid Lua version number])])
275290067Sbapt    AC_SUBST([LUA_VERSION], [$ax_cv_lua_version])
276290067Sbapt    AC_SUBST([LUA_SHORT_VERSION], [`echo "$LUA_VERSION" | $SED 's|\.||'`])
277290067Sbapt
278290067Sbapt    dnl The following check is not supported:
279290067Sbapt    dnl At times (like when building shared libraries) you may want to know
280290067Sbapt    dnl which OS platform Lua thinks this is.
281290067Sbapt    AC_CACHE_CHECK([for $ax_display_LUA platform],
282290067Sbapt      [ax_cv_lua_platform],
283290067Sbapt      [ax_cv_lua_platform=[`$LUA -e 'print("unknown")'`]])
284290067Sbapt    AC_SUBST([LUA_PLATFORM], [$ax_cv_lua_platform])
285290067Sbapt
286290067Sbapt    dnl Use the values of $prefix and $exec_prefix for the corresponding
287290067Sbapt    dnl values of LUA_PREFIX and LUA_EXEC_PREFIX. These are made distinct
288290067Sbapt    dnl variables so they can be overridden if need be. However, the general
289290067Sbapt    dnl consensus is that you shouldn't need this ability.
290290067Sbapt    AC_SUBST([LUA_PREFIX], ['${prefix}'])
291290067Sbapt    AC_SUBST([LUA_EXEC_PREFIX], ['${exec_prefix}'])
292290067Sbapt
293290067Sbapt    dnl Lua provides no way to query the script directory, and instead
294290067Sbapt    dnl provides LUA_PATH. However, we should be able to make a safe educated
295290067Sbapt    dnl guess. If the built-in search path contains a directory which is
296290067Sbapt    dnl prefixed by $prefix, then we can store scripts there. The first
297290067Sbapt    dnl matching path will be used.
298290067Sbapt    AC_CACHE_CHECK([for $ax_display_LUA script directory],
299290067Sbapt      [ax_cv_lua_luadir],
300290067Sbapt      [ AS_IF([test "x$prefix" = 'xNONE'],
301290067Sbapt          [ax_lua_prefix=$ac_default_prefix],
302290067Sbapt          [ax_lua_prefix=$prefix])
303290067Sbapt
304290067Sbapt        dnl Initialize to the default path.
305290067Sbapt        ax_cv_lua_luadir="$LUA_PREFIX/share/lua/$LUA_VERSION"
306290067Sbapt
307290067Sbapt        dnl Try to find a path with the prefix.
308290067Sbapt        _AX_LUA_FND_PRFX_PTH([$LUA], [$ax_lua_prefix], [script])
309290067Sbapt        AS_IF([test "x$ax_lua_prefixed_path" != 'x'],
310290067Sbapt        [ dnl Fix the prefix.
311290067Sbapt          _ax_strip_prefix=`echo "$ax_lua_prefix" | $SED 's|.|.|g'`
312290067Sbapt          ax_cv_lua_luadir=`echo "$ax_lua_prefixed_path" | \
313290067Sbapt            $SED "s|^$_ax_strip_prefix|$LUA_PREFIX|"`
314290067Sbapt        ])
315290067Sbapt      ])
316290067Sbapt    AC_SUBST([luadir], [$ax_cv_lua_luadir])
317290067Sbapt    AC_SUBST([pkgluadir], [\${luadir}/$PACKAGE])
318290067Sbapt
319290067Sbapt    dnl Lua provides no way to query the module directory, and instead
320290067Sbapt    dnl provides LUA_PATH. However, we should be able to make a safe educated
321290067Sbapt    dnl guess. If the built-in search path contains a directory which is
322290067Sbapt    dnl prefixed by $exec_prefix, then we can store modules there. The first
323290067Sbapt    dnl matching path will be used.
324290067Sbapt    AC_CACHE_CHECK([for $ax_display_LUA module directory],
325290067Sbapt      [ax_cv_lua_luaexecdir],
326290067Sbapt      [ AS_IF([test "x$exec_prefix" = 'xNONE'],
327290067Sbapt          [ax_lua_exec_prefix=$ax_lua_prefix],
328290067Sbapt          [ax_lua_exec_prefix=$exec_prefix])
329290067Sbapt
330290067Sbapt        dnl Initialize to the default path.
331290067Sbapt        ax_cv_lua_luaexecdir="$LUA_EXEC_PREFIX/lib/lua/$LUA_VERSION"
332290067Sbapt
333290067Sbapt        dnl Try to find a path with the prefix.
334290067Sbapt        _AX_LUA_FND_PRFX_PTH([$LUA],
335290067Sbapt          [$ax_lua_exec_prefix], [module])
336290067Sbapt        AS_IF([test "x$ax_lua_prefixed_path" != 'x'],
337290067Sbapt        [ dnl Fix the prefix.
338290067Sbapt          _ax_strip_prefix=`echo "$ax_lua_exec_prefix" | $SED 's|.|.|g'`
339290067Sbapt          ax_cv_lua_luaexecdir=`echo "$ax_lua_prefixed_path" | \
340290067Sbapt            $SED "s|^$_ax_strip_prefix|$LUA_EXEC_PREFIX|"`
341290067Sbapt        ])
342290067Sbapt      ])
343290067Sbapt    AC_SUBST([luaexecdir], [$ax_cv_lua_luaexecdir])
344290067Sbapt    AC_SUBST([pkgluaexecdir], [\${luaexecdir}/$PACKAGE])
345290067Sbapt
346290067Sbapt    dnl Run any user specified action.
347290067Sbapt    $3
348290067Sbapt  ])
349290067Sbapt])
350290067Sbapt
351290067Sbaptdnl AX_WITH_LUA is now the same thing as AX_PROG_LUA.
352290067SbaptAC_DEFUN([AX_WITH_LUA],
353290067Sbapt[
354290067Sbapt  AC_MSG_WARN([[$0 is deprecated, please use AX_PROG_LUA instead]])
355290067Sbapt  AX_PROG_LUA
356290067Sbapt])
357290067Sbapt
358290067Sbapt
359290067Sbaptdnl =========================================================================
360290067Sbaptdnl _AX_LUA_CHK_IS_INTRP(PROG, [ACTION-IF-TRUE], [ACTION-IF-FALSE])
361290067Sbaptdnl =========================================================================
362290067SbaptAC_DEFUN([_AX_LUA_CHK_IS_INTRP],
363290067Sbapt[
364290067Sbapt  dnl A minimal Lua factorial to prove this is an interpreter. This should work
365290067Sbapt  dnl for Lua interpreters version 5.0 and beyond.
366290067Sbapt  _ax_lua_factorial=[`$1 2>/dev/null -e '
367290067Sbapt    -- a simple factorial
368290067Sbapt    function fact (n)
369290067Sbapt      if n == 0 then
370290067Sbapt        return 1
371290067Sbapt      else
372290067Sbapt        return n * fact(n-1)
373290067Sbapt      end
374290067Sbapt    end
375290067Sbapt    print("fact(5) is " .. fact(5))'`]
376290067Sbapt  AS_IF([test "$_ax_lua_factorial" = 'fact(5) is 120'],
377290067Sbapt    [$2], [$3])
378290067Sbapt])
379290067Sbapt
380290067Sbapt
381290067Sbaptdnl =========================================================================
382290067Sbaptdnl _AX_LUA_CHK_VER(PROG, MINIMUM-VERSION, [TOO-BIG-VERSION],
383290067Sbaptdnl                 [ACTION-IF-TRUE], [ACTION-IF-FALSE])
384290067Sbaptdnl =========================================================================
385290067SbaptAC_DEFUN([_AX_LUA_CHK_VER],
386290067Sbapt[
387290067Sbapt  dnl Check that the Lua version is within the bounds. Only the major and minor
388290067Sbapt  dnl version numbers are considered. This should work for Lua interpreters
389290067Sbapt  dnl version 5.0 and beyond.
390290067Sbapt  _ax_lua_good_version=[`$1 -e '
391290067Sbapt    -- a script to compare versions
392290067Sbapt    function verstr2num(verstr)
393290067Sbapt      local _, _, majorver, minorver = string.find(verstr, "^(%d+)%.(%d+)")
394290067Sbapt      if majorver and minorver then
395290067Sbapt        return tonumber(majorver) * 100 + tonumber(minorver)
396290067Sbapt      end
397290067Sbapt    end
398290067Sbapt    local minver = verstr2num("$2")
399290067Sbapt    local _, _, trimver = string.find(_VERSION, "^Lua (.*)")
400290067Sbapt    local ver = verstr2num(trimver)
401290067Sbapt    local maxver = verstr2num("$3") or 1e9
402290067Sbapt    if minver <= ver and ver < maxver then
403290067Sbapt      print("yes")
404290067Sbapt    else
405290067Sbapt      print("no")
406290067Sbapt    end'`]
407290067Sbapt    AS_IF([test "x$_ax_lua_good_version" = "xyes"],
408290067Sbapt      [$4], [$5])
409290067Sbapt])
410290067Sbapt
411290067Sbapt
412290067Sbaptdnl =========================================================================
413290067Sbaptdnl _AX_LUA_FND_PRFX_PTH(PROG, PREFIX, SCRIPT-OR-MODULE-DIR)
414290067Sbaptdnl =========================================================================
415290067SbaptAC_DEFUN([_AX_LUA_FND_PRFX_PTH],
416290067Sbapt[
417290067Sbapt  dnl Get the script or module directory by querying the Lua interpreter,
418290067Sbapt  dnl filtering on the given prefix, and selecting the shallowest path. If no
419290067Sbapt  dnl path is found matching the prefix, the result will be an empty string.
420290067Sbapt  dnl The third argument determines the type of search, it can be 'script' or
421290067Sbapt  dnl 'module'. Supplying 'script' will perform the search with package.path
422290067Sbapt  dnl and LUA_PATH, and supplying 'module' will search with package.cpath and
423290067Sbapt  dnl LUA_CPATH. This is done for compatibility with Lua 5.0.
424290067Sbapt
425290067Sbapt  ax_lua_prefixed_path=[`$1 -e '
426290067Sbapt    -- get the path based on search type
427290067Sbapt    local searchtype = "$3"
428290067Sbapt    local paths = ""
429290067Sbapt    if searchtype == "script" then
430290067Sbapt      paths = (package and package.path) or LUA_PATH
431290067Sbapt    elseif searchtype == "module" then
432290067Sbapt      paths = (package and package.cpath) or LUA_CPATH
433290067Sbapt    end
434290067Sbapt    -- search for the prefix
435290067Sbapt    local prefix = "'$2'"
436290067Sbapt    local minpath = ""
437290067Sbapt    local mindepth = 1e9
438290067Sbapt    string.gsub(paths, "(@<:@^;@:>@+)",
439290067Sbapt      function (path)
440290067Sbapt        path = string.gsub(path, "%?.*$", "")
441290067Sbapt        path = string.gsub(path, "/@<:@^/@:>@*$", "")
442290067Sbapt        if string.find(path, prefix) then
443290067Sbapt          local depth = string.len(string.gsub(path, "@<:@^/@:>@", ""))
444290067Sbapt          if depth < mindepth then
445290067Sbapt            minpath = path
446290067Sbapt            mindepth = depth
447290067Sbapt          end
448290067Sbapt        end
449290067Sbapt      end)
450290067Sbapt    print(minpath)'`]
451290067Sbapt])
452290067Sbapt
453290067Sbapt
454290067Sbaptdnl =========================================================================
455290067Sbaptdnl AX_LUA_HEADERS([ACTION-IF-FOUND], [ACTION-IF-NOT-FOUND])
456290067Sbaptdnl =========================================================================
457290067SbaptAC_DEFUN([AX_LUA_HEADERS],
458290067Sbapt[
459290067Sbapt  dnl Check for LUA_VERSION.
460290067Sbapt  AC_MSG_CHECKING([if LUA_VERSION is defined])
461290067Sbapt  AS_IF([test "x$LUA_VERSION" != 'x'],
462290067Sbapt    [AC_MSG_RESULT([yes])],
463290067Sbapt    [ AC_MSG_RESULT([no])
464290067Sbapt      AC_MSG_ERROR([cannot check Lua headers without knowing LUA_VERSION])
465290067Sbapt    ])
466290067Sbapt
467290067Sbapt  dnl Make LUA_INCLUDE a precious variable.
468290067Sbapt  AC_ARG_VAR([LUA_INCLUDE], [The Lua includes, e.g. -I/usr/include/lua5.1])
469290067Sbapt
470290067Sbapt  dnl Some default directories to search.
471290067Sbapt  LUA_SHORT_VERSION=`echo "$LUA_VERSION" | $SED 's|\.||'`
472290067Sbapt  m4_define_default([_AX_LUA_INCLUDE_LIST],
473290067Sbapt    [ /usr/include/lua$LUA_VERSION \
474290067Sbapt      /usr/include/lua-$LUA_VERSION \
475290067Sbapt      /usr/include/lua/$LUA_VERSION \
476290067Sbapt      /usr/include/lua$LUA_SHORT_VERSION \
477290067Sbapt      /usr/local/include/lua$LUA_VERSION \
478290067Sbapt      /usr/local/include/lua-$LUA_VERSION \
479290067Sbapt      /usr/local/include/lua/$LUA_VERSION \
480290067Sbapt      /usr/local/include/lua$LUA_SHORT_VERSION \
481290067Sbapt    ])
482290067Sbapt
483290067Sbapt  dnl Try to find the headers.
484290067Sbapt  _ax_lua_saved_cppflags=$CPPFLAGS
485290067Sbapt  CPPFLAGS="$CPPFLAGS $LUA_INCLUDE"
486290067Sbapt  AC_CHECK_HEADERS([lua.h lualib.h lauxlib.h luaconf.h])
487290067Sbapt  CPPFLAGS=$_ax_lua_saved_cppflags
488290067Sbapt
489290067Sbapt  dnl Try some other directories if LUA_INCLUDE was not set.
490290067Sbapt  AS_IF([test "x$LUA_INCLUDE" = 'x' &&
491290067Sbapt         test "x$ac_cv_header_lua_h" != 'xyes'],
492290067Sbapt    [ dnl Try some common include paths.
493290067Sbapt      for _ax_include_path in _AX_LUA_INCLUDE_LIST; do
494290067Sbapt        test ! -d "$_ax_include_path" && continue
495290067Sbapt
496290067Sbapt        AC_MSG_CHECKING([for Lua headers in])
497290067Sbapt        AC_MSG_RESULT([$_ax_include_path])
498290067Sbapt
499290067Sbapt        AS_UNSET([ac_cv_header_lua_h])
500290067Sbapt        AS_UNSET([ac_cv_header_lualib_h])
501290067Sbapt        AS_UNSET([ac_cv_header_lauxlib_h])
502290067Sbapt        AS_UNSET([ac_cv_header_luaconf_h])
503290067Sbapt
504290067Sbapt        _ax_lua_saved_cppflags=$CPPFLAGS
505290067Sbapt        CPPFLAGS="$CPPFLAGS -I$_ax_include_path"
506290067Sbapt        AC_CHECK_HEADERS([lua.h lualib.h lauxlib.h luaconf.h])
507290067Sbapt        CPPFLAGS=$_ax_lua_saved_cppflags
508290067Sbapt
509290067Sbapt        AS_IF([test "x$ac_cv_header_lua_h" = 'xyes'],
510290067Sbapt          [ LUA_INCLUDE="-I$_ax_include_path"
511290067Sbapt            break
512290067Sbapt          ])
513290067Sbapt      done
514290067Sbapt    ])
515290067Sbapt
516290067Sbapt  AS_IF([test "x$ac_cv_header_lua_h" = 'xyes'],
517290067Sbapt    [ dnl Make a program to print LUA_VERSION defined in the header.
518290067Sbapt      dnl TODO It would be really nice if we could do this without compiling a
519290067Sbapt      dnl program, then it would work when cross compiling. But I'm not sure how
520290067Sbapt      dnl to do this reliably. For now, assume versions match when cross compiling.
521290067Sbapt
522290067Sbapt      AS_IF([test "x$cross_compiling" != 'xyes'],
523290067Sbapt        [ AC_CACHE_CHECK([for Lua header version],
524290067Sbapt            [ax_cv_lua_header_version],
525290067Sbapt            [ _ax_lua_saved_cppflags=$CPPFLAGS
526290067Sbapt              CPPFLAGS="$CPPFLAGS $LUA_INCLUDE"
527290067Sbapt              AC_RUN_IFELSE(
528290067Sbapt                [ AC_LANG_SOURCE([[
529290067Sbapt#include <lua.h>
530290067Sbapt#include <stdlib.h>
531290067Sbapt#include <stdio.h>
532290067Sbaptint main(int argc, char ** argv)
533290067Sbapt{
534290067Sbapt  if(argc > 1) printf("%s", LUA_VERSION);
535290067Sbapt  exit(EXIT_SUCCESS);
536290067Sbapt}
537290067Sbapt]])
538290067Sbapt                ],
539290067Sbapt                [ ax_cv_lua_header_version=`./conftest$EXEEXT p | \
540290067Sbapt                    $SED -n "s|^Lua \(@<:@0-9@:>@\{1,\}\.@<:@0-9@:>@\{1,\}\).\{0,\}|\1|p"`
541290067Sbapt                ],
542290067Sbapt                [ax_cv_lua_header_version='unknown'])
543290067Sbapt              CPPFLAGS=$_ax_lua_saved_cppflags
544290067Sbapt            ])
545290067Sbapt
546290067Sbapt          dnl Compare this to the previously found LUA_VERSION.
547290067Sbapt          AC_MSG_CHECKING([if Lua header version matches $LUA_VERSION])
548290067Sbapt          AS_IF([test "x$ax_cv_lua_header_version" = "x$LUA_VERSION"],
549290067Sbapt            [ AC_MSG_RESULT([yes])
550290067Sbapt              ax_header_version_match='yes'
551290067Sbapt            ],
552290067Sbapt            [ AC_MSG_RESULT([no])
553290067Sbapt              ax_header_version_match='no'
554290067Sbapt            ])
555290067Sbapt        ],
556290067Sbapt        [ AC_MSG_WARN([cross compiling so assuming header version number matches])
557290067Sbapt          ax_header_version_match='yes'
558290067Sbapt        ])
559290067Sbapt    ])
560290067Sbapt
561290067Sbapt  dnl Was LUA_INCLUDE specified?
562290067Sbapt  AS_IF([test "x$ax_header_version_match" != 'xyes' &&
563290067Sbapt         test "x$LUA_INCLUDE" != 'x'],
564290067Sbapt    [AC_MSG_ERROR([cannot find headers for specified LUA_INCLUDE])])
565290067Sbapt
566290067Sbapt  dnl Test the final result and run user code.
567290067Sbapt  AS_IF([test "x$ax_header_version_match" = 'xyes'], [$1],
568290067Sbapt    [m4_default([$2], [AC_MSG_ERROR([cannot find Lua includes])])])
569290067Sbapt])
570290067Sbapt
571290067Sbaptdnl AX_LUA_HEADERS_VERSION no longer exists, use AX_LUA_HEADERS.
572290067SbaptAC_DEFUN([AX_LUA_HEADERS_VERSION],
573290067Sbapt[
574290067Sbapt  AC_MSG_WARN([[$0 is deprecated, please use AX_LUA_HEADERS instead]])
575290067Sbapt])
576290067Sbapt
577290067Sbapt
578290067Sbaptdnl =========================================================================
579290067Sbaptdnl AX_LUA_LIBS([ACTION-IF-FOUND], [ACTION-IF-NOT-FOUND])
580290067Sbaptdnl =========================================================================
581290067SbaptAC_DEFUN([AX_LUA_LIBS],
582290067Sbapt[
583290067Sbapt  dnl TODO Should this macro also check various -L flags?
584290067Sbapt
585290067Sbapt  dnl Check for LUA_VERSION.
586290067Sbapt  AC_MSG_CHECKING([if LUA_VERSION is defined])
587290067Sbapt  AS_IF([test "x$LUA_VERSION" != 'x'],
588290067Sbapt    [AC_MSG_RESULT([yes])],
589290067Sbapt    [ AC_MSG_RESULT([no])
590290067Sbapt      AC_MSG_ERROR([cannot check Lua libs without knowing LUA_VERSION])
591290067Sbapt    ])
592290067Sbapt
593290067Sbapt  dnl Make LUA_LIB a precious variable.
594290067Sbapt  AC_ARG_VAR([LUA_LIB], [The Lua library, e.g. -llua5.1])
595290067Sbapt
596290067Sbapt  AS_IF([test "x$LUA_LIB" != 'x'],
597290067Sbapt  [ dnl Check that LUA_LIBS works.
598290067Sbapt    _ax_lua_saved_libs=$LIBS
599290067Sbapt    LIBS="$LUA_LIB $LIBS"
600290067Sbapt    AC_SEARCH_LIBS([lua_load], [],
601290067Sbapt      [_ax_found_lua_libs='yes'],
602290067Sbapt      [_ax_found_lua_libs='no'])
603290067Sbapt    LIBS=$_ax_lua_saved_libs
604290067Sbapt
605290067Sbapt    dnl Check the result.
606290067Sbapt    AS_IF([test "x$_ax_found_lua_libs" != 'xyes'],
607290067Sbapt      [AC_MSG_ERROR([cannot find libs for specified LUA_LIB])])
608290067Sbapt  ],
609290067Sbapt  [ dnl First search for extra libs.
610290067Sbapt    _ax_lua_extra_libs=''
611290067Sbapt
612290067Sbapt    _ax_lua_saved_libs=$LIBS
613290067Sbapt    LIBS="$LUA_LIB $LIBS"
614290067Sbapt    AC_SEARCH_LIBS([exp], [m])
615290067Sbapt    AC_SEARCH_LIBS([dlopen], [dl])
616290067Sbapt    LIBS=$_ax_lua_saved_libs
617290067Sbapt
618290067Sbapt    AS_IF([test "x$ac_cv_search_exp" != 'xno' &&
619290067Sbapt           test "x$ac_cv_search_exp" != 'xnone required'],
620290067Sbapt      [_ax_lua_extra_libs="$_ax_lua_extra_libs $ac_cv_search_exp"])
621290067Sbapt
622290067Sbapt    AS_IF([test "x$ac_cv_search_dlopen" != 'xno' &&
623290067Sbapt           test "x$ac_cv_search_dlopen" != 'xnone required'],
624290067Sbapt      [_ax_lua_extra_libs="$_ax_lua_extra_libs $ac_cv_search_dlopen"])
625290067Sbapt
626290067Sbapt    dnl Try to find the Lua libs.
627290067Sbapt    _ax_lua_saved_libs=$LIBS
628290067Sbapt    LIBS="$LUA_LIB $LIBS"
629290067Sbapt    AC_SEARCH_LIBS([lua_load],
630290067Sbapt      [ lua$LUA_VERSION \
631290067Sbapt        lua$LUA_SHORT_VERSION \
632290067Sbapt        lua-$LUA_VERSION \
633290067Sbapt        lua-$LUA_SHORT_VERSION \
634290067Sbapt        lua \
635290067Sbapt      ],
636290067Sbapt      [_ax_found_lua_libs='yes'],
637290067Sbapt      [_ax_found_lua_libs='no'],
638290067Sbapt      [$_ax_lua_extra_libs])
639290067Sbapt    LIBS=$_ax_lua_saved_libs
640290067Sbapt
641290067Sbapt    AS_IF([test "x$ac_cv_search_lua_load" != 'xno' &&
642290067Sbapt           test "x$ac_cv_search_lua_load" != 'xnone required'],
643290067Sbapt      [LUA_LIB="$ac_cv_search_lua_load $_ax_lua_extra_libs"])
644290067Sbapt  ])
645290067Sbapt
646290067Sbapt  dnl Test the result and run user code.
647290067Sbapt  AS_IF([test "x$_ax_found_lua_libs" = 'xyes'], [$1],
648290067Sbapt    [m4_default([$2], [AC_MSG_ERROR([cannot find Lua libs])])])
649290067Sbapt])
650290067Sbapt
651290067Sbapt
652290067Sbaptdnl =========================================================================
653290067Sbaptdnl AX_LUA_READLINE([ACTION-IF-FOUND], [ACTION-IF-NOT-FOUND])
654290067Sbaptdnl =========================================================================
655290067SbaptAC_DEFUN([AX_LUA_READLINE],
656290067Sbapt[
657290067Sbapt  AX_LIB_READLINE
658290067Sbapt  AS_IF([test "x$ac_cv_header_readline_readline_h" != 'x' &&
659290067Sbapt         test "x$ac_cv_header_readline_history_h" != 'x'],
660290067Sbapt    [ LUA_LIBS_CFLAGS="-DLUA_USE_READLINE $LUA_LIBS_CFLAGS"
661290067Sbapt      $1
662290067Sbapt    ],
663290067Sbapt    [$2])
664290067Sbapt])
665