xref: /freebsd/contrib/libucl/m4/ax_lua.m4 (revision cc426dd31990b8b50b210efc450e404596548ca1)
1# ===========================================================================
2#          http://www.gnu.org/software/autoconf-archive/ax_lua.html
3# ===========================================================================
4#
5# SYNOPSIS
6#
7#   AX_PROG_LUA[([MINIMUM-VERSION], [TOO-BIG-VERSION], [ACTION-IF-FOUND], [ACTION-IF-NOT-FOUND])]
8#   AX_LUA_HEADERS[([ACTION-IF-FOUND], [ACTION-IF-NOT-FOUND])]
9#   AX_LUA_LIBS[([ACTION-IF-FOUND], [ACTION-IF-NOT-FOUND])]
10#   AX_LUA_READLINE[([ACTION-IF-FOUND], [ACTION-IF-NOT-FOUND])]
11#
12# DESCRIPTION
13#
14#   Detect a Lua interpreter, optionally specifying a minimum and maximum
15#   version number. Set up important Lua paths, such as the directories in
16#   which to install scripts and modules (shared libraries).
17#
18#   Also detect Lua headers and libraries. The Lua version contained in the
19#   header is checked to match the Lua interpreter version exactly. When
20#   searching for Lua libraries, the version number is used as a suffix.
21#   This is done with the goal of supporting multiple Lua installs (5.1,
22#   5.2, and 5.3 side-by-side).
23#
24#   A note on compatibility with previous versions: This file has been
25#   mostly rewritten for serial 18. Most developers should be able to use
26#   these macros without needing to modify configure.ac. Care has been taken
27#   to preserve each macro's behavior, but there are some differences:
28#
29#   1) AX_WITH_LUA is deprecated; it now expands to the exact same thing as
30#   AX_PROG_LUA with no arguments.
31#
32#   2) AX_LUA_HEADERS now checks that the version number defined in lua.h
33#   matches the interpreter version. AX_LUA_HEADERS_VERSION is therefore
34#   unnecessary, so it is deprecated and does not expand to anything.
35#
36#   3) The configure flag --with-lua-suffix no longer exists; the user
37#   should instead specify the LUA precious variable on the command line.
38#   See the AX_PROG_LUA description for details.
39#
40#   Please read the macro descriptions below for more information.
41#
42#   This file was inspired by Andrew Dalke's and James Henstridge's
43#   python.m4 and Tom Payne's, Matthieu Moy's, and Reuben Thomas's ax_lua.m4
44#   (serial 17). Basically, this file is a mash-up of those two files. I
45#   like to think it combines the best of the two!
46#
47#   AX_PROG_LUA: Search for the Lua interpreter, and set up important Lua
48#   paths. Adds precious variable LUA, which may contain the path of the Lua
49#   interpreter. If LUA is blank, the user's path is searched for an
50#   suitable interpreter.
51#
52#   If MINIMUM-VERSION is supplied, then only Lua interpreters with a
53#   version number greater or equal to MINIMUM-VERSION will be accepted. If
54#   TOO-BIG-VERSION is also supplied, then only Lua interpreters with a
55#   version number greater or equal to MINIMUM-VERSION and less than
56#   TOO-BIG-VERSION will be accepted.
57#
58#   The Lua version number, LUA_VERSION, is found from the interpreter, and
59#   substituted. LUA_PLATFORM is also found, but not currently supported (no
60#   standard representation).
61#
62#   Finally, the macro finds four paths:
63#
64#     luadir             Directory to install Lua scripts.
65#     pkgluadir          $luadir/$PACKAGE
66#     luaexecdir         Directory to install Lua modules.
67#     pkgluaexecdir      $luaexecdir/$PACKAGE
68#
69#   These paths are found based on $prefix, $exec_prefix, Lua's
70#   package.path, and package.cpath. The first path of package.path
71#   beginning with $prefix is selected as luadir. The first path of
72#   package.cpath beginning with $exec_prefix is used as luaexecdir. This
73#   should work on all reasonable Lua installations. If a path cannot be
74#   determined, a default path is used. Of course, the user can override
75#   these later when invoking make.
76#
77#     luadir             Default: $prefix/share/lua/$LUA_VERSION
78#     luaexecdir         Default: $exec_prefix/lib/lua/$LUA_VERSION
79#
80#   These directories can be used by Automake as install destinations. The
81#   variable name minus 'dir' needs to be used as a prefix to the
82#   appropriate Automake primary, e.g. lua_SCRIPS or luaexec_LIBRARIES.
83#
84#   If an acceptable Lua interpreter is found, then ACTION-IF-FOUND is
85#   performed, otherwise ACTION-IF-NOT-FOUND is preformed. If ACTION-IF-NOT-
86#   FOUND is blank, then it will default to printing an error. To prevent
87#   the default behavior, give ':' as an action.
88#
89#   AX_LUA_HEADERS: Search for Lua headers. Requires that AX_PROG_LUA be
90#   expanded before this macro. Adds precious variable LUA_INCLUDE, which
91#   may contain Lua specific include flags, e.g. -I/usr/include/lua5.1. If
92#   LUA_INCLUDE is blank, then this macro will attempt to find suitable
93#   flags.
94#
95#   LUA_INCLUDE can be used by Automake to compile Lua modules or
96#   executables with embedded interpreters. The *_CPPFLAGS variables should
97#   be used for this purpose, e.g. myprog_CPPFLAGS = $(LUA_INCLUDE).
98#
99#   This macro searches for the header lua.h (and others). The search is
100#   performed with a combination of CPPFLAGS, CPATH, etc, and LUA_INCLUDE.
101#   If the search is unsuccessful, then some common directories are tried.
102#   If the headers are then found, then LUA_INCLUDE is set accordingly.
103#
104#   The paths automatically searched are:
105#
106#     * /usr/include/luaX.Y
107#     * /usr/include/lua/X.Y
108#     * /usr/include/luaXY
109#     * /usr/local/include/luaX.Y
110#     * /usr/local/include/lua-X.Y
111#     * /usr/local/include/lua/X.Y
112#     * /usr/local/include/luaXY
113#
114#   (Where X.Y is the Lua version number, e.g. 5.1.)
115#
116#   The Lua version number found in the headers is always checked to match
117#   the Lua interpreter's version number. Lua headers with mismatched
118#   version numbers are not accepted.
119#
120#   If headers are found, then ACTION-IF-FOUND is performed, otherwise
121#   ACTION-IF-NOT-FOUND is performed. If ACTION-IF-NOT-FOUND is blank, then
122#   it will default to printing an error. To prevent the default behavior,
123#   set the action to ':'.
124#
125#   AX_LUA_LIBS: Search for Lua libraries. Requires that AX_PROG_LUA be
126#   expanded before this macro. Adds precious variable LUA_LIB, which may
127#   contain Lua specific linker flags, e.g. -llua5.1. If LUA_LIB is blank,
128#   then this macro will attempt to find suitable flags.
129#
130#   LUA_LIB can be used by Automake to link Lua modules or executables with
131#   embedded interpreters. The *_LIBADD and *_LDADD variables should be used
132#   for this purpose, e.g. mymod_LIBADD = $(LUA_LIB).
133#
134#   This macro searches for the Lua library. More technically, it searches
135#   for a library containing the function lua_load. The search is performed
136#   with a combination of LIBS, LIBRARY_PATH, and LUA_LIB.
137#
138#   If the search determines that some linker flags are missing, then those
139#   flags will be added to LUA_LIB.
140#
141#   If libraries are found, then ACTION-IF-FOUND is performed, otherwise
142#   ACTION-IF-NOT-FOUND is performed. If ACTION-IF-NOT-FOUND is blank, then
143#   it will default to printing an error. To prevent the default behavior,
144#   set the action to ':'.
145#
146#   AX_LUA_READLINE: Search for readline headers and libraries. Requires the
147#   AX_LIB_READLINE macro, which is provided by ax_lib_readline.m4 from the
148#   Autoconf Archive.
149#
150#   If a readline compatible library is found, then ACTION-IF-FOUND is
151#   performed, otherwise ACTION-IF-NOT-FOUND is performed.
152#
153# LICENSE
154#
155#   Copyright (c) 2015 Reuben Thomas <rrt@sc3d.org>
156#   Copyright (c) 2014 Tim Perkins <tprk77@gmail.com>
157#
158#   This program is free software: you can redistribute it and/or modify it
159#   under the terms of the GNU General Public License as published by the
160#   Free Software Foundation, either version 3 of the License, or (at your
161#   option) any later version.
162#
163#   This program is distributed in the hope that it will be useful, but
164#   WITHOUT ANY WARRANTY; without even the implied warranty of
165#   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General
166#   Public License for more details.
167#
168#   You should have received a copy of the GNU General Public License along
169#   with this program. If not, see <http://www.gnu.org/licenses/>.
170#
171#   As a special exception, the respective Autoconf Macro's copyright owner
172#   gives unlimited permission to copy, distribute and modify the configure
173#   scripts that are the output of Autoconf when processing the Macro. You
174#   need not follow the terms of the GNU General Public License when using
175#   or distributing such scripts, even though portions of the text of the
176#   Macro appear in them. The GNU General Public License (GPL) does govern
177#   all other use of the material that constitutes the Autoconf Macro.
178#
179#   This special exception to the GPL applies to versions of the Autoconf
180#   Macro released by the Autoconf Archive. When you make and distribute a
181#   modified version of the Autoconf Macro, you may extend this special
182#   exception to the GPL to apply to your modified version as well.
183
184#serial 39
185
186dnl =========================================================================
187dnl AX_PROG_LUA([MINIMUM-VERSION], [TOO-BIG-VERSION],
188dnl             [ACTION-IF-FOUND], [ACTION-IF-NOT-FOUND])
189dnl =========================================================================
190AC_DEFUN([AX_PROG_LUA],
191[
192  dnl Check for required tools.
193  AC_REQUIRE([AC_PROG_GREP])
194  AC_REQUIRE([AC_PROG_SED])
195
196  dnl Make LUA a precious variable.
197  AC_ARG_VAR([LUA], [The Lua interpreter, e.g. /usr/bin/lua5.1])
198
199  dnl Find a Lua interpreter.
200  m4_define_default([_AX_LUA_INTERPRETER_LIST],
201    [lua lua5.3 lua53 lua5.2 lua52 lua5.1 lua51 lua50])
202
203  m4_if([$1], [],
204  [ dnl No version check is needed. Find any Lua interpreter.
205    AS_IF([test "x$LUA" = 'x'],
206      [AC_PATH_PROGS([LUA], [_AX_LUA_INTERPRETER_LIST], [:])])
207    ax_display_LUA='lua'
208
209    AS_IF([test "x$LUA" != 'x:'],
210      [ dnl At least check if this is a Lua interpreter.
211        AC_MSG_CHECKING([if $LUA is a Lua interpreter])
212        _AX_LUA_CHK_IS_INTRP([$LUA],
213          [AC_MSG_RESULT([yes])],
214          [ AC_MSG_RESULT([no])
215            AC_MSG_ERROR([not a Lua interpreter])
216          ])
217      ])
218  ],
219  [ dnl A version check is needed.
220    AS_IF([test "x$LUA" != 'x'],
221    [ dnl Check if this is a Lua interpreter.
222      AC_MSG_CHECKING([if $LUA is a Lua interpreter])
223      _AX_LUA_CHK_IS_INTRP([$LUA],
224        [AC_MSG_RESULT([yes])],
225        [ AC_MSG_RESULT([no])
226          AC_MSG_ERROR([not a Lua interpreter])
227        ])
228      dnl Check the version.
229      m4_if([$2], [],
230        [_ax_check_text="whether $LUA version >= $1"],
231        [_ax_check_text="whether $LUA version >= $1, < $2"])
232      AC_MSG_CHECKING([$_ax_check_text])
233      _AX_LUA_CHK_VER([$LUA], [$1], [$2],
234        [AC_MSG_RESULT([yes])],
235        [ AC_MSG_RESULT([no])
236          AC_MSG_ERROR([version is out of range for specified LUA])])
237      ax_display_LUA=$LUA
238    ],
239    [ dnl Try each interpreter until we find one that satisfies VERSION.
240      m4_if([$2], [],
241        [_ax_check_text="for a Lua interpreter with version >= $1"],
242        [_ax_check_text="for a Lua interpreter with version >= $1, < $2"])
243      AC_CACHE_CHECK([$_ax_check_text],
244        [ax_cv_pathless_LUA],
245        [ for ax_cv_pathless_LUA in _AX_LUA_INTERPRETER_LIST none; do
246            test "x$ax_cv_pathless_LUA" = 'xnone' && break
247            _AX_LUA_CHK_IS_INTRP([$ax_cv_pathless_LUA], [], [continue])
248            _AX_LUA_CHK_VER([$ax_cv_pathless_LUA], [$1], [$2], [break])
249          done
250        ])
251      dnl Set $LUA to the absolute path of $ax_cv_pathless_LUA.
252      AS_IF([test "x$ax_cv_pathless_LUA" = 'xnone'],
253        [LUA=':'],
254        [AC_PATH_PROG([LUA], [$ax_cv_pathless_LUA])])
255      ax_display_LUA=$ax_cv_pathless_LUA
256    ])
257  ])
258
259  AS_IF([test "x$LUA" = 'x:'],
260  [ dnl Run any user-specified action, or abort.
261    m4_default([$4], [AC_MSG_ERROR([cannot find suitable Lua interpreter])])
262  ],
263  [ dnl Query Lua for its version number.
264    AC_CACHE_CHECK([for $ax_display_LUA version],
265      [ax_cv_lua_version],
266      [ dnl Get the interpreter version in X.Y format. This should work for
267        dnl interpreters version 5.0 and beyond.
268        ax_cv_lua_version=[`$LUA -e '
269          -- return a version number in X.Y format
270          local _, _, ver = string.find(_VERSION, "^Lua (%d+%.%d+)")
271          print(ver)'`]
272      ])
273    AS_IF([test "x$ax_cv_lua_version" = 'x'],
274      [AC_MSG_ERROR([invalid Lua version number])])
275    AC_SUBST([LUA_VERSION], [$ax_cv_lua_version])
276    AC_SUBST([LUA_SHORT_VERSION], [`echo "$LUA_VERSION" | $SED 's|\.||'`])
277
278    dnl The following check is not supported:
279    dnl At times (like when building shared libraries) you may want to know
280    dnl which OS platform Lua thinks this is.
281    AC_CACHE_CHECK([for $ax_display_LUA platform],
282      [ax_cv_lua_platform],
283      [ax_cv_lua_platform=[`$LUA -e 'print("unknown")'`]])
284    AC_SUBST([LUA_PLATFORM], [$ax_cv_lua_platform])
285
286    dnl Use the values of $prefix and $exec_prefix for the corresponding
287    dnl values of LUA_PREFIX and LUA_EXEC_PREFIX. These are made distinct
288    dnl variables so they can be overridden if need be. However, the general
289    dnl consensus is that you shouldn't need this ability.
290    AC_SUBST([LUA_PREFIX], ['${prefix}'])
291    AC_SUBST([LUA_EXEC_PREFIX], ['${exec_prefix}'])
292
293    dnl Lua provides no way to query the script directory, and instead
294    dnl provides LUA_PATH. However, we should be able to make a safe educated
295    dnl guess. If the built-in search path contains a directory which is
296    dnl prefixed by $prefix, then we can store scripts there. The first
297    dnl matching path will be used.
298    AC_CACHE_CHECK([for $ax_display_LUA script directory],
299      [ax_cv_lua_luadir],
300      [ AS_IF([test "x$prefix" = 'xNONE'],
301          [ax_lua_prefix=$ac_default_prefix],
302          [ax_lua_prefix=$prefix])
303
304        dnl Initialize to the default path.
305        ax_cv_lua_luadir="$LUA_PREFIX/share/lua/$LUA_VERSION"
306
307        dnl Try to find a path with the prefix.
308        _AX_LUA_FND_PRFX_PTH([$LUA], [$ax_lua_prefix], [script])
309        AS_IF([test "x$ax_lua_prefixed_path" != 'x'],
310        [ dnl Fix the prefix.
311          _ax_strip_prefix=`echo "$ax_lua_prefix" | $SED 's|.|.|g'`
312          ax_cv_lua_luadir=`echo "$ax_lua_prefixed_path" | \
313            $SED "s|^$_ax_strip_prefix|$LUA_PREFIX|"`
314        ])
315      ])
316    AC_SUBST([luadir], [$ax_cv_lua_luadir])
317    AC_SUBST([pkgluadir], [\${luadir}/$PACKAGE])
318
319    dnl Lua provides no way to query the module directory, and instead
320    dnl provides LUA_PATH. However, we should be able to make a safe educated
321    dnl guess. If the built-in search path contains a directory which is
322    dnl prefixed by $exec_prefix, then we can store modules there. The first
323    dnl matching path will be used.
324    AC_CACHE_CHECK([for $ax_display_LUA module directory],
325      [ax_cv_lua_luaexecdir],
326      [ AS_IF([test "x$exec_prefix" = 'xNONE'],
327          [ax_lua_exec_prefix=$ax_lua_prefix],
328          [ax_lua_exec_prefix=$exec_prefix])
329
330        dnl Initialize to the default path.
331        ax_cv_lua_luaexecdir="$LUA_EXEC_PREFIX/lib/lua/$LUA_VERSION"
332
333        dnl Try to find a path with the prefix.
334        _AX_LUA_FND_PRFX_PTH([$LUA],
335          [$ax_lua_exec_prefix], [module])
336        AS_IF([test "x$ax_lua_prefixed_path" != 'x'],
337        [ dnl Fix the prefix.
338          _ax_strip_prefix=`echo "$ax_lua_exec_prefix" | $SED 's|.|.|g'`
339          ax_cv_lua_luaexecdir=`echo "$ax_lua_prefixed_path" | \
340            $SED "s|^$_ax_strip_prefix|$LUA_EXEC_PREFIX|"`
341        ])
342      ])
343    AC_SUBST([luaexecdir], [$ax_cv_lua_luaexecdir])
344    AC_SUBST([pkgluaexecdir], [\${luaexecdir}/$PACKAGE])
345
346    dnl Run any user specified action.
347    $3
348  ])
349])
350
351dnl AX_WITH_LUA is now the same thing as AX_PROG_LUA.
352AC_DEFUN([AX_WITH_LUA],
353[
354  AC_MSG_WARN([[$0 is deprecated, please use AX_PROG_LUA instead]])
355  AX_PROG_LUA
356])
357
358
359dnl =========================================================================
360dnl _AX_LUA_CHK_IS_INTRP(PROG, [ACTION-IF-TRUE], [ACTION-IF-FALSE])
361dnl =========================================================================
362AC_DEFUN([_AX_LUA_CHK_IS_INTRP],
363[
364  dnl A minimal Lua factorial to prove this is an interpreter. This should work
365  dnl for Lua interpreters version 5.0 and beyond.
366  _ax_lua_factorial=[`$1 2>/dev/null -e '
367    -- a simple factorial
368    function fact (n)
369      if n == 0 then
370        return 1
371      else
372        return n * fact(n-1)
373      end
374    end
375    print("fact(5) is " .. fact(5))'`]
376  AS_IF([test "$_ax_lua_factorial" = 'fact(5) is 120'],
377    [$2], [$3])
378])
379
380
381dnl =========================================================================
382dnl _AX_LUA_CHK_VER(PROG, MINIMUM-VERSION, [TOO-BIG-VERSION],
383dnl                 [ACTION-IF-TRUE], [ACTION-IF-FALSE])
384dnl =========================================================================
385AC_DEFUN([_AX_LUA_CHK_VER],
386[
387  dnl Check that the Lua version is within the bounds. Only the major and minor
388  dnl version numbers are considered. This should work for Lua interpreters
389  dnl version 5.0 and beyond.
390  _ax_lua_good_version=[`$1 -e '
391    -- a script to compare versions
392    function verstr2num(verstr)
393      local _, _, majorver, minorver = string.find(verstr, "^(%d+)%.(%d+)")
394      if majorver and minorver then
395        return tonumber(majorver) * 100 + tonumber(minorver)
396      end
397    end
398    local minver = verstr2num("$2")
399    local _, _, trimver = string.find(_VERSION, "^Lua (.*)")
400    local ver = verstr2num(trimver)
401    local maxver = verstr2num("$3") or 1e9
402    if minver <= ver and ver < maxver then
403      print("yes")
404    else
405      print("no")
406    end'`]
407    AS_IF([test "x$_ax_lua_good_version" = "xyes"],
408      [$4], [$5])
409])
410
411
412dnl =========================================================================
413dnl _AX_LUA_FND_PRFX_PTH(PROG, PREFIX, SCRIPT-OR-MODULE-DIR)
414dnl =========================================================================
415AC_DEFUN([_AX_LUA_FND_PRFX_PTH],
416[
417  dnl Get the script or module directory by querying the Lua interpreter,
418  dnl filtering on the given prefix, and selecting the shallowest path. If no
419  dnl path is found matching the prefix, the result will be an empty string.
420  dnl The third argument determines the type of search, it can be 'script' or
421  dnl 'module'. Supplying 'script' will perform the search with package.path
422  dnl and LUA_PATH, and supplying 'module' will search with package.cpath and
423  dnl LUA_CPATH. This is done for compatibility with Lua 5.0.
424
425  ax_lua_prefixed_path=[`$1 -e '
426    -- get the path based on search type
427    local searchtype = "$3"
428    local paths = ""
429    if searchtype == "script" then
430      paths = (package and package.path) or LUA_PATH
431    elseif searchtype == "module" then
432      paths = (package and package.cpath) or LUA_CPATH
433    end
434    -- search for the prefix
435    local prefix = "'$2'"
436    local minpath = ""
437    local mindepth = 1e9
438    string.gsub(paths, "(@<:@^;@:>@+)",
439      function (path)
440        path = string.gsub(path, "%?.*$", "")
441        path = string.gsub(path, "/@<:@^/@:>@*$", "")
442        if string.find(path, prefix) then
443          local depth = string.len(string.gsub(path, "@<:@^/@:>@", ""))
444          if depth < mindepth then
445            minpath = path
446            mindepth = depth
447          end
448        end
449      end)
450    print(minpath)'`]
451])
452
453
454dnl =========================================================================
455dnl AX_LUA_HEADERS([ACTION-IF-FOUND], [ACTION-IF-NOT-FOUND])
456dnl =========================================================================
457AC_DEFUN([AX_LUA_HEADERS],
458[
459  dnl Check for LUA_VERSION.
460  AC_MSG_CHECKING([if LUA_VERSION is defined])
461  AS_IF([test "x$LUA_VERSION" != 'x'],
462    [AC_MSG_RESULT([yes])],
463    [ AC_MSG_RESULT([no])
464      AC_MSG_ERROR([cannot check Lua headers without knowing LUA_VERSION])
465    ])
466
467  dnl Make LUA_INCLUDE a precious variable.
468  AC_ARG_VAR([LUA_INCLUDE], [The Lua includes, e.g. -I/usr/include/lua5.1])
469
470  dnl Some default directories to search.
471  LUA_SHORT_VERSION=`echo "$LUA_VERSION" | $SED 's|\.||'`
472  m4_define_default([_AX_LUA_INCLUDE_LIST],
473    [ /usr/include/lua$LUA_VERSION \
474      /usr/include/lua-$LUA_VERSION \
475      /usr/include/lua/$LUA_VERSION \
476      /usr/include/lua$LUA_SHORT_VERSION \
477      /usr/local/include/lua$LUA_VERSION \
478      /usr/local/include/lua-$LUA_VERSION \
479      /usr/local/include/lua/$LUA_VERSION \
480      /usr/local/include/lua$LUA_SHORT_VERSION \
481    ])
482
483  dnl Try to find the headers.
484  _ax_lua_saved_cppflags=$CPPFLAGS
485  CPPFLAGS="$CPPFLAGS $LUA_INCLUDE"
486  AC_CHECK_HEADERS([lua.h lualib.h lauxlib.h luaconf.h])
487  CPPFLAGS=$_ax_lua_saved_cppflags
488
489  dnl Try some other directories if LUA_INCLUDE was not set.
490  AS_IF([test "x$LUA_INCLUDE" = 'x' &&
491         test "x$ac_cv_header_lua_h" != 'xyes'],
492    [ dnl Try some common include paths.
493      for _ax_include_path in _AX_LUA_INCLUDE_LIST; do
494        test ! -d "$_ax_include_path" && continue
495
496        AC_MSG_CHECKING([for Lua headers in])
497        AC_MSG_RESULT([$_ax_include_path])
498
499        AS_UNSET([ac_cv_header_lua_h])
500        AS_UNSET([ac_cv_header_lualib_h])
501        AS_UNSET([ac_cv_header_lauxlib_h])
502        AS_UNSET([ac_cv_header_luaconf_h])
503
504        _ax_lua_saved_cppflags=$CPPFLAGS
505        CPPFLAGS="$CPPFLAGS -I$_ax_include_path"
506        AC_CHECK_HEADERS([lua.h lualib.h lauxlib.h luaconf.h])
507        CPPFLAGS=$_ax_lua_saved_cppflags
508
509        AS_IF([test "x$ac_cv_header_lua_h" = 'xyes'],
510          [ LUA_INCLUDE="-I$_ax_include_path"
511            break
512          ])
513      done
514    ])
515
516  AS_IF([test "x$ac_cv_header_lua_h" = 'xyes'],
517    [ dnl Make a program to print LUA_VERSION defined in the header.
518      dnl TODO It would be really nice if we could do this without compiling a
519      dnl program, then it would work when cross compiling. But I'm not sure how
520      dnl to do this reliably. For now, assume versions match when cross compiling.
521
522      AS_IF([test "x$cross_compiling" != 'xyes'],
523        [ AC_CACHE_CHECK([for Lua header version],
524            [ax_cv_lua_header_version],
525            [ _ax_lua_saved_cppflags=$CPPFLAGS
526              CPPFLAGS="$CPPFLAGS $LUA_INCLUDE"
527              AC_RUN_IFELSE(
528                [ AC_LANG_SOURCE([[
529#include <lua.h>
530#include <stdlib.h>
531#include <stdio.h>
532int main(int argc, char ** argv)
533{
534  if(argc > 1) printf("%s", LUA_VERSION);
535  exit(EXIT_SUCCESS);
536}
537]])
538                ],
539                [ ax_cv_lua_header_version=`./conftest$EXEEXT p | \
540                    $SED -n "s|^Lua \(@<:@0-9@:>@\{1,\}\.@<:@0-9@:>@\{1,\}\).\{0,\}|\1|p"`
541                ],
542                [ax_cv_lua_header_version='unknown'])
543              CPPFLAGS=$_ax_lua_saved_cppflags
544            ])
545
546          dnl Compare this to the previously found LUA_VERSION.
547          AC_MSG_CHECKING([if Lua header version matches $LUA_VERSION])
548          AS_IF([test "x$ax_cv_lua_header_version" = "x$LUA_VERSION"],
549            [ AC_MSG_RESULT([yes])
550              ax_header_version_match='yes'
551            ],
552            [ AC_MSG_RESULT([no])
553              ax_header_version_match='no'
554            ])
555        ],
556        [ AC_MSG_WARN([cross compiling so assuming header version number matches])
557          ax_header_version_match='yes'
558        ])
559    ])
560
561  dnl Was LUA_INCLUDE specified?
562  AS_IF([test "x$ax_header_version_match" != 'xyes' &&
563         test "x$LUA_INCLUDE" != 'x'],
564    [AC_MSG_ERROR([cannot find headers for specified LUA_INCLUDE])])
565
566  dnl Test the final result and run user code.
567  AS_IF([test "x$ax_header_version_match" = 'xyes'], [$1],
568    [m4_default([$2], [AC_MSG_ERROR([cannot find Lua includes])])])
569])
570
571dnl AX_LUA_HEADERS_VERSION no longer exists, use AX_LUA_HEADERS.
572AC_DEFUN([AX_LUA_HEADERS_VERSION],
573[
574  AC_MSG_WARN([[$0 is deprecated, please use AX_LUA_HEADERS instead]])
575])
576
577
578dnl =========================================================================
579dnl AX_LUA_LIBS([ACTION-IF-FOUND], [ACTION-IF-NOT-FOUND])
580dnl =========================================================================
581AC_DEFUN([AX_LUA_LIBS],
582[
583  dnl TODO Should this macro also check various -L flags?
584
585  dnl Check for LUA_VERSION.
586  AC_MSG_CHECKING([if LUA_VERSION is defined])
587  AS_IF([test "x$LUA_VERSION" != 'x'],
588    [AC_MSG_RESULT([yes])],
589    [ AC_MSG_RESULT([no])
590      AC_MSG_ERROR([cannot check Lua libs without knowing LUA_VERSION])
591    ])
592
593  dnl Make LUA_LIB a precious variable.
594  AC_ARG_VAR([LUA_LIB], [The Lua library, e.g. -llua5.1])
595
596  AS_IF([test "x$LUA_LIB" != 'x'],
597  [ dnl Check that LUA_LIBS works.
598    _ax_lua_saved_libs=$LIBS
599    LIBS="$LUA_LIB $LIBS"
600    AC_SEARCH_LIBS([lua_load], [],
601      [_ax_found_lua_libs='yes'],
602      [_ax_found_lua_libs='no'])
603    LIBS=$_ax_lua_saved_libs
604
605    dnl Check the result.
606    AS_IF([test "x$_ax_found_lua_libs" != 'xyes'],
607      [AC_MSG_ERROR([cannot find libs for specified LUA_LIB])])
608  ],
609  [ dnl First search for extra libs.
610    _ax_lua_extra_libs=''
611
612    _ax_lua_saved_libs=$LIBS
613    LIBS="$LUA_LIB $LIBS"
614    AC_SEARCH_LIBS([exp], [m])
615    AC_SEARCH_LIBS([dlopen], [dl])
616    LIBS=$_ax_lua_saved_libs
617
618    AS_IF([test "x$ac_cv_search_exp" != 'xno' &&
619           test "x$ac_cv_search_exp" != 'xnone required'],
620      [_ax_lua_extra_libs="$_ax_lua_extra_libs $ac_cv_search_exp"])
621
622    AS_IF([test "x$ac_cv_search_dlopen" != 'xno' &&
623           test "x$ac_cv_search_dlopen" != 'xnone required'],
624      [_ax_lua_extra_libs="$_ax_lua_extra_libs $ac_cv_search_dlopen"])
625
626    dnl Try to find the Lua libs.
627    _ax_lua_saved_libs=$LIBS
628    LIBS="$LUA_LIB $LIBS"
629    AC_SEARCH_LIBS([lua_load],
630      [ lua$LUA_VERSION \
631        lua$LUA_SHORT_VERSION \
632        lua-$LUA_VERSION \
633        lua-$LUA_SHORT_VERSION \
634        lua \
635      ],
636      [_ax_found_lua_libs='yes'],
637      [_ax_found_lua_libs='no'],
638      [$_ax_lua_extra_libs])
639    LIBS=$_ax_lua_saved_libs
640
641    AS_IF([test "x$ac_cv_search_lua_load" != 'xno' &&
642           test "x$ac_cv_search_lua_load" != 'xnone required'],
643      [LUA_LIB="$ac_cv_search_lua_load $_ax_lua_extra_libs"])
644  ])
645
646  dnl Test the result and run user code.
647  AS_IF([test "x$_ax_found_lua_libs" = 'xyes'], [$1],
648    [m4_default([$2], [AC_MSG_ERROR([cannot find Lua libs])])])
649])
650
651
652dnl =========================================================================
653dnl AX_LUA_READLINE([ACTION-IF-FOUND], [ACTION-IF-NOT-FOUND])
654dnl =========================================================================
655AC_DEFUN([AX_LUA_READLINE],
656[
657  AX_LIB_READLINE
658  AS_IF([test "x$ac_cv_header_readline_readline_h" != 'x' &&
659         test "x$ac_cv_header_readline_history_h" != 'x'],
660    [ LUA_LIBS_CFLAGS="-DLUA_USE_READLINE $LUA_LIBS_CFLAGS"
661      $1
662    ],
663    [$2])
664])
665