Blame m4/ax_lua.m4

Packit 437b5e
# ===========================================================================
Packit 437b5e
#          http://www.gnu.org/software/autoconf-archive/ax_lua.html
Packit 437b5e
# ===========================================================================
Packit 437b5e
#
Packit 437b5e
# SYNOPSIS
Packit 437b5e
#
Packit 437b5e
#   AX_PROG_LUA[([MINIMUM-VERSION], [TOO-BIG-VERSION], [ACTION-IF-FOUND], [ACTION-IF-NOT-FOUND])]
Packit 437b5e
#   AX_LUA_HEADERS[([ACTION-IF-FOUND], [ACTION-IF-NOT-FOUND])]
Packit 437b5e
#   AX_LUA_LIBS[([ACTION-IF-FOUND], [ACTION-IF-NOT-FOUND])]
Packit 437b5e
#   AX_LUA_READLINE[([ACTION-IF-FOUND], [ACTION-IF-NOT-FOUND])]
Packit 437b5e
#
Packit 437b5e
# DESCRIPTION
Packit 437b5e
#
Packit 437b5e
#   Detect a Lua interpreter, optionally specifying a minimum and maximum
Packit 437b5e
#   version number. Set up important Lua paths, such as the directories in
Packit 437b5e
#   which to install scripts and modules (shared libraries).
Packit 437b5e
#
Packit 437b5e
#   Also detect Lua headers and libraries. The Lua version contained in the
Packit 437b5e
#   header is checked to match the Lua interpreter version exactly. When
Packit 437b5e
#   searching for Lua libraries, the version number is used as a suffix.
Packit 437b5e
#   This is done with the goal of supporting multiple Lua installs (5.1 and
Packit 437b5e
#   5.2 side-by-side).
Packit 437b5e
#
Packit 437b5e
#   A note on compatibility with previous versions: This file has been
Packit 437b5e
#   mostly rewritten for serial 18. Most developers should be able to use
Packit 437b5e
#   these macros without needing to modify configure.ac. Care has been taken
Packit 437b5e
#   to preserve each macro's behavior, but there are some differences:
Packit 437b5e
#
Packit 437b5e
#   1) AX_WITH_LUA is deprecated; it now expands to the exact same thing as
Packit 437b5e
#   AX_PROG_LUA with no arguments.
Packit 437b5e
#
Packit 437b5e
#   2) AX_LUA_HEADERS now checks that the version number defined in lua.h
Packit 437b5e
#   matches the interpreter version. AX_LUA_HEADERS_VERSION is therefore
Packit 437b5e
#   unnecessary, so it is deprecated and does not expand to anything.
Packit 437b5e
#
Packit 437b5e
#   3) The configure flag --with-lua-suffix no longer exists; the user
Packit 437b5e
#   should instead specify the LUA precious variable on the command line.
Packit 437b5e
#   See the AX_PROG_LUA description for details.
Packit 437b5e
#
Packit 437b5e
#   Please read the macro descriptions below for more information.
Packit 437b5e
#
Packit 437b5e
#   This file was inspired by Andrew Dalke's and James Henstridge's
Packit 437b5e
#   python.m4 and Tom Payne's, Matthieu Moy's, and Reuben Thomas's ax_lua.m4
Packit 437b5e
#   (serial 17). Basically, this file is a mash-up of those two files. I
Packit 437b5e
#   like to think it combines the best of the two!
Packit 437b5e
#
Packit 437b5e
#   AX_PROG_LUA: Search for the Lua interpreter, and set up important Lua
Packit 437b5e
#   paths. Adds precious variable LUA, which may contain the path of the Lua
Packit 437b5e
#   interpreter. If LUA is blank, the user's path is searched for an
Packit 437b5e
#   suitable interpreter.
Packit 437b5e
#
Packit 437b5e
#   If MINIMUM-VERSION is supplied, then only Lua interpreters with a
Packit 437b5e
#   version number greater or equal to MINIMUM-VERSION will be accepted. If
Packit 437b5e
#   TOO-BIG-VERSION is also supplied, then only Lua interpreters with a
Packit 437b5e
#   version number greater or equal to MINIMUM-VERSION and less than
Packit 437b5e
#   TOO-BIG-VERSION will be accepted.
Packit 437b5e
#
Packit 437b5e
#   The Lua version number, LUA_VERSION, is found from the interpreter, and
Packit 437b5e
#   substituted. LUA_PLATFORM is also found, but not currently supported (no
Packit 437b5e
#   standard representation).
Packit 437b5e
#
Packit 437b5e
#   Finally, the macro finds four paths:
Packit 437b5e
#
Packit 437b5e
#     luadir             Directory to install Lua scripts.
Packit 437b5e
#     pkgluadir          $luadir/$PACKAGE
Packit 437b5e
#     luaexecdir         Directory to install Lua modules.
Packit 437b5e
#     pkgluaexecdir      $luaexecdir/$PACKAGE
Packit 437b5e
#
Packit 437b5e
#   These paths are found based on $prefix, $exec_prefix, Lua's
Packit 437b5e
#   package.path, and package.cpath. The first path of package.path
Packit 437b5e
#   beginning with $prefix is selected as luadir. The first path of
Packit 437b5e
#   package.cpath beginning with $exec_prefix is used as luaexecdir. This
Packit 437b5e
#   should work on all reasonable Lua installations. If a path cannot be
Packit 437b5e
#   determined, a default path is used. Of course, the user can override
Packit 437b5e
#   these later when invoking make.
Packit 437b5e
#
Packit 437b5e
#     luadir             Default: $prefix/share/lua/$LUA_VERSION
Packit 437b5e
#     luaexecdir         Default: $exec_prefix/lib/lua/$LUA_VERSION
Packit 437b5e
#
Packit 437b5e
#   These directories can be used by Automake as install destinations. The
Packit 437b5e
#   variable name minus 'dir' needs to be used as a prefix to the
Packit 437b5e
#   appropriate Automake primary, e.g. lua_SCRIPS or luaexec_LIBRARIES.
Packit 437b5e
#
Packit 437b5e
#   If an acceptable Lua interpreter is found, then ACTION-IF-FOUND is
Packit 437b5e
#   performed, otherwise ACTION-IF-NOT-FOUND is preformed. If ACTION-IF-NOT-
Packit 437b5e
#   FOUND is blank, then it will default to printing an error. To prevent
Packit 437b5e
#   the default behavior, give ':' as an action.
Packit 437b5e
#
Packit 437b5e
#   AX_LUA_HEADERS: Search for Lua headers. Requires that AX_PROG_LUA be
Packit 437b5e
#   expanded before this macro. Adds precious variable LUA_INCLUDE, which
Packit 437b5e
#   may contain Lua specific include flags, e.g. -I/usr/include/lua5.1. If
Packit 437b5e
#   LUA_INCLUDE is blank, then this macro will attempt to find suitable
Packit 437b5e
#   flags.
Packit 437b5e
#
Packit 437b5e
#   LUA_INCLUDE can be used by Automake to compile Lua modules or
Packit 437b5e
#   executables with embedded interpreters. The *_CPPFLAGS variables should
Packit 437b5e
#   be used for this purpose, e.g. myprog_CPPFLAGS = $(LUA_INCLUDE).
Packit 437b5e
#
Packit 437b5e
#   This macro searches for the header lua.h (and others). The search is
Packit 437b5e
#   performed with a combination of CPPFLAGS, CPATH, etc, and LUA_INCLUDE.
Packit 437b5e
#   If the search is unsuccessful, then some common directories are tried.
Packit 437b5e
#   If the headers are then found, then LUA_INCLUDE is set accordingly.
Packit 437b5e
#
Packit 437b5e
#   The paths automatically searched are:
Packit 437b5e
#
Packit 437b5e
#     * /usr/include/luaX.Y
Packit 437b5e
#     * /usr/include/lua/X.Y
Packit 437b5e
#     * /usr/include/luaXY
Packit 437b5e
#     * /usr/local/include/luaX.Y
Packit 437b5e
#     * /usr/local/include/lua-X.Y
Packit 437b5e
#     * /usr/local/include/lua/X.Y
Packit 437b5e
#     * /usr/local/include/luaXY
Packit 437b5e
#
Packit 437b5e
#   (Where X.Y is the Lua version number, e.g. 5.1.)
Packit 437b5e
#
Packit 437b5e
#   The Lua version number found in the headers is always checked to match
Packit 437b5e
#   the Lua interpreter's version number. Lua headers with mismatched
Packit 437b5e
#   version numbers are not accepted.
Packit 437b5e
#
Packit 437b5e
#   If headers are found, then ACTION-IF-FOUND is performed, otherwise
Packit 437b5e
#   ACTION-IF-NOT-FOUND is performed. If ACTION-IF-NOT-FOUND is blank, then
Packit 437b5e
#   it will default to printing an error. To prevent the default behavior,
Packit 437b5e
#   set the action to ':'.
Packit 437b5e
#
Packit 437b5e
#   AX_LUA_LIBS: Search for Lua libraries. Requires that AX_PROG_LUA be
Packit 437b5e
#   expanded before this macro. Adds precious variable LUA_LIB, which may
Packit 437b5e
#   contain Lua specific linker flags, e.g. -llua5.1. If LUA_LIB is blank,
Packit 437b5e
#   then this macro will attempt to find suitable flags.
Packit 437b5e
#
Packit 437b5e
#   LUA_LIB can be used by Automake to link Lua modules or executables with
Packit 437b5e
#   embedded interpreters. The *_LIBADD and *_LDADD variables should be used
Packit 437b5e
#   for this purpose, e.g. mymod_LIBADD = $(LUA_LIB).
Packit 437b5e
#
Packit 437b5e
#   This macro searches for the Lua library. More technically, it searches
Packit 437b5e
#   for a library containing the function lua_load. The search is performed
Packit 437b5e
#   with a combination of LIBS, LIBRARY_PATH, and LUA_LIB.
Packit 437b5e
#
Packit 437b5e
#   If the search determines that some linker flags are missing, then those
Packit 437b5e
#   flags will be added to LUA_LIB.
Packit 437b5e
#
Packit 437b5e
#   If libraries are found, then ACTION-IF-FOUND is performed, otherwise
Packit 437b5e
#   ACTION-IF-NOT-FOUND is performed. If ACTION-IF-NOT-FOUND is blank, then
Packit 437b5e
#   it will default to printing an error. To prevent the default behavior,
Packit 437b5e
#   set the action to ':'.
Packit 437b5e
#
Packit 437b5e
#   AX_LUA_READLINE: Search for readline headers and libraries. Requires the
Packit 437b5e
#   AX_LIB_READLINE macro, which is provided by ax_lib_readline.m4 from the
Packit 437b5e
#   Autoconf Archive.
Packit 437b5e
#
Packit 437b5e
#   If a readline compatible library is found, then ACTION-IF-FOUND is
Packit 437b5e
#   performed, otherwise ACTION-IF-NOT-FOUND is performed.
Packit 437b5e
#
Packit 437b5e
# LICENSE
Packit 437b5e
#
Packit 437b5e
#   Copyright (c) 2015 Reuben Thomas <rrt@sc3d.org>
Packit 437b5e
#   Copyright (c) 2014 Tim Perkins <tprk77@gmail.com>
Packit 437b5e
#
Packit 437b5e
#   This program is free software: you can redistribute it and/or modify it
Packit 437b5e
#   under the terms of the GNU General Public License as published by the
Packit 437b5e
#   Free Software Foundation, either version 3 of the License, or (at your
Packit 437b5e
#   option) any later version.
Packit 437b5e
#
Packit 437b5e
#   This program is distributed in the hope that it will be useful, but
Packit 437b5e
#   WITHOUT ANY WARRANTY; without even the implied warranty of
Packit 437b5e
#   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General
Packit 437b5e
#   Public License for more details.
Packit 437b5e
#
Packit 437b5e
#   You should have received a copy of the GNU General Public License along
Packit 437b5e
#   with this program. If not, see <http://www.gnu.org/licenses/>.
Packit 437b5e
#
Packit 437b5e
#   As a special exception, the respective Autoconf Macro's copyright owner
Packit 437b5e
#   gives unlimited permission to copy, distribute and modify the configure
Packit 437b5e
#   scripts that are the output of Autoconf when processing the Macro. You
Packit 437b5e
#   need not follow the terms of the GNU General Public License when using
Packit 437b5e
#   or distributing such scripts, even though portions of the text of the
Packit 437b5e
#   Macro appear in them. The GNU General Public License (GPL) does govern
Packit 437b5e
#   all other use of the material that constitutes the Autoconf Macro.
Packit 437b5e
#
Packit 437b5e
#   This special exception to the GPL applies to versions of the Autoconf
Packit 437b5e
#   Macro released by the Autoconf Archive. When you make and distribute a
Packit 437b5e
#   modified version of the Autoconf Macro, you may extend this special
Packit 437b5e
#   exception to the GPL to apply to your modified version as well.
Packit 437b5e
Packit 437b5e
#serial 38
Packit 437b5e
Packit 437b5e
dnl =========================================================================
Packit 437b5e
dnl AX_PROG_LUA([MINIMUM-VERSION], [TOO-BIG-VERSION],
Packit 437b5e
dnl             [ACTION-IF-FOUND], [ACTION-IF-NOT-FOUND])
Packit 437b5e
dnl =========================================================================
Packit 437b5e
AC_DEFUN([AX_PROG_LUA],
Packit 437b5e
[
Packit 437b5e
  dnl Check for required tools.
Packit 437b5e
  AC_REQUIRE([AC_PROG_GREP])
Packit 437b5e
  AC_REQUIRE([AC_PROG_SED])
Packit 437b5e
Packit 437b5e
  dnl Make LUA a precious variable.
Packit 437b5e
  AC_ARG_VAR([LUA], [The Lua interpreter, e.g. /usr/bin/lua5.1])
Packit 437b5e
Packit 437b5e
  dnl Find a Lua interpreter.
Packit 437b5e
  m4_define_default([_AX_LUA_INTERPRETER_LIST],
Packit 437b5e
    [lua lua5.2 lua52 lua5.1 lua51 lua50])
Packit 437b5e
Packit 437b5e
  m4_if([$1], [],
Packit 437b5e
  [ dnl No version check is needed. Find any Lua interpreter.
Packit 437b5e
    AS_IF([test "x$LUA" = 'x'],
Packit 437b5e
      [AC_PATH_PROGS([LUA], [_AX_LUA_INTERPRETER_LIST], [:])])
Packit 437b5e
    ax_display_LUA='lua'
Packit 437b5e
Packit 437b5e
    AS_IF([test "x$LUA" != 'x:'],
Packit 437b5e
      [ dnl At least check if this is a Lua interpreter.
Packit 437b5e
        AC_MSG_CHECKING([if $LUA is a Lua interpreter])
Packit 437b5e
        _AX_LUA_CHK_IS_INTRP([$LUA],
Packit 437b5e
          [AC_MSG_RESULT([yes])],
Packit 437b5e
          [ AC_MSG_RESULT([no])
Packit 437b5e
            AC_MSG_ERROR([not a Lua interpreter])
Packit 437b5e
          ])
Packit 437b5e
      ])
Packit 437b5e
  ],
Packit 437b5e
  [ dnl A version check is needed.
Packit 437b5e
    AS_IF([test "x$LUA" != 'x'],
Packit 437b5e
    [ dnl Check if this is a Lua interpreter.
Packit 437b5e
      AC_MSG_CHECKING([if $LUA is a Lua interpreter])
Packit 437b5e
      _AX_LUA_CHK_IS_INTRP([$LUA],
Packit 437b5e
        [AC_MSG_RESULT([yes])],
Packit 437b5e
        [ AC_MSG_RESULT([no])
Packit 437b5e
          AC_MSG_ERROR([not a Lua interpreter])
Packit 437b5e
        ])
Packit 437b5e
      dnl Check the version.
Packit 437b5e
      m4_if([$2], [],
Packit 437b5e
        [_ax_check_text="whether $LUA version >= $1"],
Packit 437b5e
        [_ax_check_text="whether $LUA version >= $1, < $2"])
Packit 437b5e
      AC_MSG_CHECKING([$_ax_check_text])
Packit 437b5e
      _AX_LUA_CHK_VER([$LUA], [$1], [$2],
Packit 437b5e
        [AC_MSG_RESULT([yes])],
Packit 437b5e
        [ AC_MSG_RESULT([no])
Packit 437b5e
          AC_MSG_ERROR([version is out of range for specified LUA])])
Packit 437b5e
      ax_display_LUA=$LUA
Packit 437b5e
    ],
Packit 437b5e
    [ dnl Try each interpreter until we find one that satisfies VERSION.
Packit 437b5e
      m4_if([$2], [],
Packit 437b5e
        [_ax_check_text="for a Lua interpreter with version >= $1"],
Packit 437b5e
        [_ax_check_text="for a Lua interpreter with version >= $1, < $2"])
Packit 437b5e
      AC_CACHE_CHECK([$_ax_check_text],
Packit 437b5e
        [ax_cv_pathless_LUA],
Packit 437b5e
        [ for ax_cv_pathless_LUA in _AX_LUA_INTERPRETER_LIST none; do
Packit 437b5e
            test "x$ax_cv_pathless_LUA" = 'xnone' && break
Packit 437b5e
            _AX_LUA_CHK_IS_INTRP([$ax_cv_pathless_LUA], [], [continue])
Packit 437b5e
            _AX_LUA_CHK_VER([$ax_cv_pathless_LUA], [$1], [$2], [break])
Packit 437b5e
          done
Packit 437b5e
        ])
Packit 437b5e
      dnl Set $LUA to the absolute path of $ax_cv_pathless_LUA.
Packit 437b5e
      AS_IF([test "x$ax_cv_pathless_LUA" = 'xnone'],
Packit 437b5e
        [LUA=':'],
Packit 437b5e
        [AC_PATH_PROG([LUA], [$ax_cv_pathless_LUA])])
Packit 437b5e
      ax_display_LUA=$ax_cv_pathless_LUA
Packit 437b5e
    ])
Packit 437b5e
  ])
Packit 437b5e
Packit 437b5e
  AS_IF([test "x$LUA" = 'x:'],
Packit 437b5e
  [ dnl Run any user-specified action, or abort.
Packit 437b5e
    m4_default([$4], [AC_MSG_ERROR([cannot find suitable Lua interpreter])])
Packit 437b5e
  ],
Packit 437b5e
  [ dnl Query Lua for its version number.
Packit 437b5e
    AC_CACHE_CHECK([for $ax_display_LUA version],
Packit 437b5e
      [ax_cv_lua_version],
Packit 437b5e
      [ dnl Get the interpreter version in X.Y format. This should work for
Packit 437b5e
        dnl interpreters version 5.0 and beyond.
Packit 437b5e
        ax_cv_lua_version=[`$LUA -e '
Packit 437b5e
          -- return a version number in X.Y format
Packit 437b5e
          local _, _, ver = string.find(_VERSION, "^Lua (%d+%.%d+)")
Packit 437b5e
          print(ver)'`]
Packit 437b5e
      ])
Packit 437b5e
    AS_IF([test "x$ax_cv_lua_version" = 'x'],
Packit 437b5e
      [AC_MSG_ERROR([invalid Lua version number])])
Packit 437b5e
    AC_SUBST([LUA_VERSION], [$ax_cv_lua_version])
Packit 437b5e
    AC_SUBST([LUA_SHORT_VERSION], [`echo "$LUA_VERSION" | $SED 's|\.||'`])
Packit 437b5e
Packit 437b5e
    dnl The following check is not supported:
Packit 437b5e
    dnl At times (like when building shared libraries) you may want to know
Packit 437b5e
    dnl which OS platform Lua thinks this is.
Packit 437b5e
    AC_CACHE_CHECK([for $ax_display_LUA platform],
Packit 437b5e
      [ax_cv_lua_platform],
Packit 437b5e
      [ax_cv_lua_platform=[`$LUA -e 'print("unknown")'`]])
Packit 437b5e
    AC_SUBST([LUA_PLATFORM], [$ax_cv_lua_platform])
Packit 437b5e
Packit 437b5e
    dnl Use the values of $prefix and $exec_prefix for the corresponding
Packit 437b5e
    dnl values of LUA_PREFIX and LUA_EXEC_PREFIX. These are made distinct
Packit 437b5e
    dnl variables so they can be overridden if need be. However, the general
Packit 437b5e
    dnl consensus is that you shouldn't need this ability.
Packit 437b5e
    AC_SUBST([LUA_PREFIX], ['${prefix}'])
Packit 437b5e
    AC_SUBST([LUA_EXEC_PREFIX], ['${exec_prefix}'])
Packit 437b5e
Packit 437b5e
    dnl Lua provides no way to query the script directory, and instead
Packit 437b5e
    dnl provides LUA_PATH. However, we should be able to make a safe educated
Packit 437b5e
    dnl guess. If the built-in search path contains a directory which is
Packit 437b5e
    dnl prefixed by $prefix, then we can store scripts there. The first
Packit 437b5e
    dnl matching path will be used.
Packit 437b5e
    AC_CACHE_CHECK([for $ax_display_LUA script directory],
Packit 437b5e
      [ax_cv_lua_luadir],
Packit 437b5e
      [ AS_IF([test "x$prefix" = 'xNONE'],
Packit 437b5e
          [ax_lua_prefix=$ac_default_prefix],
Packit 437b5e
          [ax_lua_prefix=$prefix])
Packit 437b5e
Packit 437b5e
        dnl Initialize to the default path.
Packit 437b5e
        ax_cv_lua_luadir="$LUA_PREFIX/share/lua/$LUA_VERSION"
Packit 437b5e
Packit 437b5e
        dnl Try to find a path with the prefix.
Packit 437b5e
        _AX_LUA_FND_PRFX_PTH([$LUA], [$ax_lua_prefix], [script])
Packit 437b5e
        AS_IF([test "x$ax_lua_prefixed_path" != 'x'],
Packit 437b5e
        [ dnl Fix the prefix.
Packit 437b5e
          _ax_strip_prefix=`echo "$ax_lua_prefix" | $SED 's|.|.|g'`
Packit 437b5e
          ax_cv_lua_luadir=`echo "$ax_lua_prefixed_path" | \
Packit 437b5e
            $SED "s|^$_ax_strip_prefix|$LUA_PREFIX|"`
Packit 437b5e
        ])
Packit 437b5e
      ])
Packit 437b5e
    AC_SUBST([luadir], [$ax_cv_lua_luadir])
Packit 437b5e
    AC_SUBST([pkgluadir], [\${luadir}/$PACKAGE])
Packit 437b5e
Packit 437b5e
    dnl Lua provides no way to query the module directory, and instead
Packit 437b5e
    dnl provides LUA_PATH. However, we should be able to make a safe educated
Packit 437b5e
    dnl guess. If the built-in search path contains a directory which is
Packit 437b5e
    dnl prefixed by $exec_prefix, then we can store modules there. The first
Packit 437b5e
    dnl matching path will be used.
Packit 437b5e
    AC_CACHE_CHECK([for $ax_display_LUA module directory],
Packit 437b5e
      [ax_cv_lua_luaexecdir],
Packit 437b5e
      [ AS_IF([test "x$exec_prefix" = 'xNONE'],
Packit 437b5e
          [ax_lua_exec_prefix=$ax_lua_prefix],
Packit 437b5e
          [ax_lua_exec_prefix=$exec_prefix])
Packit 437b5e
Packit 437b5e
        dnl Initialize to the default path.
Packit 437b5e
        ax_cv_lua_luaexecdir="$LUA_EXEC_PREFIX/lib/lua/$LUA_VERSION"
Packit 437b5e
Packit 437b5e
        dnl Try to find a path with the prefix.
Packit 437b5e
        _AX_LUA_FND_PRFX_PTH([$LUA],
Packit 437b5e
          [$ax_lua_exec_prefix], [module])
Packit 437b5e
        AS_IF([test "x$ax_lua_prefixed_path" != 'x'],
Packit 437b5e
        [ dnl Fix the prefix.
Packit 437b5e
          _ax_strip_prefix=`echo "$ax_lua_exec_prefix" | $SED 's|.|.|g'`
Packit 437b5e
          ax_cv_lua_luaexecdir=`echo "$ax_lua_prefixed_path" | \
Packit 437b5e
            $SED "s|^$_ax_strip_prefix|$LUA_EXEC_PREFIX|"`
Packit 437b5e
        ])
Packit 437b5e
      ])
Packit 437b5e
    AC_SUBST([luaexecdir], [$ax_cv_lua_luaexecdir])
Packit 437b5e
    AC_SUBST([pkgluaexecdir], [\${luaexecdir}/$PACKAGE])
Packit 437b5e
Packit 437b5e
    dnl Run any user specified action.
Packit 437b5e
    $3
Packit 437b5e
  ])
Packit 437b5e
])
Packit 437b5e
Packit 437b5e
dnl AX_WITH_LUA is now the same thing as AX_PROG_LUA.
Packit 437b5e
AC_DEFUN([AX_WITH_LUA],
Packit 437b5e
[
Packit 437b5e
  AC_MSG_WARN([[$0 is deprecated, please use AX_PROG_LUA instead]])
Packit 437b5e
  AX_PROG_LUA
Packit 437b5e
])
Packit 437b5e
Packit 437b5e
Packit 437b5e
dnl =========================================================================
Packit 437b5e
dnl _AX_LUA_CHK_IS_INTRP(PROG, [ACTION-IF-TRUE], [ACTION-IF-FALSE])
Packit 437b5e
dnl =========================================================================
Packit 437b5e
AC_DEFUN([_AX_LUA_CHK_IS_INTRP],
Packit 437b5e
[
Packit 437b5e
  dnl A minimal Lua factorial to prove this is an interpreter. This should work
Packit 437b5e
  dnl for Lua interpreters version 5.0 and beyond.
Packit 437b5e
  _ax_lua_factorial=[`$1 2>/dev/null -e '
Packit 437b5e
    -- a simple factorial
Packit 437b5e
    function fact (n)
Packit 437b5e
      if n == 0 then
Packit 437b5e
        return 1
Packit 437b5e
      else
Packit 437b5e
        return n * fact(n-1)
Packit 437b5e
      end
Packit 437b5e
    end
Packit 437b5e
    print("fact(5) is " .. fact(5))'`]
Packit 437b5e
  AS_IF([test "$_ax_lua_factorial" = 'fact(5) is 120'],
Packit 437b5e
    [$2], [$3])
Packit 437b5e
])
Packit 437b5e
Packit 437b5e
Packit 437b5e
dnl =========================================================================
Packit 437b5e
dnl _AX_LUA_CHK_VER(PROG, MINIMUM-VERSION, [TOO-BIG-VERSION],
Packit 437b5e
dnl                 [ACTION-IF-TRUE], [ACTION-IF-FALSE])
Packit 437b5e
dnl =========================================================================
Packit 437b5e
AC_DEFUN([_AX_LUA_CHK_VER],
Packit 437b5e
[
Packit 437b5e
  dnl Check that the Lua version is within the bounds. Only the major and minor
Packit 437b5e
  dnl version numbers are considered. This should work for Lua interpreters
Packit 437b5e
  dnl version 5.0 and beyond.
Packit 437b5e
  _ax_lua_good_version=[`$1 -e '
Packit 437b5e
    -- a script to compare versions
Packit 437b5e
    function verstr2num(verstr)
Packit 437b5e
      local _, _, majorver, minorver = string.find(verstr, "^(%d+)%.(%d+)")
Packit 437b5e
      if majorver and minorver then
Packit 437b5e
        return tonumber(majorver) * 100 + tonumber(minorver)
Packit 437b5e
      end
Packit 437b5e
    end
Packit 437b5e
    local minver = verstr2num("$2")
Packit 437b5e
    local _, _, trimver = string.find(_VERSION, "^Lua (.*)")
Packit 437b5e
    local ver = verstr2num(trimver)
Packit 437b5e
    local maxver = verstr2num("$3") or 1e9
Packit 437b5e
    if minver <= ver and ver < maxver then
Packit 437b5e
      print("yes")
Packit 437b5e
    else
Packit 437b5e
      print("no")
Packit 437b5e
    end'`]
Packit 437b5e
    AS_IF([test "x$_ax_lua_good_version" = "xyes"],
Packit 437b5e
      [$4], [$5])
Packit 437b5e
])
Packit 437b5e
Packit 437b5e
Packit 437b5e
dnl =========================================================================
Packit 437b5e
dnl _AX_LUA_FND_PRFX_PTH(PROG, PREFIX, SCRIPT-OR-MODULE-DIR)
Packit 437b5e
dnl =========================================================================
Packit 437b5e
AC_DEFUN([_AX_LUA_FND_PRFX_PTH],
Packit 437b5e
[
Packit 437b5e
  dnl Get the script or module directory by querying the Lua interpreter,
Packit 437b5e
  dnl filtering on the given prefix, and selecting the shallowest path. If no
Packit 437b5e
  dnl path is found matching the prefix, the result will be an empty string.
Packit 437b5e
  dnl The third argument determines the type of search, it can be 'script' or
Packit 437b5e
  dnl 'module'. Supplying 'script' will perform the search with package.path
Packit 437b5e
  dnl and LUA_PATH, and supplying 'module' will search with package.cpath and
Packit 437b5e
  dnl LUA_CPATH. This is done for compatibility with Lua 5.0.
Packit 437b5e
Packit 437b5e
  ax_lua_prefixed_path=[`$1 -e '
Packit 437b5e
    -- get the path based on search type
Packit 437b5e
    local searchtype = "$3"
Packit 437b5e
    local paths = ""
Packit 437b5e
    if searchtype == "script" then
Packit 437b5e
      paths = (package and package.path) or LUA_PATH
Packit 437b5e
    elseif searchtype == "module" then
Packit 437b5e
      paths = (package and package.cpath) or LUA_CPATH
Packit 437b5e
    end
Packit 437b5e
    -- search for the prefix
Packit 437b5e
    local prefix = "'$2'"
Packit 437b5e
    local minpath = ""
Packit 437b5e
    local mindepth = 1e9
Packit 437b5e
    string.gsub(paths, "(@<:@^;@:>@+)",
Packit 437b5e
      function (path)
Packit 437b5e
        path = string.gsub(path, "%?.*$", "")
Packit 437b5e
        path = string.gsub(path, "/@<:@^/@:>@*$", "")
Packit 437b5e
        if string.find(path, prefix) then
Packit 437b5e
          local depth = string.len(string.gsub(path, "@<:@^/@:>@", ""))
Packit 437b5e
          if depth < mindepth then
Packit 437b5e
            minpath = path
Packit 437b5e
            mindepth = depth
Packit 437b5e
          end
Packit 437b5e
        end
Packit 437b5e
      end)
Packit 437b5e
    print(minpath)'`]
Packit 437b5e
])
Packit 437b5e
Packit 437b5e
Packit 437b5e
dnl =========================================================================
Packit 437b5e
dnl AX_LUA_HEADERS([ACTION-IF-FOUND], [ACTION-IF-NOT-FOUND])
Packit 437b5e
dnl =========================================================================
Packit 437b5e
AC_DEFUN([AX_LUA_HEADERS],
Packit 437b5e
[
Packit 437b5e
  dnl Check for LUA_VERSION.
Packit 437b5e
  AC_MSG_CHECKING([if LUA_VERSION is defined])
Packit 437b5e
  AS_IF([test "x$LUA_VERSION" != 'x'],
Packit 437b5e
    [AC_MSG_RESULT([yes])],
Packit 437b5e
    [ AC_MSG_RESULT([no])
Packit 437b5e
      AC_MSG_ERROR([cannot check Lua headers without knowing LUA_VERSION])
Packit 437b5e
    ])
Packit 437b5e
Packit 437b5e
  dnl Make LUA_INCLUDE a precious variable.
Packit 437b5e
  AC_ARG_VAR([LUA_INCLUDE], [The Lua includes, e.g. -I/usr/include/lua5.1])
Packit 437b5e
Packit 437b5e
  dnl Some default directories to search.
Packit 437b5e
  LUA_SHORT_VERSION=`echo "$LUA_VERSION" | $SED 's|\.||'`
Packit 437b5e
  m4_define_default([_AX_LUA_INCLUDE_LIST],
Packit 437b5e
    [ /usr/include/lua$LUA_VERSION \
Packit 437b5e
      /usr/include/lua-$LUA_VERSION \
Packit 437b5e
      /usr/include/lua/$LUA_VERSION \
Packit 437b5e
      /usr/include/lua$LUA_SHORT_VERSION \
Packit 437b5e
      /usr/local/include/lua$LUA_VERSION \
Packit 437b5e
      /usr/local/include/lua-$LUA_VERSION \
Packit 437b5e
      /usr/local/include/lua/$LUA_VERSION \
Packit 437b5e
      /usr/local/include/lua$LUA_SHORT_VERSION \
Packit 437b5e
    ])
Packit 437b5e
Packit 437b5e
  dnl Try to find the headers.
Packit 437b5e
  _ax_lua_saved_cppflags=$CPPFLAGS
Packit 437b5e
  CPPFLAGS="$CPPFLAGS $LUA_INCLUDE"
Packit 437b5e
  AC_CHECK_HEADERS([lua.h lualib.h lauxlib.h luaconf.h])
Packit 437b5e
  CPPFLAGS=$_ax_lua_saved_cppflags
Packit 437b5e
Packit 437b5e
  dnl Try some other directories if LUA_INCLUDE was not set.
Packit 437b5e
  AS_IF([test "x$LUA_INCLUDE" = 'x' &&
Packit 437b5e
         test "x$ac_cv_header_lua_h" != 'xyes'],
Packit 437b5e
    [ dnl Try some common include paths.
Packit 437b5e
      for _ax_include_path in _AX_LUA_INCLUDE_LIST; do
Packit 437b5e
        test ! -d "$_ax_include_path" && continue
Packit 437b5e
Packit 437b5e
        AC_MSG_CHECKING([for Lua headers in])
Packit 437b5e
        AC_MSG_RESULT([$_ax_include_path])
Packit 437b5e
Packit 437b5e
        AS_UNSET([ac_cv_header_lua_h])
Packit 437b5e
        AS_UNSET([ac_cv_header_lualib_h])
Packit 437b5e
        AS_UNSET([ac_cv_header_lauxlib_h])
Packit 437b5e
        AS_UNSET([ac_cv_header_luaconf_h])
Packit 437b5e
Packit 437b5e
        _ax_lua_saved_cppflags=$CPPFLAGS
Packit 437b5e
        CPPFLAGS="$CPPFLAGS -I$_ax_include_path"
Packit 437b5e
        AC_CHECK_HEADERS([lua.h lualib.h lauxlib.h luaconf.h])
Packit 437b5e
        CPPFLAGS=$_ax_lua_saved_cppflags
Packit 437b5e
Packit 437b5e
        AS_IF([test "x$ac_cv_header_lua_h" = 'xyes'],
Packit 437b5e
          [ LUA_INCLUDE="-I$_ax_include_path"
Packit 437b5e
            break
Packit 437b5e
          ])
Packit 437b5e
      done
Packit 437b5e
    ])
Packit 437b5e
Packit 437b5e
  AS_IF([test "x$ac_cv_header_lua_h" = 'xyes'],
Packit 437b5e
    [ dnl Make a program to print LUA_VERSION defined in the header.
Packit 437b5e
      dnl TODO It would be really nice if we could do this without compiling a
Packit 437b5e
      dnl program, then it would work when cross compiling. But I'm not sure how
Packit 437b5e
      dnl to do this reliably. For now, assume versions match when cross compiling.
Packit 437b5e
Packit 437b5e
      AS_IF([test "x$cross_compiling" != 'xyes'],
Packit 437b5e
        [ AC_CACHE_CHECK([for Lua header version],
Packit 437b5e
            [ax_cv_lua_header_version],
Packit 437b5e
            [ _ax_lua_saved_cppflags=$CPPFLAGS
Packit 437b5e
              CPPFLAGS="$CPPFLAGS $LUA_INCLUDE"
Packit 437b5e
              AC_RUN_IFELSE(
Packit 437b5e
                [ AC_LANG_SOURCE([[
Packit 437b5e
#include <lua.h>
Packit 437b5e
#include <stdlib.h>
Packit 437b5e
#include <stdio.h>
Packit 437b5e
int main(int argc, char ** argv)
Packit 437b5e
{
Packit 437b5e
  if(argc > 1) printf("%s", LUA_VERSION);
Packit 437b5e
  exit(EXIT_SUCCESS);
Packit 437b5e
}
Packit 437b5e
]])
Packit 437b5e
                ],
Packit 437b5e
                [ ax_cv_lua_header_version=`./conftest$EXEEXT p | \
Packit 437b5e
                    $SED -n "s|^Lua \(@<:@0-9@:>@\{1,\}\.@<:@0-9@:>@\{1,\}\).\{0,\}|\1|p"`
Packit 437b5e
                ],
Packit 437b5e
                [ax_cv_lua_header_version='unknown'])
Packit 437b5e
              CPPFLAGS=$_ax_lua_saved_cppflags
Packit 437b5e
            ])
Packit 437b5e
Packit 437b5e
          dnl Compare this to the previously found LUA_VERSION.
Packit 437b5e
          AC_MSG_CHECKING([if Lua header version matches $LUA_VERSION])
Packit 437b5e
          AS_IF([test "x$ax_cv_lua_header_version" = "x$LUA_VERSION"],
Packit 437b5e
            [ AC_MSG_RESULT([yes])
Packit 437b5e
              ax_header_version_match='yes'
Packit 437b5e
            ],
Packit 437b5e
            [ AC_MSG_RESULT([no])
Packit 437b5e
              ax_header_version_match='no'
Packit 437b5e
            ])
Packit 437b5e
        ],
Packit 437b5e
        [ AC_MSG_WARN([cross compiling so assuming header version number matches])
Packit 437b5e
          ax_header_version_match='yes'
Packit 437b5e
        ])
Packit 437b5e
    ])
Packit 437b5e
Packit 437b5e
  dnl Was LUA_INCLUDE specified?
Packit 437b5e
  AS_IF([test "x$ax_header_version_match" != 'xyes' &&
Packit 437b5e
         test "x$LUA_INCLUDE" != 'x'],
Packit 437b5e
    [AC_MSG_ERROR([cannot find headers for specified LUA_INCLUDE])])
Packit 437b5e
Packit 437b5e
  dnl Test the final result and run user code.
Packit 437b5e
  AS_IF([test "x$ax_header_version_match" = 'xyes'], [$1],
Packit 437b5e
    [m4_default([$2], [AC_MSG_ERROR([cannot find Lua includes])])])
Packit 437b5e
])
Packit 437b5e
Packit 437b5e
dnl AX_LUA_HEADERS_VERSION no longer exists, use AX_LUA_HEADERS.
Packit 437b5e
AC_DEFUN([AX_LUA_HEADERS_VERSION],
Packit 437b5e
[
Packit 437b5e
  AC_MSG_WARN([[$0 is deprecated, please use AX_LUA_HEADERS instead]])
Packit 437b5e
])
Packit 437b5e
Packit 437b5e
Packit 437b5e
dnl =========================================================================
Packit 437b5e
dnl AX_LUA_LIBS([ACTION-IF-FOUND], [ACTION-IF-NOT-FOUND])
Packit 437b5e
dnl =========================================================================
Packit 437b5e
AC_DEFUN([AX_LUA_LIBS],
Packit 437b5e
[
Packit 437b5e
  dnl TODO Should this macro also check various -L flags?
Packit 437b5e
Packit 437b5e
  dnl Check for LUA_VERSION.
Packit 437b5e
  AC_MSG_CHECKING([if LUA_VERSION is defined])
Packit 437b5e
  AS_IF([test "x$LUA_VERSION" != 'x'],
Packit 437b5e
    [AC_MSG_RESULT([yes])],
Packit 437b5e
    [ AC_MSG_RESULT([no])
Packit 437b5e
      AC_MSG_ERROR([cannot check Lua libs without knowing LUA_VERSION])
Packit 437b5e
    ])
Packit 437b5e
Packit 437b5e
  dnl Make LUA_LIB a precious variable.
Packit 437b5e
  AC_ARG_VAR([LUA_LIB], [The Lua library, e.g. -llua5.1])
Packit 437b5e
Packit 437b5e
  AS_IF([test "x$LUA_LIB" != 'x'],
Packit 437b5e
  [ dnl Check that LUA_LIBS works.
Packit 437b5e
    _ax_lua_saved_libs=$LIBS
Packit 437b5e
    LIBS="$LIBS $LUA_LIB"
Packit 437b5e
    AC_SEARCH_LIBS([lua_load], [],
Packit 437b5e
      [_ax_found_lua_libs='yes'],
Packit 437b5e
      [_ax_found_lua_libs='no'])
Packit 437b5e
    LIBS=$_ax_lua_saved_libs
Packit 437b5e
Packit 437b5e
    dnl Check the result.
Packit 437b5e
    AS_IF([test "x$_ax_found_lua_libs" != 'xyes'],
Packit 437b5e
      [AC_MSG_ERROR([cannot find libs for specified LUA_LIB])])
Packit 437b5e
  ],
Packit 437b5e
  [ dnl First search for extra libs.
Packit 437b5e
    _ax_lua_extra_libs=''
Packit 437b5e
Packit 437b5e
    _ax_lua_saved_libs=$LIBS
Packit 437b5e
    LIBS="$LIBS $LUA_LIB"
Packit 437b5e
    AC_SEARCH_LIBS([exp], [m])
Packit 437b5e
    AC_SEARCH_LIBS([dlopen], [dl])
Packit 437b5e
    LIBS=$_ax_lua_saved_libs
Packit 437b5e
Packit 437b5e
    AS_IF([test "x$ac_cv_search_exp" != 'xno' &&
Packit 437b5e
           test "x$ac_cv_search_exp" != 'xnone required'],
Packit 437b5e
      [_ax_lua_extra_libs="$_ax_lua_extra_libs $ac_cv_search_exp"])
Packit 437b5e
Packit 437b5e
    AS_IF([test "x$ac_cv_search_dlopen" != 'xno' &&
Packit 437b5e
           test "x$ac_cv_search_dlopen" != 'xnone required'],
Packit 437b5e
      [_ax_lua_extra_libs="$_ax_lua_extra_libs $ac_cv_search_dlopen"])
Packit 437b5e
Packit 437b5e
    dnl Try to find the Lua libs.
Packit 437b5e
    _ax_lua_saved_libs=$LIBS
Packit 437b5e
    LIBS="$LIBS $LUA_LIB"
Packit 437b5e
    AC_SEARCH_LIBS([lua_load],
Packit 437b5e
      [ lua$LUA_VERSION \
Packit 437b5e
        lua$LUA_SHORT_VERSION \
Packit 437b5e
        lua-$LUA_VERSION \
Packit 437b5e
        lua-$LUA_SHORT_VERSION \
Packit 437b5e
        lua \
Packit 437b5e
      ],
Packit 437b5e
      [_ax_found_lua_libs='yes'],
Packit 437b5e
      [_ax_found_lua_libs='no'],
Packit 437b5e
      [$_ax_lua_extra_libs])
Packit 437b5e
    LIBS=$_ax_lua_saved_libs
Packit 437b5e
Packit 437b5e
    AS_IF([test "x$ac_cv_search_lua_load" != 'xno' &&
Packit 437b5e
           test "x$ac_cv_search_lua_load" != 'xnone required'],
Packit 437b5e
      [LUA_LIB="$ac_cv_search_lua_load $_ax_lua_extra_libs"])
Packit 437b5e
  ])
Packit 437b5e
Packit 437b5e
  dnl Test the result and run user code.
Packit 437b5e
  AS_IF([test "x$_ax_found_lua_libs" = 'xyes'], [$1],
Packit 437b5e
    [m4_default([$2], [AC_MSG_ERROR([cannot find Lua libs])])])
Packit 437b5e
])
Packit 437b5e
Packit 437b5e
Packit 437b5e
dnl =========================================================================
Packit 437b5e
dnl AX_LUA_READLINE([ACTION-IF-FOUND], [ACTION-IF-NOT-FOUND])
Packit 437b5e
dnl =========================================================================
Packit 437b5e
AC_DEFUN([AX_LUA_READLINE],
Packit 437b5e
[
Packit 437b5e
  AX_LIB_READLINE
Packit 437b5e
  AS_IF([test "x$ac_cv_header_readline_readline_h" != 'x' &&
Packit 437b5e
         test "x$ac_cv_header_readline_history_h" != 'x'],
Packit 437b5e
    [ LUA_LIBS_CFLAGS="-DLUA_USE_READLINE $LUA_LIBS_CFLAGS"
Packit 437b5e
      $1
Packit 437b5e
    ],
Packit 437b5e
    [$2])
Packit 437b5e
])