FindLua.cmake 7.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190
  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. # Redistribution and use in source and binary forms, with or without
  40. # modification, are permitted provided that the following conditions
  41. # are met:
  42. #
  43. # * Redistributions of source code must retain the above copyright
  44. # notice, this list of conditions and the following disclaimer.
  45. #
  46. # * Redistributions in binary form must reproduce the above copyright
  47. # notice, this list of conditions and the following disclaimer in the
  48. # documentation and/or other materials provided with the distribution.
  49. #
  50. # * Neither the names of Kitware, Inc., the Insight Software Consortium,
  51. # nor the names of their contributors may be used to endorse or promote
  52. # products derived from this software without specific prior written
  53. # permission.
  54. #
  55. # THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
  56. # "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
  57. # LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
  58. # A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
  59. # HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
  60. # SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
  61. # LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
  62. # DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
  63. # THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  64. # (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
  65. # OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  66. #===========================================================================
  67. unset(_lua_include_subdirs)
  68. unset(_lua_library_names)
  69. # this is a function only to have all the variables inside go away automatically
  70. function(set_lua_version_vars)
  71. set(LUA_VERSIONS5 5.3 5.2 5.1 5.0)
  72. if (Lua_FIND_VERSION_EXACT)
  73. if (Lua_FIND_VERSION_COUNT GREATER 1)
  74. set(lua_append_versions ${Lua_FIND_VERSION_MAJOR} ${Lua_FIND_VERSION_MINOR})
  75. endif ()
  76. elseif (Lua_FIND_VERSION)
  77. # once there is a different major version supported this should become a loop
  78. if (NOT Lua_FIND_VERSION_MAJOR GREATER 5)
  79. if (Lua_FIND_VERSION_COUNT EQUAL 1)
  80. set(lua_append_versions ${LUA_VERSIONS5})
  81. else ()
  82. foreach (subver IN LISTS LUA_VERSIONS5)
  83. if (NOT subver VERSION_LESS ${Lua_FIND_VERSION})
  84. list(APPEND lua_append_versions ${subver})
  85. endif ()
  86. endforeach ()
  87. endif ()
  88. endif ()
  89. else ()
  90. # once there is a different major version supported this should become a loop
  91. set(lua_append_versions ${LUA_VERSIONS5})
  92. endif ()
  93. foreach (ver IN LISTS lua_append_versions)
  94. string(REGEX MATCH "^([0-9]+)\\.([0-9]+)$" _ver "${ver}")
  95. list(APPEND _lua_include_subdirs
  96. include/lua${CMAKE_MATCH_1}${CMAKE_MATCH_2}
  97. include/lua${CMAKE_MATCH_1}.${CMAKE_MATCH_2}
  98. include/lua-${CMAKE_MATCH_1}.${CMAKE_MATCH_2}
  99. )
  100. list(APPEND _lua_library_names
  101. lua${CMAKE_MATCH_1}${CMAKE_MATCH_2}
  102. lua${CMAKE_MATCH_1}.${CMAKE_MATCH_2}
  103. lua-${CMAKE_MATCH_1}.${CMAKE_MATCH_2}
  104. )
  105. endforeach ()
  106. set(_lua_include_subdirs "${_lua_include_subdirs}" PARENT_SCOPE)
  107. set(_lua_library_names "${_lua_library_names}" PARENT_SCOPE)
  108. endfunction(set_lua_version_vars)
  109. set_lua_version_vars()
  110. find_path(LUA_INCLUDE_DIR lua.h
  111. HINTS
  112. ENV LUA_DIR
  113. PATH_SUFFIXES ${_lua_include_subdirs} include/lua include
  114. PATHS
  115. ~/Library/Frameworks
  116. /Library/Frameworks
  117. /sw # Fink
  118. /opt/local # DarwinPorts
  119. /opt/csw # Blastwave
  120. /opt
  121. )
  122. unset(_lua_include_subdirs)
  123. find_library(LUA_LIBRARY
  124. NAMES ${_lua_library_names} lua
  125. HINTS
  126. ENV LUA_DIR
  127. PATH_SUFFIXES lib
  128. PATHS
  129. ~/Library/Frameworks
  130. /Library/Frameworks
  131. /sw
  132. /opt/local
  133. /opt/csw
  134. /opt
  135. )
  136. unset(_lua_library_names)
  137. if (LUA_LIBRARY)
  138. # include the math library for Unix
  139. if (UNIX AND NOT APPLE AND NOT BEOS)
  140. find_library(LUA_MATH_LIBRARY m)
  141. set(LUA_LIBRARIES "${LUA_LIBRARY};${LUA_MATH_LIBRARY}")
  142. # For Windows and Mac, don't need to explicitly include the math library
  143. else ()
  144. set(LUA_LIBRARIES "${LUA_LIBRARY}")
  145. endif ()
  146. endif ()
  147. if (LUA_INCLUDE_DIR AND EXISTS "${LUA_INCLUDE_DIR}/lua.h")
  148. # At least 5.[012] have different ways to express the version
  149. # so all of them need to be tested. Lua 5.2 defines LUA_VERSION
  150. # and LUA_RELEASE as joined by the C preprocessor, so avoid those.
  151. file(STRINGS "${LUA_INCLUDE_DIR}/lua.h" lua_version_strings
  152. REGEX "^#define[ \t]+LUA_(RELEASE[ \t]+\"Lua [0-9]|VERSION([ \t]+\"Lua [0-9]|_[MR])).*")
  153. string(REGEX REPLACE ".*;#define[ \t]+LUA_VERSION_MAJOR[ \t]+\"([0-9])\"[ \t]*;.*" "\\1" LUA_VERSION_MAJOR ";${lua_version_strings};")
  154. if (LUA_VERSION_MAJOR MATCHES "^[0-9]+$")
  155. string(REGEX REPLACE ".*;#define[ \t]+LUA_VERSION_MINOR[ \t]+\"([0-9])\"[ \t]*;.*" "\\1" LUA_VERSION_MINOR ";${lua_version_strings};")
  156. string(REGEX REPLACE ".*;#define[ \t]+LUA_VERSION_RELEASE[ \t]+\"([0-9])\"[ \t]*;.*" "\\1" LUA_VERSION_PATCH ";${lua_version_strings};")
  157. set(LUA_VERSION_STRING "${LUA_VERSION_MAJOR}.${LUA_VERSION_MINOR}.${LUA_VERSION_PATCH}")
  158. else ()
  159. string(REGEX REPLACE ".*;#define[ \t]+LUA_RELEASE[ \t]+\"Lua ([0-9.]+)\"[ \t]*;.*" "\\1" LUA_VERSION_STRING ";${lua_version_strings};")
  160. if (NOT LUA_VERSION_STRING MATCHES "^[0-9.]+$")
  161. string(REGEX REPLACE ".*;#define[ \t]+LUA_VERSION[ \t]+\"Lua ([0-9.]+)\"[ \t]*;.*" "\\1" LUA_VERSION_STRING ";${lua_version_strings};")
  162. endif ()
  163. string(REGEX REPLACE "^([0-9]+)\\.[0-9.]*$" "\\1" LUA_VERSION_MAJOR "${LUA_VERSION_STRING}")
  164. string(REGEX REPLACE "^[0-9]+\\.([0-9]+)[0-9.]*$" "\\1" LUA_VERSION_MINOR "${LUA_VERSION_STRING}")
  165. string(REGEX REPLACE "^[0-9]+\\.[0-9]+\\.([0-9]).*" "\\1" LUA_VERSION_PATCH "${LUA_VERSION_STRING}")
  166. endif ()
  167. unset(lua_version_strings)
  168. endif()
  169. include(FindPackageHandleStandardArgs)
  170. # handle the QUIETLY and REQUIRED arguments and set LUA_FOUND to TRUE if
  171. # all listed variables are TRUE
  172. FIND_PACKAGE_HANDLE_STANDARD_ARGS(Lua
  173. REQUIRED_VARS LUA_LIBRARIES LUA_INCLUDE_DIR
  174. VERSION_VAR LUA_VERSION_STRING)
  175. mark_as_advanced(LUA_INCLUDE_DIR LUA_LIBRARY LUA_MATH_LIBRARY)