FindLua.cmake 6.0 KB

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