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
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
|
--!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 package.lua
--
-- define module
local package = {}
local _instance = _instance or {}
-- load modules
local io = require("base/io")
local os = require("base/os")
local path = require("base/path")
local table = require("base/table")
local utils = require("base/utils")
local semver = require("base/semver")
local rule = require("project/rule")
local config = require("project/config")
local sandbox = require("sandbox/sandbox")
local localcache = require("cache/localcache")
local instance_deps = require("base/private/instance_deps")
-- save the requires info to the cache
function _instance:save()
package._cache():set(self:name(), self._INFO)
package._cache():save()
end
-- clear the package
function _instance:clear()
local info = self._INFO
if info then
for k, v in pairs(info) do
if not k:startswith("__") then
info[k] = nil
end
end
end
self._COMPONENT_DEPS = nil
end
-- dump this package
function _instance:dump()
utils.dump(self._INFO)
end
-- get the require info
function _instance:get(infoname)
return self._INFO[infoname]
end
-- get the package name (with alias name)
function _instance:name()
return self._NAME
end
-- get the full name
function _instance:fullname()
local namespace = self:namespace()
return namespace and namespace .. "::" .. self:name() or self:name()
end
-- get the package version
function _instance:version()
-- get it from cache first
if self._VERSION ~= nil then
return self._VERSION
end
-- get version
local version = nil
local verstr = self:get("version")
if verstr then
version = semver.new(verstr)
end
self._VERSION = version or false
return version
end
-- get the package license
function _instance:license()
return self:get("license")
end
-- has static libraries?
function _instance:has_static()
return self:get("static")
end
-- has shared libraries?
function _instance:has_shared()
return self:get("shared")
end
-- get the require string
function _instance:requirestr()
return self:get("__requirestr")
end
-- get the namespace
function _instance:namespace()
return self:get("__namespace")
end
-- get the require configuration from the given name
--
-- e.g.
--
-- add_requires("xxx", {system = true, configs = {shared = true}})
--
-- local configs = pkg:requireconf()
-- local system = pkg:requireconf("system")
-- local shared = pkg:requireconf("configs", "shared")
--
function _instance:requireconf(name, key)
local requireconfs = self:get("__requireconfs")
local value = requireconfs
if name then
value = requireconfs and requireconfs[name] or nil
if value and key then
value = value[key]
end
end
return value
end
-- get the install directory
-- @see https://github.com/xmake-io/xmake/issues/3106
function _instance:installdir()
return self:get("installdir")
or self:get("__installdir") -- deprecated
end
-- get library files
function _instance:libraryfiles()
return self:get("libfiles")
end
-- get components
function _instance:components()
return self:get("components")
end
-- get default components
function _instance:components_default()
return self:get("__components_default")
end
-- get components list with link order
function _instance:components_orderlist()
return self:get("__components_orderlist")
end
-- get the dependencies of components
function _instance:components_deps()
return self:get("__components_deps")
end
-- get user extra configuration from package/on_fetch
-- @see https://github.com/xmake-io/xmake/issues/3106#issuecomment-1330143922
--
-- e.g.
--
-- @code
-- package("xxx")
-- on_fetch(function (package)
-- return {includedirs = "", links = "", extras = {foo = ""}}
-- end)
--
-- @endcode
--
-- we can also get extra configuration from package/add_xxx
--
-- e.g.
--
-- @code
-- package("xxx")
-- add_linkgroups("foo", {group = true})
--
-- target:pkg("xxx"):extraconf("linkgroups", "foo", "group")
-- @endcode
--
-- extras = {
-- linkgroups = {
-- z = {
-- group = true
-- }
-- }
-- }
--
function _instance:extraconf(name, item, key)
local extraconfs = self:get("extras")
if not extraconfs then
return
end
-- get configuration
local extraconf = extraconfs[name]
-- get configuration value
local value = extraconf
if item then
value = extraconf and extraconf[item] or nil
if value == nil and extraconf and type(item) == "table" then
value = extraconf[table.concat(item, "_")]
end
if value and key then
value = value[key]
end
end
return value
end
-- add extra configuration
function _instance:extraconf_add(name, item)
local extraconfs = self:get("extras")
if not extraconfs then
extraconfs = {}
end
local extraconf = extraconfs[name]
if extraconf ~= nil then
extraconf = table.wrap(extraconf)
table.join2(extraconf, item)
else
extraconf = item
end
extraconfs[name] = extraconf
self:set("extras", extraconfs)
end
-- get order dependencies of the given component
function _instance:component_orderdeps(name)
local component_orderdeps = self._COMPONENT_ORDERDEPS
if not component_orderdeps then
component_orderdeps = {}
self._COMPONENT_ORDERDEPS = component_orderdeps
end
-- expand dependencies
local orderdeps = component_orderdeps[name]
if not orderdeps then
orderdeps = table.reverse_unique(self:_sort_componentdeps(name))
component_orderdeps[name] = orderdeps
end
return orderdeps
end
-- set the value to the requires info
function _instance:set(name_or_info, ...)
if type(name_or_info) == "string" then
local args = ...
if args ~= nil then
self._INFO[name_or_info] = table.unwrap(table.unique(table.join(...)))
else
self._INFO[name_or_info] = nil
end
elseif table.is_dictionary(name_or_info) then
for name, info in pairs(table.join(name_or_info, ...)) do
self:set(name, info)
end
end
end
-- add the value to the requires info
function _instance:add(name_or_info, ...)
if type(name_or_info) == "string" then
local name = name_or_info
if name == "extras" then
for _, extraconf in ipairs({...}) do
for k, v in pairs(extraconf) do
self:extraconf_add(k, v)
end
end
else
local info = table.wrap(self._INFO[name])
self._INFO[name] = table.unwrap(table.unique(table.join(info, ...)))
end
elseif table.is_dictionary(name_or_info) then
for name, info in pairs(table.join(name_or_info, ...)) do
self:add(name, info)
end
end
end
-- this require info is enabled?
function _instance:enabled()
return self:get("__enabled")
end
-- enable or disable this require info
function _instance:enable(enabled)
self:set("__enabled", enabled)
end
-- get environments
function _instance:envs()
return self:get("envs")
end
-- get the given rule
function _instance:rule(name)
return self:rules()[name]
end
-- get package rules
-- @see https://github.com/xmake-io/xmake/issues/2374
function _instance:rules()
local rules = self._RULES
if rules == nil then
local ruleinfos = {}
local installdir = self:installdir()
local rulesdir = path.join(installdir, "rules")
if os.isdir(rulesdir) then
local files = os.filedirs(path.join(rulesdir, "*"))
if files then
for _, filepath in ipairs(files) do
local results, errors
if filepath:endswith(".lua") then
results, errors = rule._load(filepath)
elseif os.isdir(filepath) and os.isfile(path.join(filepath, "xmake.lua")) then
results, errors = rule._load(path.join(filepath, "xmake.lua"))
else
os.raise("unknown rule %s: %s", os.isdir(filepath) and "directory" or "file", filepath)
end
if results then
table.join2(ruleinfos, results)
else
os.raise(errors)
end
end
end
end
-- make rule instances
rules = {}
for rulename, ruleinfo in pairs(ruleinfos) do
local rulename = "@" .. self:name() .. "/" .. rulename
local instance = rule.new(rulename, ruleinfo, {package = self})
if instance:script("load") then
utils.warning("we cannot add `on_load()` in package rule(%s), please use `on_config()` instead of it!", rulename)
end
if instance:script("load_after") then
utils.warning("we cannot add `after_load()` in package rule(%s), please use `on_config()` instead of it!", rulename)
end
rules[rulename] = instance
end
self._RULES = rules
end
return rules
end
-- sort component deps
function _instance:_sort_componentdeps(name)
local orderdeps = {}
local plaindeps = self:components_deps() and self:components_deps()[name]
for _, dep in ipairs(table.wrap(plaindeps)) do
table.insert(orderdeps, dep)
table.join2(orderdeps, self:_sort_componentdeps(dep))
end
return orderdeps
end
-- we need to sort package set keys by this string
-- @see https://github.com/xmake-io/xmake/pull/2971#issuecomment-1290052169
function _instance:__tostring()
return "<package: " .. self:fullname() .. ">"
end
-- get cache
function package._cache()
return localcache.cache("package")
end
-- load the package from the cache
function package.load(name)
local info = package._cache():get(name)
if info == nil then
return
end
return package.load_withinfo(name, info)
end
-- load package from the give package info
function package.load_withinfo(name, info)
local instance = table.inherit(_instance)
instance._INFO = info
instance._NAME = name
return instance
end
-- return module
return package
|