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 default configuration files 11# include/mbedtls/mbedtls_config.h and include/psa/crypto_config.h. 12# The idea is that mbedtls_config.h and crypto_config.h contain ALL the 13# compile time configurations available in Mbed TLS (commented or uncommented). 14# This script extracts the configuration macros from the two files and this 15# information is used to automatically generate the body of the query_config() 16# function by using the template in scripts/data_files/query_config.fmt. 17# 18# Usage: scripts/generate_query_config.pl without arguments, or 19# generate_query_config.pl mbedtls_config_file psa_crypto_config_file template_file output_file 20# 21# Copyright The Mbed TLS Contributors 22# SPDX-License-Identifier: Apache-2.0 OR GPL-2.0-or-later 23 24use strict; 25 26my ($mbedtls_config_file, $psa_crypto_config_file, $query_config_format_file, $query_config_file); 27 28my $default_mbedtls_config_file = "./include/mbedtls/mbedtls_config.h"; 29my $default_psa_crypto_config_file = "./include/psa/crypto_config.h"; 30my $default_query_config_format_file = "./scripts/data_files/query_config.fmt"; 31my $default_query_config_file = "./programs/test/query_config.c"; 32 33if( @ARGV ) { 34 die "Invalid number of arguments - usage: $0 [MBED_TLS_CONFIG_FILE PSA_CRYPTO_CONFIG_FILE TEMPLATE_FILE OUTPUT_FILE]" if scalar @ARGV != 4; 35 ($mbedtls_config_file, $psa_crypto_config_file, $query_config_format_file, $query_config_file) = @ARGV; 36 37 -f $mbedtls_config_file or die "No such file: $mbedtls_config_file"; 38 -f $psa_crypto_config_file or die "No such file: $psa_crypto_config_file"; 39 -f $query_config_format_file or die "No such file: $query_config_format_file"; 40} else { 41 $mbedtls_config_file = $default_mbedtls_config_file; 42 $psa_crypto_config_file = $default_psa_crypto_config_file; 43 $query_config_format_file = $default_query_config_format_file; 44 $query_config_file = $default_query_config_file; 45 46 unless(-f $mbedtls_config_file && -f $query_config_format_file && -f $psa_crypto_config_file) { 47 chdir '..' or die; 48 -f $mbedtls_config_file && -f $query_config_format_file && -f $psa_crypto_config_file 49 or die "No arguments supplied, must be run from project root or a first-level subdirectory\n"; 50 } 51} 52 53# Excluded macros from the generated query_config.c. For example, macros that 54# have commas or function-like macros cannot be transformed into strings easily 55# using the preprocessor, so they should be excluded or the preprocessor will 56# throw errors. 57my @excluded = qw( 58MBEDTLS_SSL_CIPHERSUITES 59); 60my $excluded_re = join '|', @excluded; 61 62# This variable will contain the string to replace in the CHECK_CONFIG of the 63# format file 64my $config_check = ""; 65my $list_config = ""; 66 67for my $config_file ($mbedtls_config_file, $psa_crypto_config_file) { 68 69 next unless defined($config_file); # we might not have been given a PSA crypto config file 70 71 open(CONFIG_FILE, "<", $config_file) or die "Opening config file '$config_file': $!"; 72 73 while (my $line = <CONFIG_FILE>) { 74 if ($line =~ /^(\/\/)?\s*#\s*define\s+(MBEDTLS_\w+|PSA_WANT_\w+).*/) { 75 my $name = $2; 76 77 # Skip over the macro if it is in the excluded list 78 next if $name =~ /$excluded_re/; 79 80 $config_check .= <<EOT; 81#if defined($name) 82 if( strcmp( "$name", config ) == 0 ) 83 { 84 MACRO_EXPANSION_TO_STR( $name ); 85 return( 0 ); 86 } 87#endif /* $name */ 88 89EOT 90 91 $list_config .= <<EOT; 92#if defined($name) 93 OUTPUT_MACRO_NAME_VALUE($name); 94#endif /* $name */ 95 96EOT 97 } 98 } 99 100 close(CONFIG_FILE); 101} 102 103# Read the full format file into a string 104local $/; 105open(FORMAT_FILE, "<", $query_config_format_file) or die "Opening query config format file '$query_config_format_file': $!"; 106my $query_config_format = <FORMAT_FILE>; 107close(FORMAT_FILE); 108 109# Replace the body of the query_config() function with the code we just wrote 110$query_config_format =~ s/CHECK_CONFIG/$config_check/g; 111$query_config_format =~ s/LIST_CONFIG/$list_config/g; 112 113# Rewrite the query_config.c file 114open(QUERY_CONFIG_FILE, ">", $query_config_file) or die "Opening destination file '$query_config_file': $!"; 115print QUERY_CONFIG_FILE $query_config_format; 116close(QUERY_CONFIG_FILE); 117