summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorruki <[email protected]>2019-01-23 21:05:20 +0800
committerruki <[email protected]>2019-01-23 00:32:05 +0800
commit7d9fe778c235da802039bf46c62cd4142f4719e4 (patch)
tree4b2239aa013f24d8fc7fbd403c3e80245604363a
parentde29dc88bc3b7d39d4206d6e2bd034d15b232ca2 (diff)
rewrite find_package for brew, pkgconfig and system
-rw-r--r--xmake/modules/lib/detect/pkg_config.lua74
-rw-r--r--xmake/modules/package/manager/brew/find_package.lua58
-rw-r--r--xmake/modules/package/manager/find_package.lua18
-rw-r--r--xmake/modules/package/manager/pkg_config/find_package.lua35
-rw-r--r--xmake/modules/package/manager/system/find_package.lua123
5 files changed, 234 insertions, 74 deletions
diff --git a/xmake/modules/lib/detect/pkg_config.lua b/xmake/modules/lib/detect/pkg_config.lua
index 969e6436d..79d772b84 100644
--- a/xmake/modules/lib/detect/pkg_config.lua
+++ b/xmake/modules/lib/detect/pkg_config.lua
@@ -52,48 +52,14 @@ function info(name, opt)
return
end
- -- init options and cache
- opt = opt or {}
- _g._INFO = _g._INFO or {}
-
- -- get it from cache first
- local result = _g._INFO[name]
- if result ~= nil then
- return result and result or nil
- end
+ -- init options
+ opt = opt or {}
- -- add PKG_CONFIG_PATH
+ -- init PKG_CONFIG_PATH
local configdirs_old = os.getenv("PKG_CONFIG_PATH")
- local configdirs = opt.configdirs or {}
+ local configdirs = table.wrap(opt.configdirs)
if #configdirs > 0 then
- os.addenv("PKG_CONFIG_PATH", unpack(configdirs))
- end
-
- -- find other mode package? clear the other pkg-config pathes
- if opt.mode and opt.mode ~= "release" then
- os.setenv("PKG_CONFIG_PATH", nil)
- end
-
- -- attempt to get pkg-config path from `brew --prefix` if no flags
- local brewprefix = nil
- if not flags then
-
- -- find the config directories from the prefix directories of xmake
- local platsubdirs = path.join(config.get("plat") or os.host(), config.get("arch") or os.arch())
- os.addenv("PKG_CONFIG_PATH", path.join(config.directory(), "prefix", platsubdirs, opt.mode or "release", "lib", "pkgconfig"))
- os.addenv("PKG_CONFIG_PATH", path.join(global.directory(), "prefix", platsubdirs, opt.mode or "release", "lib", "pkgconfig"))
-
- -- find the prefix directory of brew directly, because `brew --prefix name` is too slow!
- if not opt.mode or opt.mode == "release" then
- local brew_pkg_root = try { function () return os.iorunv(brew, {"--prefix"}) end } or "/usr/local"
- brew_pkg_root = path.join(brew_pkg_root, opt.plat == "macosx" and "Cellar" or "opt")
-
- local pcfile = find_file(name .. ".pc", path.join(brew_pkg_root, (opt.brewhint or name), "*/lib/pkgconfig"))
- if pcfile then
- brewprefix = path.directory(path.directory(path.directory(pcfile)))
- os.addenv("PKG_CONFIG_PATH", path.directory(pcfile))
- end
- end
+ os.setenv("PKG_CONFIG_PATH", unpack(configdirs))
end
-- get libs and cflags
@@ -127,14 +93,6 @@ function info(name, opt)
table.insert(result.includedirs, includedir)
end
end
- elseif brewprefix then
- local links = {}
- for _, file in ipairs(os.files(path.join(brewprefix, "lib", "*.a"))) do
- table.insert(links, target.linkname(path.filename(file)))
- end
- if #links > 0 then
- result = {links = links, linkdirs = {path.join(brewprefix, "lib")}, includedirs = {path.join(brewprefix, "include")}}
- end
end
-- get version
@@ -149,9 +107,6 @@ function info(name, opt)
os.setenv("PKG_CONFIG_PATH", configdirs_old)
end
- -- save result to cache
- _g._INFO[name] = result and result or false
-
-- ok?
return result
end
@@ -186,25 +141,10 @@ function find(name, opt)
links = opt.links
end
- -- add default search linkdirs on pc host
- local linkdirs = pkginfo.linkdirs
- if links and #links > 0 and (not linkdirs or #linkdirs == 0) then
- linkdirs = linkdirs or {}
- table.insert(linkdirs, "/usr/local/lib")
- table.insert(linkdirs, "/usr/lib")
- table.insert(linkdirs, "/opt/local/lib")
- table.insert(linkdirs, "/opt/lib")
- if opt.plat == "linux" and opt.arch == "x86_64" then
- table.insert(linkdirs, "/usr/local/lib/x86_64-linux-gnu")
- table.insert(linkdirs, "/usr/lib/x86_64-linux-gnu")
- table.insert(linkdirs, "/usr/lib64")
- table.insert(linkdirs, "/opt/lib64")
- end
- end
-
-- find library
local result = nil
- for _, link in ipairs(table.wrap(links)) do
+ local linkdirs = table.wrap(pkginfo.linkdirs)
+ for _, link in ipairs(links) do
local libinfo = find_library(link, linkdirs)
if libinfo then
result = result or {}
diff --git a/xmake/modules/package/manager/brew/find_package.lua b/xmake/modules/package/manager/brew/find_package.lua
new file mode 100644
index 000000000..988c162fa
--- /dev/null
+++ b/xmake/modules/package/manager/brew/find_package.lua
@@ -0,0 +1,58 @@
+--!A cross-platform build utility based on Lua
+--
+-- Licensed to the Apache Software Foundation (ASF) under one
+-- or more contributor license agreements. See the NOTICE file
+-- distributed with this work for additional information
+-- regarding copyright ownership. The ASF licenses this file
+-- to you under the Apache License, Version 2.0 (the
+-- "License"); you may not use this file except in compliance
+-- with the License. You may obtain a copy of the License at
+--
+-- http://www.apache.org/licenses/LICENSE-2.0
+--
+-- Unless required by applicable law or agreed to in writing, software
+-- distributed under the License is distributed on an "AS IS" BASIS,
+-- WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+-- See the License for the specific language governing permissions and
+-- limitations under the License.
+--
+-- Copyright (C) 2015 - 2019, TBOOX Open Source Group.
+--
+-- @author ruki
+-- @file find_package.lua
+--
+
+-- imports
+import("lib.detect.find_tool")
+import("lib.detect.find_file")
+import("lib.detect.pkg_config")
+
+-- find package from the brew package manager
+--
+-- @param name the package name, e.g. zlib, pcre/libpcre16
+-- @param opt the options, .e.g {verbose = true, version = "1.12.x")
+--
+function main(name, opt)
+
+ -- find brew
+ local brew = find_tool("brew")
+ if not brew then
+ return
+ end
+
+ -- parse name, .e.g pcre/libpcre16
+ local nameinfo = name:split('/')
+ local pcname = nameinfo[2] or nameinfo[1]
+
+ -- find the prefix directory of brew
+ local brew_pkg_root = try { function () return os.iorunv(brew.program, {"--prefix"}) end } or "/usr/local"
+ brew_pkg_root = path.join(brew_pkg_root:trim(), opt.plat == "macosx" and "Cellar" or "opt")
+ local pcfile = find_file(pcname .. ".pc", path.join(brew_pkg_root, nameinfo[1], "*/lib/pkgconfig"))
+ if not pcfile then
+ return
+ end
+
+ -- do find
+ opt.configdirs = path.directory(pcfile)
+ return pkg_config.find(pcname, opt)
+end
diff --git a/xmake/modules/package/manager/find_package.lua b/xmake/modules/package/manager/find_package.lua
index 4d6cc5299..41c342095 100644
--- a/xmake/modules/package/manager/find_package.lua
+++ b/xmake/modules/package/manager/find_package.lua
@@ -52,7 +52,7 @@ function _find_package(manager_name, package_name, opt)
if opt.system ~= false then
-- find it from homebrew
- if not is_host("windows") then
+ if not is_host("windows") and (opt.mode == nil or opt.mode == "release") then
table.insert(managers, "brew")
end
@@ -61,15 +61,18 @@ function _find_package(manager_name, package_name, opt)
-- find it from conan
table.insert(managers, "conan")
+
+ -- only support the current host platform and architecture
+ if opt.plat == os.host() and opt.arch == os.arch() and (opt.mode == nil or opt.mode == "release") then
- -- find it from pkg-config
- table.insert(managers, "pkg-config")
+ -- find it from pkg-config
+ table.insert(managers, "pkg_config")
- -- find it from system
- table.insert(managers, "system")
+ -- find it from system
+ table.insert(managers, "system")
+ end
end
end
- assert(#managers > 0, "no suitable package manager!")
-- find package from the given package manager
local result = nil
@@ -105,7 +108,8 @@ end
-- find package using the package manager
--
--- @param name the package name, e.g. zlib 1.12.x (try all), XMAKE::zlib 1.12.x, BREW::zlib, VCPKG::zlib, CONAN::OpenSSL/1.0.2n@conan/stable
+-- @param name the package name
+-- e.g. zlib 1.12.x (try all), xmake::zlib 1.12.x, brew::zlib, brew::pcre/libpcre16, vcpkg::zlib, conan::OpenSSL/1.0.2n@conan/stable
-- @param opt the options
-- e.g. { verbose = false, force = false, plat = "iphoneos", arch = "arm64", mode = "debug", version = "1.0.x",
-- linkdirs = {"/usr/lib"}, includedirs = "/usr/include", links = {"ssl"}, includes = {"ssl.h"}
diff --git a/xmake/modules/package/manager/pkg_config/find_package.lua b/xmake/modules/package/manager/pkg_config/find_package.lua
new file mode 100644
index 000000000..89990e127
--- /dev/null
+++ b/xmake/modules/package/manager/pkg_config/find_package.lua
@@ -0,0 +1,35 @@
+--!A cross-platform build utility based on Lua
+--
+-- Licensed to the Apache Software Foundation (ASF) under one
+-- or more contributor license agreements. See the NOTICE file
+-- distributed with this work for additional information
+-- regarding copyright ownership. The ASF licenses this file
+-- to you under the Apache License, Version 2.0 (the
+-- "License"); you may not use this file except in compliance
+-- with the License. You may obtain a copy of the License at
+--
+-- http://www.apache.org/licenses/LICENSE-2.0
+--
+-- Unless required by applicable law or agreed to in writing, software
+-- distributed under the License is distributed on an "AS IS" BASIS,
+-- WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+-- See the License for the specific language governing permissions and
+-- limitations under the License.
+--
+-- Copyright (C) 2015 - 2019, TBOOX Open Source Group.
+--
+-- @author ruki
+-- @file find_package.lua
+--
+
+-- imports
+import("lib.detect.pkg_config")
+
+-- find package from the pkg-config package manager
+--
+-- @param name the package name
+-- @param opt the options, .e.g {verbose = true, version = "1.12.x")
+--
+function main(name, opt)
+ return pkg_config.find(name, opt)
+end
diff --git a/xmake/modules/package/manager/system/find_package.lua b/xmake/modules/package/manager/system/find_package.lua
new file mode 100644
index 000000000..d03e2f4cb
--- /dev/null
+++ b/xmake/modules/package/manager/system/find_package.lua
@@ -0,0 +1,123 @@
+--!A cross-platform build utility based on Lua
+--
+-- Licensed to the Apache Software Foundation (ASF) under one
+-- or more contributor license agreements. See the NOTICE file
+-- distributed with this work for additional information
+-- regarding copyright ownership. The ASF licenses this file
+-- to you under the Apache License, Version 2.0 (the
+-- "License"); you may not use this file except in compliance
+-- with the License. You may obtain a copy of the License at
+--
+-- http://www.apache.org/licenses/LICENSE-2.0
+--
+-- Unless required by applicable law or agreed to in writing, software
+-- distributed under the License is distributed on an "AS IS" BASIS,
+-- WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+-- See the License for the specific language governing permissions and
+-- limitations under the License.
+--
+-- Copyright (C) 2015 - 2019, TBOOX Open Source Group.
+--
+-- @author ruki
+-- @file find_package.lua
+--
+
+-- imports
+import("lib.detect.find_path")
+import("lib.detect.find_library")
+import("lib.detect.pkg_config")
+
+-- find package from the system directories
+--
+-- @param name the package name
+-- @param opt the options, .e.g {verbose = true, version = "1.12.x")
+--
+function main(name, opt)
+
+ -- only support the current host platform and architecture
+ if opt.plat ~= os.host() or opt.arch ~= os.arch() 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
+ table.insert(includedirs, "/usr/local/include")
+ table.insert(includedirs, "/usr/include")
+ table.insert(includedirs, "/opt/local/include")
+ table.insert(includedirs, "/opt/include")
+ end
+
+ -- add default search linkdirs on pc host
+ local linkdirs = table.wrap(opt.linkdirs)
+ if opt.plat == "linux" or opt.plat == "macosx" then
+ table.insert(linkdirs, "/usr/local/lib")
+ table.insert(linkdirs, "/usr/lib")
+ table.insert(linkdirs, "/opt/local/lib")
+ table.insert(linkdirs, "/opt/lib")
+ if opt.plat == "linux" and opt.arch == "x86_64" then
+ table.insert(linkdirs, "/usr/local/lib/x86_64-linux-gnu")
+ table.insert(linkdirs, "/usr/lib/x86_64-linux-gnu")
+ table.insert(linkdirs, "/usr/lib64")
+ table.insert(linkdirs, "/opt/lib64")
+ end
+ end
+
+ -- attempt to get links from pkg-config
+ local pkginfo = nil
+ local version = nil
+ local links = table.wrap(opt.links)
+ if #links == 0 then
+ pkginfo = pkg_config.info(name)
+ if pkginfo then
+ links = table.wrap(pkginfo.links)
+ version = pkginfo.version
+ end
+ end
+
+ -- uses name as links directly .e.g libname.a
+ if #links == 0 then
+ links = table.wrap(name)
+ end
+
+ -- 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
+
+ -- 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
+
+ -- not found? only add links
+ if not result and pkginfo and pkginfo.links then
+ result = {links = pkginfo.links}
+ end
+
+ -- save version
+ if result and version then
+ result.version = version
+ end
+
+ -- ok
+ return result
+end