1# Locate Lua library
2# This module defines
3#  LUA_EXECUTABLE, if found
4#  LUA_FOUND, if false, do not try to link to Lua
5#  LUA_LIBRARIES
6#  LUA_INCLUDE_DIR, where to find lua.h
7#  LUA_VERSION_STRING, the version of Lua found (since CMake 2.8.8)
8#
9# Note that the expected include convention is
10#  #include "lua.h"
11# and not
12#  #include <lua/lua.h>
13# This is because, the lua location is not standardized and may exist
14# in locations other than lua/
15
16#=============================================================================
17# Copyright 2007-2009 Kitware, Inc.
18# Modified to support Lua 5.2 by LuaDist 2012
19#
20# Distributed under the OSI-approved BSD License (the "License");
21# see accompanying file Copyright.txt for details.
22#
23# This software is distributed WITHOUT ANY WARRANTY; without even the
24# implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
25# See the License for more information.
26#=============================================================================
27# (To distribute this file outside of CMake, substitute the full
28#  License text for the above reference.)
29#
30# This module will try to find the newest Lua version down to 5.2
31
32# Always search for non-versioned lua first (recommended)
33SET(_POSSIBLE_LUA_INCLUDE include include/lua)
34#SET(_POSSIBLE_LUA_EXECUTABLE lua)
35#SET(_POSSIBLE_LUA_COMPILER luac)
36#SET(_POSSIBLE_LUA_LIBRARY lua)
37
38# Determine possible naming suffixes (there is no standard for this)
39SET(_POSSIBLE_SUFFIXES "52" "5.2" "-5.2" "53" "5.3" "-5.3" "")
40
41# Set up possible search names and locations
42FOREACH(_SUFFIX ${_POSSIBLE_SUFFIXES})
43  LIST(APPEND _POSSIBLE_LUA_INCLUDE "include/lua${_SUFFIX}")
44  LIST(APPEND _POSSIBLE_LUA_EXECUTABLE "lua${_SUFFIX}")
45  LIST(APPEND _POSSIBLE_LUA_COMPILER "luac${_SUFFIX}")
46  LIST(APPEND _POSSIBLE_LUA_LIBRARY "lua${_SUFFIX}")
47ENDFOREACH(_SUFFIX)
48
49# Find the lua executable
50FIND_PROGRAM(LUA_EXECUTABLE
51  NAMES ${_POSSIBLE_LUA_EXECUTABLE}
52)
53
54# Find the lua executable
55FIND_PROGRAM(LUA_COMPILER
56  NAMES luac5.3 ${_POSSIBLE_LUA_COMPILER}
57)
58
59# Find the lua header
60FIND_PATH(LUA_INCLUDE_DIR lua.h
61  HINTS
62  $ENV{LUA_DIR}
63  PATH_SUFFIXES ${_POSSIBLE_LUA_INCLUDE}
64  PATHS
65  ~/Library/Frameworks
66  /Library/Frameworks
67  /usr/local
68  /usr
69  /sw # Fink
70  /opt/local # DarwinPorts
71  /opt/csw # Blastwave
72  /opt
73)
74
75# Find the lua library
76FIND_LIBRARY(LUA_LIBRARY
77  NAMES ${_POSSIBLE_LUA_LIBRARY}
78  HINTS
79  $ENV{LUA_DIR}
80  PATH_SUFFIXES lib64 lib
81  PATHS
82  ~/Library/Frameworks
83  /Library/Frameworks
84  /usr/local
85  /usr
86  /sw
87  /opt/local
88  /opt/csw
89  /opt
90)
91
92IF(LUA_LIBRARY)
93  # include the math library for Unix
94  IF(UNIX AND NOT APPLE)
95    FIND_LIBRARY(LUA_MATH_LIBRARY m)
96    SET( LUA_LIBRARIES "${LUA_LIBRARY};${LUA_MATH_LIBRARY}" CACHE STRING "Lua Libraries")
97  # For Windows and Mac, don't need to explicitly include the math library
98  ELSE(UNIX AND NOT APPLE)
99    SET( LUA_LIBRARIES "${LUA_LIBRARY}" CACHE STRING "Lua Libraries")
100  ENDIF(UNIX AND NOT APPLE)
101ENDIF(LUA_LIBRARY)
102
103# Determine Lua version
104IF(LUA_INCLUDE_DIR AND EXISTS "${LUA_INCLUDE_DIR}/lua.h")
105  FILE(STRINGS "${LUA_INCLUDE_DIR}/lua.h" lua_version_str REGEX "^#define[ \t]+LUA_RELEASE[ \t]+\"Lua .+\"")
106
107  STRING(REGEX REPLACE "^#define[ \t]+LUA_RELEASE[ \t]+\"Lua ([^\"]+)\".*" "\\1" LUA_VERSION_STRING "${lua_version_str}")
108  UNSET(lua_version_str)
109ENDIF()
110
111INCLUDE(FindPackageHandleStandardArgs)
112# handle the QUIETLY and REQUIRED arguments and set LUA_FOUND to TRUE if
113# all listed variables are TRUE
114FIND_PACKAGE_HANDLE_STANDARD_ARGS(Lua
115                                  REQUIRED_VARS LUA_LIBRARIES LUA_INCLUDE_DIR
116                                  VERSION_VAR LUA_VERSION_STRING)
117
118MARK_AS_ADVANCED(LUA_INCLUDE_DIR LUA_LIBRARIES LUA_LIBRARY LUA_MATH_LIBRARY LUA_EXECUTABLE LUA_COMPILER)
119
120