chore(build): check if the Lua installed in the system supports lua_newstate

Signed-off-by: Leonardo de Moura <leonardo@microsoft.com>
This commit is contained in:
Leonardo de Moura 2013-11-07 17:22:28 -08:00
parent 9000c7c2fa
commit db8b16641c
4 changed files with 42 additions and 0 deletions

View file

@ -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()

View file

@ -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);

View file

@ -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 <iostream>
#include <memory>
#include <lua.hpp>
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;
}

View file

@ -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 ()