FindLua.cmake 6.5 KB

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