blob: bf08468cc82ef6f441bb45d5c5453b4878b98bab [file] [log] [blame]
Andres Amaya Garciaf1a5b262018-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úti468a76f2020-05-26 00:33:31 +020018#
19# Copyright (C) 2018-2019, Arm Limited, All Rights Reserved
Bence Szépkútif744bd72020-06-05 13:02:18 +020020# SPDX-License-Identifier: Apache-2.0 OR GPL-2.0-or-later
21#
22# This file is provided under the Apache License 2.0, or the
23# GNU General Public License v2.0 or later.
24#
25# **********
26# Apache License 2.0:
Bence Szépkúti51b41d52020-05-26 01:54:15 +020027#
28# Licensed under the Apache License, Version 2.0 (the "License"); you may
29# not use this file except in compliance with the License.
30# You may obtain a copy of the License at
31#
32# http://www.apache.org/licenses/LICENSE-2.0
33#
34# Unless required by applicable law or agreed to in writing, software
35# distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
36# WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
37# See the License for the specific language governing permissions and
38# limitations under the License.
Bence Szépkúti468a76f2020-05-26 00:33:31 +020039#
Bence Szépkútif744bd72020-06-05 13:02:18 +020040# **********
41#
42# **********
43# GNU General Public License v2.0 or later:
44#
45# This program is free software; you can redistribute it and/or modify
46# it under the terms of the GNU General Public License as published by
47# the Free Software Foundation; either version 2 of the License, or
48# (at your option) any later version.
49#
50# This program is distributed in the hope that it will be useful,
51# but WITHOUT ANY WARRANTY; without even the implied warranty of
52# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
53# GNU General Public License for more details.
54#
55# You should have received a copy of the GNU General Public License along
56# with this program; if not, write to the Free Software Foundation, Inc.,
57# 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
58#
59# **********
60#
Bence Szépkúti468a76f2020-05-26 00:33:31 +020061# This file is part of Mbed TLS (https://tls.mbed.org)
Andres Amaya Garciaf1a5b262018-10-16 22:00:13 +010062
63use strict;
64
65my $config_file = "./include/mbedtls/config.h";
66
67my $query_config_format_file = "./scripts/data_files/query_config.fmt";
68my $query_config_file = "./programs/ssl/query_config.c";
69
Andres Amaya Garcia4eb040a2019-01-03 20:16:43 +000070# Excluded macros from the generated query_config.c. For example, macros that
71# have commas or function-like macros cannot be transformed into strings easily
72# using the preprocessor, so they should be excluded or the preprocessor will
73# throw errors.
74my @excluded = qw(
75MBEDTLS_SSL_CIPHERSUITES
Andres Amaya Garciafe52d2a2019-01-08 20:02:48 +000076MBEDTLS_PARAM_FAILED
Andres Amaya Garcia4eb040a2019-01-03 20:16:43 +000077);
78my $excluded_re = join '|', @excluded;
79
Andres Amaya Garciaf1a5b262018-10-16 22:00:13 +010080open(CONFIG_FILE, "$config_file") or die "Opening config file '$config_file': $!";
81
82# This variable will contain the string to replace in the CHECK_CONFIG of the
83# format file
84my $config_check = "";
85
86while (my $line = <CONFIG_FILE>) {
87 if ($line =~ /^(\/\/)?\s*#\s*define\s+(MBEDTLS_\w+).*/) {
88 my $name = $2;
89
90 # Skip over the macro that prevents multiple inclusion
91 next if "MBEDTLS_CONFIG_H" eq $name;
92
Andres Amaya Garcia4eb040a2019-01-03 20:16:43 +000093 # Skip over the macro if it is in the ecluded list
94 next if $name =~ /$excluded_re/;
95
Andres Amaya Garciaf1a5b262018-10-16 22:00:13 +010096 $config_check .= "#if defined($name)\n";
97 $config_check .= " if( strcmp( \"$name\", config ) == 0 )\n";
98 $config_check .= " {\n";
Andres Amaya Garciac500ad82018-12-05 10:47:31 +000099 $config_check .= " MACRO_EXPANSION_TO_STR( $name );\n";
Andres Amaya Garciaf1a5b262018-10-16 22:00:13 +0100100 $config_check .= " return( 0 );\n";
101 $config_check .= " }\n";
102 $config_check .= "#endif /* $name */\n";
103 $config_check .= "\n";
104 }
105}
106
Andres Amaya Garciae8b11dd2018-10-23 19:53:14 +0100107# Read the full format file into a string
Andres Amaya Garciaf1a5b262018-10-16 22:00:13 +0100108local $/;
109open(FORMAT_FILE, "$query_config_format_file") or die "Opening query config format file '$query_config_format_file': $!";
110my $query_config_format = <FORMAT_FILE>;
111close(FORMAT_FILE);
112
113# Replace the body of the query_config() function with the code we just wrote
114$query_config_format =~ s/CHECK_CONFIG/$config_check/g;
115
116# Rewrite the query_config.c file
117open(QUERY_CONFIG_FILE, ">$query_config_file") or die "Opening destination file '$query_config_file': $!";
118print QUERY_CONFIG_FILE $query_config_format;
119close(QUERY_CONFIG_FILE);