From befc1ba876323c2df0030b9828bcdfad93b4a092 Mon Sep 17 00:00:00 2001 From: ruki Date: Wed, 3 Apr 2024 22:54:03 +0800 Subject: add module.shared template --- xmake/templates/c++/module.shared/.gitignore | 8 ++++++ .../project/modules/shared/foo/src/foo.cpp | 33 ++++++++++++++++++++++ .../project/modules/shared/foo/xmake.lua | 9 ++++++ .../c++/module.shared/project/src/main.cpp | 6 ++++ .../templates/c++/module.shared/project/xmake.lua | 15 ++++++++++ xmake/templates/c++/module.shared/template.lua | 2 ++ xmake/templates/c/module.shared/.gitignore | 8 ++++++ .../project/modules/shared/foo/src/foo.c | 29 +++++++++++++++++++ .../project/modules/shared/foo/xmake.lua | 9 ++++++ xmake/templates/c/module.shared/project/src/main.c | 6 ++++ xmake/templates/c/module.shared/project/xmake.lua | 15 ++++++++++ xmake/templates/c/module.shared/template.lua | 2 ++ 12 files changed, 142 insertions(+) create mode 100644 xmake/templates/c++/module.shared/.gitignore create mode 100644 xmake/templates/c++/module.shared/project/modules/shared/foo/src/foo.cpp create mode 100644 xmake/templates/c++/module.shared/project/modules/shared/foo/xmake.lua create mode 100644 xmake/templates/c++/module.shared/project/src/main.cpp create mode 100644 xmake/templates/c++/module.shared/project/xmake.lua create mode 100644 xmake/templates/c++/module.shared/template.lua create mode 100644 xmake/templates/c/module.shared/.gitignore create mode 100644 xmake/templates/c/module.shared/project/modules/shared/foo/src/foo.c create mode 100644 xmake/templates/c/module.shared/project/modules/shared/foo/xmake.lua create mode 100644 xmake/templates/c/module.shared/project/src/main.c create mode 100644 xmake/templates/c/module.shared/project/xmake.lua create mode 100644 xmake/templates/c/module.shared/template.lua diff --git a/xmake/templates/c++/module.shared/.gitignore b/xmake/templates/c++/module.shared/.gitignore new file mode 100644 index 000000000..152105761 --- /dev/null +++ b/xmake/templates/c++/module.shared/.gitignore @@ -0,0 +1,8 @@ +# Xmake cache +.xmake/ +build/ + +# MacOS Cache +.DS_Store + + diff --git a/xmake/templates/c++/module.shared/project/modules/shared/foo/src/foo.cpp b/xmake/templates/c++/module.shared/project/modules/shared/foo/src/foo.cpp new file mode 100644 index 000000000..b6eb451ba --- /dev/null +++ b/xmake/templates/c++/module.shared/project/modules/shared/foo/src/foo.cpp @@ -0,0 +1,33 @@ +#include +extern "C" { +#include +#include +} + +static int add(lua_State* lua) { + int a = lua_tointeger(lua, 1); + int b = lua_tointeger(lua, 2); + lua_pushinteger(lua, a + b); + return 1; +} + +static int sub(lua_State* lua) { + int a = lua_tointeger(lua, 1); + int b = lua_tointeger(lua, 2); + lua_pushinteger(lua, a - b); + return 1; +} + +static const luaL_Reg g_funcs[] = { + {"add", add}, + {"sub", sub}, + {NULL, NULL} +}; + +extern "C" { + int luaopen_foo(lua_State* lua) { + lua_newtable(lua); + luaL_setfuncs(lua, g_funcs, 0); + return 1; + } +} diff --git a/xmake/templates/c++/module.shared/project/modules/shared/foo/xmake.lua b/xmake/templates/c++/module.shared/project/modules/shared/foo/xmake.lua new file mode 100644 index 000000000..deef51363 --- /dev/null +++ b/xmake/templates/c++/module.shared/project/modules/shared/foo/xmake.lua @@ -0,0 +1,9 @@ +add_rules("mode.debug", "mode.release") + +add_requires("lua 5.4") + +target("foo") + add_rules("module.shared") + add_files("src/foo.cpp") + add_packages("lua") + diff --git a/xmake/templates/c++/module.shared/project/src/main.cpp b/xmake/templates/c++/module.shared/project/src/main.cpp new file mode 100644 index 000000000..7c775d288 --- /dev/null +++ b/xmake/templates/c++/module.shared/project/src/main.cpp @@ -0,0 +1,6 @@ +#include + +int main(int argc, char** argv) { + std::cout << "hello world!" << std::endl; + return 0; +} diff --git a/xmake/templates/c++/module.shared/project/xmake.lua b/xmake/templates/c++/module.shared/project/xmake.lua new file mode 100644 index 000000000..34496aa5b --- /dev/null +++ b/xmake/templates/c++/module.shared/project/xmake.lua @@ -0,0 +1,15 @@ +add_rules("mode.debug", "mode.release") + +add_moduledirs("modules") + +target("${TARGETNAME}") + set_kind("binary") + add_files("src/*.cpp") + on_config(function (target) + import("shared.foo") + print("shared: 1 + 1 = %s", foo.add(1, 1)) + print("shared: 1 - 1 = %s", foo.sub(1, 1)) + end) + +${FAQ} + diff --git a/xmake/templates/c++/module.shared/template.lua b/xmake/templates/c++/module.shared/template.lua new file mode 100644 index 000000000..7869165b7 --- /dev/null +++ b/xmake/templates/c++/module.shared/template.lua @@ -0,0 +1,2 @@ +template("module.shared") + add_configfiles("xmake.lua") diff --git a/xmake/templates/c/module.shared/.gitignore b/xmake/templates/c/module.shared/.gitignore new file mode 100644 index 000000000..152105761 --- /dev/null +++ b/xmake/templates/c/module.shared/.gitignore @@ -0,0 +1,8 @@ +# Xmake cache +.xmake/ +build/ + +# MacOS Cache +.DS_Store + + diff --git a/xmake/templates/c/module.shared/project/modules/shared/foo/src/foo.c b/xmake/templates/c/module.shared/project/modules/shared/foo/src/foo.c new file mode 100644 index 000000000..3024f9380 --- /dev/null +++ b/xmake/templates/c/module.shared/project/modules/shared/foo/src/foo.c @@ -0,0 +1,29 @@ +#include +#include +#include + +static int add(lua_State* lua) { + int a = lua_tointeger(lua, 1); + int b = lua_tointeger(lua, 2); + lua_pushinteger(lua, a + b); + return 1; +} + +static int sub(lua_State* lua) { + int a = lua_tointeger(lua, 1); + int b = lua_tointeger(lua, 2); + lua_pushinteger(lua, a - b); + return 1; +} + +static const luaL_Reg g_funcs[] = { + {"add", add}, + {"sub", sub}, + {NULL, NULL} +}; + +int luaopen_foo(lua_State* lua) { + lua_newtable(lua); + luaL_setfuncs(lua, g_funcs, 0); + return 1; +} diff --git a/xmake/templates/c/module.shared/project/modules/shared/foo/xmake.lua b/xmake/templates/c/module.shared/project/modules/shared/foo/xmake.lua new file mode 100644 index 000000000..ad4590ffa --- /dev/null +++ b/xmake/templates/c/module.shared/project/modules/shared/foo/xmake.lua @@ -0,0 +1,9 @@ +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/xmake/templates/c/module.shared/project/src/main.c b/xmake/templates/c/module.shared/project/src/main.c new file mode 100644 index 000000000..113930e1a --- /dev/null +++ b/xmake/templates/c/module.shared/project/src/main.c @@ -0,0 +1,6 @@ +#include + +int main(int argc, char** argv) { + printf("hello world!\n"); + return 0; +} diff --git a/xmake/templates/c/module.shared/project/xmake.lua b/xmake/templates/c/module.shared/project/xmake.lua new file mode 100644 index 000000000..e39ac665d --- /dev/null +++ b/xmake/templates/c/module.shared/project/xmake.lua @@ -0,0 +1,15 @@ +add_rules("mode.debug", "mode.release") + +add_moduledirs("modules") + +target("${TARGETNAME}") + set_kind("binary") + add_files("src/*.c") + on_config(function (target) + import("shared.foo") + print("shared: 1 + 1 = %s", foo.add(1, 1)) + print("shared: 1 - 1 = %s", foo.sub(1, 1)) + end) + +${FAQ} + diff --git a/xmake/templates/c/module.shared/template.lua b/xmake/templates/c/module.shared/template.lua new file mode 100644 index 000000000..7869165b7 --- /dev/null +++ b/xmake/templates/c/module.shared/template.lua @@ -0,0 +1,2 @@ +template("module.shared") + add_configfiles("xmake.lua") -- cgit v1.3.1