Skip to content

Commit 0c0d080

Browse files
committed
build(lua): adds options to compile Lua as C++
Introduces an option to compile the Lua source files as C++ instead of C. For CMake: /D_LUA_COMPILE_AS_CPP For xmake: --lua_compile_as_cpp=true This allows users to choose between C and C++ compilation. The change also includes necessary preprocessor definitions and header adjustments to handle the different compilation modes correctly.
1 parent 84e1927 commit 0c0d080

File tree

3 files changed

+42
-11
lines changed

3 files changed

+42
-11
lines changed

deps/first/LuaRaw/CMakeLists.txt

Lines changed: 13 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ set(TARGET LuaRaw)
44
project(${TARGET})
55

66
option(UE4SS_${TARGET}_BUILD_SHARED "Build as a shared lib" OFF)
7+
option(LUA_COMPILE_AS_CPP "Compile Lua as C++ instead of C" OFF)
78

89
set(${TARGET}_Sources
910
"${CMAKE_CURRENT_SOURCE_DIR}/src/lapi.c"
@@ -45,8 +46,12 @@ set(${TARGET}_Sources
4546

4647
# This makes the .c files be compiled with the C++ compiler
4748
# This causes __cplusplus to be defined which is what Lua uses to determine whether it's compiled as C or C++
48-
# TODO: Decide if we want to compile Lua as C++ rather than C
49-
# set_source_files_properties(${${TARGET}_Sources} PROPERTIES LANGUAGE CXX)
49+
if(LUA_COMPILE_AS_CPP)
50+
message("Compiling Lua as C++")
51+
set_source_files_properties(${${TARGET}_Sources} PROPERTIES LANGUAGE CXX)
52+
else()
53+
message("Compiling Lua as C")
54+
endif()
5055

5156
string(REGEX REPLACE "(.)([A-Z])" "\\1_\\2" MODULE_NAME ${TARGET})
5257
string(TOUPPER ${MODULE_NAME} MODULE_NAME)
@@ -64,10 +69,13 @@ target_compile_features(${TARGET} PUBLIC cxx_std_23)
6469

6570
target_include_directories(${TARGET} PUBLIC ${CMAKE_CURRENT_SOURCE_DIR}/include)
6671

67-
target_compile_definitions(${TARGET} PRIVATE
72+
target_compile_definitions(${TARGET}
73+
PRIVATE
6874
RC_${MODULE_NAME}_EXPORTS
75+
PUBLIC
6976
$<$<NOT:$<BOOL:${UE4SS_${TARGET}_BUILD_SHARED}>>:
70-
RC_${MODULE_NAME}_BUILD_STATIC>)
77+
RC_${MODULE_NAME}_BUILD_STATIC>
78+
$<$<BOOL:${LUA_COMPILE_AS_CPP}>:LUA_COMPILED_AS_CPP>)
7179

7280
# Make headers visible in the IDE
7381
# Uses make_headers_visible() from cmake/modules/IDEVisibility.cmake
@@ -76,5 +84,5 @@ make_headers_visible(${TARGET} "${CMAKE_CURRENT_SOURCE_DIR}/include")
7684
if (${CMAKE_CXX_COMPILER_ID} STREQUAL MSVC)
7785
target_compile_options(${TARGET} PRIVATE /W0)
7886
elseif (${CMAKE_CXX_COMPILER_ID} STREQUAL Clang)
79-
target_compile_options(${TARGET} PRIVATE -xc++;-Wno-deprecated-enum-enum-conversion)
87+
target_compile_options(${TARGET} PRIVATE -Wno-deprecated-enum-enum-conversion)
8088
endif ()

deps/first/LuaRaw/include/lua.hpp

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,14 @@
22
// Lua header files for C++
33
// <<extern "C">> not supplied automatically because Lua also compiles as C++
44

5-
5+
// When Lua is compiled as C, we need extern "C" wrappers
6+
// When Lua is compiled as C++, we don't want them
7+
#ifndef LUA_COMPILED_AS_CPP
68
extern "C" {
7-
#include "lauxlib.h"
8-
#include "lua.h"
9-
#include "lualib.h"
9+
#endif
10+
#include "lua.h"
11+
#include "lualib.h"
12+
#include "lauxlib.h"
13+
#ifndef LUA_COMPILED_AS_CPP
1014
}
15+
#endif

deps/first/LuaRaw/xmake.lua

Lines changed: 20 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,9 @@
1+
option("lua_compile_as_cpp")
2+
set_default(false)
3+
set_showmenu(true)
4+
set_description("Compile Lua source files as C++ instead of C")
5+
set_category("Lua Options")
6+
17
local projectName = "LuaRaw"
28

39
target(projectName)
@@ -7,8 +13,9 @@ target(projectName)
713
add_rules("ue4ss.dependency")
814
add_includedirs("include", { public = true })
915
add_headerfiles("include/**.hpp")
16+
add_options("lua_compile_as_cpp")
1017

11-
add_files(
18+
local lua_sources = {
1219
"src/lapi.c", "src/lauxlib.c", "src/lbaselib.c", "src/lcode.c",
1320
"src/lcorolib.c", "src/lctype.c", "src/ldblib.c", "src/ldebug.c",
1421
"src/ldo.c", "src/ldump.c", "src/lfunc.c", "src/lgc.c",
@@ -18,7 +25,18 @@ target(projectName)
1825
"src/lstrlib.c", "src/ltable.c", "src/ltablib.c", "src/ltm.c",
1926
"src/luauser.c", "src/lundump.c", "src/lutf8lib.c", "src/lvm.c",
2027
"src/lzio.c"
21-
)
28+
}
2229

30+
local compile_as_cpp = get_config("lua_compile_as_cpp")
31+
if compile_as_cpp == nil then compile_as_cpp = false end
32+
33+
if compile_as_cpp then
34+
add_defines("LUA_COMPILED_AS_CPP", { public = true })
35+
-- Force compilation as C++
36+
add_files(lua_sources, {sourcekind = "cxx"})
37+
else
38+
-- Compile as C
39+
add_files(lua_sources)
40+
end
2341

2442

0 commit comments

Comments
 (0)