summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorruki <[email protected]>2019-03-14 23:19:41 +0800
committerruki <[email protected]>2019-03-14 13:58:10 +0800
commit98673a8aaf92c8cf5c8bddeb49f1c02a69952ac1 (patch)
treea7307436a09cd093a84c81c1038e944fe4f6b0dd
parentfbf5ca5b9f93713dd2d7017674aebcdd31aaa2aa (diff)
improve package.configs
-rw-r--r--xmake/actions/require/impl/package.lua47
-rw-r--r--xmake/actions/require/info.lua6
-rw-r--r--xmake/core/package/package.lua22
3 files changed, 69 insertions, 6 deletions
diff --git a/xmake/actions/require/impl/package.lua b/xmake/actions/require/impl/package.lua
index b7945d5d4..8e9aabe1b 100644
--- a/xmake/actions/require/impl/package.lua
+++ b/xmake/actions/require/impl/package.lua
@@ -294,6 +294,50 @@ function _sort_packages_urls(packages)
end
end
+-- check the configurations of packages
+--
+-- package("pcre2")
+-- add_configs("bitwidth", {description = "Set the code unit width.", default = "8", values = {"8", "16", "32"}})
+-- add_configs("bitwidth", {type = "number", values = {8, 16, 32}})
+-- add_configs("bitwidth", {constraint = function(value) if tonumber(value) < 100 then return true end})
+--
+function _check_packages_configs(packages)
+ for _, package in ipairs(packages) do
+ local configs_defined = {}
+ for _, name in ipairs(package:get("configs")) do
+ configs_defined[name] = package:extraconf("configs", name) or {}
+ end
+ for name, value in pairs(package:configs()) do
+ local conf = configs_defined[name]
+ if conf then
+ local config_type = conf.type or "string"
+ if type(value) ~= config_type then
+ raise("package(%s %s): invalid type(%s) for config(%s), need type(%s)!", package:name(), package:version_str(), type(value), name, config_type)
+ end
+ if conf.values then
+ local found = false
+ for _, config_value in ipairs(conf.values) do
+ if tostring(value) == tostring(config_value) then
+ found = true
+ break
+ end
+ end
+ if not found then
+ raise("package(%s %s): invalid value(%s) for config(%s), please run `xmake require --info %s` to get all valid values!", package:name(), package:version_str(), value, name, package:name())
+ end
+ end
+ if conf.constraint then
+ if not conf.constraint(value) then
+ raise("package(%s %s): invalid value(%s) for config(%s)!", package:name(), package:version_str(), value, name)
+ end
+ end
+ else
+ raise("package(%s %s): invalid config(%s), please run `xmake require --info %s` to get all configurations!", package:name(), package:version_str(), name, package:name())
+ end
+ end
+ end
+end
+
-- select packages version
function _select_packages_version(packages)
@@ -442,6 +486,9 @@ function install_packages(requires, opt)
-- load packages
local packages = load_packages(requires, opt)
+ -- check the configurations of packages
+ _check_packages_configs(packages)
+
-- fetch packages (with system) from local first
if not option.get("force") then
process.runjobs(function (index)
diff --git a/xmake/actions/require/info.lua b/xmake/actions/require/info.lua
index 75c171559..6cf7223e0 100644
--- a/xmake/actions/require/info.lua
+++ b/xmake/actions/require/info.lua
@@ -168,10 +168,10 @@ function main(package_names)
end
-- show configs
- local configs = instance:get("configs")
- if configs then
+ local configs_defined = instance:get("configs")
+ if configs_defined then
cprint(" -> ${magenta}configs${clear}:")
- for _, conf in ipairs(configs) do
+ for _, conf in ipairs(configs_defined) do
cprint(" -> ${cyan}%s${clear}:", conf)
for name, value in pairs(instance:extraconf("configs", conf)) do
if type(value) == "table" then
diff --git a/xmake/core/package/package.lua b/xmake/core/package/package.lua
index e3a6579d6..b81536443 100644
--- a/xmake/core/package/package.lua
+++ b/xmake/core/package/package.lua
@@ -433,10 +433,26 @@ end
-- get the configurations of package
function _instance:configs()
- local requireinfo = self:requireinfo()
- if requireinfo then
- return requireinfo.configs
+ local configs = self._CONFIGS
+ if configs == nil then
+ local configs_defined = self:get("configs")
+ if configs_defined then
+ configs = {}
+ local requireinfo = self:requireinfo()
+ local configs_required = requireinfo and requireinfo.configs or {}
+ for _, name in ipairs(table.wrap(configs_defined)) do
+ local value = configs_required[name]
+ if value == nil then
+ value = self:extraconf("configs", name, "default")
+ end
+ configs[name] = value
+ end
+ else
+ configs = false
+ end
+ self._CONFIGS = configs
end
+ return configs and configs or nil
end
-- get the build hash