FindLua.cmake 7.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198
  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. unset(_lua_include_subdirs)
  38. unset(_lua_library_names)
  39. unset(_lua_append_versions)
  40. # this is a function only to have all the variables inside go away automatically
  41. function(_lua_set_version_vars)
  42. set(LUA_VERSIONS5 5.4 5.3 5.2 5.1 5.0)
  43. if (Lua_FIND_VERSION_EXACT)
  44. if (Lua_FIND_VERSION_COUNT GREATER 1)
  45. set(_lua_append_versions ${Lua_FIND_VERSION_MAJOR}.${Lua_FIND_VERSION_MINOR})
  46. endif ()
  47. elseif (Lua_FIND_VERSION)
  48. # once there is a different major version supported this should become a loop
  49. if (NOT Lua_FIND_VERSION_MAJOR GREATER 5)
  50. if (Lua_FIND_VERSION_COUNT EQUAL 1)
  51. set(_lua_append_versions ${LUA_VERSIONS5})
  52. else ()
  53. foreach (subver IN LISTS LUA_VERSIONS5)
  54. if (NOT subver VERSION_LESS ${Lua_FIND_VERSION})
  55. list(APPEND _lua_append_versions ${subver})
  56. endif ()
  57. endforeach ()
  58. endif ()
  59. endif ()
  60. else ()
  61. # once there is a different major version supported this should become a loop
  62. set(_lua_append_versions ${LUA_VERSIONS5})
  63. endif ()
  64. list(APPEND _lua_include_subdirs "include/lua" "include")
  65. foreach (ver IN LISTS _lua_append_versions)
  66. string(REGEX MATCH "^([0-9]+)\\.([0-9]+)$" _ver "${ver}")
  67. list(APPEND _lua_include_subdirs
  68. include/lua${CMAKE_MATCH_1}${CMAKE_MATCH_2}
  69. include/lua${CMAKE_MATCH_1}.${CMAKE_MATCH_2}
  70. include/lua-${CMAKE_MATCH_1}.${CMAKE_MATCH_2}
  71. )
  72. endforeach ()
  73. set(_lua_include_subdirs "${_lua_include_subdirs}" PARENT_SCOPE)
  74. set(_lua_append_versions "${_lua_append_versions}" PARENT_SCOPE)
  75. endfunction(_lua_set_version_vars)
  76. function(_lua_check_header_version _hdr_file)
  77. # At least 5.[012] have different ways to express the version
  78. # so all of them need to be tested. Lua 5.2 defines LUA_VERSION
  79. # and LUA_RELEASE as joined by the C preprocessor, so avoid those.
  80. file(STRINGS "${_hdr_file}" lua_version_strings
  81. REGEX "^#define[ \t]+LUA_(RELEASE[ \t]+\"Lua [0-9]|VERSION([ \t]+\"Lua [0-9]|_[MR])).*")
  82. string(REGEX REPLACE ".*;#define[ \t]+LUA_VERSION_MAJOR[ \t]+\"([0-9])\"[ \t]*;.*" "\\1" LUA_VERSION_MAJOR ";${lua_version_strings};")
  83. if (LUA_VERSION_MAJOR MATCHES "^[0-9]+$")
  84. string(REGEX REPLACE ".*;#define[ \t]+LUA_VERSION_MINOR[ \t]+\"([0-9])\"[ \t]*;.*" "\\1" LUA_VERSION_MINOR ";${lua_version_strings};")
  85. string(REGEX REPLACE ".*;#define[ \t]+LUA_VERSION_RELEASE[ \t]+\"([0-9])\"[ \t]*;.*" "\\1" LUA_VERSION_PATCH ";${lua_version_strings};")
  86. set(LUA_VERSION_STRING "${LUA_VERSION_MAJOR}.${LUA_VERSION_MINOR}.${LUA_VERSION_PATCH}")
  87. else ()
  88. string(REGEX REPLACE ".*;#define[ \t]+LUA_RELEASE[ \t]+\"Lua ([0-9.]+)\"[ \t]*;.*" "\\1" LUA_VERSION_STRING ";${lua_version_strings};")
  89. if (NOT LUA_VERSION_STRING MATCHES "^[0-9.]+$")
  90. string(REGEX REPLACE ".*;#define[ \t]+LUA_VERSION[ \t]+\"Lua ([0-9.]+)\"[ \t]*;.*" "\\1" LUA_VERSION_STRING ";${lua_version_strings};")
  91. endif ()
  92. string(REGEX REPLACE "^([0-9]+)\\.[0-9.]*$" "\\1" LUA_VERSION_MAJOR "${LUA_VERSION_STRING}")
  93. string(REGEX REPLACE "^[0-9]+\\.([0-9]+)[0-9.]*$" "\\1" LUA_VERSION_MINOR "${LUA_VERSION_STRING}")
  94. string(REGEX REPLACE "^[0-9]+\\.[0-9]+\\.([0-9]).*" "\\1" LUA_VERSION_PATCH "${LUA_VERSION_STRING}")
  95. endif ()
  96. foreach (ver IN LISTS _lua_append_versions)
  97. if (ver STREQUAL "${LUA_VERSION_MAJOR}.${LUA_VERSION_MINOR}")
  98. set(LUA_VERSION_MAJOR ${LUA_VERSION_MAJOR} PARENT_SCOPE)
  99. set(LUA_VERSION_MINOR ${LUA_VERSION_MINOR} PARENT_SCOPE)
  100. set(LUA_VERSION_PATCH ${LUA_VERSION_PATCH} PARENT_SCOPE)
  101. set(LUA_VERSION_STRING ${LUA_VERSION_STRING} PARENT_SCOPE)
  102. return()
  103. endif ()
  104. endforeach ()
  105. endfunction(_lua_check_header_version)
  106. _lua_set_version_vars()
  107. if (LUA_INCLUDE_DIR AND EXISTS "${LUA_INCLUDE_DIR}/lua.h")
  108. _lua_check_header_version("${LUA_INCLUDE_DIR}/lua.h")
  109. endif ()
  110. if (NOT LUA_VERSION_STRING)
  111. foreach (subdir IN LISTS _lua_include_subdirs)
  112. unset(LUA_INCLUDE_PREFIX CACHE)
  113. find_path(LUA_INCLUDE_PREFIX ${subdir}/lua.h
  114. HINTS
  115. ENV LUA_DIR
  116. PATHS
  117. ~/Library/Frameworks
  118. /Library/Frameworks
  119. /sw # Fink
  120. /opt/local # DarwinPorts
  121. /opt/csw # Blastwave
  122. /opt
  123. )
  124. if (LUA_INCLUDE_PREFIX)
  125. _lua_check_header_version("${LUA_INCLUDE_PREFIX}/${subdir}/lua.h")
  126. if (LUA_VERSION_STRING)
  127. set(LUA_INCLUDE_DIR "${LUA_INCLUDE_PREFIX}/${subdir}")
  128. break()
  129. endif ()
  130. endif ()
  131. endforeach ()
  132. endif ()
  133. unset(_lua_include_subdirs)
  134. unset(_lua_append_versions)
  135. if (LUA_VERSION_STRING)
  136. set(_lua_library_names
  137. lua${LUA_VERSION_MAJOR}${LUA_VERSION_MINOR}
  138. lua${LUA_VERSION_MAJOR}.${LUA_VERSION_MINOR}
  139. lua-${LUA_VERSION_MAJOR}.${LUA_VERSION_MINOR}
  140. lua.${LUA_VERSION_MAJOR}.${LUA_VERSION_MINOR}
  141. )
  142. endif ()
  143. find_library(LUA_LIBRARY
  144. NAMES ${_lua_library_names} lua
  145. HINTS
  146. ENV LUA_DIR
  147. PATH_SUFFIXES lib
  148. PATHS
  149. ~/Library/Frameworks
  150. /Library/Frameworks
  151. /sw
  152. /opt/local
  153. /opt/csw
  154. /opt
  155. )
  156. unset(_lua_library_names)
  157. if (LUA_LIBRARY)
  158. # include the math library for Unix
  159. if (UNIX AND NOT APPLE AND NOT BEOS)
  160. find_library(LUA_MATH_LIBRARY m)
  161. set(LUA_LIBRARIES "${LUA_LIBRARY};${LUA_MATH_LIBRARY}")
  162. # include dl library for statically-linked Lua library
  163. get_filename_component(LUA_LIB_EXT ${LUA_LIBRARY} EXT)
  164. if(LUA_LIB_EXT STREQUAL CMAKE_STATIC_LIBRARY_SUFFIX)
  165. list(APPEND LUA_LIBRARIES ${CMAKE_DL_LIBS})
  166. endif()
  167. # For Windows and Mac, don't need to explicitly include the math library
  168. else ()
  169. set(LUA_LIBRARIES "${LUA_LIBRARY}")
  170. endif ()
  171. endif ()
  172. include(FindPackageHandleStandardArgs)
  173. # handle the QUIETLY and REQUIRED arguments and set LUA_FOUND to TRUE if
  174. # all listed variables are TRUE
  175. FIND_PACKAGE_HANDLE_STANDARD_ARGS(Lua
  176. REQUIRED_VARS LUA_LIBRARIES LUA_INCLUDE_DIR
  177. VERSION_VAR LUA_VERSION_STRING)
  178. mark_as_advanced(LUA_INCLUDE_DIR LUA_LIBRARY LUA_MATH_LIBRARY)