InstantFPC

From Lazarus wiki
Revision as of 18:12, 26 January 2012 by Mattias2 (talk | contribs) (Using in Apache)

Overview

This tool allows to run pascal program as scripts under Linux, BSD, OS X and Windows. For example:

<Delphi>

  1. !/usr/bin/env instantfpc

begin

 writeln('Hello fpc user');

end. </Delphi>

Save the file as hello.pas, set the permission to execute and run it:

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

Notes:

  • The first line is called shebang and is stripped from the source before passing it to the compiler.
  • The line 'program hello;' is optional in FPC. Because instantfpc passes the -o option to the compiler the line is ignored, so you can omit it.

Of course you can pass parameters to your script:

<Delphi>

  1. !/usr/bin/env instantfpc

var

 i: Integer;

begin

 for i:=0 to ParamCount do writeln(ParamStr(i));

end. </Delphi>

Save the file as params.pas, set the permission to execute and run it:

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

Download

Instantfpc is part of the fpc 2.5.1 sources under utils/instantfpc and installed by default.

From svn

For fpc 2.5.1 and above: It is in the fpc sources:

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.

  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

  • -h: Print help and exit
  • -v: Print current version and exit
  • --get-cache: Print current cache directory and exit
  • --set-cache=<directory>: Set cache directory
  • -B: always compile even if cache is valid.
  • --compiler=<path to compiler> use this compiler. By default fpc is searched in PATH.
  • --skip-run: Do not execute the program. Useful to test if script compiles. Best combined with -B.
  • Environment variable INSTANTFPCCACHE: Set cache directory. The --get-cache can override this.
  • Environment variable INSTANTFPCOPTIONS: Options to pass to the compiler. Passed before the command line parameters.

Passing parameters to the compiler

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

<Delphi>

  1. !/usr/bin/instantfpc -O1 -Ci

begin end. </Delphi>

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

export INSTANTFPCOPTIONS="-Fu~/lazarus/components/codetools/units/x86_64-linux/"

<Delphi>

  1. !/usr/bin/instantfpc

uses FileProcs; // unit FileProcs is in codetools begin end. </Delphi>

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:

  • Lazarus project main source file is the instantfpc program

Setup:

  • Go to Project Options / Compiler Options / Compilation
  • Disable all Call on checks for Compiler
  • Set in Execute before the Command to:
instantfpc --skip-run -B -gl "-Fu$(ProjUnitPath)" $Name($(ProjFile))
  • Enable checkboxes to scan make and fpc

Notes:

  • The -gl will tell FPC to compile with debugging information
  • The --skip-run -B compiles without executing
  • See IDE Macros

Debug with Lazarus

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

Requires:

  • The above Compile in Lazarus

Setup:

  • Go to Run / Run parameters
  • Set Local / Host application to
$(InstantFPCCache)/$NameOnly($(ProjFile))
  • Set Working Directory to
$(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
</Directory>

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

<Delphi>

  1. !/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),'
');   for i:=1 to GetEnvironmentVariableCount do     writeln('Env ',GetEnvironmentString(i),'
');   writeln('</BODY></HTML>'); end. </Delphi>

Bugs / ToDos

  • Changes to the compiler or installed units are not checked. If you install a new compiler you should clean the cache (e.g. delete the directory ~/.cache/instantfpc).

Alternatives

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

<Delphi> // 2>/dev/null; fpc fpc_script.pp &> build.log && exec ./fpc_script "$@" || cat build.log; exit begin

 writeln();

end. </Delphi>