-
-
Notifications
You must be signed in to change notification settings - Fork 764
/
find_package.lua
160 lines (150 loc) · 5.85 KB
/
find_package.lua
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
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
--!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-present, TBOOX Open Source Group.
--
-- @author ruki
-- @file find_package.lua
--
-- imports
import("lib.detect.find_tool")
import("lib.detect.find_file")
import("lib.detect.find_path")
import("lib.detect.pkgconfig")
import("core.project.target")
import("package.manager.find_package")
import("private.core.base.is_cross")
-- get the root directory of the brew packages
function _brew_pkg_rootdir()
local brew_pkg_rootdir = _g.brew_pkg_rootdir
if brew_pkg_rootdir == nil then
local brew = find_tool("brew")
if brew then
brew_pkg_rootdir = try
{
function ()
return os.iorunv(brew.program, {"--prefix"})
end
} or "/usr/local"
end
if brew_pkg_rootdir then
brew_pkg_rootdir = brew_pkg_rootdir:trim()
end
_g.brew_pkg_rootdir = brew_pkg_rootdir or false
end
return brew_pkg_rootdir or nil
end
-- find package from pkg-config
function _find_package_from_pkgconfig(name, opt)
opt = opt or {}
local brew_pkg_rootdir = opt.brew_pkg_rootdir
-- parse name, e.g. pcre/libpcre16
local nameinfo = name:split('/')
local pcname = nameinfo[2] or nameinfo[1]
-- find package from pkg-config/*.pc,
-- but we cannot find it from `brew --prefix`/package, because `brew --prefix` is too slow (> 3s) when it's not found.
local result = nil
local paths = {}
table.insert(paths, path.join(brew_pkg_rootdir, nameinfo[1], "lib/pkgconfig"))
table.insert(paths, path.join(brew_pkg_rootdir, nameinfo[1], "share/pkgconfig"))
if opt.require_version then
table.insert(paths, path.join(brew_pkg_rootdir, nameinfo[1], opt.require_version, "lib/pkgconfig"))
table.insert(paths, path.join(brew_pkg_rootdir, nameinfo[1], opt.require_version, "share/pkgconfig"))
else
table.insert(paths, path.join(brew_pkg_rootdir, nameinfo[1], "*/lib/pkgconfig"))
table.insert(paths, path.join(brew_pkg_rootdir, nameinfo[1], "*/share/pkgconfig"))
end
local pcfile = find_file(pcname .. ".pc", paths)
if pcfile then
opt.configdirs = path.directory(pcfile)
result = find_package("pkgconfig::" .. pcname, opt)
if not result or not result.includedirs then
-- attempt to get includedir variable from pkg-config/xx.pc
local varinfo = pkgconfig.variables(pcname, "includedir", opt)
if varinfo and varinfo.includedir then
result = result or {}
result.version = pkgconfig.version(pcname, opt)
result.includedirs = varinfo.includedir
end
end
end
return result
end
-- 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)
opt = opt or {}
if is_cross(opt.plat, opt.arch) then
return
end
-- find the prefix directory of brew
local brew_pkg_rootdir = _brew_pkg_rootdir()
if not brew_pkg_rootdir then
return
end
brew_pkg_rootdir = path.join(brew_pkg_rootdir, opt.plat == "macosx" and "Cellar" or "opt")
-- find package from pkg-config
local result = _find_package_from_pkgconfig(name,
table.join(opt, {brew_pkg_rootdir = brew_pkg_rootdir}))
-- find components
local components
local components_extsources = opt.components_extsources
for _, comp in ipairs(opt.components) do
local extsource = components_extsources and components_extsources[comp]
if extsource then
local component_result = _find_package_from_pkgconfig(extsource,
table.join(opt, {brew_pkg_rootdir = brew_pkg_rootdir}))
if component_result then
components = components or {}
components[comp] = component_result
end
end
end
if components then
result = result or {}
result.components = components
components.__base = {}
end
-- find package from xxx/lib, xxx/include
if not result then
local nameinfo = name:split('/')
local pkgdir
if is_host("linux") then
-- /home/linuxbrew/.linuxbrew/opt/llvm
pkgdir = find_path("lib", path.join(brew_pkg_rootdir, nameinfo[1]))
else
-- /usr/local/Cellar/llvm/16.0.6
pkgdir = find_path("lib", path.join(brew_pkg_rootdir, nameinfo[1], "*"))
end
if pkgdir then
local links = {}
for _, libfile in ipairs(os.files(path.join(pkgdir, "lib", "*.a"))) do
table.insert(links, target.linkname(path.filename(libfile), {plat = opt.plat}))
end
for _, libfile in ipairs(os.files(path.join(pkgdir, "lib", opt.plat == "macosx" and "*.dylib" or "*.so"))) do
if not os.islink(libfile) then
table.insert(links, target.linkname(path.filename(libfile), {plat = opt.plat}))
end
end
opt.links = links
opt.linkdirs = path.join(pkgdir, "lib")
opt.includedirs = path.join(pkgdir, "include")
result = find_package("system::" .. name, opt)
end
end
return result
end