summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorruki <[email protected]>2021-08-05 00:35:38 +0800
committerruki <[email protected]>2021-08-05 00:35:38 +0800
commit376aaf2b9432b2da255c4aaabcbc8c84d2372c4c (patch)
treedfc6c82445dd54b18e0f664892af72001c7c5cc6
parentb700ce5f896f1a3edfadafb4c188d1f147ea4ac7 (diff)
improve bin2c rule
-rw-r--r--tests/projects/other/bin2c/src/data.bin2
-rw-r--r--tests/projects/other/bin2c/src/image.png1
-rw-r--r--tests/projects/other/bin2c/src/main.c7
-rw-r--r--tests/projects/other/bin2c/xmake.lua3
-rw-r--r--xmake/modules/private/utils/bin2c.lua3
-rw-r--r--xmake/rules/utils/bin2c/xmake.lua19
6 files changed, 21 insertions, 14 deletions
diff --git a/tests/projects/other/bin2c/src/data.bin b/tests/projects/other/bin2c/src/data.bin
index c499e585b..5d26814ee 100644
--- a/tests/projects/other/bin2c/src/data.bin
+++ b/tests/projects/other/bin2c/src/data.bin
@@ -1 +1 @@
-"hello xmake!"
+hello xmake!
diff --git a/tests/projects/other/bin2c/src/image.png b/tests/projects/other/bin2c/src/image.png
new file mode 100644
index 000000000..d2c2e1ac6
--- /dev/null
+++ b/tests/projects/other/bin2c/src/image.png
@@ -0,0 +1 @@
+fake image
diff --git a/tests/projects/other/bin2c/src/main.c b/tests/projects/other/bin2c/src/main.c
index 650531dcb..d634a917b 100644
--- a/tests/projects/other/bin2c/src/main.c
+++ b/tests/projects/other/bin2c/src/main.c
@@ -4,8 +4,13 @@ static unsigned char g_bin_data[] = {
#include "data.bin.h"
};
+static unsigned char g_png_data[] = {
+ #include "image.png.h"
+};
+
int main(int argc, char** argv)
{
- printf("bin2c: %s\n", g_bin_data);
+ printf("data.bin: %s, size: %d\n", g_bin_data, sizeof(g_bin_data));
+ printf("image.png: %s, size: %d\n", g_png_data, sizeof(g_png_data));
return 0;
}
diff --git a/tests/projects/other/bin2c/xmake.lua b/tests/projects/other/bin2c/xmake.lua
index 4cd6f95ae..b5517a54e 100644
--- a/tests/projects/other/bin2c/xmake.lua
+++ b/tests/projects/other/bin2c/xmake.lua
@@ -2,8 +2,9 @@ add_rules("mode.debug", "mode.release")
target("test")
set_kind("binary")
+ add_rules("utils.bin2c", {linewidth = 16})
add_files("src/*.c")
add_files("src/*.bin")
- add_rules("utils.bin2c")
+ add_files("src/*.png", {rules = "utils.bin2c"})
diff --git a/xmake/modules/private/utils/bin2c.lua b/xmake/modules/private/utils/bin2c.lua
index c6022eba0..91ea1eafe 100644
--- a/xmake/modules/private/utils/bin2c.lua
+++ b/xmake/modules/private/utils/bin2c.lua
@@ -81,9 +81,10 @@ function _do_bin2c(binarypath, outputpath, opt)
print("generating code data file from %s ..", binarypath)
-- do dump
- local binarydata = bytes(io.readfile(binarypath))
+ local binarydata = bytes(io.readfile(binarypath, {encoding = "binary"}))
local outputfile = io.open(outputpath, 'w')
if outputfile then
+ binarydata = binarydata .. bytes('\0')
_do_dump(binarydata, outputfile, opt)
outputfile:close()
end
diff --git a/xmake/rules/utils/bin2c/xmake.lua b/xmake/rules/utils/bin2c/xmake.lua
index 658bf113a..12db55192 100644
--- a/xmake/rules/utils/bin2c/xmake.lua
+++ b/xmake/rules/utils/bin2c/xmake.lua
@@ -20,23 +20,22 @@
rule("utils.bin2c")
set_extensions(".bin")
- on_load(function (target)
- local headerdir = path.join(target:autogendir(), "rules", "c++", "bin2c")
- if not os.isfile(headerdir) then
- os.mkdir(headerdir)
- end
- target:add("includedirs", headerdir)
- end)
before_buildcmd_file(function (target, batchcmds, sourcefile_bin, opt)
-- get header file
local headerdir = path.join(target:autogendir(), "rules", "c++", "bin2c")
- local headerfile = path.join(headerdir, (sourcefile_bin:gsub("%.", "_")) .. ".h")
+ local headerfile = path.join(headerdir, path.filename(sourcefile_bin) .. ".h")
+ target:add("includedirs", headerdir)
-- add commands
- batchcmds:show_progress(opt.progress, "${color.build.object}generating.header %s", sourcefile_bin)
+ batchcmds:show_progress(opt.progress, "${color.build.object}generating.bin2c %s", sourcefile_bin)
batchcmds:mkdir(headerdir)
- local argv = {"-i", sourcefile_bin, "-o", headerfile, "-w", "32"}
+ local argv = {"lua", "private.utils.bin2c", "-i", sourcefile_bin, "-o", headerfile}
+ local linewidth = target:extraconf("rules", "utils.bin2c", "linewidth")
+ if linewidth then
+ table.insert(argv, "-w")
+ table.insert(argv, tostring(linewidth))
+ end
batchcmds:vrunv(os.programfile(), argv)
-- add deps