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 REPLACE "^[0-9]+\\.[0-9]+\\.([0-9]).*" "\\1" LUA_VERSION_PATCH "${LUA_VERSION}")
91 string(REGEX REPLACE "^[0-9]+\\.([0-9]).*" "\\1" LUA_VERSION_MINOR "${LUA_VERSION}")
92 string(REGEX REPLACE "^([0-9]).*" "\\1" LUA_VERSION_MAJOR "${LUA_VERSION}")
96 if(NOT DEFINED ENV{LUA_DIR} AND EXISTS "${LUA_INCLUDE_DIR}")
97 get_filename_component(LUA_DIR "${LUA_INCLUDE_DIR}" DIRECTORY)
98 get_filename_component(modules "${LUA_INCLUDE_DIR}" NAME)
99 if(NOT modules STREQUAL "include")
100 get_filename_component(LUA_DIR "${LUA_DIR}" DIRECTORY)
102 elseif(DEFINED ENV{LUA_DIR})
103 file(TO_CMAKE_PATH "$ENV{LUA_DIR}" LUA_DIR)
106 string(REGEX MATCHALL "(luac|lua)" modules "${Lua_FIND_COMPONENTS}")
108 list(REMOVE_DUPLICATES modules)
110 foreach(module ${modules})
111 string(TOUPPER ${module} MODULE)
112 find_program(${MODULE}_EXECUTABLE NAMES
113 ${module}${LUA_VERSION_MAJOR}${LUA_VERSION_MINOR}
114 ${module}${LUA_VERSION_MAJOR}.${LUA_VERSION_MINOR}
115 ${module}-${LUA_VERSION_MAJOR}.${LUA_VERSION_MINOR}
116 ${module} HINTS ${LUA_DIR} PATH_SUFFIXES bin
118 mark_as_advanced(${MODULE}_EXECUTABLE)
119 if(Lua_FIND_REQUIRED_${module})
120 list(APPEND LUA_REQUIRED_VARS ${MODULE}_EXECUTABLE)
122 if(EXISTS ${${MODULE}_EXECUTABLE})
123 set(Lua_${module}_FOUND 1)
130 if(EXISTS "${LUA_EXECUTABLE}")
131 execute_process(COMMAND ${LUA_EXECUTABLE} -v OUTPUT_VARIABLE LUA_VERSION ERROR_VARIABLE LUA_VERSION)
132 get_filename_component(LUA_DIR "${LUA_EXECUTABLE}" DIRECTORY)
133 elseif(EXISTS "${LUAC_EXECUTABLE}")
134 execute_process(COMMAND ${LUAC_EXECUTABLE} -v OUTPUT_VARIABLE LUA_VERSION ERROR_VARIABLE LUA_VERSION)
135 get_filename_component(LUA_DIR "${LUAC_EXECUTABLE}" DIRECTORY)
138 get_filename_component(LUA_DIR "${LUA_DIR}" DIRECTORY)
139 find_path(LUA_INCLUDE_DIR lua.h HINTS ${LUA_DIR} PATH_SUFFIXES include)
140 string(REGEX REPLACE "Lua[ ]+([^ ]+).*" "\\1" LUA_VERSION "${LUA_VERSION}")
141 string(REGEX REPLACE "^[0-9]+\\.[0-9]+\\.([0-9]).*" "\\1" LUA_VERSION_PATCH "${LUA_VERSION}")
142 string(REGEX REPLACE "^[0-9]+\\.([0-9]).*" "\\1" LUA_VERSION_MINOR "${LUA_VERSION}")
143 string(REGEX REPLACE "^([0-9]).*" "\\1" LUA_VERSION_MAJOR "${LUA_VERSION}")
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)
155 if(EXISTS "${LUA_LIBRARY}")
156 list(APPEND LUA_LIBRARIES ${LUA_LIBRARY})
157 if(UNIX AND NOT APPLE AND NOT BEOS)
158 find_library(LUA_MATH_LIBRARY m)
159 mark_as_advanced(LUA_MATH_LIBRARY)
160 if(NOT EXISTS "${LUA_MATH_LIBRARY}")
161 list(APPEND LUA_LIBRARIES m)
163 list(APPEND LUA_LIBRARIES ${LUA_MATH_LIBRARY})
165 get_filename_component(ext ${LUA_LIBRARY} EXT)
166 if(CMAKE_STATIC_LIBRARY_SUFFIX STREQUAL ext)
167 list(APPEND LUA_LIBRARIES ${CMAKE_DL_LIBS})
173 find_package_handle_standard_args(Lua
184 if(NOT TARGET lualib AND EXISTS "${LUA_LIBRARY}")
185 add_library(lualib UNKNOWN IMPORTED)
186 set_target_properties(lualib PROPERTIES
187 INTERFACE_INCLUDE_DIRECTORIES "${LUA_INCLUDE_DIR}"
188 IMPORTED_LINK_INTERFACE_LANGUAGES C
189 IMPORTED_LOCATION "${LUA_LIBRARY}"
191 set(libraries ${LUA_LIBRARIES})
192 list(REMOVE_ITEM libraries ${LUA_LIBRARY})
193 set_property(TARGET lualib APPEND PROPERTY
194 INTERFACE_LINK_LIBRARIES ${libraries}