Skip to content

Commit

Permalink
Add Eluna LUA Engine
Browse files Browse the repository at this point in the history
Compiles but has features disabled. Use at own risk!
  • Loading branch information
Niam5 authored and Niam5 committed Sep 26, 2024
1 parent eb09ff1 commit 3dceb20
Show file tree
Hide file tree
Showing 44 changed files with 1,468 additions and 8 deletions.
4 changes: 4 additions & 0 deletions .gitmodules
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
[submodule "src/server/game/LuaEngine"]
path = src/server/game/LuaEngine
url = https://github.com/ElunaLuaEngine/Eluna.git
branch = tc-retail
3 changes: 2 additions & 1 deletion cmake/options.cmake
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ foreach(SCRIPT_MODULE ${SCRIPT_MODULE_LIST})
endforeach()

option(TOOLS "Build map/vmap/mmap extraction/assembler tools" 1)
option(ELUNA "Build Eluna Lua Engine" 1)
option(USE_SCRIPTPCH "Use precompiled headers when compiling scripts" 1)
option(USE_COREPCH "Use precompiled headers when compiling servers" 1)
option(WITH_DYNAMIC_LINKING "Enable dynamic library linking." 0)
Expand All @@ -47,7 +48,7 @@ if(WITH_DYNAMIC_LINKING OR WITH_DYNAMIC_LINKING_FORCED)
else()
set(BUILD_SHARED_LIBS OFF)
endif()
if(WITH_FILESYSTEM_WATCHER OR BUILD_SHARED_LIBS)
if(WITH_FILESYSTEM_WATCHER OR BUILD_SHARED_LIBS OR ELUNA)
set(BUILD_EFSW ON)
endif()
option(WITH_WARNINGS "Show all warnings during compile" 0)
Expand Down
9 changes: 9 additions & 0 deletions cmake/showoptions.cmake
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,15 @@ else()
message("* Build with scripts : No")
endif()

if(ELUNA)
message("* Build Eluna LuaEngine : Yes (default)")
add_definitions(-DELUNA)
add_definitions(-DELUNA_TRINITY)
add_definitions(-DELUNA_EXPANSION=9)
else()
message("* Build Eluna LuaEngine : No")
endif()

if(TOOLS)
message("* Build map/vmap tools : Yes (default)")
else()
Expand Down
4 changes: 4 additions & 0 deletions dep/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -50,3 +50,7 @@ if(BUILD_TESTING)
GIT_SHALLOW 1)
FetchContent_MakeAvailable(Catch2)
endif()

if (ELUNA)
add_subdirectory(lualib)
endif()
21 changes: 21 additions & 0 deletions dep/lualib/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
set(LUA_VERSION "lua52" CACHE STRING "Lua version to use")
set_property(CACHE LUA_VERSION PROPERTY STRINGS luajit lua51 lua52 lua53 lua54)
MESSAGE(STATUS "Lua version: ${LUA_VERSION}")

# Avoid warning about DOWNLOAD_EXTRACT_TIMESTAMP in CMake 3.24:
if (CMAKE_VERSION VERSION_GREATER_EQUAL "3.24.0")
cmake_policy(SET CMP0135 NEW)
endif()

option(LUA_STATIC "link lua statically" OFF)
if (LUA_STATIC)
MESSAGE(STATUS "Lua linking: static")
else()
MESSAGE(STATUS "Lua linking: dynamic")
endif()

if (LUA_VERSION MATCHES "luajit")
add_subdirectory(luajit)
else()
add_subdirectory(lua)
endif()
151 changes: 151 additions & 0 deletions dep/lualib/lua/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,151 @@
# BSD-3-Clause
# Copyright (c) 2022, Rochet2 <rochet2@post.com>
# All rights reserved.
#
# Redistribution and use in source and binary forms, with or without
# modification, are permitted provided that the following conditions are met:
#
# * Redistributions of source code must retain the above copyright notice, this
# list of conditions and the following disclaimer.
#
# * Redistributions in binary form must reproduce the above copyright notice,
# this list of conditions and the following disclaimer in the documentation
# and/or other materials provided with the distribution.
#
# * Neither the name of the copyright holder nor the names of its
# contributors may be used to endorse or promote products derived from
# this software without specific prior written permission.
#
# THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
# AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
# IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
# DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
# FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
# DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
# SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
# CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
# OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
# OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.

