diff options
| author | ruki <[email protected]> | 2019-01-23 21:05:20 +0800 |
|---|---|---|
| committer | ruki <[email protected]> | 2019-01-23 00:32:05 +0800 |
| commit | 7d9fe778c235da802039bf46c62cd4142f4719e4 (patch) | |
| tree | 4b2239aa013f24d8fc7fbd403c3e80245604363a /xmake/modules/package/manager/system | |
| parent | de29dc88bc3b7d39d4206d6e2bd034d15b232ca2 (diff) | |
rewrite find_package for brew, pkgconfig and system
Diffstat (limited to 'xmake/modules/package/manager/system')
| -rw-r--r-- | xmake/modules/package/manager/system/find_package.lua | 123 |
1 files changed, 123 insertions, 0 deletions
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 |
