-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathexport.lua
387 lines (318 loc) · 8.48 KB
/
export.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
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
---
-- Battle.net package management extension
-- Copyright (c) 2014-2016 Blizzard Entertainment
---
local p = premake
local findUses
premake.api.register {
name = 'use_exposed',
scope = 'config',
kind = 'list:string'
}
premake.api.register {
name = 'use_private',
scope = 'config',
kind = 'list:string'
}
p.exportRule = p.container.newClass("exportRule", p.rule, { "config" })
p.exportProject = p.container.newClass("exportProject", p.project, { "config" })
function p.exportRule.new(name)
return p.container.new(p.exportRule, name)
end
function p.exportProject.new(name)
return p.container.new(p.exportProject, name)
end
local function validateName(name)
if type(name) == "table" then
if #name == 0 then
return nil
end
if #name > 1 then
error("exportsettings cannot take more than one argument", 1)
end
name = name[1]
end
if type(name) ~= "string" then
error("exportsettings argument must be a string", 1)
end
if #name <= 0 then
return nil
end
name = name:lower()
if name ~= 'compile' and name ~= 'link' then
error("exportsettings can only be 'compile' or 'link'.", 1)
end
return name
end
function exportsettings(name)
name = validateName(name)
local scope = premake.api.scope.current
if scope then
if scope.class == p.project then
if not name then
error('invalid name for export scope')
end
return p.api._setContainer(p.exportProject, name)
end
if scope.class == p.rule then
if not name then
error('invalid name for export scope')
end
return p.api._setContainer(p.exportRule, name)
end
if scope.class == p.exportProject then
if not name then
return p.api._setContainer(p.exportProject.parent)
end
return p.api._setContainer(p.exportProject, name)
end
if scope.class == p.exportRule then
if not name then
return p.api._setContainer(p.exportRule.parent)
end
return p.api._setContainer(p.exportRule, name)
end
error('not in a valid scope, current scope == ' .. scope.class.name)
else
error('not in a valid scope, current scope == nil')
end
end
local function copyBlock(block)
local clone = {}
for k, v in pairs(block) do
if k == "_criteria" then
clone[k] = table.deepcopy(v)
else
clone[k] = v
end
end
-- The blocks may have invalid infos reserved for the first ones
if clone.filename then
clone.filename = nil
clone._basedir = clone.basedir
clone.basedir = nil
end
return clone
end
local function applyBlocks(blocklist, ctx, blocks)
for _, block in ipairs(blocks) do
if criteria.matches(block._criteria, ctx.terms) then
local newBlock = copyBlock(block)
table.insert(blocklist, newBlock)
end
end
end
local function getpackage(ctx, name)
local p = ctx.workspace.package_cache[name:lower()]
if not p then
error("Package '" .. name .. "' was not imported, but the project '" .. ctx.project.name .. "' has a dependency on it.")
end
return p
end
local function enumprojects(ctx, name)
-- split import string.
local packageName, projectName = name:match("([^.]+)%.([^.]+)")
if not packageName then
packageName = name
projectName = '*'
end
-- find the package
local pkg = getpackage(ctx, packageName)
if #pkg.projects > 0 then
local pattern = projectName:gsub('%*', '.*')
local projects = {}
for _, v in ipairs(pkg.projects) do
if v.name:find(pattern) then
table.insert(projects, v)
end
end
-- return the iterator function
local i = 0
return function()
i = i + 1
if i <= #projects then
return pkg, projects[i]
end
end
else
local i = 0
return function()
i = i + 1
if i <= 1 then
return pkg, nil
end
end
end
end
-------------------------------------------------------------------------------
-- Rules
-------------------------------------------------------------------------------
local function applyRuleBlocks(blocklist, ctx, name)
local rule = p.global.getRule(name)
if rule then
for exp in p.container.eachChild(rule, p.exportRule) do
applyBlocks(blocklist, ctx, exp.blocks)
end
end
end
local function findRules(blocklist, ctx, block)
if block.rules and #block.rules > 0 then
for _, v in ipairs(block.rules) do
applyRuleBlocks(blocklist, ctx, v)
end
end
end
-------------------------------------------------------------------------------
-- Collection methods.
-------------------------------------------------------------------------------
local function getCompileUses(result, ctx, block)
-- forward declarations.
local findExposedUsesIn
local findUses
local function apply(pkg, prj)
local n = pkg.name
if prj ~= nil then
n = n .. '.' .. prj.name
end
if not result[n] then
result[n] = {
package = pkg,
project = prj
}
if prj ~= nil then
for _, block in ipairs(prj.blocks) do
if criteria.matches(block._criteria, ctx.terms) then
findUses(ctx, block.use_exposed)
end
end
end
end
end
function findExposedUsesIn(ctx, name)
for pkg, prj in enumprojects(ctx, name) do
apply(pkg, prj)
end
end
function findUses(ctx, blocks)
if blocks then
for _, v in ipairs(blocks) do
findExposedUsesIn(ctx, v)
end
end
end
-- recursively go through all the uses blocks.
findUses(ctx, block.use_exposed)
findUses(ctx, block.use_private)
end
local function getLinkUses(result, ctx, block)
local function apply(pkg, prj)
local n = pkg.name
if prj ~= nil then
n = n .. '.' .. prj.name
end
if not result[n] then
result[n] = {
package = pkg,
project = prj
}
if prj ~= nil then
for _, block in ipairs(prj.blocks) do
if criteria.matches(block._criteria, ctx.terms) then
getLinkUses(result, ctx, block)
end
end
end
end
end
local function findUsesIn(ctx, name)
for pkg, prj in enumprojects(ctx, name) do
apply(pkg, prj)
end
end
if block.use_exposed then
for _, v in ipairs(block.use_exposed) do
findUsesIn(ctx, v)
end
end
if block.use_private then
for _, v in ipairs(block.use_private) do
findUsesIn(ctx, v)
end
end
end
-------------------------------------------------------------------------------
-- Apply methods.
-------------------------------------------------------------------------------
local function applyPackageCompiles(blocklist, ctx, pkg)
if not ctx.__compileImports[pkg] then
ctx.__compileImports[pkg] = true
block = {}
block._origin = blocklist
block.includedirs = pkg.auto_includes(ctx)
block.bindirs = pkg.auto_bindirs(ctx)
if #block.includedirs > 0 or #block.bindirs > 0 then
table.insert(blocklist, block)
end
end
end
local function applyPackageLinks(blocklist, ctx, pkg)
if not ctx.__linkImports[pkg] then
ctx.__linkImports[pkg] = true
block = {}
block._origin = blocklist
block.links = pkg.auto_links(ctx)
block.libdirs = pkg.auto_libdirs(ctx)
if #block.links > 0 or #block.libdirs > 0 then
table.insert(blocklist, block)
end
end
end
local function applyProjectExports(blocklist, ctx, prj, mode)
for exp in p.container.eachChild(prj, p.exportProject) do
if exp.name == mode then
applyBlocks(blocklist, ctx, exp.blocks)
end
end
end
-------------------------------------------------------------------------------
-- premake override.
-------------------------------------------------------------------------------
premake.override(p.oven, "finishConfig", function(base, ctx)
-- run base first.
base(ctx)
-- add all our import blocks.
if ctx.project then
ctx.hasLinkStep = (ctx.kind == p.SHAREDLIB) or (ctx.kind == p.CONSOLEAPP) or (ctx.kind == p.WINDOWEDAPP)
local blocksCopy = table.shallowcopy(ctx._cfgset.blocks)
local blocklist = ctx._cfgset.blocks
local compileUses = {}
local linkUses = {}
for _, block in pairs(blocksCopy) do
if ctx.hasLinkStep and block.use_exposed and #block.use_exposed > 0 then
local key = "use_exposed." .. ctx.project.name
p.warnOnce(key, 'Project "' .. ctx.project.name .. '" has a linkstep, but uses use_exposed to define its dependencies.')
end
getCompileUses(compileUses, ctx, block)
if ctx.hasLinkStep then
getLinkUses(linkUses, ctx, block)
end
findRules(blocklist, ctx, block)
end
ctx.__compileImports = {}
for _, u in spairs(compileUses) do
applyPackageCompiles(blocklist, ctx, u.package)
if u.project then
applyProjectExports(blocklist, ctx, u.project, 'compile')
end
end
if ctx.hasLinkStep then
ctx.__linkImports = {}
for _, u in spairs(linkUses) do
applyPackageLinks(blocklist, ctx, u.package)
if u.project then
applyProjectExports(blocklist, ctx, u.project, 'link')
end
end
end
end
end)