From 0e2574300271cdb454db610b36466dd27ea7cfa3 Mon Sep 17 00:00:00 2001 From: ruki Date: Sun, 7 Dec 2025 00:00:46 +0800 Subject: add bin2obj rule and test --- tests/projects/other/bin2obj/src/data.bin | 1 + tests/projects/other/bin2obj/src/image.png | 1 + tests/projects/other/bin2obj/src/main.c | 38 ++++++++++++++++++++++++++++++ tests/projects/other/bin2obj/test.lua | 4 ++++ tests/projects/other/bin2obj/xmake.lua | 9 +++++++ 5 files changed, 53 insertions(+) create mode 100644 tests/projects/other/bin2obj/src/data.bin create mode 100644 tests/projects/other/bin2obj/src/image.png create mode 100644 tests/projects/other/bin2obj/src/main.c create mode 100644 tests/projects/other/bin2obj/test.lua create mode 100644 tests/projects/other/bin2obj/xmake.lua (limited to 'tests/projects') 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/image.png b/tests/projects/other/bin2obj/src/image.png new file mode 100644 index 000000000..d2c2e1ac6 --- /dev/null +++ b/tests/projects/other/bin2obj/src/image.png @@ -0,0 +1 @@ +fake image diff --git a/tests/projects/other/bin2obj/src/main.c b/tests/projects/other/bin2obj/src/main.c new file mode 100644 index 000000000..038e3f514 --- /dev/null +++ b/tests/projects/other/bin2obj/src/main.c @@ -0,0 +1,38 @@ +#include +#include + +extern const uint8_t _binary_data_bin_start[]; +extern const uint8_t _binary_data_bin_end[]; +extern const uint32_t _binary_data_bin_size; + +extern const uint8_t _binary_image_png_start[]; +extern const uint8_t _binary_image_png_end[]; +extern const uint32_t _binary_image_png_size; + +int main(int argc, char** argv) { + printf("data.bin size: %u\n", (unsigned int)_binary_data_bin_size); + printf("data.bin start: %p\n", _binary_data_bin_start); + printf("data.bin end: %p\n", _binary_data_bin_end); + + printf("image.png size: %u\n", (unsigned int)_binary_image_png_size); + printf("image.png start: %p\n", _binary_image_png_start); + printf("image.png end: %p\n", _binary_image_png_end); + + // print first few bytes of data.bin + if (_binary_data_bin_size > 0) { + printf("data.bin first byte: 0x%02x\n", _binary_data_bin_start[0]); + } + if (_binary_data_bin_size > 1) { + printf("data.bin second byte: 0x%02x\n", _binary_data_bin_start[1]); + } + + // print first few bytes of image.png + if (_binary_image_png_size > 0) { + printf("image.png first byte: 0x%02x\n", _binary_image_png_start[0]); + } + if (_binary_image_png_size > 1) { + printf("image.png second byte: 0x%02x\n", _binary_image_png_start[1]); + } + + return 0; +} 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..bcd64fff5 --- /dev/null +++ b/tests/projects/other/bin2obj/xmake.lua @@ -0,0 +1,9 @@ +add_rules("mode.debug", "mode.release") + +target("test") + set_kind("binary") + add_rules("utils.bin2obj", {extensions = {".bin", ".png"}}) + add_files("src/*.c") + add_files("src/*.bin") + add_files("src/*.png") + -- cgit v1.3.1 From 05b3a19bef94606fc56c60b1bd16a3a5bf70ca29 Mon Sep 17 00:00:00 2001 From: ruki Date: Sun, 7 Dec 2025 00:22:25 +0800 Subject: fix size again --- core/src/xmake/utils/bin2coff.c | 22 ++++++++++++++-------- tests/projects/other/bin2obj/src/main.c | 8 ++++---- 2 files changed, 18 insertions(+), 12 deletions(-) (limited to 'tests/projects') diff --git a/core/src/xmake/utils/bin2coff.c b/core/src/xmake/utils/bin2coff.c index 076dd50cd..730d0dd27 100644 --- a/core/src/xmake/utils/bin2coff.c +++ b/core/src/xmake/utils/bin2coff.c @@ -180,10 +180,11 @@ static tb_bool_t xm_utils_bin2coff_dump(tb_stream_ref_t istream, tb_snprintf(symbol_size, sizeof(symbol_size), "%s_size", symbol_name); // calculate offsets + // Note: we append the size value (4 bytes) at the end of data section tb_uint32_t header_size = sizeof(xm_coff_header_t); tb_uint32_t section_header_size = sizeof(xm_coff_section_t); tb_uint32_t section_data_ofs = header_size + section_header_size; - tb_uint32_t section_data_size = datasize; + tb_uint32_t section_data_size = datasize + 4; // data + size value tb_uint32_t symbol_table_ofs = section_data_ofs + ((section_data_size + 3) & ~3); // align to 4 bytes tb_uint32_t string_table_size = 4; // initial 4-byte size field @@ -219,9 +220,9 @@ static tb_bool_t xm_utils_bin2coff_dump(tb_stream_ref_t istream, xm_coff_section_t section; tb_memset(§ion, 0, sizeof(section)); tb_strncpy(section.name, ".rdata", 8); - section.vsize = datasize; + section.vsize = section_data_size; // include size value section.vaddr = 0; - section.size = datasize; + section.size = section_data_size; // include size value section.ofs = section_data_ofs; section.relocofs = 0; section.linenoofs = 0; @@ -246,12 +247,17 @@ static tb_bool_t xm_utils_bin2coff_dump(tb_stream_ref_t istream, left -= to_read; } - // align to 4 bytes - tb_uint32_t padding = (4 - (section_data_size & 3)) & 3; + // align data to 4 bytes + tb_uint32_t padding = (4 - (datasize & 3)) & 3; if (padding > 0) { xm_utils_bin2coff_write_padding(ostream, padding); } + // append size value at the end of data section + if (!tb_stream_bwrit(ostream, (tb_byte_t const *)&datasize, 4)) { + return tb_false; + } + // write symbol table // symbol 0: .rdata section symbol xm_coff_symbol_t sym_section; @@ -267,7 +273,7 @@ static tb_bool_t xm_utils_bin2coff_dump(tb_stream_ref_t istream, } // auxiliary entry for section (18 bytes total) // format: 4 bytes size, 2 bytes nreloc, 2 bytes nlineno, 10 bytes unused - tb_uint32_t aux_section_size = datasize; + tb_uint32_t aux_section_size = section_data_size; tb_uint16_t aux_section_nreloc = 0; tb_uint16_t aux_section_nlineno = 0; if (!tb_stream_bwrit(ostream, (tb_byte_t const *)&aux_section_size, 4) || @@ -309,9 +315,9 @@ static tb_bool_t xm_utils_bin2coff_dump(tb_stream_ref_t istream, } // symbol 3: _binary_xxx_size - // Note: use section symbol instead of absolute symbol to avoid relocation issues + // Note: point to the size value appended at the end of data section xm_utils_bin2coff_write_symbol_name(ostream, symbol_size, &strtab_offset); - tb_uint32_t sym_size_value = datasize; + tb_uint32_t sym_size_value = ((datasize + padding + 3) & ~3); // offset to size value (aligned) tb_int16_t sym_size_sect = 1; // same section as data tb_uint16_t sym_size_type = 0; tb_uint8_t sym_size_scl = 2; // IMAGE_SYM_CLASS_EXTERNAL diff --git a/tests/projects/other/bin2obj/src/main.c b/tests/projects/other/bin2obj/src/main.c index 038e3f514..8c748b7bf 100644 --- a/tests/projects/other/bin2obj/src/main.c +++ b/tests/projects/other/bin2obj/src/main.c @@ -13,11 +13,11 @@ int main(int argc, char** argv) { printf("data.bin size: %u\n", (unsigned int)_binary_data_bin_size); printf("data.bin start: %p\n", _binary_data_bin_start); printf("data.bin end: %p\n", _binary_data_bin_end); - + printf("image.png size: %u\n", (unsigned int)_binary_image_png_size); printf("image.png start: %p\n", _binary_image_png_start); printf("image.png end: %p\n", _binary_image_png_end); - + // print first few bytes of data.bin if (_binary_data_bin_size > 0) { printf("data.bin first byte: 0x%02x\n", _binary_data_bin_start[0]); @@ -25,7 +25,7 @@ int main(int argc, char** argv) { if (_binary_data_bin_size > 1) { printf("data.bin second byte: 0x%02x\n", _binary_data_bin_start[1]); } - + // print first few bytes of image.png if (_binary_image_png_size > 0) { printf("image.png first byte: 0x%02x\n", _binary_image_png_start[0]); @@ -33,6 +33,6 @@ int main(int argc, char** argv) { if (_binary_image_png_size > 1) { printf("image.png second byte: 0x%02x\n", _binary_image_png_start[1]); } - + return 0; } -- cgit v1.3.1 From 2e9c0aa9908b850bf426f0a1d065815d257b84d9 Mon Sep 17 00:00:00 2001 From: ruki Date: Sun, 7 Dec 2025 00:33:32 +0800 Subject: remove size symbols --- core/src/xmake/utils/bin2coff.c | 47 +++++---------------------------- tests/projects/other/bin2obj/src/main.c | 14 +++++----- 2 files changed, 15 insertions(+), 46 deletions(-) (limited to 'tests/projects') diff --git a/core/src/xmake/utils/bin2coff.c b/core/src/xmake/utils/bin2coff.c index 730d0dd27..d48d89e09 100644 --- a/core/src/xmake/utils/bin2coff.c +++ b/core/src/xmake/utils/bin2coff.c @@ -154,7 +154,6 @@ static tb_bool_t xm_utils_bin2coff_dump(tb_stream_ref_t istream, tb_char_t symbol_name[256] = {0}; tb_char_t symbol_start[256] = {0}; tb_char_t symbol_end[256] = {0}; - tb_char_t symbol_size[256] = {0}; // use basename or default to "data" if (!basename || !basename[0]) { @@ -177,30 +176,24 @@ static tb_bool_t xm_utils_bin2coff_dump(tb_stream_ref_t istream, tb_snprintf(symbol_start, sizeof(symbol_start), "%s_start", symbol_name); tb_snprintf(symbol_end, sizeof(symbol_end), "%s_end", symbol_name); - tb_snprintf(symbol_size, sizeof(symbol_size), "%s_size", symbol_name); // calculate offsets - // Note: we append the size value (4 bytes) at the end of data section tb_uint32_t header_size = sizeof(xm_coff_header_t); tb_uint32_t section_header_size = sizeof(xm_coff_section_t); tb_uint32_t section_data_ofs = header_size + section_header_size; - tb_uint32_t section_data_size = datasize + 4; // data + size value + tb_uint32_t section_data_size = datasize; tb_uint32_t symbol_table_ofs = section_data_ofs + ((section_data_size + 3) & ~3); // align to 4 bytes tb_uint32_t string_table_size = 4; // initial 4-byte size field // calculate string table size tb_size_t start_len = tb_strlen(symbol_start); tb_size_t end_len = tb_strlen(symbol_end); - tb_size_t size_len = tb_strlen(symbol_size); if (start_len > 8) { string_table_size += (tb_uint32_t)(start_len + 1); } if (end_len > 8) { string_table_size += (tb_uint32_t)(end_len + 1); } - if (size_len > 8) { - string_table_size += (tb_uint32_t)(size_len + 1); - } // write COFF header xm_coff_header_t header; @@ -209,7 +202,7 @@ static tb_bool_t xm_utils_bin2coff_dump(tb_stream_ref_t istream, header.nsects = 1; header.time = 0; header.symtabofs = symbol_table_ofs; - header.nsyms = 5; // .rdata section symbol (1) + auxiliary entry (1) + 3 data symbols (3) + header.nsyms = 4; // .rdata section symbol (1) + auxiliary entry (1) + 2 data symbols (start, end) header.opthdr = 0; header.flags = 0; if (!tb_stream_bwrit(ostream, (tb_byte_t const *)&header, sizeof(header))) { @@ -220,9 +213,9 @@ static tb_bool_t xm_utils_bin2coff_dump(tb_stream_ref_t istream, xm_coff_section_t section; tb_memset(§ion, 0, sizeof(section)); tb_strncpy(section.name, ".rdata", 8); - section.vsize = section_data_size; // include size value + section.vsize = datasize; section.vaddr = 0; - section.size = section_data_size; // include size value + section.size = datasize; section.ofs = section_data_ofs; section.relocofs = 0; section.linenoofs = 0; @@ -247,17 +240,12 @@ static tb_bool_t xm_utils_bin2coff_dump(tb_stream_ref_t istream, left -= to_read; } - // align data to 4 bytes - tb_uint32_t padding = (4 - (datasize & 3)) & 3; + // align to 4 bytes + tb_uint32_t padding = (4 - (section_data_size & 3)) & 3; if (padding > 0) { xm_utils_bin2coff_write_padding(ostream, padding); } - // append size value at the end of data section - if (!tb_stream_bwrit(ostream, (tb_byte_t const *)&datasize, 4)) { - return tb_false; - } - // write symbol table // symbol 0: .rdata section symbol xm_coff_symbol_t sym_section; @@ -273,7 +261,7 @@ static tb_bool_t xm_utils_bin2coff_dump(tb_stream_ref_t istream, } // auxiliary entry for section (18 bytes total) // format: 4 bytes size, 2 bytes nreloc, 2 bytes nlineno, 10 bytes unused - tb_uint32_t aux_section_size = section_data_size; + tb_uint32_t aux_section_size = datasize; tb_uint16_t aux_section_nreloc = 0; tb_uint16_t aux_section_nlineno = 0; if (!tb_stream_bwrit(ostream, (tb_byte_t const *)&aux_section_size, 4) || @@ -314,22 +302,6 @@ static tb_bool_t xm_utils_bin2coff_dump(tb_stream_ref_t istream, return tb_false; } - // symbol 3: _binary_xxx_size - // Note: point to the size value appended at the end of data section - xm_utils_bin2coff_write_symbol_name(ostream, symbol_size, &strtab_offset); - tb_uint32_t sym_size_value = ((datasize + padding + 3) & ~3); // offset to size value (aligned) - tb_int16_t sym_size_sect = 1; // same section as data - tb_uint16_t sym_size_type = 0; - tb_uint8_t sym_size_scl = 2; // IMAGE_SYM_CLASS_EXTERNAL - tb_uint8_t sym_size_naux = 0; - if (!tb_stream_bwrit(ostream, (tb_byte_t const *)&sym_size_value, 4) || - !tb_stream_bwrit(ostream, (tb_byte_t const *)&sym_size_sect, 2) || - !tb_stream_bwrit(ostream, (tb_byte_t const *)&sym_size_type, 2) || - !tb_stream_bwrit(ostream, (tb_byte_t const *)&sym_size_scl, 1) || - !tb_stream_bwrit(ostream, (tb_byte_t const *)&sym_size_naux, 1)) { - return tb_false; - } - // write string table tb_stream_bwrit(ostream, (tb_byte_t const *)&string_table_size, 4); if (tb_strlen(symbol_start) > 8) { @@ -342,11 +314,6 @@ static tb_bool_t xm_utils_bin2coff_dump(tb_stream_ref_t istream, tb_byte_t null = 0; tb_stream_bwrit(ostream, &null, 1); } - if (tb_strlen(symbol_size) > 8) { - xm_utils_bin2coff_write_string(ostream, symbol_size, 0); - tb_byte_t null = 0; - tb_stream_bwrit(ostream, &null, 1); - } return tb_true; } diff --git a/tests/projects/other/bin2obj/src/main.c b/tests/projects/other/bin2obj/src/main.c index 8c748b7bf..4325873da 100644 --- a/tests/projects/other/bin2obj/src/main.c +++ b/tests/projects/other/bin2obj/src/main.c @@ -3,21 +3,23 @@ extern const uint8_t _binary_data_bin_start[]; extern const uint8_t _binary_data_bin_end[]; -extern const uint32_t _binary_data_bin_size; extern const uint8_t _binary_image_png_start[]; extern const uint8_t _binary_image_png_end[]; -extern const uint32_t _binary_image_png_size; int main(int argc, char** argv) { + // calculate size from start and end + const uint32_t _binary_data_bin_size = (uint32_t)(_binary_data_bin_end - _binary_data_bin_start); + const uint32_t _binary_image_png_size = (uint32_t)(_binary_image_png_end - _binary_image_png_start); + printf("data.bin size: %u\n", (unsigned int)_binary_data_bin_size); printf("data.bin start: %p\n", _binary_data_bin_start); printf("data.bin end: %p\n", _binary_data_bin_end); - + printf("image.png size: %u\n", (unsigned int)_binary_image_png_size); printf("image.png start: %p\n", _binary_image_png_start); printf("image.png end: %p\n", _binary_image_png_end); - + // print first few bytes of data.bin if (_binary_data_bin_size > 0) { printf("data.bin first byte: 0x%02x\n", _binary_data_bin_start[0]); @@ -25,7 +27,7 @@ int main(int argc, char** argv) { if (_binary_data_bin_size > 1) { printf("data.bin second byte: 0x%02x\n", _binary_data_bin_start[1]); } - + // print first few bytes of image.png if (_binary_image_png_size > 0) { printf("image.png first byte: 0x%02x\n", _binary_image_png_start[0]); @@ -33,6 +35,6 @@ int main(int argc, char** argv) { if (_binary_image_png_size > 1) { printf("image.png second byte: 0x%02x\n", _binary_image_png_start[1]); } - + return 0; } -- cgit v1.3.1 From 00f2799066700e1de7c4fb188857c31cc812b6b9 Mon Sep 17 00:00:00 2001 From: ruki Date: Sun, 7 Dec 2025 00:36:30 +0800 Subject: improve test --- tests/projects/other/bin2obj/src/main.c | 33 +++++---------------------------- 1 file changed, 5 insertions(+), 28 deletions(-) (limited to 'tests/projects') diff --git a/tests/projects/other/bin2obj/src/main.c b/tests/projects/other/bin2obj/src/main.c index 4325873da..948192de3 100644 --- a/tests/projects/other/bin2obj/src/main.c +++ b/tests/projects/other/bin2obj/src/main.c @@ -7,34 +7,11 @@ extern const uint8_t _binary_data_bin_end[]; extern const uint8_t _binary_image_png_start[]; extern const uint8_t _binary_image_png_end[]; +const uint32_t _binary_data_bin_size = (uint32_t)(_binary_data_bin_end - _binary_data_bin_start); +const uint32_t _binary_image_png_size = (uint32_t)(_binary_image_png_end - _binary_image_png_start); + int main(int argc, char** argv) { - // calculate size from start and end - const uint32_t _binary_data_bin_size = (uint32_t)(_binary_data_bin_end - _binary_data_bin_start); - const uint32_t _binary_image_png_size = (uint32_t)(_binary_image_png_end - _binary_image_png_start); - - printf("data.bin size: %u\n", (unsigned int)_binary_data_bin_size); - printf("data.bin start: %p\n", _binary_data_bin_start); - printf("data.bin end: %p\n", _binary_data_bin_end); - - printf("image.png size: %u\n", (unsigned int)_binary_image_png_size); - printf("image.png start: %p\n", _binary_image_png_start); - printf("image.png end: %p\n", _binary_image_png_end); - - // print first few bytes of data.bin - if (_binary_data_bin_size > 0) { - printf("data.bin first byte: 0x%02x\n", _binary_data_bin_start[0]); - } - if (_binary_data_bin_size > 1) { - printf("data.bin second byte: 0x%02x\n", _binary_data_bin_start[1]); - } - - // print first few bytes of image.png - if (_binary_image_png_size > 0) { - printf("image.png first byte: 0x%02x\n", _binary_image_png_start[0]); - } - if (_binary_image_png_size > 1) { - printf("image.png second byte: 0x%02x\n", _binary_image_png_start[1]); - } - + printf("data.bin: %s, size: %u\n", (const char*)_binary_data_bin_start, (unsigned int)_binary_data_bin_size); + printf("image.png: %s, size: %u\n", (const char*)_binary_image_png_start, (unsigned int)_binary_image_png_size); return 0; } -- cgit v1.3.1 From bce2cef4a7832f9df5b9e41e8b1689a5490249b3 Mon Sep 17 00:00:00 2001 From: ruki Date: Sun, 7 Dec 2025 00:37:58 +0800 Subject: improve test again --- tests/projects/other/bin2obj/src/main.c | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) (limited to 'tests/projects') diff --git a/tests/projects/other/bin2obj/src/main.c b/tests/projects/other/bin2obj/src/main.c index 948192de3..78ac52f73 100644 --- a/tests/projects/other/bin2obj/src/main.c +++ b/tests/projects/other/bin2obj/src/main.c @@ -7,10 +7,10 @@ extern const uint8_t _binary_data_bin_end[]; extern const uint8_t _binary_image_png_start[]; extern const uint8_t _binary_image_png_end[]; -const uint32_t _binary_data_bin_size = (uint32_t)(_binary_data_bin_end - _binary_data_bin_start); -const uint32_t _binary_image_png_size = (uint32_t)(_binary_image_png_end - _binary_image_png_start); - 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_image_png_size = (uint32_t)(_binary_image_png_end - _binary_image_png_start); + printf("data.bin: %s, size: %u\n", (const char*)_binary_data_bin_start, (unsigned int)_binary_data_bin_size); printf("image.png: %s, size: %u\n", (const char*)_binary_image_png_start, (unsigned int)_binary_image_png_size); return 0; -- cgit v1.3.1 From 582aeb5720c2a75b7dd231c68403976db8123c4a Mon Sep 17 00:00:00 2001 From: ruki Date: Sun, 7 Dec 2025 11:51:27 +0800 Subject: use dexdump to dump data --- tests/projects/other/bin2obj/src/main.c | 37 +++++++++++++++++++++++++++++++-- 1 file changed, 35 insertions(+), 2 deletions(-) (limited to 'tests/projects') diff --git a/tests/projects/other/bin2obj/src/main.c b/tests/projects/other/bin2obj/src/main.c index 78ac52f73..196247654 100644 --- a/tests/projects/other/bin2obj/src/main.c +++ b/tests/projects/other/bin2obj/src/main.c @@ -1,5 +1,6 @@ #include #include +#include extern const uint8_t _binary_data_bin_start[]; extern const uint8_t _binary_data_bin_end[]; @@ -7,11 +8,43 @@ extern const uint8_t _binary_data_bin_end[]; extern const uint8_t _binary_image_png_start[]; extern const uint8_t _binary_image_png_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; + } + for (uint32_t offset = 0; 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_image_png_size = (uint32_t)(_binary_image_png_end - _binary_image_png_start); - printf("data.bin: %s, size: %u\n", (const char*)_binary_data_bin_start, (unsigned int)_binary_data_bin_size); - printf("image.png: %s, size: %u\n", (const char*)_binary_image_png_start, (unsigned int)_binary_image_png_size); + hexdump("data.bin", _binary_data_bin_start, _binary_data_bin_size); + printf("\n"); + hexdump("image.png", _binary_image_png_start, _binary_image_png_size); return 0; } -- cgit v1.3.1 From 774c5110ef60f2878d3a2f503c7a55db4943a1cb Mon Sep 17 00:00:00 2001 From: ruki Date: Sun, 7 Dec 2025 12:06:33 +0800 Subject: add zeroend --- core/src/xmake/utils/bin2coff.c | 25 +++++++++++++++++++++++-- core/src/xmake/utils/bin2macho.c | 27 +++++++++++++++++++++++---- tests/projects/other/bin2obj/xmake.lua | 1 - xmake/core/sandbox/modules/utils.lua | 8 ++++---- xmake/modules/private/utils/bin2obj.lua | 13 ++++++++++--- xmake/rules/utils/bin2obj/xmake.lua | 6 ++++++ 6 files changed, 66 insertions(+), 14 deletions(-) (limited to 'tests/projects') diff --git a/core/src/xmake/utils/bin2coff.c b/core/src/xmake/utils/bin2coff.c index be8a313ad..58c05cd33 100644 --- a/core/src/xmake/utils/bin2coff.c +++ b/core/src/xmake/utils/bin2coff.c @@ -155,7 +155,8 @@ static tb_bool_t xm_utils_bin2coff_dump(tb_stream_ref_t istream, tb_stream_ref_t ostream, tb_char_t const *symbol_prefix, tb_char_t const *arch, - tb_char_t const *basename) { + tb_char_t const *basename, + tb_bool_t zeroend) { tb_assert_and_check_return_val(istream && ostream, tb_false); // get file size @@ -164,6 +165,13 @@ static tb_bool_t xm_utils_bin2coff_dump(tb_stream_ref_t istream, return tb_false; } tb_uint32_t datasize = (tb_uint32_t)filesize; + // add null terminator if zeroend is true + if (zeroend) { + if (datasize >= 0xffffffffU) { + return tb_false; // would overflow + } + datasize++; + } // generate symbol names from filename tb_char_t symbol_name[256] = {0}; @@ -254,6 +262,13 @@ static tb_bool_t xm_utils_bin2coff_dump(tb_stream_ref_t istream, } left -= to_read; } + // append null terminator if zeroend is true + if (zeroend) { + tb_byte_t zero = 0; + if (!tb_stream_bwrit(ostream, &zero, 1)) { + return tb_false; + } + } // align to 4 bytes tb_uint32_t padding = (4 - (section_data_size & 3)) & 3; @@ -351,6 +366,12 @@ tb_int_t xm_utils_bin2coff(lua_State *lua) { // get basename (optional) tb_char_t const *basename = lua_isstring(lua, 5) ? lua_tostring(lua, 5) : tb_null; + // get platform (optional, not used but passed for consistency) + tb_char_t const *plat = lua_isstring(lua, 6) ? lua_tostring(lua, 6) : tb_null; + + // get zeroend (optional, default: false) + tb_bool_t zeroend = lua_toboolean(lua, 7); + // do dump tb_bool_t ok = tb_false; tb_stream_ref_t istream = tb_stream_init_from_file(binaryfile, TB_FILE_MODE_RO); @@ -369,7 +390,7 @@ tb_int_t xm_utils_bin2coff(lua_State *lua) { break; } - if (!xm_utils_bin2coff_dump(istream, ostream, symbol_prefix, arch, basename)) { + if (!xm_utils_bin2coff_dump(istream, ostream, symbol_prefix, arch, basename, zeroend)) { lua_pushboolean(lua, tb_false); lua_pushfstring(lua, "bin2coff: dump data failed"); break; diff --git a/core/src/xmake/utils/bin2macho.c b/core/src/xmake/utils/bin2macho.c index 5499ee103..c0798fa02 100644 --- a/core/src/xmake/utils/bin2macho.c +++ b/core/src/xmake/utils/bin2macho.c @@ -300,7 +300,8 @@ static tb_bool_t xm_utils_bin2macho_dump_64(tb_stream_ref_t istream, tb_char_t const *arch, tb_char_t const *basename, tb_uint32_t minos, - tb_uint32_t sdk) { + tb_uint32_t sdk, + tb_bool_t zeroend) { tb_assert_and_check_return_val(istream && ostream, tb_false); // get file size @@ -309,6 +310,13 @@ static tb_bool_t xm_utils_bin2macho_dump_64(tb_stream_ref_t istream, return tb_false; } tb_uint32_t datasize = (tb_uint32_t)filesize; + // add null terminator if zeroend is true + if (zeroend) { + if (datasize >= 0xffffffffU) { + return tb_false; // would overflow + } + datasize++; + } // generate symbol names from filename tb_char_t symbol_name[256] = {0}; @@ -458,6 +466,13 @@ static tb_bool_t xm_utils_bin2macho_dump_64(tb_stream_ref_t istream, } left -= to_read; } + // append null terminator if zeroend is true + if (zeroend) { + tb_byte_t zero = 0; + if (!tb_stream_bwrit(ostream, &zero, 1)) { + return tb_false; + } + } // align to 8 bytes padding = symtab_offset - data_end_offset; @@ -539,9 +554,10 @@ static tb_bool_t xm_utils_bin2macho_dump(tb_stream_ref_t istream, tb_char_t const *arch, tb_char_t const *basename, tb_uint32_t minos, - tb_uint32_t sdk) { + tb_uint32_t sdk, + tb_bool_t zeroend) { if (xm_utils_bin2macho_is_64bit(arch)) { - return xm_utils_bin2macho_dump_64(istream, ostream, symbol_prefix, plat, arch, basename, minos, sdk); + return xm_utils_bin2macho_dump_64(istream, ostream, symbol_prefix, plat, arch, basename, minos, sdk, zeroend); } else { // 32-bit not implemented yet return tb_false; @@ -587,6 +603,9 @@ tb_int_t xm_utils_bin2macho(lua_State *lua) { tb_char_t const *sdk_str = lua_isstring(lua, 8) ? lua_tostring(lua, 8) : tb_null; tb_uint32_t sdk = xm_utils_bin2macho_parse_version(sdk_str); + // get zeroend (optional, default: false) + tb_bool_t zeroend = lua_toboolean(lua, 9); + // do dump tb_bool_t ok = tb_false; tb_stream_ref_t istream = tb_stream_init_from_file(binaryfile, TB_FILE_MODE_RO); @@ -605,7 +624,7 @@ tb_int_t xm_utils_bin2macho(lua_State *lua) { break; } - if (!xm_utils_bin2macho_dump(istream, ostream, symbol_prefix, plat, arch, basename, minos, sdk)) { + if (!xm_utils_bin2macho_dump(istream, ostream, symbol_prefix, plat, arch, basename, minos, sdk, zeroend)) { lua_pushboolean(lua, tb_false); lua_pushfstring(lua, "bin2macho: dump data failed"); break; diff --git a/tests/projects/other/bin2obj/xmake.lua b/tests/projects/other/bin2obj/xmake.lua index bcd64fff5..e20a329e1 100644 --- a/tests/projects/other/bin2obj/xmake.lua +++ b/tests/projects/other/bin2obj/xmake.lua @@ -6,4 +6,3 @@ target("test") add_files("src/*.c") add_files("src/*.bin") add_files("src/*.png") - diff --git a/xmake/core/sandbox/modules/utils.lua b/xmake/core/sandbox/modules/utils.lua index b3453cb92..3601c1470 100644 --- a/xmake/core/sandbox/modules/utils.lua +++ b/xmake/core/sandbox/modules/utils.lua @@ -160,8 +160,8 @@ end -- generate COFF object file from the binary file if utils.bin2coff then - function sandbox_utils.bin2coff(binaryfile, outputfile, symbol_prefix, arch, basename) - local ok, errors = utils.bin2coff(binaryfile, outputfile, symbol_prefix, arch, basename) + function sandbox_utils.bin2coff(binaryfile, outputfile, symbol_prefix, arch, basename, plat, zeroend) + local ok, errors = utils.bin2coff(binaryfile, outputfile, symbol_prefix, arch, basename, plat, zeroend) if not ok then os.raise(errors) end @@ -170,8 +170,8 @@ end -- generate Mach-O object file from the binary file if utils.bin2macho then - function sandbox_utils.bin2macho(binaryfile, outputfile, symbol_prefix, plat, arch, basename, target_minver, xcode_sdkver) - local ok, errors = utils.bin2macho(binaryfile, outputfile, symbol_prefix, plat, arch, basename, target_minver, xcode_sdkver) + function sandbox_utils.bin2macho(binaryfile, outputfile, symbol_prefix, plat, arch, basename, target_minver, xcode_sdkver, zeroend) + local ok, errors = utils.bin2macho(binaryfile, outputfile, symbol_prefix, plat, arch, basename, target_minver, xcode_sdkver, zeroend) if not ok then os.raise(errors) end diff --git a/xmake/modules/private/utils/bin2obj.lua b/xmake/modules/private/utils/bin2obj.lua index a41c919fe..2e2e4bd94 100644 --- a/xmake/modules/private/utils/bin2obj.lua +++ b/xmake/modules/private/utils/bin2obj.lua @@ -29,7 +29,8 @@ local options = { {'a', "arch", "kv", nil, "Set the target architecture."}, {'p', "plat", "kv", nil, "Set the target platform (macosx, iphoneos, etc.)."}, {nil, "target_minver", "kv", nil, "Set the target minimum version (e.g., 10.0, 18.2)."}, - {nil, "xcode_sdkver", "kv", nil, "Set the Xcode SDK version (e.g., 10.0, 18.2)."} + {nil, "xcode_sdkver", "kv", nil, "Set the Xcode SDK version (e.g., 10.0, 18.2)."}, + {nil, "zeroend", "k", nil, "Append a null terminator ('\\0') at the end of data."} } function _do_bin2obj_coff(binarypath, outputpath, opt) @@ -42,6 +43,9 @@ function _do_bin2obj_coff(binarypath, outputpath, opt) -- get platform (not used for COFF, but passed for consistency) local platform = opt.plat + -- get zeroend (default: false) + local zeroend = opt.zeroend or false + -- get filename from binary path (with extension, dots replaced with underscores) local filename = path.filename(binarypath) -- replace dots with underscores for symbol name (e.g., data.bin -> data_bin) @@ -52,7 +56,7 @@ function _do_bin2obj_coff(binarypath, outputpath, opt) -- do dump if utils.bin2coff then - utils.bin2coff(binarypath, outputpath, symbol_prefix, arch, basename) + utils.bin2coff(binarypath, outputpath, symbol_prefix, arch, basename, platform, zeroend) else raise("bin2obj: utils.bin2coff not available (C implementation not compiled)") end @@ -81,6 +85,9 @@ function _do_bin2obj_macho(binarypath, outputpath, opt) -- get xcode sdk version (default: nil, will use default 10.0.0 in C) local xcode_sdkver = opt.xcode_sdkver + -- get zeroend (default: false) + local zeroend = opt.zeroend or false + -- get filename from binary path (with extension, dots replaced with underscores) local filename = path.filename(binarypath) -- replace dots with underscores for symbol name (e.g., data.bin -> data_bin) @@ -91,7 +98,7 @@ function _do_bin2obj_macho(binarypath, outputpath, opt) -- do dump if utils.bin2macho then - utils.bin2macho(binarypath, outputpath, symbol_prefix, plat, arch, basename, target_minver, xcode_sdkver) + utils.bin2macho(binarypath, outputpath, symbol_prefix, plat, arch, basename, target_minver, xcode_sdkver, zeroend) else raise("bin2obj: utils.bin2macho not available (C implementation not compiled)") end diff --git a/xmake/rules/utils/bin2obj/xmake.lua b/xmake/rules/utils/bin2obj/xmake.lua index 1f8e6e0b2..53ceb6a51 100644 --- a/xmake/rules/utils/bin2obj/xmake.lua +++ b/xmake/rules/utils/bin2obj/xmake.lua @@ -47,6 +47,9 @@ rule("utils.bin2obj") -- get symbol prefix (default: _binary_) local symbol_prefix = target:extraconf("rules", "utils.bin2obj", "symbol_prefix") or "_binary_" + -- get zeroend (default: false) + local zeroend = target:extraconf("rules", "utils.bin2obj", "zeroend") or false + -- get architecture local arch = target:arch() @@ -81,6 +84,9 @@ rule("utils.bin2obj") if xcode_sdkver then table.insert(argv, "--xcode_sdkver=" .. xcode_sdkver) end + if zeroend then + table.insert(argv, "--zeroend") + end batchcmds:vlua("private.utils.bin2obj", argv) -- add deps -- cgit v1.3.1 From 282349621e4e7c02fa3cb7a6df2d523fbba3c9d7 Mon Sep 17 00:00:00 2001 From: ruki Date: Sun, 7 Dec 2025 12:19:22 +0800 Subject: add file zeroend support --- core/src/xmake/utils/bin2macho.c | 2 +- tests/projects/other/bin2obj/xmake.lua | 2 +- xmake/rules/utils/bin2obj/xmake.lua | 4 +++- 3 files changed, 5 insertions(+), 3 deletions(-) (limited to 'tests/projects') diff --git a/core/src/xmake/utils/bin2macho.c b/core/src/xmake/utils/bin2macho.c index c0798fa02..07c6fc347 100644 --- a/core/src/xmake/utils/bin2macho.c +++ b/core/src/xmake/utils/bin2macho.c @@ -534,7 +534,7 @@ static tb_bool_t xm_utils_bin2macho_dump_64(tb_stream_ref_t istream, tb_stream_bwrit(ostream, &null, 1); // align string table to 8 bytes - padding = strtab_size - (4 + start_len + 1 + end_len + 1); + padding = strtab_size - (4 + (tb_uint32_t)start_len + 1 + (tb_uint32_t)end_len + 1); if (padding > 0) { tb_byte_t zero = 0; while (padding-- > 0) { diff --git a/tests/projects/other/bin2obj/xmake.lua b/tests/projects/other/bin2obj/xmake.lua index e20a329e1..3f00aebf9 100644 --- a/tests/projects/other/bin2obj/xmake.lua +++ b/tests/projects/other/bin2obj/xmake.lua @@ -4,5 +4,5 @@ target("test") set_kind("binary") add_rules("utils.bin2obj", {extensions = {".bin", ".png"}}) add_files("src/*.c") - add_files("src/*.bin") + add_files("src/data.bin", {zeroend = true}) add_files("src/*.png") diff --git a/xmake/rules/utils/bin2obj/xmake.lua b/xmake/rules/utils/bin2obj/xmake.lua index 53ceb6a51..d35e50606 100644 --- a/xmake/rules/utils/bin2obj/xmake.lua +++ b/xmake/rules/utils/bin2obj/xmake.lua @@ -48,7 +48,9 @@ rule("utils.bin2obj") local symbol_prefix = target:extraconf("rules", "utils.bin2obj", "symbol_prefix") or "_binary_" -- get zeroend (default: false) - local zeroend = target:extraconf("rules", "utils.bin2obj", "zeroend") or 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 -- get architecture local arch = target:arch() -- cgit v1.3.1 From bbfd1bd9a4ae413354f37f46935097cf591b6c6c Mon Sep 17 00:00:00 2001 From: ruki Date: Sun, 7 Dec 2025 12:29:56 +0800 Subject: improve dump bin2obj --- tests/projects/other/bin2obj/src/image.png | 1 - tests/projects/other/bin2obj/src/main.c | 58 ++++++++++++++++++++++++++--- tests/projects/other/bin2obj/src/xmake.ico | Bin 0 -> 119943 bytes tests/projects/other/bin2obj/xmake.lua | 4 +- 4 files changed, 55 insertions(+), 8 deletions(-) delete mode 100644 tests/projects/other/bin2obj/src/image.png create mode 100644 tests/projects/other/bin2obj/src/xmake.ico (limited to 'tests/projects') diff --git a/tests/projects/other/bin2obj/src/image.png b/tests/projects/other/bin2obj/src/image.png deleted file mode 100644 index d2c2e1ac6..000000000 --- a/tests/projects/other/bin2obj/src/image.png +++ /dev/null @@ -1 +0,0 @@ -fake image diff --git a/tests/projects/other/bin2obj/src/main.c b/tests/projects/other/bin2obj/src/main.c index 196247654..bc1d2442f 100644 --- a/tests/projects/other/bin2obj/src/main.c +++ b/tests/projects/other/bin2obj/src/main.c @@ -5,15 +5,30 @@ extern const uint8_t _binary_data_bin_start[]; extern const uint8_t _binary_data_bin_end[]; -extern const uint8_t _binary_image_png_start[]; -extern const uint8_t _binary_image_png_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; } - for (uint32_t offset = 0; offset < size; offset += 16) { + + // 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); @@ -37,14 +52,47 @@ 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) { + 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_image_png_size = (uint32_t)(_binary_image_png_end - _binary_image_png_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("image.png", _binary_image_png_start, _binary_image_png_size); + 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 new file mode 100644 index 000000000..52c8ef389 Binary files /dev/null and b/tests/projects/other/bin2obj/src/xmake.ico differ diff --git a/tests/projects/other/bin2obj/xmake.lua b/tests/projects/other/bin2obj/xmake.lua index 3f00aebf9..ed0de51fc 100644 --- a/tests/projects/other/bin2obj/xmake.lua +++ b/tests/projects/other/bin2obj/xmake.lua @@ -2,7 +2,7 @@ add_rules("mode.debug", "mode.release") target("test") set_kind("binary") - add_rules("utils.bin2obj", {extensions = {".bin", ".png"}}) + add_rules("utils.bin2obj", {extensions = {".bin", ".ico"}}) add_files("src/*.c") add_files("src/data.bin", {zeroend = true}) - add_files("src/*.png") + add_files("src/*.ico") -- cgit v1.3.1 From 58045d685b6daea07dbc641e84eaaa8527c6a189 Mon Sep 17 00:00:00 2001 From: ruki Date: Sun, 7 Dec 2025 12:33:31 +0800 Subject: improve bin2c demo --- tests/projects/other/bin2c/src/image.png | 1 - tests/projects/other/bin2c/src/main.c | 90 +++++++++++++++++++++++++++++-- tests/projects/other/bin2c/src/xmake.ico | Bin 0 -> 119943 bytes tests/projects/other/bin2c/xmake.lua | 4 +- tests/projects/other/bin2obj/xmake.lua | 2 +- xmake/rules/utils/bin2c/xmake.lua | 8 ++- 6 files changed, 96 insertions(+), 9 deletions(-) delete mode 100644 tests/projects/other/bin2c/src/image.png create mode 100644 tests/projects/other/bin2c/src/xmake.ico (limited to 'tests/projects') 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 +#include +#include 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 new file mode 100644 index 000000000..52c8ef389 Binary files /dev/null and b/tests/projects/other/bin2c/src/xmake.ico differ 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 -- cgit v1.3.1