aboutsummaryrefslogtreecommitdiffstats
path: root/cmake/modules/FindLUA.cmake
blob: 26d4db653c937697e29f44797f8eab069eb6f16f (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
#
# Locate Lua library
# This module defines
#  LUA_FOUND        - If false, do not try to link to Lua
#  LUA_LIBRARIES
#  LUA_INCLUDE_DIRS - Where to find lua.h
#  LUA_DLL_DIR      - (Windows) Path to the Lua DLL.
#  LUA_DLL          - (Windows) Name of the Lua DLL.
#
# Note that the expected include convention is
#  #include "lua.h"
# and not
#  #include <lua/lua.h>
# This is because, the lua location is not standardized and may exist
# in locations other than lua/

INCLUDE(FindWSWinLibs)
FindWSWinLibs("lua-5*" "LUA_HINTS")

if(NOT WIN32)
  find_package(PkgConfig)
  pkg_search_module(LUA lua5.2 lua-5.2 lua52 lua5.1 lua-5.1 lua51)
  if(NOT LUA_FOUND)
      pkg_search_module(LUA "lua<=5.2.99")
  endif()
endif()

FIND_PATH(LUA_INCLUDE_DIR lua.h
  HINTS
    "${LUA_INCLUDEDIR}"
    "$ENV{LUA_DIR}"
  ${LUA_HINTS}
  PATH_SUFFIXES include/lua52 include/lua5.2 include/lua-5.2 include/lua51 include/lua5.1 include/lua-5.1 include/lua include
  PATHS
  ~/Library/Frameworks
  /Library/Frameworks
  /usr/local
  /usr
  /sw # Fink
  /opt/local # DarwinPorts
  /opt/csw # Blastwave
  /opt
)

if(LUA_INCLUDE_DIR AND EXISTS "${LUA_INCLUDE_DIR}/lua.h")
  file(STRINGS "${LUA_INCLUDE_DIR}/lua.h" LUA_VERSION_NUM REGEX "LUA_VERSION_NUM")
  if (LUA_VERSION_NUM)
    string(REGEX REPLACE "^#define[ \t]+LUA_VERSION_NUM[ \t]+([0-9]+)" "\\1"
      LUA_VERSION_NUM "${LUA_VERSION_NUM}")
  endif()
endif()
string( REGEX REPLACE ".*[/\\]lua(.+)$" "\\1" LUA_INC_SUFFIX "${LUA_INCLUDE_DIR}" )
if ( LUA_INCLUDE_DIR STREQUAL LUA_INC_SUFFIX )
  set( LUA_INC_SUFFIX "")
endif()

FIND_LIBRARY(LUA_LIBRARY
  NAMES lua${LUA_INC_SUFFIX} lua52 lua5.2 lua51 lua5.1 lua
  HINTS
    "${LUA_LIBDIR}"
    "$ENV{LUA_DIR}"
    ${LUA_HINTS}
  PATH_SUFFIXES lib64 lib
  PATHS
    ~/Library/Frameworks
    /Library/Frameworks
    /usr/local
    /usr
    /sw
    /opt/local
    /opt/csw
    /opt
)

# Lua 5.3 is not supported, only 5.1/5.2 are (due to bitops problem)
if(LUA_VERSION_NUM GREATER 502)
  set(LUA_VERSION_NUM)
endif()

INCLUDE(FindPackageHandleStandardArgs)
# handle the QUIETLY and REQUIRED arguments and set LUA_FOUND to TRUE if
# all listed variables are TRUE
find_package_handle_standard_args(LUA
  REQUIRED_VARS LUA_LIBRARY LUA_INCLUDE_DIR LUA_VERSION_NUM
  VERSION_VAR   LUA_VERSION_NUM)

IF(LUA_LIBRARY)
  SET( LUA_LIBRARIES "${LUA_LIBRARY}")
  SET( LUA_INCLUDE_DIRS ${LUA_INCLUDE_DIR} )
  if (WIN32)
    set ( LUA_DLL_DIR "${LUA_HINTS}"
      CACHE PATH "Path to Lua DLL"
    )
    file( GLOB _lua_dll RELATIVE "${LUA_DLL_DIR}"
      "${LUA_DLL_DIR}/lua*.dll"
    )
    set ( LUA_DLL ${_lua_dll}
      # We're storing filenames only. Should we use STRING instead?
      CACHE FILEPATH "Lua DLL file name"
    )
    mark_as_advanced( LUA_DLL_DIR LUA_DLL )
  endif()
ELSE(LUA_LIBRARY)
  SET( LUA_LIBRARIES )
  SET( LUA_INCLUDE_DIRS )
  SET( LUA_DLL_DIR )
  SET( LUA_DLL )
ENDIF(LUA_LIBRARY)

MARK_AS_ADVANCED(LUA_INCLUDE_DIRS LUA_LIBRARIES)