summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorruki <[email protected]>2017-06-12 22:50:31 +0800
committerruki <[email protected]>2017-06-12 22:50:31 +0800
commitfe5c0e9a6b99ae6ad3ab7ab24d50018fe1c8442d (patch)
treeae69d92bb776bd3956ec006f9569709f9867c1ce
parentb176145e2efa26ca36b166d7e838afcc207050a4 (diff)
simplify find_library
-rw-r--r--xmake/core/project/target.lua22
-rw-r--r--xmake/core/sandbox/modules/import/lib/detect/find_library.lua27
-rw-r--r--xmake/core/sandbox/modules/import/lib/detect/find_package.lua58
-rw-r--r--xmake/modules/lib/detect/pkg_config.lua52
4 files changed, 96 insertions, 63 deletions
diff --git a/xmake/core/project/target.lua b/xmake/core/project/target.lua
index bfb986123..008cddc91 100644
--- a/xmake/core/project/target.lua
+++ b/xmake/core/project/target.lua
@@ -66,26 +66,28 @@ function target.new(name, info)
end
-- get the target info
-function target:get(infoname)
-
- -- check
- assert(self and self._INFO and infoname)
+function target:get(name)
+ return self._INFO[name]
+end
- -- get it
- return self._INFO[infoname]
+-- add the value to the target info
+function target:add(name_or_info, ...)
+ if type(name_or_info) == "string" then
+ self._INFO[name_or_info] = table.unique(table.join2(table.wrap(self._INFO[name_or_info]), ...))
+ elseif type(name_or_info) == "table" and #name_or_info == 0 then
+ for name, info in pairs(name_or_info) do
+ self:add(name, info)
+ end
+ end
end
-- get the target name
function target:name()
-
- -- get it
return self._NAME
end
-- get the base name of target file
function target:basename()
-
- -- get it
return self:get("basename")
end
diff --git a/xmake/core/sandbox/modules/import/lib/detect/find_library.lua b/xmake/core/sandbox/modules/import/lib/detect/find_library.lua
index 9ac08777d..ded3a191d 100644
--- a/xmake/core/sandbox/modules/import/lib/detect/find_library.lua
+++ b/xmake/core/sandbox/modules/import/lib/detect/find_library.lua
@@ -31,11 +31,9 @@ local path = require("base/path")
local utils = require("base/utils")
local table = require("base/table")
local target = require("project/target")
-local config = require("project/config")
local raise = require("sandbox/modules/raise")
local import = require("sandbox/modules/import")
local find_file = import("lib.detect.find_file")
-local pkg_config = import("lib.detect.pkg_config")
-- get link name from the file name
function sandbox_lib_detect_find_library._link(filename)
@@ -67,7 +65,7 @@ end
--
-- @endcode
--
-function sandbox_lib_detect_find_library.main(names, paths, kinds)
+function sandbox_lib_detect_find_library.main(names, pathes, kinds)
-- init pathes
pathes = table.wrap(pathes)
@@ -75,29 +73,6 @@ function sandbox_lib_detect_find_library.main(names, paths, kinds)
-- init kinds
kinds = kinds or {"static", "shared"}
- -- get current platform
- local plat = config.get("plat") or os.host()
-
- -- get current architecture
- local arch = config.get("arch") or os.arch()
-
- -- attempt to add search pathes from pkg-config
- for _, name in ipairs(table.wrap(names)) do
- local pkginfo = pkg_config.find(name)
- if not pkginfo and not name:startswith("lib") then
- pkginfo = pkg_config.find("lib" .. name)
- end
- if pkginfo and pkginfo.linkdirs then
- table.join2(pathes, pkginfo.linkdirs)
- end
- end
-
- -- add default search pathes on pc host
- if plat == "macosx" or (plat == "linux" and arch == os.arch()) then
- table.insert(pathes, "/usr/local/lib")
- table.insert(pathes, "/usr/lib")
- end
-
-- find library file from the given pathes
for _, name in ipairs(table.wrap(names)) do
for _, kind in ipairs(table.wrap(kinds)) do
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 438e13ba5..0c19ac4e0 100644
--- a/xmake/core/sandbox/modules/import/lib/detect/find_package.lua
+++ b/xmake/core/sandbox/modules/import/lib/detect/find_package.lua
@@ -32,6 +32,7 @@ local utils = require("base/utils")
local table = require("base/table")
local option = require("base/option")
local cache = require("project/cache")
+local config = require("project/config")
local project = require("project/project")
local raise = require("sandbox/modules/raise")
local import = require("sandbox/modules/import")
@@ -57,27 +58,41 @@ function sandbox_lib_detect_find_package._find_from_modules(name, opt)
end
end
--- find package from system
-function sandbox_lib_detect_find_package._find_from_system(name, opt)
+-- find package from pkg-config
+function sandbox_lib_detect_find_package._find_from_pkg_config(name, opt)
+ return pkg_config.find(name, {version = true})
+end
+
+-- find package from system directories
+function sandbox_lib_detect_find_package._find_from_systemdirs(name, opt)
- -- init links and pathes
- local links = opt.links or {}
- local pathes = opt.pathes or {}
+ -- get current platform
+ local plat = config.get("plat") or os.host()
- -- attempt to add search pathes from pkg-config
- local pkginfo = pkg_config.find(name, {version = true})
- if not pkginfo and not name:startswith("lib") then
- pkginfo = pkg_config.find("lib" .. name, {version = true})
+ -- get current architecture
+ local arch = config.get("arch") or os.arch()
+
+ -- add default search pathes on pc host
+ local pathes = {}
+ if plat == "macosx" or (plat == "linux" and arch == os.arch()) then
+ table.insert(pathes, "/usr/local/lib")
+ table.insert(pathes, "/usr/lib")
+ table.insert(pathes, "/opt/local/lib")
+ table.insert(pathes, "/opt/lib")
end
- if pkginfo then
- table.join2(links, pkginfo.links)
- table.join2(pathes, pkginfo.linkdirs)
- table.join2(pathes, pkginfo.includedirs)
+
+ -- attempt to get links from pkg-config
+ local links = opt.links
+ if not links then
+ local pkginfo = pkg_config.info(name)
+ if pkginfo then
+ links = pkginfo.links
+ end
end
-- find library
local result = nil
- for _, link in ipairs(links) do
+ for _, link in ipairs(table.wrap(links)) do
local libinfo = find_library(link, pathes)
if libinfo then
result = result or {}
@@ -85,14 +100,10 @@ function sandbox_lib_detect_find_package._find_from_system(name, opt)
result.linkdirs = table.join(result.linkdirs or {}, libinfo.linkdir)
end
end
- if result and result.linkdirs then
- result.linkdirs = table.unique(result.linkdirs)
- end
- -- save version and includedirs if exists
- if pkginfo and result then
- result.version = pkginfo.version
- result.includedirs = table.join(result.includedirs or {}, pkginfo.includedirs)
+ -- found?
+ if result and result.links then
+ result.linkdirs = table.unique(result.linkdirs)
end
-- ok
@@ -107,7 +118,8 @@ function sandbox_lib_detect_find_package._find(name, opt)
{
sandbox_lib_detect_find_package._find_from_repositories
, sandbox_lib_detect_find_package._find_from_modules
- , sandbox_lib_detect_find_package._find_from_system
+ , sandbox_lib_detect_find_package._find_from_pkg_config
+ , sandbox_lib_detect_find_package._find_from_systemdirs
}
-- find it, TODO match version
@@ -155,7 +167,7 @@ function sandbox_lib_detect_find_package.main(name, opt)
cacheinfo[name] = utils.ifelse(result, result, false)
-- save cache info
- detectcache:set("find_program", cacheinfo)
+ detectcache:set("find_package", cacheinfo)
detectcache:flush()
-- trace
diff --git a/xmake/modules/lib/detect/pkg_config.lua b/xmake/modules/lib/detect/pkg_config.lua
index 37fee3018..880db92a8 100644
--- a/xmake/modules/lib/detect/pkg_config.lua
+++ b/xmake/modules/lib/detect/pkg_config.lua
@@ -23,9 +23,10 @@
--
-- imports
+import("lib.detect.find_library")
import("detect.tool.find_pkg_config")
--- find package
+-- get package info
--
-- @param name the package name
-- @param opt the argument options, {version = true}
@@ -34,11 +35,11 @@ import("detect.tool.find_pkg_config")
--
-- @code
--
--- local pkginfo = pkg_config.find("openssl")
+-- local pkginfo = pkg_config.info("openssl")
--
-- @endcode
--
-function find(name, opt)
+function info(name, opt)
-- attempt to add search pathes from pkg-config
local pkg_config = find_pkg_config()
@@ -80,7 +81,7 @@ function find(name, opt)
end
end
- -- find version
+ -- get version
if opt and opt.version then
-- get version
@@ -94,3 +95,46 @@ function find(name, opt)
-- ok?
return result
end
+
+-- find package
+--
+-- @param name the package name
+-- @param opt the argument options, {version = true}
+--
+-- @return {links = {"ssl", "crypto", "z"}, linkdirs = {""}, includedirs = {""}, version = ""}
+--
+-- @code
+--
+-- local pkginfo = pkg_config.find("openssl")
+--
+-- @endcode
+--
+function find(name, opt)
+
+ -- get package info
+ local pkginfo = info(name, opt)
+ if not pkginfo then
+ return
+ end
+
+ -- find library
+ local result = nil
+ for _, link in ipairs(table.wrap(pkginfo.links)) do
+ local libinfo = find_library(link, pkginfo.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
+
+ -- found?
+ if result and result.links then
+ result.version = pkginfo.version
+ result.linkdirs = table.unique(result.linkdirs)
+ result.includedirs = table.join(result.includedirs or {}, pkginfo.includedirs)
+ end
+
+ -- ok?
+ return result
+end