forked from fantaisie-software/purebasic
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathCompilerInterface.pb
More file actions
2420 lines (1957 loc) · 85 KB
/
CompilerInterface.pb
File metadata and controls
2420 lines (1957 loc) · 85 KB
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
;--------------------------------------------------------------------------------------------
; Copyright (c) Fantaisie Software. All rights reserved.
; Dual licensed under the GPL and Fantaisie Software licenses.
; See LICENSE and LICENSE-FANTAISIE in the project root for license information.
;--------------------------------------------------------------------------------------------
; Set this to 1 to debug all commiunication with the compiler
; We use the below wrapper macros for compiler communication for this purpose
;
; Use the CompilerRead_NoDebug() macro to read stuff that should not be in the
; debugged output (for example function list, structure list etc)
;
CompilerIf #PB_Compiler_Debugger = 0
Macro CompilerRead(Mode = #PB_Ascii)
ReadProgramString(CompilerProgram, Mode)
EndMacro
Macro CompilerRead_NoDebug(Mode = #PB_Ascii)
ReadProgramString(CompilerProgram, Mode)
EndMacro
Macro CompilerWrite(String, Mode = #PB_Ascii)
WriteProgramStringN(CompilerProgram, String, Mode)
EndMacro
CompilerElse
Global CompilerRead_FirstNoDebug = 1
Procedure.s CompilerRead(Mode = #PB_Ascii)
CompilerRead_FirstNoDebug = 1
Result$ = ReadProgramString(CompilerProgram, Mode)
Debug "[COMPILER READ] " + ReplaceString(Result$, Chr(9), "<T>")
ProcedureReturn Result$
EndProcedure
Procedure.s CompilerRead_NoDebug(Mode = #PB_Ascii)
If CompilerRead_FirstNoDebug
Debug "[COMPILER ] Skipping display of read data."
EndIf
CompilerRead_FirstNoDebug = 0
ProcedureReturn ReadProgramString(CompilerProgram, Mode)
EndProcedure
Procedure CompilerWrite(String$, Mode = #PB_Ascii)
Debug "[COMPILER WRITE] " + ReplaceString(String$, Chr(9), "<T>")
WriteProgramStringN(CompilerProgram, String$, Mode)
EndProcedure
CompilerEndIf
Procedure CompilerWriteStringValue(Command$, Value$)
If Value$
CompilerWrite(Command$ + #TAB$ + Value$, #PB_UTF8)
EndIf
EndProcedure
Global Compiler_SubSystem$ = ""
Global CompilerStartup
CompilerIf #SpiderBasic
Global NewList OpenedWebServers()
CompilerEndIf
CompilerIf #CompileWindows
#COMPILER_VERSION = " /VERSION"
#COMPILER_STANDBY = " /STANDBY"
#COMPILER_SUBSYSTEM = " /SUBSYSTEM "
#COMPILER_LANGUAGE = " /LANGUAGE "
CompilerIf #SpiderBasic
#COMPILER_EXECUTABLE = "Compilers\sbcompiler.exe"
#COMPILER_UNICODE = "" ; no special unicode mode for spiderbasic
CompilerElse
#COMPILER_EXECUTABLE = "Compilers\pbcompiler.exe"
#COMPILER_UNICODE = " /UNICODE" ; still needed to set in older compilers
CompilerEndIf
CompilerElse
CompilerIf #CompileMac And Not #SpiderBasic
#COMPILER_STANDBY = " --standby -f -ibp" ; extra flags for osx only
CompilerElse
#COMPILER_STANDBY = " --standby"
CompilerEndIf
#COMPILER_VERSION = " --version"
#COMPILER_SUBSYSTEM = " --subsystem "
#COMPILER_LANGUAGE = " --language "
CompilerIf #SpiderBasic
#COMPILER_EXECUTABLE = "compilers/sbcompiler"
#COMPILER_UNICODE = "" ; no special unicode mode for spiderbasic
CompilerElse
#COMPILER_EXECUTABLE = "compilers/pbcompiler"
#COMPILER_UNICODE = " --unicode" ; still needed to set in older compilers
CompilerEndIf
CompilerEndIf
; Windows only parts for handling the resource options
;
CompilerIf #CompileWindows
Procedure.s ReplaceVersionInfo(Value$, *Target.CompileTarget)
Select OSVersion()
Case #PB_OS_Windows_NT3_51: OS$ = "Windows NT3.51"
Case #PB_OS_Windows_95: OS$ = "Windows 95"
Case #PB_OS_Windows_NT_4: OS$ = "Windows NT4"
Case #PB_OS_Windows_98: OS$ = "Windows 98"
Case #PB_OS_Windows_ME: OS$ = "Windows ME"
Case #PB_OS_Windows_2000: OS$ = "Windows 2000"
Case #PB_OS_Windows_XP: OS$ = "Windows XP"
Case #PB_OS_Windows_Server_2003: OS$ = "Windows Server 2003"
Case #PB_OS_Windows_Vista: OS$ = "Windows Vista"
Case #PB_OS_Windows_Server_2008: OS$ = "Windows Server 2008"
Case #PB_OS_Windows_7: OS$ = "Windows 7"
Case #PB_OS_Windows_Server_2008_R2: OS$ = "Windows Server 2008 R2"
Case #PB_OS_Windows_8: OS$ = "Windows 8"
Case #PB_OS_Windows_Server_2012: OS$ = "Windows Server 2012"
Case #PB_OS_Windows_8_1: OS$ = "Windows 8.1"
Case #PB_OS_Windows_Server_2012_R2: OS$ = "Windows Server 2012 R2"
Case #PB_OS_Windows_10: OS$ = "Windows 10"
Default: OS$ = "Windows" ; Don't use _Future here, as if we forget to add a new constant, it will return an empty string
EndSelect
Value$ = ReplaceString(Value$, "%OS", OS$, 1)
Value$ = ReplaceString(Value$, "%SOURCE", GetFilePart(*Target\FileName$), 1)
Value$ = ReplaceString(Value$, "%EXECUTABLE", GetFilePart(*Target\ExecutableName$), 1)
Value$ = ReplaceString(Value$, "%COMPILECOUNT", Str(*Target\CompileCount))
Value$ = ReplaceString(Value$, "%BUILDCOUNT", Str(*Target\BuildCount))
ProcedureReturn FormatDate(Value$, Date())
EndProcedure
Procedure WriteVersionInfoLine(Name$, Value$, *Target.CompileTarget)
If Value$ <> ""
Value$ = ReplaceVersionInfo(Value$, *Target)
; We replace \ -> \\ and then we replace all '"' by '\"' to avoid conflict
; Note: ASCII only is supported here, no UTF-8
WriteStringN(#FILE_Resources, " VALUE "+Chr(34)+Name$+Chr(34)+", "+Chr(34)+ReplaceString(ReplaceString(Value$, "\", "\\"), Chr(34), "\"+Chr(34))+"\0"+Chr(34))
EndIf
EndProcedure
Procedure.s CreateResourceFile(*Target.CompileTarget)
Debug *Target\FileName$
Debug *Target\VersionInfo
Debug *Target\NbResourceFiles
Debug "--------"
; Projects always resolve relative to the project file, not the main sourcefile
If *Target\IsProject
BasePath$ = GetPathPart(ProjectFile$)
Else
BasePath$ = GetPathPart(*Target\FileName$)
EndIf
; check if there is no resource stuff needed
;
If *Target\VersionInfo = 0 And *Target\NbResourceFiles = 0
ProcedureReturn ""
; check for only one single file to add
;
ElseIf *Target\VersionInfo = 0 And *Target\NbResourceFiles = 1
ProcedureReturn ResolveRelativePath(BasePath$, *Target\ResourceFiles$[0])
ElseIf CreateFile(#FILE_Resources, TempPath$ + "PB_Resources.rc", #PB_Unicode) ; Windows accept UTF-16 file format for version, so use it !
WriteStringFormat(#FILE_Resources, #PB_Unicode)
; define some symbols needed for the VERSION resource statement
;
WriteStringN(#FILE_Resources, "#define VOS_UNKNOWN 0x00000000")
WriteStringN(#FILE_Resources, "#define VOS_DOS 0x00010000")
WriteStringN(#FILE_Resources, "#define VOS_OS216 0x00020000")
WriteStringN(#FILE_Resources, "#define VOS_OS232 0x00030000")
WriteStringN(#FILE_Resources, "#define VOS_NT 0x00040000")
WriteStringN(#FILE_Resources, "#define VOS_DOS_WINDOWS16 0x00010001")
WriteStringN(#FILE_Resources, "#define VOS_DOS_WINDOWS32 0x00010004")
WriteStringN(#FILE_Resources, "#define VOS_OS216_PM16 0x00020002")
WriteStringN(#FILE_Resources, "#define VOS_OS232_PM32 0x00030003")
WriteStringN(#FILE_Resources, "#define VOS_NT_WINDOWS32 0x00040004")
WriteStringN(#FILE_Resources, "#define VFT_UNKNOWN 0")
WriteStringN(#FILE_Resources, "#define VFT_APP 1")
WriteStringN(#FILE_Resources, "#define VFT_DLL 2")
WriteStringN(#FILE_Resources, "#define VFT_DRV 3")
WriteStringN(#FILE_Resources, "#define VFT_FONT 4")
WriteStringN(#FILE_Resources, "#define VFT_VXD 5")
WriteStringN(#FILE_Resources, "#define VFT_STATIC_LIB 7")
WriteStringN(#FILE_Resources, "")
; include additional resource files
;
If *Target\NbResourceFiles > 0
For i = 0 To *Target\NbResourceFiles-1
WriteStringN(#FILE_Resources, "#include "+Chr(34)+ResolveRelativePath(BasePath$, *Target\ResourceFiles$[i])+Chr(34))
Next i
WriteStringN(#FILE_Resources, "")
EndIf
; write the version info if needed
;
If *Target\VersionInfo
; the combobox value strings are empty when they are set to the default
If *Target\VersionField$[15] = ""
FileOS$ = "VOS_UNKNOWN"
Else
FileOS$ = *Target\VersionField$[15]
EndIf
If *Target\VersionField$[16] = ""
FileType$ = "VFT_UNKNOWN"
Else
FileType$ = *Target\VersionField$[16]
EndIf
If *Target\VersionField$[17] = ""
Language$ = "0000"
Else
Language$ = Left(*Target\VersionField$[17], 4)
EndIf
; correct the version fields 1 and 2 if needed ( if wrongly formatted they lead to a resource error)
Field1$ = ReplaceString(ReplaceVersionInfo(*Target\VersionField$[0], *Target), ".", ",")
iscorrect = 1
If CountString(Field1$, ",") <> 3
iscorrect = 0
Else
For i = 1 To Len(Field1$)
If FindString("1234567890, ", Mid(Field1$, i, 1), 1) = 0
iscorrect = 0
Break
EndIf
Next i
EndIf
If iscorrect = 0
Field1$ = "0,0,0,0"
EndIf
Field2$ = ReplaceString(ReplaceVersionInfo(*Target\VersionField$[1], *Target), ".", ",")
iscorrect = 1
If CountString(Field2$, ",") <> 3
iscorrect = 0
Else
For i = 1 To Len(Field2$)
If FindString("1234567890, ", Mid(Field2$, i, 1), 1) = 0
iscorrect = 0
Break
EndIf
Next i
EndIf
If iscorrect = 0
Field2$ = "0,0,0,0"
EndIf
WriteStringN(#FILE_Resources, "1 VERSIONINFO")
WriteStringN(#FILE_Resources, "FILEVERSION " + Field1$)
WriteStringN(#FILE_Resources, "PRODUCTVERSION " + Field2$)
WriteStringN(#FILE_Resources, "FILEOS " + FileOS$)
WriteStringN(#FILE_Resources, "FILETYPE " + FileType$)
WriteStringN(#FILE_Resources, "{")
WriteStringN(#FILE_Resources, " BLOCK "+Chr(34)+"StringFileInfo"+Chr(34))
WriteStringN(#FILE_Resources, " {")
WriteStringN(#FILE_Resources, " BLOCK "+Chr(34)+Language$+"04b0"+Chr(34))
WriteStringN(#FILE_Resources, " {")
WriteVersionInfoLine("CompanyName", *Target\VersionField$[2], *Target)
WriteVersionInfoLine("ProductName", *Target\VersionField$[3], *Target)
WriteVersionInfoLine("ProductVersion", *Target\VersionField$[4], *Target)
WriteVersionInfoLine("FileVersion", *Target\VersionField$[5], *Target)
WriteVersionInfoLine("FileDescription", *Target\VersionField$[6], *Target)
WriteVersionInfoLine("InternalName", *Target\VersionField$[7], *Target)
WriteVersionInfoLine("OriginalFilename", *Target\VersionField$[8], *Target)
WriteVersionInfoLine("LegalCopyright", *Target\VersionField$[9], *Target)
WriteVersionInfoLine("LegalTrademarks", *Target\VersionField$[10], *Target)
WriteVersionInfoLine("PrivateBuild", *Target\VersionField$[11], *Target)
WriteVersionInfoLine("SpecialBuild", *Target\VersionField$[12], *Target)
WriteVersionInfoLine("Email", *Target\VersionField$[13], *Target)
WriteVersionInfoLine("Website", *Target\VersionField$[14], *Target)
For i = 18 To 20
If *Target\VersionField$[i] <> ""
WriteVersionInfoLine(*Target\VersionField$[i], *Target\VersionField$[i+3], *Target)
EndIf
Next i
WriteStringN(#FILE_Resources, " }")
WriteStringN(#FILE_Resources, " }")
WriteStringN(#FILE_Resources, " BLOCK "+Chr(34)+"VarFileInfo"+Chr(34))
WriteStringN(#FILE_Resources, " {")
WriteStringN(#FILE_Resources, " VALUE "+Chr(34)+"Translation"+Chr(34)+", 0x"+Language$+", 0x4b0")
WriteStringN(#FILE_Resources, " }")
WriteStringN(#FILE_Resources, "}")
EndIf
CloseFile(#FILE_Resources)
ProcedureReturn TempPath$ + "PB_Resources.rc"
Else
ProcedureReturn "" ; failure
EndIf
EndProcedure
CompilerEndIf
Procedure StartCompiler(*Compiler.Compiler)
; For the default compiler, we first have to find out these values first
;
If *Compiler = @DefaultCompiler
*Compiler\Executable$ = PureBasicPath$ + #COMPILER_EXECUTABLE
*Compiler\VersionString$ = #ProductName$
*Compiler\VersionNumber = #PB_Compiler_Version
; On Unix, if the path has a space, the env variable will contain "\ " instead (shell escaping), so we must remove that
; or the RunProgram() will fail
;
CompilerIf #CompileWindows = 0
*Compiler\Executable$ = ReplaceString(*Compiler\Executable$, "\ ", " ")
CompilerEndIf
EndIf
*CurrentCompiler = *Compiler
CompilerStartup = 0
Parameters$ = #COMPILER_STANDBY
SubSystem$ = RemoveString(Compiler_SubSystem$, " ")
index = 1
While StringField(SubSystem$, index, ",") <> ""
Parameters$ + #COMPILER_SUBSYSTEM + StringField(SubSystem$, index, ",")
index + 1
Wend
CompilerIf #SpiderBasic
CompilerIf #CompileWindows
Parameters$ + " /JDK " + #DQUOTE$ + ReplaceString(OptionJDK$, "\", "/") + #DQUOTE$
CompilerElseIf #CompileMac
Parameters$ + " -at " + #DQUOTE$ + OptionAppleTeamID$ + #DQUOTE$
; Needed to have cordova path always set on OS X. Setting the var in the compiler
; directory doesn't seems to work.
;
SetEnvironmentVariable("PATH", "/usr/local/bin:" + GetEnvironmentVariable("PATH"))
CompilerEndIf
CompilerElse
If *Compiler\VersionNumber < 550
; Since 5.50 there is only unicode mode (so nothing to set)
; Force unicode mode for any older compiler from this IDE to have consistent behavior (there are no settings for this anymore)
Parameters$ + #COMPILER_UNICODE
EndIf
CompilerEndIf
If CurrentLanguage$ <> "English" And *Compiler\VersionNumber >= 430 ; the language switch works on 4.30+ only
Parameters$ + #COMPILER_LANGUAGE + CurrentLanguage$
EndIf
CompilerIf #CompileWindows = 0 And #DEBUG = 1
; Append the -ds parameter so debug symbols are not cut. This is mainly so
; I can properly debug the Debugger library which is compiled into the exe
Parameters$ + " -ds"
CompilerEndIf
Debug "[COMPILER START] "+*Compiler\Executable$+" "+Parameters$
; set the PUREBASIC_HOME for the compiler
;
Home$ = ResolveRelativePath(GetPathPart(*Compiler\Executable$), "../") ; simple trick to get the main dir
CompilerIf #SpiderBasic
SetEnvironmentVariable("SPIDERBASIC_HOME", Home$)
CompilerElse
SetEnvironmentVariable("PUREBASIC_HOME", Home$)
CompilerEndIf
CompilerProgram = RunProgram(*Compiler\Executable$, Parameters$, "", #PB_Program_Open | #PB_Program_Hide | #PB_Program_Read | #PB_Program_Write | #PB_Program_UTF8)
If CompilerProgram
; Wait for the compiler version
Response$ = CompilerRead()
If Left(Response$, 9) = "STARTING" + Chr(9)
*Compiler\VersionNumber = Val(LSet(RemoveString(StringField(Response$, 2, Chr(9)), "."), 3, "0"))
*Compiler\VersionString$ = StringField(Response$, 3, Chr(9))
CompilerStartup = 1
Else
;
; This should only happen with pre 4.10 compilers, but since the IDE is shipped
; together with the compiler, we can treat this as a fatal error condition,
; as it should not happen in a normal install
;
KillProgram(CompilerProgram)
CloseProgram(CompilerProgram)
CompilerProgram = 0
If CommandlineBuild = 0
MessageRequester(#ProductName$, LanguagePattern("Compiler", "ResponseError", "%product%", #ProductName$), #FLAG_Error)
Else
PrintN(LanguagePattern("Compiler", "ResponseError", "%product%", #ProductName$))
End 1
EndIf
EndIf
EndIf
EndProcedure
; Set the NoReadyCall to prevent the call to CompilerReady(), as it
; re-highlight all sources.
;
Procedure WaitForCompilerReady(NoReadyCall = 0)
If CompilerProgram And CompilerStartup
;
; do not lock until the compiler is loaded
;
Timeout.q = ElapsedMilliseconds() + 15000 ; just for safety
While AvailableProgramOutput(CompilerProgram) = 0 And ElapsedMilliseconds() < Timeout
If CommandlineBuild = 0
EventLoopCallback()
DispatchEvent(WaitWindowEvent(50))
Else
Delay(50)
EndIf
Wend
If ElapsedMilliseconds() < Timeout
Response$ = CompilerRead()
If Response$ = "READY"
CompilerStartup = 0
If NoReadyCall = 0 And CommandlineBuild = 0
CompilerReady()
EndIf
CompilerReady = 1 ; set it also if we did not call CompilerReady!
ElseIf Left(Response$, 16) = "ERROR"+Chr(9)+"SUBSYSTEM"+Chr(9)
Name$ = StringField(Response$, 3, Chr(9))
If CommandlineBuild
PrintN(Language("Compiler", "SubSystemError")+": "+Name$)
ElseIf UseProjectBuildWindow And IsWindow(#WINDOW_Build)
BuildLogEntry(Language("Compiler", "SubSystemError")+": "+Name$)
Else
HideCompilerWindow()
MessageRequester(#ProductName$ + " - Compiler Error", Language("Compiler", "SubSystemError")+": "+Name$, #FLAG_ERROR)
EndIf
; No kill here as the compiler terminates itself
CloseProgram(CompilerProgram)
CompilerProgram = 0
CompilerStartup = 0
ElseIf Left(Response$, 6) = "ERROR" + Chr(9)
Message$ = StringField(Response$, 2, Chr(9))
If CommandlineBuild
PrintN("Compiler error!")
PrintN(Message$)
Else
Debugger_AddLog_BySource(*ActiveSource, "[COMPILER] Compiler error!", Date())
Debugger_AddLog_BySource(*ActiveSource, "[COMPILER] "+Message$, Date())
If UseProjectBuildWindow And IsWindow(#WINDOW_Build)
BuildLogEntry(Message$)
Else
HideCompilerWindow()
MessageRequester(#ProductName$ + " - Compiler Error", Message$, #FLAG_ERROR)
EndIf
EndIf
; No kill here as the compiler terminates itself
CloseProgram(CompilerProgram)
CompilerProgram = 0
CompilerStartup = 0
EndIf
EndIf
If CompilerStartup ; if it is still on, there is something wrong
KillProgram(CompilerProgram)
CloseProgram(CompilerProgram)
CompilerProgram = 0
CompilerStartup = 0
EndIf
EndIf
ProcedureReturn CompilerReady
EndProcedure
Procedure KillCompiler()
If CompilerProgram
CompilerWrite("END") ; tell the compiler to shut down itself
; wait for the compiler to shut down itself, else force it
If WaitProgram(CompilerProgram, 1000) = 0
KillProgram(CompilerProgram)
EndIf
CloseProgram(CompilerProgram)
EndIf
Debug "[COMPILER END]"
CompilerReady = 0
CompilerProgram = 0
EndProcedure
; to be called after all sources are closed
; (as this does some cleanup of compiled files as well)
Procedure CompilerCleanup()
DeleteFile(TempPath$ + "PB_EditorOutput.pb") ; this is ok for vista
CompilerIf #CompileWindows
DeleteFile(TempPath$ + "PB_Resources.rc")
CompilerEndIf
CompilerIf #SpiderBasic
; Be sure to kill all started servers
ForEach OpenedWebServers()
KillProgram(OpenedWebServers())
Next
CompilerEndIf
If ExamineDirectory(0, TempPath$, "*")
Protected CompilerType
CompilerIf #CompileMac
CompilerType = #PB_DirectoryEntry_Directory ; a .app is a directory!
CompilerElse
CompilerType = #PB_DirectoryEntry_File
CompilerEndIf
While NextDirectoryEntry(0)
If DirectoryEntryType(0) = CompilerType
File$ = DirectoryEntryName(0)
CompilerIf #CompileWindows
If Left(File$, 21) = "PureBasic_Compilation" And Right(File$, 4) = ".exe"
DeleteFile(TempPath$ + File$)
EndIf
CompilerEndIf
CompilerIf #CompileLinux
If Left(File$, 21) = "purebasic_compilation" And Right(File$, 4) = ".out"
DeleteFile(TempPath$ + File$)
EndIf
CompilerEndIf
CompilerIf #CompileMac
If Left(File$, 10) = "PureBasic." And Right(File$, 4) = ".app"
; a .app is a directory!
DeleteDirectory(TempPath$ + File$, "*", #PB_FileSystem_Recursive|#PB_FileSystem_Force)
EndIf
CompilerEndIf
EndIf
Wend
EndIf
EndProcedure
Procedure RestartCompiler(*Compiler.Compiler, NoReadyCall = 0)
; Does not check CompilerBusy anymore, as it is called during compilation
; to switch unicode mode!
;
KillCompiler()
If CommandlineBuild = 0 And IsMenu(#MENU)
DisableMenuItem(#MENU, #MENU_StructureViewer, 1)
DisableMenuItem(#MENU, #MENU_CompileRun, 1)
DisableMenuItem(#MENU, #MENU_SyntaxCheck, 1)
DisableMenuItem(#MENU, #MENU_CreateExecutable, 1)
EndIf
If CommandlineBuild = 0
DisableToolBarButton(#TOOLBAR, #MENU_CompileRun, 1)
EndIf
CompilerProgram = 0
StartCompiler(*Compiler)
WaitForCompilerReady(NoReadyCall)
EndProcedure
Procedure ForceDefaultCompiler()
If *CurrentCompiler <> @DefaultCompiler
RestartCompiler(@DefaultCompiler, #False)
EndIf
EndProcedure
Procedure TokenizeCompilerVersion(Version$, *Version.INTEGER, *Beta.INTEGER, *OS.INTEGER, *Processor.INTEGER)
Version$ = Trim(UCase(Version$))
If StringField(Version$, 1, " ") = UCase(#ProductName$)
; remove the LTS indicator
Version$ = ReplaceString(Version$, " LTS ", " ")
*Version\i = Val(LSet(RemoveString(StringField(Version$, 2, " "), "."), 3, "0"))
; *Beta\i is a bitmask: AlphaVersion | BetaVersion << 8
; For Finals, *Beta\i is $FFFF (so higher than all alpha/beta values)
;
If StringField(Version$, 3, " ") = "ALPHA"
*Beta\i = Val(StringField(Version$, 4, " "))
ElseIf StringField(Version$, 3, " ") = "BETA"
*Beta\i = Val(StringField(Version$, 4, " ")) << 8
Else
*Beta\i = $FFFF
EndIf
If FindString(Version$, "WINDOWS", 1)
*OS\i = #PB_OS_Windows
ElseIf FindString(Version$, "LINUX", 1)
*OS\i = #PB_OS_Linux
ElseIf FindString(Version$, "MACOS", 1)
*OS\i = #PB_OS_MacOS
Else
*OS\i = #PB_OS_AmigaOS ; unlikely :)
EndIf
If FindString(Version$, "X86", 1)
*Processor\i = #PB_Processor_x86
ElseIf FindString(Version$, "X64", 1)
*Processor\i = #PB_Processor_x64
ElseIf FindString(Version$, "POWERPC", 1)
*Processor\i = #PB_Processor_PowerPC
Else
*Processor\i = #PB_Processor_mc68000 ; also unlikely
EndIf
ProcedureReturn #True
Else
ProcedureReturn #False
EndIf
EndProcedure
Procedure MatchCompilerVersion(Version1$, Version2$, Flags = #MATCH_Exact)
If TokenizeCompilerVersion(Version1$, @Version1, @Beta1, @OS1, @Processor1) = #False
ProcedureReturn #False
EndIf
If TokenizeCompilerVersion(Version2$, @Version2, @Beta2, @OS2, @Processor2) = #False
ProcedureReturn #False
EndIf
If Flags & #MATCH_OS And OS1 <> OS2
ProcedureReturn #False
EndIf
If Flags & #MATCH_Version And Version1 <> Version2
ProcedureReturn #False
EndIf
If Flags & #MATCH_VersionUp
Base1 = Version1 - (Version1 % 10)
Base2 = Version2 - (Version2 % 10)
If Base1 <> Base2 Or Version1 > Version2 ; 4.40 -> 4.41 etc is ok
ProcedureReturn #False
EndIf
EndIf
If Flags & #MATCH_Beta And Beta1 <> Beta2
ProcedureReturn #False
EndIf
If Flags & #MATCH_BetaUp And Beta1 > Beta2
ProcedureReturn #False
EndIf
If Flags & #MATCH_Processor And Processor1 <> Processor2
ProcedureReturn #False
EndIf
ProcedureReturn #True
EndProcedure
Procedure SortCompilers()
ForEach Compilers()
If Compilers()\Validated And TokenizeCompilerVersion(Compilers()\VersionString$, @Version, @Beta, @OS, @Processor)
Compilers()\SortIndex = Processor << 28 | Version << 16 | Beta ; ignore OS, as all should have the same anyway
Else
Compilers()\SortIndex = -1
EndIf
Next Compilers()
SortStructuredList(Compilers(), #PB_Sort_Descending, OffsetOf(Compiler\SortIndex), #PB_Long)
EndProcedure
Procedure FindCompiler(Version$)
;
; We ignore the OS in our matches to have better crossplatform compatibility
; (you won't find several different OS compilers on the same OS anyway)
;
; Look for an exact match first
;
If MatchCompilerVersion(Version$, DefaultCompiler\VersionString$, #MATCH_Version|#MATCH_Beta|#MATCH_Processor)
ProcedureReturn @DefaultCompiler
Else
ForEach Compilers()
If Compilers()\Validated And MatchCompilerVersion(Version$, Compilers()\VersionString$, #MATCH_Version|#MATCH_Beta|#MATCH_Processor)
ProcedureReturn @Compilers()
EndIf
Next Compilers()
EndIf
; Look for another alpha/beta version now
;
If MatchCompilerVersion(Version$, DefaultCompiler\VersionString$, #MATCH_Version|#MATCH_BetaUp|#MATCH_Processor)
ProcedureReturn @DefaultCompiler
Else
ForEach Compilers()
If Compilers()\Validated And MatchCompilerVersion(Version$, Compilers()\VersionString$, #MATCH_Version|#MATCH_BetaUp|#MATCH_Processor)
ProcedureReturn @Compilers()
EndIf
Next Compilers()
EndIf
; Now look for another higher minor version
;
If MatchCompilerVersion(Version$, DefaultCompiler\VersionString$, #MATCH_VersionUp|#MATCH_Processor)
ProcedureReturn @DefaultCompiler
Else
ForEach Compilers()
If Compilers()\Validated And MatchCompilerVersion(Version$, Compilers()\VersionString$, #MATCH_VersionUp|#MATCH_Processor)
ProcedureReturn @Compilers()
EndIf
Next Compilers()
EndIf
; No match found
ProcedureReturn 0
EndProcedure
Procedure GetCompilerVersion(Executable$, *Compiler.Compiler)
*Compiler\Executable$ = Executable$
*Compiler\Validated = #False
Result = #False
Compiler = RunProgram(Executable$, #COMPILER_VERSION, GetPathPart(Executable$), #PB_Program_Hide|#PB_Program_Open|#PB_Program_Read)
If Compiler
Timeout.q = ElapsedMilliseconds() + 2000
While ElapsedMilliseconds() < Timeout And AvailableProgramOutput(Compiler) = 0
Delay(10)
Wend
If AvailableProgramOutput(Compiler) > 0
Version$ = ReadProgramString(Compiler)
If Left(Version$, 10) = #ProductName$ + " "
copy = FindString(Version$, "- (c)", 1) ; cut the copyright statement
If copy > 0
Version$ = RTrim(Left(Version$, copy-1))
EndIf
*Compiler\Executable$ = Executable$
*Compiler\MD5$ = FileFingerprint(Executable$, #PB_Cipher_MD5)
*Compiler\VersionString$ = Version$
*Compiler\VersionNumber = Val(LSet(RemoveString(StringField(Version$, 2, " "), "."), 3, "0"))
; 3.94 and 4.00 both know the /VERSION switch, but they do not have the
; communication interface, so we cannot accept them
;
If *Compiler\VersionNumber >= 410
*Compiler\Validated = #True
Result = #True
EndIf
; purge any remaining output (should be nothing more)
While ProgramRunning(Compiler)
ReadProgramString(Compiler)
Wend
Else
; probably not a PB compiler, so kill it (we do not know how much output follows)
KillProgram(Compiler)
EndIf
Else
; timeout. probably not a PureBasic compiler
KillProgram(Compiler)
EndIf
CloseProgram(Compiler)
EndIf
ProcedureReturn Result
EndProcedure
; ----------------------------------------------------------------------
Procedure Compiler_SetCompiler(*Target.CompileTarget)
; Find a matching compiler
;
If *Target\CustomCompiler = #False
*Compiler.Compiler = @DefaultCompiler
Else
*Compiler.Compiler = FindCompiler(*Target\CompilerVersion$)
If *Compiler = 0
If CommandlineBuild
PrintN(Language("Compiler","CompilerNotFound")+": "+*Target\CompilerVersion$)
ElseIf UseProjectBuildWindow
BuildLogEntry(Language("Compiler","CompilerNotFound")+": "+*Target\CompilerVersion$)
Else
MessageRequester(#ProductName$, Language("Compiler","CompilerNotFound")+":"+#NewLine+*Target\CompilerVersion$)
EndIf
ProcedureReturn #False
EndIf
EndIf
If *Compiler <> @DefaultCompiler
If UseProjectBuildWindow And CommandlineBuild = 0
BuildLogEntry(Language("Compiler","BuildUseCompiler")+": "+*Compiler\VersionString$)
ElseIf CommandlineBuild And QuietBuild = 0
Print(Language("Compiler","BuildUseCompiler")+": "+*Compiler\VersionString$)
EndIf
EndIf
If *Compiler <> *CurrentCompiler Or CompilerProgram = 0 Or *Target\SubSystem$ <> Compiler_SubSystem$
Compiler_SubSystem$ = *Target\SubSystem$
If CommandlineBuild = 0
FlushEvents() ; process all waiting messages
EndIf
; a compiler restart is required
RestartCompiler(*Compiler, #True) ; set #true to not call CompilerReady(), as it re-highlights all code!
; no need to wait for the compiler, as RestartCompiler()
; calls WaitForCompilerReady()
;
If CompilerReady ; if 0, restart failed
; do the needed stuff for CompilerReady() as it is not called here
If CommandlineBuild = 0
DisableMenuItem(#MENU, #MENU_StructureViewer, 0)
DisableMenuItem(#MENU, #MENU_CreateExecutable, 0)
DisableMenuAndToolbarItem(#MENU_CompileRun, 0)
DisableMenuAndToolbarItem(#MENU_SyntaxCheck, 0)
EndIf
ProcedureReturn #True
Else
HideCompilerWindow()
; Try to start the compiler again without any subsystem or unicode
; (this is for the case where a subsystem cannot be found)
Compiler_SubSystem$ = ""
CompilerBusy = 0 ; disable the "compiler is busy" requester
RestartCompiler(*Compiler, #True) ; set #true to not call CompilerReady(), as it re-highlights all code!
; do not set Busy flag again as the compilation is aborted
If CompilerReady ; if 0, restart failed
; do the needed stuff for CompilerReady() as it is not called here
If CommandlineBuild = 0
DisableMenuItem(#MENU, #MENU_StructureViewer, 0)
DisableMenuItem(#MENU, #MENU_CreateExecutable, 0)
DisableMenuAndToolbarItem(#MENU_CompileRun, 0)
DisableMenuAndToolbarItem(#MENU_SyntaxCheck, 0)
EndIf
Else
; all failed, force the default compiler
ForceDefaultCompiler()
; This is fatal also in build mode, as we cannot continue the build without a compiler!
; So show this requester in all cases
;
If CommandlineBuild
PrintN(Language("Compiler","StartError")+": "+*Compiler\VersionString$)
ElseIf UseProjectBuildWindow
BuildLogEntry(Language("Compiler","StartError")+": "+*Compiler\VersionString$)
Else
MessageRequester(#ProductName$, Language("Compiler","StartError")+": "+*Compiler\VersionString$, #FLAG_Error)
EndIf
EndIf
ProcedureReturn #False; abort the compilation in any case (for subsystem problems)
EndIf
EndIf
ProcedureReturn #True ; no change needed, so all ok
EndProcedure
; Handle the error and progress display
;
Procedure Compiler_HandleCompilerResponse(*Target.CompileTarget)
Protected NewList MacroLines.s()
CompilationAborted = #False ; clear the flag
WarningCount = 0
; handle any PROGRESS messages
;
Repeat
If CommandlineBuild = 0
FlushEvents() ; flush at least once the events even if data is waiting
EndIf
; wait for data to be ready
While ProgramRunning(CompilerProgram) And AvailableProgramOutput(CompilerProgram) = 0
If CommandlineBuild = 0
While DispatchEvent(WaitWindowEvent(10))
EventLoopCallback()
Wend
Else
Delay(50)
EndIf
Wend
If ProgramRunning(CompilerProgram) = 0
; hide the window, so the requester cannot get stuck behind it as it is topmost
; the window will be closed by the restart code below.
If CommandlineBuild
Message$ = Language("Compiler","CompilerCrash") ; this is a multiline message
If FindString(Message$, #NewLine, 1)
Message$ = Left(Message$, FindString(Message$, #NewLine, 1)-1)
EndIf
PrintN(Message$)
ElseIf UseProjectBuildWindow
Message$ = Language("Compiler","CompilerCrash") ; this is a multiline message
If FindString(Message$, #NewLine, 1)
Message$ = Left(Message$, FindString(Message$, #NewLine, 1)-1)
EndIf
BuildLogEntry(Message$)
Else
HideWindow(#WINDOW_Compiler, #True)
EndIf
; Again, this is treated as a fatal error, so even the build mode gets a requester
If CommandlineBuild = 0
MessageRequester(#ProductName$, Language("Compiler","CompilerCrash"), #FLAG_Error)
EndIf
CompilationAborted = #True ; causes a compiler restart and ensures a proper compilation abort
EndIf
; Handle the event that the user aborted the compilation
; We can only safely abort a compilation by restarting the compiler, as else
; the Compiler-IDE communication is out of sync!
;
If CompilationAborted And CommandlineBuild = 0 ; not possible in commandline mode
If UseProjectBuildWindow
BuildLogEntry(Language("Compiler","Aborting"))
Else
SetGadgetText(#GADGET_Compiler_Text, Language("Compiler","Aborting"))
AddGadgetItem(#GADGET_Compiler_List, -1, Language("Compiler","Aborting"))
SetGadgetState(#GADGET_Compiler_List, CountGadgetItems(#GADGET_Compiler_List)-1)
EndIf
DisableMenuItem(#MENU, #MENU_StructureViewer, 1)
DisableMenuItem(#MENU, #MENU_CreateExecutable, 1)
DisableMenuAndToolbarItem(#MENU_CompileRun, 1)
DisableMenuAndToolbarItem(#MENU_SyntaxCheck, 1)
FlushEvents() ; ensure a proper display
; do this only if the compiler did not crash!
;
If ProgramRunning(CompilerProgram)
; give the compiler a chance to quit itself with proper cleanup
; (will only work if compilation finishes in this time anyway)
;
CompilerWrite("END")
Timeout.q = ElapsedMilliseconds() + 2000
While Timeout.q > ElapsedMilliseconds()
FlushEvents()