I have a Gnat/Gprbuild project with several build configurations. I have a main source file and an secondary ads file which the main source file includes:
with Secondary_File; use Secondary_File;
The problem is that in each configuration, the secondary file has a different name. For example, it may be called Secondary_File_1.ads for one config and Secondary_File_2.ads for another. This makes it impossible to use the above with statement.
In C, I would do something like this:
#ifdef BUILD_CFG_1
#include "secondary_file_1.h"
#else
#include "secondary_file_2.h"
#endif
Is there a clever way to do something like this in ADA, using the Gprbuild system?
Many purists reject the idea of preprocessing, but it’s possible using GNAT.
You can include this in a GPR-based build environment by writing your source, e.g. main.adb, like so:
with Secondary_File_$NUMBER;
procedure Main is
begin
null;
end Main;
(observe the $NUMBER) and the project file like so:
project Prj is
for Main use ("main.adb");
-- Configurations
type Config_Type is ("config_1", "config_2");
-- Which one? (default is "config_1")
Config : Config_Type := external ("CONFIG", "config_1");
package Compiler is
case Config is
when "config_1" =>
for Switches ("main.adb") use ("-gnateDNUMBER=1");
when "config_2" =>
for Switches ("main.adb") use ("-gnateDNUMBER=2");
end case;
end Compiler;
end Prj;
Compiling gives
$ gprbuild -Pprj
Compile
[Ada] main.adb
main.adb:1:06: error: file "secondary_file_1.ads" not found
gprbuild: *** compilation phase failed
(the compilation looked for secondary_file_1.ads)
$ gprbuild -Pprj -XCONFIG=config_2
Compile
[Ada] main.adb
main.adb:1:06: error: file "secondary_file_2.ads" not found
gprbuild: *** compilation phase failed
(the compilation looked for secondary_file_2.ads)
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With