diff options
| author | ruki <[email protected]> | 2021-12-05 22:59:47 +0800 |
|---|---|---|
| committer | GitHub <[email protected]> | 2021-12-05 22:59:47 +0800 |
| commit | 23c04732766ba2db5e5caea09d7d31f7d8631a97 (patch) | |
| tree | d16aa51b261711aacea68d8370f0dadb6f8cbf68 /xmake/rules/utils/bin2c/xmake.lua | |
| parent | 2817f6844f6e1a5ed8c17a9c3b83bc18690b1f1f (diff) | |
Update xmake.lua
Diffstat (limited to 'xmake/rules/utils/bin2c/xmake.lua')
| -rw-r--r-- | xmake/rules/utils/bin2c/xmake.lua | 114 |
1 files changed, 86 insertions, 28 deletions
diff --git a/xmake/rules/utils/bin2c/xmake.lua b/xmake/rules/utils/bin2c/xmake.lua index 77f81125c..79b52fe73 100644 --- a/xmake/rules/utils/bin2c/xmake.lua +++ b/xmake/rules/utils/bin2c/xmake.lua @@ -15,39 +15,97 @@ -- Copyright (C) 2015-present, TBOOX Open Source Group. -- -- @author ruki --- @file xmake.lua +-- @file bin2c.lua -- -rule("utils.bin2c") - set_extensions(".bin") - on_load(function (target) - local headerdir = path.join(target:autogendir(), "rules", "utils", "bin2c") - if not os.isdir(headerdir) then - os.mkdir(headerdir) +-- imports +import("core.base.bytes") +import("core.base.option") + +local options = { + {'w', "linewidth", "kv", nil, "Set the line width"}, + {nil, "nozeroend", "k", false, "Disable to patch zero terminating character"}, + {'i', "binarypath", "kv", nil, "Set the binary file path."}, + {'o', "outputpath", "kv", nil, "Set the output file path."} +} + +function _do_dump(binarydata, outputfile, opt) + local i = 0 + local n = 147 + local p = 0 + local e = binarydata:size() + local line = nil + local linewidth = opt.linewidth or 0x20 + local first = true + while p < e do + line = "" + if p + linewidth <= e then + for i = 0, linewidth - 1 do + if first then + first = false + line = line .. " " + else + line = line .. "," + end + line = line .. string.format(" 0x%02X", binarydata[p + i + 1]) + end + outputfile:print(line) + p = p + linewidth + elseif p < e then + local left = e - p + for i = 0, left - 1 do + if first then + first = false + line = line .. " " + else + line = line .. "," + end + line = line .. string.format(" 0x%02X", binarydata[p + i + 1]) + end + outputfile:print(line) + p = p + left + else + break end - target:add("includedirs", headerdir) - end) - before_buildcmd_file(function (target, batchcmds, sourcefile_bin, opt) + end +end + +function _do_bin2c(binarypath, outputpath, opt) - -- get header file - local headerdir = path.join(target:autogendir(), "rules", "utils", "bin2c") - local headerfile = path.join(headerdir, path.filename(sourcefile_bin) .. ".h") - target:add("includedirs", headerdir) + -- init source directory and options + opt = opt or {} + binarypath = path.absolute(binarypath) + outputpath = path.absolute(outputpath) + assert(os.isfile(binarypath), "%s not found!", binarypath) - -- add commands - batchcmds:show_progress(opt.progress, "${color.build.object}generating.bin2c %s", sourcefile_bin) - batchcmds:mkdir(headerdir) - 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)) + -- trace + print("generating code data file from %s ..", binarypath) + + -- do dump + local binarydata = bytes(io.readfile(binarypath, {encoding = "binary"})) + local outputfile = io.open(outputpath, 'w') + if outputfile then + if opt.nozeroend then + binarydata = binarydata .. bytes('\0') end - batchcmds:vrunv(os.programfile(), argv, {envs = {XMAKE_SKIP_HISTORY = "y"}}) + _do_dump(binarydata, outputfile, opt) + outputfile:close() + end + + -- trace + cprint("${bright}%s generated!", outputpath) +end + +-- main entry +function main(...) - -- add deps - batchcmds:add_depfiles(sourcefile_bin) - batchcmds:set_depmtime(os.mtime(headerfile)) - batchcmds:set_depcache(target:dependfile(headerfile)) - end) + print("sss") + -- parse arguments + local argv = {...} + local opt = option.parse(argv, options, "Print c/c++ code files from the given binary file." + , "" + , "Usage: xmake l private.utils.bin2c [options]") + -- do bin2c + _do_bin2c(opt.binarypath, opt.outputpath, opt) +end |
