diff options
| author | ruki <[email protected]> | 2025-12-07 21:31:25 +0800 |
|---|---|---|
| committer | GitHub <[email protected]> | 2025-12-07 21:31:25 +0800 |
| commit | 4e4b28eb4cad79cc4df5fca887b4f4b709f2b211 (patch) | |
| tree | 8e9d3cf29071bf65d3b0a097deadabe25245cacf /tests | |
| parent | 40d790cf416c8677c51a8e969d421e85c25a019b (diff) | |
| parent | 8e3cc0092d8e356c6387aee941825cf66615541c (diff) | |
Merge pull request #7103 from xmake-io/bin2obj
Add bin2obj rule, will be faster than bin2c
Diffstat (limited to 'tests')
| -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/src/data.bin | 1 | ||||
| -rw-r--r-- | tests/projects/other/bin2obj/src/main.c | 98 | ||||
| -rw-r--r-- | tests/projects/other/bin2obj/src/xmake.ico | bin | 0 -> 119943 bytes | |||
| -rw-r--r-- | tests/projects/other/bin2obj/test.lua | 4 | ||||
| -rw-r--r-- | tests/projects/other/bin2obj/xmake.lua | 8 |
9 files changed, 199 insertions, 7 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/src/data.bin b/tests/projects/other/bin2obj/src/data.bin new file mode 100644 index 000000000..5d26814ee --- /dev/null +++ b/tests/projects/other/bin2obj/src/data.bin @@ -0,0 +1 @@ +hello xmake! diff --git a/tests/projects/other/bin2obj/src/main.c b/tests/projects/other/bin2obj/src/main.c new file mode 100644 index 000000000..bc1d2442f --- /dev/null +++ b/tests/projects/other/bin2obj/src/main.c @@ -0,0 +1,98 @@ +#include <stdio.h> +#include <stdint.h> +#include <ctype.h> + +extern const uint8_t _binary_data_bin_start[]; +extern const uint8_t _binary_data_bin_end[]; + +extern const uint8_t _binary_xmake_ico_start[]; +extern const uint8_t _binary_xmake_ico_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) { + 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) { + 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); + + hexdump("data.bin", _binary_data_bin_start, _binary_data_bin_size); + printf("\n"); + hexdump("xmake.ico", _binary_xmake_ico_start, _binary_xmake_ico_size); + return 0; +} diff --git a/tests/projects/other/bin2obj/src/xmake.ico b/tests/projects/other/bin2obj/src/xmake.ico Binary files differnew file mode 100644 index 000000000..52c8ef389 --- /dev/null +++ b/tests/projects/other/bin2obj/src/xmake.ico diff --git a/tests/projects/other/bin2obj/test.lua b/tests/projects/other/bin2obj/test.lua new file mode 100644 index 000000000..7f2050b62 --- /dev/null +++ b/tests/projects/other/bin2obj/test.lua @@ -0,0 +1,4 @@ +function main(t) + t:build() +end + diff --git a/tests/projects/other/bin2obj/xmake.lua b/tests/projects/other/bin2obj/xmake.lua new file mode 100644 index 000000000..9541e17ae --- /dev/null +++ b/tests/projects/other/bin2obj/xmake.lua @@ -0,0 +1,8 @@ +add_rules("mode.debug", "mode.release") + +target("test") + set_kind("binary") + add_rules("utils.bin2obj", {extensions = {".bin", ".ico"}}) + add_files("src/*.c") + add_files("src/data.bin", {zeroend = true}) + add_files("src/xmake.ico", {zeroend = false}) |
