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
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
|
--!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, Xmake Open Source Community.
--
-- @author ruki
-- @file find_package.lua
--
-- imports
import("lib.detect.find_file")
import("lib.detect.find_tool")
import("core.base.option")
import("core.project.config")
import("core.project.target")
import("detect.sdks.find_vcpkgdir")
import("package.manager.vcpkg.configurations")
import("package.manager.pkgconfig.find_package", {alias = "find_package_from_pkgconfig"})
-- we iterate over each pkgconfig file to extract the required data
function _find_package_from_pkgconfig(pkgconfig_files, opt)
opt = opt or {}
local foundpc = false
local result = {includedirs = {}, linkdirs = {}, links = {}}
for _, pkgconfig_file in ipairs(pkgconfig_files) do
local pkgconfig_dir = path.join(opt.installdir, path.directory(pkgconfig_file))
local pkgconfig_name = path.basename(pkgconfig_file)
local pcresult = find_package_from_pkgconfig(pkgconfig_name, {configdirs = pkgconfig_dir, linkdirs = opt.linkdirs})
-- the pkgconfig file has been parse successfully
if pcresult then
for _, includedir in ipairs(pcresult.includedirs) do
table.insert(result.includedirs, includedir)
end
for _, linkdir in ipairs(pcresult.linkdirs) do
table.insert(result.linkdirs, linkdir)
end
for _, link in ipairs(pcresult.links) do
table.insert(result.links, link)
end
-- version should be the same if a pacman package contains multiples .pc
result.version = pcresult.version
foundpc = true
end
end
if foundpc == true then
result.includedirs = table.unique(result.includedirs)
result.linkdirs = table.unique(result.linkdirs)
result.links = table.reverse_unique(result.links)
return result
end
end
function _get_package_info(name, triplet, infodirs, arch, plat, mode)
-- find the package info file, e.g. zlib_1.2.11-3_x86-windows[-static].list
local infofile = find_file(format("%s_*_%s.list", name, triplet), infodirs)
if not infofile then
return
end
local installdir = path.directory(path.directory(path.directory(infofile)))
-- save includedirs, linkdirs and links
local result = nil
local pkgconfig_files = {}
local info = io.readfile(infofile)
if info then
for _, line in ipairs(info:split('\n')) do
line = line:trim()
if plat == "windows" then
line = line:lower()
end
-- get pkgconfig files
if line:find(triplet .. (mode == "debug" and "/debug" or "") .. "/lib/pkgconfig/", 1, true) and line:endswith(".pc") then
table.insert(pkgconfig_files, line)
end
-- get includedirs
if line:endswith("/include/") then
result = result or {}
result.includedirs = result.includedirs or {}
table.insert(result.includedirs, path.join(installdir, line))
end
-- get linkdirs and links
if (plat == "windows" and line:endswith(".lib")) or line:endswith(".a") or line:endswith(".so") then
if line:find(triplet .. (mode == "debug" and "/debug" or "") .. "/lib/", 1, true) then
result = result or {}
result.links = result.links or {}
result.linkdirs = result.linkdirs or {}
result.libfiles = result.libfiles or {}
table.insert(result.linkdirs, path.join(installdir, path.directory(line)))
table.insert(result.links, target.linkname(path.filename(line), {plat = plat}))
table.insert(result.libfiles, path.join(installdir, path.directory(line), path.filename(line)))
end
end
-- add shared library directory (/bin/) to linkdirs for running with PATH on windows
if plat == "windows" and line:endswith(".dll") then
if line:find(plat .. (mode == "debug" and "/debug" or "") .. "/bin/", 1, true) then
result = result or {}
result.linkdirs = result.linkdirs or {}
result.libfiles = result.libfiles or {}
table.insert(result.linkdirs, path.join(installdir, path.directory(line)))
table.insert(result.libfiles, path.join(installdir, path.directory(line), path.filename(line)))
end
end
end
end
-- find result from pkgconfig first
if #pkgconfig_files > 0 then
local pkgconfig_result = _find_package_from_pkgconfig(pkgconfig_files, {installdir = installdir, linkdirs = result and result.linkdirs})
if pkgconfig_result then
result = pkgconfig_result
end
end
return result
end
function _find_package(vcpkg, vcpkgdir, name, opt)
-- get configs
local configs = opt.configs or {}
-- fix name, e.g. ffmpeg[x264] as ffmpeg
-- @see https://github.com/xmake-io/xmake/issues/925
name = name:gsub("%[.-%]", "")
-- init triplet
local arch = opt.arch
local plat = opt.plat
local mode = opt.mode
plat = configurations.plat(plat)
arch = configurations.arch(arch)
local triplet = configurations.triplet(configs, plat, arch)
-- get the vcpkg info directories
local infodirs = {}
if opt.installdir then
table.join2(infodirs, path.join(opt.installdir, "vcpkg_installed", "vcpkg", "info"))
end
table.join2(infodirs, path.join(vcpkgdir, "installed", "vcpkg", "info"))
-- find the package info file, e.g. zlib_1.2.11-3_x86-windows[-static].list
local infofile = find_file(format("%s_*_%s.list", name, triplet), infodirs)
if not infofile then
return
end
-- find dependency package
local result = nil
local _, dependinfo = try { function () return os.iorunv(vcpkg, {"depend-info", name, "--sort=reverse", "--triplet=" .. triplet}) end }
if dependinfo then
for _, line in ipairs(dependinfo:split("\n", {plain = true})) do
if not line:startswith("vcpkg-") then
local packagename = line:match("^([^%[:]+)[^:]*:")
if packagename then
local dependencyresult = _get_package_info(packagename, triplet, infodirs, arch, plat, mode)
if dependencyresult then
result = result or {}
for key, dependencylist in pairs(dependencyresult) do
result[key] = result[key] or {}
table.join2(result[key], dependencylist)
end
end
end
end
end
end
-- save version
if result then
local infoname = path.basename(infofile)
local prefix = name
-- name maybe contains lua pattern characters, we need escape it. e.g. `-`
prefix = prefix:gsub("([%+%.%-%^%$%(%)%%])", "%%%1")
result.version = infoname:match(prefix .. "_(%d+%.?%d*%.?%d*.-)_" .. arch)
if not result.version then
result.version = infoname:match(prefix .. "_(%d+%.?%d*%.-)_" .. arch)
end
end
-- remove repeat
if result then
for k, v in pairs(result) do
if k == "links" or k == "syslinks" or k == "frameworks" then
result[k] = table.unwrap(table.reverse_unique(v))
else
result[k] = table.unwrap(table.unique(v))
end
end
end
return result
end
-- find package from the vcpkg package manager
--
-- @param name the package name, e.g. zlib, pcre
-- @param opt the options, e.g. {verbose = true)
--
function main(name, opt)
opt = opt or {}
local vcpkgdir = find_vcpkgdir()
if not vcpkgdir then
-- we need show warning if we do not try finding package and vcpkg root directory not found.
if not opt.try then
wprint("vcpkg root directory not found, maybe you need set $VCPKG_ROOT!")
end
return
end
-- attempt to find vcpkg
local vcpkg = find_tool("vcpkg")
if not vcpkg then
raise("vcpkg not found!")
end
-- do find package
return _find_package(vcpkg.program, vcpkgdir, name, opt)
end
|