summaryrefslogtreecommitdiff
path: root/xmake
diff options
context:
space:
mode:
Diffstat (limited to 'xmake')
-rw-r--r--xmake/core/base/binutils.lua61
-rw-r--r--xmake/core/base/utils.lua8
-rw-r--r--xmake/core/sandbox/modules/import/core/base/binutils.lua62
-rw-r--r--xmake/core/sandbox/modules/utils.lua13
-rw-r--r--xmake/modules/utils/binary/bin2c.lua (renamed from xmake/modules/private/utils/bin2c.lua)8
-rw-r--r--xmake/modules/utils/binary/bin2obj.lua147
-rw-r--r--xmake/rules/utils/bin2c/xmake.lua10
-rw-r--r--xmake/rules/utils/bin2obj/xmake.lua98
-rw-r--r--xmake/rules/utils/glsl2spv/xmake.lua2
-rw-r--r--xmake/rules/utils/hlsl2spv/xmake.lua2
-rw-r--r--xmake/rules/xmake_cli/xmake.lua2
11 files changed, 389 insertions, 24 deletions
diff --git a/xmake/core/base/binutils.lua b/xmake/core/base/binutils.lua
new file mode 100644
index 000000000..b21de2276
--- /dev/null
+++ b/xmake/core/base/binutils.lua
@@ -0,0 +1,61 @@
+--!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, Xmake Open Source Community.
+--
+-- @author ruki
+-- @file binutils.lua
+--
+
+-- define module
+local binutils = binutils or {}
+
+-- save original interfaces
+binutils._bin2c = binutils._bin2c or binutils.bin2c
+binutils._bin2coff = binutils._bin2coff or binutils.bin2coff
+binutils._bin2macho = binutils._bin2macho or binutils.bin2macho
+binutils._bin2elf = binutils._bin2elf or binutils.bin2elf
+
+-- generate c/c++ code from the binary file
+function binutils.bin2c(binaryfile, outputfile, opt)
+ opt = opt or {}
+ if binutils._bin2c then
+ return binutils._bin2c(binaryfile, outputfile, opt.linewidth or 32, opt.nozeroend or false)
+ else
+ -- fallback to old implementation if C implementation not available
+ return nil, "bin2c: C implementation not available"
+ end
+end
+
+-- generate COFF object file from the binary file
+function binutils.bin2coff(binaryfile, outputfile, opt)
+ opt = opt or {}
+ return binutils._bin2coff(binaryfile, outputfile, opt.symbol_prefix or "_binary_", opt.arch or "x86_64", opt.basename, opt.zeroend or false)
+end
+
+-- generate Mach-O object file from the binary file
+function binutils.bin2macho(binaryfile, outputfile, opt)
+ opt = opt or {}
+ return binutils._bin2macho(binaryfile, outputfile, opt.symbol_prefix or "_binary_", opt.plat or "macosx", opt.arch or "x86_64", opt.basename, opt.target_minver, opt.xcode_sdkver, opt.zeroend or false)
+end
+
+-- generate ELF object file from the binary file
+function binutils.bin2elf(binaryfile, outputfile, opt)
+ opt = opt or {}
+ return binutils._bin2elf(binaryfile, outputfile, opt.symbol_prefix or "_binary_", opt.arch or "x86_64", opt.basename, opt.zeroend or false)
+end
+
+-- return module
+return binutils
+
diff --git a/xmake/core/base/utils.lua b/xmake/core/base/utils.lua
index ac9884851..ddf8c127f 100644
--- a/xmake/core/base/utils.lua
+++ b/xmake/core/base/utils.lua
@@ -30,8 +30,6 @@ 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(...)
@@ -337,11 +335,5 @@ 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 or 32, opt.nozeroend or false)
-end
-
-- return module
return utils
diff --git a/xmake/core/sandbox/modules/import/core/base/binutils.lua b/xmake/core/sandbox/modules/import/core/base/binutils.lua
new file mode 100644
index 000000000..38aad9b73
--- /dev/null
+++ b/xmake/core/sandbox/modules/import/core/base/binutils.lua
@@ -0,0 +1,62 @@
+--!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, Xmake Open Source Community.
+--
+-- @author ruki
+-- @file binutils.lua
+--
+
+-- define module
+local sandbox_core_base_binutils = sandbox_core_base_binutils or {}
+
+-- load modules
+local binutils = require("base/binutils")
+local raise = require("sandbox/modules/raise")
+
+-- generate c/c++ code from the binary file
+function sandbox_core_base_binutils.bin2c(binaryfile, outputfile, opt)
+ local ok, errors = binutils.bin2c(binaryfile, outputfile, opt)
+ if not ok then
+ raise("bin2c: %s", errors or "unknown errors")
+ end
+end
+
+-- generate COFF object file from the binary file
+function sandbox_core_base_binutils.bin2coff(binaryfile, outputfile, opt)
+ local ok, errors = binutils.bin2coff(binaryfile, outputfile, opt)
+ if not ok then
+ raise("bin2coff: %s", errors or "unknown errors")
+ end
+end
+
+-- generate Mach-O object file from the binary file
+function sandbox_core_base_binutils.bin2macho(binaryfile, outputfile, opt)
+ local ok, errors = binutils.bin2macho(binaryfile, outputfile, opt)
+ if not ok then
+ raise("bin2macho: %s", errors or "unknown errors")
+ end
+end
+
+-- generate ELF object file from the binary file
+function sandbox_core_base_binutils.bin2elf(binaryfile, outputfile, opt)
+ local ok, errors = binutils.bin2elf(binaryfile, outputfile, opt)
+ if not ok then
+ raise("bin2elf: %s", errors or "unknown errors")
+ end
+end
+
+-- return module
+return sandbox_core_base_binutils
+
diff --git a/xmake/core/sandbox/modules/utils.lua b/xmake/core/sandbox/modules/utils.lua
index 88ccd6bac..df90492be 100644
--- a/xmake/core/sandbox/modules/utils.lua
+++ b/xmake/core/sandbox/modules/utils.lua
@@ -26,6 +26,7 @@ local colors = require("base/colors")
local option = require("base/option")
local log = require("base/log")
local deprecated = require("base/deprecated")
+local binutils = require("base/binutils")
local try = require("sandbox/modules/try")
local catch = require("sandbox/modules/catch")
local vformat = require("sandbox/modules/vformat")
@@ -148,14 +149,10 @@ function sandbox_utils.assert(value, format, ...)
return value
end
--- generate c/c++ code from the binary file
-if utils._bin2c then
- function sandbox_utils.bin2c(binaryfile, outputfile, opt)
- local ok, errors = utils.bin2c(binaryfile, outputfile, opt)
- if not ok then
- os.raise(errors)
- end
- end
+-- generate c/c++ code from the binary file (deprecated, use binutils.bin2c instead)
+function sandbox_utils.bin2c(binaryfile, outputfile, opt)
+ deprecated.add("binutils.bin2c", "utils.bin2c")
+ return binutils.bin2c(binaryfile, outputfile, opt)
end
-- return module
diff --git a/xmake/modules/private/utils/bin2c.lua b/xmake/modules/utils/binary/bin2c.lua
index 0d6f06a1d..34d9c4e02 100644
--- a/xmake/modules/private/utils/bin2c.lua
+++ b/xmake/modules/utils/binary/bin2c.lua
@@ -21,6 +21,7 @@
-- imports
import("core.base.bytes")
import("core.base.option")
+import("core.base.binutils")
local options = {
{'w', "linewidth", "kv", nil, "Set the line width"},
@@ -98,8 +99,8 @@ function _do_bin2c(binarypath, outputpath, opt)
end
-- do dump
- if utils.bin2c then
- utils.bin2c(binarypath, outputpath, opt)
+ if binutils.bin2c then
+ binutils.bin2c(binarypath, outputpath, opt)
else
local binarydata = bytes(io.readfile(binarypath, {encoding = "binary"}))
local outputfile = io.open(outputpath, 'w')
@@ -122,8 +123,9 @@ function main(...)
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]")
+ , "Usage: xmake l utils.binary.bin2c [options]")
-- do bin2c
_do_bin2c(opt.binarypath, opt.outputpath, opt)
end
+
diff --git a/xmake/modules/utils/binary/bin2obj.lua b/xmake/modules/utils/binary/bin2obj.lua
new file mode 100644
index 000000000..10d396551
--- /dev/null
+++ b/xmake/modules/utils/binary/bin2obj.lua
@@ -0,0 +1,147 @@
+--!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, Xmake Open Source Community.
+--
+-- @author ruki
+-- @file bin2obj.lua
+--
+
+-- imports
+import("core.base.option")
+import("core.base.binutils")
+
+local options = {
+ {'i', "binarypath", "kv", nil, "Set the binary file path."},
+ {'o', "outputpath", "kv", nil, "Set the output object file path."},
+ {'f', "format", "kv", nil, "Set the object file format (coff, elf, macho)."},
+ {nil, "symbol_prefix", "kv", nil, "Set the symbol prefix (default: _binary_)."},
+ {'a', "arch", "kv", nil, "Set the target architecture."},
+ {'p', "plat", "kv", nil, "Set the target platform (macosx, iphoneos, etc.)."},
+ {nil, "target_minver", "kv", nil, "Set the target minimum version (e.g., 10.0, 18.2)."},
+ {nil, "xcode_sdkver", "kv", nil, "Set the Xcode SDK version (e.g., 10.0, 18.2)."},
+ {nil, "zeroend", "k", nil, "Append a null terminator ('\\0') at the end of data."}
+}
+
+function _do_bin2obj_coff(binarypath, outputpath, opt)
+ -- get filename from binary path (with extension, dots replaced with underscores)
+ local filename = path.filename(binarypath)
+ -- replace dots with underscores for symbol name (e.g., data.bin -> data_bin)
+ local basename = filename:gsub("%.", "_")
+
+ -- prepare opt
+ opt = opt or {}
+ opt.basename = basename
+
+ -- trace
+ print("converting binary file %s to COFF object file %s ..", binarypath, outputpath)
+
+ -- do dump
+ if binutils.bin2coff then
+ binutils.bin2coff(binarypath, outputpath, opt)
+ else
+ raise("bin2obj: binutils.bin2coff not available (C implementation not compiled)")
+ end
+
+ -- trace
+ cprint("${bright}%s generated!", outputpath)
+end
+
+function _do_bin2obj_elf(binarypath, outputpath, opt)
+ -- get filename from binary path (with extension, dots replaced with underscores)
+ local filename = path.filename(binarypath)
+ -- replace dots with underscores for symbol name (e.g., data.bin -> data_bin)
+ local basename = filename:gsub("%.", "_")
+
+ -- prepare opt
+ opt = opt or {}
+ opt.basename = basename
+
+ -- trace
+ print("converting binary file %s to ELF object file %s ..", binarypath, outputpath)
+
+ -- do dump
+ if binutils.bin2elf then
+ binutils.bin2elf(binarypath, outputpath, opt)
+ else
+ raise("bin2obj: binutils.bin2elf not available (C implementation not compiled)")
+ end
+
+ -- trace
+ cprint("${bright}%s generated!", outputpath)
+end
+
+function _do_bin2obj_macho(binarypath, outputpath, opt)
+ -- get filename from binary path (with extension, dots replaced with underscores)
+ local filename = path.filename(binarypath)
+ -- replace dots with underscores for symbol name (e.g., data.bin -> data_bin)
+ local basename = filename:gsub("%.", "_")
+
+ -- prepare opt
+ opt = opt or {}
+ opt.basename = basename
+
+ -- trace
+ print("converting binary file %s to Mach-O object file %s ..", binarypath, outputpath)
+
+ -- do dump
+ if binutils.bin2macho then
+ binutils.bin2macho(binarypath, outputpath, opt)
+ else
+ raise("bin2obj: binutils.bin2macho not available (C implementation not compiled)")
+ end
+
+ -- trace
+ cprint("${bright}%s generated!", outputpath)
+end
+
+function _do_bin2obj(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)
+
+ -- get format (default: coff)
+ local format = opt.format or "coff"
+ format = format:lower()
+
+ -- validate format
+ if format ~= "coff" and format ~= "elf" and format ~= "macho" then
+ raise("bin2obj: unsupported format '%s' (supported: coff, elf, macho)", format)
+ end
+
+ -- do conversion based on format
+ if format == "coff" then
+ _do_bin2obj_coff(binarypath, outputpath, opt)
+ elseif format == "elf" then
+ _do_bin2obj_elf(binarypath, outputpath, opt)
+ elseif format == "macho" then
+ _do_bin2obj_macho(binarypath, outputpath, opt)
+ end
+end
+
+function main(...)
+
+ -- parse arguments
+ local argv = {...}
+ local opt = option.parse(argv, options, "Convert binary file to object file for direct linking."
+ , ""
+ , "Usage: xmake l utils.binary.bin2obj [options]")
+
+ -- do bin2obj
+ _do_bin2obj(opt.binarypath, opt.outputpath, opt)
+end
+
diff --git a/xmake/rules/utils/bin2c/xmake.lua b/xmake/rules/utils/bin2c/xmake.lua
index 082e8fb5b..7be711af1 100644
--- a/xmake/rules/utils/bin2c/xmake.lua
+++ b/xmake/rules/utils/bin2c/xmake.lua
@@ -44,11 +44,17 @@ rule("utils.bin2c")
table.insert(argv, "-w")
table.insert(argv, tostring(linewidth))
end
- local nozeroend = target:extraconf("rules", "utils.bin2c", "nozeroend")
+ -- get nozeroend (check file-level config first, then rule-level config)
+ local fileconfig = target:fileconfig(sourcefile_bin)
+ local nozeroend = (fileconfig and fileconfig.nozeroend) or target:extraconf("rules", "utils.bin2c", "nozeroend") or false
+ -- also support zeroend (inverse of nozeroend)
+ if fileconfig and fileconfig.zeroend ~= nil then
+ nozeroend = not fileconfig.zeroend
+ end
if nozeroend then
table.insert(argv, "--nozeroend")
end
- batchcmds:vlua("private.utils.bin2c", argv)
+ batchcmds:vlua("utils.binary.bin2c", argv)
-- add deps
batchcmds:add_depfiles(sourcefile_bin)
diff --git a/xmake/rules/utils/bin2obj/xmake.lua b/xmake/rules/utils/bin2obj/xmake.lua
new file mode 100644
index 000000000..69fd9e717
--- /dev/null
+++ b/xmake/rules/utils/bin2obj/xmake.lua
@@ -0,0 +1,98 @@
+--!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, Xmake Open Source Community.
+--
+-- @author ruki
+-- @file xmake.lua
+--
+
+rule("utils.bin2obj")
+ set_extensions(".bin")
+ add_orders("utils.bin2obj", "c++.build.modules.builder")
+ on_buildcmd_file(function (target, batchcmds, sourcefile_bin, opt)
+
+ -- get format (default: auto-detect from platform)
+ local format = target:extraconf("rules", "utils.bin2obj", "format")
+ if not format then
+ if target:is_plat("windows", "mingw", "msys", "cygwin") then
+ format = "coff"
+ elseif target:is_plat("macosx", "iphoneos", "watchos", "appletvos") then
+ format = "macho"
+ else
+ format = "elf"
+ end
+ end
+
+ -- get object file (use .obj for COFF, .o for others)
+ local objext = (format == "coff") and ".obj" or ".o"
+ local objectfile = path.join(target:objectdir(), path.basename(sourcefile_bin) .. objext)
+ table.insert(target:objectfiles(), objectfile)
+
+ -- add commands
+ batchcmds:show_progress(opt.progress, "${color.build.object}generating.bin2obj %s", sourcefile_bin)
+ batchcmds:mkdir(path.directory(objectfile))
+
+ -- get symbol prefix (default: _binary_)
+ local symbol_prefix = target:extraconf("rules", "utils.bin2obj", "symbol_prefix") or "_binary_"
+
+ -- get zeroend (default: false)
+ -- check file-level config first, then rule-level config
+ local fileconfig = target:fileconfig(sourcefile_bin)
+ local zeroend = (fileconfig and fileconfig.zeroend) or target:extraconf("rules", "utils.bin2obj", "zeroend") or false
+
+ -- get architecture
+ local arch = target:arch()
+
+ -- get platform
+ local plat = target:plat()
+
+ -- get target_minver and xcode_sdkver from xcode toolchain (if available)
+ local target_minver = nil
+ local xcode_sdkver = nil
+ if format == "macho" then
+ local toolchain = target:toolchain("xcode")
+ if toolchain then
+ target_minver = toolchain:config("target_minver")
+ xcode_sdkver = toolchain:config("xcode_sdkver")
+ end
+ end
+
+ -- convert binary file to object file
+ local argv = {
+ "-i", path(sourcefile_bin),
+ "-o", path(objectfile),
+ "-f", format,
+ "-a", arch,
+ "-p", plat
+ }
+ if symbol_prefix ~= "_binary_" then
+ table.insert(argv, "--symbol_prefix=" .. symbol_prefix)
+ end
+ if target_minver then
+ table.insert(argv, "--target_minver=" .. target_minver)
+ end
+ if xcode_sdkver then
+ table.insert(argv, "--xcode_sdkver=" .. xcode_sdkver)
+ end
+ if zeroend then
+ table.insert(argv, "--zeroend")
+ end
+ batchcmds:vlua("utils.binary.bin2obj", argv)
+
+ -- add deps
+ batchcmds:add_depfiles(sourcefile_bin)
+ batchcmds:set_depmtime(os.mtime(objectfile))
+ batchcmds:set_depcache(target:dependfile(objectfile))
+ end)
diff --git a/xmake/rules/utils/glsl2spv/xmake.lua b/xmake/rules/utils/glsl2spv/xmake.lua
index dfd1779fe..a6c4a2aef 100644
--- a/xmake/rules/utils/glsl2spv/xmake.lua
+++ b/xmake/rules/utils/glsl2spv/xmake.lua
@@ -86,7 +86,7 @@ rule("utils.glsl2spv")
-- add commands
local argv = {"--nozeroend", "-i", path(spvfilepath), "-o", path(headerfile)}
- batchcmds:vlua("private.utils.bin2c", argv)
+ batchcmds:vlua("utils.binary.bin2c", argv)
end
-- add deps
diff --git a/xmake/rules/utils/hlsl2spv/xmake.lua b/xmake/rules/utils/hlsl2spv/xmake.lua
index 2b285d893..4af520420 100644
--- a/xmake/rules/utils/hlsl2spv/xmake.lua
+++ b/xmake/rules/utils/hlsl2spv/xmake.lua
@@ -86,7 +86,7 @@ rule("utils.hlsl2spv")
-- add commands
local argv = {"--nozeroend", "-i", path(spvfilepath), "-o", path(headerfile)}
- batchcmds:vlua("private.utils.bin2c", argv)
+ batchcmds:vlua("utils.binary.bin2c", argv)
end
batchcmds:add_depfiles(sourcefile_hlsl)
diff --git a/xmake/rules/xmake_cli/xmake.lua b/xmake/rules/xmake_cli/xmake.lua
index d3a4b6f26..7c543cb36 100644
--- a/xmake/rules/xmake_cli/xmake.lua
+++ b/xmake/rules/xmake_cli/xmake.lua
@@ -55,7 +55,7 @@ rule("xmake.cli")
local headerfile = path.join(headerdir, "luafiles.xmz.h")
target:add("includedirs", headerdir)
argv = {"--nozeroend", "-i", path(archivefile), "-o", path(headerfile)}
- batchcmds:vlua("private.utils.bin2c", argv)
+ batchcmds:vlua("utils.binary.bin2c", argv)
batchcmds:add_depfiles(sourcefiles)
batchcmds:set_depmtime(os.mtime(dependfile))