From 99bcdf969aa6ad79034f4689a5cf715a960658ca Mon Sep 17 00:00:00 2001 From: ruki Date: Sat, 18 Jul 2026 00:34:25 +0800 Subject: add transform lua file support --- tests/projects/other/bin2c/src/asset2.bin | 1 + tests/projects/other/bin2c/src/main.c | 16 +++++++++++++++- tests/projects/other/bin2c/transform.lua | 5 +++++ tests/projects/other/bin2c/xmake.lua | 2 ++ tests/projects/other/bin2obj/src/asset2.bin | 1 + tests/projects/other/bin2obj/src/main.c | 19 ++++++++++++++++++- tests/projects/other/bin2obj/transform.lua | 5 +++++ tests/projects/other/bin2obj/xmake.lua | 2 ++ xmake/modules/private/utils/batchcmds.lua | 10 ++++++++-- 9 files changed, 57 insertions(+), 4 deletions(-) create mode 100644 tests/projects/other/bin2c/src/asset2.bin create mode 100644 tests/projects/other/bin2c/transform.lua create mode 100644 tests/projects/other/bin2obj/src/asset2.bin create mode 100644 tests/projects/other/bin2obj/transform.lua diff --git a/tests/projects/other/bin2c/src/asset2.bin b/tests/projects/other/bin2c/src/asset2.bin new file mode 100644 index 000000000..d7ffc4b96 --- /dev/null +++ b/tests/projects/other/bin2c/src/asset2.bin @@ -0,0 +1 @@ +hello luafile! \ No newline at end of file diff --git a/tests/projects/other/bin2c/src/main.c b/tests/projects/other/bin2c/src/main.c index 57a55aed5..d85e9934e 100644 --- a/tests/projects/other/bin2c/src/main.c +++ b/tests/projects/other/bin2c/src/main.c @@ -15,6 +15,10 @@ static unsigned char g_asset_data[] = { #include "asset.bin.h" }; +static unsigned char g_asset2_data[] = { + #include "asset2.bin.h" +}; + static void hexdump(const char* name, const uint8_t* data, uint32_t size) { printf("%s: size: %u bytes\n", name, (unsigned int)size); if (size == 0) { @@ -101,13 +105,23 @@ int main(int argc, char** argv) hexdump("xmake.ico", g_ico_data, (uint32_t)sizeof(g_ico_data)); printf("\n"); hexdump("asset.bin (transformed)", g_asset_data, (uint32_t)sizeof(g_asset_data)); + printf("\n"); + hexdump("asset2.bin (transformed by lua file)", g_asset2_data, (uint32_t)sizeof(g_asset2_data)); - // verify the transform: asset.bin must be the reverse of "hello transform!" (+ zeroend '\0') + // verify the function transform: asset.bin must be the reverse of "hello transform!" (+ zeroend '\0') const char* expected = "!mrofsnart olleh"; if (sizeof(g_asset_data) != 17 || memcmp(g_asset_data, expected, 16) != 0) { printf("asset.bin: transform verification failed!\n"); return 1; } printf("asset.bin: transform verification ok\n"); + + // verify the lua-file transform: asset2.bin must be the reverse of "hello luafile!" (+ zeroend '\0') + const char* expected2 = "!elifaul olleh"; + if (sizeof(g_asset2_data) != 15 || memcmp(g_asset2_data, expected2, 14) != 0) { + printf("asset2.bin: transform verification failed!\n"); + return 1; + } + printf("asset2.bin: transform verification ok\n"); return 0; } diff --git a/tests/projects/other/bin2c/transform.lua b/tests/projects/other/bin2c/transform.lua new file mode 100644 index 000000000..54cf1f394 --- /dev/null +++ b/tests/projects/other/bin2c/transform.lua @@ -0,0 +1,5 @@ +function main(inputfile, outputfile) + import("core.base.bytes") + local data = io.readfile(inputfile, {encoding = "binary"}) + io.writefile(outputfile, data:reverse(), {encoding = "binary"}) +end diff --git a/tests/projects/other/bin2c/xmake.lua b/tests/projects/other/bin2c/xmake.lua index f37aa1218..d60aea710 100644 --- a/tests/projects/other/bin2c/xmake.lua +++ b/tests/projects/other/bin2c/xmake.lua @@ -12,5 +12,7 @@ target("test") local data = io.readfile(inputfile, {encoding = "binary"}) io.writefile(outputfile, data:reverse(), {encoding = "binary"}) end}) + -- transform can also be a lua script file (supported by the project generators) + add_files("src/asset2.bin", {transform = path.join(os.projectdir(), "transform.lua")}) diff --git a/tests/projects/other/bin2obj/src/asset2.bin b/tests/projects/other/bin2obj/src/asset2.bin new file mode 100644 index 000000000..d7ffc4b96 --- /dev/null +++ b/tests/projects/other/bin2obj/src/asset2.bin @@ -0,0 +1 @@ +hello luafile! \ No newline at end of file diff --git a/tests/projects/other/bin2obj/src/main.c b/tests/projects/other/bin2obj/src/main.c index bf2a8a1d8..03c50352d 100644 --- a/tests/projects/other/bin2obj/src/main.c +++ b/tests/projects/other/bin2obj/src/main.c @@ -12,6 +12,12 @@ extern const uint8_t _binary_xmake_ico_end[]; extern const uint8_t _binary_asset_bin_start[]; extern const uint8_t _binary_asset_bin_end[]; +extern const uint8_t _binary_asset2_bin_start[]; +extern const uint8_t _binary_asset2_bin_end[]; + +extern const uint8_t _binary_asset2_bin_start[]; +extern const uint8_t _binary_asset2_bin_end[]; + static void hexdump(const char* name, const uint8_t* data, uint32_t size) { printf("%s: size: %u bytes\n", name, (unsigned int)size); if (size == 0) { @@ -95,19 +101,30 @@ int main(int argc, char** argv) { const uint32_t _binary_data_bin_size = (uint32_t)(_binary_data_bin_end - _binary_data_bin_start); const uint32_t _binary_xmake_ico_size = (uint32_t)(_binary_xmake_ico_end - _binary_xmake_ico_start); const uint32_t _binary_asset_bin_size = (uint32_t)(_binary_asset_bin_end - _binary_asset_bin_start); + const uint32_t _binary_asset2_bin_size = (uint32_t)(_binary_asset2_bin_end - _binary_asset2_bin_start); hexdump("data.bin", _binary_data_bin_start, _binary_data_bin_size); printf("\n"); hexdump("xmake.ico", _binary_xmake_ico_start, _binary_xmake_ico_size); printf("\n"); hexdump("asset.bin (transformed)", _binary_asset_bin_start, _binary_asset_bin_size); + printf("\n"); + hexdump("asset2.bin (transformed by lua file)", _binary_asset2_bin_start, _binary_asset2_bin_size); - // verify the transform: asset.bin must be the reverse of "hello transform!" (+ zeroend '\0') + // verify the function transform: asset.bin must be the reverse of "hello transform!" (+ zeroend '\0') const char* expected = "!mrofsnart olleh"; if (_binary_asset_bin_size != 17 || memcmp(_binary_asset_bin_start, expected, 16) != 0) { printf("asset.bin: transform verification failed!\n"); return 1; } printf("asset.bin: transform verification ok\n"); + + // verify the lua-file transform: asset2.bin must be the reverse of "hello luafile!" (+ zeroend '\0') + const char* expected2 = "!elifaul olleh"; + if (_binary_asset2_bin_size != 15 || memcmp(_binary_asset2_bin_start, expected2, 14) != 0) { + printf("asset2.bin: transform verification failed!\n"); + return 1; + } + printf("asset2.bin: transform verification ok\n"); return 0; } diff --git a/tests/projects/other/bin2obj/transform.lua b/tests/projects/other/bin2obj/transform.lua new file mode 100644 index 000000000..54cf1f394 --- /dev/null +++ b/tests/projects/other/bin2obj/transform.lua @@ -0,0 +1,5 @@ +function main(inputfile, outputfile) + import("core.base.bytes") + local data = io.readfile(inputfile, {encoding = "binary"}) + io.writefile(outputfile, data:reverse(), {encoding = "binary"}) +end diff --git a/tests/projects/other/bin2obj/xmake.lua b/tests/projects/other/bin2obj/xmake.lua index 0cf4fb4fc..d383bb1bb 100644 --- a/tests/projects/other/bin2obj/xmake.lua +++ b/tests/projects/other/bin2obj/xmake.lua @@ -12,3 +12,5 @@ target("test") local data = io.readfile(inputfile, {encoding = "binary"}) io.writefile(outputfile, data:reverse(), {encoding = "binary"}) end}) + -- transform can also be a lua script file (supported by the project generators) + add_files("src/asset2.bin", {zeroend = true, transform = path.join(os.projectdir(), "transform.lua")}) diff --git a/xmake/modules/private/utils/batchcmds.lua b/xmake/modules/private/utils/batchcmds.lua index 2c80574a5..349e31efe 100644 --- a/xmake/modules/private/utils/batchcmds.lua +++ b/xmake/modules/private/utils/batchcmds.lua @@ -328,8 +328,14 @@ end -- add command: call lua function function batchcmds:call(func, argv, opt) - sandbox.fork(func) - table.insert(self:cmds(), {kind = "call", func = func, argv = argv, opt = opt}) + local functype = type(func) + if functype == "string" then + self:lua(func, argv, opt) + else + assert(functype == "function") + sandbox.fork(func) + table.insert(self:cmds(), {kind = "call", func = func, argv = argv, opt = opt}) + end end -- add command: compile source files -- cgit v1.3.1