forked from bmx-ng/bmk
-
Notifications
You must be signed in to change notification settings - Fork 0
/
bmk_ng.bmx
1555 lines (1217 loc) · 32.3 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 processor:TBMK = New TBMK
Global globals:TBMKGlobals = New TBMKGlobals
' load in the base stuff
LoadBMK(AppDir + "/core.bmk", True)
LoadBMK(AppDir + "/make.bmk", True)
' 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("c_opts", New TOptionVariable)
globals.SetVar("cpp_opts", New TOptionVariable)
'globals.SetVar("gcc_version", String(processor.GCCVersion()))
Function LoadBMK(path:String, required:Int = False)
processor.LoadBMK(path, required)
End Function
' this is the core bmk processor.
Type TBMK
Field commands:TMap = New TMap
Field buildLog:TList
Method New()
LuaRegisterObject Self,"bmk"
End Method
' loads a .bmk, stores any functions, and runs any commands.
Method LoadBMK(path:String, required:Int = False)
Local str:String
Try
If FileType(path) = 1 Then
str = LoadText( path )
If Int(globals.Get("verbose")) Or opt_verbose
Print "Loading " + path
End If
Else
If FileType(AppDir + "/" + path) = 1 Then
str = LoadText( AppDir + "/" + path )
If Int(globals.Get("verbose")) Or opt_verbose
Print "Loading " + AppDir + "/" + path
End If
Else
If FileType(globals.Get("BUILDPATH") + "/" + path) = 1 Then
str = LoadText(globals.Get("BUILDPATH") + "/" + path )
If Int(globals.Get("verbose")) Or opt_verbose
Print "Loading " + globals.Get("BUILDPATH") + "/" + path
End If
Else
If required Then
Throw "Could not load required config '" + path + "'"
End If
Return
End If
End If
End If
Catch e:Object
Try
If FileType(AppDir + "/" + path) = 1 Then
str = LoadText( AppDir + "/" + path )
If Int(globals.Get("verbose")) Or opt_verbose
Print "Loading " + AppDir + "/" + path
End If
Else
If FileType(globals.Get("BUILDPATH") + "/" + path) = 1 Then
str = LoadText(globals.Get("BUILDPATH") + "/" + path )
If Int(globals.Get("verbose")) Or opt_verbose
Print "Loading " + globals.Get("BUILDPATH") + "/" + path
End If
Else
If required Then
Throw "Could not load required config '" + path + "'"
End If
Return
End If
End If
Catch e:Object
' we tried... twice
' fail silently...
' unless the file was required!
If required Then
Throw "Could not load required config '" + path + "'"
End If
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
?raspberrypi
Return "raspberrypi"
?android
Return "android"
?macos
Return "macos"
?linux
Return "linux"
?win32
Return "win32"
?emscripten
Return "emscripten"
?
Else
' the custom target platform
Return opt_target_platform
End If
End Method
'returns the app type as a string ("gui", "console" ...)
Method AppType:String()
Return opt_apptype
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
Select Platform()
Case "macos"
If opt_arch = "ppc" Then
opt_arch = "x86"
Else
opt_arch = "ppc"
End If
Case "ios"
Select CPU()
Case "x86"
opt_arch = "x64"
Case "x64"
opt_arch = "x86"
Case "armv7"
opt_arch = "arm64"
Case "arm64"
opt_arch = "armv7"
End Select
End Select
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
If opt_standalone And Not opt_nolog PushLog(cmd)
If Not opt_standalone Or (opt_standalone And opt_nolog) Then
Return system_( cmd )
End If
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
If opt_standalone And Not opt_nolog PushLog(cmd)
If Not opt_standalone Or (opt_standalone And opt_nolog) Then
?threaded
processManager.DoSystem(cmd, src)
?Not threaded
Return system_( cmd )
?
End If
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, getRawVersion:Int = False)
'?win32
Global compiler:String
Global version:String
Global rawVersion:String
If compiler Then
If getVersionNum Then
If getRawVersion Then
Return rawVersion
Else
Return version
End If
Else
Return compiler + " " + version
End If
End If
Local process:TProcess
If Platform() = "win32" Then
process = CreateProcess(MinGWBinPath() + "/gcc.exe -v")
Else
process = CreateProcess("gcc -v")
End If
Local s:String
If Not process Then
Throw "Cannot find a valid GCC compiler. Please check your paths and environment."
End If
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(" ")
rawVersion = parts[2].Trim()
Local values:String[] = parts[2].split(".")
For Local v:String = EachIn values
Local n:String = "0" + v
s:+ n[n.length - 2..]
Next
Else If line.startswith("Target:") Then
_target = line[7..].Trim()
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
If getRawVersion Then
Return rawVersion
Else
Return version
End If
End If
Return compiler + " " + version
'?
End Method
Global _target:String
Method HasTarget:Int(find:String)
If Not _target Then
GCCVersion()
End If
If _target Then
If _target.Find(find) >= 0 Then
Return True
End If
End If
Return False
End Method
Method GCCVersionInt:Int()
End Method
Method BCCVersion:String()
Global bcc:String
If bcc Then
Return bcc
End If
Local exe:String = "bcc"
If Platform() = "win32" Then
exe :+ ".exe"
End If
Local process:TProcess = CreateProcess(CQuote(BlitzMaxPath() + "/bin/" + exe))
Local s:String
If Not process Then
Throw "Cannot find a valid bcc. I am looking for it here : " + BlitzMaxPath() + "/bin/" + exe
End If
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
Method MinGWBinPath:String()
Global _path:String
If Not _path Then
_path = MinGWPath() + "/bin"
?win32
Local PATH:String = _wgetenv("PATH")
PATH = _path + ";" + PATH
_wputenv("PATH=" + PATH)
?
End If
Return _path
End Method
Method MinGWPath:String()
Global _path:String
If Not _path Then
Local path:String
' look for local MinGW32 dir
' some distros (eg. MinGW-w64) only support a single target architecture - x86 or x64
' to compile for both, requires two separate MinGW installations. Check against
' CPU target based dir first, before working through the fallbacks.
Local cpuMinGW:String = "/MinGW32x86"
If processor.CPU()="x64" Then
cpuMinGW = "/MinGW32x64"
EndIf
path = BlitzMaxPath() + cpuMinGW + "/bin"
If FileType(path) = FILETYPE_DIR Then
' bin dir exists, go with that
_path = BlitzMaxPath() + cpuMinGW
Return _path
End If
path = BlitzMaxPath() + "/MinGW32/bin"
If FileType(path) = FILETYPE_DIR Then
' bin dir exists, go with that
_path = BlitzMaxPath() + "/MinGW32"
Return _path
End If
' try MINGW environment variable
path = getenv_("MINGW")
If path And FileType(path) = FILETYPE_DIR Then
' check for bin dir
If FileType(path + "/bin") = FILETYPE_DIR Then
' go with that
_path = path
Return _path
End If
End If
' none of the above? fallback to BlitzMax dir (for bin and lib)
_path = BlitzMaxPath()
End If
Return _path
End Method
Method MinGWLinkPaths:String()
Global _links:String
If Not _links Then
Local links:String
If processor.HasTarget("x86_64") Then
If processor.CPU()="x86" Then
links :+ " -L" + CQuote(RealPath(MinGWPath() + "/lib/gcc/x86_64-w64-mingw32/" + GCCVersion(True, True) + "/32"))
links :+ " -L" + CQuote(RealPath(MinGWPath() + "/x86_64-w64-mingw32/lib32"))
Else
links :+ " -L" + CQuote(RealPath(MinGWPath() + "/lib/gcc/x86_64-w64-mingw32/" + GCCVersion(True, True)))
links :+ " -L" + CQuote(RealPath(MinGWPath() + "/x86_64-w64-mingw32/lib"))
End If
Else
links :+ " -L" + CQuote(RealPath(MinGWPath() + "/lib"))
links :+ " -L" + CQuote(RealPath(MinGWPath() +"/lib/gcc/mingw32/" + GCCVersion(True, True)))
End If
_links = links
End If
Return _links
End Method
' the path where dllcrt2.o resides
Method MinGWDLLCrtPath:String()
Global _path:String
If Not _path Then
' mingw64 ?
Local path:String = MinGWPath() + "/"
If processor.HasTarget("x86_64") Then
path :+ "x86_64-w64-mingw32/"
If processor.CPU()="x86" Then
path :+ "lib32"
Else
path :+ "lib"
End If
If FileType(path) = 0 Then
Throw "Could not determine MinGWDLLCrtPath : Expecting '" + path + "'"
End If
_path = path
Else
path :+ "lib"
If FileType(path) = 0 Then
Throw "Could not determine MinGWDLLCrtPath : Expecting '" + path + "'"
End If
_path = path
End If
End If
Return RealPath(_path)
End Method
' the path where crtbegin.o resides
Method MinGWCrtPath:String()
Global _path:String
If Not _path Then
' mingw64 ?
Local path:String = MinGWPath() + "/"
If processor.HasTarget("x86_64") Then
path :+ "x86_64-w64-mingw32/"
If processor.CPU()="x86" Then
path :+ "lib32"
Else
path :+ "lib"
End If
If FileType(path) = 0 Then
Throw "Could not determine MinGWCrtPath: Expecting '" + path + "'"
End If
_path = path
Else
Local p:String = path + "lib/gcc/mingw32/" + GCCVersion(True, True)
If FileType(p) = 0 Then
path :+ "lib/gcc/i686-w64-mingw32/" + GCCVersion(True, True)
Else
path = p
End If
If FileType(path) = 0 Then
Throw "Could not determine MinGWCrtPath: Expecting '" + p + "' or '" + path + "'"
End If
_path = path
End If
End If
Return RealPath(_path)
End Method
Method IsDebugBuild:Int()
Return opt_debug
End Method
Method 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 Method
Method PushLog(cmd:String)
If Not buildLog Then
buildLog = New TList
End If
Local p:String = FixPaths(cmd)
buildLog.AddLast(p)
End Method
Method FixPaths:String(Text:String)
Local p:String = Text
p = p.Replace(BlitzMaxPath()+"/","$BMX_ROOT/")
p = p.Replace(String(globals.GetRawVar("EXEPATH")), "$APP_ROOT")
Return p
End Method
Method AppDet:String()
Return StripExt(StripDir(app_main)) + "." + opt_apptype + opt_configmung + processor.CPU()
End Method
End Type
?win32
Extern
Function _wgetenv$w(varname$w)
Function _wputenv:Int(varname$w)
End Extern
?
' 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
Method GetOptionVar:String(variable:String, name:String)
Local obj:TOptionVariable = TOptionVariable(vars.ValueForKey(variable.ToUpper()))
If obj Then
Return obj.GetVar(name)
End If
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)
If Not AsConfigurable(variable.ToLower(), value) Then
variable = variable.ToUpper()
Local v:Object = vars.ValueForKey(variable)
If Not TOptionVariable(v) Then
If v Then
SetVar(variable, String(v) + " " + value)
Else
SetVar(variable, value)
End If
End If
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)