Skip to content

Commit

Permalink
add autogen shared module test
Browse files Browse the repository at this point in the history
  • Loading branch information
waruqi committed Apr 11, 2024
1 parent b559b4c commit 0a28b27
Show file tree
Hide file tree
Showing 16 changed files with 112 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ rule("autogen")
import("utils.progress")
import("core.project.depend")
import("core.tool.compiler")
import("autogen")
import("autogen.foo", {always_build = true})

local sourcefile_cx = path.join(target:autogendir(), "rules", "autogen", path.basename(sourcefile) .. ".cpp")
local objectfile = target:objectfile(sourcefile_cx)
Expand All @@ -17,7 +17,7 @@ rule("autogen")
depend.on_changed(function ()
progress.show(opt.progress, "${color.build.object}compiling.autogen %s", sourcefile)
os.mkdir(path.directory(sourcefile_cx))
autogen.generate(sourcefile, sourcefile_cx)
foo.generate(sourcefile, sourcefile_cx)
compiler.compile(sourcefile_cx, objectfile, {target = target})
end, {dependfile = target:dependfile(objectfile),
files = sourcefile,
Expand Down
8 changes: 8 additions & 0 deletions tests/projects/other/autogen_shared_module/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
# Xmake cache
.xmake/
build/

# MacOS Cache
.DS_Store


Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
# Xmake cache
.xmake/
build/

# MacOS Cache
.DS_Store


Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
#include <xmi.h>
#include <iostream>
#include <fstream>
#include <vector>

using namespace std;

static int generate(lua_State* lua) {
const char* inputfile = lua_tostring(lua, 1);
const char* outputfile = lua_tostring(lua, 2);

ifstream src_file(inputfile, ios::in | ios::binary);
if (!src_file) {
return 1;
}
vector<char> buffer(istreambuf_iterator<char>(src_file), {});
src_file.close();

ofstream dst_file(outputfile, ios::out);
if (!dst_file) {
return 1;
}

dst_file << "unsigned char g_codegen_data[] = {";
for (auto byte : buffer) {
dst_file << "0x" << hex << (int)(unsigned char)byte << ",";
}
dst_file << "0};" << endl;
dst_file.close();
return 0;
}

int luaopen(foo, lua_State* lua) {
static const luaL_Reg funcs[] = {
{"generate", generate},
{NULL, NULL}
};
lua_newtable(lua);
luaL_setfuncs(lua, funcs, 0);
return 1;
}

Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
add_rules("mode.debug", "mode.release")

target("foo")
add_rules("module.shared")
add_files("src/*.cpp")
set_languages("c++11")
1 change: 1 addition & 0 deletions tests/projects/other/autogen_shared_module/src/data.in
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
hello world!
10 changes: 10 additions & 0 deletions tests/projects/other/autogen_shared_module/src/main.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
#include <iostream>

using namespace std;

extern unsigned char g_codegen_data[];

int main(int argc, char** argv) {
cout << (const char*)g_codegen_data << endl;
return 0;
}
3 changes: 3 additions & 0 deletions tests/projects/other/autogen_shared_module/test.lua
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
function main(t)
t:build()
end
32 changes: 32 additions & 0 deletions tests/projects/other/autogen_shared_module/xmake.lua
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
add_rules("mode.debug", "mode.release")

add_moduledirs("modules")

rule("autogen")
set_extensions(".in")
before_build_file(function (target, sourcefile, opt)
import("utils.progress")
import("core.project.depend")
import("core.tool.compiler")
import("autogen.foo", {always_build = true})

local sourcefile_cx = path.join(target:autogendir(), "rules", "autogen", path.basename(sourcefile) .. ".cpp")
local objectfile = target:objectfile(sourcefile_cx)
table.insert(target:objectfiles(), objectfile)

depend.on_changed(function ()
progress.show(opt.progress, "${color.build.object}compiling.autogen %s", sourcefile)
os.mkdir(path.directory(sourcefile_cx))
foo.generate(sourcefile, sourcefile_cx)
compiler.compile(sourcefile_cx, objectfile, {target = target})
end, {dependfile = target:dependfile(objectfile),
files = sourcefile,
changed = target:is_rebuilt()})
end)

target("test")
set_kind("binary")
add_rules("autogen")
add_files("src/main.cpp")
add_files("src/*.in")

0 comments on commit 0a28b27

Please sign in to comment.