Sign in Join

Live chat for Free Pascal, Lazarus, Delphi and every Pascal programmer.

Mailing lists / fpc-pascal / 2017-01

TProcess failed running Swift compiler

3 messages · 2 people ·

Reply by email Subscribe to this list Your subscription

Read-only archive. Use j and k to move between messages.

Mr Bee

Hi,
I'm writing a simple editor for Swift language. I use TProcess to run the Swift REPL. Unfortunately, TProcess failed to execute the Swift REPL for no obvious reasons. The Swift compiler and REPL are installed just fine and able to execute any Swift codes. My exact same code has no problem to run other executable files, like bash or Python interpreter. Here's the code:
_____ start code _____
program Execute;
uses  SysUtils, Process;   function ExecCmd(const ExeName: string; const Params: array of string;                  var OutText: string; InText: string = ''): boolean;const  READ_BYTES = 65536;var  proc: TProcess;  i, numBytes, bytesRead,   outputLength, available: integer;begin  proc := TProcess.Create(nil);  proc.Executable := ExeName;  proc.Options := [poUsePipes, poStderrToOutput];   // set params  if high(Params) >= 0 then    for i := low(Params) to high(Params) do      proc.Parameters.Add(Params[i]);   // execute  bytesRead := 0;  outputLength := 0;  try    try      proc.Execute;       // write stdin data      if InText '' then begin        InText := InText + LineEnding;        proc.Input.Write(InText[1], Length(InText));      end;       while proc.Running do      begin        // read stdout size        available := proc.Output.NumBytesAvailable;        if available > 0 then        begin          if (bytesRead + available) > outputLength then          begin            outputLength := bytesRead + READ_BYTES;            SetLength(OutText, outputLength);          end;           numBytes := proc.Output.Read(OutText[1+bytesRead], available);          if numBytes > 0 then Inc(bytesRead, numBytes);        end        else          Sleep(10);      end;       // read stdout data      available := proc.Output.NumBytesAvailable;      while available > 0 do      begin        if (bytesRead + available) > outputlength then        begin          outputLength := bytesRead + READ_BYTES;          SetLength(OutText, outputLength);        end;         numBytes := proc.Output.Read(OutText[1+bytesRead], available);        if numBytes > 0 then Inc(bytesRead, numBytes);        available := proc.Output.NumBytesAvailable;      end;      SetLength(OutText, bytesRead);            Result := true;    except      // failed      on e : Exception do        begin          Result := false;          SetLength(OutText, bytesRead);        end;    end;  finally    proc.Free;  end;end; var  s: string = '';begin  ExecCmd('/bin/ls',['-l'],s);        // <- OK   //ExecCmd('swift',['test.swift'],s);  // <- FAILED!  //ExecCmd('python',['test.py'],s);    // <- OK     writeln(s);end.
_____ end code _____
The test code called by the REPL is just a simple hello world program, nothing fancy. Can anyone here enlighten me what did I do wrong in the code above? I'm on FPC v.3.0, Linux Ubuntu 14.04, and Swift v.3.0. Thank you.
Regards, 
–Mr Bee

Mr Bee
to Mr Bee

I found the answer myself. It seems that I need to supply the full path to the swift REPL executable. I don't understand why it requires it for swift because it could call python executable just fine without using full path. Anyway, problem solved. :)  –Mr Bee

Pada Senin, 9 Januari 2017 12:51, Mr Bee menulis:

Hi,
I'm writing a simple editor for Swift language. I use TProcess to run the Swift REPL. Unfortunately, TProcess failed to execute the Swift REPL for no obvious reasons. The Swift compiler and REPL are installed just fine and able to execute any Swift codes. My exact same code has no problem to run other executable files, like bash or Python interpreter. Here's the code:
_____ start code _____
program Execute;
uses  SysUtils, Process;   function ExecCmd(const ExeName: string; const Params: array of string;                  var OutText: string; InText: string = ''): boolean;const  READ_BYTES = 65536;var  proc: TProcess;  i, numBytes, bytesRead,   outputLength, available: integer;begin  proc := TProcess.Create(nil);  proc.Executable := ExeName;  proc.Options := [poUsePipes, poStderrToOutput];   // set params  if high(Params) >= 0 then    for i := low(Params) to high(Params) do      proc.Parameters.Add(Params[i]);   // execute  bytesRead := 0;  outputLength := 0;  try    try      proc.Execute;       // write stdin data      if InText '' then begin        InText := InText + LineEnding;        proc.Input.Write(InText[1], Length(InText));      end;       while proc.Running do      begin        // read stdout size        available := proc.Output.NumBytesAvailable;        if available > 0 then        begin          if (bytesRead + available) > outputLength then          begin            outputLength := bytesRead + READ_BYTES;            SetLength(OutText, outputLength);          end;           numBytes := proc.Output.Read(OutText[1+bytesRead], available);          if numBytes > 0 then Inc(bytesRead, numBytes);        end        else          Sleep(10);      end;       // read stdout data      available := proc.Output.NumBytesAvailable;      while available > 0 do      begin        if (bytesRead + available) > outputlength then        begin          outputLength := bytesRead + READ_BYTES;          SetLength(OutText, outputLength);        end;         numBytes := proc.Output.Read(OutText[1+bytesRead], available);        if numBytes > 0 then Inc(bytesRead, numBytes);        available := proc.Output.NumBytesAvailable;      end;      SetLength(OutText, bytesRead);            Result := true;    except      // failed      on e : Exception do        begin          Result := false;          SetLength(OutText, bytesRead);        end;    end;  finally    proc.Free;  end;end; var  s: string = '';begin  ExecCmd('/bin/ls',['-l'],s);        // <- OK   //ExecCmd('swift',['test.swift'],s);  // <- FAILED!  //ExecCmd('python',['test.py'],s);    // <- OK     writeln(s);end.
_____ end code _____
The test code called by the REPL is just a simple hello world program, nothing fancy. Can anyone here enlighten me what did I do wrong in the code above? I'm on FPC v.3.0, Linux Ubuntu 14.04, and Swift v.3.0. Thank you.
Regards, 
–Mr Bee

Lukasz Sokol
to Mr Bee

Hi Mr Bee,

Mr Bee wrote 8 lines
I found the answer myself. It seems that I need to supply the full path to the swift REPL executable. I don't understand why it requires it for swift because it could call python executable just fine without using full path. Anyway, problem solved. :) –Mr Bee

Can you verify what your $PATH environment variable holds for either interpreter ? ;) (just a thought)

-L.