-
Notifications
You must be signed in to change notification settings - Fork 17
/
Copy pathlake
executable file
·3421 lines (3108 loc) · 104 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-2013
local usage = [[
Lake version 1.4 A Lua-based Build Engine
lake <flags> <assigments> <target(s)>
* flags:
-v verbose
-q quiet
-t test (show but don't execute commands)
-n don't synthesize target
-d initial directory
-b basic print (don't show full commands)
-s don't compile strictly
-g debug build (also DEBUG=1)
-j N run jobs in parallel where possible.
-f FILE read a named lakefile
-e EXPR evaluate a lakefile expression
-L MOD load a module lake.MOD first
-l FILE build a shared library/DLL
-lua FILE build a Lua C extension
-p FILE build a program
-w write out unsatisfied needs to lakeconfig.lua
-lua FILE build a Lua binary extension
-install FILE install a new need or language
-C really clean a directory tree!
* assignments: arguments of the form VAR=STRING assign the string
to the global VAR. The env variable LAKE_PARMS may contain
extra assignments.
* target(s): any targets of the lakefile; if a file with a recognized
extension, build and run, passing any remaining arguments, unless -n
is specified. Lua scripts are run directly using Lake.
Without arguments, use a file called 'lakefile' or 'lakefile.lua'
]]
local lfs = require 'lfs'
local append = table.insert
local env = os.getenv
local unpack = unpack
local log = print
local attributes = lfs.attributes
local verbose = false
local specific_targets = {}
local nbuild = 0
local all_targets_list = {}
local change_dir,finalize,exists,Windows
TESTING = false
DIRSEP = package.config:sub(1,1)
Windows = DIRSEP == '\\'
WINDOWS = Windows -- we'll review this later
---some useful library functions for manipulating file paths, lists, etc.
-- search for '--end_of_libs' to skip to the Meat of the Matter!
-- See lakelibs.luadoc for documentation
function warning(reason,...)
reason = reason or '?'
local str = reason:format(...)
io.stderr:write('lake: ',str,'\n')
end
function quit(reason,...)
warning(reason,...)
finalize(reason and reason:format(...))
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)'
if Windows then
QUIET_CMD = '> nul'
file.filter = function() return nil end
end
function file.time(fname)
if type(fname) ~= 'string' then
return fname.time
end
local time,err = attributes(fname,'modification')
if time then
return time
else
return -1
end
end
local function is_object(t)
return type(t) == 'table' and #t == 0 and t.time
end
local filetime = file.time
function file.generate_target(r,tname,root)
local odir = r.output_dir
if root and odir then
tname = tname:sub(#root+2)
local d = path.dirname(path.join(odir,tname))
if not path.isdir(d) then
path.mkdir(d)
end
return tname
end
end
local function fail (e,verbose)
if verbose then quit(e) else return nil,e end
end
function file.copy(src,dest,v)
local inf,err = io.open(src,'rb')
if err then return nil,err end
local dir = path.splitpath(dest)
if dir ~= '' and not path.isdir(dir) then
local ok, e = path.mkdir(dir)
if not ok then return fail(e,v) end
end
local outf,err = io.open(dest,'wb')
if err then inf:close(); return fail(err,v) end
outf:write(inf:read('*a'))
outf:close()
inf:close()
if v then
log ('copying '..src..' to '..dest)
end
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 type(name) ~= 'string' then
name.time = utils.clock()
end
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,ext)
local res = file.temp()
if ext then res = res .. ext end
local ok,err = file.write(res,s)
if not ok then return nil,err end
return res
end
local canonical_path
function file.copy_tree (src,dest)
local res
if PLAT=='Windows' then
src = canonical_path(src)
dest = canonical_path(dest)
res = utils.execute('xcopy /S /E /I /Q "'..src..'" "'..dest..'"',true)
else
res = utils.execute('cp -R '..src..' '..dest,true)
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
local function at(s,i) return s:sub(i,i) end
path = {}
local join, update_pwd, splitpath
function path.exists(path,fname)
if fname then fname = join(path,fname) else fname = path end
if attributes(fname) ~= nil then
return fname
end
end
exists = path.exists
file.exists = exists
function path.isdir(path)
if path:match '/$' then path = path:sub(1,-2) end
return attributes(path,'mode') == 'directory'
end
local isdir = path.isdir
function path.isfile(path)
return attributes(path,'mode') == 'file'
end
local isfile = path.isfile
function path.isabs(path)
if Windows then return path:find '^"*%a:' ~= nil
else return path:find '^/' ~= nil
end
end
local isabs = path.isabs
function path.abs(...)
local args = {...}
if isabs(args[1]) then return args[1] end
if not PWD then
update_pwd()
end
table.insert(args,1,PWD:sub(1,-2))
return table.concat(args,DIRSEP)
end
local function quote_if_necessary (file)
if not file then return '' end
if type(file) == 'string' and file:find '%s' then
if file:find '\\$' then
file = file .. '\\\\'
end
file = '"'..file..'"'
end
return file
end
path.quote = quote_if_necessary
-- 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 path.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
get_files = path.get_files
function path.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
get_directories = path.get_directories
function path.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
local files_from_mask = path.files_from_mask
local list_
function path.mask(mask)
return list_(files_from_mask(mask))
end
local mask = path.mask
function path.is_mask (pat)
return pat:find ('*',1,true)
end
function path.dirs(dir)
return list_(get_directories(dir))
end
function path.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
splitpath = path.splitpath
function path.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
local splitext = path.splitext
function path.dirname(P,strict)
if isdir(P) then return P end
local p1,p2 = splitpath(P)
return p1
end
local dirname = path.dirname
function path.basename(path)
local p1,p2 = splitpath(path)
return p2
end
local basename = path.basename
function path.extension_of(path)
local p1,p2 = splitext(path)
return p2
end
local extension_of = path.extension_of
local user_home = NEW_HOME
local function find_user_home()
if not user_home then
user_home = env 'HOME'
if not user_home then -- has to be Windows
user_home = env 'USERPROFILE' or (env 'HOMEDRIVE' .. env 'HOMEPATH')
elseif ENV.USER == 'root' then
user_home = utils.shell("echo ~"..ENV.SUDO_USER)
end
end
end
function path.expanduser(path)
if path:sub(1,1) == '~' then
find_user_home()
return user_home..path:sub(2)
else
return path
end
end
function path.replace_extension (path,ext)
local p1,p2 = splitext(path)
return p1..ext
end
local replace_extension = path.replace_extension
function path.join(p1,p2,...)
if select('#',...) > 0 then
local p = path.join(p1,p2)
local args = {...}
for i = 1,#args do
p = join(p,args[i])
end
return p
end
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
join = path.join
local _mkdir
function _mkdir(p)
if p:find '^%a:/*$' then -- windows root drive case
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
function canonical_path (p)
if type(p) ~= 'string' then return p end
if Windows then
return (p:gsub('/','\\')) --:lower())
else
return p
end
end
local function canonical_paths (deps)
if Windows then for i = 1,#deps do
deps[i] = canonical_path(deps[i])
end end
return deps
end
utils = {}
local start_t, msg_t
utils.clock = os.clock
function utils.split(s,re)
local i1 = 1
local ls = {}
re = re or '%s+'
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
local split = utils.split
function utils.split2(s,delim)
return s:match('([^'..delim..']+)'..delim..'(.*)')
end
local lua52 = rawget(_G,'setfenv') == nil
function utils.execute (cmd,quiet)
if quiet then
local null = " > "..choose(Windows,'NUL','/dev/null').." 2>&1"
cmd = cmd .. null
end
local res1,res2,res2 = os.execute(cmd)
if not lua52 then
return res1==0,res1
else
return res1,res2
end
end
function utils.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
local subst = utils.subst
function utils.substitute (str,T) return subst(str,nil,T) end
function utils.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
-- a convenient function which gets rid of the trailing line-feed from shell_nl()
function utils.shell(cmd,...)
return (utils.shell_nl(cmd,...):gsub('\n$',''))
end
local shell = utils.shell
local marker = string.char(4)
local function hide_spaces(q) return q:gsub(' ',marker) end
function utils.split_list(s)
local n_esc
s = s:gsub('^%s+',''):gsub('%s+$','') -- trim the string
-- spaces can be escaped with double quotes
s, n_esc = s:gsub('"[^"]+"',hide_spaces)
local i1 = 1
local ls = {}
local function append_item (item)
item = item:gsub('\\ ',' ')
append(ls,item)
end
-- In Unix spaces are escaped with \
local pat = choose(Windows,'.','[^\\]')..'[%s,]+'
while true do
local i2,i3 = s:find(pat,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
local item = ls[i]
if item:match(marker) then
item = item:gsub(marker,' ')
-- unquote fully quoted items
if item:match '^"' then
item = item:sub(2,-2)
end
ls[i] = item
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
function utils.remove(items,single)
if type(items) == 'string' then
if single then
items = {items}
else
items = split_list(items)
end
end
for _,f in ipairs(items) do
if type(f)=='string' and os.remove(f) then
log ('removing',f)
end
end
end
local exec
function utils.remove_files (mask)
local cmd
if Windows then
cmd = 'del '..mask
else
cmd = 'rm '..mask
end
exec(cmd)
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
function utils.quote(fun)
return function(...) return fun(...) end
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
function utils.copy_table (t)
local res = {}
for k,v in pairs(t) do
res[k] = v
end
return res
end
function is_simple_list (t)
return type(t) == 'table' and #t > 0
end
local function append_table(l1,l2,no_overwrite)
if not l2 then return end
for k,v in pairs(l2) do
if not no_overwrite or l1[k] == nil then
l1[k] = v
end
end
return l1
end
utils.append_table = append_table
list = {}
local append_list,index_list
function list.extend(l1,l2)
for i,v in ipairs(l2) do
append(l1,v)
end
return l1
end
append_list = list.extend
function list.append_unique(l,v)
if not index_list(l,v) then
append(l,v)
end
end
append_unique = list.append_unique
function list.extend_unique(l1,l2)
l1 = l1 or {}
for i,v in ipairs(l2) do
list.append_unique(l1,v)
end
return l1
end
append_list_unique = list.extend_unique
function list.copy (l1)
return append_list({},l1)
end
function utils.set(ls)
local res = {}
for item in list_(ls) do
res[item] = true
end
return res
end
function list.erase(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
function list.concat(ls,pre,sep)
local res = ''
for i,v in ipairs(ls) do
if v ~= '' then
if v:match '%s' and not v:match '"' then
v = quote_if_necessary(v)
end
res = res..pre..v..sep
end
end
return res
end
function list.index(ls,val)
for i,v in ipairs(ls) do
if v == val then return i end
end
end
index_list = list.index
function list.find(ls,field,value)
for i,v in ipairs(ls) do
if v[field] == value then
return v
end
end
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
utils.make_callable(list,list_)
function list.column(ls,f)
local res = {}
for i,t in ipairs(ls) do
append(res,t[f])
end
return res
end
column_list = list.column
function list.parm_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
local found_threads, winapi, posix
if Windows then
found_threads, winapi = pcall(require, 'winapi')
if found_threads then
utils.sleep = winapi.sleep
end
else
found_threads, posix = pcall(require, 'posix')
if found_threads and posix.clock_gettime then
function utils.clock()
local secs,nsec = posix.clock_gettime()
return secs + nsec/1e9
end
function utils.sleep (msec)
local sec = math.floor(msec/1000)
msec = msec - 1000*sec
posix.nanosleep(sec,1e6*msec)
end
end
end
--end_of_libs---------------------------------------------
local job_execute, jobs_clear
local function command_line (cmd)
local tmpfile,cmdline = file.temp()
if cmd:match '>%s*%S+$' then
cmdline = cmd..' 2> '..tmpfile
else
cmdline = cmd..' > '..tmpfile..' 2>&1'
end
return cmdline,tmpfile
end
local function execute_wrapper (cmd,t,callback)
local cmdline,tmpfile = command_line(cmd)
local ok,code = utils.execute(cmdline)
local inf = io.open(tmpfile,'r')
callback(ok,code,inf)
inf:close()
os.remove(tmpfile)
end
local n_threads = 1
if found_threads then
local processes,outputs = {},{}
local spawn, wait
if winapi then
local comspec = env('COMSPEC')..' /c '
function spawn(cmd)
return winapi.spawn_process(comspec..cmd)
end
function wait(ps)
local idx,err = winapi.wait_for_processes(ps,false)
if err then return nil, err end
local p = processes[idx]
return idx,p:get_exit_code(),err
end
else
function spawn(cmd)
local cpid = posix.fork()
if cpid == 0 then
if posix.exec('/bin/sh','-c',cmd) == -1 then
local msg,code = posix.errno()
os.exit(code)
end
else
return cpid
end
end
function wait(ps)
local pid,status,code = posix.wait(-1)
if not pid then return nil,nil,code end
local idx = index_list(ps,pid)
return idx,code
end
end
local function jobs_wait()
if #processes == 0 then return end
local idx,code,err = wait(processes)
if err then return nil, err end
local item, p = outputs[idx], processes[idx]
local inf,err = io.open(item.tmp,'r')
item.callback(code == 0, code, inf)
if item.read then item.read:close() end
inf:close()
if winapi then
p:close()
end
os.remove(item.tmp)
table.remove(processes,idx)
table.remove(outputs,idx)
end
function jobs_clear()
while #processes > 0 do jobs_wait() end
end
local current_rule
function job_execute (cmd,t,callback)
if n_threads < 2 then
execute_wrapper(cmd,t,callback)
else
-- crucial bit of synchronization: only run processes in parallel
-- generated from the _same rule_
if t.rule ~= current_rule then -- so clear the old rule's job queue
jobs_wait()
current_rule = t.rule
end
if #processes == n_threads then -- job queue is full
jobs_wait()
end
if #processes < n_threads then
local cmdline,tmpfile = command_line(cmd)
local p,r = spawn(cmdline)
append(outputs,{read=r,callback=callback,tmp=tmpfile})
append(processes,p)
end
end
end
else
job_execute = execute_wrapper
jobs_clear = function() end
end
-- for debug purposes: dump out a table
function dump(ls,msg)
log ('<<',msg or '')
if type(ls) == 'table' then
for i,v in pairs(ls) do
log(i,v)
end
else
log(ls)
end
log '>>'
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,1,true)
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