]> git.lizzy.rs Git - dragonfireclient.git/commitdiff
Compile Lua as C++ (#11683)
authorJude Melton-Houghton <jwmhjwmh@gmail.com>
Thu, 7 Apr 2022 14:54:17 +0000 (10:54 -0400)
committerGitHub <noreply@github.com>
Thu, 7 Apr 2022 14:54:17 +0000 (15:54 +0100)
Co-authored-by: sfan5 <sfan5@live.de>
lib/lua/CMakeLists.txt
lib/lua/src/CMakeLists.txt
lib/lua/src/luaconf.h
src/unittest/CMakeLists.txt
src/unittest/test_lua.cpp [new file with mode: 0644]

index 5d0dc0f708d711704b6322bd5009b9043d0bf793..2de4840cb20ad76b59a0f739b5f2e5e6eec2e522 100644 (file)
@@ -1,12 +1,10 @@
-project(lua C)
+project(lua CXX)
 
 set(LUA_VERSION_MAJOR 5)
 set(LUA_VERSION_MINOR 1)
 set(LUA_VERSION_PATCH 4)
 set(LUA_VERSION "${LUA_VERSION_MAJOR}.${LUA_VERSION_MINOR}.${LUA_VERSION_PATCH}")
 
-set(CMAKE_MODULE_PATH ${CMAKE_CURRENT_SOURCE_DIR}/cmake)
-
 set(COMMON_CFLAGS)
 set(COMMON_LDFLAGS)
 set(LIBS)
@@ -50,19 +48,12 @@ if(LUA_ANSI)
        set(COMMON_CFLAGS "${COMMON_CFLAGS} -DLUA_ANSI")
 endif(LUA_ANSI)
 
-# COMMON_CFLAGS has no effect without this line
-set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} ${COMMON_CFLAGS}")
-
-
-# Standard flags to use for each build type.
-if(CMAKE_COMPILER_IS_GNUCC)
-       set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -pipe -Wall -Wextra -Wshadow -W -pedantic -std=gnu99")
-       set(CMAKE_C_FLAGS_RELEASE        "${CMAKE_C_FLAGS_RELEASE}     -O2")
-       set(CMAKE_C_FLAGS_DEBUG          "${CMAKE_C_FLAGS_DEBUG}       -O0 -g")
-       set(CMAKE_C_FLAGS_PROFILE        "${CMAKE_C_FLAGS_PROFILE}     -O1 -g")
-       set(CMAKE_C_FLAGS_RELWITHDEBINFO "${CMAKE_C_FLAGS_WITHDEBINFO} -O2 -g")
-endif(CMAKE_COMPILER_IS_GNUCC)
-
+# Standard flags to use
+if(CMAKE_CXX_COMPILER_ID MATCHES "GNU|(Apple)?Clang")
+       set(COMMON_CFLAGS "${COMMON_CFLAGS} -pipe -Wall -Wextra -Wshadow -W -pedantic")
+endif()
 
-add_subdirectory(src build)
+# COMMON_CFLAGS has no effect without this line
+set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} ${COMMON_CFLAGS}")
 
+add_subdirectory(src)
index 8f6cc1213d6779c2b0dbcd7c09176b30b346a214..2ca4f416854b507e52c0dae0ec7eb492e7a3fb5d 100644 (file)
@@ -31,24 +31,14 @@ set(LUA_CORE_SRC
        lvm.c
        lzio.c
 )
-set(LUA_LIB_HEADERS
-       lua.h
-       lualib.h
-       lauxlib.h
-       luaconf.h
-)
-
-include_directories(${CMAKE_CURRENT_SOURCE_DIR}
-               ${CMAKE_CURRENT_BINARY_DIR})
 
-# Lua library.
+# Lua library
 add_library(lua STATIC ${LUA_CORE_SRC})
 target_link_libraries(lua ${LIBS})
