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/mbedtls_config.h. The idea is that the mbedtls_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 mbedtls_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, or
18# generate_query_config.pl mbedtls_config_file template_file output_file [psa_crypto_config_file]
19#
20# Copyright The Mbed TLS Contributors
21# SPDX-License-Identifier: Apache-2.0
22#
23# Licensed under the Apache License, Version 2.0 (the "License"); you may
24# not use this file except in compliance with the License.
25# You may obtain a copy of the License at
26#
27# http://www.apache.org/licenses/LICENSE-2.0
28#
29# Unless required by applicable law or agreed to in writing, software
30# distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
31# WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
32# See the License for the specific language governing permissions and
33# limitations under the License.
34
35use strict;
36
37my ($mbedtls_config_file, $query_config_format_file, $query_config_file, $psa_crypto_config_file);
38
39my $default_mbedtls_config_file = "./include/mbedtls/mbedtls_config.h";
40my $default_query_config_format_file = "./scripts/data_files/query_config.fmt";
41my $default_query_config_file = "./programs/test/query_config.c";
42my $default_psa_crypto_config_file = "./include/psa/crypto_config.h";
43
44if( @ARGV ) {
45    die "Invalid number of arguments - usage: $0 [CONFIG_FILE TEMPLATE_FILE OUTPUT_FILE]" if scalar @ARGV != 3;
46    ($mbedtls_config_file, $query_config_format_file, $query_config_file) = @ARGV;
47
48    -f $mbedtls_config_file or die "No such file: $mbedtls_config_file";
49    -f $query_config_format_file or die "No such file: $query_config_format_file";
50    if (defined($psa_crypto_config_file) && length($psa_crypto_config_file)) {
51        -f $psa_crypto_config_file or die "No such file: $psa_crypto_config_file";
52    } else {
53        $psa_crypto_config_file = (-f $default_psa_crypto_config_file) ? $default_psa_crypto_config_file : undef;
54    }
55} else {
56    $mbedtls_config_file = $default_mbedtls_config_file;
57    $query_config_format_file = $default_query_config_format_file;
58    $query_config_file = $default_query_config_file;
59    $psa_crypto_config_file = $default_psa_crypto_config_file;
60
61    unless(-f $mbedtls_config_file && -f $query_config_format_file  && -f $psa_crypto_config_file) {
62        chdir '..' or die;
63        -f $mbedtls_config_file && -f $query_config_format_file && -f $psa_crypto_config_file
64          or die "No arguments supplied, must be run from project root or a first-level subdirectory\n";
65    }
66}
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
74);
75my $excluded_re = join '|', @excluded;
76
77# This variable will contain the string to replace in the CHECK_CONFIG of the
78# format file
79my $config_check = "";
80my $list_config = "";
81
82for my $config_file ($mbedtls_config_file, $psa_crypto_config_file) {
83
84    next unless defined($config_file);  # we might not have been given a PSA crypto config file
85
86    open(CONFIG_FILE, "<", $config_file) or die "Opening config file '$config_file': $!";
87
88    while (my $line = <CONFIG_FILE>) {
89        if ($line =~ /^(\/\/)?\s*#\s*define\s+(MBEDTLS_\w+|PSA_WANT_\w+).*/) {
90            my $name = $2;
91
92            # Skip over the macro if it is in the excluded list
93            next if $name =~ /$excluded_re/;
94
95            $config_check .= <<EOT;
96#if defined($name)
97    if( strcmp( "$name", config ) == 0 )
98    {
99        MACRO_EXPANSION_TO_STR( $name );
100        return( 0 );
101    }
102#endif /* $name */
103
104EOT
105
106            $list_config .= <<EOT;
107#if defined($name)
108    OUTPUT_MACRO_NAME_VALUE($name);
109#endif /* $name */
110
111EOT
112        }
113    }
114
115    close(CONFIG_FILE);
116}
117
118# Read the full format file into a string
119local $/;
120open(FORMAT_FILE, "<", $query_config_format_file) or die "Opening query config format file '$query_config_format_file': $!";
121my $query_config_format = <FORMAT_FILE>;
122close(FORMAT_FILE);
123
124# Replace the body of the query_config() function with the code we just wrote
125$query_config_format =~ s/CHECK_CONFIG/$config_check/g;
126$query_config_format =~ s/LIST_CONFIG/$list_config/g;
127
128# Rewrite the query_config.c file
129open(QUERY_CONFIG_FILE, ">", $query_config_file) or die "Opening destination file '$query_config_file': $!";
130print QUERY_CONFIG_FILE $query_config_format;
131close(QUERY_CONFIG_FILE);
132