summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorruki <[email protected]>2024-10-25 00:44:03 +0800
committerruki <[email protected]>2024-10-25 00:44:03 +0800
commitc92b5d009f2421ee3cda1933c5cac519659785f1 (patch)
treee4563b150e4453727d531ca865b9f531a8c6a668
parentdeb55641c247a86ed0321075977d9a067f73b00d (diff)
add native bin2c
-rw-r--r--core/src/xmake/engine.c13
-rw-r--r--core/src/xmake/utils/bin2c.c58
-rw-r--r--core/src/xmake/utils/prefix.h31
-rwxr-xr-xcore/src/xmake/xmake.sh1
-rw-r--r--xmake/core/base/utils.lua9
-rw-r--r--xmake/core/sandbox/modules/utils.lua8
-rw-r--r--xmake/modules/private/utils/bin2c.lua20
7 files changed, 132 insertions, 8 deletions
diff --git a/core/src/xmake/engine.c b/core/src/xmake/engine.c
index 5fab7e7c1..8753607d4 100644
--- a/core/src/xmake/engine.c
+++ b/core/src/xmake/engine.c
@@ -316,6 +316,9 @@ tb_int_t xm_tty_term_mode(lua_State* lua);
// the package functions
tb_int_t xm_package_loadxmi(lua_State* lua);
+// the utils functions
+tb_int_t xm_utils_bin2c(lua_State* lua);
+
#ifdef XM_CONFIG_API_HAVE_CURSES
// register curses functions
tb_int_t xm_lua_curses_register(lua_State* lua, tb_char_t const* module);
@@ -600,6 +603,13 @@ static luaL_Reg const g_package_functions[] =
, { tb_null, tb_null }
};
+// the utils functions
+static luaL_Reg const g_utils_functions[] =
+{
+ { "bin2c", xm_utils_bin2c }
+, { tb_null, tb_null }
+};
+
// the lua global instance for signal handler
static lua_State* g_lua = tb_null;
@@ -1364,6 +1374,9 @@ xm_engine_ref_t xm_engine_init(tb_char_t const* name, xm_engine_lni_initalizer_c
// bind package functions
xm_lua_register(engine->lua, "package", g_package_functions);
+ // bind utils functions
+ xm_lua_register(engine->lua, "utils", g_utils_functions);
+
#ifdef XM_CONFIG_API_HAVE_CURSES
// bind curses
xm_lua_curses_register(engine->lua, "curses");
diff --git a/core/src/xmake/utils/bin2c.c b/core/src/xmake/utils/bin2c.c
new file mode 100644
index 000000000..1ce85654c
--- /dev/null
+++ b/core/src/xmake/utils/bin2c.c
@@ -0,0 +1,58 @@
+/*!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.c
+ *
+ */
+
+/* //////////////////////////////////////////////////////////////////////////////////////
+ * trace
+ */
+#define TB_TRACE_MODULE_NAME "bin2c"
+#define TB_TRACE_MODULE_DEBUG (0)
+
+/* //////////////////////////////////////////////////////////////////////////////////////
+ * includes
+ */
+#include "prefix.h"
+
+/* //////////////////////////////////////////////////////////////////////////////////////
+ * implementation
+ */
+
+/* generate c/c++ code from the binary file
+ *
+ * local ok, errors = utils.bin2c(binaryfile, outputfile, linewidth, nozeroend)
+ */
+tb_int_t xm_utils_bin2c(lua_State* lua)
+{
+ // check
+ tb_assert_and_check_return_val(lua, 0);
+
+ // get the binaryfile
+ tb_char_t const* binaryfile = luaL_checkstring(lua, 1);
+ tb_check_return_val(binaryfile, 0);
+
+ // get the outputfile
+ tb_char_t const* outputfile = luaL_checkstring(lua, 2);
+ tb_check_return_val(outputfile, 0);
+
+ tb_trace_i("binaryfile: %s", binaryfile);
+ tb_trace_i("outputfile: %s", outputfile);
+
+ return 0;
+}
diff --git a/core/src/xmake/utils/prefix.h b/core/src/xmake/utils/prefix.h
new file mode 100644
index 000000000..e56baedf7
--- /dev/null
+++ b/core/src/xmake/utils/prefix.h
@@ -0,0 +1,31 @@
+/*!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 prefix.h
+ *
+ */
+#ifndef XM_UTILS_PREFIX_H
+#define XM_UTILS_PREFIX_H
+
+/* //////////////////////////////////////////////////////////////////////////////////////
+ * includes
+ */
+#include "../prefix.h"
+
+#endif
+
+
diff --git a/core/src/xmake/xmake.sh b/core/src/xmake/xmake.sh
index 62af5b2fa..8bcc60632 100755
--- a/core/src/xmake/xmake.sh
+++ b/core/src/xmake/xmake.sh
@@ -71,6 +71,7 @@ target "xmake"
add_files "semver/*.c"
add_files "string/*.c"
add_files "tty/*.c"
+ add_files "utils/*.c"
if is_plat "mingw"; then
add_files "winos/*.c"
fi
diff --git a/xmake/core/base/utils.lua b/xmake/core/base/utils.lua
index 5125d224e..2beab7bce 100644
--- a/xmake/core/base/utils.lua
+++ b/xmake/core/base/utils.lua
@@ -30,6 +30,9 @@ local io = require("base/io")
local dump = require("base/dump")
local text = require("base/text")
+-- save original interfaces
+utils._bin2c = utils._bin2c or utils.bin2c
+
-- dump values
function utils.dump(...)
if option.get("quiet") then
@@ -318,5 +321,11 @@ function utils.vtable(data, opt)
utils.vprintf(text.table(data, opt))
end
+-- generate c/c++ code from the binary file
+function utils.bin2c(binaryfile, outputfile, opt)
+ opt = opt or {}
+ return utils._bin2c(binaryfile, outputfile, opt.linewidth, opt.nozeroend)
+end
+
-- return module
return utils
diff --git a/xmake/core/sandbox/modules/utils.lua b/xmake/core/sandbox/modules/utils.lua
index 42660d36e..220d95ec5 100644
--- a/xmake/core/sandbox/modules/utils.lua
+++ b/xmake/core/sandbox/modules/utils.lua
@@ -148,6 +148,14 @@ function sandbox_utils.assert(value, format, ...)
return value
end
+-- generate c/c++ code from the binary file
+function sandbox_utils.bin2c(binaryfile, outputfile, opt)
+ local ok, errors = utils.bin2c(binaryfile, outputfile, opt)
+ if not ok then
+ os.raise(errors)
+ end
+end
+
-- return module
return sandbox_utils
diff --git a/xmake/modules/private/utils/bin2c.lua b/xmake/modules/private/utils/bin2c.lua
index 6f18833fc..31b400954 100644
--- a/xmake/modules/private/utils/bin2c.lua
+++ b/xmake/modules/private/utils/bin2c.lua
@@ -20,6 +20,7 @@
-- imports
import("core.base.bytes")
+import("core.base.utils")
import("core.base.option")
local options = {
@@ -82,21 +83,24 @@ function _do_bin2c(binarypath, outputpath, opt)
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 not opt.nozeroend then
- binarydata = binarydata .. bytes('\0')
+ if utils.bin2c then
+ utils.bin2c(binarydata, 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