-set(LUA_STATIC_LIB lua)
-set(LUA_LIBS lua)
-
-set_target_properties(${LUA_LIBS} PROPERTIES
+set_target_properties(lua PROPERTIES
        VERSION ${LUA_VERSION}
        CLEAN_DIRECT_OUTPUT 1
 )
 
+# Compile code as C++
+set_source_files_properties(${LUA_CORE_SRC} PROPERTIES LANGUAGE CXX)
index e2cb26163a2320cd5e27c89ba1e32cedbad41049..1521f0cbc573774658591dadf797e9d37c7f8654 100644 (file)
 #define LUA_INTEGER    ptrdiff_t
 
 
+/* MINETEST-SPECIFIC CHANGE: make sure API functions conform to the C ABI. */
+#if defined(__cplusplus)
+#define LUAI_API_EXTERN extern "C"
+#else
+#define LUAI_API_EXTERN extern
+#endif
+
+
 /*
 @@ LUA_API is a mark for all core API functions.
 @@ LUALIB_API is a mark for all standard library functions.
 #if defined(LUA_BUILD_AS_DLL)
 
 #if defined(LUA_CORE) || defined(LUA_LIB)
-#define LUA_API __declspec(dllexport)
+#define LUA_API LUAI_API_EXTERN __declspec(dllexport)
 #else
-#define LUA_API __declspec(dllimport)
+#define LUA_API LUAI_API_EXTERN __declspec(dllimport)
 #endif
 
 #else
 
-#define LUA_API                extern
+#define LUA_API                LUAI_API_EXTERN
 
 #endif
 
index 92f31ecaca942234769ff6d767ab4e324c781fa8..84f769e879c6fcfff48ac72758867c4cf5f933b7 100644 (file)
@@ -11,6 +11,7 @@ set (UNITTEST_SRCS
        ${CMAKE_CURRENT_SOURCE_DIR}/test_filepath.cpp
        ${CMAKE_CURRENT_SOURCE_DIR}/test_inventory.cpp
        ${CMAKE_CURRENT_SOURCE_DIR}/test_irrptr.cpp
+       ${CMAKE_CURRENT_SOURCE_DIR}/test_lua.cpp
        ${CMAKE_CURRENT_SOURCE_DIR}/test_map.cpp
        ${CMAKE_CURRENT_SOURCE_DIR}/test_map_settings_manager.cpp
        ${CMAKE_CURRENT_SOURCE_DIR}/test_mapnode.cpp
diff --git a/src/unittest/test_lua.cpp b/src/unittest/test_lua.cpp
new file mode 100644 (file)
index 0000000..fc8f895
--- /dev/null
@@ -0,0 +1,79 @@
+/*
+Minetest
+Copyright (C) 2013 celeron55, Perttu Ahola <celeron55@gmail.com>
+Copyright (C) 2021 TurkeyMcMac, Jude Melton-Houghton <jwmhjwmh@gmail.com>
+
+This program is free software; you can redistribute it and/or modify
+it under the terms of the GNU Lesser General Public License as published by
+the Free Software Foundation; either version 2.1 of the License, or
+(at your option) any later version.
+
+This program is distributed in the hope that it will be useful,
+but WITHOUT ANY WARRANTY; without even the implied warranty of
+MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+GNU Lesser General Public License for more details.
+
+You should have received a copy of the GNU Lesser General Public License along
+with this program; if not, write to the Free Software Foundation, Inc.,
+51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
+*/
+
+#include "test.h"
+
+extern "C" {
+#include <lua.h>
+#include <lauxlib.h>
+}
+
+class TestLua : public TestBase
+{
+public:
+       TestLua() { TestManager::registerTestModule(this); }
+       const char *getName() { return "TestLua"; }
+
+       void runTests(IGameDef *gamedef);
+
+       void testLuaDestructors();
+};
+
+static TestLua g_test_instance;
+
+void TestLua::runTests(IGameDef *gamedef)
+{
+       TEST(testLuaDestructors);
+}
+
+////////////////////////////////////////////////////////////////////////////////
+
+namespace
+{
+
+       class DestructorDetector {
+               bool *did_destruct;
+       public:
+               DestructorDetector(bool *did_destruct) : did_destruct(did_destruct)
+               {
+                       *did_destruct = false;
+               }
+               ~DestructorDetector()
+               {
+                       *did_destruct = true;
+               }
+       };
+
+}
+
+void TestLua::testLuaDestructors()
+{
+       bool did_destruct = false;
+
+       lua_State *L = luaL_newstate();
+       lua_cpcall(L, [](lua_State *L) -> int {
+               DestructorDetector d(reinterpret_cast<bool*>(lua_touserdata(L, 1)));
+               luaL_error(L, "error");
+               return 0;
+       }, &did_destruct);
+       lua_close(L);
+
+       UASSERT(did_destruct);
+}