-
Notifications
You must be signed in to change notification settings - Fork 48
Expand file tree
/
Copy pathbase.lua
More file actions
587 lines (500 loc) · 15.4 KB
/
base.lua
File metadata and controls
587 lines (500 loc) · 15.4 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
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
-- just install a better name for the functions and tables
ScriptArgs = _bam_scriptargs
IsString = bam_isstring
IsTable = bam_istable
IsOutput = bam_isoutput
Sleep = bam_sleep
--[[@UNITTESTS
err=0 : if bam_isdir("valid_file") then error("") end
err=0 : if not bam_isdir("valid_dir") then error("") end
@END]]--
IsDirectory = bam_isdir
--[[@UNITTESTS
err=0 : if not bam_isfile("valid_file") then error("") end
err=0 : if bam_isfile("valid_dir") then error("") end
@END]]--
IsFile = bam_isfile
--[[@UNITTESTS
err=0 : if not bam_fileexist("valid_file") then error("") end
err=0 : if bam_fileexist("invalid_file") then error("") end
@END]]--
Exist = bam_fileexist
NodeExist = bam_nodeexist
SetFilter = bam_set_filter
AddOutput = bam_add_output
AddSideEffect = bam_add_sideeffect
AddClean = bam_add_clean
SetPriority = bam_set_priority
ModifyPriority = bam_modify_priority
--[[@UNITTESTS
err=0 : if bam_path_isnice("") then error("") end
err=0 : if bam_path_isnice("test/../test") then error("") end
err=0 : if not bam_path_isnice("test/test") then error("") end
@END]]--
--[[@UNITTESTS
err=1 : bam_add_dependency_cpp("missing node")
err=0 : PseudoTarget("fakenode"); bam_add_dependency_cpp("fakenode")
@END]]--
--[[@FUNCTION Hash
Performs a 64-bit DJB2 hash on the string and returns the result has in a string
@END]]--
--[[@UNITTESTS
catch="0000000000001505" : Hash("")
catch="a1e982361190bbc4" : Hash("hello world!")
@END]]--
Hash = bam_hash
--[[@FUNCTION PathHash
Performs a 64-bit DJB2 hash on the string, but does certain character conversions (all lower case on windows) on various systems.
@END]]--
--[[@UNITTESTS
catch="0000000000001505" : PathHash("")
catch="a1e982361190bbc4" : PathHash("hello world!")
@END]]--
PathHash = bam_path_hash
--[[@GROUP Common @END]]--
--[[@FUNCTION CheckVersion
Tells bam what version this script is written for. It will either
make sure that it behaves like that version or print out an error.
{{{{
CheckVersion("0.1.0")
}}}}
@END]]--
function CheckVersion(version)
if version == _bam_version then
else
error("this file for version "..version..".x of bam. you are running ".._bam_version..".x.")
end
end
--[[@FUNCTION Execute(command)
Executes the ^command^ in the shell and returns the error code.
@END]]--
--[[@UNITTESTS
err=0 : Execute("echo")
@END]]--
function Execute(command)
local res,str,code = os.execute(command)
if family == 'windows' then
if not res and code == 0 then
-- if the program returns -1, system() returns -1, but that is what it returns when it set errno, so lua os.execute returns errno, but that is 0. Such a mess, but we really don't wan't to return 0 for failed calls
-- there was some talk about it on the lua mainling list (http://lua-users.org/lists/lua-l/2018-05/msg00045.html), but the latest 5.4 beta has no fixes.
return -1
end
end
return code
end
--[[@FUNCTION ExecuteSilent(command)
Does the same as ^Execute(command)^ but supresses stdout and stderr of
that command.
@END]]--
if family == "windows" then
ExecuteSilent = function(command) return Execute(command .. " >nul 2>&1") end
else
ExecuteSilent = function(command) return Execute(command .. " >/dev/null 2>/dev/null") end
end
--[[@GROUP Path Manipulation @END]]--
--[[@UNITTESTS
err=1 : Path(nil)
err=1 : Path({})
err=1 : Path("asdf", "asdf")
catch="" : Path("")
catch="" : Path("/")
catch="/b.c/file.ext" : Path("/a/../b.c/./file.ext")
catch="/b.c" : Path("/a/../b.c/./")
catch="/.bc" : Path("/a/../.bc/./")
catch="/a" : Path("/a/b/..")
catch="/a/..b" : Path("/a/..b/")
catch="/a/b" : Path("/a//b/")
catch="/a" : Path("/a/b/../")
catch="/a" : Path("/a/.b/../")
catch="/a/.b" : Path("/a/.b")
catch="/a/.b" : Path("/a/.b/")
catch="../../b.c" : Path("../../a/../b.c/./")
catch="../path/file.name.ext" : Path("../test/../path/file.name.ext")
@END]]--
--[[@FUNCTION Path(str)
Normalizes the path ^str^ by removing ".." and "." from it.
{{{{
Path("test/./path/../file.name.ext") -- Returns "test/file.name.ext"
Path("../test/../path/file.name.ext") -- Returns "../path/file.name.ext"
}}}}
@END]]--
Path = bam_path_normalize
--[[@UNITTESTS
err=1: PathBase(nil)
err=1: PathBase({})
err=1: PathBase("", "")
catch="": PathBase("")
catch="/": PathBase("/")
catch="test/path/file.name": PathBase("test/path/file.name.ext")
catch="/a/../b.c/./file": PathBase("/a/../b.c/./file.ext")
@END]]--
--[[@FUNCTION PathBase(path)
Returns the everthing except the extention in the path.
{{{{
PathBase("test/path/file.name.ext") -- Returns "test/path/file.name"
PathBase("test/path/file.name") -- Returns "test/path/file"
PathBase("test/path/file") -- Returns "test/path/file"
}}}}
@END]]--
PathBase = bam_path_base
--[[@UNITTESTS
err=1 : PathFileExt(nil)
err=1 : PathFileExt({})
err=1 : PathFileExt("", "")
catch="" : PathFileExt("")
catch="" : PathFileExt("/")
catch="ext" : PathFileExt("/a/../b.c/./file.ext")
@END]]--
--[[@FUNCTION PathFileExt(str)
Returns the extension of the filename in ^str^.
{{{{
PathFileExt("test/path/file.name.ext") -- Returns "ext"
}}}}
@END]]--
PathFileExt = bam_path_ext
--[[@UNITTESTS
err=1 : PathFilename(nil)
err=1 : PathFilename({})
err=1 : PathFilename("", "")
catch="" : PathFilename("")
catch="" : PathFilename("/")
catch="file.ext" : PathFilename("/a/../b.c/./file.ext")
@END]]--
--[[@FUNCTION PathFilename(str)
Returns the filename of the path in ^str^.
{{{{
PathFilename("test/path/file.name.ext") -- Returns "file.name.ext"
}}}}
@END]]--
PathFilename = bam_path_filename
--[[@UNITTESTS
err=1 : PathJoin(nil)
err=1 : PathJoin("asdf", "asdf", "asdf")
catch="a/b" : PathJoin("a/b", "")
catch="a/b" : PathJoin("a/b/", "")
catch="a/b" : PathJoin("a", "b")
catch="a" : PathJoin("", "a")
catch="a/b" : PathJoin("", "a/b")
catch="a" : PathJoin("a/b", "..")
catch="a/b" : PathJoin("a/b/", "")
catch="a/b" : PathJoin("a/", "b/")
@END]]--
--[[@FUNCTION PathJoin(base, add)
Joins the two paths ^base^ and ^add^ together and returns a
normalized path. This function haldes trailing path separators in
the ^base^ argument.
{{{{
PathJoin("test/path/", "../filename.ext") -- Returns "test/filename.ext"
PathJoin("../test", "path/filename.ext") -- Returns "../test/path/filename.ext"
}}}}
@END]]--
PathJoin = bam_path_join
--[[@UNITTESTS
err=1 : PathDir(nil)
err=1 : PathDir({})
err=1 : PathDir("", "")
catch="" : PathDir("")
catch="" : PathDir("/")
catch="/b.c" : PathDir("/a/../b.c/./file.ext")
@END]]--
--[[@FUNCTION PathDir(str)
Returns the path of the filename in ^str^.
{{{{
PathDir("test/path/file.name.ext") -- Returns "test/path"
}}}}
@END]]--
PathDir = bam_path_dir
--[[@GROUP Tables @END]]--
--[[@UNITTESTS
err=1 : TableDeepCopy(nil)
err=1 : TableDeepCopy("")
err=1 : TableDeepCopy({}, {})
err=0 : TableDeepCopy({{{"a"}, "b"}, "c", "d", {"e", "f"}})
@END]]--
--[[@FUNCTION TableDeepCopy(tbl)
Makes a deep copy of the table ^tbl^ resulting in a complete separate
table.
@END]]--
TableDeepCopy = bam_table_deepcopy
--[[@UNITTESTS
err=0 : TableFlatten({"", {"", {""}, ""}, "", {}, {""}})
err=1 : TableFlatten({"", {"", {""}, ""}, 1, {""}})
@END]]--
--[[@FUNCTION TableFlatten(tbl)
Does a deep walk of the ^tbl^ table for strings and generates a new
flat table with the strings. If it occurs anything else then a table
or string, it will generate an error.
{{{{
-- Returns {"a", "b", "c", "d", "e", "f"}
TableFlatten({"a", {"b", {"c"}, "d"}, "e", {}, {"f"}})
}}}}
@END]]--
TableFlatten = bam_table_flatten
--[[@UNITTESTS
err=0 : t = {a = 1}; TableLock(t); t.a = 2
err=1 : t = {a = 1}; TableLock(t); t.b = 2
@END]]--
--[[@FUNCTION TableLock(tbl)
Locks the table ^tbl^ so no new keys can be added. Trying to add a new
key will result in an error.
@END]]--
function TableLock(tbl)
local mt = getmetatable(tbl)
if not mt then mt = {} end
mt.__newindex = function(tbl, key, value)
error("trying to create key '" .. key .. "' on a locked table")
end
setmetatable(tbl, mt)
end
--[[@UNITTESTS
catch="[a][b]" : TableToString({"a", "b"}, "[", "]")
@END]]--
--[[@FUNCTION TableToString(tbl, prefix, postfix)
Takes every string element in the ^tbl^ table, prepends ^prefix^ and appends ^postfix^
to each element and returns the result.
{{{{
TableToString({"a", "b"}, "[", "]") -- Returns "[a][b]"
}}}}
@END]]--
TableToString = bam_table_tostring
--[[@UNITTESTS
err=0 : for s in TableWalk({"", {"", {""}, ""}, "", {}, {""}}) do end
err=1 : for s in TableWalk({"", {"", {"", 1}, ""}, {""}}) do end
@END]]--
--[[@FUNCTION TableWalk(tbl)
Returns an iterator that does a deep walk of a table looking for strings.
Only checks numeric keys and anything else then table and strings will
cause an error.
{{{{
for filename in TableWalk({...}) do
print(filename)
end
}}}}
@END]]--
TableWalk = bam_table_walk
--[[@GROUP Settings @END]]--
_bam_tools = {}
--[[@UNITTESTS
@END]]--
--[[@FUNCTION
Adds a new tool called ^name^ to bam. The ^func^ will be called
when ^NewSettings^ function is invoked with the settings object as
first parameter.
@END]]--
function AddTool(func)
table.insert(_bam_tools, func)
end
--[[@UNITTESTS
err=1; find="expected a settings object": CheckSettings(nil)
err=1; find="expected a settings object": CheckSettings("")
err=1; find="expected a settings object": CheckSettings({})
err=0 : CheckSettings(NewSettings())
@END]]--
function CheckSettings(settings)
if not IsTable(settings) or settings._is_settingsobject == nil then
error("expected a settings object, got an " .. type(settings) .. " instead")
end
end
--[[@UNITTESTS
err=0 : NewSettings()
err=1 : t = NewSettings(); t.cc.DOES_NOT_EXIST = 1
err=1 : t = NewSettings():Copy(); t.cc.DOES_NOT_EXIST = 1
@END]]--
--[[@FUNCTION
Create a new settings table with the settings for all the registered
tools. This table is passed to many of the tools and contains how
they should act.
@END]]--
function NewSettings()
local settings = {}
settings._is_settingsobject = true
settings.invoke_count = 0
SetCommonSettings(settings)
-- add all tools
for _, tool in pairs(_bam_tools) do
tool(settings)
end
-- HACK: setup default drivers
SetDefaultDrivers(settings)
-- lock the table and return
TableLock(settings)
return settings
end
--[[@GROUP Files and Directories @END]]--
-- Collects files in a directory.
--[[@FUNCTION Collect(...)
Gathers a set of files using wildcard. Accepts strings and tables
of strings as input and returns a table of all the files that
matches A single wildcard * may be used in each string to collect
a set of files.
Example:
{{{{
source_files = Collect("src/*.c", "lib/*.c")
}}}}
Note. This version collects files, non-recursive.
@END]]--
Collect = bam_collect
--[[@FUNCTION CollectRecursive(...)
Collects files as the [Collect] but does so recursivly.
@END]]--
CollectRecursive = bam_collectrecursive
--[[@FUNCTION CollectDirs(...)
Collects directories in the same fashion as [Collect] but returns
directories instead.
@END]]--
CollectDirs = bam_collectdirs
--[[@FUNCTION CollectDirsRecursive(...)
Collects directories in the same fashion as [Collect] but does so
recursivly and returns directories instead.
@END]]--
CollectDirsRecursive = bam_collectdirsrecursive
--[[@FUNCTION MakeDirectory(path)
Creates the requested directory.
@END]]--
MakeDirectory = bam_mkdir
--[[@FUNCTION MakeDirectories(filename)
Creates the path upto the filename.
Example:
{{{{
MakeDirectories("output/directory/object.o")
}}}}
This will create the complete "output/directory" path.
@END]]--
MakeDirectories = bam_mkdirs
--[[@GROUP Targets@END]]--
--[[@FUNCTION DefaultTarget(filename)
Specifies the default target use build when no targets are
specified when bam is invoked.
@END]]--
DefaultTarget = bam_default_target
--[[@FUNCTION
Creates a pseudo target named ^name^ and assigns a set of dependencies
specified by ^...^.
@END]]--
function PseudoTarget(name, ...)
local name = Path(name)
bam_add_pseudo(name)
-- all the files
for inname in TableWalk({...}) do
AddDependency(name, inname)
end
return name
end
--[[@GROUP Modules@END]]--
--[[@FUNCTION Import(filename)
Imports a script specified by ^filename^. A search for the script will be
done by first checking the current directory and then the paths specified
by the BAM_PACKAGES environment variable. Several paths can be specified
in the variable by separating them by a ':' character.
The importing script can figure out it's path by calling the
[ModuleFilename] function.
@END]]--
function Import(filename)
local paths = {PathDir(ModuleFilename()), ""}
local s = os.getenv("BAM_PACKAGES")
if s then
for w in string.gmatch(s, "[^:]*") do
if string.len(w) > 0 then
table.insert(paths, w)
end
end
end
for _,path in pairs(paths) do
local filepath = PathJoin(path, filename)
if Exist(filepath) then
local chunk = bam_loadfile(filepath)
if chunk then
local current = _bam_modulefilename
_bam_modulefilename = filepath
bam_update_globalstamp(_bam_modulefilename)
chunk()
_bam_modulefilename = current
return
end
end
end
error(filename .. " not found")
end
--[[@FUNCTION
Returns the filename of the current script being imported (by [Import])
as relative to the current working directory.
@END]]--
function ModuleFilename()
return _bam_modulefilename
end
--[[@GROUP Job and Dependencies @END]]--
--[[@FUNCTION AddJob(output, label, command, ...)
Adds a job to be done. The ^output^ string specifies the file that
will be created by the command line specified in ^command^ string.
The ^label^ is printed out before ^command^ is runned. You can also
add extra parameters, those will become for dependencies for the job.
{{{{
AddJob("myapp.o", "compiling myapp.c", "gcc -c myapp.c -o myapp.o")
AddDependency("myapp.o", "myapp.c")
}}}}
This is the same as this:
{{{{
AddJob("myapp.o", "compiling myapp.c", "gcc -c myapp.c -o myapp.o", "myapp.c")
}}}}
You can also add several dependencies at once like this:
{{{{
AddJob("myapp", "linking myapp", "gcc myapp1.o myapp2.o myapp3.o -o myapp.o", {"myapp1.o", "myapp2.o"}, "myapp3.o")
}}}}
@END]]--
AddJob = bam_add_job
--[[@FUNCTION AddDependency(filename, ...)
Adds dependencies to a job. The files specified in the argument list gets added.
Strings and nested tables of strings are accepted.
@END]]--
AddDependency = bam_add_dependency
--[[@FUNCTION AddDependencySearch(filename, paths, ...)
Searches for dependencies in the specified ^paths^ and adds them to
the ^file^.
@END]]--
AddDependencySearch = bam_add_dependency_search
--[[@FUNCTION SkipOutputVerification(output)
Skips the output timestamp verification of this output
@END]]--
SkipOutputVerification = bam_skip_output_verification
function Default_Intermediate_Output(settings, input)
return PathBase(input) .. settings.config_ext
end
-- [TODO: Should be in C?]
function str_replace(s, pattern, what)
return string.gsub(s, pattern, function(v) return what end)
end
function NewTable()
local t = {}
t.Add = function(self, ...)
for i,what in ipairs({...}) do
table.insert(self, what)
end
self.version = self.version + 1
end
t.Merge = function(self, source)
for k,v in ipairs(source) do
table.insert(self, v)
end
self.version = self.version + 1
end
t.Remove = function(self, val)
local tmp = {}
for k,v in ipairs(self) do
if v == val then
table.remove(self, k)
end
end
self.version = self.version + 1
end
t.version = 0
return t
end
function NewFlagTable()
local t = NewTable()
t.ToString = function(self)
return TableToString(self, nil, " ")
end
return t
end
AddConstraintShared = bam_add_constraint_shared
AddConstraintExclusive = bam_add_constraint_exclusive