FindLua.cmake 8.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242
  1. # Distributed under the OSI-approved BSD 3-Clause License. See accompanying
  2. # file Copyright.txt or https://cmake.org/licensing for details.
  3. #[=======================================================================[.rst:
  4. FindLua
  5. -------
  6. Locate Lua library.
  7. .. versionadded:: 3.18
  8. Support for Lua 5.4.
  9. This module defines:
  10. ``LUA_FOUND``
  11. if false, do not try to link to Lua
  12. ``LUA_LIBRARIES``
  13. both lua and lualib
  14. ``LUA_INCLUDE_DIR``
  15. where to find lua.h
  16. ``LUA_VERSION_STRING``
  17. the version of Lua found
  18. ``LUA_VERSION_MAJOR``
  19. the major version of Lua
  20. ``LUA_VERSION_MINOR``
  21. the minor version of Lua
  22. ``LUA_VERSION_PATCH``
  23. the patch version of Lua
  24. Note that the expected include convention is
  25. ::
  26. #include "lua.h"
  27. and not
  28. ::
  29. #include <lua/lua.h>
  30. This is because, the lua location is not standardized and may exist in
  31. locations other than lua/
  32. #]=======================================================================]
  33. cmake_policy(PUSH) # Policies apply to functions at definition-time
  34. cmake_policy(SET CMP0159 NEW) # file(STRINGS) with REGEX updates CMAKE_MATCH_<n>
  35. unset(_lua_include_subdirs)
  36. unset(_lua_library_names)
  37. unset(_lua_append_versions)
  38. # this is a function only to have all the variables inside go away automatically
  39. function(_lua_get_versions)
  40. set(LUA_VERSIONS5 5.4 5.3 5.2 5.1 5.0)
  41. if (Lua_FIND_VERSION_EXACT)
  42. if (Lua_FIND_VERSION_COUNT GREATER 1)
  43. set(_lua_append_versions ${Lua_FIND_VERSION_MAJOR}.${Lua_FIND_VERSION_MINOR})
  44. endif ()
  45. elseif (Lua_FIND_VERSION)
  46. # once there is a different major version supported this should become a loop
  47. if (NOT Lua_FIND_VERSION_MAJOR GREATER 5)
  48. if (Lua_FIND_VERSION_COUNT EQUAL 1)
  49. set(_lua_append_versions ${LUA_VERSIONS5})
  50. else ()
  51. foreach (subver IN LISTS LUA_VERSIONS5)
  52. if (NOT subver VERSION_LESS ${Lua_FIND_VERSION})
  53. list(APPEND _lua_append_versions ${subver})
  54. endif ()
  55. endforeach ()
  56. # New version -> Search for it (heuristic only! Defines in include might have changed)
  57. if (NOT _lua_append_versions)
  58. set(_lua_append_versions ${Lua_FIND_VERSION_MAJOR}.${Lua_FIND_VERSION_MINOR})
  59. endif()
  60. endif ()
  61. endif ()
  62. else ()
  63. # once there is a different major version supported this should become a loop
  64. set(_lua_append_versions ${LUA_VERSIONS5})
  65. endif ()
  66. if (LUA_Debug)
  67. message(STATUS "Considering following Lua versions: ${_lua_append_versions}")
  68. endif()
  69. set(_lua_append_versions "${_lua_append_versions}" PARENT_SCOPE)
  70. endfunction()
  71. function(_lua_set_version_vars)
  72. set(_lua_include_subdirs_raw "lua")
  73. foreach (ver IN LISTS _lua_append_versions)
  74. string(REGEX MATCH "^([0-9]+)\\.([0-9]+)$" _ver "${ver}")
  75. list(APPEND _lua_include_subdirs_raw
  76. lua${CMAKE_MATCH_1}${CMAKE_MATCH_2}
  77. lua${CMAKE_MATCH_1}.${CMAKE_MATCH_2}
  78. lua-${CMAKE_MATCH_1}.${CMAKE_MATCH_2}
  79. )
  80. endforeach ()
  81. # Prepend "include/" to each path directly after the path
  82. set(_lua_include_subdirs "include")
  83. foreach (dir IN LISTS _lua_include_subdirs_raw)
  84. list(APPEND _lua_include_subdirs "${dir}" "include/${dir}")
  85. endforeach ()
  86. set(_lua_include_subdirs "${_lua_include_subdirs}" PARENT_SCOPE)
  87. endfunction()
  88. function(_lua_get_header_version)
  89. unset(LUA_VERSION_STRING PARENT_SCOPE)
  90. set(_hdr_file "${LUA_INCLUDE_DIR}/lua.h")
  91. if (NOT EXISTS "${_hdr_file}")
  92. return()
  93. endif ()
  94. # At least 5.[012] have different ways to express the version
  95. # so all of them need to be tested. Lua 5.2 defines LUA_VERSION
  96. # and LUA_RELEASE as joined by the C preprocessor, so avoid those.
  97. file(STRINGS "${_hdr_file}" lua_version_strings
  98. REGEX "^#define[ \t]+LUA_(RELEASE[ \t]+\"Lua [0-9]|VERSION([ \t]+\"Lua [0-9]|_[MR])).*")
  99. string(REGEX REPLACE ".*;#define[ \t]+LUA_VERSION_MAJOR[ \t]+\"([0-9])\"[ \t]*;.*" "\\1" LUA_VERSION_MAJOR ";${lua_version_strings};")
  100. if (LUA_VERSION_MAJOR MATCHES "^[0-9]+$")
  101. string(REGEX REPLACE ".*;#define[ \t]+LUA_VERSION_MINOR[ \t]+\"([0-9])\"[ \t]*;.*" "\\1" LUA_VERSION_MINOR ";${lua_version_strings};")
  102. string(REGEX REPLACE ".*;#define[ \t]+LUA_VERSION_RELEASE[ \t]+\"([0-9])\"[ \t]*;.*" "\\1" LUA_VERSION_PATCH ";${lua_version_strings};")
  103. set(LUA_VERSION_STRING "${LUA_VERSION_MAJOR}.${LUA_VERSION_MINOR}.${LUA_VERSION_PATCH}")
  104. else ()
  105. string(REGEX REPLACE ".*;#define[ \t]+LUA_RELEASE[ \t]+\"Lua ([0-9.]+)\"[ \t]*;.*" "\\1" LUA_VERSION_STRING ";${lua_version_strings};")
  106. if (NOT LUA_VERSION_STRING MATCHES "^[0-9.]+$")
  107. string(REGEX REPLACE ".*;#define[ \t]+LUA_VERSION[ \t]+\"Lua ([0-9.]+)\"[ \t]*;.*" "\\1" LUA_VERSION_STRING ";${lua_version_strings};")
  108. endif ()
  109. string(REGEX REPLACE "^([0-9]+)\\.[0-9.]*$" "\\1" LUA_VERSION_MAJOR "${LUA_VERSION_STRING}")
  110. string(REGEX REPLACE "^[0-9]+\\.([0-9]+)[0-9.]*$" "\\1" LUA_VERSION_MINOR "${LUA_VERSION_STRING}")
  111. string(REGEX REPLACE "^[0-9]+\\.[0-9]+\\.([0-9]).*" "\\1" LUA_VERSION_PATCH "${LUA_VERSION_STRING}")
  112. endif ()
  113. foreach (ver IN LISTS _lua_append_versions)
  114. if (ver STREQUAL "${LUA_VERSION_MAJOR}.${LUA_VERSION_MINOR}")
  115. set(LUA_VERSION_MAJOR ${LUA_VERSION_MAJOR} PARENT_SCOPE)
  116. set(LUA_VERSION_MINOR ${LUA_VERSION_MINOR} PARENT_SCOPE)
  117. set(LUA_VERSION_PATCH ${LUA_VERSION_PATCH} PARENT_SCOPE)
  118. set(LUA_VERSION_STRING ${LUA_VERSION_STRING} PARENT_SCOPE)
  119. return()
  120. endif ()
  121. endforeach ()
  122. endfunction()
  123. function(_lua_find_header)
  124. _lua_set_version_vars()
  125. # Initialize as local variable
  126. set(CMAKE_IGNORE_PATH ${CMAKE_IGNORE_PATH})
  127. while (TRUE)
  128. # Find the next header to test. Check each possible subdir in order
  129. # This prefers e.g. higher versions as they are earlier in the list
  130. # It is also consistent with previous versions of FindLua
  131. foreach (subdir IN LISTS _lua_include_subdirs)
  132. find_path(LUA_INCLUDE_DIR lua.h
  133. HINTS ENV LUA_DIR
  134. PATH_SUFFIXES ${subdir}
  135. )
  136. if (LUA_INCLUDE_DIR)
  137. break()
  138. endif()
  139. endforeach()
  140. # Did not found header -> Fail
  141. if (NOT LUA_INCLUDE_DIR)
  142. return()
  143. endif()
  144. _lua_get_header_version()
  145. # Found accepted version -> Ok
  146. if (LUA_VERSION_STRING)
  147. if (LUA_Debug)
  148. message(STATUS "Found suitable version ${LUA_VERSION_STRING} in ${LUA_INCLUDE_DIR}/lua.h")
  149. endif()
  150. return()
  151. endif()
  152. # Found wrong version -> Ignore this path and retry
  153. if (LUA_Debug)
  154. message(STATUS "Ignoring unsuitable version in ${LUA_INCLUDE_DIR}")
  155. endif()
  156. list(APPEND CMAKE_IGNORE_PATH "${LUA_INCLUDE_DIR}")
  157. unset(LUA_INCLUDE_DIR CACHE)
  158. unset(LUA_INCLUDE_DIR)
  159. unset(LUA_INCLUDE_DIR PARENT_SCOPE)
  160. endwhile ()
  161. endfunction()
  162. _lua_get_versions()
  163. _lua_find_header()
  164. _lua_get_header_version()
  165. unset(_lua_append_versions)
  166. if (LUA_VERSION_STRING)
  167. set(_lua_library_names
  168. lua${LUA_VERSION_MAJOR}${LUA_VERSION_MINOR}
  169. lua${LUA_VERSION_MAJOR}.${LUA_VERSION_MINOR}
  170. lua-${LUA_VERSION_MAJOR}.${LUA_VERSION_MINOR}
  171. lua.${LUA_VERSION_MAJOR}.${LUA_VERSION_MINOR}
  172. )
  173. endif ()
  174. find_library(LUA_LIBRARY
  175. NAMES ${_lua_library_names} lua
  176. NAMES_PER_DIR
  177. HINTS
  178. ENV LUA_DIR
  179. PATH_SUFFIXES lib
  180. )
  181. unset(_lua_library_names)
  182. if (LUA_LIBRARY)
  183. # include the math library for Unix
  184. if (UNIX AND NOT APPLE AND NOT BEOS)
  185. find_library(LUA_MATH_LIBRARY m)
  186. mark_as_advanced(LUA_MATH_LIBRARY)
  187. set(LUA_LIBRARIES "${LUA_LIBRARY};${LUA_MATH_LIBRARY}")
  188. # include dl library for statically-linked Lua library
  189. get_filename_component(LUA_LIB_EXT ${LUA_LIBRARY} EXT)
  190. if(LUA_LIB_EXT STREQUAL CMAKE_STATIC_LIBRARY_SUFFIX)
  191. list(APPEND LUA_LIBRARIES ${CMAKE_DL_LIBS})
  192. endif()
  193. # For Windows and Mac, don't need to explicitly include the math library
  194. else ()
  195. set(LUA_LIBRARIES "${LUA_LIBRARY}")
  196. endif ()
  197. endif ()
  198. include(${CMAKE_CURRENT_LIST_DIR}/FindPackageHandleStandardArgs.cmake)
  199. # handle the QUIETLY and REQUIRED arguments and set LUA_FOUND to TRUE if
  200. # all listed variables are TRUE
  201. FIND_PACKAGE_HANDLE_STANDARD_ARGS(Lua
  202. REQUIRED_VARS LUA_LIBRARIES LUA_INCLUDE_DIR
  203. VERSION_VAR LUA_VERSION_STRING)
  204. mark_as_advanced(LUA_INCLUDE_DIR LUA_LIBRARY)
  205. cmake_policy(POP)