change code style in src/math.c
[liba.git] / cmake / FindLuaJIT.cmake
blob6002ac1afce3f8b4d3ff774b92e55d665908c5d6
1 #.rst:
2 # FindLuaJIT
3 # ----------
5 # Find luajit include dirs and libraries.
7 # Optional COMPONENTS
8 # ^^^^^^^^^^^^^^^^^^^
10 # ``luajit``
12 # Imported Targets
13 # ^^^^^^^^^^^^^^^^
15 # This module defines the following :ref:`Imported Targets <Imported Targets>`:
17 # ``luajit``
19 # Result Variables
20 # ^^^^^^^^^^^^^^^^
22 # This module defines the following variables:
24 # ``LUAJIT_FOUND``
26 # ``LUAJIT_INCLUDE_DIR``
28 # ``LUAJIT_LIBRARIES``
30 # ``LUAJIT_VERSION``
32 # ``LUAJIT_VERSION_MAJOR``
34 # ``LUAJIT_VERSION_MINOR``
36 # ``LUAJIT_VERSION_PATCH``
38 # ``LuaJIT_luajit_FOUND``
40 # ``LUAJIT_EXECUTABLE``
42 # Cache variables
43 # ^^^^^^^^^^^^^^^
45 # Search results are saved persistently in CMake cache entries:
47 # ``LUAJIT_INCLUDE_DIR``
49 # ``LUAJIT_LIBRARY``
51 # ``LUA_MATH_LIBRARY``
53 # ``LUAJIT_EXECUTABLE``
55 include(${CMAKE_ROOT}/Modules/FindPackageHandleStandardArgs.cmake)
56 list(APPEND LUAJIT_REQUIRED_VARS LUAJIT_LIBRARIES LUAJIT_INCLUDE_DIR)
58 foreach(VERSION ${LuaJIT_FIND_VERSION} 2.1 2.0)
59   string(REGEX MATCH "^([0-9]+)\\.([0-9]+)" VERSION "${VERSION}")
60   find_path(LUAJIT_INCLUDE_DIR luajit.h PATH_SUFFIXES include
61     include/luajit${CMAKE_MATCH_1}${CMAKE_MATCH_2}
62     include/luajit${CMAKE_MATCH_1}.${CMAKE_MATCH_2}
63     include/luajit-${CMAKE_MATCH_1}.${CMAKE_MATCH_2}
64   )
65   if(LUAJIT_INCLUDE_DIR)
66     break()
67   endif()
68 endforeach()
69 mark_as_advanced(LUAJIT_INCLUDE_DIR)
71 if(EXISTS "${LUAJIT_INCLUDE_DIR}/luajit.h")
72   file(STRINGS ${LUAJIT_INCLUDE_DIR}/luajit.h LUAJIT_VERSION REGEX "^#define[ \t]+LUAJIT_VERSION[ \t]+.*")
73   string(REGEX REPLACE ".*LUAJIT_VERSION[^0-9]+([^\"]+).*" "\\1" LUAJIT_VERSION ${LUAJIT_VERSION})
74   string(REGEX MATCHALL "[0-9]+" VERSION "${LUAJIT_VERSION}.0.0.0")
75   list(GET VERSION 0 LUAJIT_VERSION_MAJOR)
76   list(GET VERSION 1 LUAJIT_VERSION_MINOR)
77   list(GET VERSION 2 LUAJIT_VERSION_PATCH)
78 endif()
80 if(EXISTS "${LUAJIT_INCLUDE_DIR}/lua.h")
81   file(STRINGS ${LUAJIT_INCLUDE_DIR}/lua.h VERSION REGEX "^#define[ \t]+LUA_(RELEASE[ \t]+\"Lua [0-9]|VERSION([ \t]+\"Lua [0-9]|_[MR])).*")
82   string(REGEX REPLACE ".*;#define[ \t]+LUA_VERSION_MAJOR[^0-9]+([0-9]+)[^;]*;.*" "\\1" LUA_VERSION_MAJOR ";${VERSION};")
83   if(LUA_VERSION_MAJOR MATCHES "^[0-9]+$")
84     string(REGEX REPLACE ".*;#define[ \t]+LUA_VERSION_MINOR[^0-9]+([0-9]+)[^;]*;.*" "\\1" LUA_VERSION_MINOR ";${VERSION};")
85     string(REGEX REPLACE ".*;#define[ \t]+LUA_VERSION_RELEASE[^0-9(]+([0-9]+)[^;]*;.*" "\\1" LUA_VERSION_PATCH ";${VERSION};")
86     set(LUA_VERSION "${LUA_VERSION_MAJOR}.${LUA_VERSION_MINOR}.${LUA_VERSION_PATCH}")
87   else()
88     string(REGEX REPLACE ".*;#define[ \t]+LUA_RELEASE[ \t]+\"Lua ([0-9.]+)\"[ \t]*;.*" "\\1" LUA_VERSION ";${VERSION};")
89     if(NOT LUA_VERSION MATCHES "^[0-9.]+$")
90       string(REGEX REPLACE ".*;#define[ \t]+LUA_VERSION[ \t]+\"Lua ([0-9.]+)\"[ \t]*;.*" "\\1" LUA_VERSION ";${VERSION};")
91     endif()
92     string(REGEX MATCHALL "[0-9]+" VERSION "${LUA_VERSION}.0.0.0")
93     list(GET VERSION 0 LUA_VERSION_MAJOR)
94     list(GET VERSION 1 LUA_VERSION_MINOR)
95     list(GET VERSION 2 LUA_VERSION_PATCH)
96   endif()
97 endif()
99 list(FIND LuaJIT_FIND_COMPONENTS "luajit" found)
100 if(${found} GREATER -1)
101   find_program(LUAJIT_EXECUTABLE NAMES luajit-${LUAJIT_VERSION} luajit)
102   mark_as_advanced(LUAJIT_EXECUTABLE)
103 endif()
104 if(LuaJIT_FIND_REQUIRED_luajit)
105   list(APPEND LUAJIT_REQUIRED_VARS LUAJIT_EXECUTABLE)
106 endif()
107 if(EXISTS "${LUAJIT_EXECUTABLE}")
108   set(LuaJIT_luajit_FOUND 1)
109 endif()
111 if(NOT LUAJIT_VERSION AND EXISTS "${LUAJIT_EXECUTABLE}")
112   get_filename_component(LUAJIT_DIR "${LUAJIT_EXECUTABLE}" DIRECTORY)
113   get_filename_component(LUAJIT_DIR "${LUAJIT_DIR}" DIRECTORY)
114   execute_process(COMMAND ${LUAJIT_EXECUTABLE} -v OUTPUT_VARIABLE LUAJIT_VERSION)
115   find_path(LUAJIT_INCLUDE_DIR luajit.h HINTS ${LUAJIT_DIR} PATH_SUFFIXES include)
116   string(REGEX REPLACE "LuaJIT[ ]+([^\n ]+).*" "\\1" LUAJIT_VERSION "${LUAJIT_VERSION}")
117   string(REGEX MATCHALL "[0-9]+" VERSION "${LUAJIT_VERSION}.0.0.0")
118   list(GET VERSION 0 LUAJIT_VERSION_MAJOR)
119   list(GET VERSION 1 LUAJIT_VERSION_MINOR)
120   list(GET VERSION 2 LUAJIT_VERSION_PATCH)
121 endif()
123 find_library(LUAJIT_LIBRARY NAMES_PER_DIR NAMES luajit-5.1 lua51 lua5.1 lua-5.1)
124 mark_as_advanced(LUAJIT_LIBRARY)
126 set(LUAJIT_LIBRARY2)
127 if(NOT APPLE AND UNIX)
128   find_library(LUA_MATH_LIBRARY m)
129   mark_as_advanced(LUA_MATH_LIBRARY)
130   if(EXISTS "${LUA_MATH_LIBRARY}")
131     list(APPEND LUAJIT_LIBRARY2 ${LUA_MATH_LIBRARY})
132   else()
133     list(APPEND LUAJIT_LIBRARY2 m)
134   endif()
135   get_filename_component(ext ${LUAJIT_LIBRARY} EXT)
136   if(ext STREQUAL CMAKE_STATIC_LIBRARY_SUFFIX)
137     list(APPEND LUAJIT_LIBRARY2 ${CMAKE_DL_LIBS})
138   endif()
139 endif()
141 if(EXISTS "${LUAJIT_LIBRARY}")
142   set(LUAJIT_LIBRARIES
143     ${LUAJIT_LIBRARY} ${LUAJIT_LIBRARY2}
144   )
145 endif()
147 find_package_handle_standard_args(LuaJIT
148   FOUND_VAR
149     LUAJIT_FOUND
150   REQUIRED_VARS
151     ${LUAJIT_REQUIRED_VARS}
152   VERSION_VAR
153     LUAJIT_VERSION
154   HANDLE_COMPONENTS
157 if(LUAJIT_FOUND)
158   if(NOT TARGET luajit AND EXISTS "${LUAJIT_LIBRARY}")
159     add_library(luajit UNKNOWN IMPORTED)
160     set_target_properties(luajit PROPERTIES
161       IMPORTED_LINK_INTERFACE_LANGUAGES C
162       INTERFACE_INCLUDE_DIRECTORIES "${LUAJIT_INCLUDE_DIR}"
163       IMPORTED_LOCATION "${LUAJIT_LIBRARY}"
164     )
165     set_property(TARGET luajit APPEND PROPERTY
166       INTERFACE_LINK_LIBRARIES ${LUAJIT_LIBRARY2}
167     )
168   endif()
169 endif()