regen pidl all: rm epan/dissectors/pidl/*-stamp; pushd epan/dissectors/pidl/ && make...
[wireshark-sm.git] / cmake / modules / FindLua.cmake
blob7bacfdd4477ea0336d34017c441093539f1ff30e
1 # Distributed under the OSI-approved BSD 3-Clause License.  See accompanying
2 # file Copyright.txt or https://cmake.org/licensing for details.
4 #[=======================================================================[.rst:
5 FindLua
6 -------
8 Locate Lua library.
10 .. versionadded:: 3.18
11   Support for Lua 5.4.
13 This module defines:
15 ``LUA_FOUND``
16   if false, do not try to link to Lua
17 ``LUA_LIBRARIES``
18   both lua and lualib
19 ``LUA_INCLUDE_DIR``
20   where to find lua.h
21 ``LUA_VERSION_STRING``
22   the version of Lua found
23 ``LUA_VERSION_MAJOR``
24   the major version of Lua
25 ``LUA_VERSION_MINOR``
26   the minor version of Lua
27 ``LUA_VERSION_PATCH``
28   the patch version of Lua
30 Note that the expected include convention is
34   #include "lua.h"
36 and not
40   #include <lua/lua.h>
42 This is because, the lua location is not standardized and may exist in
43 locations other than lua/
44 #]=======================================================================]
46 cmake_policy(PUSH)  # Policies apply to functions at definition-time
47 cmake_policy(SET CMP0012 NEW)  # For while(TRUE)
49 INCLUDE(FindWSWinLibs)
50 FindWSWinLibs("lua-5*" "LUA_HINTS")
52 unset(_lua_include_subdirs)
53 unset(_lua_library_names)
54 unset(_lua_append_versions)
56 # this is a function only to have all the variables inside go away automatically
57 function(_lua_get_versions)
58   set(LUA_VERSIONS5 ${LUA_FIND_VERSIONS})
59   list(FILTER LUA_VERSIONS5 INCLUDE REGEX "5\.[43]")
60   set(_lua_append_versions ${LUA_VERSIONS5})
61   message(STATUS "Considering the following Lua versions: ${_lua_append_versions}")
63   set(_lua_append_versions "${_lua_append_versions}" PARENT_SCOPE)
64 endfunction()
66 function(_lua_set_version_vars)
67   set(_lua_include_subdirs_raw "lua")
69   foreach (ver IN LISTS _lua_append_versions)
70     string(REGEX MATCH "^([0-9]+)\\.([0-9]+)$" _ver "${ver}")
71     list(APPEND _lua_include_subdirs_raw
72         lua${CMAKE_MATCH_1}${CMAKE_MATCH_2}
73         lua${CMAKE_MATCH_1}.${CMAKE_MATCH_2}
74         lua-${CMAKE_MATCH_1}.${CMAKE_MATCH_2}
75         )
76   endforeach ()
78   # Prepend "include/" to each path directly after the path
79   set(_lua_include_subdirs "include")
80   foreach (dir IN LISTS _lua_include_subdirs_raw)
81     list(APPEND _lua_include_subdirs "${dir}" "include/${dir}")
82   endforeach ()
84   set(_lua_include_subdirs "${_lua_include_subdirs}" PARENT_SCOPE)
85 endfunction(_lua_set_version_vars)
87 function(_lua_get_header_version)
88   unset(LUA_VERSION_STRING PARENT_SCOPE)
89   set(_hdr_file "${LUA_INCLUDE_DIR}/lua.h")
91   if (NOT EXISTS "${_hdr_file}")
92     return()
93   endif ()
95   # At least 5.[012] have different ways to express the version
96   # so all of them need to be tested. Lua 5.2 defines LUA_VERSION
97   # and LUA_RELEASE as joined by the C preprocessor, so avoid those.
98   file(STRINGS "${_hdr_file}" lua_version_strings
99        REGEX "^#define[ \t]+LUA_(RELEASE[ \t]+\"Lua [0-9]|VERSION([ \t]+\"Lua [0-9]|_[MR])).*")
101   string(REGEX REPLACE ".*;#define[ \t]+LUA_VERSION_MAJOR[ \t]+\"([0-9])\"[ \t]*;.*" "\\1" LUA_VERSION_MAJOR ";${lua_version_strings};")
102   if (LUA_VERSION_MAJOR MATCHES "^[0-9]+$")
103     string(REGEX REPLACE ".*;#define[ \t]+LUA_VERSION_MINOR[ \t]+\"([0-9])\"[ \t]*;.*" "\\1" LUA_VERSION_MINOR ";${lua_version_strings};")
104     string(REGEX REPLACE ".*;#define[ \t]+LUA_VERSION_RELEASE[ \t]+\"([0-9])\"[ \t]*;.*" "\\1" LUA_VERSION_PATCH ";${lua_version_strings};")
105     set(LUA_VERSION_STRING "${LUA_VERSION_MAJOR}.${LUA_VERSION_MINOR}.${LUA_VERSION_PATCH}")
106   else ()
107     string(REGEX REPLACE ".*;#define[ \t]+LUA_RELEASE[ \t]+\"Lua ([0-9.]+)\"[ \t]*;.*" "\\1" LUA_VERSION_STRING ";${lua_version_strings};")
108     if (NOT LUA_VERSION_STRING MATCHES "^[0-9.]+$")
109       string(REGEX REPLACE ".*;#define[ \t]+LUA_VERSION[ \t]+\"Lua ([0-9.]+)\"[ \t]*;.*" "\\1" LUA_VERSION_STRING ";${lua_version_strings};")
110     endif ()
111     string(REGEX REPLACE "^([0-9]+)\\.[0-9.]*$" "\\1" LUA_VERSION_MAJOR "${LUA_VERSION_STRING}")
112     string(REGEX REPLACE "^[0-9]+\\.([0-9]+)[0-9.]*$" "\\1" LUA_VERSION_MINOR "${LUA_VERSION_STRING}")
113     string(REGEX REPLACE "^[0-9]+\\.[0-9]+\\.([0-9]).*" "\\1" LUA_VERSION_PATCH "${LUA_VERSION_STRING}")
114   endif ()
115   foreach (ver IN LISTS _lua_append_versions)
116     if (ver STREQUAL "${LUA_VERSION_MAJOR}.${LUA_VERSION_MINOR}")
117       set(LUA_VERSION_MAJOR ${LUA_VERSION_MAJOR} PARENT_SCOPE)
118       set(LUA_VERSION_MINOR ${LUA_VERSION_MINOR} PARENT_SCOPE)
119       set(LUA_VERSION_PATCH ${LUA_VERSION_PATCH} PARENT_SCOPE)
120       set(LUA_VERSION_STRING ${LUA_VERSION_STRING} PARENT_SCOPE)
121       return()
122     endif ()
123   endforeach ()
124 endfunction(_lua_get_header_version)
126 function(_lua_find_header)
127   _lua_set_version_vars()
129   # Initialize as local variable
130   set(CMAKE_IGNORE_PATH ${CMAKE_IGNORE_PATH})
131   while (TRUE)
132     # Find the next header to test. Check each possible subdir in order
133     # This prefers e.g. higher versions as they are earlier in the list
134     # It is also consistent with previous versions of FindLua
135     foreach (subdir IN LISTS _lua_include_subdirs)
136       find_path(LUA_INCLUDE_DIR lua.h
137         HINTS ${LUA_HINTS} ENV LUA_DIR
138         PATH_SUFFIXES ${subdir}
139         )
140       if (LUA_INCLUDE_DIR)
141         break()
142       endif()
143     endforeach()
144     # Did not found header -> Fail
145     if (NOT LUA_INCLUDE_DIR)
146       return()
147     endif()
148     _lua_get_header_version()
149     # Found accepted version -> Ok
150     if (LUA_VERSION_STRING)
151       if (LUA_Debug)
152         message(STATUS "Found suitable version ${LUA_VERSION_STRING} in ${LUA_INCLUDE_DIR}/lua.h")
153       endif()
154       return()
155     endif()
156     # Found wrong version -> Ignore this path and retry
157     if (LUA_Debug)
158       message(STATUS "Ignoring unsuitable version in ${LUA_INCLUDE_DIR}")
159     endif()
160     list(APPEND CMAKE_IGNORE_PATH "${LUA_INCLUDE_DIR}")
161     unset(LUA_INCLUDE_DIR CACHE)
162     unset(LUA_INCLUDE_DIR)
163     unset(LUA_INCLUDE_DIR PARENT_SCOPE)
164   endwhile ()
165 endfunction()
167 _lua_get_versions()
168 _lua_find_header()
169 _lua_get_header_version()
170 unset(_lua_append_versions)
172 if (LUA_VERSION_STRING)
173   set(_lua_library_names
174     lua${LUA_VERSION_MAJOR}${LUA_VERSION_MINOR}
175     lua${LUA_VERSION_MAJOR}.${LUA_VERSION_MINOR}
176     lua-${LUA_VERSION_MAJOR}.${LUA_VERSION_MINOR}
177     lua.${LUA_VERSION_MAJOR}.${LUA_VERSION_MINOR}
178     )
179 endif ()
181 find_library(LUA_LIBRARY
182   NAMES ${_lua_library_names} lua
183   NAMES_PER_DIR
184   HINTS
185     ${LUA_HINTS}
186     ENV LUA_DIR
187   PATH_SUFFIXES lib ${_lua_library_names}
189 unset(_lua_library_names)
191 if (LUA_LIBRARY)
192   # include the math library for Unix
193   if (UNIX AND NOT APPLE AND NOT BEOS)
194     find_library(LUA_MATH_LIBRARY m)
195     mark_as_advanced(LUA_MATH_LIBRARY)
196     set(LUA_LIBRARIES "${LUA_LIBRARY};${LUA_MATH_LIBRARY}")
198     # include dl library for statically-linked Lua library
199     get_filename_component(LUA_LIB_EXT ${LUA_LIBRARY} EXT)
200     if(LUA_LIB_EXT STREQUAL CMAKE_STATIC_LIBRARY_SUFFIX)
201       list(APPEND LUA_LIBRARIES ${CMAKE_DL_LIBS})
202     endif()
204   # For Windows and Mac, don't need to explicitly include the math library
205   else ()
206     set(LUA_LIBRARIES "${LUA_LIBRARY}")
207   endif ()
208 endif ()
210 include(FindPackageHandleStandardArgs)
211 # handle the QUIETLY and REQUIRED arguments and set LUA_FOUND to TRUE if
212 # all listed variables are TRUE
213 FIND_PACKAGE_HANDLE_STANDARD_ARGS(Lua
214                                   REQUIRED_VARS LUA_LIBRARIES LUA_INCLUDE_DIR
215                                   VERSION_VAR LUA_VERSION_STRING)
217 mark_as_advanced(LUA_INCLUDE_DIR LUA_LIBRARY)
219 cmake_policy(POP)
221 IF(Lua_FOUND)
222   SET( LUA_INCLUDE_DIRS ${LUA_INCLUDE_DIR} )
224   unset(HAVE_LUA_INTEGER_SIZE CACHE)
225   cmake_push_check_state()
226   include(CheckTypeSize)
227   set(CMAKE_REQUIRED_INCLUDES ${LUA_INCLUDE_DIR})
228   set(CMAKE_EXTRA_INCLUDE_FILES "luaconf.h")
229   check_type_size(LUA_INTEGER LUA_INTEGER_SIZE)
230   cmake_pop_check_state()
232   if (WIN32)
233     set ( LUA_DLL_DIR "${LUA_HINTS}" CACHE PATH "Path to Lua DLL")
234     file( GLOB _lua_dll RELATIVE "${LUA_DLL_DIR}" "${LUA_DLL_DIR}/lua*.dll")
235     set ( LUA_DLL ${_lua_dll} CACHE FILEPATH "Lua DLL file name")
236     mark_as_advanced( LUA_DLL_DIR LUA_DLL )
237   endif()
238   if(LUA_DLL_DIR MATCHES ".*/lua-.*-unicode-.*")
239     # Do we have Lua with Unicode for Windows patches?
240     # https://github.com/Lekensteyn/lua-unicode
241     # XXX Would be better if it was possible to
242     # detect a Lua-unicode build from C and Lua code
243     # but upstream rejected patches for that so we do
244     # it here.
245     set(HAVE_LUA_UNICODE True)
246   endif()
247 ELSE(Lua_FOUND)
248   SET( LUA_LIBRARIES )
249   SET( LUA_INCLUDE_DIRS )
250   SET( LUA_DLL_DIR )
251   SET( LUA_DLL )
252 ENDIF(Lua_FOUND)