FindLua51.cmake 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161
  1. # Distributed under the OSI-approved BSD 3-Clause License. See accompanying
  2. # file LICENSE.rst or https://cmake.org/licensing for details.
  3. #[=======================================================================[.rst:
  4. FindLua51
  5. ---------
  6. .. note::
  7. This module is intended specifically for Lua version branch 5.1, which is
  8. obsolete and not maintained anymore. In new code use the latest supported
  9. Lua version and the version-agnostic module :module:`FindLua` instead.
  10. Finds the Lua library:
  11. .. code-block:: cmake
  12. find_package(Lua51 [<version>] [...])
  13. Lua is a embeddable scripting language.
  14. When working with Lua, its library headers are intended to be included in
  15. project source code as:
  16. .. code-block:: c
  17. #include <lua.h>
  18. and not:
  19. .. code-block:: c
  20. #include <lua/lua.h>
  21. This is because, the location of Lua headers may differ across platforms and may
  22. exist in locations other than ``lua/``.
  23. Result Variables
  24. ^^^^^^^^^^^^^^^^
  25. This module defines the following variables:
  26. ``Lua51_FOUND``
  27. .. versionadded:: 3.3
  28. Boolean indicating whether (the requested version of) Lua was found.
  29. ``Lua51_VERSION``
  30. .. versionadded:: 4.2
  31. The version of Lua 5.1 found.
  32. Cache Variables
  33. ^^^^^^^^^^^^^^^
  34. The following cache variables may also be set:
  35. ``LUA_INCLUDE_DIR``
  36. The directory containing the Lua header files, such as ``lua.h``,
  37. ``lualib.h``, and ``lauxlib.h``, needed to use Lua.
  38. ``LUA_LIBRARIES``
  39. Libraries needed to link against to use Lua.
  40. Deprecated Variables
  41. ^^^^^^^^^^^^^^^^^^^^
  42. The following variables are provided for backward compatibility:
  43. ``LUA51_FOUND``
  44. .. deprecated:: 4.2
  45. Use ``Lua51_FOUND``, which has the same value.
  46. Boolean indicating whether (the requested version of) Lua was found.
  47. ``LUA_VERSION_STRING``
  48. .. deprecated:: 4.2
  49. Use ``Lua51_VERSION``, which has the same value.
  50. The version of Lua 5.1 found.
  51. Examples
  52. ^^^^^^^^
  53. Finding the Lua 5.1 library and creating an interface :ref:`imported target
  54. <Imported Targets>` that encapsulates its usage requirements for linking to a
  55. project target:
  56. .. code-block:: cmake
  57. find_package(Lua51)
  58. if(Lua51_FOUND AND NOT TARGET Lua51::Lua51)
  59. add_library(Lua51::Lua51 INTERFACE IMPORTED)
  60. set_target_properties(
  61. Lua51::Lua51
  62. PROPERTIES
  63. INTERFACE_INCLUDE_DIRECTORIES "${LUA_INCLUDE_DIR}"
  64. INTERFACE_LINK_LIBRARIES "${LUA_LIBRARIES}"
  65. )
  66. endif()
  67. target_link_libraries(project_target PRIVATE Lua51::Lua51)
  68. See Also
  69. ^^^^^^^^
  70. * The :module:`FindLua` module to find Lua in version-agnostic way.
  71. #]=======================================================================]
  72. cmake_policy(PUSH)
  73. cmake_policy(SET CMP0159 NEW) # file(STRINGS) with REGEX updates CMAKE_MATCH_<n>
  74. find_path(LUA_INCLUDE_DIR lua.h
  75. HINTS
  76. ENV LUA_DIR
  77. PATH_SUFFIXES lua51 lua5.1 lua-5.1 lua
  78. PATHS
  79. ~/Library/Frameworks
  80. /Library/Frameworks
  81. /opt
  82. )
  83. find_library(LUA_LIBRARY
  84. NAMES lua51 lua5.1 lua-5.1 lua
  85. HINTS
  86. ENV LUA_DIR
  87. PATH_SUFFIXES lib
  88. PATHS
  89. ~/Library/Frameworks
  90. /Library/Frameworks
  91. /opt
  92. )
  93. if(LUA_LIBRARY)
  94. # include the math library for Unix
  95. if(UNIX AND NOT APPLE AND NOT BEOS AND NOT HAIKU)
  96. find_library(LUA_MATH_LIBRARY m)
  97. set( LUA_LIBRARIES "${LUA_LIBRARY};${LUA_MATH_LIBRARY}" CACHE STRING "Lua Libraries")
  98. # For Windows and Mac, don't need to explicitly include the math library
  99. else()
  100. set( LUA_LIBRARIES "${LUA_LIBRARY}" CACHE STRING "Lua Libraries")
  101. endif()
  102. endif()
  103. if(LUA_INCLUDE_DIR AND EXISTS "${LUA_INCLUDE_DIR}/lua.h")
  104. file(STRINGS "${LUA_INCLUDE_DIR}/lua.h" lua_version_str REGEX "^#define[ \t]+LUA_RELEASE[ \t]+\"Lua .+\"")
  105. string(REGEX REPLACE "^#define[ \t]+LUA_RELEASE[ \t]+\"Lua ([^\"]+)\".*" "\\1" Lua51_VERSION "${lua_version_str}")
  106. set(LUA_VERSION_STRING "${Lua51_VERSION}")
  107. unset(lua_version_str)
  108. endif()
  109. include(FindPackageHandleStandardArgs)
  110. find_package_handle_standard_args(Lua51
  111. REQUIRED_VARS LUA_LIBRARIES LUA_INCLUDE_DIR
  112. VERSION_VAR Lua51_VERSION)
  113. mark_as_advanced(LUA_INCLUDE_DIR LUA_LIBRARIES LUA_LIBRARY LUA_MATH_LIBRARY)
  114. cmake_policy(POP)