diff --git a/xmake/core/tool/toolchain.lua b/xmake/core/tool/toolchain.lua index 87a28cb693..b230bf6742 100644 --- a/xmake/core/tool/toolchain.lua +++ b/xmake/core/tool/toolchain.lua @@ -278,6 +278,11 @@ function _instance:check() return checkok end +-- do load manually, it will call on_load() +function _instance:load() + self:_load() +end + -- check cross toolchain function _instance:check_cross_toolchain() return sandbox_module.import("toolchains.cross.check", {rootdir = os.programdir(), anonymous = true})(self) @@ -311,7 +316,15 @@ end -- save toolchain to file function _instance:savefile(filepath) - return io.save(filepath, {name = self:name(), info = self:info():info(), cachekey = self:cachekey(), configs = self._CONFIGS}) + if not self:_is_loaded() then + os.raise("we can only save toolchain(%s) after it has been loaded!", self:name()) + end + -- we strip on_load/on_check scripts to solve some issues + -- @see https://github.com/xmake-io/xmake/issues/3774 + local info = table.clone(self:info():info()) + info.load = nil + info.check = nil + return io.save(filepath, {name = self:name(), info = info, cachekey = self:cachekey(), configs = self._CONFIGS}) end -- on check (builtin) @@ -349,6 +362,11 @@ function _instance:_load() end end +-- is loaded? +function _instance:_is_loaded() + return self:info():get("__loaded") +end + -- get the tool description from the tool kind function _instance:_description(toolkind) local descriptions = self._DESCRIPTIONS diff --git a/xmake/modules/package/tools/xmake.lua b/xmake/modules/package/tools/xmake.lua index 0018128efa..e4a501b306 100644 --- a/xmake/modules/package/tools/xmake.lua +++ b/xmake/modules/package/tools/xmake.lua @@ -241,14 +241,19 @@ function _get_package_toolchains_envs(package, opt) -- pass custom toolchains definition in project for _, toolchain_inst in ipairs(toolchains_custom) do - local toolchains_file = os.tmpfile() - dprint("passing toolchain(%s) to %s", toolchain_inst:name(), toolchains_file) - local ok, errors = toolchain_inst:savefile(toolchains_file) - if not ok then - raise("save toolchain failed, %s", errors or "unknown") + -- we must load it first + -- @see https://github.com/xmake-io/xmake/issues/3774 + if toolchain_inst:check() then + toolchain_inst:load() + local toolchains_file = os.tmpfile() + dprint("passing toolchain(%s) to %s", toolchain_inst:name(), toolchains_file) + local ok, errors = toolchain_inst:savefile(toolchains_file) + if not ok then + raise("save toolchain failed, %s", errors or "unknown") + end + envs.XMAKE_TOOLCHAIN_DATAFILES = envs.XMAKE_TOOLCHAIN_DATAFILES or {} + table.insert(envs.XMAKE_TOOLCHAIN_DATAFILES, toolchains_file) end - envs.XMAKE_TOOLCHAIN_DATAFILES = envs.XMAKE_TOOLCHAIN_DATAFILES or {} - table.insert(envs.XMAKE_TOOLCHAIN_DATAFILES, toolchains_file) end end return envs