diff options
| author | ruki <[email protected]> | 2021-08-05 00:29:18 +0800 |
|---|---|---|
| committer | ruki <[email protected]> | 2021-08-05 00:29:18 +0800 |
| commit | b700ce5f896f1a3edfadafb4c188d1f147ea4ac7 (patch) | |
| tree | 298f5a01927bc8ce08eb5d0911f9f11d3f330d0d | |
| parent | 87d159290c9d9f5ad00e26640cb091f6ac068d05 (diff) | |
add bin2c module
| -rw-r--r-- | tests/projects/other/bin2c/.gitignore | 8 | ||||
| -rw-r--r-- | tests/projects/other/bin2c/src/data.bin | 1 | ||||
| -rw-r--r-- | tests/projects/other/bin2c/src/main.c | 11 | ||||
| -rw-r--r-- | tests/projects/other/bin2c/test.lua | 3 | ||||
| -rw-r--r-- | tests/projects/other/bin2c/xmake.lua | 9 | ||||
| -rw-r--r-- | xmake/modules/private/utils/bin2c.lua | 106 | ||||
| -rw-r--r-- | xmake/rules/utils/bin2c/xmake.lua (renamed from xmake/rules/c++/utils/bin2h/xmake.lua) | 10 |
7 files changed, 143 insertions, 5 deletions
diff --git a/tests/projects/other/bin2c/.gitignore b/tests/projects/other/bin2c/.gitignore new file mode 100644 index 000000000..152105761 --- /dev/null +++ b/tests/projects/other/bin2c/.gitignore @@ -0,0 +1,8 @@ +# Xmake cache +.xmake/ +build/ + +# MacOS Cache +.DS_Store + + diff --git a/tests/projects/other/bin2c/src/data.bin b/tests/projects/other/bin2c/src/data.bin new file mode 100644 index 000000000..c499e585b --- /dev/null +++ b/tests/projects/other/bin2c/src/data.bin @@ -0,0 +1 @@ +"hello xmake!" diff --git a/tests/projects/other/bin2c/src/main.c b/tests/projects/other/bin2c/src/main.c new file mode 100644 index 000000000..650531dcb --- /dev/null +++ b/tests/projects/other/bin2c/src/main.c @@ -0,0 +1,11 @@ +#include <stdio.h> + +static unsigned char g_bin_data[] = { + #include "data.bin.h" +}; + +int main(int argc, char** argv) +{ + printf("bin2c: %s\n", g_bin_data); + return 0; +} diff --git a/tests/projects/other/bin2c/test.lua b/tests/projects/other/bin2c/test.lua new file mode 100644 index 000000000..b57362078 --- /dev/null +++ b/tests/projects/other/bin2c/test.lua @@ -0,0 +1,3 @@ +function main(t) + t:build() +end diff --git a/tests/projects/other/bin2c/xmake.lua b/tests/projects/other/bin2c/xmake.lua new file mode 100644 index 000000000..4cd6f95ae --- /dev/null +++ b/tests/projects/other/bin2c/xmake.lua @@ -0,0 +1,9 @@ +add_rules("mode.debug", "mode.release") + +target("test") + set_kind("binary") + add_files("src/*.c") + add_files("src/*.bin") + add_rules("utils.bin2c") + + diff --git a/xmake/modules/private/utils/bin2c.lua b/xmake/modules/private/utils/bin2c.lua new file mode 100644 index 000000000..c6022eba0 --- /dev/null +++ b/xmake/modules/private/utils/bin2c.lua @@ -0,0 +1,106 @@ +--!A cross-platform build utility based on Lua +-- +-- Licensed under the Apache License, Version 2.0 (the "License"); +-- you may not use this file except in compliance with the License. +-- You may obtain a copy of the License at +-- +-- http://www.apache.org/licenses/LICENSE-2.0 +-- +-- Unless required by applicable law or agreed to in writing, software +-- distributed under the License is distributed on an "AS IS" BASIS, +-- WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +-- See the License for the specific language governing permissions and +-- limitations under the License. +-- +-- Copyright (C) 2015-present, TBOOX Open Source Group. +-- +-- @author ruki +-- @file bin2c.lua +-- + +-- imports +import("core.base.bytes") +import("core.base.option") + +local options = { + {'w', "linewidth", "kv", nil, "Set the line width"}, + {'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 + end +end + +function _do_bin2c(binarypath, outputpath, opt) + + -- init source directory and options + opt = opt or {} + binarypath = path.absolute(binarypath) + outputpath = path.absolute(outputpath) + assert(os.isfile(binarypath), "%s not found!", binarypath) + + -- trace + print("generating code data file from %s ..", binarypath) + + -- do dump + local binarydata = bytes(io.readfile(binarypath)) + local outputfile = io.open(outputpath, 'w') + if outputfile then + _do_dump(binarydata, outputfile, opt) + outputfile:close() + end + + -- trace + cprint("${bright}%s generated!", outputpath) +end + +-- main entry +function main(...) + + -- 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 diff --git a/xmake/rules/c++/utils/bin2h/xmake.lua b/xmake/rules/utils/bin2c/xmake.lua index 5e18090df..658bf113a 100644 --- a/xmake/rules/c++/utils/bin2h/xmake.lua +++ b/xmake/rules/utils/bin2c/xmake.lua @@ -18,10 +18,10 @@ -- @file xmake.lua -- -rule("c++.utils.bin2h") +rule("utils.bin2c") set_extensions(".bin") on_load(function (target) - local headerdir = path.join(target:autogendir(), "rules", "c++", "bin2h") + local headerdir = path.join(target:autogendir(), "rules", "c++", "bin2c") if not os.isfile(headerdir) then os.mkdir(headerdir) end @@ -30,14 +30,14 @@ rule("c++.utils.bin2h") before_buildcmd_file(function (target, batchcmds, sourcefile_bin, opt) -- get header file - local headerdir = path.join(target:autogendir(), "rules", "c++", "bin2h") + local headerdir = path.join(target:autogendir(), "rules", "c++", "bin2c") local headerfile = path.join(headerdir, (sourcefile_bin:gsub("%.", "_")) .. ".h") -- add commands batchcmds:show_progress(opt.progress, "${color.build.object}generating.header %s", sourcefile_bin) batchcmds:mkdir(headerdir) - - -- TODO + local argv = {"-i", sourcefile_bin, "-o", headerfile, "-w", "32"} + batchcmds:vrunv(os.programfile(), argv) -- add deps batchcmds:add_depfiles(sourcefile_bin) |
