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