summaryrefslogtreecommitdiff
path: root/tests/projects
diff options
context:
space:
mode:
authorruki <[email protected]>2024-04-05 16:21:00 +0800
committerGitHub <[email protected]>2024-04-05 16:21:00 +0800
commit982a307fa363ae022554df2afaa3f922c5bb0986 (patch)
tree279590bb6068b94eeade11693217b11d21e0db35 /tests/projects
parent282e468d6cf18473bac127d260fe802e7bb64392 (diff)
parent5297e2329198a683400870382699af2f37d25ec2 (diff)
Merge pull request #4932 from xmake-io/native
Improve native shared module to remove lua package dependence
Diffstat (limited to 'tests/projects')
-rw-r--r--tests/projects/other/native_module/modules/shared/foo/src/foo.c19
-rw-r--r--tests/projects/other/native_module/modules/shared/foo/xmake.lua3
-rw-r--r--tests/projects/other/native_module/test.lua4
-rw-r--r--tests/projects/other/native_module/xmake.lua12
-rw-r--r--tests/projects/other/native_module_cjson/.gitignore8
-rw-r--r--tests/projects/other/native_module_cjson/modules/lua/cjson/src/cjson.c7
-rw-r--r--tests/projects/other/native_module_cjson/modules/lua/cjson/xmake.lua19
-rw-r--r--tests/projects/other/native_module_cjson/src/main.cpp6
-rw-r--r--tests/projects/other/native_module_cjson/test.lua3
-rw-r--r--tests/projects/other/native_module_cjson/xmake.lua12
10 files changed, 70 insertions, 23 deletions
diff --git a/tests/projects/other/native_module/modules/shared/foo/src/foo.c b/tests/projects/other/native_module/modules/shared/foo/src/foo.c
index 3024f9380..0555b071f 100644
--- a/tests/projects/other/native_module/modules/shared/foo/src/foo.c
+++ b/tests/projects/other/native_module/modules/shared/foo/src/foo.c
@@ -1,6 +1,4 @@
-#include <stdlib.h>
-#include <lua.h>
-#include <lauxlib.h>
+#include <xmi.h>
static int add(lua_State* lua) {
int a = lua_tointeger(lua, 1);
@@ -16,14 +14,13 @@ static int sub(lua_State* lua) {
return 1;
}
-static const luaL_Reg g_funcs[] = {
- {"add", add},
- {"sub", sub},
- {NULL, NULL}
-};
-
-int luaopen_foo(lua_State* lua) {
+int luaopen(foo, lua_State* lua) {
+ static const luaL_Reg funcs[] = {
+ {"add", add},
+ {"sub", sub},
+ {NULL, NULL}
+ };
lua_newtable(lua);
- luaL_setfuncs(lua, g_funcs, 0);
+ luaL_setfuncs(lua, funcs, 0);
return 1;
}
diff --git a/tests/projects/other/native_module/modules/shared/foo/xmake.lua b/tests/projects/other/native_module/modules/shared/foo/xmake.lua
index ad4590ffa..035573d9f 100644
--- a/tests/projects/other/native_module/modules/shared/foo/xmake.lua
+++ b/tests/projects/other/native_module/modules/shared/foo/xmake.lua
@@ -1,9 +1,6 @@
add_rules("mode.debug", "mode.release")
-add_requires("lua 5.4")
-
target("foo")
add_rules("module.shared")
add_files("src/foo.c")
- add_packages("lua")
diff --git a/tests/projects/other/native_module/test.lua b/tests/projects/other/native_module/test.lua
index 0c2735866..b57362078 100644
--- a/tests/projects/other/native_module/test.lua
+++ b/tests/projects/other/native_module/test.lua
@@ -1,5 +1,3 @@
function main(t)
- if not xmake.luajit() then
- t:build()
- end
+ t:build()
end
diff --git a/tests/projects/other/native_module/xmake.lua b/tests/projects/other/native_module/xmake.lua
index e403454bb..211e839d3 100644
--- a/tests/projects/other/native_module/xmake.lua
+++ b/tests/projects/other/native_module/xmake.lua
@@ -6,11 +6,11 @@ target("test")
set_kind("binary")
add_files("src/*.cpp")
on_config(function (target)
- import("shared.foo")
- import("binary.bar", {always_build = true})
- print("shared: 1 + 1 = %d", foo.add(1, 1))
- print("shared: 1 - 1 = %d", foo.sub(1, 1))
- print("binary: 1 + 1 = %s", bar.add(1, 1))
- print("binary: 1 - 1 = %s", bar.sub(1, 1))
+ import("shared.foo", {always_build = true})
+ import("binary.bar")
+ print("foo: 1 + 1 = %d", foo.add(1, 1))
+ print("foo: 1 - 1 = %d", foo.sub(1, 1))
+ print("bar: 1 + 1 = %s", bar.add(1, 1))
+ print("bar: 1 - 1 = %s", bar.sub(1, 1))
end)
diff --git a/tests/projects/other/native_module_cjson/.gitignore b/tests/projects/other/native_module_cjson/.gitignore
new file mode 100644
index 000000000..152105761
--- /dev/null
+++ b/tests/projects/other/native_module_cjson/.gitignore
@@ -0,0 +1,8 @@
+# Xmake cache
+.xmake/
+build/
+
+# MacOS Cache
+.DS_Store
+
+
diff --git a/tests/projects/other/native_module_cjson/modules/lua/cjson/src/cjson.c b/tests/projects/other/native_module_cjson/modules/lua/cjson/src/cjson.c
new file mode 100644
index 000000000..d8c2e49ef
--- /dev/null
+++ b/tests/projects/other/native_module_cjson/modules/lua/cjson/src/cjson.c
@@ -0,0 +1,7 @@
+#include <xmi.h>
+
+int luaopen_cjson(lua_State* lua);
+
+int luaopen(cjson, lua_State* lua) {
+ return luaopen_cjson(lua);
+}
diff --git a/tests/projects/other/native_module_cjson/modules/lua/cjson/xmake.lua b/tests/projects/other/native_module_cjson/modules/lua/cjson/xmake.lua
new file mode 100644
index 000000000..434746541
--- /dev/null
+++ b/tests/projects/other/native_module_cjson/modules/lua/cjson/xmake.lua
@@ -0,0 +1,19 @@
+add_rules("mode.debug", "mode.release")
+
+target("cjson")
+ add_rules("module.shared")
+ set_warnings("all")
+ if is_plat("windows") then
+ set_languages("c89")
+ end
+ add_files("src/*.c")
+ add_files("../../../../../../../core/src/lua-cjson/lua-cjson/*.c|fpconv.c")
+ -- Use internal strtod() / g_fmt() code for performance and disable multi-thread
+ add_defines("NDEBUG", "USE_INTERNAL_FPCONV")
+ add_defines("XM_CONFIG_API_HAVE_LUA_CJSON")
+ if is_plat("windows") then
+ -- Windows sprintf()/strtod() handle NaN/inf differently. Not supported.
+ add_defines("DISABLE_INVALID_NUMBERS")
+ add_defines("inline=__inline")
+ end
+
diff --git a/tests/projects/other/native_module_cjson/src/main.cpp b/tests/projects/other/native_module_cjson/src/main.cpp
new file mode 100644
index 000000000..7c775d288
--- /dev/null
+++ b/tests/projects/other/native_module_cjson/src/main.cpp
@@ -0,0 +1,6 @@
+#include <iostream>
+
+int main(int argc, char** argv) {
+ std::cout << "hello world!" << std::endl;
+ return 0;
+}
diff --git a/tests/projects/other/native_module_cjson/test.lua b/tests/projects/other/native_module_cjson/test.lua
new file mode 100644
index 000000000..b57362078
--- /dev/null
+++ b/tests/projects/other/native_module_cjson/test.lua
@@ -0,0 +1,3 @@
+function main(t)
+ t:build()
+end
diff --git a/tests/projects/other/native_module_cjson/xmake.lua b/tests/projects/other/native_module_cjson/xmake.lua
new file mode 100644
index 000000000..8768aca3e
--- /dev/null
+++ b/tests/projects/other/native_module_cjson/xmake.lua
@@ -0,0 +1,12 @@
+add_rules("mode.debug", "mode.release")
+
+add_moduledirs("modules")
+
+target("test")
+ set_kind("binary")
+ add_files("src/*.cpp")
+ on_config(function (target)
+ import("lua.cjson", {always_build = true})
+ print(cjson.decode('{"foo": 1, "bar": [1, 2, 3]}'))
+ end)
+