-
Notifications
You must be signed in to change notification settings - Fork 9
Expand file tree
/
Copy pathProjectUtil.lua
More file actions
493 lines (390 loc) · 13.9 KB
/
Copy pathProjectUtil.lua
File metadata and controls
493 lines (390 loc) · 13.9 KB
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
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
local UTIL_VERSION = 1
local function Print(msg)
print(msg)
end
local function PrintError(msg)
local debugInfo = debug.getinfo(2)
if not debugInfo then
return
end
local callerName = debugInfo.name
if callerName == nil then
error(": " .. msg, 2)
else
error("[" .. callerName .. "]" .. " : " .. msg, 2)
end
end
--[[ DumpObject(object, [limit], [indent]) Recursively print arbitrary data.
Set limit (default 100) to stanch infinite loops.
Indents tables as [KEY] VALUE, nested tables as [KEY] [KEY]...[KEY] VALUE
Set indent ("") to prefix each line: Mytable [KEY] [KEY]...[KEY] VALUE
--]]
local function DumpObject(object, l, i)
l = (l) or 100; i = i or ""; -- default item limit, indent string
if (l<1) then PrintError "ERROR: Item limit reached."; return l-1 end;
local ts = type(object);
if (ts ~= "table") then Print (i,ts,object); return l-1 end
print (i,ts); -- print "table"
for k,v in pairs(object) do -- print "[KEY] VALUE"
l = DumpObject(v, l, i.."\t["..tostring(k).."]");
if (l < 0) then break end
end
return l
end
if Solution.Util then
if Solution.Util.Version >= UTIL_VERSION then
return
end
end
systemToExecutableExtensionMap =
{
windows = ".exe",
linux = ""
}
systemToDynamicLibExtensionMap =
{
windows = "dll",
linux = "so"
}
Solution.Util = {}
Solution.Util.Version = UTIL_VERSION
Solution.Util.ActiveProject = ""
Solution.Util.Print = Print
Solution.Util.DumpObject = DumpObject
Solution.Util.PrintError = PrintError
Solution.Util.Create = function(identifier, root)
Solution.NumProjects = Solution.NumProjects + 1
Solution.Projects[identifier] =
{
Index = Solution.NumProjects,
Name = identifier,
BaseDir = "",
BinDir = "",
IsRoot = root
}
return Solution.Projects[identifier]
end
Solution.Util.CreateModuleTable = function(name, dependencies)
local module = { Name = name }
module.NameLow = string.lower(module.Name)
module.Path = path.getabsolute(module.Name .. "/", Solution.Projects.Current.ModulesDir)
module.Dependencies = dependencies
return module
end
Solution.Util.CreateDepTable = function(name, dependencies)
local dependency = { Name = name }
dependency.NameLow = string.lower(dependency.Name)
dependency.Path = path.getabsolute(dependency.NameLow .. "/", Solution.Projects.Current.DependencyDir)
dependency.Dependencies = dependencies
return dependency
end
Solution.Util.GetDepTable = function(depName)
local depInternalName = "Dependency-" .. depName
local dep = _G[depInternalName]
if (dep == nil) then
Solution.Util.PrintError("Tried to fetch undeclared dependency '" .. depName .. "'")
end
return dep
end
-- Cache a value inside the dep table
Solution.Util.SetDepCache = function(depName, key, data)
local dep = Solution.Util.GetDepTable(depName)
dep.Cache[key] = data
end
-- Retrieve a cached value from the dep table, or return nil if not cached
Solution.Util.GetDepCache = function(depName, key)
local dep = Solution.Util.GetDepTable(depName)
if dep.Cache[key] then
return dep.Cache[key]
end
return nil
end
Solution.Util.IncludeSubmodule = function(name, rootDir, binDir)
local submoduleRootDir = path.getabsolute("Submodules/".. name .. "/", rootDir)
local submoduleBuildDir = path.getabsolute("Build/".. name .. "/", rootDir)
local submodulePremakeFile = path.getabsolute("premake5.lua", submoduleRootDir)
include(submodulePremakeFile)
Solution.Projects[name]:Init(submoduleRootDir, submoduleBuildDir, binDir)
end
Solution.Util.CreateProject = function(name, projectType, binDir, dependencies, callback)
local internalName = "Project-" .. name
if _G[internalName] ~= nil then
Solution.Util.PrintError("Project with name '" .. name .. "' already exists")
end
Solution.Util.Print("Creating Project '" .. name .. "'")
Solution.Util.ActiveProject = name
local projectTable = { }
projectTable.deps = {}
-- Default Args here --
dependencies = dependencies or {}
local dependencyNameToIndex = { }
local resolvedDependencies = { }
local needToResolve = true
for _, v in ipairs(dependencies) do
projectTable.deps[v] =
{
deps = {}
}
local resolvedDep =
{
name = v,
mustComeAfter = 0,
parent = projectTable.deps[v]
}
local depIndex = #resolvedDependencies + 1
table.insert(resolvedDependencies, resolvedDep)
dependencyNameToIndex[v] = depIndex
end
local numResolvedDependencies = 1
while needToResolve do
local numAddedDependencies = 0
local numDependenciesToResolve = #resolvedDependencies
for i = numResolvedDependencies, numDependenciesToResolve, 1 do
local v = resolvedDependencies[i]
local depInternalName = "Dependency-" .. v.name
local dep = _G[depInternalName]
if (dep == nil) then
Solution.Util.PrintError("'" .. name .. "' use undeclared dependency '" .. v.name .. "'")
end
if dep.Dependencies then
local deps = nil
if type(dep.Dependencies) == "function" then
deps = dep.Dependencies()
elseif type(dep.Dependencies) == "table" or type(dep.Dependencies) == "string" then
deps = dep.Dependencies
end
if deps then
for _, newDep in ipairs(deps) do
if not dependencyNameToIndex[newDep] then
v.parent.deps[newDep] =
{
deps = {}
}
local resolvedDep =
{
name = newDep,
mustComeAfter = dependencyNameToIndex[v.name],
parent = v.parent.deps[newDep]
}
local depIndex = #resolvedDependencies + 1
table.insert(resolvedDependencies, resolvedDep)
dependencyNameToIndex[newDep] = depIndex
numAddedDependencies = numAddedDependencies + 1
else
local depIndex = dependencyNameToIndex[newDep]
resolvedDependencies[depIndex].mustComeAfter = dependencyNameToIndex[v.name]
end
end
end
end
end
numDependenciesToResolve = numResolvedDependencies
needToResolve = numAddedDependencies > 0
end
table.sort(resolvedDependencies, function(a, b)
return a.mustComeAfter < b.mustComeAfter
end)
project (name)
kind (projectType)
targetdir (binDir .. "/%{cfg.buildcfg}")
characterset ("ASCII")
editandcontinue "Off"
filter "configurations:Debug"
runtime "Debug"
symbols "On"
defines { "_DEBUG", "NC_DEBUG" }
filter "configurations:RelDebug"
runtime "Release"
symbols "On"
optimize "On"
defines { "NDEBUG", "NC_RELEASE"}
filter "configurations:Release"
runtime "Release"
symbols "Off"
optimize "Full"
defines { "NDEBUG", "NC_RELEASE"}
filter "platforms:Win64"
system "Windows"
architecture "x86_64"
defines { "WIN32", "WINDOWS", "_WIN32_WINNT=0x0601" }
filter { }
for _, v in ipairs(resolvedDependencies) do
local depInternalName = "Dependency-" .. v.name
if (_G[depInternalName].Callback ~= nil) then
_G[depInternalName].Callback()
filter {}
end
end
local isMSVC = BuildSettings:Get("Using MSVC")
local multithreadedCompilation = BuildSettings:Get("Multithreaded Compilation")
local multithreadedCoreCount = BuildSettings:Get("Multithreaded Core Count")
if multithreadedCompilation then
if isMSVC then
local cores = multithreadedCoreCount or 0
if cores > 0 then
buildoptions { "/MP" .. tostring(cores) }
else
buildoptions { "/MP" }
end
end
end
_G[internalName] = projectTable
if callback then
callback()
end
vpaths {}
end
Solution.Util.CreateStaticLib = function(name, binDir, dependencies, callback)
Solution.Util.CreateProject(name, "StaticLib", binDir, dependencies, callback)
end
Solution.Util.CreateDynamicLib = function(name, binDir, dependencies, callback)
Solution.Util.CreateProject(name, "SharedLib", binDir, dependencies, callback)
end
Solution.Util.CreateConsoleApp = function(name, binDir, dependencies, callback)
Solution.Util.CreateProject(name, "ConsoleApp", binDir, dependencies, callback)
end
Solution.Util.CreateDep = function(name, dependencies, callback)
local internalName = "Dependency-" .. name
if _G[internalName] ~= nil then
Solution.Util.PrintError("Dependency with name '" .. name .. "' already exists")
end
Solution.Util.Print("Creating Dependency '" .. name .. "'")
-- Default Args here --
dependencies = dependencies or {}
if type(callback) ~= "function" then
Solution.Util.PrintError("Dependency '" .. name .. "' specified incorrect callback parameter type. Type must be 'function'")
end
local dependencyTable =
{
Callback = callback,
Dependencies = dependencies,
Cache = {}
}
_G[internalName] = dependencyTable
end
Solution.Util.SetLanguage = function(lang)
if lang ~= "C" and lang ~= "C++" then
Solution.Util.PrintError("Attempted to set unsupported language '" .. lang .. "' for project '" .. Solution.Util.ActiveProject .. "'. Supported Languages ('C', 'C++')")
end
language (lang)
end
Solution.Util.SetCppDialect = function(dialect)
local cppVersion = dialect
if cppVersion == nil or cppVersion == "" then
cppVersion = "C++20"
else
local version = tonumber(cppVersion)
if version ~= nil then
cppVersion = tostring(version)
end
if cppVersion == "03" then
cppVersion = "C++03"
elseif cppVersion == "11" then
cppVersion = "C++11"
elseif cppVersion == "14" then
cppVersion = "C++14"
elseif cppVersion == "17" then
cppVersion = "C++17"
elseif cppVersion == "20" then
cppVersion = "C++20"
elseif cppVersion == "23" then
cppVersion = "C++23"
end
if not cppVersion:find("^C++") then
cppVersion = "C++20"
end
end
cppdialect (cppVersion)
end
Solution.Util.SetFiles = function(filesToAdd)
files (filesToAdd)
end
Solution.Util.SetIncludes = function(includesToAdd)
includedirs (includesToAdd)
end
Solution.Util.SetDefines = function(definesToAdd)
defines (definesToAdd)
end
Solution.Util.SetLinks = function(linksToAdd)
links (linksToAdd)
end
Solution.Util.SetLibDirs = function(dirsToAdd)
libdirs (dirsToAdd)
end
Solution.Util.GetFilesForCpp = function(basePath)
local files =
{
(basePath .. "/**.h"),
(basePath .. "/**.hpp"),
(basePath .. "/**.c"),
(basePath .. "/**.cpp"),
(basePath .. "/**.natvis"),
}
return files
end
Solution.Util.SetGroup = function(name)
local currentProject = Solution.Projects.Current
local projectIdentifier = currentProject.Index .. ". " .. currentProject.Name
if not name or name == "" then
Solution.ActiveGroup = projectIdentifier
else
Solution.ActiveGroup = projectIdentifier .. "/" .. name
end
group (Solution.ActiveGroup)
end
Solution.Util.SetGroupRaw = function(name)
Solution.ActiveGroup = name
group (Solution.ActiveGroup)
end
Solution.Util.SetFilter = function(value, callback)
filter(value)
if callback then
callback()
Solution.Util.ClearFilter()
end
end
Solution.Util.ClearFilter = function()
filter { }
end
Solution.Util.MergeIntoTable = function(t, x)
for _, v in pairs(x) do
table.insert(t, v)
end
end
if InitBuildSettings == nil then
function InitBuildSettings(silentFailOnDuplicateSetting)
if BuildSettings == nil then
BuildSettings = { }
BuildSettings.Has = function(settings, name)
return settings[name] ~= nil
end
BuildSettings.Add = function(settings, name, value)
local silentFail = settings:ShouldSilentFailOnDuplicateSetting()
if settings:Has(name) then
if silentFail then
return
end
Solution.Util.PrintError("Tried to call BuildSettings:Add with name '" .. name .. '" but a setting with that name already exists')
end
settings[name] = value
end
BuildSettings.Get = function(settings, name)
if not settings:Has(name) then
return nil
end
return settings[name]
end
BuildSettings.SetSilentFailOnDuplicateSetting = function(settings, silentFailOnDuplicateSetting)
BuildSettings["SilentFailOnDuplicateSetting"] = silentFailOnDuplicateSetting
end
BuildSettings.ShouldSilentFailOnDuplicateSetting = function(settings)
local option = BuildSettings["SilentFailOnDuplicateSetting"]
if option == nil then
return false
end
return option
end
end
BuildSettings:SetSilentFailOnDuplicateSetting(silentFailOnDuplicateSetting)
end
end