diff options
| author | ruki <[email protected]> | 2025-12-07 12:33:31 +0800 |
|---|---|---|
| committer | ruki <[email protected]> | 2025-12-07 17:15:59 +0800 |
| commit | 58045d685b6daea07dbc641e84eaaa8527c6a189 (patch) | |
| tree | 0a8bf201e3d3f679039c22aa9e2d6f67c757928b | |
| parent | bbfd1bd9a4ae413354f37f46935097cf591b6c6c (diff) | |
improve bin2c demo
| -rw-r--r-- | tests/projects/other/bin2c/src/image.png | 1 | ||||
| -rw-r--r-- | tests/projects/other/bin2c/src/main.c | 90 | ||||
| -rw-r--r-- | tests/projects/other/bin2c/src/xmake.ico | bin | 0 -> 119943 bytes | |||
| -rw-r--r-- | tests/projects/other/bin2c/xmake.lua | 4 | ||||
| -rw-r--r-- | tests/projects/other/bin2obj/xmake.lua | 2 | ||||
| -rw-r--r-- | xmake/rules/utils/bin2c/xmake.lua | 8 |
6 files changed, 96 insertions, 9 deletions
diff --git a/tests/projects/other/bin2c/src/image.png b/tests/projects/other/bin2c/src/image.png deleted file mode 100644 index d2c2e1ac6..000000000 --- a/tests/projects/other/bin2c/src/image.png +++ /dev/null @@ -1 +0,0 @@ -fake image diff --git a/tests/projects/other/bin2c/src/main.c b/tests/projects/other/bin2c/src/main.c index 31e416157..74898132c 100644 --- a/tests/projects/other/bin2c/src/main.c +++ b/tests/projects/other/bin2c/src/main.c @@ -1,16 +1,98 @@ #include <stdio.h> +#include <stdint.h> +#include <ctype.h> static unsigned char g_bin_data[] = { #include "data.bin.h" }; -static unsigned char g_png_data[] = { - #include "image.png.h" +static unsigned char g_ico_data[] = { + #include "xmake.ico.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) { + 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) { + printf("%02x ", data[offset + i]); + } else { + printf(" "); + } + if (i == 7) { + printf(" "); + } + } + + // print ASCII representation + printf(" |"); + for (uint32_t i = 0; i < 16 && offset + i < size; i++) { + uint8_t c = data[offset + i]; + printf("%c", isprint(c) ? c : '.'); + } + 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) { + printf("%02x ", data[offset + i]); + } else { + printf(" "); + } + if (i == 7) { + printf(" "); + } + } + + // print ASCII representation + printf(" |"); + for (uint32_t i = 0; i < 16 && offset + i < size; i++) { + uint8_t c = data[offset + i]; + printf("%c", isprint(c) ? c : '.'); + } + printf("|\n"); + } + } +} + int main(int argc, char** argv) { - printf("data.bin: %s, size: %d\n", g_bin_data, (int)sizeof(g_bin_data)); - printf("image.png: %s, size: %d\n", g_png_data, (int)sizeof(g_png_data)); + hexdump("data.bin", g_bin_data, (uint32_t)sizeof(g_bin_data)); + printf("\n"); + hexdump("xmake.ico", g_ico_data, (uint32_t)sizeof(g_ico_data)); return 0; } diff --git a/tests/projects/other/bin2c/src/xmake.ico b/tests/projects/other/bin2c/src/xmake.ico Binary files differnew file mode 100644 index 000000000..52c8ef389 --- /dev/null +++ b/tests/projects/other/bin2c/src/xmake.ico diff --git a/tests/projects/other/bin2c/xmake.lua b/tests/projects/other/bin2c/xmake.lua index b4c71e705..b00dedb65 100644 --- a/tests/projects/other/bin2c/xmake.lua +++ b/tests/projects/other/bin2c/xmake.lua @@ -2,9 +2,9 @@ add_rules("mode.debug", "mode.release") target("test") set_kind("binary") - add_rules("utils.bin2c", {linewidth = 16, extensions = {".bin", ".png"}}) + add_rules("utils.bin2c", {linewidth = 16, extensions = {".bin", ".ico"}}) add_files("src/*.c") add_files("src/*.bin") - add_files("src/*.png") + add_files("src/xmake.ico", {nozeroend = true}) diff --git a/tests/projects/other/bin2obj/xmake.lua b/tests/projects/other/bin2obj/xmake.lua index ed0de51fc..9541e17ae 100644 --- a/tests/projects/other/bin2obj/xmake.lua +++ b/tests/projects/other/bin2obj/xmake.lua @@ -5,4 +5,4 @@ target("test") add_rules("utils.bin2obj", {extensions = {".bin", ".ico"}}) add_files("src/*.c") add_files("src/data.bin", {zeroend = true}) - add_files("src/*.ico") + add_files("src/xmake.ico", {zeroend = false}) diff --git a/xmake/rules/utils/bin2c/xmake.lua b/xmake/rules/utils/bin2c/xmake.lua index 082e8fb5b..fc05ecf4e 100644 --- a/xmake/rules/utils/bin2c/xmake.lua +++ b/xmake/rules/utils/bin2c/xmake.lua @@ -44,7 +44,13 @@ rule("utils.bin2c") table.insert(argv, "-w") table.insert(argv, tostring(linewidth)) end - local nozeroend = target:extraconf("rules", "utils.bin2c", "nozeroend") + -- get nozeroend (check file-level config first, then rule-level config) + local fileconfig = target:fileconfig(sourcefile_bin) + local nozeroend = (fileconfig and fileconfig.nozeroend) or target:extraconf("rules", "utils.bin2c", "nozeroend") or false + -- also support zeroend (inverse of nozeroend) + if fileconfig and fileconfig.zeroend ~= nil then + nozeroend = not fileconfig.zeroend + end if nozeroend then table.insert(argv, "--nozeroend") end |
