From b700ce5f896f1a3edfadafb4c188d1f147ea4ac7 Mon Sep 17 00:00:00 2001 From: ruki Date: Thu, 5 Aug 2021 00:29:18 +0800 Subject: add bin2c module --- tests/projects/other/bin2c/.gitignore | 8 +++ tests/projects/other/bin2c/src/data.bin | 1 + tests/projects/other/bin2c/src/main.c | 11 ++++ tests/projects/other/bin2c/test.lua | 3 + tests/projects/other/bin2c/xmake.lua | 9 +++ xmake/modules/private/utils/bin2c.lua | 106 ++++++++++++++++++++++++++++++++ xmake/rules/c++/utils/bin2h/xmake.lua | 47 -------------- xmake/rules/utils/bin2c/xmake.lua | 47 ++++++++++++++ 8 files changed, 185 insertions(+), 47 deletions(-) create mode 100644 tests/projects/other/bin2c/.gitignore create mode 100644 tests/projects/other/bin2c/src/data.bin create mode 100644 tests/projects/other/bin2c/src/main.c create mode 100644 tests/projects/other/bin2c/test.lua create mode 100644 tests/projects/other/bin2c/xmake.lua create mode 100644 xmake/modules/private/utils/bin2c.lua delete mode 100644 xmake/rules/c++/utils/bin2h/xmake.lua create mode 100644 xmake/rules/utils/bin2c/xmake.lua 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 + +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/c++/utils/bin2h/xmake.lua deleted file mode 100644 index 5e18090df..000000000 --- a/xmake/rules/c++/utils/bin2h/xmake.lua +++ /dev/null @@ -1,47 +0,0 @@ ---!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 xmake.lua --- - -rule("c++.utils.bin2h") - set_extensions(".bin") - on_load(function (target) - local headerdir = path.join(target:autogendir(), "rules", "c++", "bin2h") - if not os.isfile(headerdir) then - os.mkdir(headerdir) - end - target:add("includedirs", headerdir) - end) - before_buildcmd_file(function (target, batchcmds, sourcefile_bin, opt) - - -- get header file - local headerdir = path.join(target:autogendir(), "rules", "c++", "bin2h") - 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 - - -- add deps - batchcmds:add_depfiles(sourcefile_bin) - batchcmds:set_depmtime(os.mtime(headerfile)) - batchcmds:set_depcache(target:dependfile(headerfile)) - end) - diff --git a/xmake/rules/utils/bin2c/xmake.lua b/xmake/rules/utils/bin2c/xmake.lua new file mode 100644 index 000000000..658bf113a --- /dev/null +++ b/xmake/rules/utils/bin2c/xmake.lua @@ -0,0 +1,47 @@ +--!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 xmake.lua +-- + +rule("utils.bin2c") + set_extensions(".bin") + on_load(function (target) + local headerdir = path.join(target:autogendir(), "rules", "c++", "bin2c") + if not os.isfile(headerdir) then + os.mkdir(headerdir) + end + target:add("includedirs", headerdir) + end) + before_buildcmd_file(function (target, batchcmds, sourcefile_bin, opt) + + -- get header file + 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) + local argv = {"-i", sourcefile_bin, "-o", headerfile, "-w", "32"} + batchcmds:vrunv(os.programfile(), argv) + + -- add deps + batchcmds:add_depfiles(sourcefile_bin) + batchcmds:set_depmtime(os.mtime(headerfile)) + batchcmds:set_depcache(target:dependfile(headerfile)) + end) + -- cgit v1.3.1