-
Notifications
You must be signed in to change notification settings - Fork 2
/
fastbuild_utils.lua
431 lines (313 loc) · 10.1 KB
/
fastbuild_utils.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
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
--
-- actions/fastbuild/fastbuild.lua
-- Extend the existing exporters with support for FASTBuild
-- Copyright (c) 2017-2017 Daniel Penkała
--
local p = premake
p.fastbuild.utils = { }
local fbuild = p.fastbuild
local utils = fbuild.utils
---
-- Define the FASTBuild utility constants
---
utils.constants = { }
utils.constants.section = { }
utils.constants.section.name = "// %s"
utils.constants.section.separator = "//---------------------------------------------------------------------------"
---
-- Define the FASTBuild utility functions
---
function utils.traverseProjects(treeForTraverse)
local projects = {}
p.tree.traverse(treeForTraverse, {
onleaf = function(node)
table.insert(projects, node.project.name)
end
})
return projects
end
function utils.removeWhiteSpaces(string)
return (string:gsub("%s+", ""))
end
function utils.separator()
p.x(utils.constants.section.separator)
end
function utils.section(str, ...)
local formated = str:format(...)
p.x(utils.constants.section.name, formated)
utils.separator()
end
function utils.struct_begin(name, ...)
p.x(".%s = ", name:format(...))
p.push("[")
end
function utils.struct_pair(key, value, ...)
if type(value) == "string" then
if #({...}) == 0 then
p.x(".%s = '%s'", key, value)
else
p.x(".%s = '%s'", key, value:format(...))
end
else
p.x(".%s = {", key)
p.push()
for _, val in pairs(value) do
p.x("'%s',", val)
end
p.pop("}")
end
end
function utils.struct_pair_append(value, ...)
if #({...}) == 0 then
p.x(" + '%s'", value)
else
p.x(" + '%s'", value:format(...))
end
end
function fbuild.struct_pair_append(value, ...)
if #({...}) == 0 then
p.x(" + %s", value)
else
p.x(" + %s", value:format(...))
end
end
function utils.struct_end()
p.pop("]")
p.w()
end
function utils.struct(name, fields)
utils.struct_begin(name)
for _, field in pairs(fields) do
local name = field[1]
if type(field[2]) == "string" then
local value = field[2]:format(field[3], field[4], field[5], field[6])
utils.struct_pair(name, value)
else
local value = field[2]
utils.struct_pair(name, value[1])
for i = 2, #value do
utils.struct_pair_append(value[i])
end
end
end
utils.struct_end()
end
function fbuild.call(func, ...)
local args = { ... }
return function()
-- #todo use unpack or table.unpack, we dont want to only support premake with lua 5.3+
return func(args[1], args[2], args[3], args[4], args[5], args[6])
end
end
function fbuild.checkCompilers(prj)
local wks = prj.workspace
local configs_have_compilers = true
for cfg in p.project.eachconfig(prj) do
configs_have_compilers = configs_have_compilers and wks.compilers[fbuild.targetCompilerPlatform(cfg)] ~= nil
end
return configs_have_compilers
end
function fbuild.vstudioProjectToolset(prj)
return (prj.toolset or ""):match("msc%-(v%d+)") or prj.toolset
end
---------------------------------------------------------------------------
--
-- Naming utils
--
---------------------------------------------------------------------------
---
-- Filters nil values from the given (array or table) and returs an ARRAY!
---
local function filterempty(tab)
local result = { }
for _, v in pairs(tab) do
if v then
table.insert(result, v)
end
end
return result
end
---
-- Returns a generated name for project configuration scopes
---
local function generatedNameConfig(separator, cfg, prefix, suffix)
cfg = cfg.config or cfg
local prj = cfg.project
return table.concat(filterempty{ prefix, prj.name, cfg.platform, cfg.buildcfg, suffix }, separator)
end
---
-- Returns a generated name for project and workspace scopes
---
local function generatedNameProject(separator, prj, prefix, suffix)
prj = prj.project or prj
return table.concat(filterempty{ prefix, iif(prj.name, prj.name, prj), suffix }, separator)
end
---
-- Returns a string separated with 'minus' characters
---
function fbuild.targetName2(obj, cfg, join)
local name = obj.name or obj
return table.concat({ name, cfg.platform, utils.removeWhiteSpaces(cfg.buildcfg) }, iif(join, join, "-"))
end
function fbuild._targetName(cfg, prefix, suffix)
return iif(cfg.project and cfg.project ~= cfg, generatedNameConfig, generatedNameProject)("-", cfg, prefix, suffix)
end
---
-- Returns a string separated with 'underscore' characters
---
function fbuild.listName(cfg, prefix, suffix)
return iif(cfg.project, generatedNameConfig, generatedNameProject)("_", cfg, prefix, suffix)
end
---
-- Returns a string separated with 'underscore' characters
---
function fbuild.structName(cfg, prefix, suffix)
return iif(cfg.project, generatedNameConfig, generatedNameProject)("_", cfg, prefix, suffix)
end
---
-- Returns the target platform name for the given config
---
function fbuild.targetPlatform(cfg)
local config = cfg.config or cfg
return table.concat(filterempty{ config.system, config.architecture }, "|")
end
---
-- Returns the fbuild name for the platform structure to be used for ObjectList and Library functions
---
function fbuild.targetCompilerPlatform(cfg)
local config = cfg.config or cfg
return table.concat(filterempty{ config.system, config.architecture, (config.toolset:gsub("%-", "_")) }, "|")
end
---
-- Returns the struct name for the given platforms additional defined compiler, additional compiler suffixes: res
---
function fbuild.targetCompilerPlatformStruct(cfg, suffix)
local config = cfg.config or cfg
return ((table.concat(filterempty{ "platform", config.system, config.architecture, config.toolset, suffix }, "_")):gsub("%-", "_"))
end
---
-- Returns a target name for the given configuration
---
function fbuild.targetName(cfg, prefix, suffix)
return iif(cfg.project and cfg.project ~= cfg, generatedNameConfig, generatedNameProject)("-", cfg, prefix, suffix)
end
---------------------------------------------------------------------------
--
-- Generation utils
--
---------------------------------------------------------------------------
---
-- Emits an include statement with the given path to the output file
---
function fbuild.include(path)
p.x('#include "%s"', path)
end
---
-- Emits an Alias function call
---
function fbuild.emitAlias(name, targets, fmap)
fmap = iif(fmap, fmap, function(e) return e end)
fbuild.emitFunction("Alias", name, {
fbuild.call(fbuild.emitList, "Targets", {
fbuild.call(fbuild.emitListItems, targets, fmap)
})
})
end
---------------------------------------------------------------------------
--
-- Emitting functions, statements, structs and scopes
--
---------------------------------------------------------------------------
---
-- Emits a Using statement to the output file
---
function fbuild.emitUsing(value, ...)
p.x("Using( .%s )", value:format(...))
end
---
-- Emits a function call to the output file
---
function fbuild.emitFunction(name, alias, inner, after, ...)
if alias and #alias > 0 then
p.x("%s( '%s' )", name, alias)
else
p.x("%s()", name)
end
fbuild.emitScope(inner, after, ...)
end
---
-- Emits a for loop statement to the output file
---
function fbuild.emitForLoop(arg, array, inner, after, ...)
p.x("ForEach( .%s in .%s ) ", arg, array)
fbuild.emitScope(inner, after, arg, ...)
end
---
-- Emits a list definition to the output file
---
function fbuild.emitList(name, inner, after, ...)
p.x(".%s = ", name)
fbuild.emitScope(inner, after, ...)
end
---
-- Emits items to the output file
---
function fbuild.emitListItems(items, fmap, ...)
fmap = iif(fmap, fmap, fbuild.fmap.pass)
for _, item in ipairs(items) do
local val = fmap(item, ...)
if val then
p.x("%s, ", val)
end
end
end
---
-- Emits a scope construct to the output file
---
function fbuild.emitScope(inner, after, ...)
p.push("{")
p.callArray(inner, ...)
p.pop("}")
p.callArray(after, ...)
p.x("")
end
---
-- Emits a struct definition to the output file
---
function fbuild.emitStruct(name, inner, after, ...)
p.x(".%s = ", name)
p.push("[")
p.callArray(inner, ...)
p.pop("]")
p.callArray(after, ...)
p.x("")
end
---
-- Emits a struct value definition
---
function fbuild.emitStructValue(name, value, append, fmap)
p.x(".%s %s %s", name, iif(append, "+", "="), iif(fmap, fmap, fbuild.fmap.pass)(value))
end
---
-- Emits to parent struct value definition
---
function fbuild.emitParentStructValue(name, value, fmap)
p.x("^%s %s %s", name, "+", iif(fmap, fmap, fbuild.fmap.pass)(value))
end
---
-- FMap functions so we can later use them without defining them everywhere
---
fbuild.fmap = { }
local fmap = fbuild.fmap
function fmap.pass(value)
return value
end
function fmap.quote(value)
return ("'%s'"):format(tostring(value))
end
function fmap.variable(value)
return (".%s"):format(tostring(value))
end
function fmap.list(values)
return ("{ %s }"):format(table.concat(values, ", "))
end