LinuxCommandLibrary

gnatprep

Preprocesses Ada code for conditional compilation

TLDR

Use symbol definitions from a file

$ gnatprep [source_file] [target_file] [definitions_file]
copy

Specify symbol values in the command-line
$ gnatprep -D[name]=[value] [source_file] [target_file]
copy

SYNOPSIS

gnatprep [options] input_file [output_file]

PARAMETERS

-Dsymbol[=value]
    Defines a symbol with an optional value. If no value is specified, the symbol is defined with a value of 1.

-Usymbol
    Undefines a symbol.

-Idirectory
    Adds a directory to the include search path. gnatprep searches these directories for included files specified with `#include`.

-v
    Verbose mode: displays more detailed information during processing.

-q
    Quiet mode: suppresses warning messages.

-o output_file
    Specifies the output file. If not specified, the output is written to standard output.

--version
    Displays the version number of gnatprep.

--help
    Displays a help message with a list of available options.

DESCRIPTION

gnatprep is a preprocessor for Ada source code, similar in function to the C preprocessor (cpp), but designed specifically for Ada syntax and semantics. It allows for conditional compilation based on user-defined symbols and expressions. gnatprep processes Ada source files, including specifications and bodies, recognizing Ada syntax. It expands macros, includes files, and controls compilation based on preprocessor directives such as `#if`, `#ifdef`, `#ifndef`, `#else`, `#elsif`, `#endif`, `#define`, `#undef`, `#include`, and `#line`. It enables developers to create platform-specific or configuration-dependent Ada code within a single source file, making code more portable and adaptable. This is particularly useful when dealing with different operating systems, hardware configurations, or compiler versions. Gnatprep ensures that only the relevant sections of code are compiled, improving efficiency and reducing code size.
It can also be used to generate Ada source code from template files.
The preprocessor directives are only interpreted, if they are placed within --# comments.

CAVEATS

gnatprep only processes preprocessor directives within Ada comments (--#). It may not fully support all features of the C preprocessor, and its behavior is tailored to Ada's syntax and semantics. It's important to ensure correct Ada syntax after preprocessing.

CONDITIONAL COMPILATION

Conditional compilation in Ada via gnatprep allows developers to selectively include or exclude sections of code based on predefined symbols or conditions. This is crucial for managing platform-specific code or enabling/disabling features.
The use of --# comments as delimiters is key to distinguishing gnatprep directives from standard Ada code.

INTEGRATION WITH GNAT

gnatprep is often integrated into the GNAT build process. It can be invoked as a preprocessing step before compilation with the Ada compiler (gnatmake or gprbuild). This allows for a seamless workflow where preprocessing, compilation, and linking are performed automatically.

HISTORY

gnatprep was developed as part of the GNAT (GNU Ada Translator) project to provide a preprocessor specifically tailored for Ada source code. It addresses the need for conditional compilation and macro expansion within Ada, which is not directly supported by the core Ada language. The tool allows for including conditional compilation into Ada and the preprocessor directives are only interpreted, if they are placed within --# comments.

SEE ALSO

cpp(1), gcc(1)

Copied to clipboard