diff options
| author | ruki <[email protected]> | 2022-09-27 00:55:51 +0800 |
|---|---|---|
| committer | ruki <[email protected]> | 2022-09-27 00:55:51 +0800 |
| commit | 701f42aec0dd4acb291a7c69582823687a5edf85 (patch) | |
| tree | 71b3bf22fb13867507298894f660462d3c029ce1 | |
| parent | 9952e0cc62da89cdb899fe1dd02b86e156073e3e (diff) | |
improve rule, rename to instance
| -rw-r--r-- | tests/apis/rules_inject_deps/xmake.lua | 2 | ||||
| -rw-r--r-- | xmake/core/project/rule.lua | 253 |
2 files changed, 133 insertions, 122 deletions
diff --git a/tests/apis/rules_inject_deps/xmake.lua b/tests/apis/rules_inject_deps/xmake.lua index d7cfcf61f..383789e0a 100644 --- a/tests/apis/rules_inject_deps/xmake.lua +++ b/tests/apis/rules_inject_deps/xmake.lua @@ -2,7 +2,7 @@ rule("cppfront") set_extensions(".cpp2") on_load(function (target) local rule = target:rule("c++.build.modules.builder"):clone() - rule:add_deps("cppfront", {order = true}) + rule:add("deps", "cppfront", {order = true}) target:rule_set("c++.build.modules.builder", rule) end) before_build_files(function (target, batchjobs, sourcebatch, opt) diff --git a/xmake/core/project/rule.lua b/xmake/core/project/rule.lua index 841203c9d..1ac551ffd 100644 --- a/xmake/core/project/rule.lua +++ b/xmake/core/project/rule.lua @@ -20,6 +20,7 @@ -- define module local rule = rule or {} +local _instance = _instance or {} -- load modules local os = require("base/os") @@ -33,6 +34,136 @@ local sandbox = require("sandbox/sandbox") local sandbox_os = require("sandbox/modules/os") local sandbox_module = require("sandbox/modules/import/core/sandbox/module") +-- get the base rule +function _instance:base() + return self._BASE +end + +-- get the rule info +function _instance:get(name) + local value = self._INFO:get(name) + if value == nil and self:base() then + value = self:base():get(name) + end + if value ~= nil then + return value + end +end + +-- set the value to the package info +function _instance:set(name, ...) + self._INFO:apival_set(name, ...) +end + +-- add the value to the package info +function _instance:add(name, ...) + self._INFO:apival_add(name, ...) +end + +-- get the extra configuration +function _instance:extraconf(name, item, key) + local value = self._INFO:extraconf(name, item, key) + if value == nil and self:base() then + value = self:base():extraconf(name) + end + if value ~= nil then + return value + end +end + +-- get the rule name +function _instance:name() + return self._NAME +end + +-- get the rule kind +-- +-- current supported kind: +-- - target: default, only for each target +-- - project: global rule, for whole project +-- +function _instance:kind() + return self:get("kind") or "target" +end + +-- get the given dependent rule +function _instance:dep(name) + local deps = self:deps() + if deps then + return deps[name] + end +end + +-- get rule deps +function _instance:deps() + return self._DEPS +end + +-- get rule order deps +function _instance:orderdeps() + return self._ORDERDEPS +end + +-- get xxx_script +function _instance:script(name, generic) + + -- get script + local script = self:get(name) + local result = nil + if type(script) == "function" then + result = script + elseif type(script) == "table" then + + -- get plat and arch + local plat = config.get("plat") or "" + local arch = config.get("arch") or "" + + -- match pattern + -- + -- `@linux` + -- `@linux|x86_64` + -- `@macosx,linux` + -- `android@macosx,linux` + -- `android|armeabi-v7a@macosx,linux` + -- `android|armeabi-v7a@macosx,linux|x86_64` + -- `android|armeabi-v7a@linux|x86_64` + -- + for _pattern, _script in pairs(script) do + local hosts = {} + local hosts_spec = false + _pattern = _pattern:gsub("@(.+)", function (v) + for _, host in ipairs(v:split(',')) do + hosts[host] = true + hosts_spec = true + end + return "" + end) + if not _pattern:startswith("__") and (not hosts_spec or hosts[os.subhost() .. '|' .. os.subarch()] or hosts[os.subhost()]) + and (_pattern:trim() == "" or (plat .. '|' .. arch):find('^' .. _pattern .. '$') or plat:find('^' .. _pattern .. '$')) then + result = _script + break + end + end + + -- get generic script + result = result or script["__generic__"] or generic + end + + -- only generic script + result = result or generic + + -- imports some modules first + if result and result ~= generic then + local scope = getfenv(result) + if scope then + for _, modulename in ipairs(table.wrap(self:get("imports"))) do + scope[sandbox_module.name(modulename)] = sandbox_module.import(modulename, {anonymous = true}) + end + end + end + return result +end + -- the directories of rule function rule._directories() return { path.join(global.directory(), "rules") @@ -209,132 +340,12 @@ end -- new a rule instance function rule.new(name, info) - local instance = table.inherit(rule) + local instance = table.inherit(_instance) instance._NAME = name instance._INFO = info return instance end --- get the base rule -function rule:base() - return self._BASE -end - --- get the rule info -function rule:get(name) - local value = self._INFO:get(name) - if value == nil and self:base() then - value = self:base():get(name) - end - if value ~= nil then - return value - end -end - --- get the extra configuration -function rule:extraconf(name, item, key) - local value = self._INFO:extraconf(name, item, key) - if value == nil and self:base() then - value = self:base():extraconf(name) - end - if value ~= nil then - return value - end -end - --- get the rule name -function rule:name() - return self._NAME -end - --- get the rule kind --- --- current supported kind: --- - target: default, only for each target --- - project: global rule, for whole project --- -function rule:kind() - return self:get("kind") or "target" -end - --- get the given dependent rule -function rule:dep(name) - local deps = self:deps() - if deps then - return deps[name] - end -end - --- get rule deps -function rule:deps() - return self._DEPS -end - --- get rule order deps -function rule:orderdeps() - return self._ORDERDEPS -end - --- get xxx_script -function rule:script(name, generic) - - -- get script - local script = self:get(name) - local result = nil - if type(script) == "function" then - result = script - elseif type(script) == "table" then - - -- get plat and arch - local plat = config.get("plat") or "" - local arch = config.get("arch") or "" - - -- match pattern - -- - -- `@linux` - -- `@linux|x86_64` - -- `@macosx,linux` - -- `android@macosx,linux` - -- `android|armeabi-v7a@macosx,linux` - -- `android|armeabi-v7a@macosx,linux|x86_64` - -- `android|armeabi-v7a@linux|x86_64` - -- - for _pattern, _script in pairs(script) do - local hosts = {} - local hosts_spec = false - _pattern = _pattern:gsub("@(.+)", function (v) - for _, host in ipairs(v:split(',')) do - hosts[host] = true - hosts_spec = true - end - return "" - end) - if not _pattern:startswith("__") and (not hosts_spec or hosts[os.subhost() .. '|' .. os.subarch()] or hosts[os.subhost()]) - and (_pattern:trim() == "" or (plat .. '|' .. arch):find('^' .. _pattern .. '$') or plat:find('^' .. _pattern .. '$')) then - result = _script - break - end - end - - -- get generic script - result = result or script["__generic__"] or generic - end - - -- only generic script - result = result or generic - - -- imports some modules first - if result and result ~= generic then - local scope = getfenv(result) - if scope then - for _, modulename in ipairs(table.wrap(self:get("imports"))) do - scope[sandbox_module.name(modulename)] = sandbox_module.import(modulename, {anonymous = true}) - end - end - end - return result -end - -- get the given global rule function rule.rule(name) return rule.rules()[name] |
