Multi-language Programming with GPRbuild
Vasiliy FofanovOctober 1, 2015
What is GNAT Project technology? (representation)• Project definition language
– What programming languages are used by project ?– Where are the sources ?– How are they called ?– How to build them ?
• Toolchain definition database– How to find and use toolchains and runtimes ?– User-extensible
What is GNAT Project technology? (operational)
• Project-aware tools– GPR* family
• gprbuild• gprclean• gprinstall …
– Standard GNAT Pro tools• gnatcheck• gnatcoverage …
– IDEs (GPS and GNATbench)
• Project handling API– To develop user tools…
GNAT Project File• A text file describing the various properties of a project• Declarative language (no actions)
– Intuitive Ada-inspired syntax– Compact description even for a complex multi-language project
• Project files can be used to describe most industrial software projects– Multiple build scenarios and configurations– Complex system architectures
• Shared, standalone libraries– Multi-language (including user-defined)
A Simple Examplewith "gtkada";
project My_Project is
for Source_Dirs use ("src1", "src2"); for Object_Dir use "obj"; for Main use ("file.adb");
type Mode is ("production", "debug"); M : Mode := external ("MODE", "debug");
package Compiler is case M is when "production" => for Default_Switches ("Ada") use ("-O2"); when "debug" => for Default_Switches ("Ada") use ("-O0", "-g", "-gnata"); end case; end Compiler;
end My_Project;
Used everywhere!• Common way to describe user software project across all AdaCore
toolchains– Edit sources– Analyze sources– Build / cleanup …
• Common interface
$> gps –P my_project.gpr
$> gprbuild –P my_project.gpr -Xmode=debug
$> gnatmetric –P my_project.gpr
Project Hierarchies• Modularity of software projects can be achieved through a
hierarchy of GNAT projects
• Highly recommended for medium and big projects
• The "with" keyword identifies project dependencieswith "gtkada";with "../core";
project Prj is
end Prj;
Multi-Language Projects• GNAT Projects are multi-language multi-compiler aware
– Supports a number of languages by default, including C and C++• A project file is assumed to be Ada by default, but can declare your
own set via Languages attribute
• All languages are first class citizens• Extensible
– Define your own languages/toolchains if needed(binding generators, scripts, resource compilers, etc…)
for Languages use ("Ada", "C"); -- My sources are Ada and C
Multi-language Project Examplewith "trig";
project My_App is for Languages use ("C"); for Main use ("my_app.c");end My_App;
#include <stdio.h>
float my_sin (float angle);
main ()
{ printf ("%f\n", my_sin (3.1415/2.0)); }
package Trig is function My_Sin (Angle : Float) return Float; pragma Export (C, My_Sin, "my_sin");end Trig;
with Ada.Numerics.Generic_Elementary_Functions;package body Trig is package GEF is new Ada.Numerics.Generic_Elementary_Functions (Float); function My_Sin (Angle : Float) return Float is begin return GEF.Sin (Angle); end My_Sin;end Trig;
library project Trig is for Library_Dir use "lib"; for Library_Kind use "dynamic"; for Library_Interface use ("trig"); for Library_Name use "trig”;
end Trig; for Library_Standalone use "encapsulated";
$> gprbuild my_app.gpr...$> ./my_app1.000000 That’s it!
Defining Targets and Runtimes• Non-native target and specific runtimes can be specified
– In a project:
– On a command line:
for Target use "powerpc-elf";for Runtime("Ada") use "ravenscar-cert";
$> gprbuild –P prj.gpr –-target=powerpc-elf --RTS=ravenscar-cert
GPRbuild• Our multi-language builder
• Understands different languages, compilers and targets
• Takes care of all building steps and complex cases– compiling, binding, linking– shared libraries, encapsulated libraries …
• Can be extended to understand new toolchains
• Can distribute compilation over several computers ?
Distributed builder• Parallel builds have been supported for a long time
gcc –c …gcc –c …gcc –c …gcc –c …
gprbuild prj.gpr –j0
• …but not good enough!– At best a dozen cores or so– But we can drive hundreds of
build decisions at once– Solution: distributed build farm
Distributed builder (2)
• Communication and file sync by lightweight sockets-based protocols
Master machine Slave machines
gprslave gprslave gprslave gprslave
• sources• compile commands
• objects• availability state
$ gprbuild prj --distributed= file://comp1.xyz.com, 164.10.127.4, ...
Where to find it all?• Bundled with all GNAT compiler toolchains• Also as a separate package
– If you want to keep the current compiler and only update the builder• Soon on GitHub (~early 2016)
Use It!• GNAT Project Facility is a mature technology
– Good match for AdaCore product line and our vision of development process
– Should be powerful enough to replace most if not all custom build procedures, while remaining accessible
• Are you using it? If not, why?• Don’t hesitate to ask us for help!
Resources• Online GPR documentation
http://docs.adacore.com/gprbuild-docs/html/gprbuild_ug.html