forked from bmx-ng/bmk
-
Notifications
You must be signed in to change notification settings - Fork 0
/
bmk.bmx
547 lines (416 loc) · 12.9 KB
/
bmk.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
Strict
Framework brl.filesystem
Import "bmk_make.bmx"
Import "bmk_zap.bmx"
?MacOS
Import BRL.RamStream
Incbin "macos.icns"
?
If AppArgs.length<2 CmdError "Not enough parameters", True
Local cmd$=AppArgs[1],args$[]
args=ParseConfigArgs( AppArgs[2..], processor.BCCVersion() = "BlitzMax" )
' validate the platform configuration
ValidatePlatformArchitecture()
' preload the default options
processor.RunCommand("default_cc_opts", Null)
' load any global custom options (in BlitzMax/bin)
LoadOptions
CreateDir BlitzMaxPath()+"/tmp"
Select cmd.ToLower()
Case "makeapp"
SetConfigMung
MakeApplication args,False
Case "makelib"
SetConfigMung
MakeApplication args,True
Case "makemods"
opt_quickscan = False
If opt_debug Or opt_release
SetConfigMung
MakeModules args
If opt_universal
SetConfigMung
processor.ToggleCPU()
LoadOptions(True) ' reload options for PPC
MakeModules args
processor.ToggleCPU()
LoadOptions(True)
End If
Else
opt_debug=True
opt_release=False
SetConfigMung
MakeModules args
If opt_universal
SetConfigMung
processor.ToggleCPU()
LoadOptions(True) ' reload options for PPC
MakeModules args
processor.ToggleCPU()
LoadOptions(True)
End If
opt_debug=False
opt_release=True
SetConfigMung
MakeModules args
If opt_universal
SetConfigMung
processor.ToggleCPU()
LoadOptions(True) ' reload options for PPC
MakeModules args
processor.ToggleCPU()
LoadOptions(True)
End If
EndIf
Case "compile"
SetConfigMung
MakeApplication args,False,True
Case "cleanmods"
CleanModules args
Case "zapmod"
ZapModule args
Case "unzapmod"
UnzapModule args
Case "listmods"
ListModules args
Case "modstatus"
ModuleStatus args
Case "syncmods"
SyncModules args
Case "ranlibdir"
RanlibDir args
Case "-v"
VersionInfo(processor.GCCVersion(), GetCoreCount())
Default
CmdError "Unknown operation '" + cmd.ToLower() + "'"
End Select
Function SetConfigMung()
If opt_release
opt_debug=False
opt_configmung="release"
If processor.BCCVersion() = "BlitzMax" Then
If opt_threaded opt_configmung:+".mt"
End If
opt_configmung="."+opt_configmung+"."+processor.Platform()+"."'+opt_arch
Else
opt_debug=True
opt_release=False
opt_configmung="debug"
If processor.BCCVersion() = "BlitzMax" Then
If opt_threaded opt_configmung:+".mt"
End If
opt_configmung="."+opt_configmung+"."+processor.Platform()+"."'+opt_arch
EndIf
End Function
Function SetModfilter( t$ )
opt_modfilter=t.ToLower()
If opt_modfilter="*"
opt_modfilter=""
Else If opt_modfilter[opt_modfilter.length-1]<>"." And opt_modfilter.Find(".") < 0 Then
opt_modfilter:+"."
EndIf
End Function
Function MakeModules( args$[] )
If opt_standalone CmdError "Standalone build not available for makemods"
If args.length>1 CmdError "Expecting only 1 argument for makemods"
Local mods:TList
If args.length Then
Local m:String = args[0]
If m.find(".") > 0 And m[m.length-1]<>"." Then
' full module name?
mods = New TList
mods.AddLast(m)
SetModfilter m
Else
SetModfilter m
mods = EnumModules()
End If
Else
opt_modfilter=""
mods = EnumModules()
End If
BeginMake
Local buildManager:TBuildManager = New TBuildManager
buildManager.MakeMods(mods, opt_all)
buildManager.DoBuild(False)
End Function
Function CleanModules( args$[] )
If args.length>1 CmdError "Expecting only 1 argument for cleanmods"
If args.length SetModfilter args[0] Else opt_modfilter=""
Local mods:TList=EnumModules()
Local name$
For name=EachIn mods
If (name+".").Find(opt_modfilter)<>0 Continue
Print "Cleaning:"+name
Local path$=ModulePath(name)
DeleteDir path+"/.bmx",True
If Not opt_kill Continue
For Local f$=EachIn LoadDir( path )
Local p$=path+"/"+f
Select FileType(p)
Case FILETYPE_DIR
If f<>"doc"
DeleteDir p,True
EndIf
Case FILETYPE_FILE
Select ExtractExt(f).tolower()
Case "i","a","txt","htm","html"
'nop
Default
DeleteFile p
End Select
End Select
Next
Next
End Function
Function MakeApplication( args$[],makelib:Int,compileOnly:Int = False )
If opt_execute And Not compileOnly
If Len(args)=0 CmdError "Execute requires at least 1 argument"
Else
If Len(args)<>1 Then
If compileOnly Then
CmdError "Expecting only 1 argument for compile"
Else
CmdError "Expecting only 1 argument for makeapp"
End If
End If
EndIf
Local Main$=RealPath( args[0] )
Select ExtractExt(Main).ToLower()
Case ""
Main:+".bmx"
Case "c","cpp","cxx","mm","bmx"
Default
Throw "Unrecognized app source file type:"+ExtractExt(Main)
End Select
If FileType(Main)<>FILETYPE_FILE Throw "Unable to open source file '"+Main+"'"
If Not opt_outfile Then
opt_outfile = StripExt( Main )
Else
opt_outfile = RealPath(opt_outfile)
End If
' set some useful global variables
globals.SetVar("BUILDPATH", ExtractDir(opt_outfile))
globals.SetVar("EXEPATH", ExtractDir(opt_outfile))
globals.SetVar("OUTFILE", StripDir(StripExt(opt_outfile)))
' some more useful globals
If processor.Platform() = "macos" And opt_apptype="gui" And Not compileOnly Then
Local appId$=StripDir( opt_outfile )
globals.SetVar("APPID", appId)
' modify for bundle
globals.SetVar("EXEPATH", ExtractDir(opt_outfile+".app/Contents/MacOS/"+appId))
' make bundle dirs
Local exeDir$=opt_outfile+".app",d$
d=exeDir+"/Contents/MacOS"
Select FileType( d )
Case FILETYPE_NONE
CreateDir d,True
If FileType( d )<>FILETYPE_DIR
Throw "Unable to create application directory"
EndIf
Case FILETYPE_FILE
Throw "Unable to create application directory"
Case FILETYPE_DIR
End Select
d=exeDir+"/Contents/Resources"
Select FileType( d )
Case FILETYPE_NONE
CreateDir d
If FileType( d )<>FILETYPE_DIR
Throw "Unable to create resources directory"
EndIf
Case FILETYPE_FILE
Throw "Unable to create resources directory"
Case FILETYPE_DIR
End Select
End If
' generic pre process
LoadBMK(ExtractDir(Main) + "/pre.bmk")
' project-specific pre process
LoadBMK(ExtractDir(Main) + "/" + StripDir( opt_outfile ) + ".bmk")
If processor.Platform() = "win32" Then
If makelib
If ExtractExt(opt_outfile).ToLower()<>"dll" opt_outfile:+".dll"
Else
If ExtractExt(opt_outfile).ToLower()<>"exe" opt_outfile:+".exe"
EndIf
EndIf
If processor.Platform() = "macos" Or processor.Platform() = "osx" Then
If opt_apptype="gui" And Not compileOnly
'Local appId$=StripDir( opt_outfile )
Local appId$ = globals.Get("APPID")
Local exeDir$=opt_outfile+".app",d$,t:TStream
Rem
d=exeDir+"/Contents/MacOS"
Select FileType( d )
Case FILETYPE_NONE
CreateDir d,True
If FileType( d )<>FILETYPE_DIR
Throw "Unable to create application directory"
EndIf
Case FILETYPE_FILE
Throw "Unable to create application directory"
Case FILETYPE_DIR
End Select
d=exeDir+"/Contents/Resources"
Select FileType( d )
Case FILETYPE_NONE
CreateDir d
If FileType( d )<>FILETYPE_DIR
Throw "Unable to create resources directory"
EndIf
Case FILETYPE_FILE
Throw "Unable to create resources directory"
Case FILETYPE_DIR
End Select
End Rem
t=WriteStream( exeDir+"/Contents/Info.plist" )
If Not t Throw "Unable to create Info.plist"
t.WriteLine "<?xml version=~q1.0~q encoding=~qUTF-8~q?>"
t.WriteLine "<!DOCTYPE plist PUBLIC ~q-//Apple Computer//DTD PLIST 1.0//EN~q ~qhttp://www.apple.com/DTDs/PropertyList-1.0.dtd~q>"
t.WriteLine "<plist version=~q1.0~q>"
t.WriteLine "<dict>"
t.WriteLine "~t<key>CFBundleExecutable</key>"
t.WriteLine "~t<string>"+appId+"</string>"
t.WriteLine "~t<key>CFBundleIconFile</key>"
t.WriteLine "~t<string>"+appId+"</string>"
t.WriteLine "~t<key>CFBundlePackageType</key>"
t.WriteLine "~t<string>APPL</string>"
If globals.Get("custom_plist") Then
t.WriteLine "~t" + globals.Get("custom_plist")
End If
t.WriteLine "</dict>"
t.WriteLine "</plist>"
t.Close
t=WriteStream( exeDir+"/Contents/Resources/"+appId+".icns" )
If Not t Throw "Unable to create icons"
Local in:TStream=ReadStream( "incbin::macos.icns" )
CopyStream in,t
in.Close
t.Close
opt_outfile=exeDir+"/Contents/MacOS/"+appId
' Mac GUI exepath is in the bundle...
'globals.SetVar("EXEPATH", ExtractDir(opt_outfile))
'globals.SetVar("APPID", appId)
EndIf
End If
If processor.Platform() = "emscripten" Then
If ExtractExt(opt_outfile).ToLower()<>"html" opt_outfile:+".html"
End If
If processor.Platform() = "nx" Then
If ExtractExt(opt_outfile).ToLower()<>"elf" opt_outfile:+".elf"
End If
BeginMake
'MakeApp Main,makelib
Local buildManager:TBuildManager = New TBuildManager
' "android-project" check and copy
If processor.Platform() = "android" And Not compileOnly Then
DeployAndroidProject()
End If
buildManager.MakeApp(Main, makelib, compileOnly)
buildManager.DoBuild(makelib, Not compileOnly)
If opt_universal And processor.Platform() = "ios" Then
processor.ToggleCPU()
LoadOptions(True) ' reload options for PPC
BeginMake
Local buildManager:TBuildManager = New TBuildManager
buildManager.MakeApp(Main, makelib, compileOnly)
buildManager.DoBuild(False, True)
processor.ToggleCPU()
LoadOptions(True)
End If
Rem
If opt_universal
Local previousOutfile:String = opt_outfile
processor.ToggleCPU()
LoadOptions(True) ' reload options for PPC
opt_outfile :+ "." + processor.CPU()
BeginMake
MakeApp Main,makelib
processor.ToggleCPU()
LoadOptions(True)
MergeApp opt_outfile, previousOutfile
opt_outfile = previousOutfile
End If
End Rem
If processor.Platform() = "nx" And Not compileOnly Then
BuildNxDependencies()
End If
If opt_standalone And Not compileOnly
Local buildScript:String = String(globals.GetRawVar("EXEPATH")) + "/" + StripExt(StripDir( app_main )) + "." + opt_apptype + opt_configmung + processor.CPU() + ".build"
Local ldScript:String = "$APP_ROOT/ld." + processor.AppDet() + ".txt"
Local stream:TStream = WriteStream(buildScript)
stream.WriteString("echo ~qBuilding " + String(globals.GetRawVar("OUTFILE")) + "...~q~n~n")
stream.WriteString("if [ -z ~q${APP_ROOT}~q ]; then~n")
stream.WriteString("~tAPP_ROOT=" + String(globals.GetRawVar("EXEPATH")) + "~n")
stream.WriteString("fi~n~n")
stream.WriteString("if [ -z ~q${BMX_ROOT}~q ]; then~n")
stream.WriteString("~tBMX_ROOT=" + BlitzMaxPath() + "~n")
stream.WriteString("fi~n")
stream.WriteString("~n~n")
stream.WriteString("cp " + ldScript + " " + ldScript + ".tmp~n")
stream.WriteString("sed -i -- 's=\$BMX_ROOT='$BMX_ROOT'=g' " + ldScript + ".tmp~n")
stream.WriteString("sed -i -- 's=\$APP_ROOT='$APP_ROOT'=g' " + ldScript + ".tmp~n")
stream.WriteString("~n~n")
If processor.buildLog Then
For Local s:String = EachIn processor.buildLog
stream.WriteString(s + "~n")
Next
End If
stream.WriteString("~n")
stream.WriteString("echo ~qFinished.~q~n")
stream.Close()
End If
If opt_execute And Not compileOnly
?Not android
Print "Executing:"+StripDir( opt_outfile )
Local cmd$=CQuote( opt_outfile )
For Local i=1 Until args.length
cmd:+" "+CQuote( args[i] )
Next
Sys cmd
?android
' on android we'll deploy the apk
?
EndIf
End Function
Function ZapModule( args$[] )
If Len(args)<>2 CmdError "Both module name and outfile required"
Local modname$=args[0].ToLower()
Local outfile$=RealPath( args[1] )
Local stream:TStream=WriteStream( outfile )
If Not stream Throw "Unable to open output file"
ZapMod modname,stream
stream.Close
End Function
Function UnzapModule( args$[] )
If Len(args)<>1 CmdError "Expecting 1 argument for unzapmod"
Local infile$=args[0]
Local stream:TStream=ReadStream( infile )
If Not stream Throw "Unable to open input file"
UnzapMod stream
stream.Close
End Function
Function ListModules( args$[],modid$="" )
If Len(args)<>0 CmdError
Throw "Todo!"
End Function
Function ModuleStatus( args$[] )
If Len(args)<>1 CmdError
ListModules Null,args[0]
End Function
Function SyncModules( args$[] )
If args.length CmdError
If Sys( BlitzMaxPath()+"/bin/syncmods" ) Throw "SyncMods error"
End Function
Function RanlibDir( args$[] )
If args.length<>1 CmdError "Expecting 1 argument for ranlibdir"
Ranlib args[0]
End Function
Function LoadOptions(reload:Int = False)
If reload Then
' reset the options to default
processor.RunCommand("default_cc_opts", Null)
End If
LoadBMK(AppDir + "/custom.bmk")
End Function