project ( lua C )

# LUA_VERSION must be one of lua51, lua52, lua53, lua54

include(FetchContent)
FetchContent_Declare(
lua51
URL https://www.lua.org/ftp/lua-5.1.5.tar.gz
URL_HASH SHA256=2640fc56a795f29d28ef15e13c34a47e223960b0240e8cb0a82d9b0738695333
)
FetchContent_Declare(
lua52
URL https://www.lua.org/ftp/lua-5.2.4.tar.gz
URL_HASH SHA256=b9e2e4aad6789b3b63a056d442f7b39f0ecfca3ae0f1fc0ae4e9614401b69f4b
)
FetchContent_Declare(
lua53
URL https://www.lua.org/ftp/lua-5.3.6.tar.gz
URL_HASH SHA256=fc5fd69bb8736323f026672b1b7235da613d7177e72558893a0bdcd320466d60
)
FetchContent_Declare(
lua54
URL https://www.lua.org/ftp/lua-5.4.4.tar.gz
URL_HASH SHA256=164c7849653b80ae67bec4b7473b884bf5cc8d2dca05653475ec2ed27b9ebf61
)
FetchContent_MakeAvailable(${LUA_VERSION})

# Easen warnings
string(REGEX REPLACE "( |^)/W[0-9]( |$)" "\\1/W2\\2" CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS}")
string(REGEX REPLACE "( |^)/W[0-9]( |$)" "\\1/W2\\2" CMAKE_C_FLAGS "${CMAKE_C_FLAGS}")

set(LUA_SOURCE_FOLDER "${${LUA_VERSION}_SOURCE_DIR}/src")

