diff options
| author | ruki <[email protected]> | 2026-06-18 00:05:26 +0800 |
|---|---|---|
| committer | ruki <[email protected]> | 2026-06-18 00:05:26 +0800 |
| commit | b59597d77a411a53ef74d9e384a25dbd9e82a634 (patch) | |
| tree | f395e9c21f91d6eebe01b22b83f6634f5be401e8 | |
| parent | eba488dafedd88c1f19e23132276872aac877b73 (diff) | |
improve to get targets
| -rw-r--r-- | xmake/actions/install/install.lua | 17 | ||||
| -rw-r--r-- | xmake/actions/install/main.lua | 15 | ||||
| -rw-r--r-- | xmake/actions/package/local/main.lua | 18 | ||||
| -rw-r--r-- | xmake/actions/package/oldpkg/main.lua | 19 | ||||
| -rw-r--r-- | xmake/actions/package/remote/main.lua | 18 | ||||
| -rw-r--r-- | xmake/actions/uninstall/uninstall.lua | 17 | ||||
| -rw-r--r-- | xmake/modules/private/action/utils.lua | 35 | ||||
| -rw-r--r-- | xmake/plugins/format/main.lua | 20 |
8 files changed, 55 insertions, 104 deletions
diff --git a/xmake/actions/install/install.lua b/xmake/actions/install/install.lua index 33fc4a795..95b774d58 100644 --- a/xmake/actions/install/install.lua +++ b/xmake/actions/install/install.lua @@ -24,6 +24,7 @@ import("core.base.option") import("core.project.rule") import("core.project.project") import("target.action.install", {alias = "_do_install_target"}) +import("private.action.utils", {alias = "action_utils"}) -- on install target function _on_install_target(target) @@ -115,21 +116,7 @@ end -- @param targetnames the target names (table), a single target name, or the magic "__all"/"__def" -- function main(targetnames, group_pattern) - local targets = {} - if type(targetnames) == "table" then - for _, targetname in ipairs(targetnames) do - table.insert(targets, project.target(targetname)) - end - elseif targetnames and not targetnames:startswith("__") then - table.insert(targets, project.target(targetnames)) - else - for _, target in ipairs(project.ordertargets()) do - local group = target:get("group") - if (target:is_default() and not group_pattern) or targetnames == "__all" or (group_pattern and group and group:match(group_pattern)) then - table.insert(targets, target) - end - end - end + local targets = action_utils.get_targets(targetnames, {group_pattern = group_pattern}) if #targets > 0 then _install_targets(table.unique(targets)) end diff --git a/xmake/actions/install/main.lua b/xmake/actions/install/main.lua index 80b428cab..2cf7046b6 100644 --- a/xmake/actions/install/main.lua +++ b/xmake/actions/install/main.lua @@ -33,20 +33,7 @@ import("private.action.utils", {alias = "action_utils"}) function _check_targets(targetnames, group_pattern) -- get targets - local targets = {} - if targetnames and #targetnames > 0 then - for _, targetname in ipairs(targetnames) do - table.insert(targets, project.target(targetname)) - end - else - -- install default or all targets - for _, target in pairs(project.targets()) do - local group = target:get("group") - if (target:is_default() and not group_pattern) or option.get("all") or (group_pattern and group and group:match(group_pattern)) then - table.insert(targets, target) - end - end - end + local targets = action_utils.get_targets(targetnames, {group_pattern = group_pattern}) -- filter and check targets with builtin-install script local targetnames = {} diff --git a/xmake/actions/package/local/main.lua b/xmake/actions/package/local/main.lua index 48b0ce5bd..97b2cae37 100644 --- a/xmake/actions/package/local/main.lua +++ b/xmake/actions/package/local/main.lua @@ -25,7 +25,7 @@ import("core.project.rule") import("core.project.config") import("core.project.project") import("target.action.install") -import("private.detect.check_targetname") +import("private.action.utils", {alias = "action_utils"}) -- get library deps function _get_librarydeps(target) @@ -229,19 +229,9 @@ function main() -- build it first task.run("build", {targets = targetnames, all = option.get("all")}) - -- package the given targets? - if targetnames and #targetnames > 0 then - for _, targetname in ipairs(targetnames) do - local target = assert(check_targetname(targetname)) - _package_target(target) - end - else - -- package default or all targets - for _, target in ipairs(project.ordertargets()) do - if target:is_default() or option.get("all") then - _package_target(target) - end - end + -- package the given targets + for _, target in ipairs(action_utils.get_targets(targetnames, {all = option.get("all")})) do + _package_target(target) end -- unlock the whole project diff --git a/xmake/actions/package/oldpkg/main.lua b/xmake/actions/package/oldpkg/main.lua index f9b543cc0..db9a5c0de 100644 --- a/xmake/actions/package/oldpkg/main.lua +++ b/xmake/actions/package/oldpkg/main.lua @@ -24,7 +24,7 @@ import("core.base.task") import("core.project.rule") import("core.project.config") import("core.project.project") -import("private.detect.check_targetname") +import("private.action.utils", {alias = "action_utils"}) -- package library function _package_library(target) @@ -189,20 +189,13 @@ function main() -- build it first task.run("build", {targets = targetnames, all = option.get("all")}) - -- package the given targets? - if targetnames and #targetnames > 0 then - for _, targetname in ipairs(targetnames) do - local target = assert(check_targetname(targetname)) + -- package the given targets, also package the deps of the explicitly given targets + local explicit = targetnames and #targetnames > 0 + for _, target in ipairs(action_utils.get_targets(targetnames, {all = option.get("all")})) do + if explicit then _package_targets(target:orderdeps()) - _package_target(target) - end - else - -- package default or all targets - for _, target in ipairs(project.ordertargets()) do - if target:is_default() or option.get("all") then - _package_target(target) - end end + _package_target(target) end -- unlock the whole project diff --git a/xmake/actions/package/remote/main.lua b/xmake/actions/package/remote/main.lua index 27e7afc89..dbb9f9fde 100644 --- a/xmake/actions/package/remote/main.lua +++ b/xmake/actions/package/remote/main.lua @@ -25,7 +25,7 @@ import("core.project.rule") import("core.project.config") import("core.project.project") import("core.base.bit") -import("private.detect.check_targetname") +import("private.action.utils", {alias = "action_utils"}) -- get library deps function _get_librarydeps(target) @@ -151,20 +151,10 @@ function main() -- load config config.load() - -- package the given targets? + -- package the given targets local targetnames = option.get("targets") - if targetnames and #targetnames > 0 then - for _, targetname in ipairs(targetnames) do - local target = assert(check_targetname(targetname)) - _package_target(target) - end - else - -- package default or all targets - for _, target in ipairs(project.ordertargets()) do - if target:is_default() or option.get("all") then - _package_target(target) - end - end + for _, target in ipairs(action_utils.get_targets(targetnames, {all = option.get("all")})) do + _package_target(target) end -- unlock the whole project diff --git a/xmake/actions/uninstall/uninstall.lua b/xmake/actions/uninstall/uninstall.lua index 9ebc32d20..e5ccc6cbd 100644 --- a/xmake/actions/uninstall/uninstall.lua +++ b/xmake/actions/uninstall/uninstall.lua @@ -23,6 +23,7 @@ import("core.base.task") import("core.project.rule") import("core.project.project") import("target.action.uninstall", {alias = "_do_uninstall_target"}) +import("private.action.utils", {alias = "action_utils"}) -- on uninstall target function _on_uninstall_target(target) @@ -110,21 +111,7 @@ end -- @param targetnames the target names (table), a single target name, or the magic "__all"/"__def" -- function main(targetnames, group_pattern) - local targets = {} - if type(targetnames) == "table" then - for _, targetname in ipairs(targetnames) do - table.insert(targets, project.target(targetname)) - end - elseif targetnames and not targetnames:startswith("__") then - table.insert(targets, project.target(targetnames)) - else - for _, target in ipairs(project.ordertargets()) do - local group = target:get("group") - if (target:is_default() and not group_pattern) or targetnames == "__all" or (group_pattern and group and group:match(group_pattern)) then - table.insert(targets, target) - end - end - end + local targets = action_utils.get_targets(targetnames, {group_pattern = group_pattern}) if #targets > 0 then _uninstall_targets(table.unique(targets)) end diff --git a/xmake/modules/private/action/utils.lua b/xmake/modules/private/action/utils.lua index c31735c54..7878dc916 100644 --- a/xmake/modules/private/action/utils.lua +++ b/xmake/modules/private/action/utils.lua @@ -20,6 +20,8 @@ -- imports import("core.base.option") +import("core.project.project") +import("private.detect.check_targetname") -- get target name and group pattern from option function get_target_and_group() @@ -57,3 +59,36 @@ function get_targets_and_group() end return targetnames, group_pattern end + +-- get the selected target objects from the given target names +-- +-- if explicit target names are given, the matching targets are returned (and checked). +-- otherwise it selects the default/all/group targets, e.g. the actions like +-- build/install/uninstall/package/format share the same selection rule. +-- +-- @param targetnames a list of target names, a single target name, or the magic "__all"/"__def" +-- @param opt the options, e.g. {group_pattern = ..., all = false} +-- +-- @return the selected target objects (list) +-- +function get_targets(targetnames, opt) + opt = opt or {} + local targets = {} + if type(targetnames) == "table" then + for _, targetname in ipairs(targetnames) do + table.insert(targets, assert(check_targetname(targetname))) + end + elseif type(targetnames) == "string" and not targetnames:startswith("__") then + table.insert(targets, assert(check_targetname(targetnames))) + else + local all = opt.all or targetnames == "__all" or option.get("all") + local group_pattern = opt.group_pattern + for _, target in ipairs(project.ordertargets()) do + local group = target:get("group") + if (target:is_default() and not group_pattern) or all or (group_pattern and group and group:match(group_pattern)) then + table.insert(targets, target) + end + end + end + return targets +end diff --git a/xmake/plugins/format/main.lua b/xmake/plugins/format/main.lua index f8be7c428..408de0421 100644 --- a/xmake/plugins/format/main.lua +++ b/xmake/plugins/format/main.lua @@ -93,24 +93,6 @@ function _get_file_patterns(sourcefiles) return patterns end --- get all the targets that match the group or targetname -function _get_targets(targetnames, group_pattern) - local targets = {} - if targetnames and #targetnames > 0 then - for _, targetname in ipairs(targetnames) do - table.insert(targets, project.target(targetname)) - end - else - for _, target in pairs(project.targets()) do - local group = target:get("group") - if (target:is_default() and not group_pattern) or option.get("all") or (group_pattern and group and group:match(group_pattern)) then - table.insert(targets, target) - end - end - end - return targets -end - -- tell if the source batch is a c/c++/objc/objc++/cuda source batch function _source_batch_should_format(sourcebatch) local rulename = sourcebatch.rulename @@ -181,7 +163,7 @@ function main() -- collect sourcefiles local sourcefiles = {} local targetnames, group_pattern = action_utils.get_targets_and_group() - local targets = _get_targets(targetnames, group_pattern) + local targets = action_utils.get_targets(targetnames, {group_pattern = group_pattern}) if option.get("files") then local filepatterns = _get_file_patterns(option.get("files")) for _, target in ipairs(targets) do |
