diff options
| author | ruki <[email protected]> | 2022-09-27 23:57:20 +0800 |
|---|---|---|
| committer | ruki <[email protected]> | 2022-09-27 23:57:20 +0800 |
| commit | 309a51cb9692851e30ed9cf44f8e33b34976332c (patch) | |
| tree | 7023de4de3aad9971e7763f0e976886e55f8032e | |
| parent | a1d3d4633fcbe0d112952f5d0ea111570ce3876f (diff) | |
clone target
| -rw-r--r-- | tests/apis/clone_target/xmake.lua | 6 | ||||
| -rw-r--r-- | xmake/core/base/table.lua | 14 | ||||
| -rw-r--r-- | xmake/core/project/project.lua | 6 | ||||
| -rw-r--r-- | xmake/core/project/target.lua | 32 | ||||
| -rw-r--r-- | xmake/core/sandbox/modules/import/core/project/project.lua | 1 |
5 files changed, 54 insertions, 5 deletions
diff --git a/tests/apis/clone_target/xmake.lua b/tests/apis/clone_target/xmake.lua index 4aa01b034..4def46283 100644 --- a/tests/apis/clone_target/xmake.lua +++ b/tests/apis/clone_target/xmake.lua @@ -2,6 +2,10 @@ target("test") set_kind("binary") add_files("src/*.cpp") after_load(function (target) - --target:clone() + import("core.project.project") + local t = target:clone() + t:name_set("test2") + t:add("defines", "TEST2") + project.target_add(t) end) diff --git a/xmake/core/base/table.lua b/xmake/core/base/table.lua index b3e8840e3..5e04b0256 100644 --- a/xmake/core/base/table.lua +++ b/xmake/core/base/table.lua @@ -153,7 +153,19 @@ function table.append(array, ...) return array end --- copy the table to self +-- clone table +function table.clone(self) + local result + if type(self) == "table" then + result = {} + for k, v in pairs(self) do + result[k] = v + end + end + return result +end + +-- copy the table (deprecated, please use table.clone) function table.copy(copied) local result = {} copied = copied or {} diff --git a/xmake/core/project/project.lua b/xmake/core/project/project.lua index d0f0145df..73ff45332 100644 --- a/xmake/core/project/project.lua +++ b/xmake/core/project/project.lua @@ -340,7 +340,7 @@ function project._load_targets() -- make targets local targets = {} for targetname, targetinfo in pairs(results) do - local t = target.new(targetname, targetinfo, project) + local t = target.new(targetname, targetinfo) if t and (t:get("enabled") == nil or t:get("enabled") == true) then targets[targetname] = t end @@ -898,6 +898,10 @@ function project.target(name) return targets and targets[name] end +-- add the given target +function project.target_add(t) +end + -- get targets function project.targets() local targets = project._memcache():get("targets") diff --git a/xmake/core/project/target.lua b/xmake/core/project/target.lua index f0e519384..8365c50ff 100644 --- a/xmake/core/project/target.lua +++ b/xmake/core/project/target.lua @@ -48,11 +48,10 @@ local sandbox = require("sandbox/sandbox") local sandbox_module = require("sandbox/modules/import/core/sandbox/module") -- new a target instance -function _instance.new(name, info, project) +function _instance.new(name, info) local instance = table.inherit(_instance) instance._NAME = name instance._INFO = info - instance._PROJECT = project instance._CACHEID = 1 return instance end @@ -283,6 +282,30 @@ function _instance:_is_loaded() return self._LOADED end +-- clone target +function _instance:clone() + local instance = target.new(self:name(), self._INFO:clone()) + if self._DEPS then + instance._DEPS = table.clone(self._DEPS) + end + if self._ORDERDEPS then + instance._ORDERDEPS = table.clone(self._ORDERDEPS) + end + if self._RULES then + instance._RULES = table.clone(self._RULES) + end + if self._ORDERULES then + instance._ORDERULES = table.clone(self._ORDERULES) + end + if self._DATA then + instance._DATA = table.clone(self._DATA) + end + if self._SOURCEFILES then + instance._SOURCEFILES = table.clone(self._SOURCEFILES) + end + return instance +end + -- get the target info -- -- e.g. @@ -491,6 +514,11 @@ function _instance:name() return self._NAME end +-- set the target name +function _instance:name_set(name) + self._NAME = name +end + -- get the target kind function _instance:kind() return self:get("kind") or "binary" diff --git a/xmake/core/sandbox/modules/import/core/project/project.lua b/xmake/core/sandbox/modules/import/core/project/project.lua index 46c9b973d..a5778bc9e 100644 --- a/xmake/core/sandbox/modules/import/core/project/project.lua +++ b/xmake/core/sandbox/modules/import/core/project/project.lua @@ -43,6 +43,7 @@ sandbox_core_project.rules = project.rules sandbox_core_project.toolchain = project.toolchain sandbox_core_project.toolchains = project.toolchains sandbox_core_project.target = project.target +sandbox_core_project.target_add = project.target_add sandbox_core_project.targets = project.targets sandbox_core_project.ordertargets = project.ordertargets sandbox_core_project.option = project.option |
