Skip to content

Commit c74b86b

Browse files
authored
Merge pull request #6784 from xmake-io/opti
Continue to optimize building targets speed
2 parents 9ac44c1 + 9589296 commit c74b86b

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

54 files changed

+54
-88
lines changed

tests/apis/custom_toolchain/xmake/modules/core/tools/cl6x/has_flags.lua

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -60,7 +60,6 @@ function _check_from_arglist(flags, opt, islinker)
6060
end
6161
end
6262
detectcache:set2(key, flagskey, allflags)
63-
detectcache:save()
6463
end
6564
local flag = flags[1]
6665
return allflags[flag]

tests/benchmarks/build_targets/test.lua

Lines changed: 20 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -7,10 +7,13 @@ function test_build(t)
77
return
88
end
99

10+
local jobs = tostring(os.default_njob())
11+
1012
-- xmake
1113
os.tryrm("build")
14+
os.tryrm(".xmake")
1215
local xmake_dt = os.mclock()
13-
os.run("xmake")
16+
os.runv("xmake", {"-j" .. jobs})
1417
xmake_dt = os.mclock() - xmake_dt
1518
print("build targets/30: xmake: %d ms", xmake_dt)
1619

@@ -21,10 +24,10 @@ function test_build(t)
2124
os.mkdir("build")
2225
local cmake_default_dt = os.mclock()
2326
os.runv(cmake.program, {".."}, {curdir = "build"})
24-
os.runv(cmake.program, {"--build", "."}, {curdir = "build"})
27+
os.runv(cmake.program, {"--build", ".", "-j" .. jobs}, {curdir = "build"})
2528
cmake_default_dt = os.mclock() - cmake_default_dt
2629
print("build targets/30: cmake/default: %d ms", cmake_default_dt)
27-
--t:require((cmake_default_dt > xmake_dt) or (cmake_default_dt + 2000 > xmake_dt))
30+
t:require((cmake_default_dt > xmake_dt) or (cmake_default_dt + 2000 > xmake_dt))
2831

2932
local ninja = find_tool("ninja")
3033
if ninja then
@@ -43,11 +46,10 @@ function test_build(t)
4346
end
4447
local cmake_ninja_dt = os.mclock()
4548
os.runv(cmake.program, table.join("..", "-G", "Ninja", configs), {curdir = "build", envs = envs})
46-
os.runv(cmake.program, {"--build", "."}, {curdir = "build", envs = envs})
49+
os.runv(cmake.program, {"--build", ".", "-j" .. jobs}, {curdir = "build", envs = envs})
4750
cmake_ninja_dt = os.mclock() - cmake_ninja_dt
4851
print("build targets/30: cmake/ninja: %d ms", cmake_ninja_dt)
49-
-- TODO we need to improve it
50-
--t:require((cmake_ninja_dt > xmake_dt) or (cmake_ninja_dt + 2000 > xmake_dt))
52+
t:require((cmake_ninja_dt > xmake_dt) or (cmake_ninja_dt + 2000 > xmake_dt))
5153
end
5254

5355
local make = find_tool("make")
@@ -56,7 +58,7 @@ function test_build(t)
5658
os.mkdir("build")
5759
local cmake_makefile_dt = os.mclock()
5860
os.runv(cmake.program, {"..", "-G", "Unix Makefiles"}, {curdir = "build"})
59-
os.runv(cmake.program, {"--build", "."}, {curdir = "build"})
61+
os.runv(cmake.program, {"--build", ".", "-j" .. jobs}, {curdir = "build"})
6062
cmake_makefile_dt = os.mclock() - cmake_makefile_dt
6163
print("build targets/30: cmake/makefile: %d ms", cmake_makefile_dt)
6264
t:require((cmake_makefile_dt > xmake_dt) or (cmake_makefile_dt + 2000 > xmake_dt))
@@ -67,12 +69,19 @@ function test_build(t)
6769
local meson = find_tool("meson")
6870
if meson then
6971
os.tryrm("build")
70-
local meson_dt = os.mclock()
72+
local meson_setup_dt = os.mclock()
7173
os.runv(meson.program, {"setup", "build"})
72-
os.runv(meson.program, {"compile", "-C", "build"})
73-
meson_dt = os.mclock() - meson_dt
74+
meson_setup_dt = os.mclock() - meson_setup_dt
75+
76+
-- ccache will cache object files globally, which may affect the results of the second run.
77+
io.replace("build/build.ninja", "ccache", "")
78+
79+
local meson_build_dt = os.mclock()
80+
os.runv(meson.program, {"compile", "-j", jobs, "-C", "build"})
81+
meson_build_dt = os.mclock() - meson_build_dt
82+
local meson_dt = meson_setup_dt + meson_build_dt
7483
print("build targets/30: meson: %d ms", meson_dt)
75-
--t:require((meson_dt > xmake_dt) or (meson_dt + 2000 > xmake_dt))
84+
t:require((meson_dt > xmake_dt) or (meson_dt + 2000 > xmake_dt))
7685
end
7786
end
7887

tests/benchmarks/config_targets/test.lua

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ function test_config(t)
99

1010
-- xmake
1111
os.tryrm("build")
12+
os.tryrm(".xmake")
1213
local xmake_dt = os.mclock()
1314
os.runv("xmake", {"config", "-c"})
1415
xmake_dt = os.mclock() - xmake_dt

xmake/actions/build/main.lua

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@ import("core.base.option")
2323
import("core.base.global")
2424
import("core.base.task")
2525
import("core.tool.toolchain")
26+
import("core.cache.detectcache")
2627
import("core.project.rule")
2728
import("core.project.config")
2829
import("core.project.project")
@@ -130,6 +131,9 @@ function build_targets(targetnames, opt)
130131
-- save toolchain configs
131132
toolchain.save()
132133

134+
-- save detect cache
135+
detectcache:save()
136+
133137
-- dump cache stats
134138
if option.get("diagnosis") then
135139
build_cache.dump_stats()
@@ -148,6 +152,9 @@ function build_targets(targetnames, opt)
148152
-- save toolchain configs
149153
toolchain.save()
150154

155+
-- save detect cache
156+
detectcache:save()
157+
151158
-- raise
152159
if errors then
153160
raise(errors)

xmake/actions/config/main.lua

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@ import("core.project.project")
2828
import("core.platform.platform")
2929
import("private.detect.find_platform")
3030
import("core.cache.localcache")
31+
import("core.cache.detectcache")
3132
import("scangen")
3233
import("menuconf", {alias = "menuconf_show"})
3334
import("configfiles", {alias = "generate_configfiles"})
@@ -448,6 +449,9 @@ force to build in current directory via run `xmake -P .`]], os.projectdir())
448449
-- save toolchain cache
449450
toolchain.save()
450451

452+
-- save detect cache
453+
detectcache:save()
454+
451455
-- unlock the whole project
452456
project.unlock()
453457
end

xmake/core/sandbox/modules/import/lib/detect/find_program.lua

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -338,7 +338,6 @@ function sandbox_lib_detect_find_program.main(name, opt)
338338

339339
-- cache result
340340
detectcache:set2(cachekey, name, result and result or false)
341-
detectcache:save()
342341

343342
-- trace
344343
if option.get("verbose") or opt.verbose then

xmake/core/sandbox/modules/import/lib/detect/find_programver.lua

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -109,7 +109,6 @@ function sandbox_lib_detect_find_programver.main(program, opt)
109109

110110
-- save result
111111
detectcache:set2(cachekey, program, result and result or false)
112-
detectcache:save()
113112
scheduler.co_unlock(lockname)
114113
return result
115114
end

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)