-
Notifications
You must be signed in to change notification settings - Fork 8
/
Copy pathbuild.lua
706 lines (597 loc) · 17.9 KB
/
build.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
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
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
--
-- Copyright 2023 Milos Tosic. All rights reserved.
-- License: http://www.opensource.org/licenses/BSD-2-Clause
--
local params = { ... }
local RTM_ADD_SEARCH_PATH = params[1] or nil
--------------------------------------------------------
-- directories
--------------------------------------------------------
function script_dir()
return debug.getinfo(2, "S").source:sub(2):match("(.*[/\\])")
end
RTM_SCRIPTS_DIR = script_dir()
RTM_ROOT_DIR = path.getabsolute(RTM_SCRIPTS_DIR .. "../") .. "/" -- project root
RTM_BUILD_DIR = RTM_ROOT_DIR .. ".build/" -- temp build files
RTM_LOCATION_PATH = "" -- solution/makefile/etc.
RTM_PROJECT_DIRS = {}
newoption {
trigger = "project-dirs",
description = "Specify file with project search paths table (has to be named RTM_PROJECT_DIR_PATHS)"
}
local customProjectDirs = _OPTIONS["project-dirs"]
if (customProjectDirs == nil) then
customProjectDirs = script_dir() .. "rtm_paths.lua"
end
-- could be a relative path
if (not os.isfile(customProjectDirs)) then
customProjectDirs = _WORKING_DIR .. '/' .. customProjectDirs
end
if (os.isfile(customProjectDirs)) then
dofile (customProjectDirs)
for _,pathTable in ipairs(RTM_PROJECT_DIR_PATHS) do
local relative = pathTable[1]
local path = pathTable[2]
if relative then
path = RTM_ROOT_DIR .. path
end
if os.isdir(path) then
table.insert(RTM_PROJECT_DIRS, path)
end
end
else
print("ERROR: Custom project directories script not found at " .. customProjectDirs)
os.exit()
return
end
-- add extra search path, passed on from the invoking script
if RTM_ADD_SEARCH_PATH ~= nil then
RTM_ADD_SEARCH_PATH = path.getabsolute(RTM_ADD_SEARCH_PATH.. "../../../") .. "/"
if os.isdir(RTM_ADD_SEARCH_PATH) then
table.insert(RTM_PROJECT_DIRS, RTM_ADD_SEARCH_PATH)
else
print("Warning: path does not exist: " .. RTM_ADD_SEARCH_PATH)
end
end
dofile (RTM_SCRIPTS_DIR .. "toolchain.lua")
if _ACTION == "clean" then
rmdir(RTM_BUILD_DIR)
os.exit()
return
end
--------------------------------------------------------
-- compiler flags
--------------------------------------------------------
Flags_ThirdParty = { "StaticRuntime", "NoEditAndContinue", "NoPCH", "MinimumWarnings" }
Flags_Libraries = { "StaticRuntime", "NoEditAndContinue", "NoRTTI", "ExtraWarnings", "NoExceptions" }
Flags_Tests = { "StaticRuntime", "NoEditAndContinue", "NoRTTI", "ExtraWarnings" }
Flags_Cmd = { "StaticRuntime", "NoEditAndContinue", "NoRTTI", "ExtraWarnings", "NoExceptions" }
Flags_QtTool = { "StaticRuntime", "NoEditAndContinue", "NoRTTI", "ExtraWarnings" }
ExtraFlags = {}
ExtraFlags["debug"] = { "Symbols" }
ExtraFlags["release"] = { "NoFramePointer", "OptimizeSpeed", "NoBufferSecurityCheck", "Symbols" }
ExtraFlags["retail"] = { "NoFramePointer", "OptimizeSpeed", "NoBufferSecurityCheck" }
ExtraDefines = {}
ExtraDefines["debug"] = { "RTM_DEBUG_BUILD", "_DEBUG", "DEBUG" }
ExtraDefines["release"] = { "RTM_RELEASE_BUILD", "NDEBUG" }
ExtraDefines["retail"] = { "RTM_RETAIL_BUILD", "NDEBUG", "RETAIL" }
--------------------------------------------------------
-- utility functions to check for target compiler
--------------------------------------------------------
function actionUsesGCC()
return ("gmake" == _ACTION or "codelite" == _ACTION or "codeblocks" == _ACTION or "xcode3" == _ACTION)
end
function actionUsesMSVC()
return (_ACTION ~= nil and _ACTION:find("vs"))
end
function actionUsesXcode()
return (_ACTION ~= nil and _ACTION:find("xcode"))
end
--------------------------------------------------------
-- fixup for precompiled header path
--------------------------------------------------------
function getPCHPath(_path, _projectName, _projType)
local fullPath = _path .. _projectName .. "_pch.h"
if os.isfile(fullPath) == false then
return nil
end
local retPath = _projectName .. "_pch.h"
if actionUsesGCC() then
retPath = _path .. retPath
end
return retPath
end
function addPCH(_path, _name)
local PCH = getPCHPath(_path, _name)
if PCH ~= nil and not os.is("macosx") then
pchheader (PCH)
pchsource (_path .. _name .. "_pch.cpp")
end
end
--------------------------------------------------------
-- Library types
--------------------------------------------------------
Lib = {
Runtime = {},
Tool = {},
Game = {}
}
dofile (RTM_SCRIPTS_DIR .. "project_3rd.lua")
dofile (RTM_SCRIPTS_DIR .. "project_cmdtool.lua")
dofile (RTM_SCRIPTS_DIR .. "project_game.lua")
dofile (RTM_SCRIPTS_DIR .. "project_lib.lua")
dofile (RTM_SCRIPTS_DIR .. "project_lib_sample.lua")
dofile (RTM_SCRIPTS_DIR .. "project_lib_test.lua")
dofile (RTM_SCRIPTS_DIR .. "project_lib_tool.lua")
dofile (RTM_SCRIPTS_DIR .. "project_qt.lua")
--------------------------------------------------------
-- helper functions
--------------------------------------------------------
function mergeTwoTables(_table1, _table2)
table1 = table1 or {}
table2 = table2 or {}
local mergedTable = {}
local hash = {}
for _,v1 in ipairs(_table1) do
if (not hash[v1]) then
table.insert(mergedTable, v1)
hash[v1] = true
end
end
for _,v2 in ipairs(_table2) do
if (not hash[v2]) then
table.insert(mergedTable, v2)
hash[v2] = true
end
end
return mergedTable
end
function mergeTables(_table1, _table2, _table3, _table4, _table5, _table6, _table7, _table8)
_table1 = _table1 or {} _table2 = _table2 or {}
_table3 = _table3 or {} _table4 = _table4 or {}
_table5 = _table5 or {} _table6 = _table6 or {}
_table7 = _table7 or {} _table8 = _table8 or {}
local t1 = mergeTwoTables(_table1, _table2)
local t2 = mergeTwoTables(_table3, _table4)
local t3 = mergeTwoTables(_table5, _table6)
local t4 = mergeTwoTables(_table7, _table8)
return mergeTwoTables(mergeTwoTables(t1, t2), mergeTwoTables(t3, t4))
end
ProjectLoad = {
LoadOnly = {},
LoadAndAdd = {}
}
g_projectIsLoaded = {}
g_projectIsAdded = {}
g_projectLoadCalled = {}
g_fileIsLoaded = {}
function getProjectDesc(_name)
local descFn = _G["projectDescription_" .. _name]
if descFn then
return descFn()
end
return nil
end
ProjectPath = {
Dir = {},
Root = {}
}
function istable(_var)
return type(_var) == "table"
end
function getProjectBaseName(_projectName)
if istable(_projectName) then
for _,name in ipairs(_projectName) do
return name
end
end
return _projectName
end
function buildifyName(_name)
local nameWithUnderscore = string.gsub(_name, "-", "_")
local nameNoDot = string.gsub(nameWithUnderscore, "%.", "_")
return nameNoDot
end
function getProjectFullName(_projectName)
if istable(_projectName) then
local ret = nil
for _,name in ipairs(_projectName) do
if ret == nil then ret = name else ret = ret .. "_" .. name end
end
return buildifyName(ret)
else
return buildifyName(_projectName)
end
end
g_3rdPartyProjectPathCache = {}
function find3rdPartyProject(_name)
if g_3rdPartyProjectPathCache[_name] ~= nil then
return g_3rdPartyProjectPathCache[_name]
end
if istable(_name) then return nil end
local name = getProjectBaseName(_name)
for _,dir in ipairs(RTM_PROJECT_DIRS) do
local libDir = dir .. "3rd/" .. name
if os.isdir(libDir) then
local projectPath = libDir .. "/"
g_3rdPartyProjectPathCache[_name] = projectPath
return projectPath
end
local subDirs = os.matchdirs(dir .. "*")
for _,subDir in ipairs(subDirs) do
local subPath = subDir .. "/3rd/" .. name
if os.isdir(subPath) then
g_3rdPartyProjectPathCache[_name] = subPath .. "/"
return subPath .. "/"
end
end
end
return nil
end
g_projectPathCache = {}
function getProjectPath(_name, _pathType)
local name = getProjectBaseName(_name)
local full_name = getProjectFullName(_name)
_pathType = _pathType or ProjectPath.Dir
local key
if _pathType == ProjectPath.Dir then
key = name .. "_dir"
else
key = name .. "_root"
end
if g_projectPathCache[key] ~= nil then
return g_projectPathCache[key]
end
local projectPath
for _,dir in ipairs(RTM_PROJECT_DIRS) do
local libDir = dir .. name
if os.isdir(libDir) then
if _pathType == ProjectPath.Dir then
g_projectPathCache[key] = path.getabsolute(libDir .. "/");
return path.getabsolute(libDir .. "/")
else
g_projectPathCache[key] = path.getabsolute(dir)
return path.getabsolute(dir)
end
end
end
projectPath = find3rdPartyProject(_name)
if projectPath ~= nil then
if _pathType == ProjectPath.Root then
g_projectPathCache[key] = path.getabsolute(projectPath .. "../") .. "/"
return path.getabsolute(projectPath .. "../") .. "/"
else
g_projectPathCache[key] = path.getabsolute(projectPath)
return path.getabsolute(projectPath)
end
end
for _,dir in ipairs(RTM_PROJECT_DIRS) do
local subDirs = os.matchdirs(dir .. "*")
for _,subDir in ipairs(subDirs) do
local pth = path.getabsolute(subDir .. "/tools/" .. name)
if file_isdir(pth) then
g_projectPathCache[key] = pth
return pth
end
pth = path.getabsolute(subDir .. "/3rd/" .. name .. "/")
if file_isdir(pth) then
g_projectPathCache[key] = pth
return pth
end
end
end
return nil
end
function addIncludePath(_name, _path)
assert(_path ~= nil)
if string.len(_path) == 0 then return end
if os.isdir(_path) then includedirs { _path } end
end
function addInclude(_name, _projectName)
local basename = getProjectBaseName(_projectName)
local fullname = getProjectFullName(_projectName)
local projectParentDir = getProjectPath(_projectName, ProjectPath.Root)
if projectParentDir == nil then return false end
-- search for it..
addIncludePath(_name, projectParentDir)
addIncludePath(_name, projectParentDir .. "/" .. basename .. "/include")
addIncludePath(_name, projectParentDir .. "/" .. basename .. "/inc")
addIncludePath(_name, projectParentDir .. "/" .. basename)
end
g_projectAddCache = {}
function addProject(_name)
local name = getProjectFullName(_name)
if g_projectAddCache[_name] ~= nil then
return
end
g_projectAddCache[_name] = true
local deps = getProjectDependencies(_name)
for _,dep in ipairs(deps) do
addProject(dep)
end
if g_projectIsLoaded[name] == nil then
local nameBuild = buildifyName(name)
if _G["projectAdd_" .. nameBuild] ~= nil then -- prebuilt libs have no projects
_G["projectAdd_" .. nameBuild]()
g_projectIsLoaded[name] = true
end
end
if g_projectIsLoaded[name] == nil then
if find3rdPartyProject(name) == nil then
-- some 'missing' dependencies are actually system libraries, for example X11, GL, etc.
-- if we cannot find it on OS level - warn user
if os.findlib(name) == nil then
print('WARNING: Dependency not found - ' .. name .. ' - treating it as a system library')
end
end
end
local projectDir = getProjectPath(_name, ProjectPath.Dir)
if projectDir ~= nil then
local toolDirs = os.matchdirs(projectDir .. "/tools/*")
for _,toolPath in ipairs(toolDirs) do
local tool = path.getbasename(toolPath)
local scriptPath = toolPath .. "/genie/" .. tool .. ".lua"
if file_exists(scriptPath) then
dofile(scriptPath)
if _G["projectAdd_" .. tool] ~= nil then
_G["projectAdd_" .. tool](toolPath)
g_projectIsLoaded[tool] = true
end
end
end
end
end
function configDependency(dependency)
local dependency = getProjectFullName(dependency)
if _G["projectDependencyConfig_" .. dependency] ~= nil then -- prebuilt libs have no projects
print(dependency .. " " )
return _G["projectDependencyConfig_" .. dependency]()
end
end
function loadProject(_projectName, _load)
local name = getProjectBaseName(_projectName)
if name ~= nil then
local prjFile = ""
for _,path in ipairs(RTM_PROJECT_DIRS) do
prjFile = path .. name .. ".lua"
if os.isfile(prjFile) then
if g_fileIsLoaded[prjFile] == nil then
g_fileIsLoaded[prjFile] = true
local projectName = find3rdPartyProject(name);
if projectName ~= nil then
assert(loadfile(prjFile))(projectName)
end
break
end
end
prjFile = path .. name .. "/genie/" .. name .. ".lua"
if os.isfile(prjFile) then
if g_fileIsLoaded[prjFile] == nil then
g_fileIsLoaded[prjFile] = true
dofile(prjFile)
break
end
end
end
end
_load = _load or ProjectLoad.LoadAndAdd
if _load == ProjectLoad.LoadAndAdd then
addProject(_projectName)
end
end
function sortDependencies(a,b)
local depsA = getProjectDependencies(a)
local depsB = getProjectDependencies(b)
return #depsA > #depsB
end
function getProjectDependencies(_name, _additionalDeps)
local fullName = getProjectFullName(_name)
local dep = {}
if _G["projectDependencies_" .. fullName] then
dep = _G["projectDependencies_" .. fullName]()
end
local finalDep = {}
_additionalDeps = _additionalDeps or {}
dep = mergeTables(dep, _additionalDeps)
for _,dependency in ipairs(dep) do
table.insert(finalDep, dependency)
end
for _,dependency in ipairs(finalDep) do
loadProject(dependency, ProjectLoad.LoadOnly)
end
local depNest = {}
for _,d in ipairs(finalDep) do
depNest = mergeTables(depNest, getProjectDependencies(d))
end
finalDep = mergeTables(finalDep, depNest)
if _ACTION == "gmake" then
table.sort(finalDep, sortDependencies)
end
for _,dependency in ipairs(finalDep) do
local full = getProjectFullName(dependency)
if _G["projectDependencyConfig_" .. full] then
dep = _G["projectDependencyConfig_" .. full]()
end
end
return finalDep
end
function addExtraSettingsForExecutable(_name)
local fullProjectName = getProjectFullName(_name)
if _G["projectExtraConfigExecutable_" .. fullProjectName] then
dep = _G["projectExtraConfigExecutable_" .. fullProjectName]()
end
end
-- can be called only ONCE from one project, merge dependencies before calling!!!
function addDependencies(_name, _additionalDeps)
local dependencies = getProjectDependencies(_name, _additionalDeps)
addExtraSettingsForExecutable(_name)
if dependencies ~= nil then
for _,dependency in ipairs(dependencies) do
if dependency ~= nil then
local dependencyFullName = getProjectFullName(dependency)
addExtraSettingsForExecutable(dependencyFullName)
addInclude(_name, dependency)
if _G["projectHeaderOnlyLib_" .. dependencyFullName] == nil then
links { dependencyFullName }
end
end
end
end
if dependencies ~= nil then
for _,dependency in ipairs(dependencies) do
loadProject(dependency)
end
end
end
function addLibProjects(_name)
loadProject(_name)
end
function stripExtension( _path )
local pathFS = _path:gsub("\\","/")
return path.getdirectory(pathFS) .. "/" .. path.getbasename(pathFS)
end
function getToolForHost(_name)
-- sed is special case
if _name == "sed" and os.is("windows") then
return getProjectPath("build") .. "\\tools\\bin\\windows\\sed.exe"
end
local toolPath = path.getabsolute(script_dir() .. "/tools/bin/")
if os.is("windows") then
toolPath = toolPath .. "/windows/" .. _name .. ".exe"
elseif os.is("linux") then
toolPath = toolPath .. "/linux/" .. _name
elseif os.is("osx") then
toolPath = toolPath .. "/darwin/" .. _name
end
return toolPath
end
function flatten(t)
t = t or {}
local tmp = {}
for si,sv in ipairs(t) do
if type( sv ) == "table" then
for _,v in ipairs(sv) do
table.insert(tmp, v)
end
elseif type( sv ) == "string" then
table.insert( tmp, sv )
end
t[si] = nil
end
for _,v in ipairs(tmp) do
table.insert(t, v)
end
end
--
function getProjectTempIncludePath(_project)
mkdir(getSolutionBaseDir() .. "/include/" .. _project)
return getSolutionBaseDir() .. "/include/" .. _project
end
-- read file contents
function file_read(_file)
local f = io.open(_file, "r")
if f == nil then return "" end
local content = f:read("*all")
f:close()
return content
end
-- Check if a file exists
function file_exists(file)
if file == nil then return false end
local ok, err, code = os.rename(file, file)
if not ok then
if code == 13 then -- Permission denied, but it exists
return true
end
end
return ok, err
end
-- Check if a file is a directory
-- "/" works on both Unix and Windows
function file_isdir(path)
return file_exists(path.."/")
end
--
function mkdir(_dirname)
if os.isdir(_dirname) then return end
local dir = _dirname
if os.is("windows") then
dir = string.gsub( _dirname, "([/]+)", "\\" )
else
dir = string.gsub( _dirname, "\\\\", "\\" )
end
if not file_isdir(dir) then
if not os.is("windows") then
os.execute("mkdir -p " .. dir)
else
os.execute("mkdir " .. dir)
end
end
end
--
function recreateDir(_path)
rmdir(_path)
mkdir(_path)
end
--
function file_get_time(filepath)
if windows then
local pipe = io.popen('dir /4/tw "'..filepath..'"')
local output = pipe:read"*a"
pipe:close()
return output:match"\n(%d.-:%S*)"
else
local pipe = io.popen("stat -c %Y testfile")
local last_modified = f:read()
pipe:close()
return last_modified
end
end
--
function file_is_upToDate(outputFileName)
if file_exists(outputFileModTime) and ( inputFileModTime < outputFileModTime ) then
--print( outputFileName.." is up-to-date, not regenerating" )
io.stdout:flush()
return true
else
print( outputFileName .. " is out of date, regenerating" )
end
return false
end
function getFileNameNoExtFromPath( path )
local i = 0
local lastSlash = 0
local lastPeriod = 0
local returnFilename
while true do
i = string.find( path, "/", i+1 )
if i == nil then break end
lastSlash = i
end
i = 0
while true do
i = string.find( path, "%.", i+1 )
if i == nil then break end
lastPeriod = i
end
if lastPeriod < lastSlash then
returnFilename = path:sub( lastSlash + 1 )
else
returnFilename = path:sub( lastSlash + 1, lastPeriod - 1 )
end
return returnFilename
end
function setupCMakeProjectHeaders(srcPath, dstPath, replacePairs)
-- replace cmakedefine with define
-- replace strings with known values
-- remove all instances of '@'
os.copyfile(srcPath, dstPath);
local sedCommand = getToolForHost("sed")
if replacePairs ~= nil then
for _,replacePair in ipairs(replacePairs) do
os.execute(sedCommand .. " -i s/'" .. replacePair[1] .. "'/'" .. replacePair[2] .. "'/g " .. dstPath)
end
end
os.execute(sedCommand .. " -i s/cmakedefine/define/g " .. dstPath)
os.execute(sedCommand .. " -i s/@//g " .. dstPath)
end