summaryrefslogtreecommitdiff
path: root/xmake/modules
diff options
context:
space:
mode:
authorruki <[email protected]>2026-01-16 09:22:41 +0800
committerGitHub <[email protected]>2026-01-16 09:22:41 +0800
commit99b87757859e318b96bc6074e0a3686e38e6f0a1 (patch)
tree5642dc2ac04eee1481a1c2583075816d7798da01 /xmake/modules
parentb8c02f4d81b0aceb976401f3ce20605f4ee775cb (diff)
parent28b4ef6eaeac47cc1f7b5b40723d11d1578876d3 (diff)
Merge pull request #7226 from xmake-io/tidy
Improve to find clang-tidy
Diffstat (limited to 'xmake/modules')
-rw-r--r--xmake/modules/lib/detect/has_flags.lua4
-rw-r--r--xmake/modules/private/check/checkers/clang/tidy.lua137
2 files changed, 79 insertions, 62 deletions
diff --git a/xmake/modules/lib/detect/has_flags.lua b/xmake/modules/lib/detect/has_flags.lua
index 36409ef84..8ac8e3385 100644
--- a/xmake/modules/lib/detect/has_flags.lua
+++ b/xmake/modules/lib/detect/has_flags.lua
@@ -138,9 +138,7 @@ function main(name, flags, opt)
-- trace
if option.get("verbose") or option.get("diagnosis") or opt.verbose then
- cprintf("${dim}checking for flags (")
- io.write(opt.flagskey)
- cprint("${dim}) ... %s", result and "${color.success}${text.success}" or "${color.nothing}${text.nothing}")
+ cprint("${dim}checking for flags (%s) ... %s", opt.flagskey, result and "${color.success}${text.success}" or "${color.nothing}${text.nothing}")
if option.get("diagnosis") then
cprint("${dim}> %s \"%s\"", path.filename(tool.program), table.concat(checkflags, "\" \""))
if errors and #tostring(errors) > 0 then
diff --git a/xmake/modules/private/check/checkers/clang/tidy.lua b/xmake/modules/private/check/checkers/clang/tidy.lua
index a77b3e946..041256f70 100644
--- a/xmake/modules/private/check/checkers/clang/tidy.lua
+++ b/xmake/modules/private/check/checkers/clang/tidy.lua
@@ -85,6 +85,69 @@ function _add_target_files(sourcefiles, target)
end
end
+-- get clang-tidy
+function _get_clang_tidy()
+ local clang_tidy = find_tool("clang-tidy")
+ if clang_tidy then
+ return clang_tidy
+ end
+
+ -- enter the environments of llvm
+ local oldenvs = packagenv.enter("llvm")
+
+ -- find clang-tidy
+ local packages = {}
+ local clang_tidy = find_tool("clang-tidy")
+ if not clang_tidy then
+ table.join2(packages, install_packages("llvm"))
+ end
+
+ -- enter the environments of installed packages
+ for _, instance in ipairs(packages) do
+ instance:envs_enter()
+ end
+
+ -- we need to force detect and flush detect cache after loading all environments
+ if not clang_tidy then
+ clang_tidy = find_tool("clang-tidy", {force = true})
+ end
+
+ os.setenvs(oldenvs)
+ return clang_tidy
+end
+
+-- get compile_commands.json file
+function _get_compdb_file(opt)
+ local db_path = opt.compdb
+ if not db_path then
+ -- @see https://github.com/xmake-io/xmake/issues/5583#issuecomment-2337696628
+ local outputdir
+ local extraconf = project.extraconf("target.rules", "plugin.compile_commands.autoupdate")
+ if extraconf then
+ outputdir = extraconf.outputdir
+ end
+ if outputdir then
+ db_path = path.join(outputdir, "compile_commands.json")
+ end
+ end
+ if not db_path then
+ db_path = "compile_commands.json"
+ end
+ if os.isdir(db_path) then
+ local db_file_path = path.join(db_path, "compile_commands.json")
+ if os.isfile(db_file_path) then
+ db_path = db_file_path
+ end
+ end
+ if not os.isfile(db_path) then
+ local outputdir = os.tmpfile() .. ".dir"
+ local filename = path.filename(db_path)
+ db_path = outputdir and path.join(outputdir, filename) or filename
+ task.run("project", {quiet = true, kind = "compile_commands", lsp = "clangd", outputdir = outputdir})
+ end
+ return path.absolute(db_path)
+end
+
-- check a single sourcefile
function _check_sourcefile(clang_tidy, sourcefile, opt)
progress.show(opt.progress, "clang-tidy.analyzing %s", sourcefile)
@@ -181,34 +244,18 @@ function _check(clang_tidy, opt)
opt = opt or {}
-- generate compile_commands.json first
- local db_path = opt.compdb
- if not db_path then
- -- @see https://github.com/xmake-io/xmake/issues/5583#issuecomment-2337696628
- local outputdir
- local extraconf = project.extraconf("target.rules", "plugin.compile_commands.autoupdate")
- if extraconf then
- outputdir = extraconf.outputdir
- end
- if outputdir then
- db_path = path.join(outputdir, "compile_commands.json")
- end
- end
- if not db_path then
- db_path = "compile_commands.json"
- end
- if os.isdir(db_path) then
- local db_file_path = path.join(db_path, "compile_commands.json")
- if os.isfile(db_file_path) then
- db_path = db_file_path
- end
+ opt.compdbfile = _get_compdb_file(opt)
+
+ -- save option context
+ option.save()
+
+ -- set verbose and diagnosis if specified
+ if opt.verbose then
+ option.set("verbose", true)
end
- if not os.isfile(db_path) then
- local outputdir = os.tmpfile() .. ".dir"
- local filename = path.filename(db_path)
- db_path = outputdir and path.join(outputdir, filename) or filename
- task.run("project", {quiet = true, kind = "compile_commands", lsp = "clangd", outputdir = outputdir})
+ if opt.diagnosis then
+ option.set("diagnosis", true)
end
- opt.compdbfile = path.absolute(db_path)
-- get sourcefiles
local sourcefiles = {}
@@ -235,6 +282,9 @@ function _check(clang_tidy, opt)
-- check files
_check_sourcefiles(clang_tidy, sourcefiles, opt)
+
+ -- restore option context
+ option.restore()
end
function main(argv)
@@ -244,36 +294,8 @@ function main(argv)
, ""
, "Usage: xmake check clang.tidy [options]")
- -- save option context
- option.save()
-
- -- set verbose and diagnosis if specified
- if args.verbose then
- option.set("verbose", true)
- end
- if args.diagnosis then
- option.set("diagnosis", true)
- end
-
- -- enter the environments of llvm
- local oldenvs = packagenv.enter("llvm")
-
-- find clang-tidy
- local packages = {}
- local clang_tidy = find_tool("clang-tidy", {version = true})
- if not clang_tidy then
- table.join2(packages, install_packages("llvm"))
- end
-
- -- enter the environments of installed packages
- for _, instance in ipairs(packages) do
- instance:envs_enter()
- end
-
- -- we need to force detect and flush detect cache after loading all environments
- if not clang_tidy then
- clang_tidy = find_tool("clang-tidy", {force = true, version = true})
- end
+ local clang_tidy = _get_clang_tidy()
assert(clang_tidy, "clang-tidy not found!")
-- list checks
@@ -284,8 +306,5 @@ function main(argv)
else
_check(clang_tidy, args)
end
- os.setenvs(oldenvs)
-
- -- restore option context
- option.restore()
end
+