5 # Find lua include dirs and libraries.
10 # ``lua`` and ``luac``
15 # This module defines the following :ref:`Imported Targets <Imported Targets>`:
22 # This module defines the following variables:
32 # ``LUA_VERSION_MAJOR``
34 # ``LUA_VERSION_MINOR``
36 # ``LUA_VERSION_PATCH``
49 # Search results are saved persistently in CMake cache entries:
55 # ``LUA_MATH_LIBRARY``
61 include(${CMAKE_ROOT}/Modules/FindPackageHandleStandardArgs.cmake)
62 list(APPEND LUA_REQUIRED_VARS LUA_LIBRARIES LUA_INCLUDE_DIR)
64 foreach(VERSION ${Lua_FIND_VERSION} 5.4 5.3 5.2 5.1 5.0)
65 string(REGEX MATCH "^([0-9]+)\\.([0-9]+)" VERSION "${VERSION}")
66 find_path(LUA_INCLUDE_DIR lua.h PATH_SUFFIXES
67 include/lua${CMAKE_MATCH_1}${CMAKE_MATCH_2}
68 include/lua${CMAKE_MATCH_1}.${CMAKE_MATCH_2}
69 include/lua-${CMAKE_MATCH_1}.${CMAKE_MATCH_2}
70 include HINTS ENV LUA_DIR
76 mark_as_advanced(LUA_INCLUDE_DIR)
78 if(EXISTS "${LUA_INCLUDE_DIR}/lua.h")
79 file(STRINGS ${LUA_INCLUDE_DIR}/lua.h VERSION REGEX "^#define[ \t]+LUA_(RELEASE[ \t]+\"Lua [0-9]|VERSION([ \t]+\"Lua [0-9]|_[MR])).*")
80 string(REGEX REPLACE ".*;#define[ \t]+LUA_VERSION_MAJOR[^0-9]+([0-9]+)[^;]*;.*" "\\1" LUA_VERSION_MAJOR ";${VERSION};")
81 if(LUA_VERSION_MAJOR MATCHES "^[0-9]+$")
82 string(REGEX REPLACE ".*;#define[ \t]+LUA_VERSION_MINOR[^0-9]+([0-9]+)[^;]*;.*" "\\1" LUA_VERSION_MINOR ";${VERSION};")
83 string(REGEX REPLACE ".*;#define[ \t]+LUA_VERSION_RELEASE[^0-9(]+([0-9]+)[^;]*;.*" "\\1" LUA_VERSION_PATCH ";${VERSION};")
84 set(LUA_VERSION "${LUA_VERSION_MAJOR}.${LUA_VERSION_MINOR}.${LUA_VERSION_PATCH}")
86 string(REGEX REPLACE ".*;#define[ \t]+LUA_RELEASE[ \t]+\"Lua ([0-9.]+)\"[ \t]*;.*" "\\1" LUA_VERSION ";${VERSION};")
87 if(NOT LUA_VERSION MATCHES "^[0-9.]+$")
88 string(REGEX REPLACE ".*;#define[ \t]+LUA_VERSION[ \t]+\"Lua ([0-9.]+)\"[ \t]*;.*" "\\1" LUA_VERSION ";${VERSION};")
90 string(REGEX MATCHALL "[0-9]+" VERSION "${LUA_VERSION}.0.0.0")
91 list(GET VERSION 0 LUA_VERSION_MAJOR)
92 list(GET VERSION 1 LUA_VERSION_MINOR)
93 list(GET VERSION 2 LUA_VERSION_PATCH)
97 if(NOT DEFINED ENV{LUA_DIR} AND EXISTS "${LUA_INCLUDE_DIR}")
98 get_filename_component(LUA_DIR "${LUA_INCLUDE_DIR}" DIRECTORY)
99 get_filename_component(modules "${LUA_INCLUDE_DIR}" NAME)
100 if(NOT modules STREQUAL "include")
101 get_filename_component(LUA_DIR "${LUA_DIR}" DIRECTORY)
103 elseif(DEFINED ENV{LUA_DIR})
104 file(TO_CMAKE_PATH "$ENV{LUA_DIR}" LUA_DIR)
107 string(REGEX MATCHALL "(lua|luac)" modules "${Lua_FIND_COMPONENTS}")
109 list(REMOVE_DUPLICATES modules)
111 foreach(module ${modules})
112 string(TOUPPER ${module} MODULE)
113 find_program(${MODULE}_EXECUTABLE NAMES
114 ${module}${LUA_VERSION_MAJOR}${LUA_VERSION_MINOR}
115 ${module}${LUA_VERSION_MAJOR}.${LUA_VERSION_MINOR}
116 ${module}-${LUA_VERSION_MAJOR}.${LUA_VERSION_MINOR}
117 ${module} HINTS ${LUA_DIR} PATH_SUFFIXES bin
119 mark_as_advanced(${MODULE}_EXECUTABLE)
120 if(Lua_FIND_REQUIRED_${module})
121 list(APPEND LUA_REQUIRED_VARS ${MODULE}_EXECUTABLE)
123 if(EXISTS ${${MODULE}_EXECUTABLE})
124 set(Lua_${module}_FOUND 1)
129 if(EXISTS "${LUA_EXECUTABLE}")
130 execute_process(COMMAND ${LUA_EXECUTABLE} -v OUTPUT_VARIABLE LUA_VERSION ERROR_VARIABLE LUA_VERSION)
131 get_filename_component(LUA_DIR "${LUA_EXECUTABLE}" DIRECTORY)
132 elseif(EXISTS "${LUAC_EXECUTABLE}")
133 execute_process(COMMAND ${LUAC_EXECUTABLE} -v OUTPUT_VARIABLE LUA_VERSION ERROR_VARIABLE LUA_VERSION)
134 get_filename_component(LUA_DIR "${LUAC_EXECUTABLE}" DIRECTORY)
137 get_filename_component(LUA_DIR "${LUA_DIR}" DIRECTORY)
138 find_path(LUA_INCLUDE_DIR lua.h HINTS ${LUA_DIR} PATH_SUFFIXES include)
139 string(REGEX REPLACE "Lua[ ]+([^\n ]+).*" "\\1" LUA_VERSION "${LUA_VERSION}")
140 string(REGEX MATCHALL "[0-9]+" VERSION "${LUA_VERSION}.0.0.0")
141 list(GET VERSION 0 LUA_VERSION_MAJOR)
142 list(GET VERSION 1 LUA_VERSION_MINOR)
143 list(GET VERSION 2 LUA_VERSION_PATCH)
147 find_library(LUA_LIBRARY NAMES_PER_DIR NAMES
148 lua${LUA_VERSION_MAJOR}${LUA_VERSION_MINOR}
149 lua${LUA_VERSION_MAJOR}.${LUA_VERSION_MINOR}
150 lua-${LUA_VERSION_MAJOR}.${LUA_VERSION_MINOR}
151 lua HINTS ${LUA_DIR} PATH_SUFFIXES lib
153 mark_as_advanced(LUA_LIBRARY)
156 if(NOT APPLE AND UNIX)
157 find_library(LUA_MATH_LIBRARY m)
158 mark_as_advanced(LUA_MATH_LIBRARY)
159 if(EXISTS "${LUA_MATH_LIBRARY}")
160 list(APPEND LUA_LIBRARY2 ${LUA_MATH_LIBRARY})
162 list(APPEND LUA_LIBRARY2 m)
164 get_filename_component(ext ${LUA_LIBRARY} EXT)
165 if(ext STREQUAL CMAKE_STATIC_LIBRARY_SUFFIX)
166 list(APPEND LUA_LIBRARY2 ${CMAKE_DL_LIBS})
170 if(EXISTS "${LUA_LIBRARY}")
172 ${LUA_LIBRARY} ${LUA_LIBRARY2}
176 find_package_handle_standard_args(Lua
187 if(NOT TARGET lualib AND EXISTS "${LUA_LIBRARY}")
188 add_library(lualib UNKNOWN IMPORTED)
189 set_target_properties(lualib PROPERTIES
190 IMPORTED_LINK_INTERFACE_LANGUAGES C
191 INTERFACE_INCLUDE_DIRECTORIES "${LUA_INCLUDE_DIR}"
192 IMPORTED_LOCATION "${LUA_LIBRARY}"
194 set_property(TARGET lualib APPEND PROPERTY
195 INTERFACE_LINK_LIBRARIES ${LUA_LIBRARY2}