diff options
| author | fcying <[email protected]> | 2025-06-22 13:08:01 +0800 |
|---|---|---|
| committer | fcying <[email protected]> | 2025-06-26 09:49:14 +0800 |
| commit | 42e25f69c356dbb014459f472da1e99e27849418 (patch) | |
| tree | f5307ec2275f36abe268b30dae03fc53e158ce0a | |
| parent | 749d449e2ff77f43771467b986aa203546f3a808 (diff) | |
feat(plugin/project): support gen compile_commands.json for specific target
| -rw-r--r-- | .gitignore | 1 | ||||
| -rw-r--r-- | xmake/plugins/project/clang/compile_commands.lua | 4 | ||||
| -rw-r--r-- | xmake/plugins/project/clang/compile_flags.lua | 4 | ||||
| -rw-r--r-- | xmake/plugins/project/cmake/cmakelists.lua | 7 | ||||
| -rw-r--r-- | xmake/plugins/project/make/makefile.lua | 22 | ||||
| -rw-r--r-- | xmake/plugins/project/ninja/build_ninja.lua | 10 | ||||
| -rw-r--r-- | xmake/plugins/project/utils/target_utils.lua | 33 | ||||
| -rw-r--r-- | xmake/plugins/project/vstudio/impl/vs200x.lua | 6 | ||||
| -rw-r--r-- | xmake/plugins/project/vstudio/impl/vs200x_solution.lua | 7 | ||||
| -rw-r--r-- | xmake/plugins/project/vstudio/impl/vs201x.lua | 4 | ||||
| -rw-r--r-- | xmake/plugins/project/vstudio/impl/vs201x_solution.lua | 9 | ||||
| -rw-r--r-- | xmake/plugins/project/vsxmake/getinfo.lua | 10 | ||||
| -rw-r--r-- | xmake/plugins/project/xmake.lua | 3 |
13 files changed, 93 insertions, 27 deletions
diff --git a/.gitignore b/.gitignore index 501d7e2f9..c6ac2e121 100644 --- a/.gitignore +++ b/.gitignore @@ -5,6 +5,7 @@ .xmake/ build/ makefile +Makefile # gcc cache gcm.cache diff --git a/xmake/plugins/project/clang/compile_commands.lua b/xmake/plugins/project/clang/compile_commands.lua index 538b30f83..4eecadf8c 100644 --- a/xmake/plugins/project/clang/compile_commands.lua +++ b/xmake/plugins/project/clang/compile_commands.lua @@ -28,6 +28,7 @@ import("core.language.language") import("private.utils.batchcmds") import("private.utils.executable_path") import("plugins.project.utils.target_cmds", {rootdir = os.programdir()}) +import("plugins.project.utils.target_utils", {rootdir = os.programdir()}) import("actions.test.main", {rootdir = os.programdir(), alias = "test_action"}) -- escape path @@ -282,7 +283,8 @@ function _add_targets(jsonfile) jsonfile:print("[") _g.firstline = true - for _, target in pairs(project.targets()) do + local project_targets = target_utils.get_project_targets() + for _, target in pairs(project_targets) do if not target:is_phony() then _add_target(jsonfile, target) end diff --git a/xmake/plugins/project/clang/compile_flags.lua b/xmake/plugins/project/clang/compile_flags.lua index 5cfedbb19..d8d7e260b 100644 --- a/xmake/plugins/project/clang/compile_flags.lua +++ b/xmake/plugins/project/clang/compile_flags.lua @@ -22,6 +22,7 @@ import("core.tool.compiler") import("core.project.project") import("core.language.language") +import("plugins.project.utils.target_utils", {rootdir = os.programdir()}) -- make the object function _make_object(target, flags, sourcefile, objectfile) @@ -71,7 +72,8 @@ end -- make all function _make_all(flags) _g.firstline = true - for _, target in pairs(project.targets()) do + local project_targets = target_utils.get_project_targets() + for _, target in pairs(project_targets) do if not target:is_phony() and target:is_default() then _make_target(target, flags) end diff --git a/xmake/plugins/project/cmake/cmakelists.lua b/xmake/plugins/project/cmake/cmakelists.lua index 5b5513307..c492ce6e2 100644 --- a/xmake/plugins/project/cmake/cmakelists.lua +++ b/xmake/plugins/project/cmake/cmakelists.lua @@ -31,6 +31,7 @@ import("lib.detect.find_tool") import("private.utils.batchcmds") import("private.utils.target", {alias = "target_utils"}) import("plugins.project.utils.target_cmds", {rootdir = os.programdir()}) +import("plugins.project.utils.target_utils", {rootdir = os.programdir()}) import("rules.c++.modules.support", {alias = "module_support", rootdir = os.programdir()}) -- get cmake version @@ -348,7 +349,8 @@ function _add_project(cmakelists, outputdir) -- set project name local project_name = project.name() if not project_name then - for _, target in table.orderpairs(project.targets()) do + local project_targets = target_utils.get_project_targets() + for _, target in table.orderpairs(project_targets) do project_name = target:name() end end @@ -1389,7 +1391,8 @@ function _generate_cmakelists(cmakelists, outputdir) _add_project(cmakelists, outputdir) -- add targets - for _, target in table.orderpairs(project.targets()) do + local project_targets = target_utils.get_project_targets() + for _, target in table.orderpairs(project_targets) do _add_target(cmakelists, target, outputdir) end end diff --git a/xmake/plugins/project/make/makefile.lua b/xmake/plugins/project/make/makefile.lua index e97c50cda..26cd37ed7 100644 --- a/xmake/plugins/project/make/makefile.lua +++ b/xmake/plugins/project/make/makefile.lua @@ -28,6 +28,7 @@ import("core.platform.platform") import("lib.detect.find_tool") import("private.utils.batchcmds") import("plugins.project.utils.target_cmds", {rootdir = os.programdir()}) +import("plugins.project.utils.target_utils", {rootdir = os.programdir()}) -- tranlate path function _translate_path(filepath, outputdir) @@ -333,7 +334,8 @@ function _add_toolchains(makefile, outputdir) makefile:print("") -- add toolchains from targets - for targetname, target in pairs(project.targets()) do + local project_targets = target_utils.get_project_targets() + for targetname, target in pairs(project_targets) do if not _phony_or_headeronly(target) then local program = _get_program_from_target(target, target:linker():kind()) if program then @@ -359,7 +361,8 @@ end -- add flags function _add_flags(makefile, targetflags, outputdir) - for targetname, target in pairs(project.targets()) do + local project_targets = target_utils.get_project_targets() + for targetname, target in pairs(project_targets) do if not _phony_or_headeronly(target) then for _, sourcebatch in pairs(target:sourcebatches()) do local sourcekind = sourcebatch.sourcekind @@ -616,19 +619,20 @@ end -- add build targets function _add_build_targets(makefile, targetflags, outputdir) local default = "" - for targetname, target in pairs(project.targets()) do + local project_targets = target_utils.get_project_targets() + for targetname, target in pairs(project_targets) do if target:is_default() then default = default .. " " .. targetname end end makefile:print("default: %s\n", default) local all = "" - for targetname, _ in pairs(project.targets()) do + for targetname, _ in pairs(project_targets) do all = all .. " " .. targetname end makefile:print("all: %s\n", all) makefile:print(".PHONY: default all %s\n", all) - for _, target in pairs(project.targets()) do + for _, target in pairs(project_targets) do _add_build_target(makefile, target, targetflags, outputdir) end end @@ -638,7 +642,8 @@ function _add_build(makefile, targetflags, outputdir) -- TODO -- disable precompiled header first - for _, target in pairs(project.targets()) do + local project_targets = target_utils.get_project_targets() + for _, target in pairs(project_targets) do target:set("pcheader", nil) target:set("pcxxheader", nil) end @@ -665,13 +670,14 @@ end -- add clean targets function _add_clean_targets(makefile, outputdir) local all = "" - for targetname, _ in pairs(project.targets()) do + local project_targets = target_utils.get_project_targets() + for targetname, _ in pairs(project_targets) do all = all .. " clean_" .. targetname end makefile:print("clean: %s\n", all) -- add clean targets - for _, target in pairs(project.targets()) do + for _, target in pairs(project_targets) do _add_clean_target(makefile, target, outputdir) end end diff --git a/xmake/plugins/project/ninja/build_ninja.lua b/xmake/plugins/project/ninja/build_ninja.lua index 87bd29e7d..a4f2560b9 100644 --- a/xmake/plugins/project/ninja/build_ninja.lua +++ b/xmake/plugins/project/ninja/build_ninja.lua @@ -29,6 +29,7 @@ import("lib.detect.find_tool") import("lib.detect.find_toolname") import("core.tools.cl.parse_include") import("plugins.project.utils.target_cmds", {rootdir = os.programdir()}) +import("plugins.project.utils.target_utils", {rootdir = os.programdir()}) -- this sourcebatch is built? function _sourcebatch_is_built(sourcebatch) @@ -420,21 +421,22 @@ function _add_build_for_targets(ninjafile, outputdir) -- add build rule for generator _add_build_for_generator(ninjafile, outputdir) + local project_targets = target_utils.get_project_targets() -- TODO -- disable precompiled header first - for _, target in pairs(project.targets()) do + for _, target in pairs(project_targets) do target:set("pcheader", nil) target:set("pcxxheader", nil) end -- build targets - for _, target in pairs(project.targets()) do + for _, target in pairs(project_targets) do _add_build_for_target(ninjafile, target, outputdir) end -- build default local default = "" - for targetname, target in pairs(project.targets()) do + for targetname, target in pairs(project_targets) do if target:is_default() then default = default .. " " .. targetname end @@ -443,7 +445,7 @@ function _add_build_for_targets(ninjafile, outputdir) -- build all local all = "" - for targetname, _ in pairs(project.targets()) do + for targetname, _ in pairs(project_targets) do all = all .. " " .. targetname end ninjafile:print("build all: phony%s\n", all) diff --git a/xmake/plugins/project/utils/target_utils.lua b/xmake/plugins/project/utils/target_utils.lua new file mode 100644 index 000000000..734ad3dee --- /dev/null +++ b/xmake/plugins/project/utils/target_utils.lua @@ -0,0 +1,33 @@ +--!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, TBOOX Open Source Group. +-- +-- @author ruki +-- @file target_utils.lua +-- + +-- imports +import("core.project.project") +import("core.base.option") + +-- get project targets +function get_project_targets() + local selected_target = option.get("target") + if selected_target then + -- return table.wrap(project.target(selected_target)) + return { project.target(selected_target) } + end + return project.targets() +end diff --git a/xmake/plugins/project/vstudio/impl/vs200x.lua b/xmake/plugins/project/vstudio/impl/vs200x.lua index d1f220c8a..099b2e224 100644 --- a/xmake/plugins/project/vstudio/impl/vs200x.lua +++ b/xmake/plugins/project/vstudio/impl/vs200x.lua @@ -22,6 +22,7 @@ import("core.project.project") import("vs200x_solution") import("vs200x_vcproj") +import("plugins.project.utils.target_utils", {rootdir = os.programdir()}) -- make vstudio project function make(outputdir, vsinfo) @@ -35,15 +36,16 @@ function make(outputdir, vsinfo) -- make solution vs200x_solution.make(vsinfo) + local project_targets = target_utils.get_project_targets() -- TODO -- disable precompiled header first - for _, target in pairs(project.targets()) do + for _, target in pairs(project_targets) do target:set("pcheader", nil) target:set("pcxxheader", nil) end -- make vsprojs - for _, target in pairs(project.targets()) do + for _, target in pairs(project_targets) do if not target:is_phony() then vs200x_vcproj.make(vsinfo, target) end diff --git a/xmake/plugins/project/vstudio/impl/vs200x_solution.lua b/xmake/plugins/project/vstudio/impl/vs200x_solution.lua index 05f873dcb..d740073b7 100644 --- a/xmake/plugins/project/vstudio/impl/vs200x_solution.lua +++ b/xmake/plugins/project/vstudio/impl/vs200x_solution.lua @@ -21,6 +21,7 @@ -- imports import("core.project.project") import("vsfile") +import("plugins.project.utils.target_utils", {rootdir = os.programdir()}) -- make header function _make_header(slnfile, vsinfo) @@ -35,7 +36,8 @@ function _make_projects(slnfile, vsinfo) local vctool = "8BC9CEB8-8B4A-11D0-8D11-00A0C91BC942" -- make all targets - for targetname, target in pairs(project.targets()) do + local project_targets = target_utils.get_project_targets() + for targetname, target in pairs(project_targets) do if not target:is_phony() then -- enter project @@ -67,7 +69,8 @@ function _make_global(slnfile, vsinfo) -- add project configuration platforms slnfile:enter("GlobalSection(ProjectConfigurationPlatforms) = postSolution") - for targetname, target in pairs(project.targets()) do + local project_targets = target_utils.get_project_targets() + for targetname, target in pairs(project_targets) do if not target:is_phony() then slnfile:print("{%s}.$(mode)|Win32.ActiveCfg = $(mode)|Win32", hash.uuid4(targetname)) slnfile:print("{%s}.$(mode)|Win32.Build.0 = $(mode)|Win32", hash.uuid4(targetname)) diff --git a/xmake/plugins/project/vstudio/impl/vs201x.lua b/xmake/plugins/project/vstudio/impl/vs201x.lua index 063ec41ff..cb1d62da5 100644 --- a/xmake/plugins/project/vstudio/impl/vs201x.lua +++ b/xmake/plugins/project/vstudio/impl/vs201x.lua @@ -40,6 +40,7 @@ import("private.action.run.runenvs") import("actions.config.configfiles", {alias = "generate_configfiles", rootdir = os.programdir()}) import("private.utils.batchcmds") import("plugins.project.utils.target_cmds", {rootdir = os.programdir()}) +import("plugins.project.utils.target_utils", {rootdir = os.programdir()}) function _translate_path(dir, vcxprojdir) if dir == nil then @@ -436,7 +437,8 @@ function make(outputdir, vsinfo) os.cd(project.directory()) -- save targets - for targetname, target in table.orderpairs(project.targets()) do + local project_targets = target_utils.get_project_targets() + for targetname, target in table.orderpairs(project_targets) do -- make target with the given mode and arch targets[targetname] = targets[targetname] or {} diff --git a/xmake/plugins/project/vstudio/impl/vs201x_solution.lua b/xmake/plugins/project/vstudio/impl/vs201x_solution.lua index d4a16c207..74fb963fa 100644 --- a/xmake/plugins/project/vstudio/impl/vs201x_solution.lua +++ b/xmake/plugins/project/vstudio/impl/vs201x_solution.lua @@ -22,6 +22,7 @@ import("core.project.project") import("vsfile") import("vsutils") +import("plugins.project.utils.target_utils", {rootdir = os.programdir()}) -- make header function _make_header(slnfile, vsinfo) @@ -36,7 +37,8 @@ function _make_projects(slnfile, vsinfo) local groups = {} local targets = {} local vctool = "8BC9CEB8-8B4A-11D0-8D11-00A0C91BC942" - for targetname, target in table.orderpairs(project.targets()) do + local project_targets = target_utils.get_project_targets() + for targetname, target in table.orderpairs(project_targets) do -- we need to set startup project for default or binary target -- @see https://github.com/xmake-io/xmake/issues/1249 if target:get("default") == true then @@ -99,7 +101,8 @@ function _make_global(slnfile, vsinfo) -- add project configuration platforms slnfile:enter("GlobalSection(ProjectConfigurationPlatforms) = postSolution") - for targetname, target in table.orderpairs(project.targets()) do + local project_targets = target_utils.get_project_targets() + for targetname, target in table.orderpairs(project_targets) do for _, mode in ipairs(vsinfo.modes) do for _, arch in ipairs(vsinfo.archs) do local vs_arch = vsutils.vsarch(arch) @@ -118,7 +121,7 @@ function _make_global(slnfile, vsinfo) -- add project groups slnfile:enter("GlobalSection(NestedProjects) = preSolution") local subgroups = {} - for targetname, target in table.orderpairs(project.targets()) do + for targetname, target in table.orderpairs(project_targets) do local group_path = target:get("group") if group_path then -- target -> group diff --git a/xmake/plugins/project/vsxmake/getinfo.lua b/xmake/plugins/project/vsxmake/getinfo.lua index 670f5d994..3868fac7b 100644 --- a/xmake/plugins/project/vsxmake/getinfo.lua +++ b/xmake/plugins/project/vsxmake/getinfo.lua @@ -36,6 +36,7 @@ import("private.action.run.runenvs") import("private.action.require.install", {alias = "install_requires"}) import("actions.config.configfiles", {alias = "generate_configfiles", rootdir = os.programdir()}) import("vstudio.impl.vsutils", {rootdir = path.join(os.programdir(), "plugins", "project")}) +import("plugins.project.utils.target_utils", {rootdir = os.programdir()}) -- strip dot directories, e.g. ..\..\.. => .. -- @see https://github.com/xmake-io/xmake/issues/2039 @@ -298,7 +299,8 @@ end function _make_vsinfo_groups() local groups = {} local group_deps = {} - for targetname, target in table.orderpairs(project.targets()) do + local project_targets = target_utils.get_project_targets() + for targetname, target in table.orderpairs(project_targets) do local group_path = target:get("group") if group_path and #(group_path:trim()) > 0 then group_path = path.normalize(group_path) @@ -485,7 +487,8 @@ function main(outputdir, vsinfo) os.cd(project.directory()) -- save targets - for targetname, target in table.orderpairs(project.targets()) do + local project_targets = target_utils.get_project_targets() + for targetname, target in table.orderpairs(project_targets) do -- https://github.com/xmake-io/xmake/issues/2337 target:data_set("plugin.project.kind", "vsxmake") @@ -588,7 +591,8 @@ function main(outputdir, vsinfo) -- we need to set startup project for default or binary target -- @see https://github.com/xmake-io/xmake/issues/1249 local targetnames = {} - for targetname, target in table.orderpairs(project.targets()) do + local project_targets = target_utils.get_project_targets() + for targetname, target in table.orderpairs(project_targets) do if target:get("default") == true then table.insert(targetnames, 1, targetname) elseif target:is_binary() then diff --git a/xmake/plugins/project/xmake.lua b/xmake/plugins/project/xmake.lua index 3bf958d77..7cae4096c 100644 --- a/xmake/plugins/project/xmake.lua +++ b/xmake/plugins/project/xmake.lua @@ -61,6 +61,9 @@ task("project") , {'a', "archs", "kv" , nil , "Set the project archs." , " e.g. " , " - xmake project -k vsxmake -a \"x86,x64\"" } + , {'t', "target", "kv" , nil , "Set the project target." + , " e.g. " + , " - xmake project -k compile_commands -t \"custom\"" } , {nil, "lsp", "kv" , nil , "Set the LSP backend for compile_commands." , " e.g. " , " - xmake project -k compile_commands --lsp=clangd" |
