FindLua.cmake 8.5 KB

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