gnatprep
Preprocesses Ada code for conditional compilation
TLDR
Use symbol definitions from a file
Specify symbol values in the command-line
SYNOPSIS
gnatprep [options] infile prepfile outfile
PARAMETERS
-v, --verbose
Display detailed processing information.
-q, --quiet
Suppress non-error messages.
-C, --comments
Preserve preprocessor comments in output.
-H, --html
Generate HTML file showing preprocessing flow.
--check
Validate syntax without producing output file.
-Idir
Add dir to search path for #include files.
-f, --force
Overwrite existing output file if present.
-S, --split
Split output into multiple files based on conditions.
--version
Print version information and exit.
--help
Show usage summary and exit.
DESCRIPTION
gnatprep is a specialized preprocessor for Ada source files in the GNAT compiler suite. It enables conditional compilation using directives like #if, #elif, #else, #end if, #define, #undef, and #include, similar to C's cpp but optimized for Ada's syntax. Directives must appear in column 1, starting with #.
Typical workflow: provide an input Ada file (infile), a definition file (prepfile) with macro definitions (e.g., -D OS=LINUX or boolean conditions), and an output file (outfile). gnatprep expands macros, evaluates conditions, and selectively includes/excludes code blocks, producing clean Ada source for compilation.
Use cases include platform-specific code (Windows/Linux), feature flags, or multi-target builds without code duplication. It supports nested conditions, logical operators (and, or, not), arithmetic comparisons, and file inclusion with search paths. Comments are stripped by default but can be preserved. HTML output aids debugging preprocessor flow.
CAVEATS
Directives must start in column 1 with #; ignores Ada's normal comment syntax. Specific to GNAT/Ada; not compatible with standard C preprocessors. No support for macro arguments or stringification.
EXAMPLE USAGE
prepfile content:
#define TARGET_LINUX
#if TARGET_LINUX
Linux-specific code
#else
Other platform code
#end if;
Command: gnatprep main.adb prepfile main-prep.adb
HISTORY
Originated in the 1990s as part of GNAT project at New York University (NYU) and US Air Force; integrated into GCC Ada compiler. Maintained by AdaCore since 2004, with ongoing enhancements for modern Ada standards.


