diff options
| author | ruki <[email protected]> | 2020-12-13 20:40:03 +0800 |
|---|---|---|
| committer | ruki <[email protected]> | 2020-12-13 20:40:03 +0800 |
| commit | d313c481f3bc4288ca06ba0a043ae36234c151da (patch) | |
| tree | 25c1e72158b313a35a3ff97f7c29207873392bb2 | |
| parent | fe38b39cd90b8cd866121b3e443aeba277f4d3c6 (diff) | |
improve toolchain
| -rw-r--r-- | xmake/core/project/project.lua | 47 | ||||
| -rw-r--r-- | xmake/core/sandbox/modules/import/core/tool/toolchain.lua | 2 | ||||
| -rw-r--r-- | xmake/core/tool/toolchain.lua | 52 |
3 files changed, 69 insertions, 32 deletions
diff --git a/xmake/core/project/project.lua b/xmake/core/project/project.lua index 8098b88fc..0c6d5a463 100644 --- a/xmake/core/project/project.lua +++ b/xmake/core/project/project.lua @@ -321,7 +321,7 @@ function project._load_rules() end -- load toolchains -function project._load_toolchains(opt) +function project._load_toolchains() -- load the project file first if has not been loaded? local ok, errors = project._load() @@ -338,7 +338,7 @@ function project._load_toolchains(opt) -- make toolchain instances local toolchains = {} for toolchain_name, toolchain_info in pairs(results) do - toolchains[toolchain_name] = toolchain.new(toolchain_name, toolchain_info, opt) + toolchains[toolchain_name] = toolchain_info end return toolchains end @@ -671,6 +671,20 @@ function project._sort_targets(targets, ordertargets, targetrefs, target) end end +-- get project toolchain infos (@note only with toolchain info) +function project._toolchains() + local toolchains = project._TOOLCHAINS + if not toolchains then + local errors + toolchains, errors = project._load_toolchains() + if not toolchains then + os.raise(errors) + end + project._TOOLCHAINS = toolchains + end + return toolchains +end + -- get project apis function project.apis() @@ -1023,24 +1037,15 @@ end -- get the given toolchain function project.toolchain(name, opt) - return project.toolchains(opt)[name] + local info = project._toolchains()[name] + if info then + return toolchain.load_withinfo(name, info, opt) + end end --- get project toolchains -function project.toolchains(opt) - opt = opt or {} - local key = "key_" .. (opt.plat or "") .. "_" .. (opt.arch or "") - project._TOOLCHAINS = project._TOOLCHAINS or {} - local toolchains = project._TOOLCHAINS[key] - if not toolchains then - local errors - toolchains, errors = project._load_toolchains(opt) - if not toolchains then - os.raise(errors) - end - project._TOOLCHAINS[key] = toolchains - end - return toolchains +-- get project toolchains list +function project.toolchains() + return table.keys(project._toolchains()) end -- get the given task @@ -1050,10 +1055,7 @@ end -- get tasks function project.tasks() - if not project._TASKS then - - -- load tasks local tasks, errors = project._load_tasks() if not tasks then os.raise(errors) @@ -1065,10 +1067,7 @@ end -- get packages function project.packages() - if not project._PACKAGES then - - -- load packages local packages, errors = project._load_packages() if not packages then return nil, errors diff --git a/xmake/core/sandbox/modules/import/core/tool/toolchain.lua b/xmake/core/sandbox/modules/import/core/tool/toolchain.lua index a6dae7f9d..a2041573a 100644 --- a/xmake/core/sandbox/modules/import/core/tool/toolchain.lua +++ b/xmake/core/sandbox/modules/import/core/tool/toolchain.lua @@ -31,7 +31,7 @@ local raise = require("sandbox/modules/raise") function sandbox_core_tool_toolchain.list() local names = table.copy(platform.toolchains()) if os.isfile(os.projectfile()) then - for name, _ in pairs(project.toolchains()) do + for _, name in ipairs(project.toolchains()) do table.insert(names, name) end end diff --git a/xmake/core/tool/toolchain.lua b/xmake/core/tool/toolchain.lua index bc152a3f6..a411b4db5 100644 --- a/xmake/core/tool/toolchain.lua +++ b/xmake/core/tool/toolchain.lua @@ -36,12 +36,13 @@ local sandbox = require("sandbox/sandbox") local sandbox_module = require("sandbox/modules/import/core/sandbox/module") -- new an instance -function _instance.new(name, info, plat, arch) +function _instance.new(name, info, plat, arch, configs) local instance = table.inherit(_instance) instance._NAME = name instance._INFO = info instance._PLAT = plat instance._ARCH = arch + instance._CONFIGS = configs return instance end @@ -192,6 +193,17 @@ function _instance:sdkdir() return config.get("sdk") or self:info():get("sdkdir") end +-- get user config from `set_toolchains("", {configs = {vs = "2018"}})` +function _instance:config(name) + return self._CONFIGS and self._CONFIGS[name] +end + +-- set user config +function _instance:config_set(name, data) + self._CONFIGS = self._CONFIGS or {} + self._CONFIGS[name] = data +end + -- do check, we only check it once for all architectures function _instance:check() local checkok = true @@ -378,6 +390,19 @@ function toolchain._interpreter() return interp end +-- get cache key +function toolchain._cachekey(name, plat, arch, configs) + local cachekey = name .. plat .. arch + local configs = configs + if configs then + for _, k in ipairs(table.orderkeys(configs)) do + local v = configs[k] + cachekey = cachekey .. "_" .. k .. "_" .. tostring(v) + end + end + return cachekey +end + -- get toolchain apis function toolchain.apis() return @@ -419,14 +444,14 @@ function toolchain.directories() return dirs end --- load the given toolchain +-- load toolchain function toolchain.load(name, opt) -- init cache key 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() - local cachekey = name .. plat .. arch + local cachekey = toolchain._cachekey(name, plat, arch, opt.configs) -- get it directly from cache dirst toolchain._TOOLCHAINS = toolchain._TOOLCHAINS or {} @@ -468,17 +493,30 @@ function toolchain.load(name, opt) end -- save instance to the cache - local instance = _instance.new(name, result, plat, arch) + local instance = _instance.new(name, result, plat, arch, opt.configs) toolchain._TOOLCHAINS[cachekey] = instance return instance end --- new toolchain -function toolchain.new(name, info, opt) +-- load toolchain from the give toolchain info +function toolchain.load_withinfo(name, info, opt) + + -- init cache key 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() - return _instance.new(name, info, plat, arch) + local cachekey = toolchain._cachekey(name, plat, arch, opt.configs) + + -- get it directly from cache dirst + toolchain._TOOLCHAINS = toolchain._TOOLCHAINS or {} + if toolchain._TOOLCHAINS[cachekey] then + return toolchain._TOOLCHAINS[cachekey] + end + + -- save instance to the cache + local instance = _instance.new(name, info, plat, arch, opt.configs) + toolchain._TOOLCHAINS[cachekey] = instance + return instance end -- return module |
