summaryrefslogtreecommitdiff
path: root/xmake/plugins/project/utils/target_cmds.lua
diff options
context:
space:
mode:
authorruki <[email protected]>2023-04-27 00:19:59 +0800
committerruki <[email protected]>2023-04-27 00:19:59 +0800
commit9299a0eaa3629ee2e9b29cb8ba8bbd8bf26a80a5 (patch)
tree45f742338e9fd50b128434a71c46bd235890b37b /xmake/plugins/project/utils/target_cmds.lua
parentae92dae8d3e9e5acc5a82f8c6e1cc33fcbdfa715 (diff)
add target cmds util
Diffstat (limited to 'xmake/plugins/project/utils/target_cmds.lua')
-rw-r--r--xmake/plugins/project/utils/target_cmds.lua124
1 files changed, 124 insertions, 0 deletions
diff --git a/xmake/plugins/project/utils/target_cmds.lua b/xmake/plugins/project/utils/target_cmds.lua
new file mode 100644
index 000000000..c71b23f96
--- /dev/null
+++ b/xmake/plugins/project/utils/target_cmds.lua
@@ -0,0 +1,124 @@
+--!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_cmds.lua
+--
+
+-- imports
+import("core.project.project")
+import("core.project.config")
+import("core.base.hashset")
+import("core.project.rule")
+import("private.utils.batchcmds")
+import("private.utils.rule_groups")
+
+-- this sourcebatch is built?
+function _sourcebatch_is_built(sourcebatch)
+ -- we can only use rulename to filter them because sourcekind may be bound to multiple rules
+ local rulename = sourcebatch.rulename
+ if rulename == "c.build" or rulename == "c++.build"
+ or rulename == "asm.build" or rulename == "cuda.build"
+ or rulename == "objc.build" or rulename == "objc++.build"
+ or rulename == "win.sdk.resource" then
+ return true
+ end
+end
+
+-- get target buildcmd commands
+function get_target_buildcmd(target, cmds, suffix)
+ for _, ruleinst in ipairs(target:orderules()) do
+ local scriptname = "buildcmd" .. (suffix and ("_" .. suffix) or "")
+ local script = ruleinst:script(scriptname)
+ if script then
+ local batchcmds_ = batchcmds.new({target = target})
+ script(target, batchcmds_, {})
+ if not batchcmds_:empty() then
+ table.join2(cmds, batchcmds_:cmds())
+ end
+ end
+ end
+end
+
+-- get target buildcmd_files commands
+function get_target_buildcmd_files(target, cmds, sourcebatch, suffix)
+
+ -- get rule
+ local rulename = assert(sourcebatch.rulename, "unknown rule for sourcebatch!")
+ local ruleinst = assert(target:rule(rulename) or project.rule(rulename) or rule.rule(rulename), "unknown rule: %s", rulename)
+
+ -- generate commands for xx_buildcmd_files
+ local scriptname = "buildcmd_files" .. (suffix and ("_" .. suffix) or "")
+ local script = ruleinst:script(scriptname)
+ if script then
+ local batchcmds_ = batchcmds.new({target = target})
+ script(target, batchcmds_, sourcebatch, {})
+ if not batchcmds_:empty() then
+ table.join2(cmds, batchcmds_:cmds())
+ end
+ end
+
+ -- generate commands for xx_buildcmd_file
+ if not script then
+ scriptname = "buildcmd_file" .. (suffix and ("_" .. suffix) or "")
+ script = ruleinst:script(scriptname)
+ if script then
+ local sourcekind = sourcebatch.sourcekind
+ for _, sourcefile in ipairs(sourcebatch.sourcefiles) do
+ local batchcmds_ = batchcmds.new({target = target})
+ script(target, batchcmds_, sourcefile, {})
+ if not batchcmds_:empty() then
+ table.join2(cmds, batchcmds_:cmds())
+ end
+ end
+ end
+ end
+end
+
+-- get target buildcmd commands of source group
+function get_target_buildcmd_sourcegroups(target, cmds, groups, suffix)
+ for idx, group in irpairs(groups) do
+ for _, item in pairs(group) do
+ -- buildcmd scripts are always in rule, so we need ignore target item (item.target).
+ local sourcebatch = item.sourcebatch
+ if item.rule then
+ if not _sourcebatch_is_built(sourcebatch) then
+ get_target_buildcmd_files(target, cmds_before, sourcebatch, suffix)
+ end
+ end
+ end
+ end
+end
+
+-- get target custom commands
+function _get_target_cmds(target)
+
+ -- build sourcebatch groups first
+ local groups = rule_groups.build_sourcebatch_groups(target, target:sourcebatches())
+
+ -- add before commands
+ -- we use irpairs(groups), because the last group that should be given the highest priority.
+ local cmds_before = {}
+ get_target_buildcmd(target, cmds_before, "before")
+ get_target_buildcmd_sourcegroups(target, cmds_before, groups, "before")
+ _add_target_cmds_for_batchcmds(target, "before", cmds_before)
+
+ -- add after commands
+ local cmds_after = {}
+ get_target_buildcmd(target, cmds_after, "after")
+ get_target_buildcmd_sourcegroups(target, cmds_after, groups, "after")
+ _add_target_cmds_for_batchcmds(target, "after", cmds_after)
+end