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
|
unit TestDbgCompilerProcess;
{$mode objfpc}{$H+}
{$modeswitch advancedrecords}
interface
uses
Classes, SysUtils, fgl, UTF8Process, LazLoggerBase, FileUtil, LazFileUtils,
TestOutputLogger, process, fpcunit;
type
{ TCompilerProcess }
TCompilerProcess = object
private
FCommandLine: string;
FCompilerOutput: string;
FDidRunCompiler: Boolean;
FLastError: Integer;
function CheckOutputFileOnDisk(AnExeName, ACommandLine: String): boolean;
function CheckOutputDirOnDisk(AnDirName, ACommandLine: String): boolean;
procedure MaybeDeleteFileOnDisk(AnExeName: String);
procedure MaybeDeleteDirOnDisk(AnDirName: String);
function ReadOutput(AProcess:TProcessUTF8): TStringList;
function CallCompiler(const ACommand, ACurDir: String): Boolean;
public
function TestCompile(const FpcExe, FpcOpts, PascalPrgFile, ExeName: string; ForceReCompile: Boolean = False): boolean;
function TestCompileUnits(const FpcExe, FpcOpts, SrcDirName, OutLibName: string; ForceReCompile: Boolean = False): Boolean;
property DidRunCompiler: Boolean read FDidRunCompiler;
property LastError: Integer read FLastError;
property CommandLine: string read FCommandLine;
property CompilerOutput: string read FCompilerOutput;
end;
procedure ClearFilesAndDirs;
implementation
type
{ TCreatedExecutable }
TCreatedExecutable = record
FExeName: string;
FComandline: string;
class operator = (a, b: TCreatedExecutable): Boolean;
end;
{ TCreatedExecutableList }
TCreatedExecutableList = class(specialize TFPGList<TCreatedExecutable>)
private
function IndexOfExe(AnExeName: String): Integer;
protected
procedure DeleteFiles;
procedure DeleteDirectories;
public
procedure AddExe(AnExeName, ACommandLine: String);
procedure RemoveExe(AnExeName: String);
function HasExe(AnExeName: String): String;
end;
var
CreatedExecutableList: TCreatedExecutableList;
CreatedLibDirList: TCreatedExecutableList;
class operator TCreatedExecutable. = (a, b: TCreatedExecutable): Boolean;
begin
raise Exception.Create('unreachable'); // should never enter here
Result := False;
end;
{ TCreatedExecutableList }
function TCreatedExecutableList.IndexOfExe(AnExeName: String): Integer;
begin
Result := Count - 1;
while Result >= 0 do begin
if Items[Result].FExeName = AnExeName then
break;
Dec(Result);
end;
end;
procedure TCreatedExecutableList.DeleteFiles;
var
i: Integer;
begin
i := Count - 1;
while i >= 0 do begin
if not DeleteFile(Items[i].FExeName) then
TestLogger.DebugLn(['Failed to delete file ', Items[i].FExeName, ' err: ', GetLastOSError]);
Dec(i);
end;
Clear;
end;
procedure TCreatedExecutableList.DeleteDirectories;
var
i: Integer;
begin
i := Count - 1;
while i >= 0 do begin
DeleteDirectory(Items[i].FExeName, False);
Dec(i);
end;
Clear;
end;
procedure TCreatedExecutableList.AddExe(AnExeName, ACommandLine: String);
var
i: Integer;
n: TCreatedExecutableList;
begin
i := IndexOfExe(AnExeName);
if i < 0 then begin
i := Count;
Count := i + 1;
end;
List^[i].FExeName := AnExeName;
List^[i].FComandline := ACommandLine;
end;
procedure TCreatedExecutableList.RemoveExe(AnExeName: String);
var
i: Integer;
begin
i := IndexOfExe(AnExeName);
if i >= 0 then
Delete(i);
end;
function TCreatedExecutableList.HasExe(AnExeName: String): String;
var
i: Integer;
begin
Result := '';
i := IndexOfExe(AnExeName);
if i >= 0 then
Result := Items[i].FExeName;
end;
{ TCompilerProcess }
function TCompilerProcess.CheckOutputFileOnDisk(AnExeName, ACommandLine: String
): boolean;
var
LastCmdLine: String;
begin
Result := False;
LastCmdLine := CreatedExecutableList.HasExe(AnExeName);
if LastCmdLine = '' then begin
// should not yet exist
if FileExists(AnExeName) then
raise EAssertionFailedError.Create('Unexpected pre-existing compiled exe ' + AnExeName);
end
else if LastCmdLine = ACommandLine then begin
// existing file can be used
if not FileExists(AnExeName) then begin
DebugLn('Expected file from last build, but was missing: ' + AnExeName);
CreatedExecutableList.RemoveExe(AnExeName);
exit;
end;
Result := True;
end
else begin
// need rebuild
DebugLn(['Rebuilding: ', AnExeName]);
DeleteFile(AnExeName);
CreatedExecutableList.RemoveExe(AnExeName);
if FileExists(AnExeName) then
raise EAssertionFailedError.Create('Unable to delete old file ' + AnExeName);
end;
end;
procedure TCompilerProcess.MaybeDeleteFileOnDisk(AnExeName: String);
begin
if CreatedExecutableList.IndexOfExe(AnExeName) >= 0 then begin
if not FileExists(AnExeName) then
DebugLn('Expected file from last build, but was missing: ' + AnExeName)
else
DeleteFile(AnExeName);
CreatedExecutableList.RemoveExe(AnExeName);
end;
if FileExists(AnExeName) then
raise EAssertionFailedError.Create('Unexpected pre-existing compiled exe ' + AnExeName);
end;
procedure TCompilerProcess.MaybeDeleteDirOnDisk(AnDirName: String);
begin
if CreatedLibDirList.IndexOfExe(AnDirName) >= 0 then begin
if not DirectoryExists(AnDirName) then
DebugLn('Expected dir from last build, but was missing: ' + AnDirName)
else
DeleteDirectory(AnDirName, False);
CreatedLibDirList.RemoveExe(AnDirName);
end;
if DirectoryExists(AnDirName) then
raise EAssertionFailedError.Create('Unexpected pre-existing compiled exe ' + AnDirName);
end;
function TCompilerProcess.CheckOutputDirOnDisk(AnDirName, ACommandLine: String
): boolean;
var
LastCmdLine: String;
begin
Result := False;
LastCmdLine := CreatedLibDirList.HasExe(AnDirName);
if LastCmdLine = '' then begin
// should not yet exist
if DirectoryExists(AnDirName) then
raise EAssertionFailedError.Create('Unexpected pre-existing compiled dir ' + AnDirName);
end
else if LastCmdLine = ACommandLine then begin
// existing dir can be used
if not DirectoryExists(AnDirName) then begin
DebugLn('Expected dir from last build, but was missing: ' + AnDirName);
CreatedLibDirList.RemoveExe(AnDirName);
exit;
end;
Result := True;
end
else begin
// need rebuild
DebugLn(['Rebuilding(dir): ', AnDirName]);
DeleteDirectory(AnDirName, False);
CreatedLibDirList.RemoveExe(AnDirName);
if DirectoryExists(AnDirName) then
raise EAssertionFailedError.Create('Unable to delete old dir ' + AnDirName);
end;
end;
function TCompilerProcess.ReadOutput(AProcess: TProcessUTF8): TStringList;
const
TIME_OUT = 300;
READ_BYTES = 8192;
var
BytesRead: Integer;
n: Integer;
EndTime: TDateTime;
OutputStream: TMemoryStream;
begin
OutputStream := TMemoryStream.Create;
BytesRead := 0;
EndTime := Now + TIME_OUT / (24 * 60 * 60);
while AProcess.Running and (Now<EndTime) do
begin
// make sure we have room
OutputStream.SetSize(BytesRead + READ_BYTES);
// try reading it
if AProcess.Output.NumBytesAvailable>0 then begin
n := AProcess.Output.Read((OutputStream.Memory + BytesRead)^, READ_BYTES);
Inc(BytesRead, n)
end
else
// no data, wait 100 ms
Sleep(100);
end;
// read last part
repeat
// make sure we have room
OutputStream.SetSize(BytesRead + READ_BYTES);
// try reading it
if AProcess.Output.NumBytesAvailable>0 then begin
n := AProcess.Output.Read((OutputStream.Memory + BytesRead)^, READ_BYTES);
Inc(BytesRead, n);
end
else
n := 0;
until n <= 0;
OutputStream.SetSize(BytesRead);
OutputStream.Position:=0;
Result := TStringList.Create;
Result.LoadFromStream(OutputStream);
OutputStream.Free;
end;
function TCompilerProcess.CallCompiler(const ACommand, ACurDir: String
): Boolean;
var
FpcBuild: TProcessUTF8;
OutputLines: TStrings;
begin
FpcBuild := TProcessUTF8.Create(nil);
OutputLines := nil;
FLastError := -1;
try
{$IFDEF windows}
FpcBuild.Options := [poNewConsole, poUsePipes];
{$ELSE}
FpcBuild.Options := [poNoConsole, poUsePipes];
{$ENDIF}
FpcBuild.ShowWindow := swoHIDE;
DebugLn(['**** running compiler: ', ACommand]);
FCommandLine := ACommand;
FpcBuild.CommandLine := ACommand;
FpcBuild.CurrentDirectory := ACurDir;
FpcBuild.Execute;
OutputLines := ReadOutput(FpcBuild);
if FpcBuild.Running then begin
FpcBuild.Terminate(99);
end;
FLastError := FpcBuild.ExitStatus;
FCompilerOutput := OutputLines.Text;
finally
FpcBuild.Free;
OutputLines.Free;
end;
Result := FLastError = 0;
if not Result then begin
DebugLn(['**** compile error: ', FCompilerOutput, ' FLastError: ', FLastError, ' OS: ',GetLastOSError, ' ACurDir:', ACurDir]);
TestLogger.DebugLn(['**** compile error: ', FCompilerOutput, ' FLastError: ', FLastError, ' OS: ',GetLastOSError, ' ACurDir:', ACurDir]);
end;
end;
function TCompilerProcess.TestCompile(const FpcExe, FpcOpts, PascalPrgFile,
ExeName: string; ForceReCompile: Boolean): boolean;
var
CmdLine: string;
begin
CmdLine := FpcExe + ' -B -MObjFPC -FUlib -o'+ ExeName + ' ' + FpcOpts + ' ' + PascalPrgFile;
if ForceReCompile then begin
Result := False;
MaybeDeleteFileOnDisk(ExeName);
end
else
Result := CheckOutputFileOnDisk(ExeName, CmdLine);
FDidRunCompiler := not Result;
if Result then
exit;
Result := CallCompiler(CmdLine, ExtractFileDir(PascalPrgFile));
if FileExists(ExeName) then
CreatedExecutableList.AddExe(ExeName, CmdLine)
else
Result := False;
end;
function TCompilerProcess.TestCompileUnits(const FpcExe, FpcOpts, SrcDirName,
OutLibName: string; ForceReCompile: Boolean): Boolean;
var
CmdLine: string;
begin
CmdLine := FpcExe + ' -MObjFPC -FU' + OutLibName + ' ' + FpcOpts + ' ' + SrcDirName;
if ForceReCompile then begin
Result := False;
MaybeDeleteDirOnDisk(OutLibName);
end
else
Result := CheckOutputDirOnDisk(OutLibName, CmdLine);
FDidRunCompiler := not Result;
if Result then
exit;
CreateDirUTF8(OutLibName);
Result := CallCompiler(CmdLine, ExtractFileDir(SrcDirName));
if DirectoryExists(AppendPathDelim(OutLibName)) then
CreatedLibDirList.AddExe(OutLibName, CmdLine)
else
Result := False;
end;
procedure ClearFilesAndDirs;
begin
CreatedExecutableList.DeleteFiles;
CreatedLibDirList.DeleteDirectories;
end;
initialization
CreatedExecutableList := TCreatedExecutableList.Create;
CreatedLibDirList := TCreatedExecutableList.Create;
finalization
ClearFilesAndDirs;
CreatedExecutableList.Free;
CreatedLibDirList.Free;
end.
|