Andres Amaya Garcia | 88121a9 | 2018-10-16 22:00:13 +0100 | [diff] [blame] | 1 | #! /usr/bin/env perl |
| 2 | |
| 3 | # Generate query_config.c |
| 4 | # |
| 5 | # The file query_config.c contains a C function that can be used to check if |
| 6 | # a configuration macro is defined and to retrieve its expansion in string |
| 7 | # form (if any). This facilitates querying the compile time configuration of |
| 8 | # the library, for example, for testing. |
| 9 | # |
| 10 | # The query_config.c is generated from the current configuration at |
| 11 | # include/mbedtls/config.h. The idea is that the config.h contains ALL the |
| 12 | # compile time configurations available in Mbed TLS (commented or uncommented). |
| 13 | # This script extracts the configuration macros from the config.h and this |
| 14 | # information is used to automatically generate the body of the query_config() |
| 15 | # function by using the template in scripts/data_files/query_config.fmt. |
| 16 | # |
| 17 | # Usage: ./scripts/generate_query_config.pl without arguments |
Bence Szépkúti | 700ee44 | 2020-05-26 00:33:31 +0200 | [diff] [blame] | 18 | # |
Bence Szépkúti | 1e14827 | 2020-08-07 13:07:28 +0200 | [diff] [blame] | 19 | # Copyright The Mbed TLS Contributors |
Dave Rodgman | 7ff7965 | 2023-11-03 12:04:52 +0000 | [diff] [blame] | 20 | # SPDX-License-Identifier: Apache-2.0 OR GPL-2.0-or-later |
Andres Amaya Garcia | 88121a9 | 2018-10-16 22:00:13 +0100 | [diff] [blame] | 21 | |
| 22 | use strict; |
| 23 | |
| 24 | my $config_file = "./include/mbedtls/config.h"; |
| 25 | |
| 26 | my $query_config_format_file = "./scripts/data_files/query_config.fmt"; |
Jaeden Amero | 7cb47de | 2019-02-28 11:37:23 +0000 | [diff] [blame] | 27 | my $query_config_file = "./programs/test/query_config.c"; |
Andres Amaya Garcia | 88121a9 | 2018-10-16 22:00:13 +0100 | [diff] [blame] | 28 | |
Andres Amaya Garcia | ef672f0 | 2019-01-03 20:16:43 +0000 | [diff] [blame] | 29 | # Excluded macros from the generated query_config.c. For example, macros that |
| 30 | # have commas or function-like macros cannot be transformed into strings easily |
| 31 | # using the preprocessor, so they should be excluded or the preprocessor will |
| 32 | # throw errors. |
| 33 | my @excluded = qw( |
| 34 | MBEDTLS_SSL_CIPHERSUITES |
Andres Amaya Garcia | 8645f73 | 2019-01-08 20:02:48 +0000 | [diff] [blame] | 35 | MBEDTLS_PARAM_FAILED |
Andres Amaya Garcia | ef672f0 | 2019-01-03 20:16:43 +0000 | [diff] [blame] | 36 | ); |
| 37 | my $excluded_re = join '|', @excluded; |
| 38 | |
Andres Amaya Garcia | 88121a9 | 2018-10-16 22:00:13 +0100 | [diff] [blame] | 39 | open(CONFIG_FILE, "$config_file") or die "Opening config file '$config_file': $!"; |
| 40 | |
| 41 | # This variable will contain the string to replace in the CHECK_CONFIG of the |
| 42 | # format file |
| 43 | my $config_check = ""; |
Jerry Yu | 0abd677 | 2021-12-06 13:40:37 +0800 | [diff] [blame] | 44 | my $list_config = ""; |
Andres Amaya Garcia | 88121a9 | 2018-10-16 22:00:13 +0100 | [diff] [blame] | 45 | |
| 46 | while (my $line = <CONFIG_FILE>) { |
| 47 | if ($line =~ /^(\/\/)?\s*#\s*define\s+(MBEDTLS_\w+).*/) { |
| 48 | my $name = $2; |
| 49 | |
| 50 | # Skip over the macro that prevents multiple inclusion |
| 51 | next if "MBEDTLS_CONFIG_H" eq $name; |
| 52 | |
Andres Amaya Garcia | ef672f0 | 2019-01-03 20:16:43 +0000 | [diff] [blame] | 53 | # Skip over the macro if it is in the ecluded list |
| 54 | next if $name =~ /$excluded_re/; |
| 55 | |
Andres Amaya Garcia | 88121a9 | 2018-10-16 22:00:13 +0100 | [diff] [blame] | 56 | $config_check .= "#if defined($name)\n"; |
| 57 | $config_check .= " if( strcmp( \"$name\", config ) == 0 )\n"; |
| 58 | $config_check .= " {\n"; |
Andres Amaya Garcia | 27b3372 | 2018-12-05 10:47:31 +0000 | [diff] [blame] | 59 | $config_check .= " MACRO_EXPANSION_TO_STR( $name );\n"; |
Andres Amaya Garcia | 88121a9 | 2018-10-16 22:00:13 +0100 | [diff] [blame] | 60 | $config_check .= " return( 0 );\n"; |
| 61 | $config_check .= " }\n"; |
| 62 | $config_check .= "#endif /* $name */\n"; |
| 63 | $config_check .= "\n"; |
Jerry Yu | 0abd677 | 2021-12-06 13:40:37 +0800 | [diff] [blame] | 64 | |
| 65 | $list_config .= "#if defined($name)\n"; |
| 66 | $list_config .= " OUTPUT_MACRO_NAME_VALUE($name);\n"; |
| 67 | $list_config .= "#endif /* $name */\n"; |
| 68 | $list_config .= "\n"; |
Andres Amaya Garcia | 88121a9 | 2018-10-16 22:00:13 +0100 | [diff] [blame] | 69 | } |
| 70 | } |
| 71 | |
Andres Amaya Garcia | 109f8b6 | 2018-10-23 19:53:14 +0100 | [diff] [blame] | 72 | # Read the full format file into a string |
Andres Amaya Garcia | 88121a9 | 2018-10-16 22:00:13 +0100 | [diff] [blame] | 73 | local $/; |
| 74 | open(FORMAT_FILE, "$query_config_format_file") or die "Opening query config format file '$query_config_format_file': $!"; |
| 75 | my $query_config_format = <FORMAT_FILE>; |
| 76 | close(FORMAT_FILE); |
| 77 | |
| 78 | # Replace the body of the query_config() function with the code we just wrote |
| 79 | $query_config_format =~ s/CHECK_CONFIG/$config_check/g; |
Jerry Yu | 0abd677 | 2021-12-06 13:40:37 +0800 | [diff] [blame] | 80 | $query_config_format =~ s/LIST_CONFIG/$list_config/g; |
Andres Amaya Garcia | 88121a9 | 2018-10-16 22:00:13 +0100 | [diff] [blame] | 81 | |
| 82 | # Rewrite the query_config.c file |
| 83 | open(QUERY_CONFIG_FILE, ">$query_config_file") or die "Opening destination file '$query_config_file': $!"; |
| 84 | print QUERY_CONFIG_FILE $query_config_format; |
| 85 | close(QUERY_CONFIG_FILE); |