-
Notifications
You must be signed in to change notification settings - Fork 4
/
lake
executable file
·2769 lines (2517 loc) · 85.3 KB
/
lake
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
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
#!/usr/bin/env lua
-- Lake - a build framework in Lua
-- Freely distributable for any purpose, as long as copyright notice is retained. (X11/MIT)
-- (And remember my dog did not eat your homework)
-- Steve Donovan, 2007-2010
local usage = [[
Lake version 1.2 A Lua-based Build Engine
lake <flags> <assigments> <target(s)>
-Flags:
-v verbose
-n don't synthesize target
-d initial directory
-t test (show but don't execute commands)
-s strict compile
-g debug build
-f FILE read a named lakefile
-e EXPR evaluate an expression
-l FILE build a shared library/DLL
-p FILE build a program
-w write out unsatisfied needs to lakeconfig.lua
-lua FILE build a Lua binary extension
-assigmnents: arguments of the form VAR=STRING assign the string
to the global VAR
-target(s): any targets of the lakefile; if a file with a recognized
extension, build and run, passing any remaining arguments.
]]
local lfs = require 'lfs'
local append = table.insert
local verbose = false
local specific_targets = {}
local nbuild = 0
local all_targets_list = {}
local attributes = lfs.attributes
local env = os.getenv
local TMPSPEC = '_t_.spec'
local specfile
local outspec
local lakefile
local change_dir,finalize,exists
TESTING = false
DIRSEP = package.config:sub(1,1)
WINDOWS = DIRSEP == '\\'
---some useful library functions for manipulating file paths, lists, etc.
-- search for '--end_of_libs' to skip to the Meat of the Matter!
function warning(reason,...)
local str = reason:format(...)
io.stderr:write('lake: ',str,'\n')
end
function quit(reason,...)
warning(reason,...)
finalize()
os.exit(1)
end
function choose(cond,v1,v2)
if type(cond) == 'string' then
cond = cond~='0' and cond~='false'
end
if cond then return v1 else return v2 end
end
function pick(a,b)
if a ~= nil then return a else return b end
end
file = {ext='*'}
COPY = choose(WINDOWS,'copy','cp')
file.compile = '$(COPY) $(DEPENDS) $(TARGET)'
function file.copy(src,dest)
local inf,err = io.open(src,'r')
if err then return nil,err end
local dir = path.splitpath(dest)
if not path.isdir(dir) then
local ok, e = path.mkdir(dir)
if not ok then return nil,e end
end
local outf,err = io.open(dest,'w')
if err then inf:close(); return nil,err end
outf:write(inf:read('*a'))
outf:close()
inf:close()
return true
end
function file.write (name,text)
local outf,err = io.open(name,'w')
if not outf then return false,err end
outf:write(text);
outf:close()
return true
end
function file.read (name)
local inf,err = io.open(name,'r')
if not inf then return false,err end
local res = inf:read('*a')
inf:close()
return res
end
function file.touch(name)
if not path.exists(name) then
return file.write(name,'dummy')
else
return lfs.touch(name)
end
end
function file.temp ()
local res = os.tmpname()
if WINDOWS then -- note this necessary workaround for Windows
res = env 'TMP'..res
end
return res
end
function file.temp_copy (s)
local res = file.temp()
local ok,err = file.write(res,s)
if not ok then return nil,err end
return res
end
function file.find(...)
local t_remove = table.remove
local args = {...}
if #args == 1 then return exists(args[1]) end
for i = 1,#args do
if type(args[i]) == 'string' then args[i] = {args[i]} end
end
local p,q = args[1],args[2]
local pres = {}
for _,pi in ipairs(p) do
for _,qi in ipairs(q) do
local P
if qi:find '^%.' then P = pi..qi
else P = pi..DIRSEP..qi
end
P = exists(P)
if P then append(pres,P) end
end
end
if #pres == 0 then return pres end
local a1= t_remove(args,1)
local a2 = t_remove(args,1)
if #args > 0 then
return file.find(pres,unpack(args))
else
return pres,a1,a2
end
end
find = {}
if WINDOWS then
SYS_PREFIX = ''
else
SYS_PREFIX = {'/usr','/usr/share','/usr/local'}
end
function find.include_path(candidates)
local res = file.find(SYS_PREFIX,'include',candidates)
if #res == 0 then return nil end -- can't find it!
res = res[1] -- _might_ be other instances, probably pathological?
if type(candidates)=='string' then candidates = {candidates} end
for _,c in ipairs(candidates) do
local i1,i2 = res:find(C..'$')
end
return res
end
function file.time(fname)
local time,err = attributes(fname,'modification')
if time then
return time
else
return -1
end
end
local filetime = file.time
local function at(s,i)
return s:sub(i,i)
end
path = {}
local join
function exists(path,fname)
if fname then fname = join(path,fname) else fname = path end
if attributes(fname) ~= nil then
return fname
end
end
path.exists = exists
-- is @path a directory?
local function isdir(path)
if path:match '/$' then path = path:sub(1,-2) end
return attributes(path,'mode') == 'directory'
end
path.isdir = isdir
-- is @path a file?
local function isfile(path)
return attributes(path,'mode') == 'file'
end
path.isfile = isfile
-- is this an absolute @path?
local function isabs(path)
if WINDOWS then return path:find '^"*%a:' ~= nil
else return path:find '^/' ~= nil
end
end
path.isabs = isabs
local function quote_if_necessary (file)
if file:find '%s' then
file = '"'..file..'"'
end
return file
end
-- this is used for building up strings when the initial value might be nil
-- s = concat_str(s,"hello")
local function concat_str (v,u,no_quote)
if not no_quote then u = quote_if_necessary(u) end
if type(v) == 'table' then v = table.concat(v,' ') end
return (v or '')..' '..u
end
local get_files
function get_files (files,path,pat,recurse)
for f in lfs.dir(path) do
if f ~= '.' and f ~= '..' then
local file = f
if path ~= '.' then file = join(path,file) end
if recurse and isdir(file) then
get_files(files,file,pat,recurse)
elseif f:find(pat) then
append(files,file)
end
end
end
end
path.get_files = get_files
local function get_directories (dir)
local res = {}
for f in lfs.dir(dir) do
if f ~= '.' and f ~= '..' then
local path = join(dir,f)
if isdir(path) then append(res,path) end
end
end
return res
end
path.get_directories = get_directories
local splitpath
local function files_from_mask (mask,recurse)
local path,pat = splitpath(mask)
if not pat:find('%*') then return nil end
local files = {}
if path=='' then path = '.' end
-- turn shell-style wildcard into Lua regexp
pat = pat:gsub('%.','%%.'):gsub('%*','.*')..'$'
get_files(files,path,pat,recurse)
return files
end
path.files_from_mask = files_from_mask
local list_
local function mask(mask)
return list_(files_from_mask(mask))
end
path.mask = mask
local function is_mask (pat)
return pat:find ('*',1,true)
end
path.is_mask = is_mask
local function dirs(dir)
return list_(get_directories(dir))
end
path.dirs = dirs
utils = {}
local function split(s,re)
local i1 = 1
local ls = {}
while true do
local i2,i3 = s:find(re,i1)
if not i2 then
append(ls,s:sub(i1))
return ls
end
append(ls,s:sub(i1,i2-1))
i1 = i3+1
end
end
utils.split = split
local function split2(s,delim)
return s:match('([^'..delim..']+)'..delim..'(.*)')
end
local lua52 = table.pack ~= nil
function utils.execute (cmd)
local res1,res2,res2 = os.execute(cmd)
if not lua52 then
return res1==0,res1
else
return res1,res2
end
end
-- given a path @path, return the directory part and a file part.
-- if there's no directory part, the first value will be empty
function splitpath(path)
local i = #path
local ch = at(path,i)
while i > 0 and ch ~= '/' and ch ~= '\\' do
i = i - 1
ch = at(path,i)
end
if i == 0 then
return '',path
else
return path:sub(1,i-1), path:sub(i+1)
end
end
path.splitpath = splitpath
-- given a path @path, return the root part and the extension part
-- if there's no extension part, the second value will be empty
local function splitext(path)
local i = #path
local ch = at(path,i)
while i > 0 and ch ~= '.' do
if ch == '/' or ch == '\\' then
return path,''
end
i = i - 1
ch = at(path,i)
end
if i == 0 then
return path,''
else
return path:sub(1,i-1),path:sub(i)
end
end
path.splitext = splitext
-- return the directory part of @path
local function dirname(path)
local p1,p2 = splitpath(path)
return p1
end
path.dirname = dirname
-- return the file part of @path
local function basename(path)
local p1,p2 = splitpath(path)
return p2
end
path.basename = basename
local function extension_of(path)
local p1,p2 = splitext(path)
return p2
end
path.extension_of = extension_of
local function expanduser(path)
if path:sub(1,1) == '~' then
local home = env 'HOME'
if not home then -- has to be Windows
home = env 'USERPROFILE' or (env 'HOMEDRIVE' .. env 'HOMEPATH')
end
return home..path:sub(2)
else
return path
end
end
path.expanduser = expanduser
local function replace_extension (path,ext)
local p1,p2 = splitext(path)
return p1..ext
end
path.replace_extension = replace_extension
-- return the path resulting from combining @p1,@p2 and optionally @p3 (an extension);
-- if @p2 is already an absolute path, then it returns @p2
function join(p1,p2,p3)
if p3 then p2 = p2 .. p3 end -- extension part
if isabs(p2) then return p2 end
local endc = at(p1,#p1)
if endc ~= '/' and endc ~= '\\' then
p1 = p1..DIRSEP
end
return p1..p2
end
path.join = join
local _mkdir
function _mkdir(p)
-- windows root drive case
if p:find '^%a:/*$' then
return true
end
if not path.isdir(p) then
local subp = p:match '(.+)/[^/]+$'
if subp and not _mkdir(subp) then return nil,'cannot create '..subp end
return lfs.mkdir(p)
else
return true
end
end
function path.mkdir (p)
if WINDOWS then p = p:gsub('\\','/') end
return _mkdir(p)
end
-- this expands any $(VAR) occurances in @s (where VAR is a global varialable).
-- If VAR is not present, then the expansion is just the empty string, unless
-- it is on the @exclude list, where it remains unchanged, ready for further
-- expansion at a later stage.
local function subst(str,exclude,T)
local count
T = T or _G
repeat
local excluded = 0
str, count = str:gsub('%$%(([%w,_]+)%)',function (f)
if exclude and exclude[f] then
excluded = excluded + 1
return '$('..f..')'
else
local s = T[f]
if not s then return ''
else return s end
end
end)
until count == 0 or exclude
return str
end
utils.subst = subst
function utils.substitute (str,T) return subst(str,nil,T) end
-- this executes a shell command @cmd, which may contain % string.format specifiers,
-- in which case any extra arguments are used. It may contain ${VAR} which will
-- be substituted
local function shell_nl(cmd,...)
cmd = subst(cmd):format(...)
local inf = io.popen(cmd..' 2>&1','r')
if not inf then return '' end
local res = inf:read('*a')
inf:close()
return res
end
utils.shell_nl = shell_nl
-- a convenient function which gets rid of the trailing line-feed from shell_nl()
local function shell(cmd,...)
return (shell_nl(cmd,...):gsub('\n$',''))
end
utils.shell = shell
-- splits a list separated by ' ' or ','. Note that some hackery is needed
-- to preserve double quoted items.
local marker = string.char(4)
local function hide_spaces(q) return q:gsub(' ',marker) end
function utils.split_list(s)
s = s:gsub('^%s+',''):gsub('%s+$','') -- trim the string
s = s:gsub('"[^"]+"',hide_spaces)
local i1 = 1
local ls = {}
local function append_item (item)
item = item:gsub('\\ ',' ')
append(ls,item)
end
while true do
local i2,i3 = s:find('[^\\][%s,]+',i1)
if not i2 then
append_item(s:sub(i1))
break
end
append_item(s:sub(i1,i2))
i1 = i3+1
end
for i = 1,#ls do
if ls[i]:find(marker) then
ls[i] = ls[i]:gsub(marker,' ') --:gsub('"','')
end
end
return ls
end
local split_list = utils.split_list
local expand_args
function utils.forall(ls,action)
ls = expand_args(ls)
for i,v in ipairs(ls) do
action(v)
end
end
local forall = utils.forall
-- useful global function which deletes a list of files @items
function utils.remove(items)
if type(items) == 'string' then
items = split_list(items)
end
forall(items,function(f)
if os.remove(f) then
print ('removing',f)
end
end)
end
local remove = utils.remove
function utils.remove_files (mask)
local cmd
if WINDOWS then
cmd = 'del '..mask
else
cmd = 'rm '..mask
end
exec(cmd)
end
local remove_files = utils.remove_files
function is_simple_list (t)
return type(t) == 'table' and t[1]
end
list = {}
local append_list,copy_list,copy_table,append_table,erase_list,concat_list,index_list,find_list
function append_list(l1,l2)
for i,v in ipairs(l2) do
append(l1,v)
end
return l1
end
list.extend = append_list
local function append_unique(l,v)
if not index_list(l,v) then
append(l,v)
end
end
list.append_unique = append_unique
local function append_list_unique(l1,l2)
for i,v in ipairs(l2) do
append_unique(l1,v)
end
end
list.extend_unique = append_list_unique
function copy_list (l1)
return append_list({},l1)
end
list.copy = copy_list
function copy_table (t)
local res = {}
for k,v in pairs(t) do
res[k] = v
end
return res
end
table.copy = copy_table
function append_table(l1,l2)
if not l2 then return end
for k,v in pairs(l2) do
l1[k] = v
end
return l1
end
table.update = append_table
function table.set(ls)
local res = {}
for item in list_(ls) do
res[item] = true
end
return res
end
function erase_list(l1,l2)
for i,v in ipairs(l2) do
local idx = index_list(l1,v)
if idx then
table.remove(l1,idx)
end
end
end
list.erase = erase_list
function concat_list(pre,ls,sep)
local res = ''
for i,v in ipairs(ls) do
if v ~= '' then
res = res..pre..v..sep
end
end
return res
end
list.concat = concat_list
function index_list(ls,val)
for i,v in ipairs(ls) do
if v == val then return i end
end
end
list.index = index_list
function find_list(ls,field,value)
for i,v in ipairs(ls) do
if v[field] == value then
return v
end
end
end
list.find = find_list
-- used to iterate over a list, which may be given as a string:
-- for val in list(ls) do ... end
-- for val in list 'one two three' do .. end
function list_(ls)
if type(ls) == 'string' then
ls = split_list(ls)
end
local n = #ls
local i = 0
return function()
i = i + 1
if i > n then return nil end
return ls[i]
end
end
function utils.make_callable (obj,fun)
local mt = getmetatable(obj)
if not mt then
mt = {}
setmetatable(obj,mt)
end
mt.__call = function(obj,...) return fun(...) end
return mt
end
utils.make_callable(list,list_)
function append_unique(ls,val)
if not index_list(ls,val) then
return append(ls,val)
end
end
list.append_unique = append_unique
function column_list(ls,f)
local res = {}
for i,t in ipairs(ls) do
append(res,t[f])
end
return res
end
list.column = column_list
local function parm_list_concat(ls,istart)
local s = ' '
istart = istart or 1
for i = istart,#ls do
local a = ls[i]
if a:find(' ') then a = '"'..a..'"' end
s = s..a..' '
end
return s
end
-- readlines(f) works like f:lines(), except it will handle lines separated by '\'
function utils.readlines(f)
return function()
local line = ''
repeat
local l = f:read()
if not l then return nil end
local last = l:sub(-1,-1)
if last == '\\' then
l = l:sub(1,-2)
end
line = line..l
until last ~= '\\'
return line
end
end
local readlines = utils.readlines
-- for debug purposes: dump out a table
function dump(ls,msg)
print ('<<<',msg)
if type(ls) == 'table' then
for i,v in pairs(ls) do
print(i,v)
end
else
print(ls)
end
print '>>'
end
function utils.which (prog)
if isabs(prog) then return prog end
if WINDOWS then -- no 'which' commmand, so do it directly
if extension_of(prog) == '' then prog = prog..'.exe' end
local path = split(env 'PATH',';')
for dir in list_(path) do
local file = exists(dir,prog)
if file then return file end
end
return false
else
local res = shell('which %s 2> /dev/null',prog)
if res == '' then return false end
return res
end
end
--- answering the question: is an include file within the MSVC include path?
local msvc_includes
local msvc_include_cache = {}
local function within_msvc_include_path (file)
if not msvc_includes then
local include = env 'INCLUDE':gsub(';$',''):lower()
msvc_includes = split(include,';')
end
local res = msvc_include_cache[file]
if res ~= nil then return res end
local dir = splitpath(file):lower()
for d in list_(msvc_includes) do
local i1,i2 = dir:find(d)
if i1==1 and i2 == #d then
res = true
break
end
end
msvc_include_cache[file] = res
return res
end
local function concat(s1,s2) return (s1 or '')..' '..s2 end
--end_of_libs---------------------------------------------
local interpreters = {
['.lua'] = 'lua', ['.py'] = 'python',
}
local check_options
if WINDOWS then
LOCAL_EXEC = ''
EXE_EXT = '.exe'
DLL_EXT = '.dll'
else
LOCAL_EXEC = './'
EXE_EXT = ''
DLL_EXT = '.so'
end
LIBS = ''
CFLAGS = ''
local function inherits_from (c)
local mt = {__index = c}
return function(t)
return setmetatable(t,mt)
end
end
local function appender ()
local t = {}
utils.make_callable(t,function(a)
check_options(a)
append_table(t,a)
end)
return t
end
lake = {}
local baselang = {}
baselang.defaults = appender()
function lake.new_lang(bl,t)
bl = bl or baselang
t = t or {}
return inherits_from(bl)(t)
end
c = lake.new_lang(nil,{ext='.c'})
local CI = inherits_from(c)
c.defaults = appender()
-- these chaps inherit from C lang for many of their fields
cpp = CI{ext='.cpp'}
f = CI{ext='.f'}
c99 = CI{ext='.c'}
cpp.defaults = appender()
f.defaults = appender()
c99.defaults = appender()
-- precompiled C++ headers
cpp_ch = inherits_from(cpp) { ext = '.h' }
-- experimental C++11 support
cpp11 = inherits_from(cpp) {}
wresource = {ext='.rc'}
local extensions = {
['.c'] = c, ['.cpp'] = cpp, ['.cxx'] = cpp, ['.C'] = cpp,
['.f'] = f, ['.for'] = f, ['.f90'] = f,
}
local deps_args
function lake.register(lang,extra)
extensions[lang.ext] = lang
if extra then
for e in list_(deps_arg(extra)) do
extensions[e] = lang
end
end
end
-- @doc any <var>=<value> pair means set the global variable <var> to the <value>, as a string.
function process_var_pair(a)
local var,val = split2(a,'=')
if var then
_G[var] = val
return true
end
end
-- @doc dependencies are stored as lists, but if you go through deps_arg, then any string
-- delimited with ' ' or ',' will be converted into an appropriate list.
-- This function is guaranteed to return a plain list, and will wrap other objects like
-- targets and rules appropriately. Target lists are extracted.
function deps_arg(deps,base)
local T = type(deps)
if T=='table' and not is_simple_list(deps) then
local tl = get_target_list(deps)
if tl then
return tl
else
return {deps}
end
end
if T=='string' then
deps = split_list(deps)
elseif T~='table' then
--quit("deps_arg must be passed a string or table; got "..T)
end
if base then
for i = 1,#deps do
deps[i] = join(base,deps[i])
end
end
return deps
end
lake.deps_arg = deps_arg
-- expand_args() goes one step further than deps_arg(); it will expand a wildcard expression into a list of files
-- as well as handling lists as strings. If the argument is a table, it will attempt
-- to expand each string - e.g. {'a','b c'} => {'a','b','c'}
function expand_args(src,ext,recurse,base)
if type(src) == 'table' then
local res = {}
for s in list_(src) do
for l in list_(split_list(s)) do
if base then l = join(base,l) end
append_list(res,expand_args(l,ext,recurse))
end
end
return res
end
local items = split_list(src)
if #items > 1 then return expand_args(items,ext,recurse,base) end
src = items[1]
-- @doc 'src' if it is a directory, then regard that as an implicit wildcard
if base then src = join(base,src) end
if ext and isdir(src) and not isfile(src..ext) then
src = src..'/*'
end
if src:find('%*') then
if src:find '%*$' then src = src..ext end
return files_from_mask(src,recurse)
else
local res = deps_arg(src) --,base)
if ext then
-- add the extension to the list of files, unless there's already an extension...
for i = 1,#res do
if extension_of(res[i]) == '' then res[i] = res[i]..ext end
end
end
return res
end
end
lake.expand_args = expand_args
function utils.quote(fun)
return function(...) return fun(...) end
end
utils.foreach = utils.quote(forall)
local tmt,tcnt = {},1
function istarget (t)
return type(t) == 'table' and getmetatable(t) == tmt
end
local function new_target(tname,deps,cmd,upfront)
local t = setmetatable({},tmt)
if tname == '*' then
tname = '*'..tcnt
tcnt = tcnt + 1
end
t.target = tname
t.deps = deps_arg(deps)
t.cmd = cmd
if upfront then
table.insert(all_targets_list,1,t)
else
append(all_targets_list,t)
end
if type(cmd) == 'string' then
if specfile then
-- @doc [checking against specfile] for each target, we check the command generated
-- against the stored command, and delete the target if the command is different.
local oldcmd = specfile:read()
if oldcmd ~= cmd then
if verbose then
print(oldcmd); print(cmd)
print('command changed: removing '..tname)
end
os.remove(tname)
end
end
if outspec then outspec:write(cmd,'\n') end
end
return t
end
function phony(deps,cmd)
return new_target('*',deps,cmd,true)
end
--- @doc [Rule Objects] ----
-- serve two functions (1) define a conversion operation between file types (such as .c -> .o)
-- and (2) manage a list of dependent files.