summaryrefslogtreecommitdiff
path: root/xmake/modules/package/manager/system/find_package.lua
blob: df2e812a00f03f0cfc68a4a3fe9406ee21d3b6cb (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
--!A cross-platform build utility based on Lua
--
-- Licensed 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 #includedirs == 0 then
        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
    end

    -- add default search linkdirs on pc host
    local linkdirs = table.wrap(opt.linkdirs)
    if #linkdirs == 0 then
        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
    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.libinfo(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
    if opt.includes then
        for _, include in ipairs(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
    elseif result and result.links and opt.includedirs then
        result.includedirs = opt.includedirs
    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