Torsten Knodt
Torsten Knodt

Reputation: 726

How can I add an additional compiler option in an Alire .toml file?

For cross-compilation I need to specify the MCU type as a option to the Ada, C and theoretically C++ compiler.

How can I do that and in a way that it is also applied when building the dependencies?

At https://alire.ada.dev/docs/catalog-format-spec.html#build-profiles-and-switches I saw how I can replace e.g. the optimization options per build profile.

However, this is not adding, but replacing and, beyond that, seems to only apply to the Ada compiler.

Before adding anything to my alire.toml file, the generated .gpr file in the config directory contains:

   Ada_Compiler_Switches := Ada_Compiler_Switches &
          (
            "-Og" -- Optimize for debug
           ,"-ffunction-sections" -- Separate ELF section for each function
           ,"-fdata-sections" -- Separate ELF section for each variable
           ,"-g" -- Generate debug info
           ,"-gnatwa" -- Enable all warnings

To alire.toml then I add:

[build-switches]
release.optimization = ["-mmcu=avr32dd20"]
development.optimization = ["-mmcu=avr32dd20"]
validation.optimization = ["-mmcu=avr32dd20"]

and get the following in the .gpr file in the config directory:

   Ada_Compiler_Switches := Ada_Compiler_Switches &
          (
            "-mmcu=avr32dd20"
           ,"-g" -- Generate debug info
           ,"-gnatwa" -- Enable all warnings

So the information is replaced and not added (as expected based on the explanation) and unfortunately only applied for Ada and not the other languages.

How can I solve that?

Upvotes: 2

Views: 273

Answers (4)

ATL_DEV
ATL_DEV

Reputation: 9591

For the sake of IDE portability, your projects should be buildable without Alire. Ideally, required configurations should be kept in the GPR project file, or the Alire generated config file. There are cases, however, where need to make quick and temporary changes. In such cases, Alire's project index file is a good place for such changes. Unfortunately, it doesn't support appending additional data to existing settings, but there's a workaround.

In your Alire.toml file, define a new environment variable in the environment table:

[environment]
MMCU.append = "-mmcu=avr32dd20"

As shown in Jere's answer, add the following definition to your GPR project file:

package Compiler is
   for Default_Switches ("Ada") use My_Project_Config.Ada_Compiler_Switches & 
     (external("MMCU");
end Compiler;

Using an environment variable is more flexible. You can even set or append additional flags in a dependent crate's index file, or set defaults in your shell:

export MMCU="-num_cores=5"

Compile! Bob is your uncle, and Ada is your aunt!

Upvotes: 1

Simon Wright
Simon Wright

Reputation: 25491

As another example, I set up a binary project gpio, then alr with minimal_containers. gpio.gpr contains

   for Target use "riscv64-elf";
   for Runtime ("ada") use project'Project_Dir & "../../local/esp32h2";

and the main program is

with Minimal_Containers.Bounded_Vectors;
procedure Gpio is
   package Boolean_Vectors is new Minimal_Containers.Bounded_Vectors
     (Index_Type => Positive,
      Element_Type => Boolean);
   Dummy : Boolean_Vectors.Vector (Capacity => 10);
begin
   null;
end Gpio;

... and it built successfully, so the minimal_containers code must have been compiled with the same (well, at least compatible) switches as the main program.

For confirmation,

$ cd $HOME/local/share/alire/builds/minimal[TAB]/[TAB]/.build
$ head minimal_containers-bounded_vectors.ali
V "GNAT Lib v14"
A -gnatA
A --RTS=/Users/simon/Developer/cortex-gnat-rts/local/esp32h2/
A -mabi=ilp32
A -misa-spec=20191213
A -march=rv32imac_zicsr_zifencei
P DB ZX

RN
RV NO_DISPATCH

where -mabi= and -march= come from that runtime specified in --RTS=.

So, the conclusion is that the compilation environment used when building a withed crate is inherited from the withing crate.

Clearly this is going to work best if the withed crate has a fairly bland project file. If it specified, for example, -mmcu=avr32dd20, things might not work so well.

Upvotes: 2

Jere
Jere

Reputation: 3641

Simon and Alex discussed this, but just for visual clarity, you update your main GPR file in the same directory as your TOML file to have

package Compiler is
   for Default_Switches ("Ada") use My_Project_Config.Ada_Compiler_Switches
      & ("-mmcu=avr32dd20");
end Compiler;

Full example from a test project I have:

with "config/hello_config.gpr";
project Hello is

   for Source_Dirs use ("src/", "config/");
   for Object_Dir use "obj/" & Hello_Config.Build_Profile;
   for Create_Missing_Dirs use "True";
   for Exec_Dir use "bin";
   for Main use ("hello.adb");

   package Compiler is
      for Default_Switches ("Ada") use Hello_Config.Ada_Compiler_Switches
         & ("-gnat2022", "-v");
   end Compiler;

   package Binder is
      for Switches ("Ada") use ("-Es"); --  Symbolic traceback
   end Binder;

   package Install is
      for Artifacts (".") use ("share");
   end Install;

end Hello;

Upvotes: 3

Álex
Álex

Reputation: 1703

At the time (v2.0), you can only replace switches. To append, you need to go the way @simon-wright indicated, which is to use your top-level .gpr file to extend the switches defined in the config one. The top-level .gpr is never edited by Alire after creation.

Upvotes: 2

Related Questions