Changeset 161 for trunk/Target.pas


Ignore:
Timestamp:
Aug 20, 2024, 12:20:49 AM (5 weeks ago)
Author:
chronos
Message:
  • Fixed: All targets compilation and run.
File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/Target.pas

    r151 r161  
    8080  protected
    8181    FCompiledExtension: string;
    82     FRunExtension: string;
    8382    FSourceExtension: string;
    8483    FName: string;
     
    118117    procedure Compile; virtual;
    119118    procedure CompileToFile; virtual;
     119    function GetSourceFileName: string; virtual;
     120    function GetCompileParams: TStringArray; virtual;
     121    function GetCompileFileName: string; virtual;
    120122    procedure RunFromFile; virtual;
     123    function GetRunParams: TStringArray; virtual;
    121124    procedure Run; virtual;
    122125    procedure Pause; virtual;
     
    129132    procedure SaveToRegistry(Context: TRegistryContext); virtual;
    130133    property SourceExtension: string read FSourceExtension;
    131     property RunExtension: string read FRunExtension;
    132134    property CompiledExtension: string read FCompiledExtension;
    133135    property Name: string read FName;
     
    183185
    184186uses
    185   FormConsole;
     187  FormConsole, Common;
    186188
    187189procedure UpdateTranslation;
     
    491493procedure TTarget.CompileToFile;
    492494var
    493   Process: TProcess;
    494   CompiledFile: string;
     495  SourceFile: string;
    495496  Lines: TStringList;
    496   I: Integer;
    497 begin
    498   CompiledFile := ExtractFilePath(ProjectFileName) +
    499     CompiledDir + DirectorySeparator + Name + DirectorySeparator +
    500     ExtractFileNameOnly(ProjectFileName) + SourceExtension;
    501   ForceDirectories(ExtractFilePath(CompiledFile));
     497  Output, Error: string;
     498  Executable: string;
     499  Parameters: array of string;
     500begin
     501  SourceFile := GetSourceFileName;
     502  ForceDirectories(ExtractFilePath(SourceFile));
    502503  with TStringList.Create do
    503504  try
    504505    Text := FTargetCode;
    505     SaveToFile(CompiledFile);
     506    SaveToFile(SourceFile);
    506507  finally
    507508    Free;
    508509  end;
    509510  if CompilerPath <> '' then begin;
    510     if FileExists(CompilerPath) then
    511     try
    512       Process := TProcess.Create(nil);
    513       Process.CurrentDirectory := ExtractFilePath(CompiledFile);
    514       Process.Executable := LongFileName(CompilerPath);
    515       Process.Parameters.Add(LongFileName(CompiledFile));
    516       for I := 0 to GetEnvironmentVariableCount - 1 do
    517         Process.Environment.Add(GetEnvironmentString(I));
    518       Process.Options := [poWaitOnExit, poUsePipes];
    519       Process.Execute;
     511    if FileExists(CompilerPath) then begin
     512      Executable := LongFileName(CompilerPath);
     513      Parameters := GetCompileParams;
     514
     515      ExecuteProgramOutput(Executable, GetCompileParams, GetEnvironmentVariables,
     516        Output, Error, ExitCode, ExtractFilePath(SourceFile));
     517
    520518      if Assigned(FOnLog) then begin
    521519        Lines := TStringList.Create;
    522         Lines.LoadFromStream(Process.Output);
    523         Lines.Insert(0, Process.Executable + ' ' + Process.Parameters.Text);
     520        Lines.Text := Output;
     521        Lines.Insert(0, Executable + ' ' + Implode(',', Parameters));
    524522        FOnLog(Lines);
    525523        Lines.Free;
    526524      end;
    527     finally
    528       Process.Free;
     525
    529526    end else raise Exception.Create(Format(SCompilerNotFound, [CompilerPath]));
    530527  end;
    531528end;
    532529
    533 procedure TTarget.RunFromFile;
    534 var
    535   CompiledFile: string;
    536   RunFile: string;
    537   FormConsole: TFormConsole;
    538 begin
    539   CompiledFile := ExtractFilePath(ProjectFileName) +
     530function TTarget.GetSourceFileName: string;
     531begin
     532  Result := ExtractFilePath(ProjectFileName) +
     533    CompiledDir + DirectorySeparator + Name + DirectorySeparator +
     534    ExtractFileNameOnly(ProjectFileName) + SourceExtension;
     535end;
     536
     537function TTarget.GetCompileParams: TStringArray;
     538begin
     539  Result := [LongFileName(GetSourceFileName)];
     540end;
     541
     542function TTarget.GetCompileFileName: string;
     543begin
     544  Result := ExtractFilePath(ProjectFileName) +
    540545    CompiledDir + DirectorySeparator + Name + DirectorySeparator +
    541546    ExtractFileNameOnly(ProjectFileName) + CompiledExtension;
    542   RunFile := ExtractFilePath(ProjectFileName) +
    543     CompiledDir + DirectorySeparator + Name + DirectorySeparator +
    544     ExtractFileNameOnly(ProjectFileName) + RunExtension;
     547end;
     548
     549procedure TTarget.RunFromFile;
     550var
     551  CompiledFile: string;
     552  FormConsole: TFormConsole;
     553  Parameters: TStringArray;
     554  I: Integer;
     555begin
     556  CompiledFile := GetCompileFileName;
    545557  if FileExists(CompiledFile) then
    546558  try
     
    550562        raise Exception.Create(Format(SExecutorNotFound, [ExecutorPath]));
    551563      FormConsole.Executable := LongFileName(ExecutorPath);
    552       FormConsole.Parameters.Add(LongFileName(RunFile));
     564      Parameters := GetRunParams;
     565      for I := 0 to Length(Parameters) - 1 do
     566        FormConsole.Parameters.Add(Parameters[I]);
    553567    end else
    554       FormConsole.Executable := LongFileName(RunFile);
     568      FormConsole.Executable := LongFileName(CompiledFile);
    555569    FormConsole.ShowModal;
    556570  finally
     
    558572  end
    559573  else raise Exception.Create(Format(SCompiledFileNotFound, [CompiledFile]));
     574end;
     575
     576function TTarget.GetRunParams: TStringArray;
     577begin
     578  Result := [LongFileName(GetCompileFileName)];
    560579end;
    561580
Note: See TracChangeset for help on using the changeset viewer.