Andres Amaya Garcia | f1a5b26 | 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 |
| 18 | |
| 19 | use strict; |
| 20 | |
| 21 | my $config_file = "./include/mbedtls/config.h"; |
| 22 | |
| 23 | my $query_config_format_file = "./scripts/data_files/query_config.fmt"; |
| 24 | my $query_config_file = "./programs/ssl/query_config.c"; |
| 25 | |
| 26 | open(CONFIG_FILE, "$config_file") or die "Opening config file '$config_file': $!"; |
| 27 | |
| 28 | # This variable will contain the string to replace in the CHECK_CONFIG of the |
| 29 | # format file |
| 30 | my $config_check = ""; |
| 31 | |
| 32 | while (my $line = <CONFIG_FILE>) { |
| 33 | if ($line =~ /^(\/\/)?\s*#\s*define\s+(MBEDTLS_\w+).*/) { |
| 34 | my $name = $2; |
| 35 | |
| 36 | # Skip over the macro that prevents multiple inclusion |
| 37 | next if "MBEDTLS_CONFIG_H" eq $name; |
| 38 | |
| 39 | $config_check .= "#if defined($name)\n"; |
| 40 | $config_check .= " if( strcmp( \"$name\", config ) == 0 )\n"; |
| 41 | $config_check .= " {\n"; |
Andres Amaya Garcia | c500ad8 | 2018-12-05 10:47:31 +0000 | [diff] [blame^] | 42 | $config_check .= " MACRO_EXPANSION_TO_STR( $name );\n"; |
Andres Amaya Garcia | f1a5b26 | 2018-10-16 22:00:13 +0100 | [diff] [blame] | 43 | $config_check .= " return( 0 );\n"; |
| 44 | $config_check .= " }\n"; |
| 45 | $config_check .= "#endif /* $name */\n"; |
| 46 | $config_check .= "\n"; |
| 47 | } |
| 48 | } |
| 49 | |
Andres Amaya Garcia | e8b11dd | 2018-10-23 19:53:14 +0100 | [diff] [blame] | 50 | # Read the full format file into a string |
Andres Amaya Garcia | f1a5b26 | 2018-10-16 22:00:13 +0100 | [diff] [blame] | 51 | local $/; |
| 52 | open(FORMAT_FILE, "$query_config_format_file") or die "Opening query config format file '$query_config_format_file': $!"; |
| 53 | my $query_config_format = <FORMAT_FILE>; |
| 54 | close(FORMAT_FILE); |
| 55 | |
| 56 | # Replace the body of the query_config() function with the code we just wrote |
| 57 | $query_config_format =~ s/CHECK_CONFIG/$config_check/g; |
| 58 | |
| 59 | # Rewrite the query_config.c file |
| 60 | open(QUERY_CONFIG_FILE, ">$query_config_file") or die "Opening destination file '$query_config_file': $!"; |
| 61 | print QUERY_CONFIG_FILE $query_config_format; |
| 62 | close(QUERY_CONFIG_FILE); |