diff --git a/xmake/actions/config/main.lua b/xmake/actions/config/main.lua index 4e6e7fce0a7..6749cd14d7b 100644 --- a/xmake/actions/config/main.lua +++ b/xmake/actions/config/main.lua @@ -195,10 +195,8 @@ end function _find_default_mode() local mode = config.mode() if not mode then - local allowedmodes = table.wrap(project.get("allowedmodes")) - if #allowedmodes > 0 then - mode = allowedmodes[1] - end + local _, defaultmode = project.allowed_modes() + mode = defaultmode if not mode then mode = "release" end @@ -211,31 +209,22 @@ end function _check_configs() -- check allowed modes local mode = config.mode() - local allowedmodes = table.wrap(project.get("allowedmodes")) - if #allowedmodes > 0 then - local allowedmodes_set = hashset.from(allowedmodes) - if not allowedmodes_set:has(mode) then - local allowedmodes_str = table.concat(allowedmodes, ", ") - raise("`%s` is not a valid complation mode for this project, please use one of %s", mode, allowedmodes_str) + local allowed_modes = project.allowed_modes() + if allowed_modes then + if not allowed_modes:has(mode) then + local allowed_modes_str = table.concat(allowed_modes:to_array(), ", ") + raise("`%s` is not a valid complation mode for this project, please use one of %s", mode, allowed_modes_str) end end -- check allowed archs local plat = config.plat() local arch = config.arch() - local allowedarchs = table.wrap(project.get("allowedarchs")) - if #allowedarchs > 0 then - local allowedarchs_current = {} - for _, allowedarch in ipairs(allowedarchs) do - local p = project.extraconf("allowedarchs", allowedarch, "plat") - if p == plat then - table.insert(allowedarchs_current, allowedarch) - end - end - local allowedarchs_set = hashset.from(allowedarchs_current) - if not allowedarchs_set:has(arch) then - local allowedarchs_str = table.concat(allowedarchs_current, ", ") - raise("`%s` is not a valid complation arch for this project, please use one of %s", arch, allowedarchs_str) + local allowed_archs = project.allowed_archs() + if allowed_archs then + if not allowed_archs:has(arch) then + local allowed_archs_str = table.concat(allowed_archs:to_array(), ", ") + raise("`%s` is not a valid complation arch for this project, please use one of %s", arch, allowed_archs_str) end end end diff --git a/xmake/core/project/project.lua b/xmake/core/project/project.lua index ea66f2bb6eb..7cc5dd0bc3b 100644 --- a/xmake/core/project/project.lua +++ b/xmake/core/project/project.lua @@ -30,6 +30,7 @@ local utils = require("base/utils") local table = require("base/table") local global = require("base/global") local process = require("base/process") +local hashset = require("base/hashset") local deprecated = require("base/deprecated") local interpreter = require("base/interpreter") local memcache = require("cache/memcache") @@ -1082,20 +1083,6 @@ function project.mtimes() return project.interpreter():mtimes() end --- get the project modes -function project.modes() - local modes = project.get("allowedmodes") or {} - for _, target in pairs(table.wrap(project.targets())) do - for _, rule in ipairs(target:orderules()) do - local name = rule:name() - if name:startswith("mode.") then - table.insert(modes, name:sub(6)) - end - end - end - return table.unique(modes) -end - -- get the project menu function project.menu() @@ -1212,5 +1199,97 @@ function project.tmpfile(opt_or_key) return path.join(project.tmpdir(opt), "_" .. (hash.uuid4(key):gsub("-", ""))) end +-- get all modes +function project.modes() + local modes = project.get("allowed_modes") or {} + for _, target in pairs(table.wrap(project.targets())) do + for _, rule in ipairs(target:orderules()) do + local name = rule:name() + if name:startswith("mode.") then + table.insert(modes, name:sub(6)) + end + end + end + return table.unique(modes) +end + +-- get allowed modes +-- +-- add_allowedmodes("releasedbg", {default = true}) +-- add_allowedmodes("debug") +-- +function project.allowed_modes() + local allowed_modes_set = project._ALLOWED_MODES + if not allowed_modes_set then + local allowed_modes = table.wrap(project.get("allowedmodes")) + if #allowed_modes > 0 then + allowed_modes_set = hashset.from(allowed_modes) + end + project._ALLOWED_MODES = allowed_modes_set or false + end + local default_mode = project._DEFAULT_MODE + if not default_mode then + local allowed_modes = table.wrap(project.get("allowedmodes")) + for _, allowed_mode in ipairs(allowed_modes) do + if project.extraconf("allowedmodes", allowed_mode, "default") then + default_mode = allowed_mode + break + end + end + -- we use the first mode as default value if no default configuration + if not default_mode then + default_mode = allowed_modes[1] + end + project._DEFAULT_MODE = default_mode + end + return allowed_modes_set or nil, default_mode or nil +end + +-- get allowed archs +-- +-- add_allowedarchs("arm64", "x86_64", {plat = "macosx"}) +-- add_allowedarchs("i386", {plat = "linux"}) +-- +function project.allowed_archs(plat) + local allowed_archs_set = project._ALLOWED_ARCHS + if not allowed_archs_set then + local allowed_archs = table.wrap(project.get("allowedarchs")) + if #allowed_archs > 0 then + allowed_archs_set = hashset.new() + for _, allowed_arch in ipairs(allowed_archs) do + if not plat or project.extraconf("allowedarchs", allowed_arch, "plat") == plat then + allowed_archs_set:insert(allowed_arch) + end + end + end + project._ALLOWED_ARCHS = allowed_archs_set or false + end + local default_arch = project._DEFAULT_ARCH + if not default_arch then + if allowed_archs_set then + for _, allowed_arch in allowed_archs_set:keys() do + if project.extraconf("allowedarchs", allowed_arch, "default") then + default_arch = allowed_arch + break + end + end + end + -- we use the first arch as default value if no default configuration + if not default_arch then + local allowed_archs = table.wrap(project.get("allowedarchs")) + if #allowed_archs > 0 then + for _, allowed_arch in ipairs(allowed_archs) do + if not plat or project.extraconf("allowedarchs", allowed_arch, "plat") == plat then + default_arch = allowed_arch + break + end + end + end + end + project._DEFAULT_ARCH = default_arch + end + return allowed_archs_set or nil, default_arch or nil +end + -- return module: project return project diff --git a/xmake/core/sandbox/modules/import/core/project/project.lua b/xmake/core/sandbox/modules/import/core/project/project.lua index dcd78b205c7..2d9d7e46311 100644 --- a/xmake/core/sandbox/modules/import/core/project/project.lua +++ b/xmake/core/sandbox/modules/import/core/project/project.lua @@ -53,6 +53,8 @@ sandbox_core_project.rcfiles = project.rcfiles sandbox_core_project.directory = project.directory sandbox_core_project.name = project.name sandbox_core_project.modes = project.modes +sandbox_core_project.allowed_modes = project.allowed_modes +sandbox_core_project.allowed_archs = project.allowed_archs sandbox_core_project.mtimes = project.mtimes sandbox_core_project.version = project.version sandbox_core_project.required_package = project.required_package