summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorruki <[email protected]>2018-12-01 00:42:11 +0800
committerruki <[email protected]>2018-11-30 22:13:10 +0800
commit4b5c554e53ca52e80c66f698d41f999a4664c7d4 (patch)
treeeedef660a3594d5d87df0a3fed8efd8097463ea3
parent55cebc0c6439c674088a18d3cc289f9f03d666f3 (diff)
improve find_package to get version
-rw-r--r--xmake/actions/require/list.lua8
-rw-r--r--xmake/core/sandbox/modules/import/lib/detect/find_package.lua47
-rw-r--r--xmake/modules/detect/packages/find_zlib.lua14
-rw-r--r--xmake/modules/lib/detect/pkg_config.lua28
4 files changed, 58 insertions, 39 deletions
diff --git a/xmake/actions/require/list.lua b/xmake/actions/require/list.lua
index 21f979cdb..e42b3abdd 100644
--- a/xmake/actions/require/list.lua
+++ b/xmake/actions/require/list.lua
@@ -71,12 +71,18 @@ function main()
task.run("repo", {update = true})
end
- -- list all packages
+ -- list all required packages
for _, instance in ipairs(package.load_packages(requires, {requires_extra = requires_extra})) do
cprint(" ${magenta}require${clear}(%s): %s", instance:requireinfo().originstr, _info(instance))
for _, dep in ipairs(instance:orderdeps()) do
cprint(" -> ${magenta}dep${clear}(%s): %s", dep:requireinfo().originstr, _info(dep))
end
+ local fetchinfo = instance:fetch()
+ if fetchinfo then
+ for name, info in pairs(fetchinfo) do
+ print(" -> %s: %s", name, table.concat(table.wrap(info), " "))
+ end
+ end
end
-- leave environment
diff --git a/xmake/core/sandbox/modules/import/lib/detect/find_package.lua b/xmake/core/sandbox/modules/import/lib/detect/find_package.lua
index c1c30394b..f38f3a518 100644
--- a/xmake/core/sandbox/modules/import/lib/detect/find_package.lua
+++ b/xmake/core/sandbox/modules/import/lib/detect/find_package.lua
@@ -38,6 +38,7 @@ local project = require("project/project")
local raise = require("sandbox/modules/raise")
local import = require("sandbox/modules/import")
local cache = require("sandbox/modules/import/lib/detect/cache")
+local semver = require("base/semver")
local pkg_config = import("lib.detect.pkg_config")
-- find package from the package directories
@@ -267,14 +268,8 @@ function sandbox_lib_detect_find_package._find_from_prefixdirs(name, opt)
end
end
- -- save version
- if opt.version then
- local prefixname = path.basename(prefixfile)
- result.version = prefixname:match(name .. "%-(%d+%.?%d*%.?%d*.-)")
- if not result.version then
- result.version = infoname:match(name .. "%-(%d+%.?%d*%.-)")
- end
- end
+ -- get version
+ result.version = path.basename(path.directory(prefixfile))
-- ok
return result
@@ -303,11 +298,6 @@ end
-- find package from the system directories
function sandbox_lib_detect_find_package._find_from_systemdirs(name, opt)
- -- cannot get the version of package
- if opt.version then
- return
- end
-
-- add default search includedirs on pc host
local includedirs = table.wrap(opt.includedirs)
if opt.plat == "linux" or opt.plat == "macosx" then
@@ -334,11 +324,13 @@ function sandbox_lib_detect_find_package._find_from_systemdirs(name, opt)
-- attempt to get links from pkg-config
local pkginfo = nil
+ local version = nil
local links = table.wrap(opt.links)
if #links == 0 then
pkginfo = import("lib.detect.pkg_config").info(name)
if pkginfo then
links = table.wrap(pkginfo.links)
+ version = pkginfo.version
end
end
@@ -384,6 +376,11 @@ function sandbox_lib_detect_find_package._find_from_systemdirs(name, opt)
result = {links = pkginfo.links}
end
+ -- save version
+ if result and version then
+ result.version = version
+ end
+
-- ok
return result
end
@@ -442,6 +439,16 @@ function sandbox_lib_detect_find_package._find(name, opt)
result.includedirs = table.unique(result.includedirs)
end
+ -- check valid version
+ if result and result.version then
+ local version = semver.new(result.version)
+ if version then
+ result.version = version:rawstr()
+ else
+ result.version = nil
+ end
+ end
+
-- ok?
return result
end
@@ -459,7 +466,7 @@ end
-- @code
--
-- local package = find_package("openssl")
--- local package = find_package("openssl", {version = "1.0.1"})
+-- local package = find_package("openssl", {version = "1.0.*"})
-- local package = find_package("openssl", {plat = "iphoneos"})
-- local package = find_package("openssl", {linkdirs = {"/usr/lib", "/usr/local/lib"}, includedirs = "/usr/local/include", version = "1.0.1"})
-- local package = find_package("openssl", {linkdirs = {"/usr/lib", "/usr/local/lib", links = {"ssl", "crypto"}, includes = {"ssl.h"}})
@@ -476,6 +483,9 @@ function sandbox_lib_detect_find_package.main(name, opt)
-- init cache key
local key = "find_package_" .. opt.plat .. "_" .. opt.arch
+ if opt.version then
+ key = key .. "_" .. opt.version
+ end
if opt.cachekey then
key = key .. "_" .. opt.cachekey
end
@@ -490,6 +500,13 @@ function sandbox_lib_detect_find_package.main(name, opt)
-- find package
result = sandbox_lib_detect_find_package._find(name, opt)
+ -- match version?
+ if opt.version then
+ if not result.version or not semver.satisfies(result.version, opt.version) then
+ result = nil
+ end
+ end
+
-- cache result
cacheinfo[name] = result and result or false
cache.save(key, cacheinfo)
@@ -497,7 +514,7 @@ function sandbox_lib_detect_find_package.main(name, opt)
-- trace
if opt.verbose or option.get("verbose") then
if result then
- utils.cprint("checking for the %s ... ${green}ok", name)
+ utils.cprint("checking for the %s ... ${green}%s", name, result.version and result.version or "ok")
else
utils.cprint("checking for the %s ... ${red}no", name)
end
diff --git a/xmake/modules/detect/packages/find_zlib.lua b/xmake/modules/detect/packages/find_zlib.lua
index e31065f6e..56a76f4cd 100644
--- a/xmake/modules/detect/packages/find_zlib.lua
+++ b/xmake/modules/detect/packages/find_zlib.lua
@@ -63,14 +63,12 @@ function main(opt)
-- save include directory
table.insert(result.includedirs, includedir)
- -- match version
- if opt.version then
- local zlib_h = io.readfile(path.join(includedir, "zlib.h"))
- if zlib_h then
- local version = zlib_h:match("#define ZLIB_VERSION \"(%d+%.?%d+%.?%d+)\"")
- if version ~= opt.version then
- return
- end
+ -- get version
+ local zlib_h = io.readfile(path.join(includedir, "zlib.h"))
+ if zlib_h then
+ local version = zlib_h:match("#define ZLIB_VERSION \"(%d+%.?%d+%.?%d+)\"")
+ if version then
+ result.version = version
end
end
end
diff --git a/xmake/modules/lib/detect/pkg_config.lua b/xmake/modules/lib/detect/pkg_config.lua
index 2eb675ed7..1f18e15ef 100644
--- a/xmake/modules/lib/detect/pkg_config.lua
+++ b/xmake/modules/lib/detect/pkg_config.lua
@@ -130,14 +130,10 @@ function info(name, opt)
end
-- get version
- if opt.version then
-
- -- get version
- local version = try { function() return os.iorunv(pkg_config, {"--modversion", name}) end }
- if version then
- result = result or {}
- result.version = version:trim()
- end
+ local version = try { function() return os.iorunv(pkg_config, {"--modversion", name}) end }
+ if version then
+ result = result or {}
+ result.version = version:trim()
end
-- restore PKG_CONFIG_PATH
@@ -155,7 +151,7 @@ end
-- find package
--
-- @param name the package name
--- @param opt the argument options, {plat = "", arch = "", version = "1.0.1", links = {...}}
+-- @param opt the argument options, {plat = "", arch = "", links = {...}}
--
-- @return {links = {"ssl", "crypto", "z"}, linkdirs = {""}, includedirs = {""}}
--
@@ -167,18 +163,15 @@ end
--
function find(name, opt)
+ -- init options
+ opt = opt or {}
+
-- get package info
local pkginfo = info(name, opt)
if not pkginfo then
return
end
- -- match version?
- opt = opt or {}
- if opt.version and pkginfo.version ~= opt.version then
- return
- end
-
-- get links
local links = pkginfo.links
if not links or #links == 0 then
@@ -218,6 +211,11 @@ function find(name, opt)
result.includedirs = table.join(result.includedirs or {}, pkginfo.includedirs)
end
+ -- save version
+ if result and pkginfo.version then
+ result.version = pkginfo.version
+ end
+
-- ok?
return result
end