5 # Find luajit include dirs and libraries.
15 # This module defines the following :ref:`Imported Targets <Imported Targets>`:
22 # This module defines the following variables:
26 # ``LUAJIT_INCLUDE_DIR``
28 # ``LUAJIT_LIBRARIES``
32 # ``LUAJIT_VERSION_MAJOR``
34 # ``LUAJIT_VERSION_MINOR``
36 # ``LUAJIT_VERSION_PATCH``
38 # ``LuaJIT_luajit_FOUND``
40 # ``LUAJIT_EXECUTABLE``
45 # Search results are saved persistently in CMake cache entries:
47 # ``LUAJIT_INCLUDE_DIR``
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}
65 if(LUAJIT_INCLUDE_DIR)
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)
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}")
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};")
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)
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)
104 if(LuaJIT_FIND_REQUIRED_luajit)
105 list(APPEND LUAJIT_REQUIRED_VARS LUAJIT_EXECUTABLE)
107 if(EXISTS "${LUAJIT_EXECUTABLE}")
108 set(LuaJIT_luajit_FOUND 1)
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)
123 find_library(LUAJIT_LIBRARY NAMES_PER_DIR NAMES luajit-5.1 lua51 lua5.1 lua-5.1)
124 mark_as_advanced(LUAJIT_LIBRARY)
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})
133 list(APPEND LUAJIT_LIBRARY2 m)
135 get_filename_component(ext ${LUAJIT_LIBRARY} EXT)
136 if(ext STREQUAL CMAKE_STATIC_LIBRARY_SUFFIX)
137 list(APPEND LUAJIT_LIBRARY2 ${CMAKE_DL_LIBS})
141 if(EXISTS "${LUAJIT_LIBRARY}")
143 ${LUAJIT_LIBRARY} ${LUAJIT_LIBRARY2}
147 find_package_handle_standard_args(LuaJIT
151 ${LUAJIT_REQUIRED_VARS}
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}"
165 set_property(TARGET luajit APPEND PROPERTY
166 INTERFACE_LINK_LIBRARIES ${LUAJIT_LIBRARY2}