From 0d2c125cbb66247930d7534d414fe881b59a9fc5 Mon Sep 17 00:00:00 2001 From: Kevin Brightwell Date: Tue, 29 Aug 2017 18:17:05 -0400 Subject: [PATCH] Add FindLua/FindLuaJIT that behave better Better FindLua modules as per CMake devs --- .gitignore | 5 + CMakeLists.txt | 25 ++ cmake/Modules/FindLua.cmake | 233 +++++++++++++++++++ cmake/Modules/FindLua/set_version_vars.cmake | 56 +++++ cmake/Modules/FindLua/version_check.cmake | 28 +++ cmake/Modules/FindLuaJIT.cmake | 173 ++++++++++++++ 6 files changed, 520 insertions(+) create mode 100644 cmake/Modules/FindLua.cmake create mode 100644 cmake/Modules/FindLua/set_version_vars.cmake create mode 100644 cmake/Modules/FindLua/version_check.cmake create mode 100644 cmake/Modules/FindLuaJIT.cmake diff --git a/.gitignore b/.gitignore index b99bdf02..a4f37ddb 100644 --- a/.gitignore +++ b/.gitignore @@ -22,6 +22,11 @@ x64/ # VSCode .vscode/ +# CMake +build/ +CMakeCache.txt +CMakeFiles/ + # Compiler outputs obj/* bin/* diff --git a/CMakeLists.txt b/CMakeLists.txt index 905f636d..652de97c 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -6,6 +6,8 @@ set(CMAKE_CXX_STANDARD 14) set(CMAKE_CXX_STANDARD_REQUIRED ON) include_directories(.) +set(CMAKE_MODULE_PATH "${CMAKE_SOURCE_DIR}/cmake/Modules" "${CMAKE_MODULE_PATH}") + set(LUA_VERSION "5.3" CACHE STRING "The version of Lua needed (5.1, 5.2, 5.3)") if (LUA_VERSION MATCHES "5.1") find_package(Lua 5.1 EXACT REQUIRED) @@ -21,6 +23,28 @@ endif() include_directories(${LUA_INCLUDE_DIR}) +# Features a C++ compiler must have to be used to compile sol2 +# This list is not *complete* as CMake does not support features for +# all of the advanced features utilized. +set(CXX_FEATURES cxx_auto_type + cxx_constexpr + cxx_decltype + cxx_decltype_auto + cxx_default_function_template_args + cxx_final + cxx_lambdas + cxx_noexcept + cxx_nullptr + cxx_override + cxx_range_for + cxx_return_type_deduction + cxx_right_angle_brackets + cxx_static_assert + cxx_strong_enums + cxx_variadic_macros + cxx_variadic_templates + ) + set(HEADER_SRCS sol.hpp sol/as_args.hpp sol/as_returns.hpp @@ -154,6 +178,7 @@ message(STATUS "${example_source_file}") get_filename_component(example_name ${example_source_file} NAME_WE) add_executable(${example_name} ${example_source_file} ${HEADER_SRCS}) target_link_libraries(${example_name} ${LUA_LIBRARIES}) + target_compile_features(${example_name} PUBLIC ${CXX_FEATURES}) install(TARGETS ${example_name} RUNTIME DESTINATION bin/examples) endforeach() diff --git a/cmake/Modules/FindLua.cmake b/cmake/Modules/FindLua.cmake new file mode 100644 index 00000000..1e498a86 --- /dev/null +++ b/cmake/Modules/FindLua.cmake @@ -0,0 +1,233 @@ +# 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. 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_LIBRARY_DIR - Dir(s) where Lua libraries are found +# 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 +# +# This is because, the lua location is not standardized and may exist in +# locations other than lua/ + +unset(_lua_include_subdirs) +unset(_lua_append_versions) +unset(_lua_library_names) + + +include(${CMAKE_CURRENT_LIST_DIR}/FindLua/set_version_vars.cmake) +include(${CMAKE_CURRENT_LIST_DIR}/FindLua/version_check.cmake) + +# # this is a function only to have all the variables inside go away automatically +# function(_lua_set_version_vars) +# set(LUA_VERSIONS5 5.3 5.2 5.1 5.0) + +# if (Lua_FIND_VERSION_EXACT) +# if (Lua_FIND_VERSION_COUNT GREATER 1) +# set(_lua_append_versions ${Lua_FIND_VERSION_MAJOR}.${Lua_FIND_VERSION_MINOR}) +# endif () +# elseif (Lua_FIND_VERSION) +# # once there is a different major version supported this should become a loop +# if (NOT Lua_FIND_VERSION_MAJOR GREATER 5) +# if (Lua_FIND_VERSION_COUNT EQUAL 1) +# set(_lua_append_versions ${LUA_VERSIONS5}) +# else () +# foreach (subver IN LISTS LUA_VERSIONS5) +# if (NOT subver VERSION_LESS ${Lua_FIND_VERSION}) +# list(APPEND _lua_append_versions ${subver}) +# endif () +# endforeach () +# endif () +# endif () +# else () +# # once there is a different major version supported this should become a loop +# set(_lua_append_versions ${LUA_VERSIONS5}) +# endif () + +# list(APPEND _lua_include_subdirs "include/lua" "include") + +# foreach (ver IN LISTS _lua_append_versions) +# string(REGEX MATCH "^([0-9]+)\\.([0-9]+)$" _ver "${ver}") +# list(APPEND _lua_include_subdirs +# include/lua${CMAKE_MATCH_1}${CMAKE_MATCH_2} +# include/lua${CMAKE_MATCH_1}.${CMAKE_MATCH_2} +# include/lua-${CMAKE_MATCH_1}.${CMAKE_MATCH_2} +# ) +# list(APPEND _lua_library_names +# lua${CMAKE_MATCH_1}${CMAKE_MATCH_2} +# lua${CMAKE_MATCH_1}.${CMAKE_MATCH_2} +# lua.${CMAKE_MATCH_1}.${CMAKE_MATCH_2} +# lua-${CMAKE_MATCH_1}.${CMAKE_MATCH_2} +# ) +# endforeach () + +# set(_lua_include_subdirs "${_lua_include_subdirs}" PARENT_SCOPE) +# set(_lua_append_versions "${_lua_append_versions}" PARENT_SCOPE) +# endfunction(_lua_set_version_vars) + +# function(_lua_check_header_version _hdr_file) +# # 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_check_header_version) + +_lua_set_version_vars(lua "") + +find_path(LUA_INCLUDE_DIR lua.h + HINTS + ENV LUA_DIR + PATH_SUFFIXES ${_lua_include_subdirs} include + PATHS + ${LUA_DIR} + ~/Library/Frameworks + /Library/Frameworks + /sw # Fink + /opt/local # DarwinPorts + /opt/csw # Blastwave + /opt + /usr + /usr/local # Homebrew +) + +if (LUA_INCLUDE_DIR AND EXISTS "${LUA_INCLUDE_DIR}/lua.h") + _lua_check_header_version("${LUA_INCLUDE_DIR}/lua.h" "LUA") +endif () + +if (NOT LUA_VERSION_STRING) + foreach (subdir IN LISTS _lua_include_subdirs) + unset(LUA_INCLUDE_PREFIX CACHE) + find_path(LUA_INCLUDE_PREFIX ${subdir}/lua.h + HINTS + ENV LUA_DIR + PATHS + ${LUA_DIR} + ~/Library/Frameworks + /Library/Frameworks + /sw # Fink + /opt/local # DarwinPorts + /opt/csw # Blastwave + /opt + /usr + /usr/local + ) + if (LUA_INCLUDE_PREFIX) + _lua_check_header_version("${LUA_INCLUDE_PREFIX}/${subdir}/lua.h") + if (LUA_VERSION_STRING) + set(LUA_INCLUDE_DIR "${LUA_INCLUDE_PREFIX}/${subdir}") + break() + endif () + endif () + endforeach () +endif () +unset(_lua_include_subdirs) +unset(_lua_append_versions) + +find_library(LUA_LIBRARY + NAMES ${_lua_library_names} lua + HINTS + ENV LUA_DIR + PATH_SUFFIXES lib bin + PATHS + ${LUA_DIR} + ~/Library/Frameworks + /Library/Frameworks + /sw + /opt/local + /opt/csw + /opt + /usr + /usr/local + + # From the include_dir + ${LUA_INCLUDE_DIR}/../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) + 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 () + + set(LUA_LIBRARY_DIR ) + foreach (lib ${LUA_LIBRARIES}) + get_filename_component(lib_dir ${lib} DIRECTORY CACHE) + list(APPEND LUA_LIBRARY_DIR ${lib_dir}) + endforeach () + list(REMOVE_DUPLICATES LUA_LIBRARY_DIR) +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 + FOUND_VAR Lua_FOUND + REQUIRED_VARS LUA_LIBRARIES LUA_LIBRARY_DIR LUA_INCLUDE_DIR + VERSION_VAR LUA_VERSION_STRING) + +mark_as_advanced(LUA_INCLUDE_DIR LUA_LIBRARY LUA_LIBRARY_DIR LUA_MATH_LIBRARY) diff --git a/cmake/Modules/FindLua/set_version_vars.cmake b/cmake/Modules/FindLua/set_version_vars.cmake new file mode 100644 index 00000000..d60016dd --- /dev/null +++ b/cmake/Modules/FindLua/set_version_vars.cmake @@ -0,0 +1,56 @@ + +# Uses the Lua version and calculates the names of the include directories and library +# name accordingly. +# +# :: +# _prefix - Prefix added to all variables exposed, e.g. "lua" or "luajit" +# _lua_suffix - Name suffix, typically only used for specifiying luajit, which is "jit" + +function(_lua_set_version_vars _prefix _lua_suffix) + set(LUA_VERSIONS5 5.3 5.2 5.1 5.0) + + if (${_prefix}_FIND_VERSION_EXACT) + if (${_prefix}_FIND_VERSION_COUNT GREATER 1) + set(_${_prefix}_append_versions ${${_prefix}_FIND_VERSION_MAJOR}.${${_prefix}_FIND_VERSION_MINOR}) + endif () + elseif (${_prefix}_FIND_VERSION) + # once there is a different major version supported this should become a loop + if (NOT ${_prefix}_FIND_VERSION_MAJOR GREATER 5) + if (${_prefix}_FIND_VERSION_COUNT EQUAL 1) + set(_${_prefix}_append_versions ${${_prefix}_VERSIONS5}) + else () + foreach (subver IN LISTS ${_prefix}_VERSIONS5) + if (NOT subver VERSION_LESS ${${_prefix}_FIND_VERSION}) + list(APPEND _${_prefix}_append_versions ${subver}) + endif () + endforeach () + endif () + endif () + else () + # once there is a different major version supported this should become a loop + set(_${_prefix}_append_versions ${LUA_VERSIONS5}) + endif () + + list(APPEND _${_prefix}_include_subdirs "include/lua" "include") + + foreach (ver IN LISTS _${_prefix}_append_versions) + string(REGEX MATCH "^([0-9]+)\\.([0-9]+)$" _ver "${ver}") + list(APPEND _${_prefix}_include_subdirs + include/lua${_lua_suffix}${CMAKE_MATCH_1}${CMAKE_MATCH_2} + include/lua${_lua_suffix}${CMAKE_MATCH_1}.${CMAKE_MATCH_2} + include/lua${_lua_suffix}-${CMAKE_MATCH_1}.${CMAKE_MATCH_2} + ) + + # LuaJIT hides itself as Lua lib (maintaining ABI compat) + list(APPEND _${_prefix}_library_names + lua${CMAKE_MATCH_1}${CMAKE_MATCH_2} + lua${CMAKE_MATCH_1}.${CMAKE_MATCH_2} + lua.${CMAKE_MATCH_1}.${CMAKE_MATCH_2} + lua-${CMAKE_MATCH_1}.${CMAKE_MATCH_2} + ) + endforeach () + + set(_${_prefix}_include_subdirs "${_${_prefix}_include_subdirs}" PARENT_SCOPE) + set(_${_prefix}_append_versions "${_${_prefix}_append_versions}" PARENT_SCOPE) + set(_${_prefix}_library_names "${_${_prefix}_library_names}" PARENT_SCOPE) +endfunction(_lua_set_version_vars) \ No newline at end of file diff --git a/cmake/Modules/FindLua/version_check.cmake b/cmake/Modules/FindLua/version_check.cmake new file mode 100644 index 00000000..fa62f434 --- /dev/null +++ b/cmake/Modules/FindLua/version_check.cmake @@ -0,0 +1,28 @@ +# Distributed under the OSI-approved BSD 3-Clause License. See accompanying +# file Copyright.txt or https://cmake.org/licensing for details. + +# Provides a common-mechanic for extracting Lua versions from Lua/LuaJIT headers +macro(_lua_check_header_version _header_file _prefix) + # 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 "${_header_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" ${_prefix}_VERSION_MAJOR ";${lua_version_strings};") + if (${_prefix}_VERSION_MAJOR MATCHES "^[0-9]+$") + string(REGEX REPLACE ".*;#define[ \t]+LUA_VERSION_MINOR[ \t]+\"([0-9])\"[ \t]*;.*" "\\1" "${_prefix}_VERSION_MINOR" ";${lua_version_strings};") + string(REGEX REPLACE ".*;#define[ \t]+LUA_VERSION_RELEASE[ \t]+\"([0-9])\"[ \t]*;.*" "\\1" "${_prefix}_VERSION_PATCH" ";${lua_version_strings};") + set(${_prefix}_VERSION_STRING "${${_prefix}_VERSION_MAJOR}.${${_prefix}_VERSION_MINOR}.${${_prefix}_VERSION_PATCH}") + else () + string(REGEX REPLACE ".*;#define[ \t]+LUA_RELEASE[ \t]+\"Lua ([0-9.]+)\"[ \t]*;.*" "\\1" ${_prefix}_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" ${_prefix}_VERSION_STRING ";${lua_version_strings};") + endif () + string(REGEX REPLACE "^([0-9]+)\\.[0-9.]*$" "\\1" ${_prefix}_VERSION_MAJOR "${${_prefix}_VERSION_STRING}") + string(REGEX REPLACE "^[0-9]+\\.([0-9]+)[0-9.]*$" "\\1" ${_prefix}_VERSION_MINOR "${${_prefix}_VERSION_STRING}") + string(REGEX REPLACE "^[0-9]+\\.[0-9]+\\.([0-9]).*" "\\1" ${_prefix}_VERSION_PATCH "${${_prefix}_VERSION_STRING}") + endif () + + unset(lua_version_strings) +endmacro(_lua_check_header_version) \ No newline at end of file diff --git a/cmake/Modules/FindLuaJIT.cmake b/cmake/Modules/FindLuaJIT.cmake new file mode 100644 index 00000000..8ef3dbdc --- /dev/null +++ b/cmake/Modules/FindLuaJIT.cmake @@ -0,0 +1,173 @@ +# Distributed under the OSI-approved BSD 3-Clause License. See accompanying +# file Copyright.txt or https://cmake.org/licensing for details. + +#.rst: +# FindLuaJIT +# ------- +# +# +# +# Locate LuaJIT library. This module defines +# +# :: +# +# LUAJIT_FOUND - if false, do not try to link to LuaJIT +# LUAJIT_LIBRARIES - both lua and lualib +# LUAJIT_INCLUDE_DIR - where to find lua.h and luajit.h +# LUAJIT_VERSION_STRING - the version of LuaJIT found +# LUAJIT_VERSION_MAJOR - the major version of LuaJIT +# LUAJIT_VERSION_MINOR - the minor version of LuaJIT +# LUAJIT_VERSION_PATCH - the patch version of LuaJIT +# LUAJIT_LUA_VERSION_STRING - the version of Lua the found LuaJIT is compatible with +# +# +# +# Note that the expected include convention is +# +# :: +# +# #include "lua.h" +# +# and not +# +# :: +# +# #include +# +# This is because, the lua location is not standardized and may exist in +# locations other than lua/ + +unset(_luajit_include_subdirs) +unset(_luajit_append_versions) +unset(_luajit_library_names) + +include(${CMAKE_CURRENT_LIST_DIR}/FindLua/set_version_vars.cmake) +include(${CMAKE_CURRENT_LIST_DIR}/FindLua/version_check.cmake) + +_lua_set_version_vars(luajit "jit") + +find_path(LUAJIT_INCLUDE_DIR luajit.h + HINTS + ENV LUAJIT_DIR + PATH_SUFFIXES ${_luajit_include_subdirs} include/luajit include + PATHS + ${LUAJIT_DIR} + ~/Library/Frameworks + /Library/Frameworks + /sw # Fink + /opt/local # DarwinPorts + /opt/csw # Blastwave + /opt + /usr + /usr/local # Homebrew +) + +if (LUAJIT_INCLUDE_DIR AND EXISTS "${LUAJIT_INCLUDE_DIR}/lua.h") + _lua_check_header_version("${LUAJIT_INCLUDE_DIR}/lua.h" "LUAJIT") +endif () + +if (NOT LUAJIT_VERSION_STRING) + foreach (subdir IN LISTS _luajit_include_subdirs) + unset(LUAJIT_INCLUDE_PREFIX CACHE) + find_path(LUAJIT_INCLUDE_PREFIX ${subdir}/lua.h + HINTS + ENV LUA_DIR + PATHS + ${LUA_DIR} + ~/Library/Frameworks + /Library/Frameworks + /sw # Fink + /opt/local # DarwinPorts + /opt/csw # Blastwave + /opt + /usr + /usr/local # Homebrew + ) + if (LUAJIT_INCLUDE_PREFIX) + _lua_check_header_version("${LUAJIT_INCLUDE_PREFIX}/${subdir}/lua.h" "LUAJIT") + if (LUAJIT_VERSION_STRING) + set(LUAJIT_INCLUDE_DIR "${LUAJIT_INCLUDE_PREFIX}/${subdir}") + break() + endif () + endif () + endforeach () +endif () +unset(_luajit_include_subdirs) +unset(_luajit_append_versions) + +if (LUAJIT_INCLUDE_DIR AND EXISTS "${LUAJIT_INCLUDE_DIR}/luajit.h") + # LuaJIT defines two preprocessor macros: + # LUA_VERSION -> version string with lua version in it + # LUA_VERSION_NUM -> numeric representation, i.e. 20003 for 2.0.3 + # This just parses the LUAJIT_VERSION macro and extracts the version. + file(STRINGS "${LUAJIT_INCLUDE_DIR}/luajit.h" version_strings + REGEX "^#define[ \t]+LUAJIT_VERSION?[ \t]+(\"LuaJIT [0-9\\.]+(-(beta|alpha)[0-9]*)?\").*") + + string(REGEX REPLACE ".*;#define[ \t]+LUAJIT_VERSION[ \t]+\"LuaJIT ([0-9\\.]+(-(beta|alpha)[0-9]*)?)\"[ \t]*;.*" "\\1" LUAJIT_VERSION_STRING_SHORT ";${version_strings};") + string(REGEX REPLACE ".*;([0-9]+\\.[0-9]+\\.[0-9]+(-(beta|alpha)[0-9]*)?);.*" "\\1" luajit_version_num ";${LUAJIT_VERSION_STRING_SHORT};") + + string(REGEX REPLACE "^([0-9]+)\\.[0-9]+\\.(-(beta|alpha)[0-9]*)?$" "\\1" LUAJIT_VERSION_MAJOR "${luajit_version_num}") + string(REGEX REPLACE "^[0-9]+\\.([0-9]+)\\.[0-9](-(beta|alpha)[0-9]*)?$" "\\1" LUAJIT_VERSION_MINOR "${luajit_version_num}") + string(REGEX REPLACE "^[0-9]+\\.[0-9]+\\.([0-9]+(-(beta|alpha)[0-9]*)?)$" "\\1" LUAJIT_VERSION_PATCH "${luajit_version_num}") + + # We can also set the LUAJIT_LUA_VERSION_* fields that are found by FindLua. + # We do this as LuaJIT claims full compatibility with a certain LUA version. + _lua_check_header_version("${LUAJIT_INCLUDE_DIR}/lua.h" "LUAJIT_LUA_") + + set(LUAJIT_VERSION_STRING "${LUAJIT_LUA_VERSION_STRING} (${LUAJIT_VERSION_STRING_SHORT})") +endif() + +find_library(LUAJIT_LIBRARY + NAMES ${_luajit_library_names} luajit lua + HINTS + ENV LUAJIT_DIR + PATH_SUFFIXES lib + PATHS + ${LUAJIT_DIR} + ~/Library/Frameworks + /Library/Frameworks + /sw + /opt/local + /opt/csw + /opt + /usr + /usr/local # Homebrew +) +unset(_luajit_library_names) + +if (LUAJIT_LIBRARY) + # include the math library for Unix + if (UNIX AND NOT APPLE AND NOT BEOS) + find_library(LUAJIT_MATH_LIBRARY m) + set(LUAJIT_LIBRARIES "${LUAJIT_LIBRARY};${LUAJIT_MATH_LIBRARY}") + # For Windows and Mac, don't need to explicitly include the math library + else () + set(LUAJIT_LIBRARIES "${LUAJIT_LIBRARY}") + endif () + + set(LUAJIT_LIBRARY_DIR ) + foreach (lib ${LUAJIT_LIBRARIES}) + get_filename_component(lib_dir ${lib} DIRECTORY CACHE) + list(APPEND LUAJIT_LIBRARY_DIR ${lib_dir}) + endforeach () + list(REMOVE_DUPLICATES LUAJIT_LIBRARY_DIR) +endif () + +if(APPLE) + # Not sure why this is true, but its mentioned here: + # http://luajit.org/install.html#embed + set(LUAJIT_LINK_FLAGS "-pagezero_size 10000 -image_base 100000000") +else() + set(LUAJIT_LINK_FLAGS "") +endif() + +include(FindPackageHandleStandardArgs) +# handle the QUIETLY and REQUIRED arguments and set LuaJIT_FOUND to TRUE if +# all listed variables are TRUE +FIND_PACKAGE_HANDLE_STANDARD_ARGS(LuaJIT + FOUND_VAR LuaJIT_FOUND + REQUIRED_VARS LUAJIT_LIBRARIES LUAJIT_INCLUDE_DIR LUAJIT_LIBRARY_DIR + VERSION_VAR LUAJIT_VERSION_STRING) + +mark_as_advanced(LUAJIT_INCLUDE_DIR LUAJIT_LIBRARY LUAJIT_LIBRARY_DIR LUAJIT_MATH_LIBRARY LUAJIT_LINK_FLAGS) +