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:
10 .. versionadded:: 3.18
16 if false, do not try to link to Lua
21 ``LUA_VERSION_STRING``
22 the version of Lua found
24 the major version of Lua
26 the minor version of Lua
28 the patch version of Lua
30 Note that the expected include convention is
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)
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}
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}")
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}")
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}")
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};")
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}")
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)
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})
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}
144 # Did not found header -> Fail
145 if (NOT LUA_INCLUDE_DIR)
148 _lua_get_header_version()
149 # Found accepted version -> Ok
150 if (LUA_VERSION_STRING)
152 message(STATUS "Found suitable version ${LUA_VERSION_STRING} in ${LUA_INCLUDE_DIR}/lua.h")
156 # Found wrong version -> Ignore this path and retry
158 message(STATUS "Ignoring unsuitable version in ${LUA_INCLUDE_DIR}")
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)
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}
181 find_library(LUA_LIBRARY
182 NAMES ${_lua_library_names} lua
187 PATH_SUFFIXES lib ${_lua_library_names}
189 unset(_lua_library_names)
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})
204 # For Windows and Mac, don't need to explicitly include the math library
206 set(LUA_LIBRARIES "${LUA_LIBRARY}")
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)
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()
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 )
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
245 set(HAVE_LUA_UNICODE True)
249 SET( LUA_INCLUDE_DIRS )