summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorruki <[email protected]>2025-11-19 14:02:33 +0800
committerGitHub <[email protected]>2025-11-19 14:02:33 +0800
commit9eb2b81c1d3bab990f8b1ddcec2cdd30be47f0b2 (patch)
treeeccaa0cfe76f154b5654aebf07d3ad32d413305d
parent91e10df41769a679e97e152a866394da3b41cd81 (diff)
parentdb139ec89242d6d0f9b6803c464c5ee68e5a4f41 (diff)
Merge pull request #7037 from xmake-io/format
Improve clang format
-rw-r--r--xmake/plugins/format/main.lua33
-rw-r--r--xmake/plugins/format/xmake.lua2
2 files changed, 26 insertions, 9 deletions
diff --git a/xmake/plugins/format/main.lua b/xmake/plugins/format/main.lua
index 87744ad12..aa2814af8 100644
--- a/xmake/plugins/format/main.lua
+++ b/xmake/plugins/format/main.lua
@@ -24,6 +24,8 @@ import("core.base.hashset")
import("core.project.config")
import("core.project.project")
import("lib.detect.find_tool")
+import("async.runjobs")
+import("utils.progress")
import("private.action.require.impl.packagenv")
import("private.action.require.impl.install_packages")
import("private.action.utils", {alias = "action_utils"})
@@ -174,6 +176,8 @@ function main()
table.insert(argv, "--verbose")
end
+ -- collect sourcefiles
+ local sourcefiles = {}
local targetname, group_pattern = action_utils.get_target_and_group()
local targets = _get_targets(targetname, group_pattern)
if option.get("files") then
@@ -181,12 +185,12 @@ function main()
for _, target in ipairs(targets) do
for _, source in ipairs(target:sourcefiles()) do
if _match_sourcefiles(source, filepatterns) then
- table.insert(argv, path.join(projectdir, source))
+ table.insert(sourcefiles, path.join(projectdir, source))
end
end
for _, header in ipairs(target:headerfiles()) do
if _match_sourcefiles(header, filepatterns) then
- table.insert(argv, path.join(projectdir, header))
+ table.insert(sourcefiles, path.join(projectdir, header))
end
end
end
@@ -195,20 +199,31 @@ function main()
for _, sourcebatch in pairs(target:sourcebatches()) do
if _source_batch_should_format(sourcebatch) then
for _, source in ipairs(sourcebatch.sourcefiles) do
- table.insert(argv, path.join(projectdir, source))
+ table.insert(sourcefiles, path.join(projectdir, source))
end
end
end
for _, header in ipairs(target:headerfiles()) do
- table.insert(argv, path.join(projectdir, header))
+ table.insert(sourcefiles, path.join(projectdir, header))
end
end
end
- -- format files
- os.vrunv(clang_format.program, argv, {curdir = projectdir})
- cprint("${color.success}format ok!")
-
- -- done
+ -- format files in parallel
+ if #sourcefiles > 0 then
+ local jobs = tonumber(option.get("jobs"))
+ if not jobs or jobs <= 0 then
+ jobs = os.default_njob()
+ end
+ local format_time = os.mclock()
+ runjobs("clang-format", function (index, total, opt)
+ local sourcefile = sourcefiles[index]
+ local format_argv = table.join(argv, {sourcefile})
+ progress.show(index * 100 / total, "clang-format.formatting %s", sourcefile)
+ os.execv(clang_format.program, format_argv, {curdir = projectdir})
+ end, {total = #sourcefiles, comax = jobs})
+ format_time = os.mclock() - format_time
+ progress.show(100, "${color.success}clang-format formatted %d files, spent %.3fs", #sourcefiles, format_time / 1000)
+ end
os.setenvs(oldenvs)
end
diff --git a/xmake/plugins/format/xmake.lua b/xmake/plugins/format/xmake.lua
index 1b31ff9b3..d9e34572b 100644
--- a/xmake/plugins/format/xmake.lua
+++ b/xmake/plugins/format/xmake.lua
@@ -30,6 +30,8 @@ task("format")
{nil, "create", "k", nil, "Create a .clang-format file from a coding style"},
{'n', "dry-run", "k", nil, "Do not make any changes, just show the files that would be formatted."},
{'e', "error", "k", nil, "If set, changes formatting warnings to errors."},
+ {'j', "jobs", "kv", tostring(os.default_njob()),
+ "Set the number of parallel format jobs."},
{'a', "all", "k", nil, "Format all targets."},
{'g', "group", "kv", nil, "Format all targets of the given group. It support path pattern matching.",
"e.g.",