FindLua.cmake 8.2 KB

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