file(GLOB LOCAL_SOURCES_H ${LUA_SOURCE_FOLDER}/*.h)
file(GLOB LOCAL_SOURCES_C ${LUA_SOURCE_FOLDER}/*.c)
# Compile lua as C++ so it uses exceptions instead of longjmp
# Disabled for now as some libraries expect lua to be C
# set_source_files_properties(${LOCAL_SOURCES_H} ${LOCAL_SOURCES_C} PROPERTIES LANGUAGE CXX )
list(REMOVE_ITEM LOCAL_SOURCES_C ${LUA_SOURCE_FOLDER}/lua.c)
list(REMOVE_ITEM LOCAL_SOURCES_C ${LUA_SOURCE_FOLDER}/luac.c)

if (LUA_STATIC)
add_library(lualib STATIC ${LOCAL_SOURCES_H} ${LOCAL_SOURCES_C})
set_property(TARGET lualib PROPERTY POSITION_INDEPENDENT_CODE ON)
else()
add_library(lualib SHARED ${LOCAL_SOURCES_H} ${LOCAL_SOURCES_C})
set_property(TARGET lualib PROPERTY POSITION_INDEPENDENT_CODE ON)
endif()
add_library(lualib_static STATIC ${LOCAL_SOURCES_H} ${LOCAL_SOURCES_C})
set_target_properties(lualib PROPERTIES LINKER_LANGUAGE C)
set_target_properties(lualib_static PROPERTIES LINKER_LANGUAGE C)
target_include_directories(lualib PUBLIC "${LUA_SOURCE_FOLDER}" "${CMAKE_CURRENT_SOURCE_DIR}")
target_include_directories(lualib_static PUBLIC "${LUA_SOURCE_FOLDER}" "${CMAKE_CURRENT_SOURCE_DIR}")
if (WIN32)
set_target_properties(lualib PROPERTIES OUTPUT_NAME ${LUA_VERSION})
install(TARGETS lualib DESTINATION "${CMAKE_INSTALL_PREFIX}")
if (NOT LUA_STATIC)
install(FILES $<TARGET_PDB_FILE:lualib> DESTINATION "${CMAKE_INSTALL_PREFIX}" OPTIONAL)
endif()
# set_target_properties(lualib PROPERTIES PUBLIC_HEADER "${LOCAL_SOURCES_H};${CMAKE_CURRENT_SOURCE_DIR}/lua.hpp")
# install(TARGETS lualib PUBLIC_HEADER DESTINATION "${CMAKE_INSTALL_PREFIX}/include")
# set_target_properties(lualib_static PROPERTIES OUTPUT_NAME ${LUA_VERSION}_static)
# install(TARGETS lualib_static DESTINATION "${CMAKE_INSTALL_PREFIX}")
else()
set_target_properties(lualib PROPERTIES PUBLIC_HEADER "${LOCAL_SOURCES_H};${CMAKE_CURRENT_SOURCE_DIR}/lua.hpp")
install(TARGETS lualib
DESTINATION "${CMAKE_INSTALL_PREFIX}/lib"
PUBLIC_HEADER DESTINATION "${CMAKE_INSTALL_PREFIX}/include"
)
install(TARGETS lualib_static DESTINATION "${CMAKE_INSTALL_PREFIX}/lib")
endif()
if (WIN32)
target_compile_definitions(lualib PRIVATE _CRT_SECURE_NO_WARNINGS)
target_compile_definitions(lualib_static PRIVATE _CRT_SECURE_NO_WARNINGS)
if (NOT LUA_STATIC)
target_compile_definitions(lualib PRIVATE LUA_BUILD_AS_DLL)
endif()
elseif (APPLE)
target_compile_definitions(lualib PUBLIC LUA_USE_MACOSX)
target_compile_definitions(lualib_static PUBLIC LUA_USE_MACOSX)
target_compile_options(lualib PRIVATE -Wno-deprecated-declarations -Wno-empty-body)
target_compile_options(lualib_static PRIVATE -Wno-deprecated-declarations -Wno-empty-body)
target_link_libraries(lualib readline)
target_link_libraries(lualib_static readline)
# set_target_properties(lualib PROPERTIES COMPILE_FLAGS -undefined dynamic_lookup)
# set_target_properties(lualib_static PROPERTIES COMPILE_FLAGS -undefined dynamic_lookup)
elseif (UNIX)
target_compile_definitions(lualib PUBLIC LUA_USE_LINUX)
target_link_libraries(lualib ${CMAKE_DL_LIBS} m readline)
target_compile_definitions(lualib_static PUBLIC LUA_USE_LINUX)
target_link_libraries(lualib_static ${CMAKE_DL_LIBS} m readline)
set_target_properties(lualib PROPERTIES OUTPUT_NAME ${LUA_VERSION})
set_target_properties(lualib_static PROPERTIES OUTPUT_NAME ${LUA_VERSION})
endif()

add_executable(lua_interpreter ${LUA_SOURCE_FOLDER}/lua.c)
target_link_libraries(lua_interpreter lualib_static)
target_compile_definitions(lua_interpreter PRIVATE _CRT_SECURE_NO_WARNINGS)
set_target_properties(lua_interpreter PROPERTIES OUTPUT_NAME ${LUA_VERSION}_interpreter)
if (WIN32)
install(TARGETS lua_interpreter DESTINATION "${CMAKE_INSTALL_PREFIX}")
install(FILES $<TARGET_PDB_FILE:lua_interpreter> DESTINATION "${CMAKE_INSTALL_PREFIX}" OPTIONAL)
else()
install(TARGETS lua_interpreter DESTINATION "${CMAKE_INSTALL_PREFIX}/bin")
endif()
#install(TARGETS lualib
# DESTINATION "${CMAKE_INSTALL_PREFIX}"
# LIBRARY DESTINATION "${CMAKE_INSTALL_PREFIX}/lua/lib"
# RUNTIME DESTINATION "${CMAKE_INSTALL_PREFIX}/lua/lib"
# ARCHIVE DESTINATION "${CMAKE_INSTALL_PREFIX}/lua/lib"
#)

add_executable(lua_compiler ${LUA_SOURCE_FOLDER}/luac.c)
target_link_libraries(lua_compiler lualib_static)
target_compile_definitions(lua_compiler PRIVATE _CRT_SECURE_NO_WARNINGS)
set_target_properties(lua_compiler PROPERTIES OUTPUT_NAME ${LUA_VERSION}_compiler)
if (WIN32)
install(TARGETS lua_compiler DESTINATION "${CMAKE_INSTALL_PREFIX}")
install(FILES $<TARGET_PDB_FILE:lua_compiler> DESTINATION "${CMAKE_INSTALL_PREFIX}" OPTIONAL)
else()
install(TARGETS lua_compiler DESTINATION "${CMAKE_INSTALL_PREFIX}/bin")
endif()
10 changes: 10 additions & 0 deletions dep/lualib/lua/lua.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
// Some lua sources include this file and some do not,
// so here is the file to be used with all lua sources as some libraries expect it to exist.

#pragma once

extern "C" {
#include "lua.h"
#include "lualib.h"
#include "lauxlib.h"
}
155 changes: 155 additions & 0 deletions dep/lualib/luajit/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,155 @@
# BSD-3-Clause
# Copyright (c) 2022, Rochet2 <rochet2@post.com>
# All rights reserved.
#
# Redistribution and use in source and binary forms, with or without
# modification, are permitted provided that the following conditions are met:
#
# * Redistributions of source code must retain the above copyright notice, this
# list of conditions and the following disclaimer.
#
# * Redistributions in binary form must reproduce the above copyright notice,
# this list of conditions and the following disclaimer in the documentation
# and/or other materials provided with the distribution.
#
# * Neither the name of the copyright holder nor the names of its
# contributors may be used to endorse or promote products derived from
# this software without specific prior written permission.
#
# THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
# AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
# IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
# DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
# FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
# DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
# SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
# CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
# OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
# OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.

project ( lua C )

# Download source
include(FetchContent)
FetchContent_Declare(
luajit21
GIT_REPOSITORY https://github.com/LuaJIT/LuaJIT.git
GIT_TAG a91d0d9d3bba1a936669cfac3244509a0f2ac0e3 # 2.1.0-beta3+ where mac builds without extra setup
)
FetchContent_MakeAvailable(luajit21)

set(LUA_SRC_FOLDER "${luajit21_SOURCE_DIR}")
set(LUA_BIN_FOLDER "${luajit21_BINARY_DIR}")
# note that the / at the end means that we copy folder contents, not the folder itself
file(COPY ${LUA_SRC_FOLDER}/ DESTINATION ${LUA_BIN_FOLDER})

if (WIN32)
if (LUA_STATIC)
# build luajit static
add_custom_command(
OUTPUT ${LUA_BIN_FOLDER}/src/lua51.lib ${LUA_BIN_FOLDER}/src/luajit.exe
WORKING_DIRECTORY ${LUA_BIN_FOLDER}/src
COMMAND call msvcbuild.bat static
)

# add it as a library target
add_custom_target(luajit_target DEPENDS ${LUA_BIN_FOLDER}/src/lua51.lib)
add_library(lualib STATIC IMPORTED GLOBAL)
add_dependencies(lualib luajit_target)
set_target_properties(lualib
PROPERTIES
IMPORTED_LOCATION ${LUA_BIN_FOLDER}/src/lua51.lib
INTERFACE_INCLUDE_DIRECTORIES "${LUA_SRC_FOLDER}/src"
)

# install generated files
install(FILES ${LUA_BIN_FOLDER}/src/lua51.lib ${LUA_BIN_FOLDER}/src/luajit.exe DESTINATION "${CMAKE_INSTALL_PREFIX}")
install(DIRECTORY ${LUA_BIN_FOLDER}/src/jit DESTINATION "${CMAKE_INSTALL_PREFIX}/lua")
else()
# build luajit dll
add_custom_command(
OUTPUT ${LUA_BIN_FOLDER}/src/lua51.dll ${LUA_BIN_FOLDER}/src/lua51.lib ${LUA_BIN_FOLDER}/src/luajit.exe
WORKING_DIRECTORY ${LUA_BIN_FOLDER}/src
# COMMAND echo luajit built on platform $(Platform)
# COMMAND cd $(VSInstallDir)/VC
# COMMAND if \"$(Platform)\"==\"Win32\" echo \"luajit building $(Platform)\" & call $(VSInstallDir)/VC/vcvarsall.bat $(Platform) & call msvcbuild.bat
# COMMAND if \"$(Platform)\"==\"Win32\" echo \"luajit building 64bit\" & call $(VSInstallDir)/VC/vcvarsall.bat x64
# COMMAND if \"$(Platform)\"==\"Win64\" echo luajit building 64bit
# COMMAND if \"$(Platform)\"==\"Win64\" call $(VSInstallDir)/VC/vcvarsall.bat x64
# COMMAND cd ${LUA_BIN_FOLDER}/src
COMMAND call msvcbuild.bat
# COMMAND ${CMAKE_COMMAND} -E copy ${LUA_BIN_FOLDER}/src/lua51.dll ${CMAKE_BINARY_DIR}/$(ConfigurationName)/
# COMMAND ${CMAKE_COMMAND} -E copy ${LUA_BIN_FOLDER}/src/lua51.lib ${CMAKE_BINARY_DIR}/$(ConfigurationName)/
# COMMAND ${CMAKE_COMMAND} -E copy ${LUA_BIN_FOLDER}/src/luajit.exe ${CMAKE_BINARY_DIR}/$(ConfigurationName)/
)

# add it as a library target
add_custom_target(luajit_target DEPENDS ${LUA_BIN_FOLDER}/src/lua51.lib)
add_library(lualib SHARED IMPORTED GLOBAL)
add_dependencies(lualib luajit_target)
set_target_properties(lualib
PROPERTIES
IMPORTED_LOCATION ${LUA_BIN_FOLDER}/src/lua51.dll
IMPORTED_IMPLIB ${LUA_BIN_FOLDER}/src/lua51.lib
INTERFACE_INCLUDE_DIRECTORIES "${LUA_SRC_FOLDER}/src"
)

# install generated files
install(FILES ${LUA_BIN_FOLDER}/src/lua51.dll ${LUA_BIN_FOLDER}/src/lua51.lib ${LUA_BIN_FOLDER}/src/luajit.exe DESTINATION "${CMAKE_INSTALL_PREFIX}")
install(DIRECTORY ${LUA_BIN_FOLDER}/src/jit DESTINATION "${CMAKE_INSTALL_PREFIX}/lua")
endif()
endif()

if (UNIX OR APPLE)
#option(LUA_USR "Use /usr/local/ as lua library location" OFF)
#if (LUA_USR)
# set(LUA_INSTALL_PATH "/usr/local")
#else()
set(LUA_INSTALL_PATH "${CMAKE_CURRENT_BINARY_DIR}/BIN")
#endif()

if (LUA_STATIC)
set(LUAJIT_LIB_PATH "${LUA_INSTALL_PATH}/lib/libluajit-5.1.a")
else()
if (APPLE)
set(LUAJIT_LIB_PATH "${LUA_INSTALL_PATH}/lib/libluajit-5.1.2.1.0.dylib")
elseif(UNIX)
set(LUAJIT_LIB_PATH "${LUA_INSTALL_PATH}/lib/libluajit-5.1.so.2.1.0")
endif()
endif()

# build luajit
# if (LUA_USR)
# add_custom_command(
# OUTPUT ${LUAJIT_LIB_PATH}
# COMMAND $(MAKE) -C ${LUA_BIN_FOLDER}
# COMMAND $(MAKE) -C ${LUA_BIN_FOLDER} install
# )
# else ()
add_custom_command(
OUTPUT ${LUAJIT_LIB_PATH}
# COMMAND $(MAKE) -C ${LUA_BIN_FOLDER} PREFIX=${LUA_INSTALL_PATH}
COMMAND $(MAKE) -C ${LUA_BIN_FOLDER} install PREFIX=${LUA_INSTALL_PATH}
)
# endif()
add_custom_target(luajit_target DEPENDS ${LUAJIT_LIB_PATH})

# add it as a library target
if (LUA_STATIC)
add_library(lualib STATIC IMPORTED GLOBAL)
# on static build the libraries are not a part of the luajit archive
target_link_libraries(lualib INTERFACE ${CMAKE_DL_LIBS})
else()
add_library(lualib SHARED IMPORTED GLOBAL)
endif()
add_dependencies(lualib luajit_target)
set_target_properties(lualib
PROPERTIES
# IMPORTED_LOCATION ${LUAJIT_LIB_PATH} # cmake bullshit. spent days figuring this and turns out set_target_properties does squat shit while set_property works fine.
INTERFACE_INCLUDE_DIRECTORIES "${LUA_SRC_FOLDER}/src"
)
set_property(TARGET lualib PROPERTY IMPORTED_LOCATION ${LUAJIT_LIB_PATH})

# install generated files
install(DIRECTORY ${LUA_INSTALL_PATH}/ DESTINATION ${CMAKE_INSTALL_PREFIX})
endif()
Loading

0 comments on commit 3dceb20

Please sign in to comment.