blob: 99128ca7ac5c8a12cf1b5263ef9e99ce5b7f93cc [file] [log] [blame]
Andres Amaya Garcia88121a92018-10-16 22:00:13 +01001#! /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#
Ronald Croneab20552023-08-30 17:36:25 +020010# 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
Andres Amaya Garcia88121a92018-10-16 22:00:13 +010013# compile time configurations available in Mbed TLS (commented or uncommented).
Ronald Croneab20552023-08-30 17:36:25 +020014# This script extracts the configuration macros from the two files and this
Andres Amaya Garcia88121a92018-10-16 22:00:13 +010015# 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#
Manuel Pégourié-Gonnard3a8413d2021-05-14 09:23:57 +020018# Usage: scripts/generate_query_config.pl without arguments, or
Ronald Croneab20552023-08-30 17:36:25 +020019# generate_query_config.pl mbedtls_config_file psa_crypto_config_file template_file output_file
Bence Szépkúti700ee442020-05-26 00:33:31 +020020#
Bence Szépkúti1e148272020-08-07 13:07:28 +020021# Copyright The Mbed TLS Contributors
Dave Rodgman16799db2023-11-02 19:47:20 +000022# SPDX-License-Identifier: Apache-2.0 OR GPL-2.0-or-later
Andres Amaya Garcia88121a92018-10-16 22:00:13 +010023
24use strict;
25
Ronald Croneab20552023-08-30 17:36:25 +020026my ($mbedtls_config_file, $psa_crypto_config_file, $query_config_format_file, $query_config_file);
Tom Cosgroveef83b832022-07-25 11:42:38 +010027
28my $default_mbedtls_config_file = "./include/mbedtls/mbedtls_config.h";
Ronald Cronc7e9e362024-06-10 09:41:49 +020029my $default_psa_crypto_config_file = "./tf-psa-crypto/include/psa/crypto_config.h";
Tom Cosgroveef83b832022-07-25 11:42:38 +010030my $default_query_config_format_file = "./scripts/data_files/query_config.fmt";
31my $default_query_config_file = "./programs/test/query_config.c";
Andres Amaya Garcia88121a92018-10-16 22:00:13 +010032
Manuel Pégourié-Gonnard3a8413d2021-05-14 09:23:57 +020033if( @ARGV ) {
Ronald Crond7a983a2023-09-08 10:53:35 +020034 die "Invalid number of arguments - usage: $0 [MBED_TLS_CONFIG_FILE PSA_CRYPTO_CONFIG_FILE TEMPLATE_FILE OUTPUT_FILE]" if scalar @ARGV != 4;
Ronald Croneab20552023-08-30 17:36:25 +020035 ($mbedtls_config_file, $psa_crypto_config_file, $query_config_format_file, $query_config_file) = @ARGV;
Andres Amaya Garcia88121a92018-10-16 22:00:13 +010036
Tom Cosgroveef83b832022-07-25 11:42:38 +010037 -f $mbedtls_config_file or die "No such file: $mbedtls_config_file";
Ronald Croneab20552023-08-30 17:36:25 +020038 -f $psa_crypto_config_file or die "No such file: $psa_crypto_config_file";
Manuel Pégourié-Gonnard3a8413d2021-05-14 09:23:57 +020039 -f $query_config_format_file or die "No such file: $query_config_format_file";
40} else {
Tom Cosgroveef83b832022-07-25 11:42:38 +010041 $mbedtls_config_file = $default_mbedtls_config_file;
Ronald Croneab20552023-08-30 17:36:25 +020042 $psa_crypto_config_file = $default_psa_crypto_config_file;
Tom Cosgroveef83b832022-07-25 11:42:38 +010043 $query_config_format_file = $default_query_config_format_file;
44 $query_config_file = $default_query_config_file;
Manuel Pégourié-Gonnard3a8413d2021-05-14 09:23:57 +020045
Tom Cosgroveff3c6c12022-07-25 12:19:35 +010046 unless(-f $mbedtls_config_file && -f $query_config_format_file && -f $psa_crypto_config_file) {
Manuel Pégourié-Gonnard3a8413d2021-05-14 09:23:57 +020047 chdir '..' or die;
Tom Cosgroveff3c6c12022-07-25 12:19:35 +010048 -f $mbedtls_config_file && -f $query_config_format_file && -f $psa_crypto_config_file
David Horstmannd64f4b22021-10-20 12:29:47 +010049 or die "No arguments supplied, must be run from project root or a first-level subdirectory\n";
Manuel Pégourié-Gonnard3a8413d2021-05-14 09:23:57 +020050 }
Gilles Peskinec86f20a2021-04-22 00:20:47 +020051}
Gilles Peskine8b006ce2025-07-25 19:51:17 +020052-f 'include/mbedtls/build_info.h'
53 or die "$0: must be run from project root, or from a first-level subdirectory with no arguments\n";
Gilles Peskinec86f20a2021-04-22 00:20:47 +020054
Andres Amaya Garciaef672f02019-01-03 20:16:43 +000055# Excluded macros from the generated query_config.c. For example, macros that
56# have commas or function-like macros cannot be transformed into strings easily
57# using the preprocessor, so they should be excluded or the preprocessor will
58# throw errors.
59my @excluded = qw(
60MBEDTLS_SSL_CIPHERSUITES
61);
62my $excluded_re = join '|', @excluded;
63
Andres Amaya Garcia88121a92018-10-16 22:00:13 +010064# This variable will contain the string to replace in the CHECK_CONFIG of the
65# format file
66my $config_check = "";
Jerry Yu84e63a72021-12-06 13:40:37 +080067my $list_config = "";
Andres Amaya Garcia88121a92018-10-16 22:00:13 +010068
Tom Cosgroveff3c6c12022-07-25 12:19:35 +010069for my $config_file ($mbedtls_config_file, $psa_crypto_config_file) {
Tom Cosgroveef83b832022-07-25 11:42:38 +010070
Tom Cosgroveff3c6c12022-07-25 12:19:35 +010071 next unless defined($config_file); # we might not have been given a PSA crypto config file
Andres Amaya Garcia88121a92018-10-16 22:00:13 +010072
Tom Cosgroveff3c6c12022-07-25 12:19:35 +010073 open(CONFIG_FILE, "<", $config_file) or die "Opening config file '$config_file': $!";
Andres Amaya Garciaef672f02019-01-03 20:16:43 +000074
Tom Cosgroveff3c6c12022-07-25 12:19:35 +010075 while (my $line = <CONFIG_FILE>) {
76 if ($line =~ /^(\/\/)?\s*#\s*define\s+(MBEDTLS_\w+|PSA_WANT_\w+).*/) {
77 my $name = $2;
78
Tom Cosgrove5900c1d2022-07-27 08:55:03 +010079 # Skip over the macro if it is in the excluded list
Tom Cosgroveff3c6c12022-07-25 12:19:35 +010080 next if $name =~ /$excluded_re/;
81
82 $config_check .= <<EOT;
Tom Cosgroveef83b832022-07-25 11:42:38 +010083#if defined($name)
84 if( strcmp( "$name", config ) == 0 )
85 {
86 MACRO_EXPANSION_TO_STR( $name );
87 return( 0 );
88 }
89#endif /* $name */
Jerry Yu84e63a72021-12-06 13:40:37 +080090
Tom Cosgroveef83b832022-07-25 11:42:38 +010091EOT
92
Tom Cosgroveff3c6c12022-07-25 12:19:35 +010093 $list_config .= <<EOT;
Tom Cosgroveef83b832022-07-25 11:42:38 +010094#if defined($name)
95 OUTPUT_MACRO_NAME_VALUE($name);
96#endif /* $name */
97
98EOT
Tom Cosgroveff3c6c12022-07-25 12:19:35 +010099 }
Andres Amaya Garcia88121a92018-10-16 22:00:13 +0100100 }
Andres Amaya Garcia88121a92018-10-16 22:00:13 +0100101
Tom Cosgroveff3c6c12022-07-25 12:19:35 +0100102 close(CONFIG_FILE);
103}
Tom Cosgroveef83b832022-07-25 11:42:38 +0100104
Gilles Peskinec0a562c2025-07-25 17:07:13 +0200105# We need to include all the headers with public APIs in case they
106# define a macro to its default value when that configuration is not
107# set in a header included by build_info.h (crypto_config.h,
108# mbedtls_config.h, *adjust*.h). Some module-specific macros are set
109# in that module's header. For simplicity, include all headers, with
110# some ad hoc knowledge of headers that are included by other headers
111# and should not be included directly. We don't include internal headers
112# because those should not define configurable macros.
113my @header_files = ();
114my @header_roots = qw(
115 include
116 tf-psa-crypto/include
117 tf-psa-crypto/drivers/builtin/include
118 );
119for my $root (@header_roots) {
120 my @paths = glob "$root/*/*.h $root/*/*/*.h";
121 map {s!^\Q$root/!!} @paths;
122 # Exclude some headers that are included by build_info.h and cannot
123 # be included directly.
Gilles Peskine409c6882025-07-26 00:15:21 +0200124 push @header_files, grep {!m[
Gilles Peskine4995d442025-07-26 00:19:32 +0200125 ^psa/crypto_(platform|struct)\.h$ | # have alt versions, included by psa/crypto.h anyway
Gilles Peskine409c6882025-07-26 00:15:21 +0200126 ^mbedtls/platform_time\.h$ | # errors without time.h
127 _config\.h |
128 [/_]adjust[/_]
129 ]x} @paths;
Gilles Peskinec0a562c2025-07-25 17:07:13 +0200130}
131my $include_headers = join('', map {"#include <$_>\n"} @header_files);
132
Andres Amaya Garcia109f8b62018-10-23 19:53:14 +0100133# Read the full format file into a string
Andres Amaya Garcia88121a92018-10-16 22:00:13 +0100134local $/;
Tom Cosgroveef83b832022-07-25 11:42:38 +0100135open(FORMAT_FILE, "<", $query_config_format_file) or die "Opening query config format file '$query_config_format_file': $!";
Andres Amaya Garcia88121a92018-10-16 22:00:13 +0100136my $query_config_format = <FORMAT_FILE>;
137close(FORMAT_FILE);
138
139# Replace the body of the query_config() function with the code we just wrote
Gilles Peskinec0a562c2025-07-25 17:07:13 +0200140$query_config_format =~ s/INCLUDE_HEADERS/$include_headers/g;
Andres Amaya Garcia88121a92018-10-16 22:00:13 +0100141$query_config_format =~ s/CHECK_CONFIG/$config_check/g;
Jerry Yu84e63a72021-12-06 13:40:37 +0800142$query_config_format =~ s/LIST_CONFIG/$list_config/g;
Andres Amaya Garcia88121a92018-10-16 22:00:13 +0100143
144# Rewrite the query_config.c file
Tom Cosgroveef83b832022-07-25 11:42:38 +0100145open(QUERY_CONFIG_FILE, ">", $query_config_file) or die "Opening destination file '$query_config_file': $!";
Andres Amaya Garcia88121a92018-10-16 22:00:13 +0100146print QUERY_CONFIG_FILE $query_config_format;
147close(QUERY_CONFIG_FILE);