summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorruki <[email protected]>2018-09-19 22:57:51 +0800
committerruki <[email protected]>2018-09-19 11:47:11 +0800
commit0fadfe24d1a42bc7a969fcaa49a3f2a1767c85d1 (patch)
treeb21b9022fd48fd2c57cac476b78bf37de5ee4970
parent4ee805906a0dcf442a80e0bb072c538622bd2a32 (diff)
improve to find_package to find prefix directory
-rw-r--r--xmake/core/package/package.lua16
-rw-r--r--xmake/core/sandbox/modules/import/lib/detect/find_package.lua122
-rw-r--r--xmake/modules/lib/detect/vcpkg.lua2
3 files changed, 127 insertions, 13 deletions
diff --git a/xmake/core/package/package.lua b/xmake/core/package/package.lua
index a506a8751..643eaf146 100644
--- a/xmake/core/package/package.lua
+++ b/xmake/core/package/package.lua
@@ -294,8 +294,10 @@ end
-- set the require info
function _instance:requireinfo_set(requireinfo)
self._REQUIREINFO = requireinfo
- -- switch to local package if exists package configuration or debug package
- if requireinfo and (requireinfo.config or requireinfo.debug) then
+ -- switch to local package if exists package configuration or debug package or limit version
+ local version = requireinfo and requireinfo.version or nil
+ local limitversion = version and version ~= "master" and version ~= "lastest"
+ if requireinfo and (requireinfo.config or requireinfo.debug or limitversion) then
self._FROMKIND = "local"
end
end
@@ -419,14 +421,12 @@ function _instance:fetch(opt)
-- import find_package
self._find_package = self._find_package or sandbox_module.import("lib.detect.find_package", {anonymous = true})
- -- TODO fetch it from the package directories first
- --[[
- local packagedir = self:directory()
- if not fetchinfo and packagedir then
+ -- fetch it from the prefix directories first
+ if not fetchinfo then
-- add cache key to make a distinction with finding system package
- fetchinfo = self._find_package(self:name(), {packagedirs = packagedir, system = false, cachekey = "package:fetch", force = opt.force})
+ fetchinfo = self._find_package(self:name(), {prefixdirs = self:prefixdir(), system = false, cachekey = "package:fetch", force = opt.force})
if fetchinfo then fetchfrom = self._FROMKIND end
- end]]
+ end
-- fetch it from the system directories
if not fetchinfo then
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 cfd683217..36ef8e1c2 100644
--- a/xmake/core/sandbox/modules/import/lib/detect/find_package.lua
+++ b/xmake/core/sandbox/modules/import/lib/detect/find_package.lua
@@ -31,14 +31,16 @@ local path = require("base/path")
local utils = require("base/utils")
local table = require("base/table")
local option = require("base/option")
+local global = require("base/global")
local config = require("project/config")
+local target = require("project/target")
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 pkg_config = import("lib.detect.pkg_config")
--- find package from package directories
+-- find package from the package directories
function sandbox_lib_detect_find_package._find_from_packagedirs(name, opt)
-- get package path (.e.g name.pkg) in the package directories
@@ -131,7 +133,116 @@ function sandbox_lib_detect_find_package._find_from_packagedirs(name, opt)
return result
end
--- find package from modules (detect.packages.find_xxx)
+-- find package from the prefix directories
+function sandbox_lib_detect_find_package._find_from_prefixdirs(name, opt)
+
+ -- get prefix directories
+ local prefixdirs = table.wrap(opt.prefixdirs)
+ if #prefixdirs == 0 then
+ table.insert(prefixdirs, path.join(global.directory(), "prefix", "release", config.get("plat") or os.host(), config.get("arch") or os.arch()))
+ table.insert(prefixdirs, path.join(config.directory(), "prefix", "release", config.get("plat") or os.host(), config.get("arch") or os.arch()))
+ end
+
+ -- find the package list file, .e.g zlib-1.2.11.txt
+ local prefixfile = find_file(name .. "-*.txt", prefixdirs, {suffixes = ".list"})
+
+ -- get the include and link directories
+ local linkdirs = table.wrap(opt.linkdirs)
+ local includedirs = table.wrap(opt.includedirs)
+ if prefixfile then
+ local prefixdir = path.directory(path.directory(prefixfile))
+ table.insert(linkdirs, path.join(prefixdir, "lib"))
+ table.insert(includedirs, path.join(prefixdir, "include"))
+ else
+ for _, prefixdir in ipairs(prefixdirs) do
+ table.insert(linkdirs, path.join(prefixdir, "lib"))
+ table.insert(includedirs, path.join(prefixdir, "include"))
+ end
+ end
+
+ -- get links and linkdirs
+ local links = {}
+ local prefixlist = prefixfile and io.load(prefixfile) or nil
+ if prefixlist then
+ local found = false
+ for _, line in ipairs(prefixlist) do
+ line = line:trim()
+ if line:endswith(".lib") or line:endswith(".a") then
+ found = true
+ for _, prefixdir in ipairs(prefixdirs) do
+ table.insert(linkdirs, path.join(prefixdir, path.directory(line)))
+ end
+ table.insert(links, target.linkname(path.filename(line)))
+ end
+ end
+ if not found then
+ for _, line in ipairs(prefixlist) do
+ line = line:trim()
+ if line:endswith(".so") or line:endswith(".dylib") then
+ found = true
+ for _, prefixdir in ipairs(prefixdirs) do
+ table.insert(linkdirs, path.join(prefixdir, path.directory(line)))
+ end
+ table.insert(links, target.linkname(path.filename(line)))
+ end
+ end
+ end
+ end
+
+ -- uses name as links directly .e.g libname.a
+ if #links == 0 then
+ links = table.wrap(name)
+ end
+
+ -- import find_path and find_library
+ local find_path = import("lib.detect.find_path")
+ local find_library = import("lib.detect.find_library")
+
+ -- find library
+ local result = nil
+ for _, link in ipairs(links) do
+ local libinfo = find_library(link, linkdirs)
+ if libinfo then
+ result = result or {}
+ result.links = table.join(result.links or {}, libinfo.link)
+ result.linkdirs = table.join(result.linkdirs or {}, libinfo.linkdir)
+ end
+ end
+ if result and result.links then
+ result.links = table.unique(result.links)
+ end
+
+ -- find includes
+ for _, include in ipairs(table.wrap(opt.includes)) do
+ local includedir = find_path(include, includedirs)
+ if includedir then
+ result = result or {}
+ result.includedirs = table.join(result.includedirs or {}, includedir)
+ end
+ end
+ for _, include in ipairs({name .. "/" .. name .. ".h", name .. ".h"}) do
+ local includedir = find_path(include, includedirs)
+ if includedir then
+ result = result or {}
+ result.includedirs = table.join(result.includedirs or {}, includedir)
+ break
+ 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
+
+ -- ok
+ return result
+end
+
+-- find package from the modules (detect.packages.find_xxx)
function sandbox_lib_detect_find_package._find_from_modules(name, opt)
-- "detect.packages.find_xxx" exists?
@@ -141,7 +252,7 @@ function sandbox_lib_detect_find_package._find_from_modules(name, opt)
end
end
--- find package from pkg-config/brew
+-- find package from the pkg-config/brew
function sandbox_lib_detect_find_package._find_from_pkg_config(name, opt)
return import("lib.detect.pkg_config").find(name, opt)
end
@@ -151,7 +262,7 @@ function sandbox_lib_detect_find_package._find_from_vcpkg(name, opt)
return import("lib.detect.vcpkg").find(name, opt)
end
--- find package from system directories
+-- find package from the system directories
function sandbox_lib_detect_find_package._find_from_systemdirs(name, opt)
-- cannot get the version of package
@@ -245,6 +356,9 @@ function sandbox_lib_detect_find_package._find(name, opt)
-- init find scripts
local findscripts = {}
+ -- find package from the prefix directories
+ table.insert(findscripts, sandbox_lib_detect_find_package._find_from_prefixdirs)
+
-- find package from the package directories
if opt.packagedirs then
table.insert(findscripts, sandbox_lib_detect_find_package._find_from_packagedirs)
diff --git a/xmake/modules/lib/detect/vcpkg.lua b/xmake/modules/lib/detect/vcpkg.lua
index 156b9f7a9..02b7ac6fe 100644
--- a/xmake/modules/lib/detect/vcpkg.lua
+++ b/xmake/modules/lib/detect/vcpkg.lua
@@ -74,7 +74,7 @@ function info(name, opt)
local infodir = path.join(installdir, "vcpkg", "info")
-- find the package info file, .e.g zlib_1.2.11-3_x86-windows.list
- local infofile = find_file(vformat("%s_*_%s-%s.list", name, arch, plat), infodir)
+ local infofile = find_file(format("%s_*_%s-%s.list", name, arch, plat), infodir)
-- save includedirs, linkdirs and links
local info = infofile and io.readfile(infofile) or nil