1#=============================================================================
2# Copyright 2007-2009 Kitware, Inc.
3# Copyright 2013 Rolf Eike Beer <eike@sf-mail.de>
4#
5# Distributed under the OSI-approved BSD License (the "License");
6# see accompanying file Copyright.txt for details.
7#
8# This software is distributed WITHOUT ANY WARRANTY; without even the
9# implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
10# See the License for more information.
11#=============================================================================
12# We use code from the CMake project to detect the Lua version.
13
14# Locate LuaJIT library
15# This module defines
16#  LUAJIT_FOUND, if false, do not try to link to Lua JIT
17#  LUAJIT_LIBRARIES
18#  LUAJIT_INCLUDE_DIR, where to find lua.h
19#
20# Additionally it defines the Lua API/ABI version:
21#  LUA_VERSION_STRING - the version of Lua found
22#  LUA_VERSION_MAJOR  - the major version of Lua
23#  LUA_VERSION_MINOR  - the minor version of Lua
24#  LUA_VERSION_PATCH  - the patch version of Lua
25
26FIND_PATH(LUAJIT_INCLUDE_DIR NAMES lua.h PATH_SUFFIXES luajit-2.0 luajit-2.1)
27FIND_LIBRARY(LUAJIT_LIBRARIES NAMES luajit-5.1 luajit)
28
29if (LUAJIT_INCLUDE_DIR AND EXISTS "${LUAJIT_INCLUDE_DIR}/lua.h")
30    # At least 5.[012] have different ways to express the version
31    # so all of them need to be tested. Lua 5.2 defines LUA_VERSION
32    # and LUA_RELEASE as joined by the C preprocessor, so avoid those.
33    file(STRINGS "${LUAJIT_INCLUDE_DIR}/lua.h" lua_version_strings
34         REGEX "^#define[ \t]+LUA_(RELEASE[ \t]+\"Lua [0-9]|VERSION([ \t]+\"Lua [0-9]|_[MR])).*")
35
36    string(REGEX REPLACE ".*;#define[ \t]+LUA_VERSION_MAJOR[ \t]+\"([0-9])\"[ \t]*;.*" "\\1" LUA_VERSION_MAJOR ";${lua_version_strings};")
37    if (LUA_VERSION_MAJOR MATCHES "^[0-9]+$")
38        string(REGEX REPLACE ".*;#define[ \t]+LUA_VERSION_MINOR[ \t]+\"([0-9])\"[ \t]*;.*" "\\1" LUA_VERSION_MINOR ";${lua_version_strings};")
39        string(REGEX REPLACE ".*;#define[ \t]+LUA_VERSION_RELEASE[ \t]+\"([0-9])\"[ \t]*;.*" "\\1" LUA_VERSION_PATCH ";${lua_version_strings};")
40        set(LUA_VERSION_STRING "${LUA_VERSION_MAJOR}.${LUA_VERSION_MINOR}.${LUA_VERSION_PATCH}")
41    else ()
42        string(REGEX REPLACE ".*;#define[ \t]+LUA_RELEASE[ \t]+\"Lua ([0-9.]+)\"[ \t]*;.*" "\\1" LUA_VERSION_STRING ";${lua_version_strings};")
43        if (NOT LUA_VERSION_STRING MATCHES "^[0-9.]+$")
44            string(REGEX REPLACE ".*;#define[ \t]+LUA_VERSION[ \t]+\"Lua ([0-9.]+)\"[ \t]*;.*" "\\1" LUA_VERSION_STRING ";${lua_version_strings};")
45        endif ()
46        string(REGEX REPLACE "^([0-9]+)\\.[0-9.]*$" "\\1" LUA_VERSION_MAJOR "${LUA_VERSION_STRING}")
47        string(REGEX REPLACE "^[0-9]+\\.([0-9]+)[0-9.]*$" "\\1" LUA_VERSION_MINOR "${LUA_VERSION_STRING}")
48        string(REGEX REPLACE "^[0-9]+\\.[0-9]+\\.([0-9]).*" "\\1" LUA_VERSION_PATCH "${LUA_VERSION_STRING}")
49    endif ()
50
51    unset(lua_version_strings)
52endif()
53
54INCLUDE(FindPackageHandleStandardArgs)
55FIND_PACKAGE_HANDLE_STANDARD_ARGS(LUAJIT DEFAULT_MSG LUAJIT_LIBRARIES LUAJIT_INCLUDE_DIR)
56