Skip to content

Commit

Permalink
[ADD] Add lua package finding in CMakeLists.txt. Default to Builtin v…
Browse files Browse the repository at this point in the history
…ersion. Support: Builtin/LuaJIT/5.1/5.2/5.3/5.4.
  • Loading branch information
paintdream committed May 28, 2023
1 parent c88d6e9 commit 114481d
Show file tree
Hide file tree
Showing 4 changed files with 129 additions and 13 deletions.
65 changes: 54 additions & 11 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,10 @@ ENABLE_TESTING()
OPTION (ENABLE_COROUTINE "Enable Iris Coroutine support" ON)
OPTION (ENABLE_LUA "Enable Lua Binding support" ON)

SET (USE_LUA_VERSION "Builtin" CACHE STRING "Lua version")
SET_PROPERTY (CACHE USE_LUA_VERSION PROPERTY STRINGS "Builtin" "LuaJIT" "5.1" "5.2" "5.3" "5.4")
SET (CMAKE_MODULE_PATH ${CMAKE_MODULE_PATH} "${CMAKE_SOURCE_DIR}/cmake/")

IF (ENABLE_COROUTINE)
SET (CMAKE_CXX_STANDARD 20)
ELSE (ENABLE_COROUTINE)
Expand All @@ -21,7 +25,7 @@ ELSE (ENABLE_COROUTINE)
ENDIF (ENABLE_COROUTINE)

INCLUDE_DIRECTORIES ("${PROJECT_BINARY_DIR}")
STRING (REPLACE "/" "\\" LOCAL_SOURCE_DIR "${PROJECT_SOURCE_DIR}")
STRING (REPLACE "/" "\\" BUILTIN_SOURCE_DIR "${PROJECT_SOURCE_DIR}")

