diff options
Diffstat (limited to 'cmake/modules')
-rw-r--r-- | cmake/modules/FindLUA.cmake | 119 | ||||
-rw-r--r-- | cmake/modules/FindLua.cmake | 251 |
2 files changed, 251 insertions, 119 deletions
diff --git a/cmake/modules/FindLUA.cmake b/cmake/modules/FindLUA.cmake deleted file mode 100644 index 23c03e059c..0000000000 --- a/cmake/modules/FindLUA.cmake +++ /dev/null @@ -1,119 +0,0 @@ -# -# Locate Lua library -# This module defines -# LUA_FOUND - If false, do not try to link to Lua -# LUA_LIBRARIES -# LUA_INCLUDE_DIRS - Where to find lua.h -# LUA_DLL_DIR - (Windows) Path to the Lua DLL. -# LUA_DLL - (Windows) Name of the Lua DLL. -# -# Note that the expected include convention is -# #include "lua.h" -# and not -# #include <lua/lua.h> -# This is because, the lua location is not standardized and may exist -# in locations other than lua/ - -INCLUDE(FindWSWinLibs) -FindWSWinLibs("lua-5*" "LUA_HINTS") - -if(NOT USE_REPOSITORY) - find_package(PkgConfig) - pkg_search_module(LUA lua5.2 lua-5.2 lua52 lua5.1 lua-5.1 lua51) - if(NOT LUA_FOUND) - pkg_search_module(LUA "lua<=5.2.99") - endif() -endif() - -FIND_PATH(LUA_INCLUDE_DIR lua.h - HINTS - "${LUA_INCLUDEDIR}" - "$ENV{LUA_DIR}" - ${LUA_HINTS} - PATH_SUFFIXES include/lua52 include/lua5.2 include/lua-5.2 include/lua51 include/lua5.1 include/lua-5.1 include/lua include - PATHS - ~/Library/Frameworks - /Library/Frameworks - /usr/local - /usr - /sw # Fink - /opt/local # DarwinPorts - /opt/csw # Blastwave - /opt -) - -if(LUA_INCLUDE_DIR AND EXISTS "${LUA_INCLUDE_DIR}/lua.h") - file(STRINGS "${LUA_INCLUDE_DIR}/lua.h" LUA_VERSION_NUM REGEX "LUA_VERSION_NUM") - if (LUA_VERSION_NUM) - string(REGEX REPLACE "^#define[ \t]+LUA_VERSION_NUM[ \t]+([0-9]+)" "\\1" - LUA_VERSION_NUM "${LUA_VERSION_NUM}") - endif() -endif() -string( REGEX REPLACE ".*[/\\]lua(.+)$" "\\1" LUA_INC_SUFFIX "${LUA_INCLUDE_DIR}" ) -if ( LUA_INCLUDE_DIR STREQUAL LUA_INC_SUFFIX ) - set( LUA_INC_SUFFIX "") -endif() - -FIND_LIBRARY(LUA_LIBRARY - NAMES lua${LUA_INC_SUFFIX} lua52 lua5.2 lua-5.2 lua51 lua5.1 lua-5.1 lua - HINTS - "${LUA_LIBDIR}" - "$ENV{LUA_DIR}" - ${LUA_HINTS} - PATH_SUFFIXES lib64 lib - PATHS - ~/Library/Frameworks - /Library/Frameworks - /usr/local - /usr - /sw - /opt/local - /opt/csw - /opt -) - -# Lua 5.3 is not supported, only 5.1/5.2 are (due to bitops problem) -if(LUA_VERSION_NUM GREATER 502) - set(LUA_VERSION_NUM) -endif() - -INCLUDE(FindPackageHandleStandardArgs) -# handle the QUIETLY and REQUIRED arguments and set LUA_FOUND to TRUE if -# all listed variables are TRUE -find_package_handle_standard_args(LUA - REQUIRED_VARS LUA_LIBRARY LUA_INCLUDE_DIR LUA_VERSION_NUM - VERSION_VAR LUA_VERSION_NUM) - -IF(LUA_FOUND) - SET( LUA_LIBRARIES "${LUA_LIBRARY}") - SET( LUA_INCLUDE_DIRS ${LUA_INCLUDE_DIR} ) - if (WIN32) - set ( LUA_DLL_DIR "${LUA_HINTS}" - CACHE PATH "Path to Lua DLL" - ) - file( GLOB _lua_dll RELATIVE "${LUA_DLL_DIR}" - "${LUA_DLL_DIR}/lua*.dll" - ) - set ( LUA_DLL ${_lua_dll} - # We're storing filenames only. Should we use STRING instead? - CACHE FILEPATH "Lua DLL file name" - ) - mark_as_advanced( LUA_DLL_DIR LUA_DLL ) - endif() - if(LUA_DLL_DIR MATCHES ".*/lua-.*-unicode-.*") - # Do we have Lua with Unicode for Windows patches? - # https://github.com/Lekensteyn/lua-unicode - # XXX Would be better if it was possible to - # detect a Lua-unicode build from C and Lua code - # but upstream rejected patches for that so we do - # it here. - set(HAVE_LUA_UNICODE True) - endif() -ELSE(LUA_FOUND) - SET( LUA_LIBRARIES ) - SET( LUA_INCLUDE_DIRS ) - SET( LUA_DLL_DIR ) - SET( LUA_DLL ) -ENDIF(LUA_FOUND) - -MARK_AS_ADVANCED(LUA_INCLUDE_DIRS LUA_LIBRARIES) diff --git a/cmake/modules/FindLua.cmake b/cmake/modules/FindLua.cmake new file mode 100644 index 0000000000..7e835843c2 --- /dev/null +++ b/cmake/modules/FindLua.cmake @@ -0,0 +1,251 @@ +# Distributed under the OSI-approved BSD 3-Clause License. See accompanying +# file Copyright.txt or https://cmake.org/licensing for details. + +#[=======================================================================[.rst: +FindLua +------- + +Locate Lua library. + +.. versionadded:: 3.18 + Support for Lua 5.4. + +This module defines: + +``LUA_FOUND`` + if false, do not try to link to Lua +``LUA_LIBRARIES`` + both lua and lualib +``LUA_INCLUDE_DIR`` + where to find lua.h +``LUA_VERSION_STRING`` + the version of Lua found +``LUA_VERSION_MAJOR`` + the major version of Lua +``LUA_VERSION_MINOR`` + the minor version of Lua +``LUA_VERSION_PATCH`` + the patch version of Lua + +Note that the expected include convention is + +:: + + #include "lua.h" + +and not + +:: + + #include <lua/lua.h> + +This is because, the lua location is not standardized and may exist in +locations other than lua/ +#]=======================================================================] + +cmake_policy(PUSH) # Policies apply to functions at definition-time +cmake_policy(SET CMP0012 NEW) # For while(TRUE) + +INCLUDE(FindWSWinLibs) +FindWSWinLibs("lua-5*" "LUA_HINTS") + +unset(_lua_include_subdirs) +unset(_lua_library_names) +unset(_lua_append_versions) + +# this is a function only to have all the variables inside go away automatically +function(_lua_get_versions) + if (LUA_FIND_VERSIONS STREQUAL 5.2) + set(LUA_VERSIONS5 5.2) + elseif(LUA_FIND_VERSIONS STREQUAL 5.1) + set(LUA_VERSIONS5 5.1) + else() + set(LUA_VERSIONS5 5.2 5.1) + endif() + set(_lua_append_versions ${LUA_VERSIONS5}) + + if (LUA_Debug) + message(STATUS "Considering following Lua versions: ${_lua_append_versions}") + endif() + + set(_lua_append_versions "${_lua_append_versions}" PARENT_SCOPE) +endfunction() + +function(_lua_set_version_vars) + set(_lua_include_subdirs_raw "lua") + + foreach (ver IN LISTS _lua_append_versions) + string(REGEX MATCH "^([0-9]+)\\.([0-9]+)$" _ver "${ver}") + list(APPEND _lua_include_subdirs_raw + lua${CMAKE_MATCH_1}${CMAKE_MATCH_2} + lua${CMAKE_MATCH_1}.${CMAKE_MATCH_2} + lua-${CMAKE_MATCH_1}.${CMAKE_MATCH_2} + ) + endforeach () + + # Prepend "include/" to each path directly after the path + set(_lua_include_subdirs "include") + foreach (dir IN LISTS _lua_include_subdirs_raw) + list(APPEND _lua_include_subdirs "${dir}" "include/${dir}") + endforeach () + + set(_lua_include_subdirs "${_lua_include_subdirs}" PARENT_SCOPE) +endfunction(_lua_set_version_vars) + +function(_lua_get_header_version) + unset(LUA_VERSION_STRING PARENT_SCOPE) + set(_hdr_file "${LUA_INCLUDE_DIR}/lua.h") + + if (NOT EXISTS "${_hdr_file}") + return() + endif () + + # At least 5.[012] have different ways to express the version + # so all of them need to be tested. Lua 5.2 defines LUA_VERSION + # and LUA_RELEASE as joined by the C preprocessor, so avoid those. + file(STRINGS "${_hdr_file}" lua_version_strings + REGEX "^#define[ \t]+LUA_(RELEASE[ \t]+\"Lua [0-9]|VERSION([ \t]+\"Lua [0-9]|_[MR])).*") + + string(REGEX REPLACE ".*;#define[ \t]+LUA_VERSION_MAJOR[ \t]+\"([0-9])\"[ \t]*;.*" "\\1" LUA_VERSION_MAJOR ";${lua_version_strings};") + if (LUA_VERSION_MAJOR MATCHES "^[0-9]+$") + string(REGEX REPLACE ".*;#define[ \t]+LUA_VERSION_MINOR[ \t]+\"([0-9])\"[ \t]*;.*" "\\1" LUA_VERSION_MINOR ";${lua_version_strings};") + string(REGEX REPLACE ".*;#define[ \t]+LUA_VERSION_RELEASE[ \t]+\"([0-9])\"[ \t]*;.*" "\\1" LUA_VERSION_PATCH ";${lua_version_strings};") + set(LUA_VERSION_STRING "${LUA_VERSION_MAJOR}.${LUA_VERSION_MINOR}.${LUA_VERSION_PATCH}") + else () + string(REGEX REPLACE ".*;#define[ \t]+LUA_RELEASE[ \t]+\"Lua ([0-9.]+)\"[ \t]*;.*" "\\1" LUA_VERSION_STRING ";${lua_version_strings};") + if (NOT LUA_VERSION_STRING MATCHES "^[0-9.]+$") + string(REGEX REPLACE ".*;#define[ \t]+LUA_VERSION[ \t]+\"Lua ([0-9.]+)\"[ \t]*;.*" "\\1" LUA_VERSION_STRING ";${lua_version_strings};") + endif () + string(REGEX REPLACE "^([0-9]+)\\.[0-9.]*$" "\\1" LUA_VERSION_MAJOR "${LUA_VERSION_STRING}") + string(REGEX REPLACE "^[0-9]+\\.([0-9]+)[0-9.]*$" "\\1" LUA_VERSION_MINOR "${LUA_VERSION_STRING}") + string(REGEX REPLACE "^[0-9]+\\.[0-9]+\\.([0-9]).*" "\\1" LUA_VERSION_PATCH "${LUA_VERSION_STRING}") + endif () + foreach (ver IN LISTS _lua_append_versions) + if (ver STREQUAL "${LUA_VERSION_MAJOR}.${LUA_VERSION_MINOR}") + set(LUA_VERSION_MAJOR ${LUA_VERSION_MAJOR} PARENT_SCOPE) + set(LUA_VERSION_MINOR ${LUA_VERSION_MINOR} PARENT_SCOPE) + set(LUA_VERSION_PATCH ${LUA_VERSION_PATCH} PARENT_SCOPE) + set(LUA_VERSION_STRING ${LUA_VERSION_STRING} PARENT_SCOPE) + return() + endif () + endforeach () +endfunction(_lua_get_header_version) + +function(_lua_find_header) + _lua_set_version_vars() + + # Initialize as local variable + set(CMAKE_IGNORE_PATH ${CMAKE_IGNORE_PATH}) + while (TRUE) + # Find the next header to test. Check each possible subdir in order + # This prefers e.g. higher versions as they are earlier in the list + # It is also consistent with previous versions of FindLua + foreach (subdir IN LISTS _lua_include_subdirs) + find_path(LUA_INCLUDE_DIR lua.h + HINTS ${LUA_HINTS} ENV LUA_DIR + PATH_SUFFIXES ${subdir} + ) + if (LUA_INCLUDE_DIR) + break() + endif() + endforeach() + # Did not found header -> Fail + if (NOT LUA_INCLUDE_DIR) + return() + endif() + _lua_get_header_version() + # Found accepted version -> Ok + if (LUA_VERSION_STRING) + if (LUA_Debug) + message(STATUS "Found suitable version ${LUA_VERSION_STRING} in ${LUA_INCLUDE_DIR}/lua.h") + endif() + return() + endif() + # Found wrong version -> Ignore this path and retry + if (LUA_Debug) + message(STATUS "Ignoring unsuitable version in ${LUA_INCLUDE_DIR}") + endif() + list(APPEND CMAKE_IGNORE_PATH "${LUA_INCLUDE_DIR}") + unset(LUA_INCLUDE_DIR CACHE) + unset(LUA_INCLUDE_DIR) + unset(LUA_INCLUDE_DIR PARENT_SCOPE) + endwhile () +endfunction() + +_lua_get_versions() +_lua_find_header() +_lua_get_header_version() +unset(_lua_append_versions) + +if (LUA_VERSION_STRING) + set(_lua_library_names + lua${LUA_VERSION_MAJOR}${LUA_VERSION_MINOR} + lua${LUA_VERSION_MAJOR}.${LUA_VERSION_MINOR} + lua-${LUA_VERSION_MAJOR}.${LUA_VERSION_MINOR} + lua.${LUA_VERSION_MAJOR}.${LUA_VERSION_MINOR} + ) +endif () + +find_library(LUA_LIBRARY + NAMES ${_lua_library_names} lua + NAMES_PER_DIR + HINTS + ${LUA_HINTS} + ENV LUA_DIR + PATH_SUFFIXES lib +) +unset(_lua_library_names) + +if (LUA_LIBRARY) + # include the math library for Unix + if (UNIX AND NOT APPLE AND NOT BEOS) + find_library(LUA_MATH_LIBRARY m) + mark_as_advanced(LUA_MATH_LIBRARY) + set(LUA_LIBRARIES "${LUA_LIBRARY};${LUA_MATH_LIBRARY}") + + # include dl library for statically-linked Lua library + get_filename_component(LUA_LIB_EXT ${LUA_LIBRARY} EXT) + if(LUA_LIB_EXT STREQUAL CMAKE_STATIC_LIBRARY_SUFFIX) + list(APPEND LUA_LIBRARIES ${CMAKE_DL_LIBS}) + endif() + + # For Windows and Mac, don't need to explicitly include the math library + else () + set(LUA_LIBRARIES "${LUA_LIBRARY}") + endif () +endif () + +include(FindPackageHandleStandardArgs) +# handle the QUIETLY and REQUIRED arguments and set LUA_FOUND to TRUE if +# all listed variables are TRUE +FIND_PACKAGE_HANDLE_STANDARD_ARGS(Lua + REQUIRED_VARS LUA_LIBRARIES LUA_INCLUDE_DIR + VERSION_VAR LUA_VERSION_STRING) + +mark_as_advanced(LUA_INCLUDE_DIR LUA_LIBRARY) + +cmake_policy(POP) + +IF(Lua_FOUND) + SET( LUA_INCLUDE_DIRS ${LUA_INCLUDE_DIR} ) + if (WIN32) + set ( LUA_DLL_DIR "${LUA_HINTS}" CACHE PATH "Path to Lua DLL") + file( GLOB _lua_dll RELATIVE "${LUA_DLL_DIR}" "${LUA_DLL_DIR}/lua*.dll") + set ( LUA_DLL ${_lua_dll} CACHE FILEPATH "Lua DLL file name") + mark_as_advanced( LUA_DLL_DIR LUA_DLL ) + endif() + if(LUA_DLL_DIR MATCHES ".*/lua-.*-unicode-.*") + # Do we have Lua with Unicode for Windows patches? + # https://github.com/Lekensteyn/lua-unicode + # XXX Would be better if it was possible to + # detect a Lua-unicode build from C and Lua code + # but upstream rejected patches for that so we do + # it here. + set(HAVE_LUA_UNICODE True) + endif() +ELSE(Lua_FOUND) + SET( LUA_LIBRARIES ) + SET( LUA_INCLUDE_DIRS ) + SET( LUA_DLL_DIR ) + SET( LUA_DLL ) +ENDIF(Lua_FOUND) |