blob: 8c8c1880e2d2b66b1366d0b5be23fb2481a8f986 [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#
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
Bence Szépkúti700ee442020-05-26 00:33:31 +020018#
Bence Szépkúti1e148272020-08-07 13:07:28 +020019# Copyright The Mbed TLS Contributors
Bence Szépkútic7da1fe2020-05-26 01:54:15 +020020# SPDX-License-Identifier: Apache-2.0
21#
22# Licensed under the Apache License, Version 2.0 (the "License"); you may
23# not use this file except in compliance with the License.
24# You may obtain a copy of the License at
25#
26# http://www.apache.org/licenses/LICENSE-2.0
27#
28# Unless required by applicable law or agreed to in writing, software
29# distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
30# WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
31# See the License for the specific language governing permissions and
32# limitations under the License.
Andres Amaya Garcia88121a92018-10-16 22:00:13 +010033
34use strict;
35
36my $config_file = "./include/mbedtls/config.h";
37
38my $query_config_format_file = "./scripts/data_files/query_config.fmt";
Jaeden Amero7cb47de2019-02-28 11:37:23 +000039my $query_config_file = "./programs/test/query_config.c";
Andres Amaya Garcia88121a92018-10-16 22:00:13 +010040
Gilles Peskinec86f20a2021-04-22 00:20:47 +020041unless( -f $config_file && -f $query_config_format_file ) {
42 chdir '..' or die;
43 -f $config_file && -f $query_config_format_file
44 or die "Without arguments, must be run from root or a subdirectory\n";
45}
46
Andres Amaya Garciaef672f02019-01-03 20:16:43 +000047# Excluded macros from the generated query_config.c. For example, macros that
48# have commas or function-like macros cannot be transformed into strings easily
49# using the preprocessor, so they should be excluded or the preprocessor will
50# throw errors.
51my @excluded = qw(
52MBEDTLS_SSL_CIPHERSUITES
Andres Amaya Garcia8645f732019-01-08 20:02:48 +000053MBEDTLS_PARAM_FAILED
Andres Amaya Garciaef672f02019-01-03 20:16:43 +000054);
55my $excluded_re = join '|', @excluded;
56
Andres Amaya Garcia88121a92018-10-16 22:00:13 +010057open(CONFIG_FILE, "$config_file") or die "Opening config file '$config_file': $!";
58
59# This variable will contain the string to replace in the CHECK_CONFIG of the
60# format file
61my $config_check = "";
62
63while (my $line = <CONFIG_FILE>) {
64 if ($line =~ /^(\/\/)?\s*#\s*define\s+(MBEDTLS_\w+).*/) {
65 my $name = $2;
66
67 # Skip over the macro that prevents multiple inclusion
68 next if "MBEDTLS_CONFIG_H" eq $name;
69
Andres Amaya Garciaef672f02019-01-03 20:16:43 +000070 # Skip over the macro if it is in the ecluded list
71 next if $name =~ /$excluded_re/;
72
Andres Amaya Garcia88121a92018-10-16 22:00:13 +010073 $config_check .= "#if defined($name)\n";
74 $config_check .= " if( strcmp( \"$name\", config ) == 0 )\n";
75 $config_check .= " {\n";
Andres Amaya Garcia27b33722018-12-05 10:47:31 +000076 $config_check .= " MACRO_EXPANSION_TO_STR( $name );\n";
Andres Amaya Garcia88121a92018-10-16 22:00:13 +010077 $config_check .= " return( 0 );\n";
78 $config_check .= " }\n";
79 $config_check .= "#endif /* $name */\n";
80 $config_check .= "\n";
81 }
82}
83
Andres Amaya Garcia109f8b62018-10-23 19:53:14 +010084# Read the full format file into a string
Andres Amaya Garcia88121a92018-10-16 22:00:13 +010085local $/;
86open(FORMAT_FILE, "$query_config_format_file") or die "Opening query config format file '$query_config_format_file': $!";
87my $query_config_format = <FORMAT_FILE>;
88close(FORMAT_FILE);
89
90# Replace the body of the query_config() function with the code we just wrote
91$query_config_format =~ s/CHECK_CONFIG/$config_check/g;
92
93# Rewrite the query_config.c file
94open(QUERY_CONFIG_FILE, ">$query_config_file") or die "Opening destination file '$query_config_file': $!";
95print QUERY_CONFIG_FILE $query_config_format;
96close(QUERY_CONFIG_FILE);