FindLua.cmake 7.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199
  1. #.rst:
  2. # FindLua
  3. # -------
  4. #
  5. #
  6. #
  7. # Locate Lua library This module defines
  8. #
  9. # ::
  10. #
  11. # LUA_FOUND - if false, do not try to link to Lua
  12. # LUA_LIBRARIES - both lua and lualib
  13. # LUA_INCLUDE_DIR - where to find lua.h
  14. # LUA_VERSION_STRING - the version of Lua found
  15. # LUA_VERSION_MAJOR - the major version of Lua
  16. # LUA_VERSION_MINOR - the minor version of Lua
  17. # LUA_VERSION_PATCH - the patch version of Lua
  18. #
  19. #
  20. #
  21. # Note that the expected include convention is
  22. #
  23. # ::
  24. #
  25. # #include "lua.h"
  26. #
  27. # and not
  28. #
  29. # ::
  30. #
  31. # #include <lua/lua.h>
  32. #
  33. # This is because, the lua location is not standardized and may exist in
  34. # locations other than lua/
  35. #=============================================================================
  36. # Copyright 2007-2009 Kitware, Inc.
  37. # Copyright 2013 Rolf Eike Beer <[email protected]>
  38. #
  39. # Distributed under the OSI-approved BSD License (the "License");
  40. # see accompanying file Copyright.txt for details.
  41. #
  42. # This software is distributed WITHOUT ANY WARRANTY; without even the
  43. # implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
  44. # See the License for more information.
  45. #=============================================================================
  46. # (To distribute this file outside of CMake, substitute the full
  47. # License text for the above reference.)
  48. unset(_lua_include_subdirs)
  49. unset(_lua_library_names)
  50. unset(_lua_append_versions)
  51. # this is a function only to have all the variables inside go away automatically
  52. function(_lua_set_version_vars)
  53. set(LUA_VERSIONS5 5.3 5.2 5.1 5.0)
  54. if (Lua_FIND_VERSION_EXACT)
  55. if (Lua_FIND_VERSION_COUNT GREATER 1)
  56. set(_lua_append_versions ${Lua_FIND_VERSION_MAJOR}.${Lua_FIND_VERSION_MINOR})
  57. endif ()
  58. elseif (Lua_FIND_VERSION)
  59. # once there is a different major version supported this should become a loop
  60. if (NOT Lua_FIND_VERSION_MAJOR GREATER 5)
  61. if (Lua_FIND_VERSION_COUNT EQUAL 1)
  62. set(_lua_append_versions ${LUA_VERSIONS5})
  63. else ()
  64. foreach (subver IN LISTS LUA_VERSIONS5)
  65. if (NOT subver VERSION_LESS ${Lua_FIND_VERSION})
  66. list(APPEND _lua_append_versions ${subver})
  67. endif ()
  68. endforeach ()
  69. endif ()
  70. endif ()
  71. else ()
  72. # once there is a different major version supported this should become a loop
  73. set(_lua_append_versions ${LUA_VERSIONS5})
  74. endif ()
  75. list(APPEND _lua_include_subdirs "include/lua" "include")
  76. foreach (ver IN LISTS _lua_append_versions)
  77. string(REGEX MATCH "^([0-9]+)\\.([0-9]+)$" _ver "${ver}")
  78. list(APPEND _lua_include_subdirs
  79. include/lua${CMAKE_MATCH_1}${CMAKE_MATCH_2}
  80. include/lua${CMAKE_MATCH_1}.${CMAKE_MATCH_2}
  81. include/lua-${CMAKE_MATCH_1}.${CMAKE_MATCH_2}
  82. )
  83. list(APPEND _lua_library_names
  84. lua${CMAKE_MATCH_1}${CMAKE_MATCH_2}
  85. lua${CMAKE_MATCH_1}.${CMAKE_MATCH_2}
  86. lua-${CMAKE_MATCH_1}.${CMAKE_MATCH_2}
  87. lua.${CMAKE_MATCH_1}.${CMAKE_MATCH_2}
  88. )
  89. endforeach ()
  90. set(_lua_include_subdirs "${_lua_include_subdirs}" PARENT_SCOPE)
  91. set(_lua_library_names "${_lua_library_names}" PARENT_SCOPE)
  92. set(_lua_append_versions "${_lua_append_versions}" PARENT_SCOPE)
  93. endfunction(_lua_set_version_vars)
  94. function(_lua_check_header_version _hdr_file)
  95. # At least 5.[012] have different ways to express the version
  96. # so all of them need to be tested. Lua 5.2 defines LUA_VERSION
  97. # and LUA_RELEASE as joined by the C preprocessor, so avoid those.
  98. file(STRINGS "${_hdr_file}" lua_version_strings
  99. REGEX "^#define[ \t]+LUA_(RELEASE[ \t]+\"Lua [0-9]|VERSION([ \t]+\"Lua [0-9]|_[MR])).*")
  100. string(REGEX REPLACE ".*;#define[ \t]+LUA_VERSION_MAJOR[ \t]+\"([0-9])\"[ \t]*;.*" "\\1" LUA_VERSION_MAJOR ";${lua_version_strings};")
  101. if (LUA_VERSION_MAJOR MATCHES "^[0-9]+$")
  102. string(REGEX REPLACE ".*;#define[ \t]+LUA_VERSION_MINOR[ \t]+\"([0-9])\"[ \t]*;.*" "\\1" LUA_VERSION_MINOR ";${lua_version_strings};")
  103. string(REGEX REPLACE ".*;#define[ \t]+LUA_VERSION_RELEASE[ \t]+\"([0-9])\"[ \t]*;.*" "\\1" LUA_VERSION_PATCH ";${lua_version_strings};")
  104. set(LUA_VERSION_STRING "${LUA_VERSION_MAJOR}.${LUA_VERSION_MINOR}.${LUA_VERSION_PATCH}")
  105. else ()
  106. string(REGEX REPLACE ".*;#define[ \t]+LUA_RELEASE[ \t]+\"Lua ([0-9.]+)\"[ \t]*;.*" "\\1" LUA_VERSION_STRING ";${lua_version_strings};")
  107. if (NOT LUA_VERSION_STRING MATCHES "^[0-9.]+$")
  108. string(REGEX REPLACE ".*;#define[ \t]+LUA_VERSION[ \t]+\"Lua ([0-9.]+)\"[ \t]*;.*" "\\1" LUA_VERSION_STRING ";${lua_version_strings};")
  109. endif ()
  110. string(REGEX REPLACE "^([0-9]+)\\.[0-9.]*$" "\\1" LUA_VERSION_MAJOR "${LUA_VERSION_STRING}")
  111. string(REGEX REPLACE "^[0-9]+\\.([0-9]+)[0-9.]*$" "\\1" LUA_VERSION_MINOR "${LUA_VERSION_STRING}")
  112. string(REGEX REPLACE "^[0-9]+\\.[0-9]+\\.([0-9]).*" "\\1" LUA_VERSION_PATCH "${LUA_VERSION_STRING}")
  113. endif ()
  114. foreach (ver IN LISTS _lua_append_versions)
  115. if (ver STREQUAL "${LUA_VERSION_MAJOR}.${LUA_VERSION_MINOR}")
  116. set(LUA_VERSION_MAJOR ${LUA_VERSION_MAJOR} PARENT_SCOPE)
  117. set(LUA_VERSION_MINOR ${LUA_VERSION_MINOR} PARENT_SCOPE)
  118. set(LUA_VERSION_PATCH ${LUA_VERSION_PATCH} PARENT_SCOPE)
  119. set(LUA_VERSION_STRING ${LUA_VERSION_STRING} PARENT_SCOPE)
  120. return()
  121. endif ()
  122. endforeach ()
  123. endfunction(_lua_check_header_version)
  124. _lua_set_version_vars()
  125. if (LUA_INCLUDE_DIR AND EXISTS "${LUA_INCLUDE_DIR}/lua.h")
  126. _lua_check_header_version("${LUA_INCLUDE_DIR}/lua.h")
  127. endif ()
  128. if (NOT LUA_VERSION_STRING)
  129. foreach (subdir IN LISTS _lua_include_subdirs)
  130. unset(LUA_INCLUDE_PREFIX CACHE)
  131. find_path(LUA_INCLUDE_PREFIX ${subdir}/lua.h
  132. HINTS
  133. ENV LUA_DIR
  134. PATHS
  135. ~/Library/Frameworks
  136. /Library/Frameworks
  137. /sw # Fink
  138. /opt/local # DarwinPorts
  139. /opt/csw # Blastwave
  140. /opt
  141. )
  142. if (LUA_INCLUDE_PREFIX)
  143. _lua_check_header_version("${LUA_INCLUDE_PREFIX}/${subdir}/lua.h")
  144. if (LUA_VERSION_STRING)
  145. set(LUA_INCLUDE_DIR "${LUA_INCLUDE_PREFIX}/${subdir}")
  146. break()
  147. endif ()
  148. endif ()
  149. endforeach ()
  150. endif ()
  151. unset(_lua_include_subdirs)
  152. unset(_lua_append_versions)
  153. find_library(LUA_LIBRARY
  154. NAMES ${_lua_library_names} lua
  155. HINTS
  156. ENV LUA_DIR
  157. PATH_SUFFIXES lib
  158. PATHS
  159. ~/Library/Frameworks
  160. /Library/Frameworks
  161. /sw
  162. /opt/local
  163. /opt/csw
  164. /opt
  165. )
  166. unset(_lua_library_names)
  167. if (LUA_LIBRARY)
  168. # include the math library for Unix
  169. if (UNIX AND NOT APPLE AND NOT BEOS)
  170. find_library(LUA_MATH_LIBRARY m)
  171. set(LUA_LIBRARIES "${LUA_LIBRARY};${LUA_MATH_LIBRARY}")
  172. # For Windows and Mac, don't need to explicitly include the math library
  173. else ()
  174. set(LUA_LIBRARIES "${LUA_LIBRARY}")
  175. endif ()
  176. endif ()
  177. include(${CMAKE_CURRENT_LIST_DIR}/FindPackageHandleStandardArgs.cmake)
  178. # handle the QUIETLY and REQUIRED arguments and set LUA_FOUND to TRUE if
  179. # all listed variables are TRUE
  180. FIND_PACKAGE_HANDLE_STANDARD_ARGS(Lua
  181. REQUIRED_VARS LUA_LIBRARIES LUA_INCLUDE_DIR
  182. VERSION_VAR LUA_VERSION_STRING)
  183. mark_as_advanced(LUA_INCLUDE_DIR LUA_LIBRARY LUA_MATH_LIBRARY)