add xmake rule codegen

This commit is contained in:
ouczbs 2024-04-22 22:53:41 +08:00
parent 545a72a5de
commit 27648e1b41
12 changed files with 190 additions and 23 deletions

1
.gitignore vendored
View File

@ -8,3 +8,4 @@ vsxmake*/
engine/logs/zengine.log
tools/

34
.vscode/launch.json vendored Normal file
View File

@ -0,0 +1,34 @@
{
// 使 IntelliSense
//
// 访: https://go.microsoft.com/fwlink/?linkid=830387
"version": "0.2.0",
"configurations": [
{
"type": "lua",
"request": "launch",
"tag": "normal",
"name": "LuaPanda",
"description": "通用模式,通常调试项目请选择此模式 | launchVer:3.2.0",
"cwd": "${workspaceFolder}",
"luaFileExtension": "",
"connectionPort": 8818,
"stopOnEntry": true,
"useCHook": true,
"autoPathMode": true
},
{
"type": "lua",
"request": "launch",
"tag": "independent_file",
"name": "LuaPanda-IndependentFile",
"description": "独立文件调试模式,使用前请参考文档",
"luaPath": "",
"packagePath": [],
"luaFileExtension": "",
"connectionPort": 8820,
"stopOnEntry": true,
"useCHook": true
}
]
}

34
engine/.vscode/launch.json vendored Normal file
View File

@ -0,0 +1,34 @@
{
// 使 IntelliSense
//
// 访: https://go.microsoft.com/fwlink/?linkid=830387
"version": "0.2.0",
"configurations": [
{
"type": "lua",
"request": "launch",
"tag": "normal",
"name": "LuaPanda",
"description": "通用模式,通常调试项目请选择此模式 | launchVer:3.2.0",
"cwd": "${workspaceFolder}",
"luaFileExtension": "",
"connectionPort": 8818,
"stopOnEntry": true,
"useCHook": true,
"autoPathMode": true
},
{
"type": "lua",
"request": "launch",
"tag": "independent_file",
"name": "LuaPanda-IndependentFile",
"description": "独立文件调试模式,使用前请参考文档",
"luaPath": "",
"packagePath": [],
"luaFileExtension": "",
"connectionPort": 8820,
"stopOnEntry": true,
"useCHook": true
}
]
}

View File

