forked from bmx-ng/bmk
-
Notifications
You must be signed in to change notification settings - Fork 0
/
bmk_util.bmx
1611 lines (1253 loc) · 41.5 KB
/
bmk_util.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
Strict
Import "bmk_config.bmx"
Import "bmk_ng.bmx"
'OS X Nasm doesn't work? Used to produce incorrect reloc offsets - haven't checked for a while
Const USE_NASM=False
Const CC_WARNINGS=False'True
Type TModOpt ' BaH
Field cc_opts:String = ""
Field ld_opts:TList = New TList
Field cpp_opts:String = ""
Field c_opts:String = ""
Method addOption(qval:String)
If qval.startswith("CC_OPTS") Then
cc_opts:+ " " + ReQuote(qval[qval.find(":") + 1..].Trim())
ElseIf qval.startswith("CPP_OPTS") Then
cpp_opts:+ " " + ReQuote(qval[qval.find(":") + 1..].Trim())
ElseIf qval.startswith("C_OPTS") Then
c_opts:+ " " + ReQuote(qval[qval.find(":") + 1..].Trim())
ElseIf qval.startswith("LD_OPTS") Then
Local opt:String = ReQuote(qval[qval.find(":") + 1..].Trim())
If opt.startsWith("-L") Then
opt = "-L" + CQuote(opt[2..])
End If
ld_opts.addLast opt
End If
End Method
Method hasCCopt:Int(value:String)
Return cc_opts.find(value) >= 0
End Method
Method hasCPPopt:Int(value:String)
Return cpp_opts.find(value) >= 0
End Method
Method hasCopt:Int(value:String)
Return c_opts.find(value) >= 0
End Method
Method hasLDopt:Int(value:String)
For Local opt:String = EachIn ld_opts
If opt.find(value) >= 0 Then
Return True
End If
Next
Return False
End Method
Function setPath:String(value:String, path:String)
Return value.Replace("%PWD%", path)
End Function
End Type
Global mod_opts:TModOpt ' BaH
Function Match( ext$,pat$ )
Return (";"+pat+";").Find( ";"+ext+";" )<>-1
End Function
Function HTTPEsc$( t$ )
t=t.Replace( " ","%20" )
Return t
End Function
Function Sys( cmd$ )
If opt_verbose
Print cmd
Else If opt_dumpbuild
Local p$=cmd
p=p.Replace( BlitzMaxPath()+"/","./" )
WriteStdout p+"~n"
Local t$="mkdir "
If cmd.StartsWith( t ) And FileType( cmd[t.length..] ) Return
EndIf
Return system_( cmd )
End Function
Function Ranlib( dir$ )
'
?MacOS
If macos_version>=$1040 Return
?
'
For Local f$=EachIn LoadDir( dir )
Local p$=dir+"/"+f
Select FileType( p )
Case FILETYPE_DIR
Ranlib p
Case FILETYPE_FILE
If ExtractExt(f).ToLower()="a" Sys "ranlib "+p
End Select
Next
End Function
Function Assemble( src$,obj$ )
processor.RunCommand("assemble", [src, obj])
End Function
Function AssembleNative( src$, obj$ )
processor.RunCommand("assembleNative", [src, obj])
End Function
Function Fasm2As( src$,obj$ )
processor.RunCommand("fasm2as", [src, obj])
End Function
Function CompileC( src$,obj$,opts$ )
processor.RunCommand("CompileC", [src, obj, opts])
End Function
Function CompileBMX( src$,obj$,opts$ )
DeleteFile obj
Local azm$=StripExt(obj)
If processor.BCCVersion() = "BlitzMax" Then
' remove any "NG" generated source.
DeleteFile azm + ".c"
azm :+ ".s"
Else
' remove any "legacy" generated source.
DeleteFile azm + ".s"
opts :+ " -p " + processor.Platform()
End If
If opt_standalone opt_nolog = True
processor.RunCommand("CompileBMX", [src, azm, opts])
If opt_standalone opt_nolog = False
End Function
Function CreateMergeArc( path$ , arc_path:String )
Local cmd$
If processor.Platform() = "ios" Then
Local proc:String = processor.CPU()
Local opp:String
Select proc
Case "x86"
proc = "i386"
opp = "x86_64"
Case "x64"
proc = "x86_64"
opp = "i386"
Case "armv7"
opp = "arm64"
Case "arm64"
opp = "armv7"
End Select
cmd = "lipo "
If Not FileType(path) Then
cmd :+ "-create -arch_blank " + opp + " -arch "
Else
cmd :+ CQuote(path)
cmd :+ " -replace "
End If
cmd :+ proc + " " + CQuote(arc_path)
cmd :+ " -output " + CQuote(path)
End If
If cmd And processor.MultiSys( cmd, path )
DeleteFile path
Throw "Build Error: Failed to merge archive " + path
EndIf
End Function
Function CreateArc( path$ , oobjs:TList )
DeleteFile path
Local cmd$,t$
If processor.Platform() = "win32"
For t$=EachIn oobjs
If Len(cmd)+Len(t)>1000
If processor.Sys( cmd )
DeleteFile path
Throw "Build Error: Failed to create archive "+path
EndIf
cmd=""
EndIf
If Not cmd cmd= processor.Option("path_to_ar", processor.MinGWBinPath() + "/ar.exe") + " -r "+CQuote(path)
cmd:+" "+CQuote(t)
Next
End If
If processor.Platform() = "macos" Or processor.Platform() = "osx" Then
cmd="libtool -o "+CQuote(path)
For Local t$=EachIn oobjs
cmd:+" "+CQuote(t)
Next
End If
If processor.Platform() = "ios" Then
Local proc:String = processor.CPU()
Select proc
Case "x86"
proc = "i386"
Case "x64"
proc = "x86_64"
End Select
cmd="libtool -static -arch_only " + proc + " -o "+CQuote(path)
For Local t$=EachIn oobjs
cmd:+" "+CQuote(t)
Next
End If
If processor.Platform() = "linux" Or processor.Platform() = "raspberrypi" Or processor.Platform() = "android" Or processor.Platform() = "emscripten"
For Local t$=EachIn oobjs
If Len(cmd)+Len(t)>1000
If processor.Sys( cmd )
DeleteFile path
Throw "Build Error: Failed to create archive "+path
EndIf
cmd=""
EndIf
If processor.Platform() = "emscripten" Then
If Not cmd cmd=processor.Option(processor.BuildName("ar"), "emar") + " r "+CQuote(path)
Else
If Not cmd cmd=processor.Option(processor.BuildName("ar"), "ar") + " -r "+CQuote(path)
End If
cmd:+" "+CQuote(t)
Next
End If
If cmd And processor.MultiSys( cmd, path )
DeleteFile path
Throw "Build Error: Failed to create archive "+path
EndIf
End Function
Function LinkApp( path$,lnk_files:TList,makelib,opts$ )
If processor.Platform() = "ios" Then
PackageIOSApp(path, lnk_files, opts)
Return
End If
DeleteFile path
Local cmd$
Local files$
Local tmpfile$=BlitzMaxPath()+"/tmp/ld.tmp"
Local sb:TStringBuffer = New TStringBuffer
Local fb:TStringBuffer = New TStringBuffer
If opt_standalone tmpfile = String(globals.GetRawVar("EXEPATH")) + "/ld." + processor.AppDet() + ".txt.tmp"
If processor.Platform() = "macos" Or processor.Platform() = "osx" Then
sb.Append("g++")
If processor.CPU()="ppc"
sb.Append(" -arch ppc" )
Else If processor.CPU()="x86"
sb.Append(" -arch i386 -read_only_relocs suppress")
Else
sb.Append(" -arch x86_64")
EndIf
sb.Append(" -o ").Append(CQuote( path ))
sb.Append(" ").Append(CQuote("-L" +CQuote( BlitzMaxPath()+"/lib" ) ))
If Not opt_dumpbuild Then
sb.Append(" -filelist ").Append(CQuote( tmpfile ))
End If
For Local t$=EachIn lnk_files
If opt_dumpbuild Or (t[..1]="-")
sb.Append(" ").Append(t)
Else
fb.Append(t).Append(Chr(10))
EndIf
Next
sb.Append(" -lSystem -framework CoreServices -framework CoreFoundation")
If opts Then
sb.Append(" ").Append(opts)
End If
If processor.CPU() = "ppc"
sb.Append(" -lc -lgcc_eh")
End If
End If
If processor.Platform() = "win32"
Local version:Int = Int(processor.GCCVersion(True))
Local usingLD:Int = False
' always use g++ instead of LD...
' uncomment if we want to change to only use LD for GCC's < 4.x
'If version < 40000 Then
' usingLD = True
'End If
' or we can override in the config...
If globals.Get("link_with_ld") Or (version >= 40600 And version < 60000) Then
usingLD = True
End If
Local blitzMaxLibDir:String = "/lib"
If processor.CPU()="x64" Then
blitzMaxLibDir = "/lib64"
End If
If usingLD Then
sb.Append(CQuote(processor.Option("path_to_ld", processor.MinGWBinPath()+ "/ld.exe"))).Append(" -stack 4194304")
sb.Append(processor.option("strip.debug", " -s "))
If opt_apptype="gui" Then
sb.Append(" -subsystem windows")
End If
Else
sb.Append(CQuote(processor.Option("path_to_gpp", processor.MinGWBinPath() + "/g++.exe")))
If version < 60000 Then
sb.Append(" --stack=4194304")
Else
sb.Append(" -Wl,--stack,4194304")
End If
sb.Append(processor.option("strip.debug", " -s "))
If opt_apptype="gui"
If version < 60000 Then
sb.Append(" --subsystem,windows -mwindows")
Else
sb.Append(" -Wl,--subsystem,windows -mwindows")
End If
Else
If Not makelib
sb.Append(" -mconsole")
End If
End If
If opt_threaded Then
If version < 60000 Then
sb.Append(" -mthread")
Else
sb.Append(" -mthreads")
End If
End If
End If
If makelib Then
sb.Append(" -shared")
Else
sb.Append(" -static")
End If
sb.Append(" -o ").Append(CQuote( path ))
If usingLD Then
If processor.CPU()="x86"
sb.Append(" ").Append(processor.MinGWLinkPaths()) ' the BlitzMax lib folder
' linking for x86 when using mingw64 binaries
If processor.HasTarget("x86_64") Then
sb.Append(" -mi386pe")
End If
Else
sb.Append(" ").Append(processor.MinGWLinkPaths()) ' the BlitzMax lib folder
End If
If globals.Get("path_to_mingw_lib") Then
sb.Append(" ").Append(CQuote( "-L"+CQuote( RealPath(processor.Option("path_to_mingw_lib", BlitzMaxPath()+"/lib") ) ) ))
End If
If globals.Get("path_to_mingw_lib2") Then
sb.Append(" ").Append(CQuote( "-L"+CQuote( RealPath(processor.Option("path_to_mingw_lib2", BlitzMaxPath()+"/lib") ) ) ))
End If
If globals.Get("path_to_mingw_lib3") Then
sb.Append(" ").Append(CQuote( "-L"+CQuote( RealPath(processor.Option("path_to_mingw_lib3", BlitzMaxPath()+"/lib") ) ) ))
End If
End If
If makelib
Local imp$=StripExt(path)+".a"
Local def$=StripExt(path)+".def"
If FileType( def )<>FILETYPE_FILE Throw "Cannot locate .def file"
sb.Append(" ").Append(def)
sb.Append(" --out-implib ").Append(imp)
If usingLD Then
fb.Append(" ").Append(CQuote( RealPath(processor.Option("path_to_mingw_lib", processor.MinGWDLLCrtPath()) + "/dllcrt2.o" ) ))
End If
Else
If usingLD
fb.Append(" ").Append(CQuote( RealPath(processor.Option("path_to_mingw_lib2", processor.MinGWCrtPath()) + "/crtbegin.o" ) ))
fb.Append(" ").Append(CQuote( RealPath(processor.Option("path_to_mingw_lib", processor.MinGWDLLCrtPath()) + "/crt2.o" ) ))
End If
EndIf
Local xpmanifest$
For Local f$=EachIn lnk_files
Local t$=CQuote( f )
If opt_dumpbuild Or (t[..1]="-" And t[..2]<>"-l")
sb.Append(" ").Append(t)
Else
If f.EndsWith( "/win32maxguiex.mod/xpmanifest.o" )
xpmanifest=t
Else
fb.Append(" ").Append(t)
EndIf
EndIf
Next
If xpmanifest Then
fb.Append(" ").Append(xpmanifest)
End If
sb.Append(" ").Append(CQuote( tmpfile ))
fb.Append(" -lgdi32 -lwsock32 -lwinmm -ladvapi32")
' add any user-defined linker options
fb.Append(" ").Append(opts)
If usingLD
If opts.Find("stdc++") = -1 Then
fb.Append(" -lstdc++")
End If
fb.Append(" -lmingwex")
' for a native Win32 runtiime of mingw 3.4.5, this needs to appear early.
'If Not processor.Option("path_to_mingw", "") Then
fb.Append(" -lmingw32")
'End If
If opts.Find("gcc") = -1 Then
fb.Append(" -lgcc")
End If
' if using 4.8+ or mingw64, we need to link to pthreads
If version >= 40800 Or processor.HasTarget("x86_64") Then
fb.Append(" -lwinpthread ")
If processor.CPU()="x86" Then
fb.Append(" -lgcc")
End If
End If
fb.Append(" -lmoldname -lmsvcrt ")
End If
fb.Append(" -luser32 -lkernel32 ")
'If processor.Option("path_to_mingw", "") Then
' for a non-native Win32 runtime, this needs to appear last.
' (Actually, also for native gcc 4.x, but I dunno how we'll handle that yet!)
If usingLD
fb.Append(" -lmingw32 ")
End If
' add any user-defined linker options, again - just to cover whether we missed dependencies before.
fb.Append(" ").Append(opts)
'End If
If Not makelib
If usingLD
fb.Append(" ").Append(CQuote( processor.Option("path_to_mingw_lib2", processor.MinGWCrtPath()) + "/crtend.o" ))
End If
EndIf
fb.Insert(0,"INPUT(").Append(")")
End If
If processor.Platform() = "linux" Or processor.Platform() = "raspberrypi"
sb.Append(processor.Option(processor.BuildName("gpp"), "g++"))
'cmd:+" -m32 -s -Os -pthread"
If processor.CPU() = "x86" Or processor.CPU() = "arm" Then
sb.Append(" -m32")
End If
If processor.CPU() = "x64" Or processor.CPU() = "arm64" Then
sb.Append(" -m64")
End If
If opt_static Then
sb.Append(" -static")
End If
sb.Append(" -pthread")
sb.Append(" -o ").Append(CQuote( path ))
sb.Append(" ").Append(CQuote( tmpfile ))
If processor.CPU() = "x86" Then
sb.Append(" -L").Append(processor.Option(processor.BuildName("lib32"), "/usr/lib32"))
End If
sb.Append(" -L").Append(processor.Option(processor.BuildName("x11lib"), "/usr/X11R6/lib"))
sb.Append(" -L").Append(processor.Option(processor.BuildName("lib"), "/usr/lib"))
sb.Append(" -L").Append(CQuote( BlitzMaxPath()+"/lib" ))
For Local t$=EachIn lnk_files
t=CQuote(t)
If opt_dumpbuild Or (t[..1]="-" And t[..2]<>"-l")
sb.Append(" ").Append(t)
Else
fb.Append(" ").Append(t)
EndIf
Next
fb.Insert(0,"INPUT(").Append(")")
End If
If processor.Platform() = "android" Then
sb.Append(processor.Option(processor.BuildName("gpp"), "g++"))
Local libso:String = StripDir(path)
sb.Append(" -fPIC -shared ")
' for stlport shared lib
sb.Append(" -L").Append(AndroidSTLPortDir())
sb.Append(" -Wl,-soname,lib").Append(libso).Append(".so ")
sb.Append(" -Wl,--export-dynamic -rdynamic ")
sb.Append(" -o ").Append(CQuote( ExtractDir(path) + "/lib" + libso + ".so" ))
sb.Append(" ").Append(CQuote( tmpfile ))
sb.Append(" ").Append(processor.Option("android.platform.sysroot", ""))
For Local t$=EachIn lnk_files
t=CQuote(t)
If opt_dumpbuild Or (t[..1]="-" And t[..2]<>"-l")
sb.Append(" ").Append(t)
Else
fb.Append(" ").Append(t)
EndIf
Next
sb.Append(" -Wl,-Bdynamic -lGLESv2 -lGLESv1_CM ")
' libstlport
sb.Append(" -lstlport_shared")
sb.Append(" -llog -ldl -landroid ")
fb.Insert(0,"INPUT(").Append(")")
End If
If processor.Platform() = "emscripten"
sb.Append(processor.Option(processor.BuildName("gpp"), "em++"))
' cmd:+" -pthread" ' No threading support yet...
sb.Append(" -o ").Append(CQuote( path ))
'cmd:+" -filelist "+CQuote( tmpfile )
sb.Append(" ").Append(opts)
For Local t$=EachIn lnk_files
t=CQuote(t)
'If opt_dumpbuild Or (t[..1]="-" And t[..2]<>"-l")
sb.Append(" ").Append(t)
'Else
' files:+" "+t
'EndIf
Next
fb.Insert(0,"INPUT(").Append(")")
End If
Local t$=getenv_( "BMK_LD_OPTS" )
If t
sb.Append(" ").Append(t)
EndIf
cmd = sb.ToString()
files = fb.ToString()
If Not opt_standalone Then
Local stream:TStream=WriteStream( tmpfile )
stream.WriteBytes files.ToCString(),files.length
stream.Close
End If
If processor.Sys( cmd ) Throw "Build Error: Failed to link "+path
If opt_standalone
Local stream:TStream=WriteStream( StripExt(tmpfile) )
Local f:String = processor.FixPaths(files)
stream.WriteBytes f.ToCString(),f.length
stream.Close
End If
End Function
Function MergeApp(fromFile:String, toFile:String)
If Not opt_quiet Print "Merging:"+StripDir(fromFile) + " + " + StripDir(toFile)
Local cmd:String = "lipo -create ~q" + fromFile + "~q ~q" + toFile + "~q -output ~q" + toFile + "~q"
If processor.Sys( cmd ) Throw "Merge Error: Failed to merge " + toFile
DeleteFile fromFile
End Function
Function DeployAndroidProject()
Local appId:String = StripDir(StripExt(opt_outfile))
If opt_debug And opt_outfile.EndsWith(".debug") Then
appId :+ ".debug"
End If
Local buildDir:String = ExtractDir(opt_outfile)
' eg. android-project-test_01
Local projectDir:String = buildDir + "/android-project-" + appId '+ "-" + processor.CPU()
' check for dir
If Not FileType(projectDir) Then
' doesn't exist. create it
Local resourceProject:String = BlitzMaxPath() + "/resources/android/android-project"
If Not FileType(resourceProject) Then
Throw "Missing resources folder for Android build : " + resourceProject
End If
CopyDir(resourceProject, projectDir)
End If
' check for valid dir
If FileType(projectDir) <> FILETYPE_DIR Then
Throw "Error creating project dir '" + projectDir + "'"
End If
' create assets dir if missing
Local assetsDir:String = projectDir + "/assets"
If opt_all Then
' remove assets if we are doing a full build
DeleteDir(assetsDir, True)
End If
If FileType(assetsDir) <> FILETYPE_DIR Then
CreateDir(assetsDir)
If FileType(assetsDir) <> FILETYPE_DIR Then
Throw "Error creating assests dir '" + assetsDir + "'"
End If
End If
' create libs/abi dir if missing
Local abiDir:String = projectDir + "/libs"
If FileType(abiDir) <> FILETYPE_DIR Then
CreateDir(abiDir)
If FileType(abiDir) <> FILETYPE_DIR Then
Throw "Error creating libs dir '" + abiDir + "'"
End If
End If
abiDir :+ "/" + GetAndroidArch()
If FileType(abiDir) <> FILETYPE_DIR Then
CreateDir(abiDir)
If FileType(abiDir) <> FILETYPE_DIR Then
Throw "Error creating libs abi dir '" + abiDir + "'"
End If
End If
' copy stlport
Local stlportDest:String = abiDir + "/libstlport_shared.so"
If opt_all Or Not FileType(stlportDest) Then
Local stlportSrc:String = AndroidSTLPortDir() + "/libstlport_shared.so"
CopyFile(stlportSrc, stlportDest)
If Not FileType(stlportDest) Then
Throw "Error copying libstlport_shared.so from '" + stlportSrc + "'"
End If
End If
Local projectSettings:TMap = ParseAndroidIniFile()
Local appPackage:String = String(projectSettings.ValueForKey("app.package"))
Local packagePath:String = projectDir + "/src/" + PathFromPackage(appPackage)
' create the package
If Not FileType(packagePath) Then
CreateDir(packagePath, True)
If FileType(packagePath) <> FILETYPE_DIR Then
Throw "Error creating package '" + packagePath + "'"
End If
End If
' copy/create java
Local gameClassFile:String = packagePath+ "/BlitzMaxApp.java"
If Not FileType(gameClassFile) Then
CopyFile(projectDir + "/BlitzMaxApp.java", gameClassFile)
If Not FileType(gameClassFile) Then
Throw "Error creating class file '" + gameClassFile + "'"
End If
End If
' merge project data
' update AndroidManifest.xml
MergeFile(projectDir, "AndroidManifest.xml", projectSettings)
' update BlitzMaxApp.java
MergeFile(packagePath, "BlitzMaxApp.java", projectSettings)
Local javaApp:String = LoadString( gameClassFile )
' set the package
javaApp = ReplaceBlock( javaApp, "app.package","package " + appPackage + ";" )
' lib imports
javaApp = ReplaceBlock( javaApp, "lib.imports", GetAndroidLibImports() )
SaveString(javaApp, gameClassFile)
' update strings.xml
MergeFile(projectDir + "/res/values", "strings.xml", projectSettings)
' update build.xml
MergeFile(projectDir, "build.xml", projectSettings)
' set the sdk target
Local projectPropertiesFile:String = projectDir + "/project.properties"
Local projectProperties:String = LoadString( projectPropertiesFile )
projectProperties = ReplaceBlock( projectProperties, "sdk.target","target=android-" + processor.option("android.sdk.target", ""), "~n#")
SaveString(projectProperties, projectPropertiesFile)
' copy resources to assets
CopyAndroidResources(buildDir, assetsDir)
End Function
Function GetAndroidLibImports:String()
Local imports:String
imports = "System.loadLibrary( ~qstlport_shared~q);~n"
' TODO : others imported via project...
Return imports
End Function
Function GetAndroidArch:String()
Local arch:String
Select processor.CPU()
Case "x86"
arch = "x86"
Case "x64"
arch = "x86_64"
Case "arm"
arch = "armeabi-v7a"
Case "armeabi"
arch = "armeabi"
Case "armeabiv7a"
arch = "armeabi-v7a"
Case "arm64v8a"
arch = "arm64-v8a"
Default
Throw "Not a valid architecture '" + processor.CPU() + "'"
End Select
Return arch
End Function
Function AndroidSTLPortDir:String()
Return processor.Option("android.ndk", "") + "/sources/cxx-stl/stlport/libs/" + GetAndroidArch()
End Function
Function CopyAndroidResources(buildDir:String, assetsDir:String)
Local paths:String[] = SplitPaths(String(globals.GetRawVar("resource_path")))
If paths Then
For Local dir:String = EachIn paths
Local resourceDir:String = buildDir + "/" + dir
If Not FileType(resourceDir) Then
Print "Warning : Defined resource_path '" + dir + "' not found"
End If
If FileType(resourceDir) = FILETYPE_DIR Then
CopyDir assetsDir + "/" + dir, resourceDir
End If
Next
End If
End Function
Function ParseAndroidIniFile:TMap()
Local appId:String = StripDir(StripExt(opt_outfile))
Local buildDir:String = ExtractDir(opt_outfile)
Local path:String = ExtractDir(opt_outfile) + "/" + appId + ".android"
Local settings:TMap = New TMap
If Not FileType(path) Then
Print "Not Found : application settings file '" + appId + ".android'. Using defaults..."
Return DefaultAndroidSettings()
End If
Local file:TStream = ReadFile(path)
If Not file
Return Null
EndIf
Local line:String
Local pos:Int
While Not Eof(file)
line = ReadLine(file).Trim()
If line.Find("#") = 0 Then
Continue
End If
pos = line.Find("=")
If pos = -1 Then
Continue
End If
settings.Insert(line[..pos], line[pos+1..])
Wend
file.Close()
Local id:String = StripDir(StripExt(opt_outfile))
If opt_debug And opt_outfile.EndsWith(".debug") Then
id :+ ".debug"
End If
settings.Insert("app.id", id)
Return settings
End Function
Function DefaultAndroidSettings:TMap()
Local settings:TMap = New TMap
settings.Insert("app.package", "com.blitzmax.android")
settings.Insert("app.version.code", "1")
settings.Insert("app.version.name", "1.0")
settings.Insert("app.name", "BlitzMax Application")
settings.Insert("app.orientation", "landscape")
Local appId:String = StripDir(StripExt(opt_outfile))
If opt_debug And opt_outfile.EndsWith(".debug") Then
appId :+ ".debug"
End If
settings.Insert("app.id", appId)
Return settings
End Function
Function GetAndroidSDKTarget:String()
Local sdkPath:String = processor.Option("android.sdk", "") + "/platforms"
Local target:String = processor.Option("android.sdk.target", "")
Local targetPath:String
If target Then
targetPath = sdkPath + "/android-" + target
If FileType(targetPath) = FILETYPE_DIR Then
Return target
End If
End If
' find highest numbered target platform dir
Local dirs:String[] = LoadDir(sdkPath, True)
Local high:Int
For Local dir:String = EachIn dirs
Local index:Int = dir.Find("android-")
If index >= 0 Then
Local value:Int = dir[index + 8..].ToInt()
high = Max(value, high)
End If
Next
If high > 0 Then
Return high
End If
End Function
Function PathFromPackage:String(package:String)
Return package.Replace(".", "/")
End Function
Function MergeFile(dir:String, file:String, settings:TMap)
Local s:String = LoadString(dir + "/" + file)
s = ReplaceEnv(s, settings)
SaveString(s, dir + "/" + file)
End Function
Function ReplaceEnv:String( str:String, settings:TMap )
Local bits:TStringStack = New TStringStack
Repeat
Local i:Int = str.Find( "${" )
If i=-1 Exit
Local e:Int = str.Find( "}",i+2 )
If e=-1 Exit
If i>=2 And str[i-2..i] = "//" Then
bits.AddLast str[..e+1]
str = str[e+1..]
Continue
EndIf
Local t:String = str[i+2..e]
Local v:String = String(settings.ValueForKey(t))
If Not v Then
v = "${" + t + "}"
End If
bits.AddLast str[..i]
bits.AddLast v
str = str[e+1..]
Forever
If bits.IsEmpty() Then
Return str
End If
bits.AddLast str
Return bits.Join( "" )
End Function
Function ReplaceBlock:String( Text:String,tag:String,repText:String,mark:String="~n//" )
'find begin tag
Local beginTag:String = mark+"${start."+tag+"}"
Local i:Int = Text.Find( beginTag )
If i=-1 Throw "Error updating target project - can't find block begin tag '"+tag+"'."
i :+ beginTag.Length
While i < Text.Length And Text[i-1]<>10
i :+ 1
Wend
'find end tag
Local endTag:String = mark+"${end."+tag+"}"
Local i2:Int = Text.Find( endTag,i-1 )
If i2=-1 Throw "Error updating target project - can't find block end tag '"+tag+"'."
If Not repText Or repText[repText.Length-1]=10 Then
i2 :+ 1
End If
Return Text[..i]+repText+Text[i2..]
End Function
Function SplitPaths:String[](paths:String)
Local split:String[] = New String[0]
Local inQuote:Int
Local token:String
For Local i:Int = 0 Until paths.length
Local char:Int = paths[i]
If char = Asc("~q") Then
inQuote = Not inQuote
Else If char = Asc(" ") And Not inQuote Then
token = token.Trim()
If token Then
split :+ [token]
End If
token = Null
Else
token :+ Chr(char)
End If
Next
token = token.Trim()
If token Then
split :+ [token]
End If
Return split
End Function
Function PackageIOSApp( path$, lnk_files:TList, opts$ )
Local templatePath:String = BlitzMaxPath() + "/resources/ios/template"
If Not FileType(templatePath) Then