diff options
| author | ruki <[email protected]> | 2017-06-12 11:31:28 +0800 |
|---|---|---|
| committer | ruki <[email protected]> | 2017-06-12 11:31:44 +0800 |
| commit | b176145e2efa26ca36b166d7e838afcc207050a4 (patch) | |
| tree | bce6446ad2f541ce567daeeeaf9a24b36cdb7f33 | |
| parent | e6f6d43feb29a7c87a70bc8cbfdb0670809a65d8 (diff) | |
improve find_package and add pkg_config module
| -rw-r--r-- | xmake/core/sandbox/modules/import/lib/detect/find_library.lua | 31 | ||||
| -rw-r--r-- | xmake/core/sandbox/modules/import/lib/detect/find_package.lua | 48 | ||||
| -rw-r--r-- | xmake/modules/lib/detect/pkg_config.lua | 96 |
3 files changed, 150 insertions, 25 deletions
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 4b386f947..9ac08777d 100644 --- a/xmake/core/sandbox/modules/import/lib/detect/find_library.lua +++ b/xmake/core/sandbox/modules/import/lib/detect/find_library.lua @@ -34,10 +34,8 @@ local target = require("project/target") local config = require("project/config") local raise = require("sandbox/modules/raise") local import = require("sandbox/modules/import") - --- import sandbox modules -import("lib.detect.find_file") -import("detect.tool.find_pkg_config") +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) @@ -84,24 +82,13 @@ function sandbox_lib_detect_find_library.main(names, paths, kinds) local arch = config.get("arch") or os.arch() -- attempt to add search pathes from pkg-config - local pkg_config = find_pkg_config() - if pkg_config then - for _, name in ipairs(table.wrap(names)) do - - -- attempt to get -L/xxx/dir - local ok, libs_only_L = os.iorunv(pkg_config, {"--libs-only-L", name}) - if not ok and not name:startswith("lib") then - ok, libs_only_L = os.iorunv(pkg_config, {"--libs-only-L", "lib" .. name}) - end - - -- add linkdir to pathes - if libs_only_L then - local linkdir = (libs_only_L:match("%-L(.+)") or ""):trim() - if os.isdir(linkdir) then - table.insert(pathes, linkdir) - break - end - end + 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 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 a225945b7..438e13ba5 100644 --- a/xmake/core/sandbox/modules/import/lib/detect/find_package.lua +++ b/xmake/core/sandbox/modules/import/lib/detect/find_package.lua @@ -35,6 +35,9 @@ local cache = require("project/cache") local project = require("project/project") local raise = require("sandbox/modules/raise") local import = require("sandbox/modules/import") +local find_file = import("lib.detect.find_file") +local find_library = import("lib.detect.find_library") +local pkg_config = import("lib.detect.pkg_config") -- find package from repositories function sandbox_lib_detect_find_package._find_from_repositories(name, opt) @@ -56,7 +59,44 @@ end -- find package from system function sandbox_lib_detect_find_package._find_from_system(name, opt) - -- TODO + + -- init links and pathes + local links = opt.links or {} + local pathes = opt.pathes or {} + + -- 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}) + end + if pkginfo then + table.join2(links, pkginfo.links) + table.join2(pathes, pkginfo.linkdirs) + table.join2(pathes, pkginfo.includedirs) + end + + -- find library + local result = nil + for _, link in ipairs(links) do + local libinfo = find_library(link, pathes) + 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.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) + end + + -- ok + return result end -- find package @@ -70,7 +110,8 @@ function sandbox_lib_detect_find_package._find(name, opt) , sandbox_lib_detect_find_package._find_from_system } - -- find it + -- find it, TODO match version + opt = opt or {} for _, find in ipairs(findscripts) do local package = find(name, opt) if package then @@ -82,7 +123,7 @@ end -- find package -- -- @param name the package name --- @param opt the package options. e.g. {version = ">1.0.1", pathes = {"/usr/lib"}} +-- @param opt the package options. e.g. {version = ">1.0.1", pathes = {"/usr/lib"}, links = {"ssl"}, includes = {"ssl.h"}} -- -- @return {links = {"ssl", "crypto", "z"}, linkdirs = {"/usr/local/lib"}, includedirs = {"/usr/local/include"}, version = "1.0.2"} -- @@ -91,6 +132,7 @@ end -- local package = find_package("openssl") -- local package = find_package("openssl", {version = ">1.0.1"}) -- local package = find_package("openssl", {pathes = {"/usr/lib", "/usr/local/lib", "/usr/local/include"}, version = ">1.0.1"}) +-- local package = find_package("openssl", {pathes = {"/usr/lib", "/usr/local/lib", "/usr/local/include"}, links = {"ssl", "crypto"}, includes = {"ssl.h"}}) -- -- @endcode -- diff --git a/xmake/modules/lib/detect/pkg_config.lua b/xmake/modules/lib/detect/pkg_config.lua new file mode 100644 index 000000000..37fee3018 --- /dev/null +++ b/xmake/modules/lib/detect/pkg_config.lua @@ -0,0 +1,96 @@ +--!The Make-like 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 - 2017, TBOOX Open Source Group. +-- +-- @author ruki +-- @file pkg_config.lua +-- + +-- imports +import("detect.tool.find_pkg_config") + +-- 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) + + -- attempt to add search pathes from pkg-config + local pkg_config = find_pkg_config() + if not pkg_config then + return + end + + -- get libs and cflags + local result = nil + local flags = try { function () return os.iorunv(pkg_config, {"--libs", "--cflags", name}) end } + if flags then + + -- init result + result = {} + for _, flag in ipairs(flags:split('%s*')) do + + -- get links + local link = flag:match("%-l(.*)") + if link then + result.links = result.links or {} + table.insert(result.links, link) + end + + -- get linkdirs + local linkdirs = nil + local linkdir = flag:match("%-L(.*)") + if linkdir and os.isdir(linkdir) then + result.linkdirs = result.linkdirs or {} + table.insert(result.linkdirs, linkdir) + end + + -- get includedirs + local includedirs = nil + local includedir = flag:match("%-I(.*)") + if includedir and os.isdir(includedir) then + result.includedirs = result.includedirs or {} + table.insert(result.includedirs, includedir) + end + end + end + + -- find version + if opt and 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 + end + end + + -- ok? + return result +end |
