diff options
| author | ruki <[email protected]> | 2022-02-10 00:33:18 +0800 |
|---|---|---|
| committer | ruki <[email protected]> | 2022-02-10 00:33:18 +0800 |
| commit | 763c2455abcde109ae9e9a8cbc8d0b81bbc68543 (patch) | |
| tree | 48ed436814d578e76525e229872c65e8b291b82e | |
| parent | 6efa53d991361c262f514246db0f1d808d3b297a (diff) | |
save and load toolchain
| -rw-r--r-- | tests/projects/package/toolchain_muslcc/xmake.lua | 21 | ||||
| -rw-r--r-- | xmake/core/base/io.lua | 27 | ||||
| -rw-r--r-- | xmake/core/tool/toolchain.lua | 42 | ||||
| -rw-r--r-- | xmake/modules/package/tools/xmake.lua | 22 | ||||
| -rw-r--r-- | xmake/toolchains/muslcc/xmake.lua | 4 |
5 files changed, 72 insertions, 44 deletions
diff --git a/tests/projects/package/toolchain_muslcc/xmake.lua b/tests/projects/package/toolchain_muslcc/xmake.lua index b111d5976..829ca8b9e 100644 --- a/tests/projects/package/toolchain_muslcc/xmake.lua +++ b/tests/projects/package/toolchain_muslcc/xmake.lua @@ -7,6 +7,22 @@ set_arch("arm") -- lock requires set_policy("package.requires_lock", true) +-- custom toolchain +toolchain("my_muslcc") + set_homepage("https://musl.cc/") + set_description("The musl-based cross-compilation toolchains") + set_kind("cross") + on_load(function (toolchain) + toolchain:load_cross_toolchain() + if toolchain:is_arch("arm") then + toolchain:add("cxflags", "-march=armv7-a", "-msoft-float", {force = true}) + toolchain:add("ldflags", "-march=armv7-a", "-msoft-float", {force = true}) + toolchain:add("syslinks", "atomic") -- fix undefined reference to `__atomic_fetch_add_8' + end + toolchain:add("syslinks", "gcc", "c") + end) +toolchain_end() + -- add library packages -- for testing zlib/xmake, libplist/autoconf, libogg/cmake add_requires("zlib", "libogg", {system = false}) @@ -18,7 +34,10 @@ end add_requires("muslcc") -- set global toolchains for target and packages -set_toolchains("@muslcc") +set_toolchains("my_muslcc@muslcc") + +-- use the builltin toolchain("muslcc") instead of "my_muslcc" +--set_toolchains("@muslcc") target("test") set_kind("binary") diff --git a/xmake/core/base/io.lua b/xmake/core/base/io.lua index 38df841a1..2f7fed671 100644 --- a/xmake/core/base/io.lua +++ b/xmake/core/base/io.lua @@ -601,66 +601,41 @@ end -- save object the the given filepath function io.save(filepath, object, opt) - - -- check assert(filepath and object) - -- init option opt = opt or {} - - -- open the file local file, err = io.open(filepath, "wb", opt) if err then - -- error return false, err end - -- save object to file local ok, errors = file:save(object, opt) - -- close file file:close() if not ok then - -- error return false, string.format("save %s failed, %s!", filepath, errors) end - - -- ok return true end -- load object from the given file function io.load(filepath, opt) - - -- check assert(filepath) - -- init option opt = opt or {} - - -- open the file local file, err = io.open(filepath, "rb", opt) if err then - -- error return nil, err end - - -- load object local result, errors = file:load() - - -- close file file:close() - - -- ok? return result, errors end -- gsub the given file and return replaced data function io.gsub(filepath, pattern, replace, opt) - -- init option - opt = opt or {} - -- read all data from file + opt = opt or {} local data, errors = io.readfile(filepath, opt) if not data then return nil, 0, errors end diff --git a/xmake/core/tool/toolchain.lua b/xmake/core/tool/toolchain.lua index f4a511f0f..b20479c61 100644 --- a/xmake/core/tool/toolchain.lua +++ b/xmake/core/tool/toolchain.lua @@ -29,6 +29,7 @@ local utils = require("base/utils") local table = require("base/table") local global = require("base/global") local option = require("base/option") +local scopeinfo = require("base/scopeinfo") local interpreter = require("base/interpreter") local config = require("project/config") local memcache = require("cache/memcache") @@ -38,13 +39,14 @@ local sandbox = require("sandbox/sandbox") local sandbox_module = require("sandbox/modules/import/core/sandbox/module") -- new an instance -function _instance.new(name, info, cachekey, configs) - local instance = table.inherit(_instance) - instance._NAME = name - instance._INFO = info - instance._CACHE = toolchain._localcache() - instance._CACHEKEY = cachekey - instance._CONFIGS = instance._CACHE:get(cachekey) or {} +function _instance.new(name, info, cachekey, is_builtin, configs) + local instance = table.inherit(_instance) + instance._NAME = name + instance._INFO = info + instance._IS_BUILTIN = is_builtin + instance._CACHE = toolchain._localcache() + instance._CACHEKEY = cachekey + instance._CONFIGS = instance._CACHE:get(cachekey) or {} for k, v in pairs(configs) do instance._CONFIGS[k] = v end @@ -162,6 +164,11 @@ function _instance:is_global() return self:config("__global") end +-- is builtin toolchain? it's not from local project +function _instance:is_builtin() + return self._IS_BUILTIN +end + -- get the run environments function _instance:runenvs() local runenvs = self._RUNENVS @@ -292,6 +299,11 @@ function _instance:packages() return packages or nil end +-- save toolchain to file +function _instance:savefile(filepath) + return io.save(filepath, self:info():info()) +end + -- on check (builtin) function _instance:_on_check() local on_check = self:info():get("check") @@ -603,7 +615,7 @@ function toolchain.load(name, opt) end -- save instance to the cache - instance = _instance.new(name, result, cachekey, opt) + instance = _instance.new(name, result, cachekey, true, opt) cache:set(cachekey, instance) return instance end @@ -630,11 +642,23 @@ function toolchain.load_withinfo(name, info, opt) end -- save instance to the cache - instance = _instance.new(name, info, cachekey, opt) + instance = _instance.new(name, info, cachekey, false, opt) cache:set(cachekey, instance) return instance end +-- load toolchain from file +function toolchain.load_fromfile(name, filepath, opt) + local fileinfo, errors = io.load(filepath) + if not fileinfo then + return nil, errors + end + local scope_opt = {interpreter = toolchain._interpreter(), deduplicate = true, enable_filter = true} + local info = scopeinfo.new("toolchain", fileinfo, scope_opt) + return toolchain.load_withinfo(name, info, opt) +end + + -- get the program and name of the given tool kind function toolchain.tool(toolchains, toolkind, opt) diff --git a/xmake/modules/package/tools/xmake.lua b/xmake/modules/package/tools/xmake.lua index 9bfcffaff..4a1908307 100644 --- a/xmake/modules/package/tools/xmake.lua +++ b/xmake/modules/package/tools/xmake.lua @@ -138,25 +138,29 @@ function _get_package_requireinfo(packagename) end end --- get the build environments -function buildenvs(package, opt) +-- get package toolchains envs +function _get_package_toolchains_envs(package, opt) opt = opt or {} local envs = {} - -- pass toolchains local toolchains = package:config("toolchains") if toolchains then + + -- pass toolchains name and their package configurations local toolchain_packages = {} + local toolchains_custom = {} for _, name in ipairs(toolchains) do local toolchain_inst = toolchain.load(name, {plat = package:plat(), arch = package:arch()}) if toolchain_inst then table.join2(toolchain_packages, toolchain_inst:config("packages")) + if not toolchain_inst:is_builtin() then + table.insert(toolchains_custom, toolchain_inst) + end end end local rcfile_path = os.tmpfile() .. ".lua" local rcfile = io.open(rcfile_path, 'w') if #toolchain_packages > 0 then for _, packagename in ipairs(toolchain_packages) do - -- pass package configurations, {configs = {}} local requireinfo = _get_package_requireinfo(packagename) if requireinfo then requireinfo.originstr = nil @@ -171,10 +175,20 @@ function buildenvs(package, opt) envs.XMAKE_RCFILES = {} table.insert(envs.XMAKE_RCFILES, rcfile_path) table.join2(envs.XMAKE_RCFILES, os.getenv("XMAKE_RCFILES")) + + -- pass custom toolchains definition in project + for _, toolchain_inst in ipairs(toolchains_custom) do + print("pass", toolchain_inst:name()) + end end return envs end +-- get the build environments +function buildenvs(package, opt) + return _get_package_toolchains_envs(package, opt) +end + -- install package function install(package, configs, opt) diff --git a/xmake/toolchains/muslcc/xmake.lua b/xmake/toolchains/muslcc/xmake.lua index 4c8de24ac..bfb57ecc1 100644 --- a/xmake/toolchains/muslcc/xmake.lua +++ b/xmake/toolchains/muslcc/xmake.lua @@ -18,17 +18,13 @@ -- @file xmake.lua -- --- define toolchain toolchain("muslcc") - - -- set homepage set_homepage("https://musl.cc/") set_description("The musl-based cross-compilation toolchains") -- mark as cross-compilation toolchain set_kind("cross") - -- on load on_load(function (toolchain) -- load basic configuration of cross toolchain |