@ -6,10 +6,12 @@
#include <functional>
using Ubpa::Name;
using Ubpa::type_name;
consteval Name FName(const char* buf) {
return Name(buf);
}
namespace refl {
using Offset = uint32_t;
using Method = void*;
struct MemberData {
Offset offset;
Any value;

View File

@ -21,7 +21,7 @@ struct vec3 : public vec3_parent {
float y = 2;
__cppast(Meta = { 0.f})
float z = 3;
string name{ "hellohellohellohellohellohello" };
string name{ "hello" };
__cppast(Meta = { {3,4} })
int norm(int x1, int& x2)override {
int tmp = x1 * 2 + 1;
@ -32,8 +32,8 @@ struct vec3 : public vec3_parent {
}
virtual float norm1(int& x1) {
x1 = x1 * x * y * z;
//x = x1;
//y = x1 - 1;
x = x1;
y = x1 - 1;
//z = x1 - 10;
//cout << x1 << "::norm1" << endl;
return x1;
@ -53,19 +53,18 @@ struct vec3_Static_Meta {
};
consteval static auto __MakeStaticFields() {
return std::array{
StaticMemberField(&vec3::x, "x", {0.f,vec4{}}),
StaticMemberField(&vec3::y, "y", {0.f,{}}),
StaticMemberField(&vec3::z, "z", {0.f,{}}),
StaticMethodField(&vec3::norm, "norm", {{10,9},{}}),
StaticMethodField(&vec3::norm1, "norm1", {{10},{}}),
StaticMethodField(&vec3::norm2, "norm2", {{10},{}}),
StaticMemberField(&vec3::x, FName("x"), {0.f,vec4{}}),
StaticMemberField(&vec3::y, FName("y"), {0.f,{}}),
StaticMemberField(&vec3::z, FName("z"), {0.f,{}}),
StaticMethodField(&vec3::norm, FName("norm"), {{10,9},{}}),
StaticMethodField(&vec3::norm1, FName("norm1"), {{10},{}}),
StaticMethodField(&vec3::norm2, FName("norm2"), {{10},{}}),
};
}
consteval static int Size() {
return fetch_meta_size<vec3_Static_Meta>();
}
};
constexpr int size = vec3_Static_Meta::Size();
struct vec3_Meta : public Meta{
using MyStatic = vec3_Static_Meta;
using MyUClass = UClass_Meta<vec3, vec3_parent>;
@ -73,12 +72,13 @@ struct vec3_Meta : public Meta{
static auto __MakeFields() {
char* memory = &s_data[0];
return std::array{
MemberField(&vec3::x, "x",memory, {0.f,vec4{}}),
MemberField(&vec3::y, "y",memory, {0.f,{}}),
MemberField(&vec3::z, "z",memory, {0.f,{}}),
MethodField(&vec3::norm, "norm", memory, {{10,9},{}}),
MethodField(&vec3::norm1, "norm1",memory, {{10},{}}),
MethodField(&vec3::norm2, "norm2",memory, {{10},{}}),
MemberField(&vec3::x, FName("x"),memory, {0.f,vec4{}}),
MemberField(&vec3::y, FName("y"),memory, {0.f,{}}),
MemberField(&vec3::z, FName("z"),memory, {0.f,{}}),
MethodField(&vec3::norm, FName("norm"), memory, {{10,9},{}}),
MethodField(&vec3::norm1, FName("norm1"),memory, {{10},{}}),
MethodField(&vec3::norm2, FName("norm2"),memory, {{10},{}}),
};
};
};
};
#include "meta_vertex.inl"

View File

@ -5,19 +5,19 @@ vec3 v;
constexpr int smeta = sizeof(vec3_Meta);
constexpr int scls = sizeof(decltype(*cls));
auto ov = cls->New((void*)&v);
constexpr Name func("norm");
constexpr Name func = FName("norm");
void TestRefl1(benchmark::State& state) {
int x = 1, y = 2;
for (auto _ : state) {
std::array<Any, 4> arr{&x, ov.ptr};
svector<Any> svec(&arr.front(), arr.size(), 2);
ov.Invoke("norm", svec);
ov.Invoke(FName("norm"), svec);
}
}
BENCHMARK(TestRefl1);
void TestRefl2(benchmark::State& state) {
int x = 1, y = 2;
constexpr auto id = GetStaticFieldID<vec3_Meta>(func);
constexpr auto id = GetStaticFieldID<vec3_Meta>(FName("norm"));
auto field = cls->GetField(id);
for (auto _ : state) {
std::array<Any, 4> arr{&x, ov.ptr};
@ -28,7 +28,7 @@ void TestRefl2(benchmark::State& state) {
BENCHMARK(TestRefl2);
void TestRefl3(benchmark::State& state) {
int x = 1, y = 2;
constexpr auto id = GetStaticFieldID<vec3_Meta>(func);
constexpr auto id = GetStaticFieldID<vec3_Meta>(FName("norm"));
auto field = cls->GetField(id);
for (auto _ : state) {
field->Invoke({ &x,ov.ptr, x, y });

View File

@ -31,6 +31,9 @@ target("zlib_test")
-- add_files("test/04promise.cpp")
target("refl_zlib")
add_rules("c++.codegen",{
files = {"test/refl/*.h", "test/refl/*.hpp"}
})
set_kind("binary")
add_deps("zlib")
add_packages("benchmark")

View File

@ -1,5 +1,5 @@
includes("3rdparty/xmake.lua")
includes("xmake/**xmake.lua")
includes("xmake/xmake.lua")
--includes("test/**xmake.lua")
target("zengine")
set_kind("binary")

View File

@ -0,0 +1,45 @@
import("core.project.depend")
function cmd_compile(genfile, sourcefile)
print(os.programdir())
import("find_sdk")
local meta = find_sdk.find_my_program("refl")
argv = {"build", sourcefile, "--output=" .. genfile}
print("cmd_compile", genfile)
os.execv(meta.program, argv, {envs = {PATH = meta.sdkdir}})
return argv
end
function _listen_gen_file(target, batch)
genfile, sourcefile = batch[1], batch[2]
local dependfile = target:dependfile(genfile)
depend.on_changed(
function()
cmd_compile(genfile, sourcefile)
end,
{dependfile = dependfile, files = sourcefile}
)
end
function gen(target)
local gen_batch = target:data("codegen.batch")
if not gen_batch then
return
end
for _, batch in ipairs(gen_batch) do
if batch[2] then
_listen_gen_file(target, batch)
end
end
end
function main(target, headerfiles)
local sourcedir = path.join(target:autogendir({root = true}), target:plat(), "inl")
target:add("includedirs", sourcedir, {public = true})
local gen_batch = {}
for idx, headerfile in pairs(headerfiles) do
-- batch
sourcefile = path.join(sourcedir, "meta_" .. path.basename(headerfile) .. ".inl")
table.insert(gen_batch, {sourcefile, headerfile})
end
-- save unit batch
target:data_set("codegen.batch", gen_batch)
end

View File

@ -0,0 +1,18 @@
rule("c++.codegen")
set_extensions(".inl")
after_load(function (target)
import("meta_refl")
local headerfiles = {}
local files = target:extraconf("rules", "c++.codegen", "files")
for _, file in ipairs(files) do
local p = path.join(target:scriptdir(), file)
for __, filepath in ipairs(os.files(p)) do
table.insert(headerfiles, filepath)
end
end
meta_refl(target, headerfiles)
end)
on_config(function (target)
print("c++.codegen on_config")
import("meta_refl").gen(target)
end)

View File

@ -0,0 +1,28 @@
function find_my_program(name, sdkdir, use_next)
import("lib.detect.find_file")
import("lib.detect.find_program")
import("lib.detect.find_tool")
local sdkdir = sdkdir or path.join(os.projectdir(), "tools")
local tool = find_tool(name, {pathes = {sdkdir, "/usr/local/bin"}})
local prog = tool and tool.program or find_program(name, {pathes = {sdkdir, "/usr/local/bin"}})
prog = prog or find_file(name, {sdkdir})
if (prog == nil) then
if os.host() ~= "windows" then
local outdata, errdata = os.iorun("which " .. name)
if (errdata ~= nil or errdata ~= "") then
prog = string.gsub(outdata, "%s+", "")
end
else
prog = find_file(name .. ".exe", {sdkdir})
end
end
if (prog == nil) then
if not use_next then
return find_my_program(name, path.join(sdkdir, name), true)
end
print(name .. "_f not found! under " .. sdkdir)
return
end
return {program = prog, sdkdir = sdkdir}
end

2
engine/xmake/xmake.lua Normal file
View File

@ -0,0 +1,2 @@
includes("*/xmake.lua")
add_moduledirs(path.join(os.projectdir(), "engine/xmake/modules"))