Fix for x86_64 build fail
[platform/upstream/connectedhomeip.git] / third_party / mbedtls / repo / scripts / generate_query_config.pl
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 # Excluded macros from the generated query_config.c. For example, macros that
27 # have commas or function-like macros cannot be transformed into strings easily
28 # using the preprocessor, so they should be excluded or the preprocessor will
29 # throw errors.
30 my @excluded = qw(
31 MBEDTLS_SSL_CIPHERSUITES
32 MBEDTLS_PARAM_FAILED
33 );
34 my $excluded_re = join '|', @excluded;
35
36 open(CONFIG_FILE, "$config_file") or die "Opening config file '$config_file': $!";
37
38 # This variable will contain the string to replace in the CHECK_CONFIG of the
39 # format file
40 my $config_check = "";
41
42 while (my $line = <CONFIG_FILE>) {
43     if ($line =~ /^(\/\/)?\s*#\s*define\s+(MBEDTLS_\w+).*/) {
44         my $name = $2;
45
46         # Skip over the macro that prevents multiple inclusion
47         next if "MBEDTLS_CONFIG_H" eq $name;
48
49         # Skip over the macro if it is in the ecluded list
50         next if $name =~ /$excluded_re/;
51
52         $config_check .= "#if defined($name)\n";
53         $config_check .= "    if( strcmp( \"$name\", config ) == 0 )\n";
54         $config_check .= "    {\n";
55         $config_check .= "        MACRO_EXPANSION_TO_STR( $name );\n";
56         $config_check .= "        return( 0 );\n";
57         $config_check .= "    }\n";
58         $config_check .= "#endif /* $name */\n";
59         $config_check .= "\n";
60     }
61 }
62
63 # Read the full format file into a string
64 local $/;
65 open(FORMAT_FILE, "$query_config_format_file") or die "Opening query config format file '$query_config_format_file': $!";
66 my $query_config_format = <FORMAT_FILE>;
67 close(FORMAT_FILE);
68
69 # Replace the body of the query_config() function with the code we just wrote
70 $query_config_format =~ s/CHECK_CONFIG/$config_check/g;
71
72 # Rewrite the query_config.c file
73 open(QUERY_CONFIG_FILE, ">$query_config_file") or die "Opening destination file '$query_config_file': $!";
74 print QUERY_CONFIG_FILE $query_config_format;
75 close(QUERY_CONFIG_FILE);