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 REPLACE "^[0-9]+\\.[0-9]+\\.([0-9]).*" "\\1" LUAJIT_VERSION_PATCH "${LUAJIT_VERSION}")
75 string(REGEX REPLACE "^[0-9]+\\.([0-9]).*" "\\1" LUAJIT_VERSION_MINOR "${LUAJIT_VERSION}")
76 string(REGEX REPLACE "^([0-9]).*" "\\1" LUAJIT_VERSION_MAJOR "${LUAJIT_VERSION}")
79 if(EXISTS "${LUAJIT_INCLUDE_DIR}/lua.h")
80 file(STRINGS ${LUAJIT_INCLUDE_DIR}/lua.h VERSION REGEX "^#define[ \t]+LUA_(RELEASE[ \t]+\"Lua [0-9]|VERSION([ \t]+\"Lua [0-9]|_[MR])).*")
81 string(REGEX REPLACE ".*;#define[ \t]+LUA_VERSION_MAJOR[ \t]+\"([0-9])\"[ \t]*;.*" "\\1" LUA_VERSION_MAJOR ";${VERSION};")
82 if(LUA_VERSION_MAJOR MATCHES "^[0-9]+$")
83 string(REGEX REPLACE ".*;#define[ \t]+LUA_VERSION_MINOR[ \t]+\"([0-9])\"[ \t]*;.*" "\\1" LUA_VERSION_MINOR ";${VERSION};")
84 string(REGEX REPLACE ".*;#define[ \t]+LUA_VERSION_RELEASE[ \t]+\"([0-9])\"[ \t]*;.*" "\\1" LUA_VERSION_PATCH ";${VERSION};")
85 set(LUA_VERSION "${LUA_VERSION_MAJOR}.${LUA_VERSION_MINOR}.${LUA_VERSION_PATCH}")
87 string(REGEX REPLACE ".*;#define[ \t]+LUA_RELEASE[ \t]+\"Lua ([0-9.]+)\"[ \t]*;.*" "\\1" LUA_VERSION ";${VERSION};")
88 if(NOT LUA_VERSION MATCHES "^[0-9.]+$")
89 string(REGEX REPLACE ".*;#define[ \t]+LUA_VERSION[ \t]+\"Lua ([0-9.]+)\"[ \t]*;.*" "\\1" LUA_VERSION ";${VERSION};")
91 string(REGEX REPLACE "^[0-9]+\\.[0-9]+\\.([0-9]).*" "\\1" LUA_VERSION_PATCH "${LUA_VERSION}")
92 string(REGEX REPLACE "^[0-9]+\\.([0-9]).*" "\\1" LUA_VERSION_MINOR "${LUA_VERSION}")
93 string(REGEX REPLACE "^([0-9]).*" "\\1" LUA_VERSION_MAJOR "${LUA_VERSION}")
97 list(FIND LuaJIT_FIND_COMPONENTS "luajit" index)
98 if(${index} GREATER -1)
99 find_program(LUAJIT_EXECUTABLE NAMES luajit-${LUAJIT_VERSION} luajit)
100 mark_as_advanced(LUAJIT_EXECUTABLE)
102 if(LuaJIT_FIND_REQUIRED_luajit)
103 list(APPEND LUAJIT_REQUIRED_VARS LUAJIT_EXECUTABLE)
105 if(EXISTS "${LUAJIT_EXECUTABLE}")
106 set(LuaJIT_luajit_FOUND 1)
110 if(NOT LUAJIT_VERSION AND EXISTS "${LUAJIT_EXECUTABLE}")
111 get_filename_component(LUAJIT_DIR "${LUAJIT_EXECUTABLE}" DIRECTORY)
112 get_filename_component(LUAJIT_DIR "${LUAJIT_DIR}" DIRECTORY)
113 execute_process(COMMAND ${LUAJIT_EXECUTABLE} -v OUTPUT_VARIABLE LUAJIT_VERSION)
114 find_path(LUAJIT_INCLUDE_DIR luajit.h HINTS ${LUAJIT_DIR} PATH_SUFFIXES include)
115 string(REGEX REPLACE "LuaJIT[ ]+([^ ]+).*" "\\1" LUAJIT_VERSION "${LUAJIT_VERSION}")
116 string(REGEX REPLACE "^[0-9]+\\.[0-9]+\\.([0-9]).*" "\\1" LUAJIT_VERSION_PATCH "${LUAJIT_VERSION}")
117 string(REGEX REPLACE "^[0-9]+\\.([0-9]).*" "\\1" LUAJIT_VERSION_MINOR "${LUAJIT_VERSION}")
118 string(REGEX REPLACE "^([0-9]).*" "\\1" LUAJIT_VERSION_MAJOR "${LUAJIT_VERSION}")
121 find_library(LUAJIT_LIBRARY NAMES_PER_DIR NAMES luajit-5.1 lua51 lua5.1 lua-5.1)
122 mark_as_advanced(LUAJIT_LIBRARY)
123 if(EXISTS "${LUAJIT_LIBRARY}")
124 list(APPEND LUAJIT_LIBRARIES ${LUAJIT_LIBRARY})
125 if(UNIX AND NOT APPLE AND NOT BEOS)
126 find_library(LUA_MATH_LIBRARY m)
127 mark_as_advanced(LUA_MATH_LIBRARY)
128 if(NOT EXISTS "${LUA_MATH_LIBRARY}")
129 list(APPEND LUAJIT_LIBRARIES m)
131 list(APPEND LUAJIT_LIBRARIES ${LUA_MATH_LIBRARY})
133 get_filename_component(ext ${LUAJIT_LIBRARY} EXT)
134 if(CMAKE_STATIC_LIBRARY_SUFFIX STREQUAL ext)
135 list(APPEND LUAJIT_LIBRARIES ${CMAKE_DL_LIBS})
141 find_package_handle_standard_args(LuaJIT
145 ${LUAJIT_REQUIRED_VARS}
152 if(NOT TARGET luajit AND EXISTS "${LUAJIT_LIBRARY}")
153 add_library(luajit UNKNOWN IMPORTED)
154 set_target_properties(luajit PROPERTIES
155 INTERFACE_INCLUDE_DIRECTORIES "${LUAJIT_INCLUDE_DIR}"
156 IMPORTED_LINK_INTERFACE_LANGUAGES C
157 IMPORTED_LOCATION "${LUAJIT_LIBRARY}"
159 set(libraries ${LUAJIT_LIBRARIES})
160 list(REMOVE_ITEM libraries ${LUAJIT_LIBRARY})
161 set_property(TARGET luajit APPEND PROPERTY
162 INTERFACE_LINK_LIBRARIES ${libraries}