Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Specifying different platform specific package at compile time in Ada (GNAT)

I'm still new to the Ada programming world so forgive me if this question is obvious.

I am looking at developing an application (in Ada, using the features in the 2005 revision) that reads from the serial port and basically performs manipulation of the strings and numbers it receives from an external device.

Now my intention was to likely use Florist and the POSIX terminal interfaces to do all the serial work on Linux first....I'll get to Windows/MacOS/etc... some other time but I want to leave that option open.

I would like to follow Ada best practices in whatever I do with this. So instead of a hack like conditional compilation under C (which I know Ada does not have anyway) I would like to find out how you are suppose to specify a change in package files from the command line (gnatmake for example)?

The only thing I can think of right now is I could name all platform packages exactly the same (i.e. package name Serial.Connector with the same filenames) and place them in different folders in the project archive and then upon compilation specify the directories/Libraries to look in for the files with -I argument and change directory names for different platforms.

This is way I was shown for GCC using C/C++...is this still the best way with Ada using GNAT?.

Thanks, -Josh

like image 886
Josh Avatar asked Jul 23 '10 18:07

Josh


3 Answers

That's a perfectly acceptable way of handling this kind of situation. If at all possible you should have a common package specification (or specifications if more than one is appropriate), with all the platform-specific stuff strictly confined to the corresponding package body variations.

(If you did want to go down the preprocessor path, there's a GNAT preprocessor called gnatprep that can be used, but I don't like conditional compilation either, so I'd recommend staying with the separate subdirectories approach.)

like image 185
Marc C Avatar answered Nov 20 '22 04:11

Marc C


You could use the GNAT Project file package Naming: an extract from a real example, where I wanted to choose between two versions of a package in the same directory, one with debug additions, is

...
type Debug_Code is ("no", "yes");
Debug : Debug_Code := External ("DEBUG", "no");
...
package Naming is
   case Debug is
      when "yes" =>
         for Spec ("BC.Support.Managed_Storage")
           use "bc-support-managed_storage.ads-debug";
         for Body ("BC.Support.Managed_Storage")
           use "bc-support-managed_storage.adb-debug";
      when "no" =>
         null;
   end case;
end Naming;

To select the special naming, either set the environment variable DEBUG to yes or build with gnatmake -XDEBUG=yes.

like image 20
Simon Wright Avatar answered Nov 20 '22 04:11

Simon Wright


Yes, the generally accepted way to handle this in Ada is to do it with different files, selected by your build system. Gnu make is about as multiplatform as it gets, and can allow you to build different files (with different names and/or directories and everything) under different configurations.

As a matter of fact, I find this a superior way (over #ifdefs) to do it in C as well.

like image 3
T.E.D. Avatar answered Nov 20 '22 03:11

T.E.D.