diff options
| author | ruki <[email protected]> | 2025-12-03 00:52:16 +0800 |
|---|---|---|
| committer | ruki <[email protected]> | 2025-12-03 00:52:16 +0800 |
| commit | d66186fac83f009715e4d3307b47e361954fa708 (patch) | |
| tree | dc5191d6d7b8d93ca70394430e78b2859d7c4cea | |
| parent | 3aca93627b3cada55db8d0215d6ff87ca418ceab (diff) | |
add xmake check syntax
| -rw-r--r-- | xmake/actions/build/build_files.lua | 9 | ||||
| -rw-r--r-- | xmake/core/project/policy.lua | 2 | ||||
| -rw-r--r-- | xmake/modules/private/action/build/build_binary.lua | 20 | ||||
| -rw-r--r-- | xmake/modules/private/action/build/target.lua | 4 | ||||
| -rw-r--r-- | xmake/modules/private/check/checker.lua | 4 | ||||
| -rw-r--r-- | xmake/modules/private/check/checkers/syntax.lua | 85 | ||||
| -rw-r--r-- | xmake/rules/c++/config/main.lua | 6 | ||||
| -rw-r--r-- | xmake/rules/c++/config/syntax_only.lua | 55 |
8 files changed, 174 insertions, 11 deletions
diff --git a/xmake/actions/build/build_files.lua b/xmake/actions/build/build_files.lua index 6818418f2..209b9bd07 100644 --- a/xmake/actions/build/build_files.lua +++ b/xmake/actions/build/build_files.lua @@ -30,6 +30,9 @@ import("deprecated.build_files", {alias = "deprecated_build_files"}) -- convert all sourcefiles to lua pattern function _get_file_patterns(sourcefiles) local patterns = {} + if not sourcefiles then + return patterns + end for _, sourcefile in ipairs(path.splitenv(sourcefiles)) do -- get the excludes @@ -90,7 +93,11 @@ function _build_files(targets_root, opt) opt.distcc = distcc_build_client.singleton() end if not target_buildutils.run_filejobs(targets_root, opt) then - wprint("%s not found!", opt.sourcefiles) + if opt.sourcefiles then + wprint("%s not found!", opt.sourcefiles) + else + wprint("no any files found!") + end end end diff --git a/xmake/core/project/policy.lua b/xmake/core/project/policy.lua index 1cecfd01a..ba005f16e 100644 --- a/xmake/core/project/policy.lua +++ b/xmake/core/project/policy.lua @@ -110,6 +110,8 @@ function policy.policies() ["build.c++.msvc.runtime"] = {description = "Set the default vs runtime.", type = "string", values = {"MT", "MD"}}, -- Enable C++ Dynamic Debugging for MSVC (requires MSVC toolset 14.44+, x64 only, incompatible with LTCG/PGO/OPT-ICF). ["build.c++.dynamic_debugging"] = {description = "Enable C++ Dynamic Debugging for MSVC (requires MSVC toolset 14.44+, x64 only, incompatible with LTCG/PGO/OPT-ICF).", type = "boolean"}, + -- Enable syntax-only check for C/C++ building. + ["build.c++.syntax_only"] = {description = "Enable syntax-only check for C/C++ building.", type = "boolean"}, -- Enable cuda device link ["build.cuda.devlink"] = {description = "Enable Cuda devlink.", type = "boolean"}, -- Enable linker output, e.g. show -Wl,--print-memory-usage output for gcc/g++ diff --git a/xmake/modules/private/action/build/build_binary.lua b/xmake/modules/private/action/build/build_binary.lua index 581b2dde9..47884fd44 100644 --- a/xmake/modules/private/action/build/build_binary.lua +++ b/xmake/modules/private/action/build/build_binary.lua @@ -23,6 +23,7 @@ import("build_object") import("private.action.build.target", {alias = "target_buildutils"}) function main(jobgraph, target, opt) + opt = opt or {} local objects_group = target:fullname() .. "/objects" local jobsize = jobgraph:size() jobgraph:group(objects_group, function () @@ -30,13 +31,16 @@ function main(jobgraph, target, opt) end) local has_object_jobs = jobgraph:size() > jobsize - -- @note We always need the link task, even if the current target does not have any object files - -- https://github.com/xmake-io/xmake-repo/pull/7479#issuecomment-3007049158 - local link_group = target:fullname() .. "/link" - jobgraph:group(link_group, function () - target_buildutils.add_linkjobs(jobgraph, target, opt) - end) - if has_object_jobs then - jobgraph:add_orders(objects_group, link_group) + -- skip link jobs if linkjobs is disabled + if opt.linkjobs ~= false then + -- @note We always need the link task, even if the current target does not have any object files + -- https://github.com/xmake-io/xmake-repo/pull/7479#issuecomment-3007049158 + local link_group = target:fullname() .. "/link" + jobgraph:group(link_group, function () + target_buildutils.add_linkjobs(jobgraph, target, opt) + end) + if has_object_jobs then + jobgraph:add_orders(objects_group, link_group) + end end end diff --git a/xmake/modules/private/action/build/target.lua b/xmake/modules/private/action/build/target.lua index 280610e71..e906d4ee8 100644 --- a/xmake/modules/private/action/build/target.lua +++ b/xmake/modules/private/action/build/target.lua @@ -701,6 +701,10 @@ end -- add link jobs for the given target function add_linkjobs(jobgraph, target, opt) opt = table.clone(opt or {}) + -- skip link jobs if linkjobs is disabled + if opt.linkjobs == false then + return + end opt.job_kind = "link" local with_stages = opt.with_stages local group, group_before, group_after diff --git a/xmake/modules/private/check/checker.lua b/xmake/modules/private/check/checker.lua index 6ff8b3864..6d43eecb9 100644 --- a/xmake/modules/private/check/checker.lua +++ b/xmake/modules/private/check/checker.lua @@ -59,7 +59,9 @@ function checkers() -- cuda checkers ["cuda.devlink"] = {description = "Check devlink for targets.", build_failure = true}, -- clang tidy checker - ["clang.tidy"] = {description = "Check project code using clang-tidy.", showstats = false} + ["clang.tidy"] = {description = "Check project code using clang-tidy.", showstats = false}, + -- syntax checker + ["syntax"] = {description = "Check the project sourcecode syntax without linking.", showstats = false} } _g._CHECKERS = checkers end diff --git a/xmake/modules/private/check/checkers/syntax.lua b/xmake/modules/private/check/checkers/syntax.lua new file mode 100644 index 000000000..f0a04386a --- /dev/null +++ b/xmake/modules/private/check/checkers/syntax.lua @@ -0,0 +1,85 @@ +--!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 syntax.lua +-- + +-- imports +import("core.base.option") +import("core.base.task") +import("core.project.config") +import("core.project.project") +import("actions.build.build_files", {rootdir = os.programdir(), alias = "build_files"}) +import("actions.build.build", {rootdir = os.programdir(), alias = "build"}) +import("utils.progress") + +-- the syntax check options +local options = { + {'f', "files", "kv", nil, "Check the given source files.", + "e.g.", + " - xmake check syntax -f src/foo.cpp", + " - xmake check syntax -f 'src/*.cpp'"}, + {nil, "targets", "vs", nil, "Check the sourcefiles of the given target.", + "e.g.", + " - xmake check syntax", + " - xmake check syntax [targets]"} +} + +-- do check +function _check(opt) + opt = opt or {} + + local sourcefiles = opt.files + local targetnames = opt.targets + local check_time = os.mclock() + if sourcefiles then + build_files(targetnames, {sourcefiles = sourcefiles, linkjobs = false}) + else + build(targetnames, {linkjobs = false}) + end + check_time = os.mclock() - check_time + progress.show(100, "${color.success}syntax check ok, spent %.3fs", check_time / 1000) +end + +function main(argv) + -- parse arguments + local args = option.parse(argv or {}, options, "Check the project sourcecode syntax without linking." + , "" + , "Usage: xmake check syntax [options]") + + -- lock the whole project + project.lock() + + -- enable syntax-only policy and disable ccache via config + config.set("policies", "build.c++.syntax_only,build.ccache:n") + + -- config it first + task.run("config", {}, {disable_dump = true}) + + -- enter project directory + local oldir = os.cd(project.directory()) + + -- do check + _check(args) + + -- leave project directory + os.cd(oldir) + + -- unlock the whole project + project.unlock() +end + diff --git a/xmake/rules/c++/config/main.lua b/xmake/rules/c++/config/main.lua index dce7cff6b..bde849965 100644 --- a/xmake/rules/c++/config/main.lua +++ b/xmake/rules/c++/config/main.lua @@ -20,9 +20,10 @@ -- imports import("rules.c++.config.basic", {rootdir = os.programdir(), alias = "config_basic"}) +import("rules.c++.config.dynamic_debugging", {rootdir = os.programdir(), alias = "config_dynamic_debugging"}) +import("rules.c++.config.syntax_only", {rootdir = os.programdir(), alias = "config_syntax_only"}) import("rules.c++.config.optimization", {rootdir = os.programdir(), alias = "config_optimization"}) import("rules.c++.config.sanitizer", {rootdir = os.programdir(), alias = "config_sanitizer"}) -import("rules.c++.config.dynamic_debugging", {rootdir = os.programdir(), alias = "config_dynamic_debugging"}) -- main entry function main(target, sourcekind) @@ -33,6 +34,9 @@ function main(target, sourcekind) -- config dynamic debugging configs (must be before optimization to disable incompatible flags) config_dynamic_debugging(target, sourcekind) + -- config syntax only configs (must be before optimization to disable incompatible flags) + config_syntax_only(target, sourcekind) + -- config optimization configs config_optimization(target, sourcekind) diff --git a/xmake/rules/c++/config/syntax_only.lua b/xmake/rules/c++/config/syntax_only.lua new file mode 100644 index 000000000..4f0045192 --- /dev/null +++ b/xmake/rules/c++/config/syntax_only.lua @@ -0,0 +1,55 @@ +--!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 syntax_only.lua +-- + +-- imports +import("core.project.project") + +-- add syntax-only check support +-- @see https://github.com/mesonbuild/meson/issues/12228 +function main(target, sourcekind) + -- check if syntax-only is enabled + local enabled = target:policy("build.c++.syntax_only") + if enabled == nil then + enabled = project.policy("build.c++.syntax_only") + end + if not enabled then + return + end + + -- get flag name for sourcekind + local flagnames = { + cc = "cflags", + cxx = "cxxflags", + mm = "mflags", + mxx = "mxxflags" + } + local cflag = flagnames[sourcekind] or (sourcekind == "cxx" and "cxxflags" or "cflags") + + -- check compiler and add appropriate flag + local toolname = sourcekind == "cxx" and "cxx" or "cc" + if target:has_tool(toolname, "gcc", "gxx", "clang", "clangxx") then + -- gcc/clang: -fsyntax-only + target:add(cflag, "-fsyntax-only", {force = true}) + elseif target:has_tool(toolname, "cl") then + -- MSVC: /Zs (syntax check only) + target:add(cflag, "/Zs", {force = true}) + end +end + |
