diff options
| author | ruki <[email protected]> | 2021-02-24 22:53:59 +0800 |
|---|---|---|
| committer | GitHub <[email protected]> | 2021-02-24 22:53:59 +0800 |
| commit | 47b2606b26d9280f2144b4b9c39daecd6155a59b (patch) | |
| tree | e473f2edb561578f23bed9fd8c8ed4c6a98243e9 | |
| parent | 538aa613ae3414c11f0e979246ff4959f75b135b (diff) | |
| parent | 755d6cb62335f9b3a3cecf0830138886e9510f8a (diff) | |
Merge pull request #1253 from xmake-io/package
Support to export packages/options
| -rwxr-xr-x | tests/projects/package/basic/src/main.c | 5 | ||||
| -rw-r--r-- | tests/projects/package/basic/src/test.c | 10 | ||||
| -rw-r--r-- | tests/projects/package/basic/xmake.lua | 10 | ||||
| -rw-r--r-- | xmake/core/package/package.lua | 6 | ||||
| -rw-r--r-- | xmake/core/project/project.lua | 9 | ||||
| -rw-r--r-- | xmake/core/project/target.lua | 117 | ||||
| -rw-r--r-- | xmake/core/tool/builder.lua | 7 | ||||
| -rw-r--r-- | xmake/modules/private/action/require/impl/package.lua | 10 | ||||
| -rw-r--r-- | xmake/rules/utils/inherit_links/inherit_links.lua | 2 |
9 files changed, 111 insertions, 65 deletions
diff --git a/tests/projects/package/basic/src/main.c b/tests/projects/package/basic/src/main.c index 9ae580511..740b34694 100755 --- a/tests/projects/package/basic/src/main.c +++ b/tests/projects/package/basic/src/main.c @@ -1,7 +1,12 @@ #include <stdio.h> +#include <pcre2.h> + +int test(); int main(int argc, char** argv) { printf("hello world!\n"); + pcre2_compile(0, 0, 0, 0, 0, 0); + test(); return 0; } diff --git a/tests/projects/package/basic/src/test.c b/tests/projects/package/basic/src/test.c new file mode 100644 index 000000000..e711dd197 --- /dev/null +++ b/tests/projects/package/basic/src/test.c @@ -0,0 +1,10 @@ +#include <stdio.h> +#include <pcre2.h> + +int test() +{ + printf("hello world!\n"); + pcre2_compile(0, 0, 0, 0, 0, 0); + return 0; +} + diff --git a/tests/projects/package/basic/xmake.lua b/tests/projects/package/basic/xmake.lua index f06d2a199..0cf08600c 100644 --- a/tests/projects/package/basic/xmake.lua +++ b/tests/projects/package/basic/xmake.lua @@ -4,8 +4,14 @@ add_requires("pcre2", {system = false, optional = true}) add_rules("mode.debug", "mode.release") +target("test") + set_kind("static") + add_files("src/test.c") + add_packages("pcre2", {public = true}) + target("console") set_kind("binary") - add_files("src/*.c") - add_packages("tbox", "zlib", "pcre2") + add_deps("test") + add_files("src/main.c") + add_packages("tbox", "zlib") diff --git a/xmake/core/package/package.lua b/xmake/core/package/package.lua index ebb8c795c..d5dc5a3b2 100644 --- a/xmake/core/package/package.lua +++ b/xmake/core/package/package.lua @@ -838,9 +838,9 @@ function _instance:buildhash() end end if not sourcehashs:empty() then - for _, sourcehash in sourcehashs:keys() do - str = str .. "_" .. sourcehash - end + local hashs = sourcehashs:to_array() + table.sort(hashs) + str = str .. "_" .. table.concat(hashs, "_") end end return hash.uuid4(str):gsub('-', ''):lower() diff --git a/xmake/core/project/project.lua b/xmake/core/project/project.lua index ebdf5d2e0..ba97f973b 100644 --- a/xmake/core/project/project.lua +++ b/xmake/core/project/project.lua @@ -360,15 +360,6 @@ function project._load_target(t, requires) return false, errors end - -- load 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 - -- load toolchains local toolchains = t:get("toolchains") if toolchains then diff --git a/xmake/core/project/target.lua b/xmake/core/project/target.lua index 6f6a38494..4b14c8a13 100644 --- a/xmake/core/project/target.lua +++ b/xmake/core/project/target.lua @@ -30,6 +30,7 @@ local utils = require("base/utils") local table = require("base/table") local baseoption = require("base/option") local deprecated = require("base/deprecated") +local memcache = require("cache/memcache") local rule = require("project/rule") local option = require("project/option") local config = require("project/config") @@ -47,14 +48,24 @@ local sandbox_module = require("sandbox/modules/import/core/sandbox/module") -- new a target instance function _instance.new(name, info, project) - local instance = table.inherit(_instance) - instance._NAME = name - instance._INFO = info - instance._PROJECT = project - instance._CACHEID = 1 + local instance = table.inherit(_instance) + instance._NAME = name + instance._INFO = info + instance._PROJECT = project + instance._CACHEID = 1 return instance end +-- get memcache +function _instance:_memcache() + local cache = self._MEMCACHE + if not cache then + cache = memcache.cache("core.project.target." .. self:name()) + self._MEMCACHE = cache + end + return cache +end + -- load rule, move cache to target function _instance:_load_rule(ruleinst, suffix) @@ -321,24 +332,26 @@ function _instance:get_from_deps(name, opt) local depinherit = self:extraconf("deps", dep:name(), "inherit") if depinherit == nil or depinherit then table.join2(values, dep:get(name, opt)) + table.join2(values, dep:get_from_opts(name, opt)) + table.join2(values, dep:get_from_pkgs(name, opt)) end end return values end --- get values from target options -function _instance:get_from_opts(name) +-- get values from target options with {interface|public = ...} +function _instance:get_from_opts(name, opt) local values = {} - for _, opt in ipairs(self:orderopts()) do - table.join2(values, table.wrap(opt:get(name))) + for _, opt_ in ipairs(self:orderopts(opt)) do + table.join2(values, table.wrap(opt_:get(name))) end return values end --- get values from target packages -function _instance:get_from_pkgs(name) +-- get values from target packages with {interface|public = ...} +function _instance:get_from_pkgs(name, opt) local values = {} - for _, pkg in ipairs(self:orderpkgs()) do + for _, pkg in ipairs(self:orderpkgs(opt)) do -- uses them instead of the builtin configs if exists extra package config -- e.g. `add_packages("xxx", {links = "xxx"})` local configinfo = self:pkgconfig(pkg:name()) @@ -659,37 +672,41 @@ function _instance:opts() return self._OPTS_ENABLED end --- get the enabled ordered options -function _instance:orderopts() - - -- attempt to get it from cache first - if self._ORDEROPTS_ENABLED then - return self._ORDEROPTS_ENABLED +-- get the enabled ordered options with {public|interface = ...} +function _instance:orderopts(opt) + opt = opt or {} + local cachekey = "orderopts" + if opt.public then + cachekey = cachekey .. "_public" + elseif opt.interface then + cachekey = cachekey .. "_interface" end + local orderopts = self:_memcache():get(cachekey) + if not orderopts then - -- load options if be enabled - self._ORDEROPTS_ENABLED = {} - for _, name in ipairs(table.wrap(self:get("options"))) do - local opt = nil - if config.get(name) then opt = option.load(name) end - if opt then - table.insert(self._ORDEROPTS_ENABLED, opt) + -- load options if be enabled + orderopts = {} + for _, name in ipairs(table.wrap(self:get("options"))) do + local opt_ = nil + if config.get(name) then opt_ = option.load(name) end + if opt_ then + table.insert(orderopts, opt_) + end end - end - -- load options from packages if no require info, be compatible with the option package in (*.pkg) - for _, name in ipairs(table.wrap(self:get("packages"))) do - if not project_package.load(name) then - local opt = nil - if config.get(name) then opt = option.load(name) end - if opt then - table.insert(self._ORDEROPTS_ENABLED, opt) + -- load options from packages if no require info, be compatible with the option package in (*.pkg) + for _, name in ipairs(table.wrap(self:get("packages"))) do + if not project_package.load(name) then + local opt_ = nil + if config.get(name) then opt_ = option.load(name) end + if opt_ then + table.insert(orderopts, opt_) + end end end + self:_memcache():set(cachekey, orderopts) end - - -- get it - return self._ORDEROPTS_ENABLED + return orderopts end -- get the enabled package @@ -713,20 +730,30 @@ function _instance:pkgs() return self._PKGS_ENABLED end --- get the required packages -function _instance:orderpkgs() - if not self._ORDERPKGS_ENABLED then - local packages = {} - if self._PACKAGES then - for _, pkg in ipairs(self._PACKAGES) do - if pkg:enabled() then +-- get the required packages with {interface|public = ..} +function _instance:orderpkgs(opt) + opt = opt or {} + local cachekey = "orderpkgs" + if opt.public then + cachekey = cachekey .. "_public" + elseif opt.interface then + cachekey = cachekey .. "_interface" + end + local packages = self:_memcache():get(cachekey) + if not packages then + packages = {} + local requires = self._PROJECT.required_packages() + if requires then + for _, packagename in ipairs(table.wrap(self:get("packages", opt))) do + local pkg = requires[packagename] + if pkg and pkg:enabled() then table.insert(packages, pkg) end end end - self._ORDERPKGS_ENABLED = packages + self:_memcache():set(cachekey, packages) end - return self._ORDERPKGS_ENABLED + return packages end -- get the environments of packages diff --git a/xmake/core/tool/builder.lua b/xmake/core/tool/builder.lua index 96bac57e6..04349dbf2 100644 --- a/xmake/core/tool/builder.lua +++ b/xmake/core/tool/builder.lua @@ -280,7 +280,7 @@ function builder:_add_flags_from_language(flags, target, getters) local results = {} if target:type() == "target" then - -- get flagvalues (public or interface) of all dependent targets + -- get flagvalues (public or interface) of all dependent targets (contain packages/options) table.join2(results, target:get_from_deps(name, {interface = true})) -- get flagvalues of target with given flagname @@ -372,9 +372,10 @@ function builder:_preprocess_flags(flags) local flags_new = {} for idx = count, 1, -1 do local flag = flags[idx] - if flag and not unique[flag] then + local flagkey = type(flag) == "table" and table.concat(flag, "") or flag + if flag and not unique[flagkey] then table.insert(flags_new, flag) - unique[flag] = true + unique[flagkey] = true end end flags = flags_new diff --git a/xmake/modules/private/action/require/impl/package.lua b/xmake/modules/private/action/require/impl/package.lua index 9d2c87bc4..3095e10a9 100644 --- a/xmake/modules/private/action/require/impl/package.lua +++ b/xmake/modules/private/action/require/impl/package.lua @@ -196,9 +196,13 @@ end -- function _sort_packagedeps(package) local orderdeps = {} - for _, dep in pairs(package:deps()) do - table.join2(orderdeps, _sort_packagedeps(dep)) - table.insert(orderdeps, dep) + local deps = package:deps() + if deps then + for _, depname in ipairs(table.orderkeys(deps)) do + local dep = deps[depname] + table.join2(orderdeps, _sort_packagedeps(dep)) + table.insert(orderdeps, dep) + end end return orderdeps end diff --git a/xmake/rules/utils/inherit_links/inherit_links.lua b/xmake/rules/utils/inherit_links/inherit_links.lua index 83d36d1d2..65e8bdce1 100644 --- a/xmake/rules/utils/inherit_links/inherit_links.lua +++ b/xmake/rules/utils/inherit_links/inherit_links.lua @@ -51,6 +51,8 @@ function main(target) -- we need add includedirs to support import modules for golang target:add("includedirs", path.directory(targetfile), {interface = true}) end + + -- we export all links and linkdirs in self/packages/options to the parent target by default for _, name in ipairs({"frameworkdirs", "frameworks", "linkdirs", "links", "syslinks"}) do local values = _get_values_from_target(target, name) if values and #values > 0 then |
