summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorruki <[email protected]>2026-07-17 23:49:52 +0800
committerruki <[email protected]>2026-07-17 23:49:52 +0800
commitf467d71abc40d4fee6e896d7a0b8e4f5be7e16db (patch)
treec46a673aa416c6b76f7ba4e875cb56e699017e13
parent0cb38ea4d9823ee49d15debb7a94a2fc9e72d3cc (diff)
add batchcmds:call and transform for bin2obj/c
-rw-r--r--tests/projects/other/bin2obj/src/asset.bin1
-rw-r--r--tests/projects/other/bin2obj/src/main.c33
-rw-r--r--tests/projects/other/bin2obj/test.lua2
-rw-r--r--tests/projects/other/bin2obj/xmake.lua7
-rw-r--r--xmake/modules/private/utils/batchcmds.lua18
-rw-r--r--xmake/rules/utils/bin2c/utils.lua12
-rw-r--r--xmake/rules/utils/bin2c/xmake.lua2
-rw-r--r--xmake/rules/utils/bin2obj/utils.lua14
-rw-r--r--xmake/rules/utils/bin2obj/xmake.lua6
9 files changed, 77 insertions, 18 deletions
diff --git a/tests/projects/other/bin2obj/src/asset.bin b/tests/projects/other/bin2obj/src/asset.bin
new file mode 100644
index 000000000..0c8cc9a8c
--- /dev/null
+++ b/tests/projects/other/bin2obj/src/asset.bin
@@ -0,0 +1 @@
+hello transform!
diff --git a/tests/projects/other/bin2obj/src/main.c b/tests/projects/other/bin2obj/src/main.c
index bc1d2442f..a12bb22d0 100644
--- a/tests/projects/other/bin2obj/src/main.c
+++ b/tests/projects/other/bin2obj/src/main.c
@@ -1,6 +1,7 @@
#include <stdio.h>
#include <stdint.h>
#include <ctype.h>
+#include <string.h>
extern const uint8_t _binary_data_bin_start[];
extern const uint8_t _binary_data_bin_end[];
@@ -8,30 +9,33 @@ extern const uint8_t _binary_data_bin_end[];
extern const uint8_t _binary_xmake_ico_start[];
extern const uint8_t _binary_xmake_ico_end[];
+extern const uint8_t _binary_asset_bin_start[];
+extern const uint8_t _binary_asset_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) {
return;
}
-
+
// if file is larger than 128 bytes, only dump first 64 and last 64 bytes
uint32_t dump_size = size;
uint32_t start_offset = 0;
uint32_t end_offset = 0;
uint32_t skip_size = 0;
-
+
if (size > 128) {
dump_size = 64;
start_offset = 0;
end_offset = size - 64;
skip_size = end_offset - start_offset - dump_size;
}
-
+
// dump start
for (uint32_t offset = start_offset; offset < start_offset + dump_size; offset += 16) {
// print offset
printf("%08x ", (unsigned int)offset);
-
+
// print hex bytes (8 bytes, space, 8 bytes)
for (uint32_t i = 0; i < 16; i++) {
if (offset + i < size) {
@@ -43,7 +47,7 @@ static void hexdump(const char* name, const uint8_t* data, uint32_t size) {
printf(" ");
}
}
-
+
// print ASCII representation
printf(" |");
for (uint32_t i = 0; i < 16 && offset + i < size; i++) {
@@ -52,18 +56,18 @@ static void hexdump(const char* name, const uint8_t* data, uint32_t size) {
}
printf("|\n");
}
-
+
// print skip indicator if needed
if (skip_size > 0) {
printf(" ... (skipped %u bytes) ...\n", (unsigned int)skip_size);
}
-
+
// dump end if needed
if (size > 128) {
for (uint32_t offset = end_offset; offset < size; offset += 16) {
// print offset
printf("%08x ", (unsigned int)offset);
-
+
// print hex bytes (8 bytes, space, 8 bytes)
for (uint32_t i = 0; i < 16; i++) {
if (offset + i < size) {
@@ -75,7 +79,7 @@ static void hexdump(const char* name, const uint8_t* data, uint32_t size) {
printf(" ");
}
}
-
+
// print ASCII representation
printf(" |");
for (uint32_t i = 0; i < 16 && offset + i < size; i++) {
@@ -90,9 +94,20 @@ static void hexdump(const char* name, const uint8_t* data, uint32_t size) {
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);
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);
+
+ // verify the transform: asset.bin must be the reverse of "hello transform!\n" (+ zeroend '\0')
+ const char* expected = "\n!mrofsnart olleh";
+ if (_binary_asset_bin_size != 18 || memcmp(_binary_asset_bin_start, expected, 17) != 0) {
+ printf("asset.bin: transform verification failed!\n");
+ return 1;
+ }
+ printf("asset.bin: transform verification ok\n");
return 0;
}
diff --git a/tests/projects/other/bin2obj/test.lua b/tests/projects/other/bin2obj/test.lua
index 7f2050b62..a75aa7e40 100644
--- a/tests/projects/other/bin2obj/test.lua
+++ b/tests/projects/other/bin2obj/test.lua
@@ -1,4 +1,6 @@
function main(t)
t:build()
+ -- run the target, main.c verifies the transformed asset.bin and exits non-zero on mismatch
+ os.exec("xmake run test")
end
diff --git a/tests/projects/other/bin2obj/xmake.lua b/tests/projects/other/bin2obj/xmake.lua
index 9541e17ae..74a50a027 100644
--- a/tests/projects/other/bin2obj/xmake.lua
+++ b/tests/projects/other/bin2obj/xmake.lua
@@ -6,3 +6,10 @@ target("test")
add_files("src/*.c")
add_files("src/data.bin", {zeroend = true})
add_files("src/xmake.ico", {zeroend = false})
+ add_files("src/asset.bin", {zeroend = true, transform = function (inputfile, outputfile, opt)
+ print("transform ..")
+ import("core.base.bytes")
+ assert(bytes and opt.target)
+ local data = io.readfile(inputfile, {encoding = "binary"})
+ io.writefile(outputfile, data:reverse(), {encoding = "binary"})
+ end})
diff --git a/xmake/modules/private/utils/batchcmds.lua b/xmake/modules/private/utils/batchcmds.lua
index 3f584b286..a22879a81 100644
--- a/xmake/modules/private/utils/batchcmds.lua
+++ b/xmake/modules/private/utils/batchcmds.lua
@@ -126,6 +126,16 @@ function _runcmd_vlua(cmd, opt)
end
end
+-- run command: call
+function _runcmd_call(cmd, opt)
+ local func = cmd.func
+ if func then
+ if not opt.dryrun then
+ func(table.unpack(cmd.argv), cmd.opt)
+ end
+ end
+end
+
-- run command: os.mkdir
function _runcmd_mkdir(cmd, opt)
local dir = cmd.dir
@@ -222,6 +232,7 @@ function _runcmd(cmd, opt)
vexecv = _runcmd_vexecv,
lua = _runcmd_lua,
vlua = _runcmd_vlua,
+ call = _runcmd_call,
mkdir = _runcmd_mkdir,
rmdir = _runcmd_rmdir,
cd = _runcmd_cd,
@@ -305,11 +316,16 @@ function batchcmds:lua(script, argv, opt)
table.insert(self:cmds(), {kind = "lua", script = script, argv = argv, opt = opt})
end
--- add command: run lua script file, command or module
+-- add command: run lua script, command or module
function batchcmds:vlua(script, argv, opt)
table.insert(self:cmds(), {kind = "vlua", script = script, argv = argv, opt = opt})
end
+-- add command: call lua function
+function batchcmds:call(func, argv, opt)
+ table.insert(self:cmds(), {kind = "call", func = func, argv = argv, opt = opt})
+end
+
-- add command: compile source files
--
-- @param sourcefiles the source file paths
diff --git a/xmake/rules/utils/bin2c/utils.lua b/xmake/rules/utils/bin2c/utils.lua
index 405c2ed62..b5bf7638b 100644
--- a/xmake/rules/utils/bin2c/utils.lua
+++ b/xmake/rules/utils/bin2c/utils.lua
@@ -36,6 +36,7 @@ function generate_headerfile(target, batchcmds, binaryfile, opt)
opt = opt or {}
local rulename = opt.rulename or "utils.bin2c"
local progress = opt.progress
+ local fileconfig = target:fileconfig(binaryfile)
-- get header directory and file
local headerdir = opt.headerdir
@@ -52,6 +53,16 @@ function generate_headerfile(target, batchcmds, binaryfile, opt)
-- add includedirs
target:add("includedirs", headerdir)
+ -- transform binary data first
+ -- @see https://github.com/xmake-io/xmake/issues/7513
+ local transform = opt.transform or (fileconfig and fileconfig.transform) or target:extraconf("rules", rulename, "transform")
+ if transform then
+ batchcmds:show_progress(progress, "${color.build.object}transforming.bin2obj %s", binaryfile)
+ local transformed_file = target:autogenfile(binaryfile .. ".transformed")
+ batchcmds:call(transform, {binaryfile, transformed_file}, {target = target})
+ binaryfile = transformed_file
+ end
+
-- add commands
if progress then
batchcmds:show_progress(progress, "${color.build.object}generating.bin2c %s", binaryfile)
@@ -69,7 +80,6 @@ function generate_headerfile(target, batchcmds, binaryfile, opt)
end
-- get nozeroend/zeroend (check file-level config first, then rule-level config, then opt)
- local fileconfig = target:fileconfig(binaryfile)
local nozeroend = nil
if fileconfig then
if fileconfig.nozeroend ~= nil then
diff --git a/xmake/rules/utils/bin2c/xmake.lua b/xmake/rules/utils/bin2c/xmake.lua
index 9f6338758..0d2745b60 100644
--- a/xmake/rules/utils/bin2c/xmake.lua
+++ b/xmake/rules/utils/bin2c/xmake.lua
@@ -33,7 +33,7 @@ rule("utils.bin2c")
-- generate header file
local headerfile = bin2c_utils.generate_headerfile(target, batchcmds, sourcefile_bin, {
- progress = opt.progress
+ progress = opt.progress,
})
-- add deps
diff --git a/xmake/rules/utils/bin2obj/utils.lua b/xmake/rules/utils/bin2obj/utils.lua
index 3e8802e97..c7da5f1a1 100644
--- a/xmake/rules/utils/bin2obj/utils.lua
+++ b/xmake/rules/utils/bin2obj/utils.lua
@@ -35,6 +35,7 @@ function generate_objectfile(target, batchcmds, binaryfile, opt)
opt = opt or {}
local rulename = opt.rulename or "utils.bin2obj"
local progress = opt.progress
+ local fileconfig = target:fileconfig(binaryfile)
-- check for cosmocc toolchain
local is_cosmocc = target:toolchain("cosmocc") or (target:has_tool("cc", "cosmocc") and target:has_tool("ar", "cosmoar"))
@@ -63,6 +64,16 @@ function generate_objectfile(target, batchcmds, binaryfile, opt)
end
table.insert(target:objectfiles(), objectfile)
+ -- transform binary data first
+ -- @see https://github.com/xmake-io/xmake/issues/7513
+ local transform = opt.transform or (fileconfig and fileconfig.transform) or target:extraconf("rules", rulename, "transform")
+ if transform then
+ batchcmds:show_progress(progress, "${color.build.object}transforming.bin2obj %s", binaryfile)
+ local transformed_file = target:autogenfile(binaryfile .. ".transformed")
+ batchcmds:call(transform, {binaryfile, transformed_file}, {target = target})
+ binaryfile = transformed_file
+ end
+
-- add commands
if progress then
batchcmds:show_progress(progress, "${color.build.object}generating.bin2obj %s", binaryfile)
@@ -75,6 +86,9 @@ function generate_objectfile(target, batchcmds, binaryfile, opt)
-- get zeroend (default: false, but can be overridden)
local zeroend = opt.zeroend
if zeroend == nil then
+ zeroend = fileconfig and fileconfig.zeroend
+ end
+ if zeroend == nil then
zeroend = target:extraconf("rules", rulename, "zeroend") or false
end
diff --git a/xmake/rules/utils/bin2obj/xmake.lua b/xmake/rules/utils/bin2obj/xmake.lua
index 760b9d536..58ec87a90 100644
--- a/xmake/rules/utils/bin2obj/xmake.lua
+++ b/xmake/rules/utils/bin2obj/xmake.lua
@@ -24,15 +24,9 @@ rule("utils.bin2obj")
on_buildcmd_file(function (target, batchcmds, sourcefile_bin, opt)
import("rules.utils.bin2obj.utils", {alias = "bin2obj_utils", rootdir = os.programdir()})
- -- get zeroend (default: false)
- -- check file-level config first, then rule-level config
- local fileconfig = target:fileconfig(sourcefile_bin)
- local zeroend = (fileconfig and fileconfig.zeroend) or target:extraconf("rules", "utils.bin2obj", "zeroend") or false
-
-- convert binary file to object file
local objectfile = bin2obj_utils.generate_objectfile(target, batchcmds, sourcefile_bin, {
progress = opt.progress,
- zeroend = zeroend
})
-- add deps