InstantFPC/fr

This is an old revision of InstantFPC/fr, edited by E-ric at . The current revision is 102400.

Created page with "{{InstantFPC}} <br> <br> =Overview= Cet outil vous permet d'exécuter un programme Pascal comme un script sous Linux, BSD, OS X et Windows. P.ex. : <syntaxhighlight> #!/usr/..."

Overview

Cet outil vous permet d'exécuter un programme Pascal comme un script sous Linux, BSD, OS X et Windows. P.ex. :

#!/usr/bin/env instantfpc
begin
  writeln('Hello fpc user');
end.

Enregistrez le fichier en hello.pas, définissez la permission en exécution (pour Linux et consort) et exécutez-le:

[]$ chmod a+x hello.pas
[]$ ./hello.pas
Hello fpc user

Notes:

Bien sûr vous pouvez passer des paramètres à votre script:

#!/usr/bin/env instantfpc
var
  i: Integer;
begin
  for i:=0 to ParamCount do writeln(ParamStr(i));
end.

Enregistrez le fichier comme params.pas, définissez la permission en exécution et exécutez-le:

[]$ chmod a+x params.pas
[]$ ./params.pas A B
/home/mattias/.cache/instantfpc/params.pas
A
B

Télécharger

Pas besoin de télécharger: instantfpc fait partie des sources de FPC 2.6+ dans utils/instantfpc et est installé par défaut.

Anciennes versions

Pour fpc 2.5.1 et antérieure: c'est dans les sources de fpc:

svn co http://svn.freepascal.org/svn/fpc/trunk/utils/instantfpc instantfpc

For fpc 2.4.4 and 2.4.5:

svn co https://lazarus-ccr.svn.sourceforge.net/svnroot/lazarus-ccr/applications/instantfpc instantfpc

Installation

Note: It is already installed when using fpc 2.5.1 and above. Windows users have to put the instantfpc.exe into a directory of their PATH.

  1. Compile the instantfpc.lpi with Lazarus or lazbuild. Or compile directly with fpc instantfpc.pas.
  2. Put the executable into PATH, for example /usr/bin:
sudo cp instantfpc /usr/bin/

How it works

It uses a cache directory to compile. Default is $HOME/.cache/instantfpc. If HOME is not set it gives an error. You can override the directory by setting the environment variable INSTANTFPCCACHE. It compares the source with the stored file in cache and if it differs compiles the source, given the parameters in the shebang line (the first line of the script after #!). After successful compile it executes the program. If compilation fails it writes the fpc output to stdout.

Parameters of instantfpc

Passing parameters to the compiler

Compiler parameters can be passed in the shebang line. Note: You can not use the 'env' command.

#!/usr/bin/instantfpc -O1 -Ci
begin
end.

And/Or you can put compiler options in the environment variable INSTANTFPCOPTIONS:

export INSTANTFPCOPTIONS="-Fu~/lazarus/components/codetools/units/x86_64-linux/"
#!/usr/bin/instantfpc
uses FileProcs; // unit FileProcs is in codetools
begin
end.

Example

The following example script delivers the numeronym of a string (e.g. i18n for internationalization) that may be provided either as a parameter or, if missing, be entered by the user:

#!/usr/bin/env instantfpc
uses
  SysUtils;
var
  theWord: String;
begin  
  if ParamStr(1) = '' then
  begin
    write('Please enter the word to be numeronymized: ');
    readln(theWord);
  end
  else
    theWord := ParamStr(1);
  if length(theWord) > 2 then
    writeln(theWord[1] + IntToStr(length(theWord)-2) + theWord[length(theWord)])
  else
    writeln('N/A');
end.

Using with Lazarus

Since Lazarus 0.9.31 the IDE ignores the shebang line. You get all the normal code features.

Install the IDE package instantfpclaz, which comes with Lazarus. This will add a new project item to create instantfpc programs, for example under File / New / Project / InstantFPC program.

Compile in Lazarus

How to convert a Lazarus project to compile it with instantfpc:

Requires:

Setup:

instantfpc --skip-run -B -gl "-Fu$(ProjUnitPath)" $Name($(ProjFile))

Notes:

Debug with Lazarus

How to convert a Lazarus project to debug an instantfpc program:

Requires:

Setup:

$(InstantFPCCache)/$NameOnly($(ProjFile))
$(ProjPath)

Using in Apache

This is how to use instantfpc in Apache Webserver. Instantfpc needs the cache directory, which must be writable by the account of Apache. Let's say Apache is running under user www-data, group www-data (Redhat, Fedora: apache.apache, see apache config for User and Group).

mkdir /var/apacheinstantfpc
chown www-data.www-data /var/apacheinstantfpc

Pass an environment variable to cgi scripts by adding the following line to the Apache configs:

SetEnv INSTANTFPCCACHE /var/apacheinstantfpc

Security advice: Although the apache user must have full access to the cache directory, make sure the directory is not available from outside (e.g. do not put it under /var/www). It may contain old scripts. And like any other type of scripts: Check all input for attacks.

Optimization note: The instantfpc cache uses one file per script name. Do not use the same script name twice, otherwise it will be compiled many times. You can create multiple cache directories. Each <directory> and <virtualhost> section can define their own environment variables, and therefore you can setup one cache directory for each.

Apache example showing env

Configure a directory to execute cgi scripts. For example:

ScriptAlias /cgi-bin/ /usr/lib/cgi-bin/
<Directory "/usr/lib/cgi-bin">
  AllowOverride None
  Options +ExecCGI -MultiViews +SymLinksIfOwnerMatch
  Order allow,deny
  Allow from all
  SetEnv INSTANTFPCCACHE /var/apacheinstantfpc
  # You can put fpc options into /etdc/fpc.cfg or here:
  #SetEnv INSTANTFPCOPTIONS -Fu/path/to/somevar/ppufiles

  # If this is root directory, you might have to set:
  #Require all granted
</Directory>

If your apache has not yet enabled the cgi module:

a2enmode cgi

Put the following script into /usr/lib/cgi-bin/showenv.pas:

#!/usr/bin/instantfpc
{$mode objfpc}{$H+}
uses  SysUtils;
var  i: Integer;
begin
  writeln('Content-type: text/html');
  writeln;
  writeln('<HTML><BODY>');
  for i:=0 to Paramcount do
    writeln('Param ',i,' ',ParamStr(i),'<br>');
  for i:=1 to GetEnvironmentVariableCount do
    writeln('Env ',GetEnvironmentString(i),'<br>');
  writeln('</BODY></HTML>');
end.

Bugs / ToDos / Limitations

Alternatives

The following trick works using the bash shell. Put the program into a file fpc_script.sh:

// 2>/dev/null; fpc fpc_script.pp &> build.log && exec ./fpc_script "$@" || cat build.log; exit
begin
  writeln('');
end.