diff options
| author | ruki <[email protected]> | 2024-10-25 11:57:02 +0800 |
|---|---|---|
| committer | GitHub <[email protected]> | 2024-10-25 11:57:02 +0800 |
| commit | 7a95d412fee5a670737068869b811d295f954cfd (patch) | |
| tree | e1fbe36e6d50d451ec71237cfa2f2069fd7dde28 /xmake/modules/private/utils/bin2c.lua | |
| parent | deb55641c247a86ed0321075977d9a067f73b00d (diff) | |
| parent | 9ab3585df20a4b59e75675d86eb488154c0449e9 (diff) | |
Merge pull request #5762 from xmake-io/bin2c
Use native implementation to improve bin2c speed
Diffstat (limited to 'xmake/modules/private/utils/bin2c.lua')
| -rw-r--r-- | xmake/modules/private/utils/bin2c.lua | 37 |
1 files changed, 28 insertions, 9 deletions
diff --git a/xmake/modules/private/utils/bin2c.lua b/xmake/modules/private/utils/bin2c.lua index 6f18833fc..29c89338e 100644 --- a/xmake/modules/private/utils/bin2c.lua +++ b/xmake/modules/private/utils/bin2c.lua @@ -35,7 +35,7 @@ function _do_dump(binarydata, outputfile, opt) local p = 0 local e = binarydata:size() local line = nil - local linewidth = opt.linewidth or 0x20 + local linewidth = opt.linewidth or 32 local first = true while p < e do line = "" @@ -81,22 +81,41 @@ function _do_bin2c(binarypath, outputpath, opt) -- trace print("generating code data file from %s ..", binarypath) + -- optimize the default linewidth for reading large file + if not opt.linewidth then + local filesize = os.filesize(binarypath) + if filesize > 1024 * 1024 * 1024 then + opt.linewidth = 512 + elseif filesize > 100 * 1024 * 1024 then + opt.linewidth = 256 + elseif filesize > 10 * 1024 * 1024 then + opt.linewidth = 128 + elseif filesize > 1024 * 1024 then + opt.linewidth = 64 + else + opt.linewidth = 32 + end + end + -- do dump - local binarydata = bytes(io.readfile(binarypath, {encoding = "binary"})) - local outputfile = io.open(outputpath, 'w') - if outputfile then - if not opt.nozeroend then - binarydata = binarydata .. bytes('\0') + if utils.bin2c then + utils.bin2c(binarypath, outputpath, opt) + else + local binarydata = bytes(io.readfile(binarypath, {encoding = "binary"})) + local outputfile = io.open(outputpath, 'w') + if outputfile then + if not opt.nozeroend then + binarydata = binarydata .. bytes('\0') + end + _do_dump(binarydata, outputfile, opt) + outputfile:close() end - _do_dump(binarydata, outputfile, opt) - outputfile:close() end -- trace cprint("${bright}%s generated!", outputpath) end --- main entry function main(...) -- parse arguments |
