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# Copyright The Mbed TLS Contributors 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: 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. 39# 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# ********** 60 61use strict; 62 63my $config_file = "./include/mbedtls/config.h"; 64 65my $query_config_format_file = "./scripts/data_files/query_config.fmt"; 66my $query_config_file = "./programs/ssl/query_config.c"; 67 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. 72my @excluded = qw( 73MBEDTLS_SSL_CIPHERSUITES 74MBEDTLS_PARAM_FAILED 75); 76my $excluded_re = join '|', @excluded; 77 78open(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 82my $config_check = ""; 83 84while (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 91 # Skip over the macro if it is in the ecluded list 92 next if $name =~ /$excluded_re/; 93 94 $config_check .= "#if defined($name)\n"; 95 $config_check .= " if( strcmp( \"$name\", config ) == 0 )\n"; 96 $config_check .= " {\n"; 97 $config_check .= " MACRO_EXPANSION_TO_STR( $name );\n"; 98 $config_check .= " return( 0 );\n"; 99 $config_check .= " }\n"; 100 $config_check .= "#endif /* $name */\n"; 101 $config_check .= "\n"; 102 } 103} 104 105# Read the full format file into a string 106local $/; 107open(FORMAT_FILE, "$query_config_format_file") or die "Opening query config format file '$query_config_format_file': $!"; 108my $query_config_format = <FORMAT_FILE>; 109close(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 115open(QUERY_CONFIG_FILE, ">$query_config_file") or die "Opening destination file '$query_config_file': $!"; 116print QUERY_CONFIG_FILE $query_config_format; 117close(QUERY_CONFIG_FILE); 118