fix version matching in FindPackage.cmake
[liba.git] / cmake / FindLuaJIT.cmake
blob9ab9fe1dc891aac969824425a5b48f673c617518
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 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}")
77 endif()
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}")
86   else()
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};")
90     endif()
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}")
94   endif()
95 endif()
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)
101 endif()
102 if(LuaJIT_FIND_REQUIRED_luajit)
103   list(APPEND LUAJIT_REQUIRED_VARS LUAJIT_EXECUTABLE)
104 endif()
105 if(EXISTS "${LUAJIT_EXECUTABLE}")
106   set(LuaJIT_luajit_FOUND 1)
107 endif()
108 set(index)
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[ ]+([^\n ]+).*" "\\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}")
119 endif()
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)
130     else()
131       list(APPEND LUAJIT_LIBRARIES ${LUA_MATH_LIBRARY})
132     endif()
133     get_filename_component(ext ${LUAJIT_LIBRARY} EXT)
134     if(CMAKE_STATIC_LIBRARY_SUFFIX STREQUAL ext)
135       list(APPEND LUAJIT_LIBRARIES ${CMAKE_DL_LIBS})
136     endif()
137     set(ext)
138   endif()
139 endif()
141 find_package_handle_standard_args(LuaJIT
142   FOUND_VAR
143     LUAJIT_FOUND
144   REQUIRED_VARS
145     ${LUAJIT_REQUIRED_VARS}
146   VERSION_VAR
147     LUAJIT_VERSION
148   HANDLE_COMPONENTS
151 if(LUAJIT_FOUND)
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}"
158     )
159     set(libraries ${LUAJIT_LIBRARIES})
160     list(REMOVE_ITEM libraries ${LUAJIT_LIBRARY})
161     set_property(TARGET luajit APPEND PROPERTY
162       INTERFACE_LINK_LIBRARIES ${libraries}
163     )
164     set(libraries)
165   endif()
166 endif()