Skip to content

feat(plugin/project): support gen compile_commands.json for specific target#6572

Merged
waruqi merged 1 commit intoxmake-io:devfrom
fcying:dev
Jun 26, 2025
Merged

feat(plugin/project): support gen compile_commands.json for specific target#6572
waruqi merged 1 commit intoxmake-io:devfrom
fcying:dev

Conversation

@fcying
Copy link
Contributor

@fcying fcying commented Jun 22, 2025

add option to generate compile_commands for specific target, for #6140

for name, target in pairs(project.targets()) do
if not target:is_phony() then
_add_target(jsonfile, target)
if selected_target == nil or name == selected_target then
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

指定 target ,应该直接从 project.target("xxx") 取,而不是走遍历,太慢

, " - xmake project -k vsxmake -a \"x86,x64\"" }
, {'t', "target", "kv" , nil , "Set specific target for compile_commands."
, " e.g. "
, " - xmake project -k compile_commands -t \"custom\"" }
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

尽可能把参数做成通用的,而不是仅仅针对 compile_commands。 像 cmakelists/vs 这些 generator 都可以指定 target 。

local project_targets
local selected_target = option.get("target")
if selected_target then
project_targets = { project.target(selected_target) }
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

不用 {} 去 wrap,pairs 在外面的 sandbox 里面,可以直接遍历的。或者用 table.wrap(target)

另外,可以弄成 function 封装下

function _get_targets()
end

Copy link
Contributor Author

@fcying fcying Jun 23, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

-- get project targets
function _get_targets()
    local project_targets
    local selected_target = option.get("target")
    if selected_target then
        project_targets = table.wrap(project.target(selected_target))
        -- project_targets = project.target(selected_target)
        -- project_targets = { project.target(selected_target) }
    else
        project_targets = project.targets()
    end
    return project_targets
end

试了下3种写法, 只有直接加{}的能用. 另外的都是提示. error: attempt to index a function value (local 'target')

Copy link
Contributor Author

@fcying fcying Jun 23, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

_get_targets 如果要做成 project模块共用的函数要放什么地方比较好? 每个文件放一份, 改一次要改一堆地方...

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

放这里,https://github.com/xmake-io/xmake/tree/dev/xmake/plugins/project/utils

有个 utils 目录,参考这个的导入方式

import("plugins.project.utils.target_cmds", {rootdir = os.programdir()})

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

更新了下, 看看还有没有问题

project_targets = { project.target(selected_target) }
else
project_targets = project.targets()
end
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

不要放全局。

_g.firstline = true

for _, target in pairs(project.targets()) do
local project_targets = target_cmds.get_project_targets()
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

不是,我的意思的参考这个,在同位置加个模块,而不是用这个。。比如 加个

target_utils.get_targets()

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

更新了, 加了target_utils.lua文件

@waruqi waruqi added this to the v3.0.1 milestone Jun 26, 2025
@waruqi waruqi merged commit 2fd95e3 into xmake-io:dev Jun 26, 2025
11 of 22 checks passed
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()})
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This causes a naming conflict with the previous target_utils import.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I have fixed it.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants