Skip to content

Commit 530cf15

Browse files
committed
limit some calls in package on_load
1 parent 29b96db commit 530cf15

File tree

3 files changed

+44
-18
lines changed

3 files changed

+44
-18
lines changed

xmake/core/package/package.lua

+30-1
Original file line numberDiff line numberDiff line change
@@ -1026,10 +1026,14 @@ function _instance:_load()
10261026
if on_load then
10271027
on_load(self)
10281028
end
1029-
self._LOADED = true
10301029
end
10311030
end
10321031

1032+
-- mark as loaded package
1033+
function _instance:_mark_as_loaded()
1034+
self._LOADED = true
1035+
end
1036+
10331037
-- get the raw environments
10341038
function _instance:_rawenvs()
10351039
local envs = self._RAWENVS
@@ -1214,6 +1218,14 @@ function _instance:has_runtime(...)
12141218
end
12151219
end
12161220

1221+
-- check call limits in on_load
1222+
-- @see https://github.com/xmake-io/xmake/issues/5455
1223+
function _instance:_check_limits_on_load(apiname)
1224+
if not self._LOADED then
1225+
os.raise("we cannot call package:%s() in on_load(), please call it in on_check/on_install/on_test.", apiname)
1226+
end
1227+
end
1228+
12171229
-- get the given toolchain
12181230
function _instance:toolchain(name)
12191231
local toolchains_map = self:_memcache():get("toolchains_map")
@@ -1259,6 +1271,7 @@ end
12591271

12601272
-- get the program and name of the given tool kind
12611273
function _instance:tool(toolkind)
1274+
self:_check_limits_on_load("tool")
12621275
if self:toolchains() then
12631276
local cachekey = "package_" .. tostring(self)
12641277
return toolchain.tool(self:toolchains(), toolkind, {cachekey = cachekey, plat = self:plat(), arch = self:arch()})
@@ -1269,6 +1282,7 @@ end
12691282

12701283
-- get tool configuration from the toolchains
12711284
function _instance:toolconfig(name)
1285+
self:_check_limits_on_load("toolconfig")
12721286
if self:toolchains() then
12731287
local cachekey = "package_" .. tostring(self)
12741288
return toolchain.toolconfig(self:toolchains(), name, {cachekey = cachekey, plat = self:plat(), arch = self:arch()})
@@ -1302,6 +1316,7 @@ end
13021316
-- ...
13031317
-- end
13041318
function _instance:has_tool(toolkind, ...)
1319+
self:_check_limits_on_load("has_tool")
13051320
local _, toolname = self:tool(toolkind)
13061321
if toolname then
13071322
for _, v in ipairs(table.join(...)) do
@@ -2432,6 +2447,7 @@ end
24322447
-- @return true or false, errors
24332448
--
24342449
function _instance:has_cfuncs(funcs, opt)
2450+
self:_check_limits_on_load("has_cfuncs")
24352451
opt = opt or {}
24362452
opt.target = self
24372453
opt.configs = self:_generate_build_configs(opt.configs, {sourcekind = "cc"})
@@ -2446,6 +2462,7 @@ end
24462462
-- @return true or false, errors
24472463
--
24482464
function _instance:has_cxxfuncs(funcs, opt)
2465+
self:_check_limits_on_load("has_cxxfuncs")
24492466
opt = opt or {}
24502467
opt.target = self
24512468
opt.configs = self:_generate_build_configs(opt.configs, {sourcekind = "cxx"})
@@ -2460,6 +2477,7 @@ end
24602477
-- @return true or false, errors
24612478
--
24622479
function _instance:has_ctypes(types, opt)
2480+
self:_check_limits_on_load("has_ctypes")
24632481
opt = opt or {}
24642482
opt.target = self
24652483
opt.configs = self:_generate_build_configs(opt.configs, {sourcekind = "cc"})
@@ -2474,6 +2492,7 @@ end
24742492
-- @return true or false, errors
24752493
--
24762494
function _instance:has_cxxtypes(types, opt)
2495+
self:_check_limits_on_load("has_cxxtypes")
24772496
opt = opt or {}
24782497
opt.target = self
24792498
opt.configs = self:_generate_build_configs(opt.configs, {sourcekind = "cxx"})
@@ -2488,6 +2507,7 @@ end
24882507
-- @return true or false, errors
24892508
--
24902509
function _instance:has_cincludes(includes, opt)
2510+
self:_check_limits_on_load("has_cincludes")
24912511
opt = opt or {}
24922512
opt.target = self
24932513
opt.configs = self:_generate_build_configs(opt.configs, {sourcekind = "cc"})
@@ -2502,6 +2522,7 @@ end
25022522
-- @return true or false, errors
25032523
--
25042524
function _instance:has_cxxincludes(includes, opt)
2525+
self:_check_limits_on_load("has_cxxincludes")
25052526
opt = opt or {}
25062527
opt.target = self
25072528
opt.configs = self:_generate_build_configs(opt.configs, {sourcekind = "cxx"})
@@ -2516,6 +2537,7 @@ end
25162537
-- @return true or false, errors
25172538
--
25182539
function _instance:has_cflags(flags, opt)
2540+
self:_check_limits_on_load("has_cflags")
25192541
local compinst = self:compiler("cc")
25202542
return compinst:has_flags(flags, "cflags", opt)
25212543
end
@@ -2528,6 +2550,7 @@ end
25282550
-- @return true or false, errors
25292551
--
25302552
function _instance:has_cxxflags(flags, opt)
2553+
self:_check_limits_on_load("has_cxxflags")
25312554
local compinst = self:compiler("cxx")
25322555
return compinst:has_flags(flags, "cxxflags", opt)
25332556
end
@@ -2540,6 +2563,7 @@ end
25402563
-- @return true or false, errors
25412564
--
25422565
function _instance:has_features(features, opt)
2566+
self:_check_limits_on_load("has_features")
25432567
opt = opt or {}
25442568
opt.target = self
25452569
return sandbox_module.import("core.tool.compiler", {anonymous = true}).has_features(features, opt)
@@ -2553,6 +2577,7 @@ end
25532577
-- @return the type size
25542578
--
25552579
function _instance:check_sizeof(typename, opt)
2580+
self:_check_limits_on_load("check_sizeof")
25562581
opt = opt or {}
25572582
opt.target = self
25582583
return sandbox_module.import("lib.detect.check_sizeof", {anonymous = true})(typename, opt)
@@ -2566,6 +2591,7 @@ end
25662591
-- @return true or false, errors
25672592
--
25682593
function _instance:check_csnippets(snippets, opt)
2594+
self:_check_limits_on_load("check_csnippets")
25692595
opt = opt or {}
25702596
opt.target = self
25712597
opt.configs = self:_generate_build_configs(opt.configs, {sourcekind = "cc"})
@@ -2580,6 +2606,7 @@ end
25802606
-- @return true or false, errors
25812607
--
25822608
function _instance:check_cxxsnippets(snippets, opt)
2609+
self:_check_limits_on_load("check_cxxsnippets")
25832610
opt = opt or {}
25842611
opt.target = self
25852612
opt.configs = self:_generate_build_configs(opt.configs, {sourcekind = "cxx"})
@@ -2594,6 +2621,7 @@ end
25942621
-- @return true or false, errors
25952622
--
25962623
function _instance:check_msnippets(snippets, opt)
2624+
self:_check_limits_on_load("check_msnippets")
25972625
opt = opt or {}
25982626
opt.target = self
25992627
opt.configs = self:_generate_build_configs(opt.configs, {sourcekind = "mm"})
@@ -2608,6 +2636,7 @@ end
26082636
-- @return true or false, errors
26092637
--
26102638
function _instance:check_mxxsnippets(snippets, opt)
2639+
self:_check_limits_on_load("check_mxxsnippets")
26112640
opt = opt or {}
26122641
opt.target = self
26132642
opt.configs = self:_generate_build_configs(opt.configs, {sourcekind = "mxx"})