FILE (GLOB_RECURSE IRIS_DISPATCHER_DEMO_SRC
"${PROJECT_SOURCE_DIR}/src/iris_common.h"
Expand Down Expand Up @@ -114,24 +118,63 @@ ENDIF (ENABLE_COROUTINE)
IF (ENABLE_LUA)
FILE (GLOB_RECURSE IRIS_LUA_DEMO_SRC
"${PROJECT_SOURCE_DIR}/src/optional/iris_lua.h"
"${PROJECT_SOURCE_DIR}/src/optional/lua/src/*.h"
"${PROJECT_SOURCE_DIR}/src/optional/lua/src/*.hpp"
"${PROJECT_SOURCE_DIR}/src/optional/lua/src/*.c"
"${PROJECT_SOURCE_DIR}/test/iris_lua_demo.cpp"
)

LIST (REMOVE_ITEM IRIS_LUA_DEMO_SRC "${PROJECT_SOURCE_DIR}/src/optional/lua/src/lua.c")
LIST (REMOVE_ITEM IRIS_LUA_DEMO_SRC "${PROJECT_SOURCE_DIR}/src/optional/lua/src/luac.c")
IF (${USE_LUA_VERSION} STREQUAL "Builtin")
SET (ENABLE_LUA_BUILTIN ON)
ELSE (${USE_LUA_VERSION} STREQUAL "Builtin")
SET (ENABLE_LUA_BUILTIN OFF)
ENDIF (${USE_LUA_VERSION} STREQUAL "Builtin")

IF (ENABLE_LUA_BUILTIN)
MESSAGE (STATUS "Use built-in lua version.")
# default to lua 5.4
FILE (GLOB_RECURSE IRIS_LUA_CORE_SRC
"${PROJECT_SOURCE_DIR}/src/optional/lua/src/*.h"
"${PROJECT_SOURCE_DIR}/src/optional/lua/src/*.hpp"
"${PROJECT_SOURCE_DIR}/src/optional/lua/src/*.c"
)

LIST (REMOVE_ITEM IRIS_LUA_CORE_SRC "${PROJECT_SOURCE_DIR}/src/optional/lua/src/lua.c")
LIST (REMOVE_ITEM IRIS_LUA_CORE_SRC "${PROJECT_SOURCE_DIR}/src/optional/lua/src/luac.c")
SET (LUALIB)
ELSE (ENABLE_LUA_BUILTIN)
IF (${USE_LUA_VERSION} STREQUAL "LuaJIT")
FIND_PACKAGE (LuaJIT)
ELSE (${USE_LUA_VERSION} STREQUAL "LuaJIT")
FIND_PACKAGE (Lua ${USE_LUA_VERSION} REQUIRED)
ENDIF (${USE_LUA_VERSION} STREQUAL "LuaJIT")

IF (LUA_FOUND OR LUAJIT_FOUND)
MESSAGE (STATUS "Find lua version " ${USE_LUA_VERSION})
FILE (GLOB_RECURSE IRIS_LUA_CORE_SRC
"${LUA_INCLUDE_DIR}/*.h"
"${LUA_INCLUDE_DIR}/*.hpp"
)

SET (LUALIB ${LUA_LIBRARIES})
ELSE (LUA_FOUND OR LUAJIT_FOUND)
MESSAGE (FATAL_ERROR "Unable to find lua with version " ${USE_LUA_VERSION})
ENDIF (LUA_FOUND OR LUAJIT_FOUND)
ENDIF (ENABLE_LUA_BUILTIN)

ADD_EXECUTABLE (iris_lua_demo ${IRIS_LUA_DEMO_SRC})
ADD_EXECUTABLE (iris_lua_demo ${IRIS_LUA_DEMO_SRC} ${IRIS_LUA_CORE_SRC})

IF (ENABLE_COROUTINE)
TARGET_COMPILE_DEFINITIONS(iris_lua_demo PRIVATE USE_LUA_COROUTINE)
TARGET_COMPILE_DEFINITIONS (iris_lua_demo PRIVATE USE_LUA_COROUTINE)
ENDIF (ENABLE_COROUTINE)

IF (NOT ENABLE_LUA_BUILTIN)
TARGET_INCLUDE_DIRECTORIES (iris_lua_demo PUBLIC ${LUA_INCLUDE_DIR})
TARGET_COMPILE_DEFINITIONS (iris_lua_demo PUBLIC USE_LUA_LIBRARY)
ENDIF (NOT ENABLE_LUA_BUILTIN)

ADD_TEST (test_lua iris_lua_demo)

IF (NOT MSVC)
TARGET_LINK_LIBRARIES(iris_lua_demo m ${STDLIB})
ENDIF (NOT MSVC)
IF (MSVC)
TARGET_LINK_LIBRARIES(iris_lua_demo ${LUALIB})
ELSE (MSVC)
TARGET_LINK_LIBRARIES(iris_lua_demo m ${STDLIB} ${LUALIB})
ENDIF (MSVC)
ENDIF (ENABLE_LUA)
66 changes: 66 additions & 0 deletions cmake/FindLuaJIT.cmake
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
# This file is from lua-cbson library: https://github.com/isage/lua-cbson/blob/master/cmake/FindLuaJIT.cmake
# Locate LuaJIT library
# This module defines
# LUAJIT_FOUND, if false, do not try to link to Lua
# LUA_LIBRARIES
# LUA_INCLUDE_DIR, where to find lua.h
# LUAJIT_VERSION_STRING, the version of Lua found (since CMake 2.8.8)

## Copied from default CMake FindLua51.cmake

find_path(LUA_INCLUDE_DIR luajit.h
HINTS
ENV LUA_DIR
PATH_SUFFIXES include/luajit-2.0 include/luajit-2.1 include
PATHS
~/Library/Frameworks
/Library/Frameworks
/sw # Fink
/opt/local # DarwinPorts
/opt/csw # Blastwave
/opt
/usr/local/openresty/luajit/ # openresty
)

find_library(LUA_LIBRARY
NAMES luajit-5.1 luajit-5.2 lua
HINTS
ENV LUA_DIR
PATH_SUFFIXES lib
PATHS
~/Library/Frameworks
/Library/Frameworks
/sw
/opt/local
/opt/csw
/opt
/usr/local/openresty/luajit/ # openresty
)

if(LUA_LIBRARY)
# include the math library for Unix
# if(UNIX AND NOT APPLE)
# find_library(LUA_MATH_LIBRARY m)
# set( LUA_LIBRARIES "${LUA_LIBRARY};${LUA_MATH_LIBRARY}" CACHE STRING "Lua Libraries")
# For Windows and Mac, don't need to explicitly include the math library
# else()
set( LUA_LIBRARIES "${LUA_LIBRARY}" CACHE STRING "Lua Libraries")
# endif()
endif()

if(LUA_INCLUDE_DIR AND EXISTS "${LUA_INCLUDE_DIR}/luajit.h")
file(STRINGS "${LUA_INCLUDE_DIR}/luajit.h" luajit_version_str REGEX "^#define[ \t]+LUAJIT_VERSION[ \t]+\"LuaJIT .+\"")

string(REGEX REPLACE "^#define[ \t]+LUAJIT_VERSION[ \t]+\"LuaJIT ([^\"]+)\".*" "\\1" LUAJIT_VERSION_STRING "${luajit_version_str}")
unset(luajit_version_str)
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(LuaJIT
REQUIRED_VARS LUA_LIBRARIES LUA_INCLUDE_DIR
VERSION_VAR LUAJIT_VERSION_STRING)

mark_as_advanced(LUA_INCLUDE_DIR LUA_LIBRARIES LUA_LIBRARY LUA_MATH_LIBRARY)

10 changes: 8 additions & 2 deletions src/optional/iris_lua.h
Original file line number Diff line number Diff line change
Expand Up @@ -33,9 +33,15 @@ SOFTWARE.
#include <array>

extern "C" {
#if !USE_LUA_LIBRARY
#include "lua/src/lua.h"
#include "lua/src/lualib.h"
#include "lua/src/lauxlib.h"
#else
#include "lua.h"
#include "lualib.h"
#include "lauxlib.h"
#endif
}

// compatible with old lua versions
Expand Down Expand Up @@ -312,7 +318,7 @@ namespace iris {

// hash code is to check types when passing as a argument to C++
push_variable(L, "__hash");
push_variable(L, reinterpret_cast<void*>(typeid(type_t).hash_code()));
push_variable(L, reinterpret_cast<void*>(typeid(type_t).hash_code() & 0xffffffff)); // avoid luajit checks
lua_rawset(L, -3);

// readable name
Expand Down Expand Up @@ -675,7 +681,7 @@ namespace iris {

// returns empty if hashes are not equal!
static const size_t hash_code = typeid(std::remove_volatile_t<std::remove_const_t<std::remove_reference_t<std::remove_pointer_t<value_t>>>>).hash_code();
if (lua_touserdata(L, -1) != reinterpret_cast<void*>(hash_code)) {
if (lua_touserdata(L, -1) != reinterpret_cast<void*>(hash_code & 0xffffffff)) {
lua_pop(L, 2);
return value_t();
}
Expand Down
1 change: 1 addition & 0 deletions test/iris_lua_demo.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -187,6 +187,7 @@ int main(void) {
#endif

bool ret = lua.run<bool>("\
print(_VERSION)\n\
local a = example_t.create()\n\
local b = example_t.create()\n\
b:join_value_required(a)\n\
Expand Down

0 comments on commit 114481d

Please sign in to comment.