summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorruki <[email protected]>2025-11-19 22:48:16 +0800
committerruki <[email protected]>2025-11-19 22:48:16 +0800
commit1b564b63347ad5add8208d69ca6cdc04faedde52 (patch)
treeb76d6253efd9828559e414c6e817516273bd26db
parent77f00b9a773f3e26cfc32fd18502789018807b91 (diff)
improve xmake format
-rw-r--r--xmake/plugins/format/main.lua33
-rw-r--r--xmake/plugins/format/xmake.lua2
2 files changed, 28 insertions, 7 deletions
diff --git a/xmake/plugins/format/main.lua b/xmake/plugins/format/main.lua
index 87744ad12..76cdbbee3 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,19 +199,34 @@ 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!")
+ -- format files in parallel
+ if #sourcefiles > 0 then
+ local jobs = option.get("jobs")
+ if jobs then
+ jobs = tonumber(jobs)
+ else
+ jobs = os.default_njob()
+ end
+ local format_time = os.mclock()
+ runjobs("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
-- done
os.setenvs(oldenvs)
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.",