-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathpackageresolver.lua
308 lines (263 loc) · 7.23 KB
/
packageresolver.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
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
---
-- Package management module
-- Package dependency resolving code.
-- Copyright (c) 2014-2017 Blizzard Entertainment
---
local p = premake
local m = p.modules.packagemanager
local project = p.project
local import_filter = {}
---
-- private helper function
---
local function __matchLink(filename, match)
if type(match) == 'function' then
return match(filename)
end
return string.match(filename, match:lower())
end
---
-- help filter to link only those libs that are mentioned.
---
local function __createFilter(t)
if t then
return function(l)
local matches = {}
local nonmatches = {}
local all_index = nil
for k, link in ipairs(l) do
local filename = path.getname(link):lower()
for index, match in ipairs(t) do
if match == '*' then
all_index = index
elseif __matchLink(filename, match) then
if matches[index] then
table.insert(matches[index], link)
else
matches[index] = {link}
end
break
else
table.insert(nonmatches, link)
end
end
end
if all_index then
matches[all_index] = nonmatches
end
local linres = {}
for key, val in ipairs(matches) do
if t[key] == '*' then
table.insertflat(linres, val)
elseif #val > 0 then
table.insert(linres, val[1])
end
end
return linres;
end
else
return function(l)
return l
end
end
end
---
--- Gets the default import filter
---
local function __defaultImportFilter(name)
if import_filter[name:lower()] then
return import_filter[name:lower()]
end
return nil
end
---
--- resolve packages, internal method.
---
local function __resolvePackages(ctx)
if ctx.packages_resolved then
return
end
local function getpackage(wks, name)
local result = p.packagemanager.getPackage(name)
if not result then
local prjname = iif(ctx.project, ctx.project.name, ctx.name)
error("Package '" .. name .. "' was not imported, but the project '" .. prjname .. "' has a dependency on it.")
end
return result
end
local function sortedpairs(t)
-- collect all the keys for entries that are not numbers.
-- and store the values for entries that are numbers.
local keys = {}
local values = {}
for k, v in pairs(t) do
if tonumber(k) ~= nil then
table.insert(values, v)
else
table.insert(keys, k)
end
end
-- sort the keys.
table.sort(keys)
-- return the iterator function
local i = 0
local n = #values
return function()
i = i + 1
if (i <= n) then
return values[i], nil
else
local k = keys[i-n]
if k then
return k, t[k]
end
end
end
end
local function insertkeyed(tbl, values)
for _, value in ipairs(values) do
table.insertkeyed(tbl, value)
end
end
local function addOrMoveToBottom(tbl, values)
for _, value in ipairs(values) do
if not table.insertkeyed(tbl, value) then
local idx = table.indexof(tbl, value)
table.remove(tbl, idx)
table.insert(tbl, value)
end
end
end
local function recursiveIncludeDependencies(ctx, deps)
for name,_ in sortedpairs(deps) do
local pkg = getpackage(ctx.workspace, name)
insertkeyed(ctx.includedirs, pkg.auto_includes(ctx))
insertkeyed(ctx.defines, pkg.auto_defines(ctx))
insertkeyed(ctx.dependson, pkg.auto_dependson(ctx))
recursiveIncludeDependencies(ctx, pkg.auto_includedependencies(ctx))
end
end
local function recursiveLinkDependencies(ctx, deps)
for name, value in sortedpairs(deps) do
local filter = nil
if type(value) == 'table' then
filter = __createFilter(value)
else
filter = __createFilter(__defaultImportFilter(name))
end
local pkg = getpackage(ctx.workspace, name)
addOrMoveToBottom(ctx.links, filter(pkg.auto_links(ctx)))
insertkeyed(ctx.libdirs, pkg.auto_libdirs(ctx))
recursiveLinkDependencies(ctx, pkg.auto_linkdependencies(ctx))
end
end
-- resolve package includes & defines
if ctx.includedependencies then
recursiveIncludeDependencies(ctx, ctx.includedependencies);
end
-- resolve package binpath.
if ctx.bindirdependencies then
for name,_ in sortedpairs(ctx.bindirdependencies) do
local pkg = getpackage(ctx.workspace, name)
insertkeyed(ctx.bindirs, pkg.auto_bindirs(ctx))
end
end
-- resolve package includes.
if ctx.copybindependencies then
local seperator = package.config:sub(1,1)
local info = premake.config.gettargetinfo(ctx)
local targetDir = ctx.copybintarget or info.directory
for name, value in sortedpairs(ctx.copybindependencies) do
local pkg = getpackage(ctx.workspace, name)
for _, dir in ipairs(pkg.auto_bindirs(ctx)) do
local src = project.getrelative(ctx.project, dir)
local dst = project.getrelative(ctx.project, targetDir)
local command = string.format('{COPY} "%s" "%s"',
path.translate(src, seperator),
path.translate(dst, seperator))
table.insert(ctx.postbuildcommands, command)
end
end
end
-- we don't want static libs with links.
if ctx.links and next(ctx.links) and ctx.kind == p.STATICLIB then
local prjname = iif(ctx.project, ctx.project.name, ctx.name)
p.warnOnce(prjname..'l', "The project '" .. prjname .. "' is a static library, but uses 'links', for buildorder use 'dependson'.")
end
-- resolve package links.
if ctx.linkdependencies and next(ctx.linkdependencies) then
if ctx.kind == p.STATICLIB then
local prjname = iif(ctx.project, ctx.project.name, ctx.name)
p.warnOnce(prjname..'d', "The project '" .. prjname .. "' is a static library, but uses 'use/linkdependencies'.")
end
recursiveLinkDependencies(ctx, ctx.linkdependencies)
end
ctx.packages_resolved = true
end
---
--- inject package resolver into p.oven.bake
---
function m.generateManifest(wks)
local tbl = {}
for _, pkg in pairs(wks.package_cache) do
pkg:generateManifest(tbl, wks)
end
return tbl
end
---
--- inject package resolver into p.oven.bake
---
p.override(p.oven, 'bake', function(base)
-- run bake first.
base()
-- Resolve Package dependencies.
print('Resolving Packages...')
verbosef('Package cache: %s', p.packagemanager.getCacheLocation())
for wks in p.global.eachWorkspace() do
for prj in p.workspace.eachproject(wks) do
if not prj.external then
verbosef("Resolving '%s'...", prj.name)
if _ACTION == 'xcode' then
if not cfg then
cfg = prj
end
__resolvePackages(prj)
end
for cfg in project.eachconfig(prj) do
__resolvePackages(cfg)
end
end
end
end
-- Write package metadata.
print('Generating Package Manifests...')
for wks in p.global.eachWorkspace() do
local mantbl = m.generateManifest(wks)
local manifest, err = json.encode_pretty(mantbl)
if manifest == nil then
p.error(err)
end
if #manifest > 2 then
p.generate(wks, ".pmanifest", function()
p.utf8()
p.outln(manifest)
end)
end
end
-- Force a lua GC.
collectgarbage()
end)
---
-- Import lib filter for a set of packages.
---
function importlibfilter(table)
if not table then
return nil
end
-- import packages.
for name, filter in pairs(table) do
if not import_filter[name:lower()] then
import_filter[name:lower()] = filter
end
end
end