forked from LongDirtyAnimAlf/fpcupdeluxe
-
Notifications
You must be signed in to change notification settings - Fork 0
/
processutils.pas
executable file
·1304 lines (1169 loc) · 36.6 KB
/
processutils.pas
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
unit processutils;
{$mode objfpc}{$H+}
{$ifdef LCL}
{$define THREADEDEXECUTE}
{$endif}
interface
uses
Classes, SysUtils,
Process;
const
{$ifdef LCL}
BeginSnippet='fpcupdeluxe:'; //helps identify messages as coming from fpcupdeluxe instead of make etc
{$else}
{$ifndef FPCONLY}
BeginSnippet='fpclazup:'; //helps identify messages as coming from fpclazup instead of make etc
{$else}
BeginSnippet='fpcup:'; //helps identify messages as coming from fpcup instead of make etc
{$endif}
{$endif}
{$IFDEF MSWINDOWS}
PATHVARNAME = 'Path'; //Name for path environment variable
{$ELSE}
//Unix/Linux
PATHVARNAME = 'PATH';
{$ENDIF MSWINDOWS}
resourcestring
lisExitCode = 'Exit code %s';
lisToolHasNoExecutable = 'tool "%s" has no executable';
lisCanNotFindExecutable = 'cannot find executable "%s"';
lisMissingExecutable = 'missing executable "%s"';
lisExecutableIsADirectory = 'executable "%s" is a directory';
lisExecutableLacksThePermissionToRun = 'executable "%s" lacks the permission to run';
lisSuccess = 'Success';
lisAborted = 'Aborted';
lisCanNotExecute = 'cannot execute "%s"';
lisMissingDirectory = 'missing directory "%s"';
lisUnableToExecute = 'unable to execute: %s';
lisUnableToReadProcessExitStatus = 'unable to read process ExitStatus';
lisFreeingBufferLines = 'freeing buffer lines: %s';
const
AbortedExitCode = 12321;
type
{ TProcessEnvironment }
TProcessEnvironment = class(TObject)
private
FEnvironmentList:TStringList;
FCaseSensitive:boolean;
function GetVarIndex(VarName:string):integer;
public
// Get environment variable
function GetVar(VarName:string):string;
// Set environment variable
procedure SetVar(VarName,VarValue:string);
// List of all environment variables (name and value)
property EnvironmentList:TStringList read FEnvironmentList;
constructor Create;
destructor Destroy; override;
end;
TExternalToolStage = (
etsInit, // just created, set your parameters, then call Execute
etsInitializing, // set in Execute, during resolving macros
etsWaitingForStart, // waiting for a process slot
etsStarting, // creating the thread and process
etsRunning, // process started
etsWaitingForStop, // waiting for process to stop
etsStopped, // process has stopped
etsDestroying // during destructor
);
TExternalToolStages = set of TExternalToolStage;
TExternalToolNewOutputEvent = procedure(Sender: TObject;
FirstNewMsgLine: integer) of object;
TExternalToolHandler = (
ethNewOutput,
ethStopped
);
TOnUpdateEvent = procedure(Sender: TObject;Status:TExternalToolStage) of object;
TAbstractExternalTool = class(TComponent)
private
FCritSec: TRTLCriticalSection;
FData: TObject;
FExitCode: integer;
FExitStatus: integer;
FFreeData: boolean;
FReadStdOutBeforeErr: boolean;
FTitle: string;
FProcessEnvironment:TProcessEnvironment;
FCmdLineExe: string;
FOnUpdateEvent: TOnUpdateEvent;
function GetCmdLineParams: string;
procedure SetCmdLineParams(aParams: string);
procedure SetCmdLineExe(aExe: string);
procedure SetTitle(const AValue: string);
procedure UpdateEvent(Sender : TObject;Status:TExternalToolStage);
protected
FErrorMessage: string;
FTerminated: boolean;
FStage: TExternalToolStage;
FWorkerOutput: TStringList;
FProcess: TProcess;
function GetProcessEnvironment: TProcessEnvironment;
procedure DoExecute; virtual; abstract;
function CanFree: boolean; virtual;
public
constructor Create(AOwner: TComponent); override;
destructor Destroy; override;
procedure EnterCriticalSection;
procedure LeaveCriticalSection;
procedure AutoFree;
property Title: string read FTitle write SetTitle;
property Data: TObject read FData write FData;
property FreeData: boolean read FFreeData write FFreeData default false;
// process
property Process: TProcess read FProcess;
property Executable: string read FCmdLineExe write SetCmdLineExe;
property CmdLineParams: string read GetCmdLineParams write SetCmdLineParams;
property Stage: TExternalToolStage read FStage;
procedure Execute; virtual; abstract;
procedure Terminate; virtual; abstract;
procedure WaitForExit; virtual; abstract;
property Terminated: boolean read FTerminated;
property ExitCode: integer read FExitCode write FExitCode;
property ExitStatus: integer read FExitStatus write FExitStatus;
property ErrorMessage: string read FErrorMessage write FErrorMessage;
property ReadStdOutBeforeErr: boolean read FReadStdOutBeforeErr write FReadStdOutBeforeErr;
property Environment:TProcessEnvironment read GetProcessEnvironment;
Property OnUpdateEvent : TOnUpdateEvent Read FOnUpdateEvent Write FOnUpdateEvent;
// output
property WorkerOutput: TStringList read FWorkerOutput; // the raw output
end;
TExternalTool = class;
{ TExternalToolThread }
{$ifdef THREADEDEXECUTE}
TExternalToolThread = class(TThread)
{$else}
TExternalToolThread = class(TObject)
{$endif}
private
fLines: TStringList;
FTool: TExternalTool;
procedure SetTool(AValue: TExternalTool);
function GetFilter(line: string; aVerbosity:boolean):boolean;
public
property Tool: TExternalTool read FTool write SetTool;
{$ifdef THREADEDEXECUTE}
procedure Execute; override;
{$else}
procedure Execute;
{$endif}
destructor Destroy; override;
end;
{ TExternalTool }
TExternalTool = class(TAbstractExternalTool)
private
FThread: TExternalToolThread;
FVerbose:boolean;
procedure ProcessRunning;
procedure ProcessStopped;
procedure AddOutputLines(Lines: TStringList);
procedure SetThread(AValue: TExternalToolThread);
procedure DoTerminate;
procedure SyncAutoFree({%H-}aData: PtrInt=0);
protected
FFPCMagic:boolean; // tricky filtering
procedure DoExecute; override;
procedure DoStart;
function CanFree: boolean; override;
procedure QueueAsyncAutoFree;
public
constructor Create(aOwner: TComponent); override;
destructor Destroy; override;
property Thread: TExternalToolThread read FThread write SetThread;
property Verbose: boolean read FVerbose write FVerbose;
procedure Execute; override;
procedure Terminate; override;
procedure WaitForExit; override;
function GetExeInfo:string;
function CanStart: boolean;
function ExecuteAndWait:integer;
end;
procedure ThreadLog(const aMsg: string;{%H-}const aEvent:TEventType=etInfo);
implementation
uses
{$ifdef LCL}
Forms,
Controls, // for crHourGlass
LCLIntf,
LMessages,
{$endif}
StrUtils,
Pipes,
Math,
FileUtil,
LazFileUtils;
{ TProcessEnvironment }
function TProcessEnvironment.GetVarIndex(VarName: string): integer;
var
idx:integer;
function ExtractVar(VarVal:string):string;
begin
result:='';
if length(Varval)>0 then
begin
if VarVal[1] = '=' then //windows
delete(VarVal,1,1);
result:=trim(copy(VarVal,1,pos('=',VarVal)-1));
if not FCaseSensitive then
result:=UpperCase(result);
end
end;
begin
if (Length(VarName)=0) then
begin
result:=-1;
end
else
begin
if not FCaseSensitive then
VarName:=UpperCase(VarName);
idx:=0;
while idx<FEnvironmentList.Count do
begin
if VarName = ExtractVar(FEnvironmentList[idx]) then
break;
idx:=idx+1;
end;
if idx<FEnvironmentList.Count then
result:=idx
else
result:=-1;
end;
end;
function TProcessEnvironment.GetVar(VarName: string): string;
var
idx:integer;
function ExtractVal(VarVal:string):string;
begin
result:='';
if length(Varval)>0 then
begin
if VarVal[1] = '=' then //windows
delete(VarVal,1,1);
result:=trim(copy(VarVal,pos('=',VarVal)+1,length(VarVal)));
end
end;
begin
idx:=GetVarIndex(VarName);
if idx>=0 then
result:=ExtractVal(FEnvironmentList[idx])
else
result:='';
end;
procedure TProcessEnvironment.SetVar(VarName, VarValue: string);
var
idx:integer;
s:string;
begin
if (Length(VarName)=0) then exit;
idx:=GetVarIndex(VarName);
if (idx>=0) AND (Length(VarValue)=0) then
begin
FEnvironmentList.Delete(idx);
end
else
if (Length(VarValue)>0) then
begin
s:=trim(Varname)+'='+trim(VarValue);
if idx>=0 then
FEnvironmentList[idx]:=s
else
FEnvironmentList.Add(s);
end;
end;
constructor TProcessEnvironment.Create;
var
i: integer;
begin
FEnvironmentList:=TStringList.Create;
{$ifdef WINDOWS}
FCaseSensitive:=false;
{$else}
FCaseSensitive:=true;
{$endif WINDOWS}
// GetEnvironmentVariableCount is 1 based
for i:=1 to GetEnvironmentVariableCount do
EnvironmentList.Add(trim(GetEnvironmentString(i)));
end;
destructor TProcessEnvironment.Destroy;
begin
FEnvironmentList.Free;
inherited Destroy;
end;
{ TAbstractExternalTool }
function TAbstractExternalTool.GetCmdLineParams: string;
var
i: Integer;
begin
Result:='';
if Process.Parameters=nil then exit;
for i:=0 to Pred(Process.Parameters.Count) do
begin
if i>0 then Result+=' ';
Result:=Result+Process.Parameters[i];
end;
end;
procedure TAbstractExternalTool.SetCmdLineParams(aParams: string);
var
sl: TStringList;
begin
sl:=TStringList.Create;
try
SplitCmdLineParams(aParams,sl);
Process.Parameters:=sl;
finally
sl.Free;
end;
end;
procedure TAbstractExternalTool.SetCmdLineExe(aExe: string);
begin
FCmdLineExe:=aExe;
Process.Executable:=FCmdLineExe;
end;
procedure TAbstractExternalTool.SetTitle(const AValue: string);
begin
if FTitle=AValue then exit;
FTitle:=AValue;
end;
procedure TAbstractExternalTool.UpdateEvent(Sender: TObject;Status:TExternalToolStage);
begin
if MainThreadID=ThreadID then
begin
//if IsMultiThread then
{$ifdef LCL}
Application.ProcessMessages;
{$else}
CheckSynchronize(0);
{$endif}
end;
//if status=etsRunning then
// //sleep(Process.RunCommandSleepTime);
// sleep(10);
end;
function TAbstractExternalTool.CanFree: boolean;
begin
Result:=false;
if csDestroying in ComponentState then exit;
if (Process<>nil) and (Process.Running) then
exit;
Result:=true;
end;
constructor TAbstractExternalTool.Create(AOwner: TComponent);
begin
inherited Create(AOwner);
FStage:=etsInit;
InitCriticalSection(FCritSec);
end;
destructor TAbstractExternalTool.Destroy;
begin
EnterCriticalSection;
try
if FreeData then FreeAndNil(FData);
if assigned(FProcessEnvironment) then FProcessEnvironment.Free;
finally
LeaveCriticalsection;
end;
DoneCriticalSection(FCritSec);
inherited Destroy;
end;
procedure TAbstractExternalTool.EnterCriticalSection;
begin
System.EnterCriticalsection(FCritSec);
end;
procedure TAbstractExternalTool.LeaveCriticalSection;
begin
System.LeaveCriticalsection(FCritSec);
end;
procedure TAbstractExternalTool.AutoFree;
begin
if MainThreadID<>GetCurrentThreadId then
raise Exception.Create('AutoFree only via main thread');
if CanFree then
Free;
end;
function TAbstractExternalTool.GetProcessEnvironment: TProcessEnvironment;
begin
If not assigned(FProcessEnvironment) then
FProcessEnvironment:=TProcessEnvironment.Create;
result:=FProcessEnvironment;
end;
{ TExternalTool }
procedure TExternalTool.ProcessRunning;
begin
EnterCriticalSection;
try
if FStage<>etsStarting then exit;
FStage:=etsRunning;
finally
LeaveCriticalSection;
end;
end;
procedure TExternalTool.ProcessStopped;
begin
EnterCriticalSection;
try
if (not Terminated) and (ErrorMessage='') then
begin
if ExitCode<>0 then
ErrorMessage:=Format(lisExitCode, [IntToStr(ExitCode)])
else if ExitStatus<>0 then
ErrorMessage:='ExitStatus '+IntToStr(ExitStatus);
end;
if FStage>=etsStopped then exit;
if Assigned(FProcessEnvironment) then FProcessEnvironment.Destroy;
FProcessEnvironment:=nil;
FStage:=etsStopped;
finally
LeaveCriticalSection;
end;
{$ifndef THREADEDEXECUTE}
Thread.Destroy;
{$endif}
fThread:=nil;
end;
procedure TExternalTool.AddOutputLines(Lines: TStringList);
var
Line: LongInt;
OldOutputCount: LongInt;
LineStr: String;
begin
if (Lines=nil) or (Lines.Count=0) then exit;
EnterCriticalSection;
try
OldOutputCount:=WorkerOutput.Count;
WorkerOutput.AddStrings(Lines);
for Line:=OldOutputCount to WorkerOutput.Count-1 do
begin
LineStr:=WorkerOutput[Line];
if IsMultiThread then
begin
end;
if Verbose OR FFPCMagic
//OR (NOT IsMultiThread)
{$ifndef LCL} OR True{$endif}
{$ifdef DEBUG} OR True{$endif}
then
begin
ThreadLog(LineStr);
end;
end;
finally
LeaveCriticalSection;
end;
end;
procedure TExternalTool.SetThread(AValue: TExternalToolThread);
var
CallAutoFree: Boolean;
begin
// Note: in lazbuild ProcessStopped sets FThread:=nil, so SetThread is not called.
EnterCriticalSection;
try
if FThread=AValue then Exit;
FThread:=AValue;
CallAutoFree:=CanFree;
finally
LeaveCriticalSection;
end;
if CallAutoFree then
begin
if MainThreadID=GetCurrentThreadId then
AutoFree
else
QueueAsyncAutoFree;
end;
end;
constructor TExternalTool.Create(aOwner: TComponent);
begin
inherited Create(aOwner);
FWorkerOutput:=TStringList.Create;
FProcess:=TProcess.Create(nil);
//FProcess:=DefaultTProcess.Create(nil);
//Process.Options:= [poUsePipes{$IFDEF Windows},poStderrToOutPut{$ENDIF}];
//Process.Options := FProcess.Options +[poUsePipes, poStderrToOutPut];
Process.Options:= [{poWaitOnExit,}poUsePipes{$ifdef Windows},poStderrToOutPut{$endif}];
//Process.Options := FProcess.Options +[poUsePipes, poStderrToOutPut]-[poRunSuspended,poWaitOnExit];
{$ifdef LCL}
FProcess.ShowWindow := swoHide;
{$endif}
Process.RunCommandSleepTime:=10; // rest the default sleep time to 0 (context switch only)
Self.OnUpdateEvent:=@UpdateEvent;
FVerbose:=true;
end;
destructor TExternalTool.Destroy;
begin
EnterCriticalSection;
try
FStage:=etsDestroying;
if Thread is TExternalToolThread then
TExternalToolThread(Thread).Tool:=nil;
FreeAndNil(FProcess);
FreeAndNil(FWorkerOutput);
finally
LeaveCriticalSection;
end;
inherited Destroy;
end;
procedure TExternalTool.DoExecute;
// in main thread
function CheckError: boolean;
begin
if (FStage>=etsStopped) then exit(true);
if (ErrorMessage='') then exit(false);
EnterCriticalSection;
try
if FStage>=etsStopped then exit(true);
FStage:=etsStopped;
finally
LeaveCriticalSection;
end;
Result:=true;
end;
var
ExeFile: String;
begin
if Terminated then exit;
EnterCriticalSection;
try
if Stage<>etsInit then
raise Exception.Create('TExternalTool.Execute: already initialized');
FStage:=etsInitializing;
WorkerOutput.Clear;
finally
LeaveCriticalSection;
end;
// init CurrentDirectory
Process.CurrentDirectory:=TrimFilename(Process.CurrentDirectory);
if not FilenameIsAbsolute(Process.CurrentDirectory) then
Process.CurrentDirectory:=AppendPathDelim(GetCurrentDir)+Process.CurrentDirectory;
// init Executable
Process.Executable:=TrimFilename(Process.Executable);
if not FilenameIsAbsolute(Process.Executable) then
begin
if ExtractFilePath(Process.Executable)<>'' then
Process.Executable:=AppendPathDelim(GetCurrentDir)+Process.Executable
else if Process.Executable='' then
begin
ErrorMessage:=Format(lisToolHasNoExecutable, [Title]);
CheckError;
exit;
end else begin
ExeFile:=FindDefaultExecutablePath(Process.Executable,GetCurrentDir);
if ExeFile='' then
begin
ErrorMessage:=Format(lisCanNotFindExecutable, [Process.Executable]);
CheckError;
exit;
end;
Process.Executable:=ExeFile;
end;
end;
ExeFile:=Process.Executable;
if not FileExists(ExeFile) then
begin
ErrorMessage:=Format(lisMissingExecutable, [ExeFile]);
CheckError;
exit;
end;
if DirectoryExists(ExeFile) then
begin
ErrorMessage:=Format(lisExecutableIsADirectory, [ExeFile]);
CheckError;
exit;
end;
if not FileIsExecutable(ExeFile) then
begin
ErrorMessage:=Format(lisExecutableLacksThePermissionToRun, [ExeFile]);
CheckError;
exit;
end;
//Do we have something FPC like. If so, apply some filtering when not Verbose
//Filtering is dome here to limit the amount of thread message traffic
//Bit tricky ... ;-)
FFPCMagic:=False;
if (NOT Verbose) then
begin
ExeFile:=LowerCase(ExtractFileName(Process.Executable));
if
((Pos('fpc',ExeFile)=1)
OR
(Pos('ppc',ExeFile)=1)
OR
(Pos('lazbuild',ExeFile)=1)
OR
(Pos('make',ExeFile)=1))
then
begin
FFPCMagic:=True;
end;
end;
// init misc
if Assigned(FProcessEnvironment) then
Process.Environment:=FProcessEnvironment.EnvironmentList;
EnterCriticalSection;
try
if Stage<>etsInitializing then
raise Exception.Create('TExternalTool.Execute: bug in initialization');
FStage:=etsWaitingForStart;
finally
LeaveCriticalSection;
end;
end;
procedure TExternalTool.DoStart;
begin
EnterCriticalSection;
try
if Stage<>etsWaitingForStart then
raise Exception.Create('TExternalTool.Execute: already started');
FStage:=etsStarting;
finally
LeaveCriticalSection;
end;
{$ifdef THREADEDEXECUTE}
if Thread=nil then
begin
FThread:=TExternalToolThread.Create(true);
Thread.Tool:=Self;
Thread.FreeOnTerminate:=true;
end;
Thread.Start;
{$else}
if Thread=nil then
begin
FThread:=TExternalToolThread.Create;
Thread.Tool:=Self;
end;
Thread.Execute;
{$endif}
end;
procedure TExternalTool.DoTerminate;
var
NeedProcTerminate: Boolean;
begin
NeedProcTerminate:=false;
EnterCriticalSection;
try
if Terminated then exit;
if Stage=etsStopped then exit;
if ErrorMessage='' then
ErrorMessage:=lisAborted;
fTerminated:=true;
if Stage=etsRunning then
NeedProcTerminate:=true;
if Stage<etsStarting then
FStage:=etsStopped
else if Stage<=etsRunning then
FStage:=etsWaitingForStop;
finally
LeaveCriticalSection;
end;
if NeedProcTerminate and (Process<>nil) then
begin
Process.Terminate(AbortedExitCode);
{$IF FPC_FULLVERSION < 30300}
Process.WaitOnExit;
{$ELSE}
Process.WaitOnExit(5000);
{$ENDIF}
//To check !!
//fTerminated:=false;
end;
end;
function TExternalTool.CanFree: boolean;
begin
Result:=(FThread=nil) and inherited CanFree;
end;
procedure TExternalTool.SyncAutoFree(aData: PtrInt);
begin
AutoFree;
end;
procedure TExternalTool.QueueAsyncAutoFree;
begin
{$ifdef LCL}
Application.QueueAsyncCall(@SyncAutoFree,0);
{$endif}
end;
function TExternalTool.CanStart: boolean;
begin
Result:=false;
if Stage<>etsWaitingForStart then exit;
if Terminated then exit;
Result:=true;
end;
procedure TExternalTool.Execute;
begin
if Stage<>etsInit then
begin
if Stage=etsStopped then
begin
EnterCriticalSection;
try
FStage:=etsInit;
finally
LeaveCriticalSection;
end;
end else raise Exception.Create('TExternalTool.Execute "'+Title+'" already started');
end;
DoExecute;
if Stage<>etsWaitingForStart then
exit
else
DoStart;
end;
procedure TExternalTool.Terminate;
begin
DoTerminate;
end;
procedure TExternalTool.WaitForExit;
begin
repeat
try
EnterCriticalSection;
try
if Stage=etsDestroying then break;
if Stage=etsStopped then break;
// still running => wait a bit to prevent cpu cycle burning
finally
LeaveCriticalSection;
end;
finally
//WakeMainThread;
//ThreadSwitch;
if MainThreadID=ThreadID then
begin
//if IsMultiThread then
{$ifdef LCL}
Application.ProcessMessages;
{$else}
CheckSynchronize(0); // if we use Thread.Synchronize
{$endif}
//TExternalToolsBase(Owner).HandleMesages;
end;
end;
sleep(10)
until false;
end;
function TExternalTool.GetExeInfo:string;
begin
result:='Executing: '+Process.Executable+' '+CmdLineParams+' (working dir: '+ Process.CurrentDirectory +')';
end;
function TExternalTool.ExecuteAndWait:integer;
begin
result:=-1;
Execute;
WaitForExit;
//result:=ExitCode;
result:=ExitStatus;
//result:=(ErrorMessage='') and (not Terminated) and (ExitStatus=0);
end;
{ TExternalToolThread }
function TExternalToolThread.GetFilter(line: string; aVerbosity:boolean):boolean;
var
s:string;
begin
result:=false;
// skip stray empty lines
if (Length(line)=0) then exit;
{$ifdef Darwin}
// suppress all setfocus errors on Darwin, always
if AnsiContainsText(line,'.setfocus') then exit;
{$endif}
{$ifdef Unix}
// suppress all Kb Used messages, always
if AnsiContainsText(line,'Kb Used') then exit;
{$endif}
// suppress all SynEdit PaintLock errors, always
if AnsiContainsText(line,'PaintLock') then exit;
// suppress some GIT errors, always
if AnsiContainsText(line,'fatal: not a git repository') then exit;
// suppress some lazbuild errors, always
if AnsiContainsText(line,'lazbuild') then
begin
if AnsiContainsText(line,'only for runtime') then exit;
if AnsiContainsText(line,'lpk file expected') then exit;
end;
if AnsiStartsText('TExternalTool',line) then exit;
result:=(NOT aVerbosity);
if (NOT result) then
begin
// to be absolutely sure not to miss errors and fatals and fpcupdeluxe messages !!
// will be a bit redundant , but just to be sure !
if (AnsiContainsText(line,'error:'))
OR (AnsiContainsText(line,'donalf:'))
OR (AnsiContainsText(line,'fatal:'))
OR (AnsiContainsText(line,'fpcupdeluxe:'))
OR (AnsiContainsText(line,'execute:'))
OR (AnsiContainsText(line,'executing:'))
OR ((AnsiContainsText(line,'compiling ')) AND (NOT AnsiContainsText(line,'when compiling target')))
OR (AnsiContainsText(line,'linking '))
then result:=true;
if (NOT result) then
begin
// remove hints and other "trivial"* warnings from output
// these line are not that interesting for the average user of fpcupdeluxe !
if AnsiContainsText(line,'hint: ') then exit;
if AnsiContainsText(line,'verbose: ') then exit;
if AnsiContainsText(line,'note: ') then exit;
if AnsiContainsText(line,'assembling ') then exit;
if AnsiContainsText(line,': entering directory ') then exit;
if AnsiContainsText(line,': leaving directory ') then exit;
// when generating help
if AnsiContainsText(line,'illegal XML element: ') then exit;
if AnsiContainsText(line,'parsing used unit ') then exit;
if AnsiContainsText(line,'extracting ') then exit;
// during building of lazarus components, default compiler switches cause version and copyright info to be shown
// do not know if this is allowed, but this version / copyright info is very redundant as it is shown everytime the compiler is called ...
// I stand corrected if this has to be changed !
if AnsiContainsText(line,'Copyright (c) 1993-') then exit;
if AnsiContainsText(line,'Free Pascal Compiler version ') then exit;
// harmless make error
if AnsiContainsText(line,'make') then
begin
if AnsiContainsText(line,'error 1') then exit;
if AnsiContainsText(line,'(e=1)') then exit;
if AnsiContainsText(line,'error 87') then exit;
if AnsiContainsText(line,'(e=87)') then exit;
//if AnsiContainsText(line,'dependency dropped') then exit;
end;
if AnsiContainsText(line,'~~~~~~~~') then exit;
if AnsiContainsText(line,', coalesced') then exit;
if AnsiContainsText(line,'TODO: ') then exit;
// When building a java cross-compiler
if AnsiContainsText(line,'Generated: ') then exit;
// filter warnings
if AnsiContainsText(line,'warning: ') then
begin
if AnsiContainsText(line,'is not portable') then exit;
if AnsiContainsText(line,'is deprecated') then exit;
if AnsiContainsText(line,'implicit string type conversion') then exit;
if AnsiContainsText(line,'function result does not seem to be set') then exit;
if AnsiContainsText(line,'comparison might be always') then exit;
if AnsiContainsText(line,'converting pointers to signed integers') then exit;
if AnsiContainsText(line,'does not seem to be initialized') then exit;
if AnsiContainsText(line,'an inherited method is hidden') then exit;
if AnsiContainsText(line,'with abstract method') then exit;
if AnsiContainsText(line,'comment level 2 found') then exit;
if AnsiContainsText(line,'did you forget -T') then exit;
if AnsiContainsText(line,'is not recommended') then exit;
if AnsiContainsText(line,'were not initialized') then exit;
if AnsiContainsText(line,'which is not available for the') then exit;
if AnsiContainsText(line,'argument unused during compilation') then exit;
if AnsiContainsText(line,'invalid unitname') then exit;
if AnsiContainsText(line,'procedure type "FAR" ignored') then exit;
if AnsiContainsText(line,'duplicate unit') then exit;
if AnsiContainsText(line,'is ignored for the current target platform') then exit;
if AnsiContainsText(line,'Inlining disabled') then exit;
if AnsiContainsText(line,'not yet supported inside inline procedure/function') then exit;
if AnsiContainsText(line,'Check size of memory operand') then exit;
if AnsiContainsText(line,'User defined: TODO') then exit;
if AnsiContainsText(line,'Circular dependency detected when compiling target') then exit;
if AnsiContainsText(line,'overriding recipe for target') then exit;
if AnsiContainsText(line,'ignoring old recipe for target') then exit;
if AnsiContainsText(line,'Case statement does not handle all possible cases') then exit;
if AnsiContainsText(line,'unreachable code') then exit;
if AnsiContainsText(line,'Fix implicit pointer conversions') then exit;
if AnsiContainsText(line,'are not related') then exit;
if AnsiContainsText(line,'Constructor should be public') then exit;
if AnsiContainsText(line,'is experimental') then exit;
if AnsiContainsText(line,'This code is not thread-safe') then exit;
if AnsiContainsText(line,'Range check error while') then exit;
// when generating help
if AnsiContainsText(line,'is unknown') then exit;
{$ifdef MSWINDOWS}
if AnsiContainsText(line,'unable to determine the libgcc path') then exit;
{$endif}
end;
// suppress "trivial"* build commands
{$ifdef MSWINDOWS}
if AnsiContainsText(line,'rm.exe ') then exit;
if AnsiContainsText(line,'mkdir.exe ') then exit;
if AnsiContainsText(line,'mv.exe ') then exit;
if AnsiContainsText(line,'cmp.exe ') then exit;
if (AnsiContainsText(line,'cp.exe ')) AND (AnsiContainsText(line,'.compiled')) then exit;
{$endif}
s:='rm -f ';
if AnsiContainsText(line,'/'+s) OR AnsiStartsText(s,line) then exit;
if AnsiContainsText(line,'/'+TrimRight(s)) OR AnsiStartsText(TrimRight(s),line) then exit;
s:='rm -rf ';
if AnsiContainsText(line,'/'+s) OR AnsiStartsText(s,line) then exit;
if AnsiContainsText(line,'/'+TrimRight(s)) OR AnsiStartsText(TrimRight(s),line) then exit;
s:='mkdir ';
if AnsiContainsText(line,'/'+s) OR AnsiStartsText(s,line) then exit;
s:='mv ';
if AnsiContainsText(line,'/'+s) OR AnsiStartsText(s,line) then exit;
s:='cp ';
if ( (AnsiContainsText(line,'/'+s) OR AnsiStartsText(s,line)) AND AnsiContainsText(line,'.compiled') ) then exit;
s:='grep: ';