-
Notifications
You must be signed in to change notification settings - Fork 2
/
fastbuild_solution.lua
462 lines (333 loc) · 14.6 KB
/
fastbuild_solution.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
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
--
-- actions/fastbuild/fastbuild.lua
-- Extend the existing exporters with support for FASTBuild
-- Copyright (c) 2017-2017 Daniel Penkała
--
local p = premake
p.fastbuild.fbsln = { }
local tree = p.tree
local project = p.project
local workspace = p.workspace
local fbuild = p.fastbuild
local dependency_resolver = fbuild.dependency_resolver
local fastbuild = p.fastbuild
local fbuild = fastbuild
local fbsln = fastbuild.fbsln
local f = fastbuild.utils
local m = p.fastbuild.fbsln
---
-- Add namespace for element definition lists for p.callArray()
---
m.elements = {}
--
-- Return the list of sections contained in the solution.
-- TODO: Get rid of this when the MonoDevelop module no longer needs it
--
m.elements.workspace = function(wks)
return {
-- General
m.header,
m.globals,
m.settings,
m.compilers,
-- Projects
m.allStructs,
m.includeProjects,
m.allTargets,
iif(_OPTIONS["fb-vstudio"], m.emitSolutionFunc, fbuild.fmap.pass)
}
end
---
-- Define the FASTBuild solution generation function
---
function m.generate(wks)
p.callArray(m.elements.workspace, wks)
end
---
-- Prints the workspace file header
---
function m.header(wks)
f.section("FASTBuild Solution: %s", wks.name)
end
---
-- Tries to find the defined compiler file definitions and includes them to the workspace
---
function m.compilers(wks)
p.x("\n// Available compilers ")
p.x("//-----")
-- A list of all available compilers
local available_compilers = { }
-- Iterate over each compiler and save the architecture | system pair
table.foreachi(wks.fbcompilers, function(compiler)
local name = compiler.name
local system = compiler.system
local architecture = compiler.architecture
local toolset = compiler.toolset
local path = compiler.path
assert(name, "The given compiler does not have a name?")
assert(system, "Compiler %s does not have any target system defined!", name)
assert(architecture, "Compiler %s does not have any architecture defined!", name)
assert(toolset, "Compiler %s does not any toolset defined!", name)
assert(path, "Where can I find the given compiler? %s [%s]", name, platform)
-- Include the compiler file and save it in the list
fbuild.include(fbuild.path(wks, path))
-- Save the system | architecture pair
local target_platform = system .. "|" .. architecture .. "|" .. toolset
assert(not available_compilers[target_platform], "Compiler for target platform %s already exists", target_platform)
available_compilers[target_platform] = true
end)
-- Check if we have a compiler for each workspace configuration (these are required to be present)
for config in workspace.eachconfig(wks) do
local target_platform = fbuild.targetCompilerPlatform(config)
local is_compiler_present = available_compilers[target_platform]
assert(is_compiler_present, ("No compiler found for target platform %s!"):format(target_platform))
end
-- Save the compiler list for later use
wks.compilers = available_compilers
end
---
-- Write global values info the solution file
---
m.elements.globals = function(wks)
return {
fbuild.call(fbuild.emitStructValue, "WorkspaceLocation", wks.location, false, fbuild.fmap.quote)
}
end
function m.globals(wks)
p.x("\n// FASTBuild global values ")
p.x("//-----")
p.callArray(m.elements.globals, wks)
end
---
-- Write settings info the solution file
---
m.elements.settings = function(wks)
return {
m.settingCachePath
}
end
function m.settings(wks)
p.x("\n// FASTBuild settings ")
p.x("//-----")
p.x("Settings") -- The 'Settings' element in fastbuild is quite special, it's not a function call nor a struct so we 'emulate' it with a scope
fbuild.emitScope(m.elements.settings)
end
function m.settingCachePath(wks)
local cache_path = _OPTIONS["fb-cache-path"]
if cache_path and #cache_path > 0 then
p.x(".CachePath = '%s'", path.translate(cache_path))
end
end
---
-- Emit 'All' structs which will hold all targets to be compiled when using an 'All' target
---
function m.allStructs(wks)
p.x("\n// All structures (used to create 'All' alliases)")
p.x("//-----")
for cfg in p.workspace.eachconfig(wks) do
p.x(".AllTargets_%s = { }", fbuild.solutionConfig(cfg))
end
end
---
-- Write out the list of projects and groups contained by the solution.
---
function m.includeProjects(wks)
p.x("\n// Included projects ")
p.x("//-----")
-- Resolves project dependencies and calls the callback for each project in a ordered way
dependency_resolver.eachproject(wks, function(prj)
local location = fbuild.path(wks, p.filename(prj, ".prj.bff"))
fbuild.include(location)
end)
end
function m.allTargets(wks)
p.x("\n// All targets (for default configurations) ")
p.x("//-----")
for cfg in workspace.eachconfig(wks) do
fbuild.emitFunction("Alias", fbuild.targetName2("all", cfg), {
fbuild.call(p.x, ".Targets = .AllTargets_%s", fbuild.solutionConfig(cfg))
})
end
end
---------------------------------------------------------------------------
--
-- VSSolution function call
--
---------------------------------------------------------------------------
m.elements.vstudio_filters = function(wks)
return {
}
end
m.elements.vstudio = function(wks)
return {
m.solutionOutputLocation,
m.solutionBuildProject,
m.solutionFolders,
m.solutionProjects,
m.solutionDependencies,
}
end
---
-- Emits an additional "All" project which is used to build executable project with the f5 key, however it will always perform a full solution build
---
function m.emitAllProject(wks)
local function emitValue(name, value, fmap, ...)
return fbuild.call(fbuild.emitStructValue, name, value:format(...), false, fmap)
end
fbuild.emitFunction("VCXProject", fbuild._targetName("All", nil, "vcxproj"), {
emitValue("ProjectOutput", "..\\build\\fastbuild\\All.vcxproj", fbuild.fmap.quote),
emitValue("ProjectConfigs", ".SolutionConfigs"),
emitValue("ProjectBuildCommand", "cd \"$(SolutionDir)\" & fbuild -config %s.wks.bff -ide -monitor -dist -cache all-$(Platform)-$(Configuration)", fbuild.fmap.quote, wks.filename),
emitValue("ProjectRebuildCommand", "cd \"$(SolutionDir)\" & fbuild -config %s.wks.bff -ide -monitor -dist -cache -clean all-$(Platform)-$(Configuration)", fbuild.fmap.quote, wks.filename),
emitValue("ProjectCleanCommand", "cd \"$(SolutionDir)\" & fbuild -config %s.wks.bff -ide -monitor -dist -cache -clean", fbuild.fmap.quote, wks.filename),
emitValue("PlatformToolset", fbuild.vstudioProjectToolset(wks), fbuild.fmap.quote)
})
end
---
-- Emits an additional "RebuildSln" project which is used to rebuild the FastBuild solution
---
function m.emitRebuildProject(wks)
local function emitValue(name, value, fmap, ...)
return fbuild.call(fbuild.emitStructValue, name, value:format(...), false, fmap)
end
fbuild.emitFunction("VCXProject", fbuild._targetName("Rebuild", nil, "vcxproj"), {
emitValue("ProjectOutput", "..\\build\\fastbuild\\Rebuild.vcxproj", fbuild.fmap.quote),
emitValue("ProjectConfigs", ".SolutionConfigs"),
emitValue("ProjectBuildCommand", "cd \"$(SolutionDir)\" & fbuild -config %s.wks.bff %s-sln", fbuild.fmap.quote, wks.filename, wks.name),
emitValue("PlatformToolset", fbuild.vstudioProjectToolset(wks), fbuild.fmap.quote)
})
end
---
-- Emits the VSSolution function call which allows to create a FASTBuild compatible solution
---
function m.emitSolutionFunc(wks)
p.x("\n// VSSolution definiton ")
p.x("//-----")
m.emitSolutionConfigs(wks)
m.emitAllProject(wks)
m.emitRebuildProject(wks)
m.emitSolutionProjectFolders(wks)
fbuild.emitFunction("VSSolution", fbuild._targetName(wks, nil, "sln"), m.elements.vstudio, nil, wks)
end
---
-- Emits the solution output location
---
function m.solutionOutputLocation(wks)
local filename = wks.name .. "_fb.sln"
-- Emit the struct value
fbuild.emitStructValue("SolutionOutput", filename, false, fbuild.fmap.quote)
end
---
-- Emits the solution active build projecty (f5 enabled project)
---
function m.solutionBuildProject(wks)
local buildprj = fbuild._targetName("All", nil, "vcxproj")
-- Emit the active build project (f5 enabled project)
fbuild.emitStructValue("SolutionBuildProject", buildprj, false, fbuild.fmap.quote)
end
---
-- Emits all projects which the solution should open
---
function m.solutionProjects(wks)
local function emitAdditionalProjects()
p.x("%s,", fbuild.fmap.quote(fbuild._targetName("All", nil, "vcxproj")))
p.x("%s", fbuild.fmap.quote(fbuild._targetName("Rebuild", nil, "vcxproj")))
end
-- Emits all solution projects in dependent order
fbuild.emitList("SolutionProjects", { fbuild.emitListItems, emitAdditionalProjects }, nil, dependency_resolver.allprojects(wks), function(prj)
if fbuild.checkCompilers(prj) then
local target_name = fbuild._targetName(prj.name, nil, "vcxproj")
return fbuild.fmap.quote(target_name)
end
end)
end
---
-- Emits the solution folders setting for the given solution
---
function m.solutionFolders(wks)
fbuild.emitStructValue("SolutionFolders", fbuild.structName(wks, nil, "SolutionFolders"), false, fbuild.fmap.variable)
end
---
-- Emits dependency declartions across solution projects
---
function m.solutionDependencies(wks)
-- Helps to emit the list within the emited struct
local function emitExecutableProjectList(wks)
fbuild.emitList("Projects", { fbuild.emitListItems }, nil, dependency_resolver.allprojects(wks), function(prj)
-- Check it it's an 'App' project
if prj.kind == p.CONSOLEAPP or prj.kind == p.WINDOWEDAPP then
local target_name = fbuild._targetName(prj.name, nil, "vcxproj")
return fbuild.fmap.quote(target_name)
end
end)
end
-- Helps to emit the dependencies list of the given 'App' projects
local function emitExecutableDependencies(wks)
local all_target = fbuild._targetName("All", nil, "vcxproj")
fbuild.emitStructValue("Dependencies", { fbuild.fmap.quote(all_target) }, false, fbuild.fmap.list)
end
p.x("// Solution project dependencies")
-- First emit the structure which will define dependencies to the 'All' project (we care only about 'executable' kinds)
fbuild.emitStruct("MainSolutionDependency", { emitExecutableProjectList, emitExecutableDependencies }, {
fbuild.call(fbuild.emitStructValue, "SolutionDependencies", { ".MainSolutionDependency" }, false, fbuild.fmap.list)
}, wks)
end
---
-- Emits the solution configurations
---
function m.emitSolutionConfigs(wks)
p.x("// List of all configurations")
fbuild.emitList("Configurations", { fbuild.emitListItems }, nil, wks.configurations, fbuild.fmap.quote)
p.x("// List of all platforms")
fbuild.emitList("Platforms", { fbuild.emitListItems }, nil, wks.platforms, fbuild.fmap.quote)
p.x("// The actual solution configuration list")
fbuild.emitStructValue("SolutionConfigs", "{ }")
p.x("")
local call = fbuild.call
fbuild.emitForLoop("Platform", "Platforms",
{
call(fbuild.emitForLoop, "Config", "Configurations",
{
call(fbuild.emitStruct, "Configuration", {
call(fbuild.emitStructValue, "Platform", "$Platform$", false, fbuild.fmap.quote),
call(fbuild.emitStructValue, "Config", "$Config$", false, fbuild.fmap.quote)
}),
call(fbuild.emitParentStructValue, "SolutionConfigs", ".Configuration")
})
})
end
function m.emitSolutionProjectFolders(wks)
for _, group in ipairs(dependency_resolver.allgroups(wks)) do
if #group.projects > 0 then
local struct_name = ("%s_SolutionFolder"):format(group.name:gsub("/", "_"))
if group.name ~= "" then
fbuild.emitStruct(struct_name, {
fbuild.call(fbuild.emitStructValue, "Path", group.name, false, fbuild.fmap.quote),
fbuild.call(fbuild.emitList, "Projects", { fbuild.emitListItems }, nil, group.projects, function(prj)
if fbuild.checkCompilers(prj) then
return fbuild.fmap.quote(fbuild._targetName(prj.name, nil, "vcxproj"))
end
end)
})
end
end
end
-- Emit solution folder for FBuild special generated projects
fbuild.emitStruct(fbuild.structName(wks, nil, "FBuildSolutionFolder"), {
fbuild.call(fbuild.emitStructValue, "Path", "_fbuild", false, fbuild.fmap.quote),
fbuild.call(fbuild.emitList, "Projects", { fbuild.emitListItems }, nil, { "All", "Rebuild" }, function(name)
return fbuild.fmap.quote(fbuild._targetName(name, nil, "vcxproj"))
end)
})
-- Emit the SolutionFolders list
local inner = {
fbuild.emitListItems,
fbuild.call(p.x, ".%s", fbuild.structName(wks, nil, "FBuildSolutionFolder"))
}
fbuild.emitList(fbuild.structName(wks, nil, "SolutionFolders"), inner, nil, dependency_resolver.allgroups(wks), function(group)
if #group.projects > 0 and group.name ~= "" then
local struct_name = (".%s_SolutionFolder"):format(group.name:gsub("/", "_"))
return struct_name
end
end)
end