summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorruki <[email protected]>2018-11-24 00:37:09 +0800
committerruki <[email protected]>2018-11-23 21:42:56 +0800
commitb4c0ad8d5b238e228530f147759e1282f1311e74 (patch)
treee420e98eb4b17eda0dc11c435acaa68c0edc65fc
parentaf9576c2015458afa1595c0b7fe5fd3ce659052d (diff)
improve has_package
-rw-r--r--xmake/actions/require/install.lua2
-rw-r--r--xmake/core/project/project.lua52
-rw-r--r--xmake/core/project/requireinfo.lua17
-rw-r--r--xmake/core/project/target.lua24
4 files changed, 64 insertions, 31 deletions
diff --git a/xmake/actions/require/install.lua b/xmake/actions/require/install.lua
index 941a43fc2..c9ddb0974 100644
--- a/xmake/actions/require/install.lua
+++ b/xmake/actions/require/install.lua
@@ -54,7 +54,7 @@ function _register_required_package(instance, requireinfo)
end
-- save this package version
- requireinfo:set("version", instance:version_str())
+ requireinfo:version_set(instance:version_str())
-- enable this require info
requireinfo:enable(true)
diff --git a/xmake/core/project/project.lua b/xmake/core/project/project.lua
index 5178315e8..502a5b6cf 100644
--- a/xmake/core/project/project.lua
+++ b/xmake/core/project/project.lua
@@ -109,6 +109,20 @@ function project._api_has_config(interp, ...)
return config.has(...)
end
+-- some packages are enabled?
+function project._api_has_package(interp, ...)
+ -- only for loading targets
+ local requires = project._REQUIRES
+ if requires then
+ for _, name in ipairs(table.join(...)) do
+ local pkg = requires[name]
+ if pkg and pkg:enabled() then
+ return true
+ end
+ end
+ end
+end
+
-- set config from the given name
function project._api_set_config(interp, name, value)
if not config.readonly(name) then
@@ -219,6 +233,7 @@ function project.interpreter()
, {"get_config", project._api_get_config }
-- has_xxx
, {"has_config", project._api_has_config }
+ , {"has_package", project._api_has_package }
-- add_xxx
, {"add_moduledirs", project._api_add_moduledirs }
, {"add_plugindirs", project._api_add_plugindirs }
@@ -361,6 +376,9 @@ end
-- load targets
function project._load_targets()
+ -- load all requires first (ensure has_package() works for targets)
+ local requires = project.requires()
+
-- load targets
local results, errors = project._load_scope("target", true, true)
if not results then
@@ -376,7 +394,7 @@ function project._load_targets()
end
end
- -- load and attach target deps and rules
+ -- load and attach target deps, rules and packages
for _, t in pairs(targets) do
-- load deps
@@ -409,6 +427,15 @@ function project._load_targets()
table.insert(t._ORDERULES, r)
end
end
+
+ -- laod packages
+ t._PACKAGES = t._PACKAGES or {}
+ for _, packagename in ipairs(table.wrap(t:get("packages"))) do
+ local p = requires[packagename]
+ if p then
+ table.insert(t._PACKAGES, p)
+ end
+ end
end
-- enter toolchains environment
@@ -490,10 +517,6 @@ function project._load_requires()
-- get the package name
local packagename = requirestr:split('%s+')[1]
- -- init a require info instance
- local instance = table.inherit(requireinfo)
- assert(instance)
-
-- get alias
local alias = nil
local extrainfo = requires_extra[requirestr]
@@ -501,9 +524,22 @@ function project._load_requires()
alias = extrainfo.alias
end
- -- save name and info
- instance._NAME = packagename
- instance._INFO = { requirestr = requirestr, extrainfo = extrainfo }
+ -- load it from cache first
+ local instance = requireinfo.load(alias or packagename)
+ if not instance then
+
+ -- init a require info instance
+ instance = table.inherit(requireinfo)
+
+ -- save name and info
+ instance._NAME = packagename
+ instance._INFO = { __requirestr = requirestr, __extrainfo = extrainfo }
+ end
+
+ -- need not links? remove it
+ if instance:extra("nolink") then
+ instance:set("links", nil)
+ end
-- add require info
requires[alias or packagename] = instance
diff --git a/xmake/core/project/requireinfo.lua b/xmake/core/project/requireinfo.lua
index ac5b397ce..e5aa12b6a 100644
--- a/xmake/core/project/requireinfo.lua
+++ b/xmake/core/project/requireinfo.lua
@@ -32,6 +32,7 @@ local path = require("base/path")
local table = require("base/table")
local utils = require("base/utils")
local cache = require("project/cache")
+local config = require("project/config")
local semver = require("base/semver")
local sandbox = require("sandbox/sandbox")
@@ -101,7 +102,7 @@ function requireinfo:version()
-- get version
local version = nil
- local verstr = self:get("version")
+ local verstr = self:get("__version")
if verstr then
version = semver.new(verstr)
end
@@ -113,9 +114,15 @@ function requireinfo:version()
return version
end
+-- set the package version
+function requireinfo:version_set(version)
+ self._VERSION = nil
+ self:set("__version", version)
+end
+
-- get the require string
function requireinfo:requirestr()
- return self:get("requirestr")
+ return self:get("__requirestr")
end
-- get the extra info from the given name
@@ -128,7 +135,7 @@ end
-- get the extra info
function requireinfo:extrainfo()
- return self:get("extrainfo")
+ return self:get("__extrainfo")
end
-- set the value to the requires info
@@ -161,7 +168,7 @@ end
-- this require info is enabled?
function requireinfo:enabled()
- return self:get("enabled")
+ return self:get("__enabled")
end
-- enable or disable this require info
@@ -169,7 +176,7 @@ end
-- @param enabled enable it?
--
function requireinfo:enable(enabled)
- self:set("enabled", enabled)
+ self:set("__enabled", enabled)
end
-- load the requires info from the cache
diff --git a/xmake/core/project/target.lua b/xmake/core/project/target.lua
index a24427d4b..986aaf56c 100644
--- a/xmake/core/project/target.lua
+++ b/xmake/core/project/target.lua
@@ -531,26 +531,16 @@ end
-- get the required packages
function target:packages()
-
- -- attempt to get it from cache first
- if self._PACKAGES then
- return self._PACKAGES
- end
-
- -- load packages if be enabled
- self._PACKAGES = {}
- for _, name in ipairs(table.wrap(self:get("packages"))) do
- local package = requireinfo.load(name)
- if package and package:enabled() then
- if package:extra("nolink") then
- package:set("links", nil)
+ if not self._PACKAGES_ENABLED then
+ local packages = {}
+ for _, pkg in ipairs(self._PACKAGES) do
+ if pkg:enabled() then
+ table.insert(packages, pkg)
end
- table.insert(self._PACKAGES, package)
end
+ self._PACKAGES_ENABLED = packages
end
-
- -- get it
- return self._PACKAGES
+ return self._PACKAGES_ENABLED
end
-- get the object files directory