Difference between revisions of "InstantFPC"

From Lazarus wiki
(Download)
(Parameters of instantfpc)
Line 64: Line 64:
 
*-v: Print current version and exit
 
*-v: Print current version and exit
 
*--get-cache: Print current cache directory and exit
 
*--get-cache: Print current cache directory and exit
 +
*--set-cache=<directory>: Set cache directory
 
*-h: print help and exit
 
*-h: print help and exit
 
*-B: always compile even if cache is valid.
 
*-B: always compile even if cache is valid.

Revision as of 09:45, 9 July 2011

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

You can download the whole fpc 2.5.1 or you can download only instantfpc to use it with fpc 2.4.4.

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

Installation

  1. Compile the instantfpc.lpi with Lazarus or lazbuild. Or compile directly with fpc instantfpc.lpr.
  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

  • -v: Print current version and exit
  • --get-cache: Print current cache directory and exit
  • --set-cache=<directory>: Set cache directory
  • -h: print help and exit
  • -B: always compile even if cache is valid.
  • --compiler=<path to compiler> use this compiler. By default fpc is searched in PATH.

Passing parameters to the compiler

Compiler parameters can be passed in the shebang line. But then you can not use the 'env' command.

<Delphi>

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

begin end. </Delphi>

Editing in Lazarus

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

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>