FindLua.cmake 8.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236
  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 This module defines
  7. ::
  8. LUA_FOUND - if false, do not try to link to Lua
  9. LUA_LIBRARIES - both lua and lualib
  10. LUA_INCLUDE_DIR - where to find lua.h
  11. LUA_VERSION_STRING - the version of Lua found
  12. LUA_VERSION_MAJOR - the major version of Lua
  13. LUA_VERSION_MINOR - the minor version of Lua
  14. LUA_VERSION_PATCH - the patch version of Lua
  15. Note that the expected include convention is
  16. ::
  17. #include "lua.h"
  18. and not
  19. ::
  20. #include <lua/lua.h>
  21. This is because, the lua location is not standardized and may exist in
  22. locations other than lua/
  23. #]=======================================================================]
  24. cmake_policy(PUSH) # Policies apply to functions at definition-time
  25. cmake_policy(SET CMP0012 NEW) # For while(TRUE)
  26. unset(_lua_include_subdirs)
  27. unset(_lua_library_names)
  28. unset(_lua_append_versions)
  29. # this is a function only to have all the variables inside go away automatically
  30. function(_lua_get_versions)
  31. set(LUA_VERSIONS5 5.3 5.2 5.1 5.0)
  32. if (Lua_FIND_VERSION_EXACT)
  33. if (Lua_FIND_VERSION_COUNT GREATER 1)
  34. set(_lua_append_versions ${Lua_FIND_VERSION_MAJOR}.${Lua_FIND_VERSION_MINOR})
  35. endif ()
  36. elseif (Lua_FIND_VERSION)
  37. # once there is a different major version supported this should become a loop
  38. if (NOT Lua_FIND_VERSION_MAJOR GREATER 5)
  39. if (Lua_FIND_VERSION_COUNT EQUAL 1)
  40. set(_lua_append_versions ${LUA_VERSIONS5})
  41. else ()
  42. foreach (subver IN LISTS LUA_VERSIONS5)
  43. if (NOT subver VERSION_LESS ${Lua_FIND_VERSION})
  44. list(APPEND _lua_append_versions ${subver})
  45. endif ()
  46. endforeach ()
  47. # New version -> Search for it (heuristic only! Defines in include might have changed)
  48. if (NOT _lua_append_versions)
  49. set(_lua_append_versions ${Lua_FIND_VERSION_MAJOR}.${Lua_FIND_VERSION_MINOR})
  50. endif()
  51. endif ()
  52. endif ()
  53. else ()
  54. # once there is a different major version supported this should become a loop
  55. set(_lua_append_versions ${LUA_VERSIONS5})
  56. endif ()
  57. if (LUA_Debug)
  58. message(STATUS "Considering following Lua versions: ${_lua_append_versions}")
  59. endif()
  60. set(_lua_append_versions "${_lua_append_versions}" PARENT_SCOPE)
  61. endfunction()
  62. function(_lua_set_version_vars)
  63. set(_lua_include_subdirs_raw "lua")
  64. foreach (ver IN LISTS _lua_append_versions)
  65. string(REGEX MATCH "^([0-9]+)\\.([0-9]+)$" _ver "${ver}")
  66. list(APPEND _lua_include_subdirs_raw
  67. lua${CMAKE_MATCH_1}${CMAKE_MATCH_2}
  68. lua${CMAKE_MATCH_1}.${CMAKE_MATCH_2}
  69. lua-${CMAKE_MATCH_1}.${CMAKE_MATCH_2}
  70. )
  71. endforeach ()
  72. # Prepend "include/" to each path directly after the path
  73. set(_lua_include_subdirs "include")
  74. foreach (dir IN LISTS _lua_include_subdirs_raw)
  75. list(APPEND _lua_include_subdirs "${dir}" "include/${dir}")
  76. endforeach ()
  77. set(_lua_include_subdirs "${_lua_include_subdirs}" PARENT_SCOPE)
  78. endfunction(_lua_set_version_vars)
  79. function(_lua_get_header_version)
  80. unset(LUA_VERSION_STRING PARENT_SCOPE)
  81. set(_hdr_file "${LUA_INCLUDE_DIR}/lua.h")
  82. if (NOT EXISTS "${_hdr_file}")
  83. return()
  84. endif ()
  85. # At least 5.[012] have different ways to express the version
  86. # so all of them need to be tested. Lua 5.2 defines LUA_VERSION
  87. # and LUA_RELEASE as joined by the C preprocessor, so avoid those.
  88. file(STRINGS "${_hdr_file}" lua_version_strings
  89. REGEX "^#define[ \t]+LUA_(RELEASE[ \t]+\"Lua [0-9]|VERSION([ \t]+\"Lua [0-9]|_[MR])).*")
  90. string(REGEX REPLACE ".*;#define[ \t]+LUA_VERSION_MAJOR[ \t]+\"([0-9])\"[ \t]*;.*" "\\1" LUA_VERSION_MAJOR ";${lua_version_strings};")
  91. if (LUA_VERSION_MAJOR MATCHES "^[0-9]+$")
  92. string(REGEX REPLACE ".*;#define[ \t]+LUA_VERSION_MINOR[ \t]+\"([0-9])\"[ \t]*;.*" "\\1" LUA_VERSION_MINOR ";${lua_version_strings};")
  93. string(REGEX REPLACE ".*;#define[ \t]+LUA_VERSION_RELEASE[ \t]+\"([0-9])\"[ \t]*;.*" "\\1" LUA_VERSION_PATCH ";${lua_version_strings};")
  94. set(LUA_VERSION_STRING "${LUA_VERSION_MAJOR}.${LUA_VERSION_MINOR}.${LUA_VERSION_PATCH}")
  95. else ()
  96. string(REGEX REPLACE ".*;#define[ \t]+LUA_RELEASE[ \t]+\"Lua ([0-9.]+)\"[ \t]*;.*" "\\1" LUA_VERSION_STRING ";${lua_version_strings};")
  97. if (NOT LUA_VERSION_STRING MATCHES "^[0-9.]+$")
  98. string(REGEX REPLACE ".*;#define[ \t]+LUA_VERSION[ \t]+\"Lua ([0-9.]+)\"[ \t]*;.*" "\\1" LUA_VERSION_STRING ";${lua_version_strings};")
  99. endif ()
  100. string(REGEX REPLACE "^([0-9]+)\\.[0-9.]*$" "\\1" LUA_VERSION_MAJOR "${LUA_VERSION_STRING}")
  101. string(REGEX REPLACE "^[0-9]+\\.([0-9]+)[0-9.]*$" "\\1" LUA_VERSION_MINOR "${LUA_VERSION_STRING}")
  102. string(REGEX REPLACE "^[0-9]+\\.[0-9]+\\.([0-9]).*" "\\1" LUA_VERSION_PATCH "${LUA_VERSION_STRING}")
  103. endif ()
  104. foreach (ver IN LISTS _lua_append_versions)
  105. if (ver STREQUAL "${LUA_VERSION_MAJOR}.${LUA_VERSION_MINOR}")
  106. set(LUA_VERSION_MAJOR ${LUA_VERSION_MAJOR} PARENT_SCOPE)
  107. set(LUA_VERSION_MINOR ${LUA_VERSION_MINOR} PARENT_SCOPE)
  108. set(LUA_VERSION_PATCH ${LUA_VERSION_PATCH} PARENT_SCOPE)
  109. set(LUA_VERSION_STRING ${LUA_VERSION_STRING} PARENT_SCOPE)
  110. return()
  111. endif ()
  112. endforeach ()
  113. endfunction(_lua_get_header_version)
  114. function(_lua_find_header)
  115. _lua_set_version_vars()
  116. # Initialize as local variable
  117. set(CMAKE_IGNORE_PATH ${CMAKE_IGNORE_PATH})
  118. while (TRUE)
  119. # Find the next header to test. Check each possible subdir in order
  120. # This prefers e.g. higher versions as they are earlier in the list
  121. # It is also consistent with previous versions of FindLua
  122. foreach (subdir IN LISTS _lua_include_subdirs)
  123. find_path(LUA_INCLUDE_DIR lua.h
  124. HINTS
  125. 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)