Using the gnatprep Command (with examples)
Use Case 1: Use symbol definitions from a file
Code:
gnatprep source_file target_file definitions_file
Motivation:
The gnatprep
command allows us to use symbol definitions from a file when preprocessing Ada source code files. This can be useful when we want to have different versions of the source code depending on the specific symbols defined.
Explanation:
source_file
: The path to the source file that needs to be preprocessed.target_file
: The path to the output file where the preprocessed content will be saved.definitions_file
: The path to the file that contains the symbol definitions. This file should have one definition per line, in the format “name=value”, where “name” is the symbol name and “value” is its corresponding value.
Example Output:
Assuming we have a source file called my_code.adb
with the following content:
with Ada.Text_IO;
procedure My_Code is
begin
-- This is a preprocessed statement
-- ${EXAMPLE_MESSAGE}
Ada.Text_IO.Put("Hello, World!");
end My_Code;
And we have a definitions file called definitions.txt
with the following content:
EXAMPLE_MESSAGE="This is a preprocessed message"
Running the following command:
gnatprep my_code.adb preprocessed_code.adb definitions.txt
Will preprocess the my_code.adb
file using the symbol definition from definitions.txt
file and generate a new file called preprocessed_code.adb
with the following content:
with Ada.Text_IO;
procedure My_Code is
begin
-- This is a preprocessed statement
-- "This is a preprocessed message"
Ada.Text_IO.Put("Hello, World!");
end My_Code;
Use Case 2: Specify symbol values in the command-line
Code:
gnatprep -Dname=value source_file target_file
Motivation:
The second use case of the gnatprep
command allows us to specify symbol values directly in the command-line. This can be useful when we want to override the default symbol definitions or provide different values for different preprocessing scenarios.
Explanation:
-Dname=value
: This option allows us to specify symbol names and their corresponding values directly in the command-line. Multiple symbol definitions can be provided by separating them with spaces.
Example Output:
Using the same my_code.adb
file as in the previous example, running the following command:
gnatprep -DEXAMPLE_MESSAGE="This is a preprocessed message" my_code.adb preprocessed_code.adb
Will preprocess the my_code.adb
file using the symbol definition specified in the command-line and generate a new file called preprocessed_code.adb
with the following content:
with Ada.Text_IO;
procedure My_Code is
begin
-- This is a preprocessed statement
-- "This is a preprocessed message"
Ada.Text_IO.Put("Hello, World!");
end My_Code;
Note that in this case, the symbol definition from the definitions.txt
file is not used because we provided a directly in the command-line.
Conclusion
Using the gnatprep
command, we can preprocess Ada source code files by utilizing symbol definitions from a file or specifying symbol values directly in the command-line. These features enable us to have flexible code generation based on different symbol definitions, making our development process more efficient and adaptable.