diff options
| author | ruki <[email protected]> | 2021-02-13 22:37:46 +0800 |
|---|---|---|
| committer | ruki <[email protected]> | 2021-02-13 22:37:46 +0800 |
| commit | d99e19ac256c00f965b4f39c8f4243844393e256 (patch) | |
| tree | 11bc8c4e7b9877bd84c8753df5b75718b63024a3 | |
| parent | 3e1f4c096a20cadbefa55bc3e38d900845f0ec9c (diff) | |
rewrite target/platform.tool
| -rw-r--r-- | xmake/core/platform/platform.lua | 79 | ||||
| -rw-r--r-- | xmake/core/project/target.lua | 72 | ||||
| -rw-r--r-- | xmake/core/tool/toolchain.lua | 101 |
3 files changed, 105 insertions, 147 deletions
diff --git a/xmake/core/platform/platform.lua b/xmake/core/platform/platform.lua index 5b8a0d663..0cdc7c1be 100644 --- a/xmake/core/platform/platform.lua +++ b/xmake/core/platform/platform.lua @@ -188,45 +188,15 @@ end -- get the program and name of the given tool kind function _instance:tool(toolkind) - - -- init tools - local tools = self._TOOLS - if not tools then - tools = {} - self._TOOLS = tools - end - - -- get tool program - local program, toolname, toolchain_info - local toolinfo = tools[toolkind] - if toolinfo == nil then - toolinfo = {} - local toolchains = self:toolchains() - for idx, toolchain_inst in ipairs(toolchains) do - program, toolname = toolchain_inst:tool(toolkind) - if program then - toolchain_info = {name = toolchain_inst:name(), - plat = toolchain_inst:plat(), - arch = toolchain_inst:arch(), - cachekey = toolchain_inst:cachekey()} - toolinfo[1] = program - toolinfo[2] = toolname - toolinfo[3] = toolchain_info - break - end - end - tools[toolkind] = toolinfo - else - program = toolinfo[1] - toolname = toolinfo[2] - toolchain_info = toolinfo[3] - end - return program, toolname, toolchain_info + return toolchain.tool(self:toolchains(), toolkind, {cachekey = "platform", plat = self:name(), arch = self:arch(), + before_get = function () + return config.get(toolkind) + end}) end -- get tool configuration from the toolchains function _instance:toolconfig(name) - return toolchain.toolconfig(self:toolchains(), name, {cachekey = "platform"}) + return toolchain.toolconfig(self:toolchains(), name, {cachekey = "platform", plat = self:name(), arch = self:arch()}) end -- get the platform script @@ -484,41 +454,12 @@ end -- e.g. cc, cxx, mm, mxx, as, ar, ld, sh, .. -- function platform.tool(toolkind, plat, arch) - - -- attempt to get program from config first - plat = plat or config.get("plat") or os.host() - arch = arch or config.get("arch") or os.arch() - local key = toolkind .. "_" .. plat .. "_" .. arch - local program = config.get(toolkind) or config.get("__tool_" .. key) - local toolname = config.get("__toolname_" .. key) - local toolchain_info = config.get("__toolchain_info_" .. key) - if program == nil then - - -- get the current platform - local instance, errors = platform.load(plat, arch) - if not instance then - os.raise(errors) - end - - -- get it from the platform toolchains - program, toolname, toolchain_info = instance:tool(toolkind) - if program then - config.set("__tool_" .. key, program, {force = true, readonly = true}) - config.set("__toolname_" .. key, toolname) - config.set("__toolchain_info_" .. key, toolchain_info) - config.save() - end - end - - -- contain toolname? parse it, e.g. '[email protected]' - if program and type(program) == "string" then - local pos = program:find('@', 1, true) - if pos then - toolname = program:sub(1, pos - 1) - program = program:sub(pos + 1) - end + local instance, errors = platform.load(plat, arch) + if instance then + return instance:tool(toolkind) + else + os.raise(errors) end - return program, toolname, toolchain_info end -- get the given tool configuration diff --git a/xmake/core/project/target.lua b/xmake/core/project/target.lua index e9d73846c..1f0cf5232 100644 --- a/xmake/core/project/target.lua +++ b/xmake/core/project/target.lua @@ -1737,82 +1737,28 @@ end -- get the program and name of the given tool kind function _instance:tool(toolkind) - - -- init tools - local tools = self._TOOLS - if not tools then - tools = {} - self._TOOLS = tools - end - - -- get tool program - local key = self:name() .. "_" .. toolkind .. "_" .. self:plat() .. "_" .. self:arch() - local program, toolname, toolchain_info - local toolinfo = tools[key] - if toolinfo == nil then - toolinfo = {} - + return toolchain.tool(self:toolchains(), toolkind, {cachekey = "target_" .. self:name(), plat = self:plat(), arch = self:arch(), + before_get = function() -- get program from set_toolchain/set_tools (deprecated) - program = self:get("toolset." .. toolkind) or self:get("toolchain." .. toolkind) + local program = self:get("toolset." .. toolkind) or self:get("toolchain." .. toolkind) if not program then local tools = self:get("tools") -- TODO: deprecated if tools then program = tools[toolkind] end end - - -- attempt to get program from config first if no the given toolchains in target + -- get program from `xmake f --cc` if not program and not self:get("toolchains") then - program = config.get(toolkind) or config.get("__tool_" .. key) - toolname = config.get("__toolname_" .. key) - toolchain_info = config.get("__toolchain_info_" .. key) - end - - -- get program from target/toolchains - if not program then - local toolchains = self:toolchains() - for idx, toolchain_inst in ipairs(toolchains) do - program, toolname = toolchain_inst:tool(toolkind) - if program then - toolchain_info = {name = toolchain_inst:name(), - plat = toolchain_inst:plat(), - arch = toolchain_inst:arch(), - cachekey = toolchain_inst:cachekey()} - break - end - end + program = config.get(toolkind) end - - -- contain toolname? parse it, e.g. '[email protected]' - if program and type(program) == "string" then - local pos = program:find('@', 1, true) - if pos then - toolname = program:sub(1, pos - 1) - program = program:sub(pos + 1) - end - end - - if program then - toolinfo[1] = program - toolinfo[2] = toolname - toolinfo[3] = toolchain_info - config.set("__tool_" .. key, program, {force = true, readonly = true}) - config.set("__toolname_" .. key, toolname) - config.set("__toolchain_info_" .. key, toolchain_info) - config.save() - end - tools[key] = toolinfo - else - program = toolinfo[1] - toolname = toolinfo[2] - toolchain_info = toolinfo[3] - end - return program, toolname, toolchain_info + return program + end}) end -- get tool configuration from the toolchains function _instance:toolconfig(name) - return toolchain.toolconfig(self:toolchains(), name, {cachekey = "target", after_get = function(toolchain_inst) + return toolchain.toolconfig(self:toolchains(), name, {cachekey = "target_" .. self:name(), plat = self:plat(), arch = self:arch(), + after_get = function(toolchain_inst) -- get flags from target.on_xxflags() local script = toolchain_inst:get("target.on_" .. name) if type(script) == "function" then diff --git a/xmake/core/tool/toolchain.lua b/xmake/core/tool/toolchain.lua index 7a1ca6c05..fdcf10325 100644 --- a/xmake/core/tool/toolchain.lua +++ b/xmake/core/tool/toolchain.lua @@ -31,6 +31,7 @@ local global = require("base/global") local option = require("base/option") local interpreter = require("base/interpreter") local config = require("project/config") +local memcache = require("cache/memcache") local localcache = require("cache/localcache") local language = require("language/language") local sandbox = require("sandbox/sandbox") @@ -41,7 +42,7 @@ function _instance.new(name, info, cachekey, configs) local instance = table.inherit(_instance) instance._NAME = name instance._INFO = info - instance._CACHE = localcache.cache("toolchain") + instance._CACHE = toolchain._localcache() instance._CACHEKEY = cachekey instance._CONFIGS = instance._CACHE:get(cachekey) or {} for k, v in pairs(configs) do @@ -420,6 +421,16 @@ function _instance:_checktool(toolkind, toolpath) return program, toolname end +-- get memcache +function toolchain._memcache() + return memcache.cache("core.tool.toolchain") +end + +-- get local cache +function toolchain._localcache() + return localcache.cache("toolchain") +end + -- the interpreter function toolchain._interpreter() @@ -600,23 +611,86 @@ function toolchain.load_withinfo(name, info, opt) return instance end +-- get the program and name of the given tool kind +function toolchain.tool(toolchains, toolkind, opt) + + -- get plat and arch + opt = opt or {} + local plat = opt.plat or config.get("plat") or os.host() + local arch = opt.arch or config.get("arch") or os.arch() + + -- get cache and cachekey + local cache = toolchain:_localcache() + local cachekey = "tool_" .. (opt.cachekey or "") .. "_" .. plat .. "_" .. arch .. "_" .. toolkind + local updatecache = false + + -- get program from before_script + local program, toolname, toolchain_info + local before_get = opt.before_get + if before_get then + program, toolname, toolchain_info = before_get(toolkind) + if program then + updatecache = true + end + end + + -- get program from local cache + if not program then + program = cache:get2(cachekey, "program") + toolname = cache:get2(cachekey, "toolname") + toolchain_info = cache:get2(cachekey, "toolchain_info") + end + + -- get program from toolchains + if not program then + for idx, toolchain_inst in ipairs(toolchains) do + program, toolname = toolchain_inst:tool(toolkind) + if program then + toolchain_info = {name = toolchain_inst:name(), + plat = toolchain_inst:plat(), + arch = toolchain_inst:arch(), + cachekey = toolchain_inst:cachekey()} + updatecache = true + break + end + end + end + + -- contain toolname? parse it, e.g. '[email protected]' + if program and type(program) == "string" then + local pos = program:find('@', 1, true) + if pos then + toolname = program:sub(1, pos - 1) + program = program:sub(pos + 1) + updatecache = true + end + end + + -- update cache + if program and updatecache then + cache:set2(cachekey, "program", program) + cache:set2(cachekey, "toolname", toolname) + cache:set2(cachekey, "toolchain_info", toolchain_info) + cache:save() + end + return program, toolname, toolchain_info +end + -- get tool configuration from the toolchains function toolchain.toolconfig(toolchains, name, opt) - -- init tool configs cache + -- get plat and arch opt = opt or {} - local toolconfigs = toolchain._TOOLCONFIGS - if not toolconfigs then - toolconfigs = {} - toolchain._TOOLCONFIGS = toolconfigs - end + local plat = opt.plat or config.get("plat") or os.host() + local arch = opt.arch or config.get("arch") or os.arch() + + -- get cache and cachekey + local cache = toolchain._memcache() + local cachekey = "toolconfig_" .. (opt.cachekey or "") .. "_" .. plat .. "_" .. arch -- get configuration - local cachekey = (opt.cachekey or "") .. name - local toolconfig = toolconfigs[cachekey] + local toolconfig = cache:get2(cachekey, name) if toolconfig == nil then - - -- get them from all toolchains for _, toolchain_inst in ipairs(toolchains) do local values = toolchain_inst:get(name) if values then @@ -632,10 +706,7 @@ function toolchain.toolconfig(toolchains, name, opt) end end end - - -- cache it - toolconfig = toolconfig or false - toolconfigs[cachekey] = toolconfig + cache:set2(cachekey, name, toolconfig or false) end return toolconfig or nil end |
