forked from bmx-ng/bmk
-
Notifications
You must be signed in to change notification settings - Fork 0
/
bmk_ng.bmx
1040 lines (821 loc) · 21.2 KB
/
bmk_ng.bmx
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
SuperStrict
Import BRL.Reflection
Import BRL.Map
Import BRL.LinkedList
'?win32
Import Pub.FreeProcess
'?
?threaded
Import BRL.Threads
?
?Not win32
Import "waitpid.c"
?
Import "bmk_config.bmx"
Import "bmk_ng_utils.bmx"
Global commands:TMap = New TMap
Global processor:TBMK = New TBMK
Global globals:TBMKGlobals = New TBMKGlobals
' load in the base stuff
LoadBMK(AppDir + "/core.bmk")
LoadBMK(AppDir + "/make.bmk")
' optional
LoadBMK(AppDir + "/config.bmk")
' add some defaults
If processor.Platform() = "macos"
globals.SetVar("macos_version", String(macos_version))
End If
globals.SetVar("cc_opts", New TOptionVariable)
globals.SetVar("ld_opts", New TOptionVariable)
'globals.SetVar("gcc_version", String(processor.GCCVersion()))
Function LoadBMK(path:String)
processor.LoadBMK(path)
End Function
' this is the core bmk processor.
Type TBMK
Method New()
LuaRegisterObject Self,"bmk"
End Method
' loads a .bmk, stores any functions, and runs any commands.
Method LoadBMK(path:String)
Local str:String
Try
If FileType(path) = 1 Then
str = LoadText( path )
Else
If FileType(AppDir + "/" + path) = 1 Then
str = LoadText( AppDir + "/" + path )
Else
If FileType(globals.Get("BUILDPATH") + "/" + path) = 1 Then
str = LoadText(globals.Get("BUILDPATH") + "/" + path )
Else
Return
End If
End If
End If
Catch e:Object
Try
If FileType(AppDir + "/" + path) = 1 Then
str = LoadText( AppDir + "/" + path )
Else
If FileType(globals.Get("BUILDPATH") + "/" + path) = 1 Then
str = LoadText(globals.Get("BUILDPATH") + "/" + path )
Else
Return
End If
End If
Catch e:Object
' we tried... twice
' fail silently...
Return
End Try
End Try
Local pos:Int, inDefine:Int, text:String, name:String
While pos < str.length
Local eol:Int = str.Find( "~n",pos )
If eol = -1 Then
eol = str.length
End If
Local line:String = str[pos..eol].Trim()
pos = eol+1
ProcessLine(line, inDefine, text, name)
' anything else?
Wend
End Method
' processes a pragma
Method ProcessPragma(line:String, inDefine:Int Var, text:String Var, name:String Var)
ProcessLine(line, inDefine, text, name)
End Method
Method ProcessLine(line:String, inDefine:Int Var, text:String Var, name:String Var)
If line.StartsWith("#") Then
Return
End If
Local lline:String = line.ToLower()
If line.StartsWith("@") Then
If lline[1..].StartsWith("define") Then
inDefine = True
name = line[8..].Trim()
Local cmd:TBMKCommand = New TBMKCommand
cmd.name = name
commands.Insert(name.ToLower(), cmd)
Return
End If
If lline[1..].StartsWith("end") Then
If inDefine Then
Local cmd:TBMKCommand = TBMKCommand(commands.ValueForKey(name.ToLower()))
cmd.LoadCommand(text)
text = ""
inDefine = False
End If
Return
End If
End If
If inDefine Then
text:+ line + "~n"
Return
End If
If line.length = 0 Then
Return
End If
' find command, and run
Local i:Int=1
While i < lline.length And (CharIsAlpha(lline[i]) Or CharIsDigit(lline[i]))
i:+1
Wend
'If i = lline.length Then
' Continue
'End If
Local command:String = lline[..i]
Local cmd:TBMKCommand = TBMKCommand(commands.ValueForKey(command))
' this is a command!
If cmd Then
cmd.RunCommand(line[i+1..])
Return
End If
' what's left?
' setting a variable?
i = line.Find("=")
If i <> -1 Then
' hmm. maybe a variable...
Local variable:String = line[..i].Trim()
Local value:String = Parse(line[i+1..].Trim())
globals.SetVar(variable, value)
End If
End Method
Method Parse:String(str:String)
Local done:Int
While Not done
Local pos:Int, restart:Int, changed:Int
While pos < str.length And Not restart
Local eol:Int = str.Find( "~n",pos )
If eol = -1 Then
eol = str.length
End If
Local line:String = str[pos..eol].Trim()
pos = eol+1
Local i:Int
While i < line.length
i = line.find("%", i)
If i = -1 Then
i = line.length
Continue
End If
Local start:Int = i
i:+ 1
While i < line.length And (CharIsAlpha(line[i]) Or CharIsDigit(line[i]))
i:+1
Wend
If i > start Then
If line[i..i+1] = "%" Then
i:+ 1
Local toReplace:String = line[start..i]
' we want to replace this with something, so we
' will look in the globals list and env for a match.
' Otherwise, it will swap % with $, and leave it as is.
Local with:String = FindValue(toReplace)
If with Then
str = str.Replace(toReplace, with)
restart = True
End If
End If
End If
Wend
Wend
If Not restart Then
done = True
End If
Wend
Return str
End Method
Method FindValue:String(variable:String)
Local plainVar:String = variable.Replace("%", "")
Local value:String = globals.Get(plainVar)
If value Then
Return value
End If
' look for environment variable ?
Local env:String = getenv_(plainVar)
If env Then
Return env
End If
' return the original
Return variable.Replace("%", "$")
End Method
' quotes a string, if required (does it have spaces in it?)
Method Quote:String(t:String)
Return CQuote(t)
End Method
' returns the platform as a string
Method Platform:String()
If Not opt_target_platform Then
' the native target platform
?macos
Return "macos"
?linux
Return "linux"
?win32
Return "win32"
?
Else
' the custom target platform
Return opt_target_platform
End If
End Method
' returns the cpu type, as a string
Method CPU:String()
Return opt_arch
' Return cputypes[cputype]
End Method
Method ToggleCPU()
If opt_universal Then
If opt_arch = "ppc" Then
opt_arch = "x86"
Else
opt_arch = "ppc"
End If
End If
End Method
Method BuildName:String(v:String)
Local s:String = Platform() + "." + CPU() + "." + v
Return s.ToLower()
End Method
Method Sys:Int(cmd:String)
If Int(globals.Get("verbose")) Or opt_verbose
Print cmd
Else If Int(globals.Get("dumpbuild"))
Local p$=cmd
p = p.Replace( BlitzMaxPath()+"/","./" )
WriteStdout p+"~n"
Local t$="mkdir "
If cmd.StartsWith( t ) And FileType( cmd[t.length..] ) Return False
EndIf
Return system_( cmd )
End Method
Method MultiSys:Int(cmd:String, src:String)
If Int(globals.Get("verbose")) Or opt_verbose
Print cmd
Else If Int(globals.Get("dumpbuild"))
Local p$=cmd
p = p.Replace( BlitzMaxPath()+"/","./" )
WriteStdout p+"~n"
Local t$="mkdir "
If cmd.StartsWith( t ) And FileType( cmd[t.length..] ) Return False
EndIf
?threaded
processManager.DoSystem(cmd, src)
?Not threaded
Return system_( cmd )
?
End Method
Method ThrowNew(e:String)
Throw e
End Method
Method Call(name:String, args:String[])
RunCommand(name, args)
End Method
Method AddArg(option:String, extra:String)
Local args:String[] = [option]
If extra Then
args:+ [extra]
End If
ParseConfigArgs args
End Method
Method Option:String(key:String, defaultValue:String)
Local value:String = globals.Get(key)
If Not value Then
Return defaultValue
Else
Return value
End If
End Method
Method GCCVersion:String(getVersionNum:Int = False)
'?win32
Global compiler:String
Global version:String
If compiler Then
If getVersionNum Then
Return version
Else
Return compiler + " " + version
End If
End If
Local process:TProcess = CreateProcess("gcc -v")
Local s:String
While True
Delay 10
Local line:String = process.err.ReadLine()
If Not process.Status() And Not line Then
Exit
End If
If line.startswith("gcc") Then
compiler = "gcc"
Local parts:String[] = line.split(" ")
Local values:String[] = parts[2].split(".")
For Local v:String = EachIn values
Local n:String = "0" + v
s:+ n[n.length - 2..]
Next
Else
Local pos:Int = line.Find("clang")
If pos >= 0 Then
compiler = "clang"
s = line[pos + 6..line.find(")", pos)]
End If
End If
Wend
version = s
If getVersionNum Then
Return version
End If
Return compiler + " " + version
'?
End Method
Method GCCVersionInt:Int()
End Method
Method BCCVersion:String()
Global bcc:String
If bcc Then
Return bcc
End If
Local process:TProcess = CreateProcess(BlitzMaxPath() + "/bin/bcc")
Local s:String
While True
Delay 10
Local line:String = process.pipe.ReadLine()
If Not process.Status() And Not line Then
Exit
End If
If line.startswith("BlitzMax") Then
bcc = "BlitzMax"
Else
bcc = line[..line.Find(" ")]
End If
Wend
Return bcc
End Method
End Type
' stores variables, as well as a variable stack which can be pushed and popped.
Type TBMKGlobals
' current value of variables
Field vars:TMap = New TMap
' variable stack
Field stack:TMap = New TMap
Method New()
LuaRegisterObject Self,"globals"
End Method
' sets the variable with value
Method SetVar(variable:String, value:Object)
'Print "SetVar : " + variable + " : " + String(value)
vars.Insert(variable.ToUpper(), value)
End Method
' returns the current value for variable
Method Get:String(variable:String)
Local obj:Object = vars.ValueForKey(variable.ToUpper())
If obj Then
If String(obj) Then
Return String(obj)
End If
Return obj.ToString()
End If
End Method
Method GetRawVar:Object(variable:String)
Local obj:Object = vars.ValueForKey(variable.ToUpper())
If TOptionVariable(obj) Then
' return a copy of the object - any changes to this won't affect the current value.
Return TOptionVariable(obj).Clone()
End If
Return obj
End Method
' push the variable onto the stack (save the value)
Method Push(variable:String)
variable = variable.ToUpper()
Local list:TList = TList(stack.ValueForKey(variable))
If Not list Then
list = New TList
stack.Insert(variable, list)
End If
list.AddLast(GetRawVar(variable))
End Method
' pop the variable from the stack (load the value)
Method Pop(variable:String)
variable = variable.ToUpper()
Local list:TList = TList(stack.ValueForKey(variable))
If list And Not list.IsEmpty() Then
SetVar(variable, list.RemoveLast())
End If
End Method
' push all the variables
Method PushAll(exclude:String[] = Null)
For Local v:String = EachIn vars.Keys()
If Not exclude
Push(v)
Else
For Local s:String = EachIn exclude
If s <> v Then
Push(v)
Exit
End If
Next
End If
Next
End Method
' pop all the variables
Method PopAll()
For Local v:String = EachIn vars.Keys()
Pop(v)
Next
End Method
' adds value to the end of variable
Method Add(variable:String, value:String)
variable = variable.ToUpper()
Local v:Object = vars.ValueForKey(variable)
If Not TOptionVariable(v) Then
SetVar(variable, String(v) + " " + value)
End If
End Method
Method AddOption(variable:String, key:String, value:String)
variable = variable.ToUpper()
Local v:Object = vars.ValueForKey(variable)
If TOptionVariable(v) Then
TOptionVariable(v).AddVar(key, value)
Else
Local opt:TOptionVariable = New TOptionVariable
opt.addVar(key, value)
setVar(variable, opt)
End If
End Method
Method SetOption(variable:String, key:String, value:String)
variable = variable.ToUpper()
Local v:Object = vars.ValueForKey(variable)
If TOptionVariable(v) Then
TOptionVariable(v).SetVar(key, value)
Else
Local opt:TOptionVariable = New TOptionVariable
opt.SetVar(key, value)
setVar(variable, opt)
End If
End Method
' only appropriate for TOptionVariables
Method RemoveVar(variable:String, name:String)
variable = variable.ToUpper()
Local v:Object = vars.ValueForKey(variable)
If TOptionVariable(v) Then
TOptionVariable(v).RemoveVar(name)
End If
End Method
Method Clear(variable:String)
variable = variable.ToUpper()
Local v:Object = vars.ValueForKey(variable)
If TOptionVariable(v) Then
vars.remove(variable)
End If
End Method
Method Reset()
stack.Clear()
End Method
Method Dump()
For Local k:String = EachIn vars.Keys()
Print k + " : " + Get(k)
Next
End Method
End Type
Type TOpt
Field name:String
Field value:String
End Type
' holds a list of options.
' useful for storing a list of cc_opts, for example.
' the list can be modified as required, and cloned during push/pop calls.
Type TOptionVariable
Field options:TMap = New TMap
Field orderedOptions:TList = New TList
Method AddVar(name:String, value:String)', insertBefore:Int = False)
Local opt:TOpt = New TOpt
If Not name Then
Global count:Int
count:+1
name = "VAR" + count
opt.name = name
Else
opt.name = name
End If
opt.value = value
options.Insert(name, opt)
orderedOptions.AddLast(opt)
End Method
Method SetVar(name:String, value:String)', insertBefore:Int = False)
Local opt:TOpt = New TOpt
If Not name Then
Global count:Int
count:+1
name = "VAR" + count
opt.name = name
Else
opt.name = name
End If
opt.value = value
' option already exists?
Local o:TOpt = TOpt(options.ValueForKey(name))
If o Then
orderedOptions.Remove(o)
End If
options.Insert(name, opt)
orderedOptions.AddLast(opt)
End Method
' finds and removes a matching value
Method RemoveVar(name:String)
Local opt:TOpt = TOpt(options.ValueForKey(name))
options.Remove(opt)
orderedOptions.Remove(opt)
End Method
Method ToString:String()
Local s:String = " "
For Local opt:TOpt = EachIn orderedOptions
s:+ opt.value + " "
Next
Return s
End Method
' create an exact copy of me
Method Clone:TOptionVariable()
Local me:TOptionVariable = New TOptionVariable
For Local name:String = EachIn options.Keys()
me.options.insert(name, options.ValueForKey(name))
Next
For Local opt:TOpt = EachIn orderedOptions
me.orderedOptions.AddLast(opt)
Next
Return me
End Method
End Type
Function RunCommand:Object(command:String, args:String[])
Local cmd:TBMKCommand = TBMKCommand(commands.ValueForKey(command.ToLower()))
If cmd Then
' we need to add the "arg0" string to the front of the array
Local all:String
For Local i:Int = 0 Until args.length
Local arg:String = args[i]
all:+ CQuote$(arg) + " "
Next
args = [ all.Trim() ] + args
' now we can run the command
Return cmd.RunCommandArgs(args)
End If
End Function
' a bmk function/command
Type TBMKCommand
Field name:String
Field command:String
Field argCount:Int = 0
Field class:TLuaClass
Field instance:TLuaObject
Method LoadCommand(cmd:String)
cmd = WrapVariables(ParseArgs(cmd))
Local code:String = "function bmk_" + name + "(...)~n" + ..
GetArgs() + ..
"nvl = function(a1,a2) if a1 == nil then return a2 else return a1 end end~n" + ..
cmd + ..
"end"
class = New TLuaClass.SetSourceCode( code )
instance = New TLuaObject.Init( class, Null )
End Method
Method RunCommand:Object(args:String)
Return RunCommandArgs([args] + ExtractArgs(args))
End Method
' This assumes we have arg0 + other args
Method RunCommandArgs:Object(args:Object[])
Return instance.invoke("bmk_" + name, args)
End Method
' handles quotes and arrays [].
' [] inside quotes are ignored.
Method ExtractArgs:Object[](args:String)
Local argArray:Object[]
Local arg:String, arr:String[]
Local i:Int, inString:Int, inArray:Int
While i < args.length
Local c:String = args[i..i+1]
i:+ 1
If c = "~q" Then
If inString Then
If Not inArray Then
argArray:+ [ arg ]
Else
arr:+ [ arg ]
End If
arg = ""
inString = False
Continue
Else
arg = ""
inString = True
Continue
End If
End If
If c = " " And Not inString Then
If arg Then
If Not inArray Then
argArray:+ [ arg ]
Else
arr:+ [ arg ]
End If
arg = ""
End If
Continue
End If
If c = "[" And Not inString Then
If Not inArray Then
inArray = True
arr = Null
arg = ""
Continue
End If
End If
If c = "]" And Not inString Then
If inArray Then
If arg Then
arr:+ [ arg ]
End If
inArray = False
argArray:+ [ arr ]
arr = Null
arg = ""
Continue
End If
End If
arg:+ c
Wend
If arg Then
If arr Then
arr:+ [arg]
argArray:+ [arr]
Else
argArray:+ [arg]
End If
Else
If arr Then
argArray:+ [arr]
End If
End If
Return argArray
End Method
Method ParseArgs:String(cmd:String)
' This needs to process the command text to work out what args are used.
' so, for example, arg0, arg1 and arg2.
' That way, we generate the correct functionality when we build the function code.
Local pos:Int
While pos < cmd.length
Local eol:Int = cmd.Find( "~n",pos )
If eol = -1 Then
eol = cmd.length
End If
Local line:String = cmd[pos..eol].Trim()
pos = eol+1
Local i:Int
While i < line.length
i = line.find("arg", i)
If i = -1 Then
i = line.length
Continue
End If
i:+ 3
Local start:Int = i
While i < line.length And CharIsDigit(line[i])
i:+1
Wend
Local num:Int = line[start..i].ToInt()
If num Then
argCount = Max(argCount, num)
End If
Wend
Wend
Return cmd
End Method
Method GetArgs:String()
Local args:String = "local arg0"
Local rep:String = "arg0 = bmk.Parse(arg0)~n"
If argCount > 0 Then
For Local i:Int = 1 To argCount
args:+ ",arg" + i
rep :+ "arg" + i + " = bmk.Parse(arg" + i + ")~n"
Next
End If
args :+ " = unpack(arg)~n"
args :+ rep
Return args
End Method
Method WrapVariables:String(str:String)
Local done:Int
While Not done
Local pos:Int, restart:Int, changed:Int
While pos < str.length And Not restart
Local eol:Int = str.Find( "~n",pos )
If eol = -1 Then
eol = str.length
End If
Local line:String = str[pos..eol].Trim()
pos = eol+1
Local i:Int
While i < line.length
i = line.find("%", i)
If i = -1 Then
i = line.length
Continue
End If
Local start:Int = i
i:+ 1
While i < line.length And (CharIsAlpha(line[i]) Or CharIsDigit(line[i]))
i:+1
Wend
If i > start Then
If line[i..i+1] = "%" Then
i:+ 1
Local toReplace:String = line[start..i]
Local with:String = "globals.Get(~q" + toReplace.Replace("%", "") + "~q)"
str = str.Replace(toReplace, with)
restart = True
End If
End If
Wend
Wend
If Not restart Then
done = True
End If
Wend
Return str
End Method
End Type
?threaded
Type TProcessManager
Field cpuCount:Int
Field threads:TList = New TList
Method New()
cpuCount = GetCoreCount()
' single cpu boost...
If cpuCount = 1 Then
cpuCount = 2
End If
End Method
Method CheckThreads()
While threads.Count() = cpuCount
For Local thread:TThread = EachIn threads
If Not thread.Running() Then
threads.Remove(thread)
End If
Next
Delay 5
Wend
End Method
Method WaitForThreads()
While threads.Count()
For Local thread:TThread = EachIn threads
If Not thread.Running() Then
threads.Remove(thread)
End If
Next
Delay 5
Wend
End Method
Method DoSystem(cmd:String, src:String)
threads.AddLast(CreateThread(TProcessTask._DoTasks, New TProcessTask.Create(cmd, src)))
CheckThreads()
End Method
End Type
?Not win32
Extern
Function fork:Int()
Function bmx_waitpid:Int(pid:Int)
Function bmx_system(cmd:Byte Ptr)
End Extern
?
Type TProcessTask