FindLua.cmake 8.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243
  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 CMP0012 NEW) # For while(TRUE)
  35. cmake_policy(SET CMP0159 NEW) # file(STRINGS) with REGEX updates CMAKE_MATCH_<n>
  36. unset(_lua_include_subdirs)
  37. unset(_lua_library_names)
  38. unset(_lua_append_versions)
  39. # this is a function only to have all the variables inside go away automatically
  40. function(_lua_get_versions)
  41. set(LUA_VERSIONS5 5.4 5.3 5.2 5.1 5.0)
  42. if (Lua_FIND_VERSION_EXACT)
  43. if (Lua_FIND_VERSION_COUNT GREATER 1)
  44. set(_lua_append_versions ${Lua_FIND_VERSION_MAJOR}.${Lua_FIND_VERSION_MINOR})
  45. endif ()
  46. elseif (Lua_FIND_VERSION)
  47. # once there is a different major version supported this should become a loop
  48. if (NOT Lua_FIND_VERSION_MAJOR GREATER 5)
  49. if (Lua_FIND_VERSION_COUNT EQUAL 1)
  50. set(_lua_append_versions ${LUA_VERSIONS5})
  51. else ()
  52. foreach (subver IN LISTS LUA_VERSIONS5)
  53. if (NOT subver VERSION_LESS ${Lua_FIND_VERSION})
  54. list(APPEND _lua_append_versions ${subver})
  55. endif ()
  56. endforeach ()
  57. # New version -> Search for it (heuristic only! Defines in include might have changed)
  58. if (NOT _lua_append_versions)
  59. set(_lua_append_versions ${Lua_FIND_VERSION_MAJOR}.${Lua_FIND_VERSION_MINOR})
  60. endif()
  61. endif ()
  62. endif ()
  63. else ()
  64. # once there is a different major version supported this should become a loop
  65. set(_lua_append_versions ${LUA_VERSIONS5})
  66. endif ()
  67. if (LUA_Debug)
  68. message(STATUS "Considering following Lua versions: ${_lua_append_versions}")
  69. endif()
  70. set(_lua_append_versions "${_lua_append_versions}" PARENT_SCOPE)
  71. endfunction()
  72. function(_lua_set_version_vars)
  73. set(_lua_include_subdirs_raw "lua")
  74. foreach (ver IN LISTS _lua_append_versions)
  75. string(REGEX MATCH "^([0-9]+)\\.([0-9]+)$" _ver "${ver}")
  76. list(APPEND _lua_include_subdirs_raw
  77. lua${CMAKE_MATCH_1}${CMAKE_MATCH_2}
  78. lua${CMAKE_MATCH_1}.${CMAKE_MATCH_2}
  79. lua-${CMAKE_MATCH_1}.${CMAKE_MATCH_2}
  80. )
  81. endforeach ()
  82. # Prepend "include/" to each path directly after the path
  83. set(_lua_include_subdirs "include")
  84. foreach (dir IN LISTS _lua_include_subdirs_raw)
  85. list(APPEND _lua_include_subdirs "${dir}" "include/${dir}")
  86. endforeach ()
  87. set(_lua_include_subdirs "${_lua_include_subdirs}" PARENT_SCOPE)
  88. endfunction(_lua_set_version_vars)
  89. function(_lua_get_header_version)
  90. unset(LUA_VERSION_STRING PARENT_SCOPE)
  91. set(_hdr_file "${LUA_INCLUDE_DIR}/lua.h")
  92. if (NOT EXISTS "${_hdr_file}")
  93. return()
  94. 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])).*")
  100. string(REGEX REPLACE ".*;#define[ \t]+LUA_VERSION_MAJOR[ \t]+\"([0-9])\"[ \t]*;.*" "\\1" LUA_VERSION_MAJOR ";${lua_version_strings};")
  101. if (LUA_VERSION_MAJOR MATCHES "^[0-9]+$")
  102. string(REGEX REPLACE ".*;#define[ \t]+LUA_VERSION_MINOR[ \t]+\"([0-9])\"[ \t]*;.*" "\\1" LUA_VERSION_MINOR ";${lua_version_strings};")
  103. string(REGEX REPLACE ".*;#define[ \t]+LUA_VERSION_RELEASE[ \t]+\"([0-9])\"[ \t]*;.*" "\\1" LUA_VERSION_PATCH ";${lua_version_strings};")
  104. set(LUA_VERSION_STRING "${LUA_VERSION_MAJOR}.${LUA_VERSION_MINOR}.${LUA_VERSION_PATCH}")
  105. else ()
  106. string(REGEX REPLACE ".*;#define[ \t]+LUA_RELEASE[ \t]+\"Lua ([0-9.]+)\"[ \t]*;.*" "\\1" LUA_VERSION_STRING ";${lua_version_strings};")
  107. if (NOT LUA_VERSION_STRING MATCHES "^[0-9.]+$")
  108. string(REGEX REPLACE ".*;#define[ \t]+LUA_VERSION[ \t]+\"Lua ([0-9.]+)\"[ \t]*;.*" "\\1" LUA_VERSION_STRING ";${lua_version_strings};")
  109. endif ()
  110. string(REGEX REPLACE "^([0-9]+)\\.[0-9.]*$" "\\1" LUA_VERSION_MAJOR "${LUA_VERSION_STRING}")
  111. string(REGEX REPLACE "^[0-9]+\\.([0-9]+)[0-9.]*$" "\\1" LUA_VERSION_MINOR "${LUA_VERSION_STRING}")
  112. string(REGEX REPLACE "^[0-9]+\\.[0-9]+\\.([0-9]).*" "\\1" LUA_VERSION_PATCH "${LUA_VERSION_STRING}")
  113. endif ()
  114. foreach (ver IN LISTS _lua_append_versions)
  115. if (ver STREQUAL "${LUA_VERSION_MAJOR}.${LUA_VERSION_MINOR}")
  116. set(LUA_VERSION_MAJOR ${LUA_VERSION_MAJOR} PARENT_SCOPE)
  117. set(LUA_VERSION_MINOR ${LUA_VERSION_MINOR} PARENT_SCOPE)
  118. set(LUA_VERSION_PATCH ${LUA_VERSION_PATCH} PARENT_SCOPE)
  119. set(LUA_VERSION_STRING ${LUA_VERSION_STRING} PARENT_SCOPE)
  120. return()
  121. endif ()
  122. endforeach ()
  123. endfunction(_lua_get_header_version)
  124. function(_lua_find_header)
  125. _lua_set_version_vars()
  126. # Initialize as local variable
  127. set(CMAKE_IGNORE_PATH ${CMAKE_IGNORE_PATH})
  128. while (TRUE)
  129. # Find the next header to test. Check each possible subdir in order
  130. # This prefers e.g. higher versions as they are earlier in the list
  131. # It is also consistent with previous versions of FindLua
  132. foreach (subdir IN LISTS _lua_include_subdirs)
  133. find_path(LUA_INCLUDE_DIR lua.h
  134. HINTS ENV LUA_DIR
  135. PATH_SUFFIXES ${subdir}
  136. )
  137. if (LUA_INCLUDE_DIR)
  138. break()
  139. endif()
  140. endforeach()
  141. # Did not found header -> Fail
  142. if (NOT LUA_INCLUDE_DIR)
  143. return()
  144. endif()
  145. _lua_get_header_version()
  146. # Found accepted version -> Ok
  147. if (LUA_VERSION_STRING)
  148. if (LUA_Debug)
  149. message(STATUS "Found suitable version ${LUA_VERSION_STRING} in ${LUA_INCLUDE_DIR}/lua.h")
  150. endif()
  151. return()
  152. endif()
  153. # Found wrong version -> Ignore this path and retry
  154. if (LUA_Debug)
  155. message(STATUS "Ignoring unsuitable version in ${LUA_INCLUDE_DIR}")
  156. endif()
  157. list(APPEND CMAKE_IGNORE_PATH "${LUA_INCLUDE_DIR}")
  158. unset(LUA_INCLUDE_DIR CACHE)
  159. unset(LUA_INCLUDE_DIR)
  160. unset(LUA_INCLUDE_DIR PARENT_SCOPE)
  161. endwhile ()
  162. endfunction()
  163. _lua_get_versions()
  164. _lua_find_header()
  165. _lua_get_header_version()
  166. unset(_lua_append_versions)
  167. if (LUA_VERSION_STRING)
  168. set(_lua_library_names
  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. lua.${LUA_VERSION_MAJOR}.${LUA_VERSION_MINOR}
  173. )
  174. endif ()
  175. find_library(LUA_LIBRARY
  176. NAMES ${_lua_library_names} lua
  177. NAMES_PER_DIR
  178. HINTS
  179. ENV LUA_DIR
  180. PATH_SUFFIXES lib
  181. )
  182. unset(_lua_library_names)
  183. if (LUA_LIBRARY)
  184. # include the math library for Unix
  185. if (UNIX AND NOT APPLE AND NOT BEOS)
  186. find_library(LUA_MATH_LIBRARY m)
  187. mark_as_advanced(LUA_MATH_LIBRARY)
  188. set(LUA_LIBRARIES "${LUA_LIBRARY};${LUA_MATH_LIBRARY}")
  189. # include dl library for statically-linked Lua library
  190. get_filename_component(LUA_LIB_EXT ${LUA_LIBRARY} EXT)
  191. if(LUA_LIB_EXT STREQUAL CMAKE_STATIC_LIBRARY_SUFFIX)
  192. list(APPEND LUA_LIBRARIES ${CMAKE_DL_LIBS})
  193. endif()
  194. # For Windows and Mac, don't need to explicitly include the math library
  195. else ()
  196. set(LUA_LIBRARIES "${LUA_LIBRARY}")
  197. endif ()
  198. endif ()
  199. include(${CMAKE_CURRENT_LIST_DIR}/FindPackageHandleStandardArgs.cmake)
  200. # handle the QUIETLY and REQUIRED arguments and set LUA_FOUND to TRUE if
  201. # all listed variables are TRUE
  202. FIND_PACKAGE_HANDLE_STANDARD_ARGS(Lua
  203. REQUIRED_VARS LUA_LIBRARIES LUA_INCLUDE_DIR
  204. VERSION_VAR LUA_VERSION_STRING)
  205. mark_as_advanced(LUA_INCLUDE_DIR LUA_LIBRARY)
  206. cmake_policy(POP)