FindLua.cmake 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186
  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. foreach (ver IN LISTS _lua_append_versions)
  76. string(REGEX MATCH "^([0-9]+)\\.([0-9]+)$" _ver "${ver}")
  77. list(APPEND _lua_include_subdirs
  78. include/lua${CMAKE_MATCH_1}${CMAKE_MATCH_2}
  79. include/lua${CMAKE_MATCH_1}.${CMAKE_MATCH_2}
  80. include/lua-${CMAKE_MATCH_1}.${CMAKE_MATCH_2}
  81. )
  82. list(APPEND _lua_library_names
  83. lua${CMAKE_MATCH_1}${CMAKE_MATCH_2}
  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. )
  88. endforeach ()
  89. set(_lua_include_subdirs "${_lua_include_subdirs}" PARENT_SCOPE)
  90. set(_lua_library_names "${_lua_library_names}" PARENT_SCOPE)
  91. set(_lua_append_versions "${_lua_append_versions}" PARENT_SCOPE)
  92. endfunction(_lua_set_version_vars)
  93. function(_lua_check_header_version _hdr_file)
  94. # At least 5.[012] have different ways to express the version
  95. # so all of them need to be tested. Lua 5.2 defines LUA_VERSION
  96. # and LUA_RELEASE as joined by the C preprocessor, so avoid those.
  97. file(STRINGS "${_hdr_file}" lua_version_strings
  98. REGEX "^#define[ \t]+LUA_(RELEASE[ \t]+\"Lua [0-9]|VERSION([ \t]+\"Lua [0-9]|_[MR])).*")
  99. string(REGEX REPLACE ".*;#define[ \t]+LUA_VERSION_MAJOR[ \t]+\"([0-9])\"[ \t]*;.*" "\\1" LUA_VERSION_MAJOR ";${lua_version_strings};")
  100. if (LUA_VERSION_MAJOR MATCHES "^[0-9]+$")
  101. string(REGEX REPLACE ".*;#define[ \t]+LUA_VERSION_MINOR[ \t]+\"([0-9])\"[ \t]*;.*" "\\1" LUA_VERSION_MINOR ";${lua_version_strings};")
  102. string(REGEX REPLACE ".*;#define[ \t]+LUA_VERSION_RELEASE[ \t]+\"([0-9])\"[ \t]*;.*" "\\1" LUA_VERSION_PATCH ";${lua_version_strings};")
  103. set(LUA_VERSION_STRING "${LUA_VERSION_MAJOR}.${LUA_VERSION_MINOR}.${LUA_VERSION_PATCH}")
  104. else ()
  105. string(REGEX REPLACE ".*;#define[ \t]+LUA_RELEASE[ \t]+\"Lua ([0-9.]+)\"[ \t]*;.*" "\\1" LUA_VERSION_STRING ";${lua_version_strings};")
  106. if (NOT LUA_VERSION_STRING MATCHES "^[0-9.]+$")
  107. string(REGEX REPLACE ".*;#define[ \t]+LUA_VERSION[ \t]+\"Lua ([0-9.]+)\"[ \t]*;.*" "\\1" LUA_VERSION_STRING ";${lua_version_strings};")
  108. endif ()
  109. string(REGEX REPLACE "^([0-9]+)\\.[0-9.]*$" "\\1" LUA_VERSION_MAJOR "${LUA_VERSION_STRING}")
  110. string(REGEX REPLACE "^[0-9]+\\.([0-9]+)[0-9.]*$" "\\1" LUA_VERSION_MINOR "${LUA_VERSION_STRING}")
  111. string(REGEX REPLACE "^[0-9]+\\.[0-9]+\\.([0-9]).*" "\\1" LUA_VERSION_PATCH "${LUA_VERSION_STRING}")
  112. endif ()
  113. foreach (ver IN LISTS _lua_append_versions)
  114. if (ver STREQUAL "${LUA_VERSION_MAJOR}.${LUA_VERSION_MINOR}")
  115. set(LUA_VERSION_MAJOR ${LUA_VERSION_MAJOR} PARENT_SCOPE)
  116. set(LUA_VERSION_MINOR ${LUA_VERSION_MINOR} PARENT_SCOPE)
  117. set(LUA_VERSION_PATCH ${LUA_VERSION_PATCH} PARENT_SCOPE)
  118. set(LUA_VERSION_STRING ${LUA_VERSION_STRING} PARENT_SCOPE)
  119. return()
  120. endif ()
  121. endforeach ()
  122. endfunction(_lua_check_header_version)
  123. _lua_set_version_vars()
  124. find_path(LUA_INCLUDE_DIR lua.h
  125. HINTS
  126. ENV LUA_DIR
  127. PATH_SUFFIXES ${_lua_include_subdirs} include/lua include
  128. PATHS
  129. ~/Library/Frameworks
  130. /Library/Frameworks
  131. /sw # Fink
  132. /opt/local # DarwinPorts
  133. /opt/csw # Blastwave
  134. /opt
  135. )
  136. unset(_lua_include_subdirs)
  137. unset(_lua_append_versions)
  138. find_library(LUA_LIBRARY
  139. NAMES ${_lua_library_names} lua
  140. HINTS
  141. ENV LUA_DIR
  142. PATH_SUFFIXES lib
  143. PATHS
  144. ~/Library/Frameworks
  145. /Library/Frameworks
  146. /sw
  147. /opt/local
  148. /opt/csw
  149. /opt
  150. )
  151. unset(_lua_library_names)
  152. if (LUA_LIBRARY)
  153. # include the math library for Unix
  154. if (UNIX AND NOT APPLE AND NOT BEOS)
  155. find_library(LUA_MATH_LIBRARY m)
  156. set(LUA_LIBRARIES "${LUA_LIBRARY};${LUA_MATH_LIBRARY}")
  157. # For Windows and Mac, don't need to explicitly include the math library
  158. else ()
  159. set(LUA_LIBRARIES "${LUA_LIBRARY}")
  160. endif ()
  161. endif ()
  162. if (LUA_INCLUDE_DIR AND EXISTS "${LUA_INCLUDE_DIR}/lua.h")
  163. _lua_check_header_version("${LUA_INCLUDE_DIR}/lua.h")
  164. endif ()
  165. include(${CMAKE_CURRENT_LIST_DIR}/FindPackageHandleStandardArgs.cmake)
  166. # handle the QUIETLY and REQUIRED arguments and set LUA_FOUND to TRUE if
  167. # all listed variables are TRUE
  168. FIND_PACKAGE_HANDLE_STANDARD_ARGS(Lua
  169. REQUIRED_VARS LUA_LIBRARIES LUA_INCLUDE_DIR
  170. VERSION_VAR LUA_VERSION_STRING)
  171. mark_as_advanced(LUA_INCLUDE_DIR LUA_LIBRARY LUA_MATH_LIBRARY)