Conditional compilation by Target controller?

Discussion and questions about programming with Ultibo.
Chord
Posts: 24
Joined: Sat Jan 07, 2017 6:02 am

Conditional compilation by Target controller?

Postby Chord » Thu Apr 11, 2019 2:36 pm

Usually ultibo source has specific unit file by its target controller.

for example :
for QEMUVPB,

Code: Select all

uses
  QEMUVersatilePB,

for RPI3B,

Code: Select all

uses
  RaspberryPi3,


Does pascal has something can automatically choose its line to compile by project's target controller, like C's preprocessor?
User avatar
Ultibo
Site Admin
Posts: 2207
Joined: Sat Dec 19, 2015 3:49 am
Location: Australia

Re: Conditional compilation by Target controller?

Postby Ultibo » Fri Apr 12, 2019 12:04 am

Chord wrote:Does pascal has something can automatically choose its line to compile by project's target controller, like C's preprocessor?

Not yet, we borrowed the target controller functionality from the embedded target of FPC and something it didn't implement was adding a define to identify the selected target (whereas there is one for the target CPU and OS).

We hope to add that as an option when we upgrade to the latest FPC in the next release but in the meantime there are a couple of easy ways to deal with this.

For many small projects, tests and examples we compile from the command line and just maintain a set of batch files with the correct parameters for each Pi model we want to compile for, when compiling from the command line it is a simple matter to just add an extra define using the -d option (eg -dRPI3) to allow testing for it in the code.

If you prefer to use Lazarus you can do something equally as simple by just creating multiple .lpr files for your project. The basic idea is to put only the bare minimum amount of code in the project lpr file and instead put all of your project code into units that are included from the project file.

For example you might have MyProject.lpr (and the corresponding MyProject.lpi) which includes the necessary driver units so that your project has all the needed services available but simply does some very basic setup before calling your main project code in another unit. Instead of having just MyProject.lpr you could have MyProject-RPI.lpr, MyProject-RPI3.lpr and MyProject-QEUM.lpr with each one including the correct driver units for that target, then you can add a define to each project under Project, Project Options..., Compiler Options, Custom options, Defines... and also set the correct target CPU, processor and controller so the project lpr and lpi files contain everything needed to correctly compile for that target.
Ultibo.org | Make something amazing
https://ultibo.org
User avatar
Ultibo
Site Admin
Posts: 2207
Joined: Sat Dec 19, 2015 3:49 am
Location: Australia

Re: Conditional compilation by Target controller?

Postby Ultibo » Fri Apr 12, 2019 12:13 am

It's funny how sometimes you don't think of the obvious solution until you try to explain it to someone else.

After posting the above I suddenly realized there is an even easier way to add a define that will be automatically available to every project regardless of whether you compile from the command line or from Lazarus.

In the bin directory of your Ultibo installation (C:\Ultibo\Core\fpc\3.1.1\bin\i386-Win32 on Windows) you will find four config files called rpi.cfg, rpi2.cfg, rpi3.cfg and qemuvpb.cfg which provide the default compiler options for each target. If you add a new line to each one with a define for that target (eg -dRPI or -dRPI2 etc) then it will be available to test for in any project you compile for that target.

This is a such a simple idea that we'll add these to the default installation and instructions in addition to adding a target controller specific define (eg RPIA, RPIB, RPI3B etc) to the next release.
Ultibo.org | Make something amazing
https://ultibo.org

Return to “General”

Who is online

Users browsing this forum: No registered users and 0 guests