summaryrefslogtreecommitdiff
path: root/examples/apps/ide/src/builderthread.pas
diff options
context:
space:
mode:
authorGraeme Geldenhuys <graeme@mastermaths.co.za>2011-07-16 19:56:28 +0200
committerGraeme Geldenhuys <graeme@mastermaths.co.za>2011-07-16 19:56:28 +0200
commit7eee7a7d0ffb55fbee2b7d2e3f2903ad1f7711fc (patch)
tree94417b26c048f55849efb1fc92ecfdff0d1a58a0 /examples/apps/ide/src/builderthread.pas
parent2122524e4d56618197e4f0ddd69db49f3552bbfb (diff)
parent0a6e1179e7f192f4350a01074de86f77f0e927ca (diff)
downloadfpGUI-7eee7a7d0ffb55fbee2b7d2e3f2903ad1f7711fc.tar.xz
Merged fpgIDE project as a subdirectory examples/apps/ide/
Diffstat (limited to 'examples/apps/ide/src/builderthread.pas')
-rw-r--r--examples/apps/ide/src/builderthread.pas128
1 files changed, 128 insertions, 0 deletions
diff --git a/examples/apps/ide/src/builderthread.pas b/examples/apps/ide/src/builderthread.pas
new file mode 100644
index 00000000..bfdc48b1
--- /dev/null
+++ b/examples/apps/ide/src/builderthread.pas
@@ -0,0 +1,128 @@
+unit BuilderThread;
+
+{$mode objfpc}{$H+}
+
+interface
+
+uses
+ Classes, SysUtils;
+
+type
+ TOutputLineEvent = procedure(Sender: TObject; const ALine: string) of object;
+
+ TBuilderThread = class(TThread)
+ private
+ FBuildMode: integer;
+ FOnAvailableOutput: TOutputLineEvent;
+ OutputLine: string;
+ procedure DoOutputLine;
+ protected
+ procedure Execute; override;
+ public
+ procedure AfterConstruction; override;
+ property BuildMode: integer read FBuildMode write FBuildMode;
+ property OnAvailableOutput: TOutputLineEvent read FOnAvailableOutput write FOnAvailableOutput;
+ end;
+
+implementation
+
+uses
+ project
+ ,process
+ ,fpg_base
+ ,fpg_iniutils
+ ,fpg_utils
+ ,ideconst
+ ,idemacros
+ ;
+
+{ TBuilderThread }
+
+procedure TBuilderThread.AfterConstruction;
+begin
+ inherited AfterConstruction;
+ FBuildMode := -1; // signals use of project's default build mode
+ FreeOnTerminate := True;
+end;
+
+procedure TBuilderThread.Execute;
+const
+ BufSize = 1024; //4096;
+var
+ p: TProcess;
+ c: TfpgString;
+ unitdir: TfpgString;
+ Buf: string;
+ Count: integer;
+ i: integer;
+ LineStart: integer;
+begin
+ unitdir := GProject.ProjectDir + GProject.UnitOutputDir;
+ unitdir := GMacroList.ExpandMacro(unitdir);
+ if not fpgDirectoryExists(unitdir) then
+ begin
+ {$IFDEF DEBUG}
+ writeln('DEBUG: TBuilderThread.Execute - Creating dir: ' + unitdir);
+ {$ENDIF}
+ fpgForceDirectories(unitDir);
+ end;
+
+ p := TProcess.Create(nil);
+ p.Options := [poUsePipes, poStdErrToOutPut];
+ p.ShowWindow := swoShowNormal;
+ p.CurrentDirectory := GProject.ProjectDir;
+
+ // build compilation string
+ c := gINI.ReadString(cEnvironment, 'Compiler', '');
+ c := c + GProject.GenerateCmdLine(False, BuildMode);
+ c := GMacroList.ExpandMacro(c);
+
+// AddMessage('Compile command: ' + c);
+ p.CommandLine := c;
+ try
+ p.Execute;
+
+ { Now process the output }
+ OutputLine:='';
+ SetLength(Buf,BufSize);
+ repeat
+ if (p.Output<>nil) then
+ begin
+ Count:=p.Output.Read(Buf[1],Length(Buf));
+ end
+ else
+ Count:=0;
+ LineStart:=1;
+ i:=1;
+ while i<=Count do
+ begin
+ if Buf[i] in [#10,#13] then
+ begin
+ OutputLine:=OutputLine+Copy(Buf,LineStart,i-LineStart);
+ Synchronize(@DoOutputLine);
+ OutputLine:='';
+ if (i<Count) and (Buf[i+1] in [#10,#13]) and (Buf[i]<>Buf[i+1]) then
+ inc(i);
+ LineStart:=i+1;
+ end;
+ inc(i);
+ end;
+ OutputLine:=Copy(Buf,LineStart,Count-LineStart+1);
+ until Count=0;
+ if OutputLine <> '' then
+ Synchronize(@DoOutputLine);
+ p.WaitOnExit;
+ finally
+ FreeAndNil(p);
+ end;
+
+end;
+
+procedure TBuilderThread.DoOutputLine;
+begin
+ if Assigned(FOnAvailableOutput) then
+ FOnAvailableOutput(self, OutputLine);
+end;
+
+end.
+