From db8b16641ce24636942e1d4649326f1fdf9981e2 Mon Sep 17 00:00:00 2001 From: Leonardo de Moura Date: Thu, 7 Nov 2013 17:22:28 -0800 Subject: [PATCH] chore(build): check if the Lua installed in the system supports lua_newstate Signed-off-by: Leonardo de Moura --- src/CMakeLists.txt | 3 +++ src/bindings/lua/leanlua_state.cpp | 4 ++++ src/cmake/Modules/CheckLuaNewstate.cc | 19 +++++++++++++++++++ src/cmake/Modules/FindLua.cmake | 16 ++++++++++++++++ 4 files changed, 42 insertions(+) create mode 100644 src/cmake/Modules/CheckLuaNewstate.cc diff --git a/src/CMakeLists.txt b/src/CMakeLists.txt index 92ee77e32d..6895ccc650 100644 --- a/src/CMakeLists.txt +++ b/src/CMakeLists.txt @@ -126,6 +126,9 @@ if ("${LUA_FOUND}" MATCHES "TRUE") message(STATUS "Using Lua script language") set(EXTRA_LIBS ${EXTRA_LIBS} ${LUA_LIBRARIES}) set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -I ${LUA_INCLUDE_DIR} -D LEAN_USE_LUA") + if ("${HAS_LUA_NEWSTATE}$" MATCHES "TRUE") + set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -D LEAN_USE_LUA_NEWSTATE") + endif() else() message(WARNING "FAILED to find Lua script language, it will not be available") endif() diff --git a/src/bindings/lua/leanlua_state.cpp b/src/bindings/lua/leanlua_state.cpp index e58af96a6e..5cacfe06d4 100644 --- a/src/bindings/lua/leanlua_state.cpp +++ b/src/bindings/lua/leanlua_state.cpp @@ -29,7 +29,11 @@ struct leanlua_state::imp { std::mutex m_mutex; imp() { + #ifdef LEAN_USE_LUA_NEWSTATE m_state = lua_newstate(lua_realloc, nullptr); + #else + m_state = luaL_newstate(); + #endif if (m_state == nullptr) throw exception("fail to create Lua interpreter"); luaL_openlibs(m_state); diff --git a/src/cmake/Modules/CheckLuaNewstate.cc b/src/cmake/Modules/CheckLuaNewstate.cc new file mode 100644 index 0000000000..c9884bd72b --- /dev/null +++ b/src/cmake/Modules/CheckLuaNewstate.cc @@ -0,0 +1,19 @@ +/* +Copyright (c) 2013 Microsoft Corporation. All rights reserved. +Released under Apache 2.0 license as described in the file LICENSE. + +Author: Leonardo de Moura +*/ +#include +#include +#include + +static void * lua_realloc(void *, void * q, size_t, size_t sz) { return realloc(q, sz); } + +// Little program for checking whether lua_newstate is available +int main() { + lua_State * L; + L = lua_newstate(lua_realloc, nullptr); + lua_close(L); + return 0; +} diff --git a/src/cmake/Modules/FindLua.cmake b/src/cmake/Modules/FindLua.cmake index 31ddba6fd7..444ab8b791 100644 --- a/src/cmake/Modules/FindLua.cmake +++ b/src/cmake/Modules/FindLua.cmake @@ -115,3 +115,19 @@ FIND_PACKAGE_HANDLE_STANDARD_ARGS(Lua VERSION_VAR LUA_VERSION_STRING) MARK_AS_ADVANCED(LUA_INCLUDE_DIR LUA_LIBRARIES LUA_LIBRARY LUA_MATH_LIBRARY LUA_EXECUTABLE) + +# Print out version number +if (LUA_FOUND) + try_run(LUA_CHECK LUA_CHECK_BUILD + ${LEAN_BINARY_DIR}${CMAKE_FILES_DIRECTORY}/CMakeTmp + ${LEAN_SOURCE_DIR}/cmake/Modules/CheckLuaNewstate.cc + CMAKE_FLAGS -DINCLUDE_DIRECTORIES=${LUA_INCLUDE_DIR} + -DLINK_LIBRARIES=${LUA_LIBRARIES} + RUN_OUTPUT_VARIABLE LUA_TRY_OUT) + if ("${LUA_CHECK}" MATCHES "0" AND "${LUA_CHECK_BUILD}$" MATCHES "TRUE") + message(STATUS "lua_newstate works") + set(HAS_LUA_NEWSTATE TRUE) + else() + message(STATUS "lua_newstate is not supported by your Lua engine, Lean will not be able to track memory consumed by the Lua engine") + endif() +endif ()