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 |
Bence Szépkúti | 468a76f | 2020-05-26 00:33:31 +0200 | [diff] [blame] | 18 | # |
Bence Szépkúti | a2947ac | 2020-08-19 16:37:36 +0200 | [diff] [blame^] | 19 | # Copyright The Mbed TLS Contributors |
Bence Szépkúti | f744bd7 | 2020-06-05 13:02:18 +0200 | [diff] [blame] | 20 | # SPDX-License-Identifier: Apache-2.0 OR GPL-2.0-or-later |
| 21 | # |
| 22 | # This file is provided under the Apache License 2.0, or the |
| 23 | # GNU General Public License v2.0 or later. |
| 24 | # |
| 25 | # ********** |
| 26 | # Apache License 2.0: |
Bence Szépkúti | 51b41d5 | 2020-05-26 01:54:15 +0200 | [diff] [blame] | 27 | # |
| 28 | # Licensed under the Apache License, Version 2.0 (the "License"); you may |
| 29 | # not use this file except in compliance with the License. |
| 30 | # You may obtain a copy of the License at |
| 31 | # |
| 32 | # http://www.apache.org/licenses/LICENSE-2.0 |
| 33 | # |
| 34 | # Unless required by applicable law or agreed to in writing, software |
| 35 | # distributed under the License is distributed on an "AS IS" BASIS, WITHOUT |
| 36 | # WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 37 | # See the License for the specific language governing permissions and |
| 38 | # limitations under the License. |
Bence Szépkúti | 468a76f | 2020-05-26 00:33:31 +0200 | [diff] [blame] | 39 | # |
Bence Szépkúti | f744bd7 | 2020-06-05 13:02:18 +0200 | [diff] [blame] | 40 | # ********** |
| 41 | # |
| 42 | # ********** |
| 43 | # GNU General Public License v2.0 or later: |
| 44 | # |
| 45 | # This program is free software; you can redistribute it and/or modify |
| 46 | # it under the terms of the GNU General Public License as published by |
| 47 | # the Free Software Foundation; either version 2 of the License, or |
| 48 | # (at your option) any later version. |
| 49 | # |
| 50 | # This program is distributed in the hope that it will be useful, |
| 51 | # but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 52 | # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 53 | # GNU General Public License for more details. |
| 54 | # |
| 55 | # You should have received a copy of the GNU General Public License along |
| 56 | # with this program; if not, write to the Free Software Foundation, Inc., |
| 57 | # 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. |
| 58 | # |
| 59 | # ********** |
Andres Amaya Garcia | f1a5b26 | 2018-10-16 22:00:13 +0100 | [diff] [blame] | 60 | |
| 61 | use strict; |
| 62 | |
| 63 | my $config_file = "./include/mbedtls/config.h"; |
| 64 | |
| 65 | my $query_config_format_file = "./scripts/data_files/query_config.fmt"; |
| 66 | my $query_config_file = "./programs/ssl/query_config.c"; |
| 67 | |
Andres Amaya Garcia | 4eb040a | 2019-01-03 20:16:43 +0000 | [diff] [blame] | 68 | # Excluded macros from the generated query_config.c. For example, macros that |
| 69 | # have commas or function-like macros cannot be transformed into strings easily |
| 70 | # using the preprocessor, so they should be excluded or the preprocessor will |
| 71 | # throw errors. |
| 72 | my @excluded = qw( |
| 73 | MBEDTLS_SSL_CIPHERSUITES |
Andres Amaya Garcia | fe52d2a | 2019-01-08 20:02:48 +0000 | [diff] [blame] | 74 | MBEDTLS_PARAM_FAILED |
Andres Amaya Garcia | 4eb040a | 2019-01-03 20:16:43 +0000 | [diff] [blame] | 75 | ); |
| 76 | my $excluded_re = join '|', @excluded; |
| 77 | |
Andres Amaya Garcia | f1a5b26 | 2018-10-16 22:00:13 +0100 | [diff] [blame] | 78 | open(CONFIG_FILE, "$config_file") or die "Opening config file '$config_file': $!"; |
| 79 | |
| 80 | # This variable will contain the string to replace in the CHECK_CONFIG of the |
| 81 | # format file |
| 82 | my $config_check = ""; |
| 83 | |
| 84 | while (my $line = <CONFIG_FILE>) { |
| 85 | if ($line =~ /^(\/\/)?\s*#\s*define\s+(MBEDTLS_\w+).*/) { |
| 86 | my $name = $2; |
| 87 | |
| 88 | # Skip over the macro that prevents multiple inclusion |
| 89 | next if "MBEDTLS_CONFIG_H" eq $name; |
| 90 | |
Andres Amaya Garcia | 4eb040a | 2019-01-03 20:16:43 +0000 | [diff] [blame] | 91 | # Skip over the macro if it is in the ecluded list |
| 92 | next if $name =~ /$excluded_re/; |
| 93 | |
Andres Amaya Garcia | f1a5b26 | 2018-10-16 22:00:13 +0100 | [diff] [blame] | 94 | $config_check .= "#if defined($name)\n"; |
| 95 | $config_check .= " if( strcmp( \"$name\", config ) == 0 )\n"; |
| 96 | $config_check .= " {\n"; |
Andres Amaya Garcia | c500ad8 | 2018-12-05 10:47:31 +0000 | [diff] [blame] | 97 | $config_check .= " MACRO_EXPANSION_TO_STR( $name );\n"; |
Andres Amaya Garcia | f1a5b26 | 2018-10-16 22:00:13 +0100 | [diff] [blame] | 98 | $config_check .= " return( 0 );\n"; |
| 99 | $config_check .= " }\n"; |
| 100 | $config_check .= "#endif /* $name */\n"; |
| 101 | $config_check .= "\n"; |
| 102 | } |
| 103 | } |
| 104 | |
Andres Amaya Garcia | e8b11dd | 2018-10-23 19:53:14 +0100 | [diff] [blame] | 105 | # Read the full format file into a string |
Andres Amaya Garcia | f1a5b26 | 2018-10-16 22:00:13 +0100 | [diff] [blame] | 106 | local $/; |
| 107 | open(FORMAT_FILE, "$query_config_format_file") or die "Opening query config format file '$query_config_format_file': $!"; |
| 108 | my $query_config_format = <FORMAT_FILE>; |
| 109 | close(FORMAT_FILE); |
| 110 | |
| 111 | # Replace the body of the query_config() function with the code we just wrote |
| 112 | $query_config_format =~ s/CHECK_CONFIG/$config_check/g; |
| 113 | |
| 114 | # Rewrite the query_config.c file |
| 115 | open(QUERY_CONFIG_FILE, ">$query_config_file") or die "Opening destination file '$query_config_file': $!"; |
| 116 | print QUERY_CONFIG_FILE $query_config_format; |
| 117 | close(QUERY_CONFIG_FILE); |