summaryrefslogtreecommitdiff
path: root/xmake/rules/utils
diff options
context:
space:
mode:
authorruki <[email protected]>2025-12-07 23:16:00 +0800
committerruki <[email protected]>2025-12-07 23:16:00 +0800
commiteecb6774a518fbe5df5c5820fa35bd3d8def8dd9 (patch)
tree6e58f82c21a65a83020bbe69180fe2eedbb373b8 /xmake/rules/utils
parent0187b34737611b7b194761ef1a241dd4ac6eee05 (diff)
improve bin2c
Diffstat (limited to 'xmake/rules/utils')
-rw-r--r--xmake/rules/utils/bin2c/utils.lua98
-rw-r--r--xmake/rules/utils/bin2c/xmake.lua30
-rw-r--r--xmake/rules/utils/glsl2spv/xmake.lua15
-rw-r--r--xmake/rules/utils/hlsl2spv/xmake.lua16
4 files changed, 117 insertions, 42 deletions
diff --git a/xmake/rules/utils/bin2c/utils.lua b/xmake/rules/utils/bin2c/utils.lua
new file mode 100644
index 000000000..cf2f074f2
--- /dev/null
+++ b/xmake/rules/utils/bin2c/utils.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 utils.lua
+--
+
+-- generate header file from binary file
+--
+-- @param target the target
+-- @param batchcmds the batch commands
+-- @param binaryfile the binary file path
+-- @param opt the options
+-- - progress: the progress callback
+-- - linewidth: the line width for output (optional)
+-- - nozeroend: disable null terminator (deprecated, for backward compatibility only)
+-- - zeroend: enable null terminator (default: true, bin2c enables zeroend by default for compatibility)
+-- - rulename: the rule name for getting extra config (default: utils.bin2c)
+-- - headerfile: the output header file path (optional, auto-generated if not provided)
+-- - headerdir: the header directory (optional, auto-generated if not provided)
+--
+function generate_headerfile(target, batchcmds, binaryfile, opt)
+ opt = opt or {}
+ local rulename = opt.rulename or "utils.bin2c"
+ local progress = opt.progress
+
+ -- get header directory and file
+ local headerdir = opt.headerdir
+ local headerfile = opt.headerfile
+ if not headerfile then
+ if not headerdir then
+ headerdir = path.join(target:autogendir(), "rules", "utils", "bin2c")
+ end
+ headerfile = path.join(headerdir, path.filename(binaryfile) .. ".h")
+ else
+ headerdir = headerdir or path.directory(headerfile)
+ end
+
+ -- add includedirs
+ target:add("includedirs", headerdir)
+
+ -- add commands
+ if progress then
+ batchcmds:show_progress(progress, "${color.build.object}generating.bin2c %s", binaryfile)
+ end
+ batchcmds:mkdir(headerdir)
+
+ -- build argv
+ local argv = {"-i", path(binaryfile), "-o", path(headerfile)}
+
+ -- get linewidth
+ local linewidth = opt.linewidth or target:extraconf("rules", rulename, "linewidth")
+ if linewidth then
+ table.insert(argv, "-w")
+ table.insert(argv, tostring(linewidth))
+ end
+
+ -- get nozeroend/zeroend (check file-level config first, then rule-level config, then opt)
+ local fileconfig = target:fileconfig(binaryfile)
+ local nozeroend = nil
+ if fileconfig then
+ if fileconfig.nozeroend ~= nil then
+ nozeroend = fileconfig.nozeroend
+ elseif fileconfig.zeroend ~= nil then
+ nozeroend = not fileconfig.zeroend
+ end
+ end
+ if nozeroend == nil then
+ nozeroend = target:extraconf("rules", rulename, "nozeroend") or false
+ end
+ -- also support zeroend in opt (inverse of nozeroend)
+ if opt.zeroend ~= nil then
+ nozeroend = not opt.zeroend
+ elseif opt.nozeroend ~= nil then
+ nozeroend = opt.nozeroend
+ end
+ if nozeroend then
+ table.insert(argv, "--nozeroend")
+ end
+
+ batchcmds:vlua("utils.binary.bin2c", argv)
+
+ return headerfile
+end
+
diff --git a/xmake/rules/utils/bin2c/xmake.lua b/xmake/rules/utils/bin2c/xmake.lua
index 7be711af1..9f6338758 100644
--- a/xmake/rules/utils/bin2c/xmake.lua
+++ b/xmake/rules/utils/bin2c/xmake.lua
@@ -29,32 +29,12 @@ rule("utils.bin2c")
target:add("includedirs", headerdir)
end)
on_preparecmd_file(function (target, batchcmds, sourcefile_bin, opt)
+ import("rules.utils.bin2c.utils", {alias = "bin2c_utils", rootdir = os.programdir()})
- -- 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)
-
- -- add commands
- batchcmds:show_progress(opt.progress, "${color.build.object}generating.bin2c %s", sourcefile_bin)
- batchcmds:mkdir(headerdir)
- local argv = {"-i", path(sourcefile_bin), "-o", path(headerfile)}
- local linewidth = target:extraconf("rules", "utils.bin2c", "linewidth")
- if linewidth then
- table.insert(argv, "-w")
- table.insert(argv, tostring(linewidth))
- end
- -- 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("utils.binary.bin2c", argv)
+ -- generate header file
+ local headerfile = bin2c_utils.generate_headerfile(target, batchcmds, sourcefile_bin, {
+ progress = opt.progress
+ })
-- add deps
batchcmds:add_depfiles(sourcefile_bin)
diff --git a/xmake/rules/utils/glsl2spv/xmake.lua b/xmake/rules/utils/glsl2spv/xmake.lua
index 7fc456c80..a534b43ad 100644
--- a/xmake/rules/utils/glsl2spv/xmake.lua
+++ b/xmake/rules/utils/glsl2spv/xmake.lua
@@ -56,6 +56,7 @@ rule("utils.glsl2spv")
before_buildcmd_file(function (target, batchcmds, sourcefile_glsl, opt)
import("lib.detect.find_tool")
import("rules.utils.bin2obj.utils", {alias = "bin2obj_utils", rootdir = os.programdir()})
+ import("rules.utils.bin2c.utils", {alias = "bin2c_utils", rootdir = os.programdir()})
-- get glslangValidator
local glslc
@@ -88,15 +89,13 @@ rule("utils.glsl2spv")
local is_bin2c = target:extraconf("rules", "utils.glsl2spv", "bin2c")
local is_bin2obj = target:extraconf("rules", "utils.glsl2spv", "bin2obj")
if is_bin2c then
- -- get header file
- local headerdir = outputdir
- local headerfile = path.join(headerdir, path.filename(spvfilepath) .. ".h")
- target:add("includedirs", headerdir)
+ -- generate header file
+ local headerfile = bin2c_utils.generate_headerfile(target, batchcmds, spvfilepath, {
+ progress = opt.progress,
+ headerdir = outputdir,
+ zeroend = true -- default enable zeroend for shader data
+ })
outputfile = headerfile
-
- -- add commands
- local argv = {"--nozeroend", "-i", path(spvfilepath), "-o", path(headerfile)}
- batchcmds:vlua("utils.binary.bin2c", argv)
elseif is_bin2obj then
-- convert to object file using bin2obj
local objectfile = bin2obj_utils.generate_objectfile(target, batchcmds, spvfilepath, {
diff --git a/xmake/rules/utils/hlsl2spv/xmake.lua b/xmake/rules/utils/hlsl2spv/xmake.lua
index de84aa3ab..a1326b97f 100644
--- a/xmake/rules/utils/hlsl2spv/xmake.lua
+++ b/xmake/rules/utils/hlsl2spv/xmake.lua
@@ -57,6 +57,7 @@ rule("utils.hlsl2spv")
before_buildcmd_file(function (target, batchcmds, sourcefile_hlsl, opt)
import("lib.detect.find_tool")
import("rules.utils.bin2obj.utils", {alias = "bin2obj_utils", rootdir = os.programdir()})
+ import("rules.utils.bin2c.utils", {alias = "bin2c_utils", rootdir = os.programdir()})
local dxc = assert(find_tool("dxc"), "dxc not found!")
@@ -87,16 +88,13 @@ rule("utils.hlsl2spv")
local is_bin2c = target:extraconf("rules", "utils.hlsl2spv", "bin2c")
local is_bin2obj = target:extraconf("rules", "utils.hlsl2spv", "bin2obj")
if is_bin2c then
- -- get header file
- local headerdir = outputdir
- local headerfile = path.join(headerdir, path.filename(spvfilepath) .. ".h")
-
- target:add("includedirs", headerdir)
+ -- generate header file
+ local headerfile = bin2c_utils.generate_headerfile(target, batchcmds, spvfilepath, {
+ progress = opt.progress,
+ headerdir = outputdir,
+ zeroend = true -- default enable zeroend for shader data
+ })
outputfile = headerfile
-
- -- add commands
- local argv = {"--nozeroend", "-i", path(spvfilepath), "-o", path(headerfile)}
- batchcmds:vlua("utils.binary.bin2c", argv)
elseif is_bin2obj then
-- convert to object file using bin2obj
local objectfile = bin2obj_utils.generate_objectfile(target, batchcmds, spvfilepath, {