Skip to content

Commit 9589296

Browse files
committed
fix load tool cache
1 parent c54be37 commit 9589296

File tree

3 files changed

+22
-28
lines changed

3 files changed

+22
-28
lines changed

xmake/core/tool/compiler.lua

Lines changed: 17 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -122,17 +122,18 @@ function compiler.load(sourcekind, target)
122122
return nil, "unknown source kind!"
123123
end
124124

125-
-- load compiler tool first (with cache)
126-
local compiler_tool, program_or_errors = compiler._load_tool(sourcekind, target)
127-
if not compiler_tool then
128-
return nil, program_or_errors
129-
end
130-
131125
-- init cache key
132126
-- @note we need plat/arch,
133127
-- because it is possible for the compiler to do cross-compilation with the -target parameter
134-
local plat = compiler_tool:plat() or config.plat() or os.host()
135-
local arch = compiler_tool:arch() or config.arch() or os.arch()
128+
local plat = config.plat() or os.host()
129+
local arch = config.arch() or os.arch()
130+
if target and target.tool then
131+
local _, _, toolchain_info = target:tool(sourcekind)
132+
if toolchain_info then
133+
plat = toolchain_info.plat
134+
arch = toolchain_info.arch
135+
end
136+
end
136137
local cachekey = sourcekind .. (program_or_errors or "") .. plat .. arch
137138
if target then
138139
cachekey = cachekey .. tostring(target)
@@ -143,6 +144,13 @@ function compiler.load(sourcekind, target)
143144
local instance = compiler._INSTANCES[cachekey]
144145
if not instance then
145146
instance = table.inherit(compiler, builder)
147+
148+
-- load compiler tool
149+
-- @NOTE We cannot cache the tool, otherwise it may cause duplicate toolchain flags to be added
150+
local compiler_tool, program_or_errors = compiler._load_tool(sourcekind, target)
151+
if not compiler_tool then
152+
return nil, program_or_errors
153+
end
146154
instance._TOOL = compiler_tool
147155

148156
-- load the compiler language from the source kind
@@ -188,7 +196,7 @@ function compiler.load(sourcekind, target)
188196

189197
-- we need to load it at the end because in tool.load().
190198
-- because we may need to call has_flags, which requires the full platform toolchain flags
191-
local ok, errors = compiler_tool:_load_once()
199+
local ok, errors = instance:_tool():_load_once()
192200
if not ok then
193201
return nil, errors
194202
end

xmake/core/tool/linker.lua

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -130,7 +130,8 @@ function linker.load(targetkind, sourcekinds, target)
130130
end
131131
end
132132

133-
-- load linker tool first (with cache)
133+
-- load linker tool first, we need to get the correct plat/arch
134+
-- @NOTE We cannot cache the tool, otherwise it may cause duplicate toolchain flags to be added
134135
local linkertool, linkerinfo_or_errors = linker._load_tool(targetkind, sourcekinds, target)
135136
if not linkertool then
136137
return nil, linkerinfo_or_errors
@@ -218,7 +219,7 @@ function linker.load(targetkind, sourcekinds, target)
218219

219220
-- we need to load it at the end because in tool.load().
220221
-- because we may need to call has_flags, which requires the full platform toolchain flags
221-
local ok, errors = linkertool:_load_once()
222+
local ok, errors = instance:_tool():_load_once()
222223
if not ok then
223224
return nil, errors
224225
end

xmake/core/tool/tool.lua

Lines changed: 2 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -241,22 +241,8 @@ function tool.load(kind, opt)
241241
local toolchain_info = opt.toolchain_info or {}
242242

243243
-- get platform and architecture
244-
local plat = toolchain_info.plat or config.get("plat") or os.host()
245-
local arch = toolchain_info.arch or config.get("arch") or os.arch()
246-
247-
-- init cachekey
248-
local cachekey = kind .. (program or "") .. plat .. arch .. (opt.host and "host" or "")
249-
if toolchain_info and toolchain_info.cachekey then
250-
-- it maybe contains target key
251-
-- @see https://github.com/xmake-io/xmake/issues/6672
252-
cachekey = cachekey .. toolchain_info.cachekey
253-
end
254-
255-
-- get it directly from cache dirst
256-
tool._TOOLS = tool._TOOLS or {}
257-
if tool._TOOLS[cachekey] then
258-
return tool._TOOLS[cachekey]
259-
end
244+
local plat = toolchain_info.plat or config.plat() or os.host()
245+
local arch = toolchain_info.arch or config.arch() or os.arch()
260246

261247
-- contain toolname? parse it, e.g. '[email protected]'
262248
if program then
@@ -310,7 +296,6 @@ function tool.load(kind, opt)
310296
if not instance then
311297
return nil, errors
312298
end
313-
tool._TOOLS[cachekey] = instance
314299
return instance
315300
end
316301

0 commit comments

Comments
 (0)