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