disable misc-non-private-member-variables-in-classes in .clang-tidy
[liba.git] / cmake / FindLua.cmake
blobf08041ae4afbfabf172dc19210aee1c6d6648102
1 #.rst:
2 # FindLua
3 # -------
5 # Find lua include dirs and libraries.
7 # Optional COMPONENTS
8 # ^^^^^^^^^^^^^^^^^^^
10 # ``lua`` and ``luac``
12 # Imported Targets
13 # ^^^^^^^^^^^^^^^^
15 # This module defines the following :ref:`Imported Targets <Imported Targets>`:
17 # ``lualib``
19 # Result Variables
20 # ^^^^^^^^^^^^^^^^
22 # This module defines the following variables:
24 # ``LUA_FOUND``
26 # ``LUA_INCLUDE_DIR``
28 # ``LUA_LIBRARIES``
30 # ``LUA_VERSION``
32 # ``LUA_VERSION_MAJOR``
34 # ``LUA_VERSION_MINOR``
36 # ``LUA_VERSION_PATCH``
38 # ``Lua_lua_FOUND``
40 # ``LUA_EXECUTABLE``
42 # ``Lua_luac_FOUND``
44 # ``LUAC_EXECUTABLE``
46 # Cache variables
47 # ^^^^^^^^^^^^^^^
49 # Search results are saved persistently in CMake cache entries:
51 # ``LUA_INCLUDE_DIR``
53 # ``LUA_LIBRARY``
55 # ``LUA_MATH_LIBRARY``
57 # ``LUA_EXECUTABLE``
59 # ``LUAC_EXECUTABLE``
61 include(${CMAKE_ROOT}/Modules/FindPackageHandleStandardArgs.cmake)
62 list(APPEND LUA_REQUIRED_VARS LUA_LIBRARIES LUA_INCLUDE_DIR)
64 foreach(VERSION ${Lua_FIND_VERSION} 5.4 5.3 5.2 5.1 5.0)
65   string(REGEX MATCH "^([0-9]+)\\.([0-9]+)" VERSION "${VERSION}")
66   find_path(LUA_INCLUDE_DIR lua.h PATH_SUFFIXES
67     include/lua${CMAKE_MATCH_1}${CMAKE_MATCH_2}
68     include/lua${CMAKE_MATCH_1}.${CMAKE_MATCH_2}
69     include/lua-${CMAKE_MATCH_1}.${CMAKE_MATCH_2}
70     include HINTS ENV LUA_DIR
71   )
72   if(LUA_INCLUDE_DIR)
73     break()
74   endif()
75 endforeach()
76 mark_as_advanced(LUA_INCLUDE_DIR)
78 if(EXISTS "${LUA_INCLUDE_DIR}/lua.h")
79   file(STRINGS ${LUA_INCLUDE_DIR}/lua.h VERSION REGEX "^#define[ \t]+LUA_(RELEASE[ \t]+\"Lua [0-9]|VERSION([ \t]+\"Lua [0-9]|_[MR])).*")
80   string(REGEX REPLACE ".*;#define[ \t]+LUA_VERSION_MAJOR[^0-9]+([0-9]+)[^;]*;.*" "\\1" LUA_VERSION_MAJOR ";${VERSION};")
81   if(LUA_VERSION_MAJOR MATCHES "^[0-9]+$")
82     string(REGEX REPLACE ".*;#define[ \t]+LUA_VERSION_MINOR[^0-9]+([0-9]+)[^;]*;.*" "\\1" LUA_VERSION_MINOR ";${VERSION};")
83     string(REGEX REPLACE ".*;#define[ \t]+LUA_VERSION_RELEASE[^0-9(]+([0-9]+)[^;]*;.*" "\\1" LUA_VERSION_PATCH ";${VERSION};")
84     set(LUA_VERSION "${LUA_VERSION_MAJOR}.${LUA_VERSION_MINOR}.${LUA_VERSION_PATCH}")
85   else()
86     string(REGEX REPLACE ".*;#define[ \t]+LUA_RELEASE[ \t]+\"Lua ([0-9.]+)\"[ \t]*;.*" "\\1" LUA_VERSION ";${VERSION};")
87     if(NOT LUA_VERSION MATCHES "^[0-9.]+$")
88       string(REGEX REPLACE ".*;#define[ \t]+LUA_VERSION[ \t]+\"Lua ([0-9.]+)\"[ \t]*;.*" "\\1" LUA_VERSION ";${VERSION};")
89     endif()
90     string(REGEX REPLACE "^[0-9]+\\.[0-9]+\\.([0-9]).*" "\\1" LUA_VERSION_PATCH "${LUA_VERSION}")
91     string(REGEX REPLACE "^[0-9]+\\.([0-9]).*" "\\1" LUA_VERSION_MINOR "${LUA_VERSION}")
92     string(REGEX REPLACE "^([0-9]).*" "\\1" LUA_VERSION_MAJOR "${LUA_VERSION}")
93   endif()
94 endif()
96 if(NOT DEFINED ENV{LUA_DIR} AND EXISTS "${LUA_INCLUDE_DIR}")
97   get_filename_component(LUA_DIR "${LUA_INCLUDE_DIR}" DIRECTORY)
98   get_filename_component(modules "${LUA_INCLUDE_DIR}" NAME)
99   if(NOT modules STREQUAL "include")
100     get_filename_component(LUA_DIR "${LUA_DIR}" DIRECTORY)
101   endif()
102 elseif(DEFINED ENV{LUA_DIR})
103   file(TO_CMAKE_PATH "$ENV{LUA_DIR}" LUA_DIR)
104 endif()
106 string(REGEX MATCHALL "(luac|lua)" modules "${Lua_FIND_COMPONENTS}")
107 if(modules)
108   list(REMOVE_DUPLICATES modules)
109 endif()
110 foreach(module ${modules})
111   string(TOUPPER ${module} MODULE)
112   find_program(${MODULE}_EXECUTABLE NAMES
113     ${module}${LUA_VERSION_MAJOR}${LUA_VERSION_MINOR}
114     ${module}${LUA_VERSION_MAJOR}.${LUA_VERSION_MINOR}
115     ${module}-${LUA_VERSION_MAJOR}.${LUA_VERSION_MINOR}
116     ${module} HINTS ${LUA_DIR} PATH_SUFFIXES bin
117   )
118   mark_as_advanced(${MODULE}_EXECUTABLE)
119   if(Lua_FIND_REQUIRED_${module})
120     list(APPEND LUA_REQUIRED_VARS ${MODULE}_EXECUTABLE)
121   endif()
122   if(EXISTS ${${MODULE}_EXECUTABLE})
123     set(Lua_${module}_FOUND 1)
124   endif()
125   set(MODULE)
126 endforeach()
127 set(modules)
129 if(NOT LUA_VERSION)
130   if(EXISTS "${LUA_EXECUTABLE}")
131     execute_process(COMMAND ${LUA_EXECUTABLE} -v OUTPUT_VARIABLE LUA_VERSION ERROR_VARIABLE LUA_VERSION)
132     get_filename_component(LUA_DIR "${LUA_EXECUTABLE}" DIRECTORY)
133   elseif(EXISTS "${LUAC_EXECUTABLE}")
134     execute_process(COMMAND ${LUAC_EXECUTABLE} -v OUTPUT_VARIABLE LUA_VERSION ERROR_VARIABLE LUA_VERSION)
135     get_filename_component(LUA_DIR "${LUAC_EXECUTABLE}" DIRECTORY)
136   endif()
137   if(LUA_VERSION)
138     get_filename_component(LUA_DIR "${LUA_DIR}" DIRECTORY)
139     find_path(LUA_INCLUDE_DIR lua.h HINTS ${LUA_DIR} PATH_SUFFIXES include)
140     string(REGEX REPLACE "Lua[ ]+([^ ]+).*" "\\1" LUA_VERSION "${LUA_VERSION}")
141     string(REGEX REPLACE "^[0-9]+\\.[0-9]+\\.([0-9]).*" "\\1" LUA_VERSION_PATCH "${LUA_VERSION}")
142     string(REGEX REPLACE "^[0-9]+\\.([0-9]).*" "\\1" LUA_VERSION_MINOR "${LUA_VERSION}")
143     string(REGEX REPLACE "^([0-9]).*" "\\1" LUA_VERSION_MAJOR "${LUA_VERSION}")
144   endif()
145 endif()
147 find_library(LUA_LIBRARY NAMES_PER_DIR NAMES
148   lua${LUA_VERSION_MAJOR}${LUA_VERSION_MINOR}
149   lua${LUA_VERSION_MAJOR}.${LUA_VERSION_MINOR}
150   lua-${LUA_VERSION_MAJOR}.${LUA_VERSION_MINOR}
151   lua HINTS ${LUA_DIR} PATH_SUFFIXES lib
153 mark_as_advanced(LUA_LIBRARY)
155 if(EXISTS "${LUA_LIBRARY}")
156   list(APPEND LUA_LIBRARIES ${LUA_LIBRARY})
157   if(UNIX AND NOT APPLE AND NOT BEOS)
158     find_library(LUA_MATH_LIBRARY m)
159     mark_as_advanced(LUA_MATH_LIBRARY)
160     if(NOT EXISTS "${LUA_MATH_LIBRARY}")
161       list(APPEND LUA_LIBRARIES m)
162     else()
163       list(APPEND LUA_LIBRARIES ${LUA_MATH_LIBRARY})
164     endif()
165     get_filename_component(ext ${LUA_LIBRARY} EXT)
166     if(CMAKE_STATIC_LIBRARY_SUFFIX STREQUAL ext)
167       list(APPEND LUA_LIBRARIES ${CMAKE_DL_LIBS})
168     endif()
169     set(ext)
170   endif()
171 endif()
173 find_package_handle_standard_args(Lua
174   FOUND_VAR
175     LUA_FOUND
176   REQUIRED_VARS
177     ${LUA_REQUIRED_VARS}
178   VERSION_VAR
179     LUA_VERSION
180   HANDLE_COMPONENTS
183 if(LUA_FOUND)
184   if(NOT TARGET lualib AND EXISTS "${LUA_LIBRARY}")
185     add_library(lualib UNKNOWN IMPORTED)
186     set_target_properties(lualib PROPERTIES
187       INTERFACE_INCLUDE_DIRECTORIES "${LUA_INCLUDE_DIR}"
188       IMPORTED_LINK_INTERFACE_LANGUAGES C
189       IMPORTED_LOCATION "${LUA_LIBRARY}"
190     )
191     set(libraries ${LUA_LIBRARIES})
192     list(REMOVE_ITEM libraries ${LUA_LIBRARY})
193     set_property(TARGET lualib APPEND PROPERTY
194       INTERFACE_LINK_LIBRARIES ${libraries}
195     )
196     set(libraries)
197   endif()
198 endif()