xmake/modules/private/action/require/impl/actions/install.lua

+11-3
Original file line numberDiff line numberDiff line change
@@ -225,9 +225,17 @@ end
225225

226226
-- check package toolchains
227227
function _check_package_toolchains(package)
228-
for _, toolchain_inst in pairs(package:toolchains()) do
229-
if not toolchain_inst:check() then
230-
raise("toolchain(\"%s\"): not found!", toolchain_inst:name())
228+
if package:toolchains() then
229+
for _, toolchain_inst in pairs(package:toolchains()) do
230+
if not toolchain_inst:check() then
231+
raise("toolchain(\"%s\"): not found!", toolchain_inst:name())
232+
end
233+
end
234+
else
235+
-- maybe this package is host package, it's platform and toolchain has been not checked yet.
236+
local platform_inst = platform.load(package:plat(), package:arch())
237+
if not platform_inst:check() then
238+
raise("no any matched platform for this package(%s)!", package:name())
231239
end
232240
end
233241
end

xmake/modules/private/action/require/impl/package.lua

+3-14
Original file line numberDiff line numberDiff line change
@@ -852,17 +852,6 @@ function _select_package_runtimes(package)
852852
end
853853
end
854854

855-
-- check platform toolchains, package maybe use host platform, it's toolchain has been not checked yet.
856-
-- @see https://github.com/xmake-io/xmake/issues/5455
857-
function _check_package_platform_toolchains(package)
858-
if not package:toolchains() then
859-
local platform_inst = platform.load(package:plat(), package:arch())
860-
if not platform_inst:check() then
861-
raise("no any matched platform for this package(%s)!", package:name())
862-
end
863-
end
864-
end
865-
866855
-- load required packages
867856
function _load_package(packagename, requireinfo, opt)
868857

@@ -1015,9 +1004,6 @@ function _load_package(packagename, requireinfo, opt)
10151004
end
10161005
end
10171006

1018-
-- check package platform toolchains first, package:has_tool will be called in on_load
1019-
_check_package_platform_toolchains(package)
1020-
10211007
-- do load
10221008
package:_load()
10231009

@@ -1031,6 +1017,9 @@ function _load_package(packagename, requireinfo, opt)
10311017

10321018
-- save this package package to cache
10331019
_memcache():set2("packages", packagekey, package)
1020+
1021+
-- load ok
1022+
package:_mark_as_loaded()
10341023
return package
10351024
end
10361025

0 commit comments

Comments
 (0)