blob: a6bc3da99d36b2086aace675efb91247e0f334d0 [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
Bence Szépkútibb0cfeb2021-05-28 09:42:25 +020011# include/mbedtls/mbedtls_config.h. The idea is that the mbedtls_config.h contains ALL the
Andres Amaya Garcia88121a92018-10-16 22:00:13 +010012# compile time configurations available in Mbed TLS (commented or uncommented).
Bence Szépkútibb0cfeb2021-05-28 09:42:25 +020013# This script extracts the configuration macros from the mbedtls_config.h and this
Andres Amaya Garcia88121a92018-10-16 22:00:13 +010014# 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#
Manuel Pégourié-Gonnard3a8413d2021-05-14 09:23:57 +020017# Usage: scripts/generate_query_config.pl without arguments, or
Tom Cosgroveef83b832022-07-25 11:42:38 +010018# generate_query_config.pl mbedtls_config_file template_file output_file
Bence Szépkúti700ee442020-05-26 00:33:31 +020019#
Bence Szépkúti1e148272020-08-07 13:07:28 +020020# Copyright The Mbed TLS Contributors
Bence Szépkútic7da1fe2020-05-26 01:54:15 +020021# 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.
Andres Amaya Garcia88121a92018-10-16 22:00:13 +010034
35use strict;
36
Tom Cosgroveef83b832022-07-25 11:42:38 +010037my ($mbedtls_config_file, $query_config_format_file, $query_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";
Andres Amaya Garcia88121a92018-10-16 22:00:13 +010042
Manuel Pégourié-Gonnard3a8413d2021-05-14 09:23:57 +020043if( @ARGV ) {
David Horstmannff0a3b32021-10-20 14:04:02 +010044 die "Invalid number of arguments - usage: $0 [CONFIG_FILE TEMPLATE_FILE OUTPUT_FILE]" if scalar @ARGV != 3;
Tom Cosgroveef83b832022-07-25 11:42:38 +010045 ($mbedtls_config_file, $query_config_format_file, $query_config_file) = @ARGV;
Andres Amaya Garcia88121a92018-10-16 22:00:13 +010046
Tom Cosgroveef83b832022-07-25 11:42:38 +010047 -f $mbedtls_config_file or die "No such file: $mbedtls_config_file";
Manuel Pégourié-Gonnard3a8413d2021-05-14 09:23:57 +020048 -f $query_config_format_file or die "No such file: $query_config_format_file";
49} else {
Tom Cosgroveef83b832022-07-25 11:42:38 +010050 $mbedtls_config_file = $default_mbedtls_config_file;
51 $query_config_format_file = $default_query_config_format_file;
52 $query_config_file = $default_query_config_file;
Manuel Pégourié-Gonnard3a8413d2021-05-14 09:23:57 +020053
Tom Cosgroveef83b832022-07-25 11:42:38 +010054 unless( -f $mbedtls_config_file && -f $query_config_format_file ) {
Manuel Pégourié-Gonnard3a8413d2021-05-14 09:23:57 +020055 chdir '..' or die;
Tom Cosgroveef83b832022-07-25 11:42:38 +010056 -f $mbedtls_config_file && -f $query_config_format_file
David Horstmannd64f4b22021-10-20 12:29:47 +010057 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 +020058 }
Gilles Peskinec86f20a2021-04-22 00:20:47 +020059}
60
Andres Amaya Garciaef672f02019-01-03 20:16:43 +000061# Excluded macros from the generated query_config.c. For example, macros that
62# have commas or function-like macros cannot be transformed into strings easily
63# using the preprocessor, so they should be excluded or the preprocessor will
64# throw errors.
65my @excluded = qw(
66MBEDTLS_SSL_CIPHERSUITES
67);
68my $excluded_re = join '|', @excluded;
69
Andres Amaya Garcia88121a92018-10-16 22:00:13 +010070# This variable will contain the string to replace in the CHECK_CONFIG of the
71# format file
72my $config_check = "";
Jerry Yu84e63a72021-12-06 13:40:37 +080073my $list_config = "";
Andres Amaya Garcia88121a92018-10-16 22:00:13 +010074
Tom Cosgroveef83b832022-07-25 11:42:38 +010075open(CONFIG_FILE, "<", $mbedtls_config_file) or die "Opening config file '$mbedtls_config_file': $!";
76
Andres Amaya Garcia88121a92018-10-16 22:00:13 +010077while (my $line = <CONFIG_FILE>) {
78 if ($line =~ /^(\/\/)?\s*#\s*define\s+(MBEDTLS_\w+).*/) {
79 my $name = $2;
80
Andres Amaya Garciaef672f02019-01-03 20:16:43 +000081 # Skip over the macro if it is in the ecluded list
82 next if $name =~ /$excluded_re/;
83
Tom Cosgroveef83b832022-07-25 11:42:38 +010084 $config_check .= <<EOT;
85#if defined($name)
86 if( strcmp( "$name", config ) == 0 )
87 {
88 MACRO_EXPANSION_TO_STR( $name );
89 return( 0 );
90 }
91#endif /* $name */
Jerry Yu84e63a72021-12-06 13:40:37 +080092
Tom Cosgroveef83b832022-07-25 11:42:38 +010093EOT
94
95 $list_config .= <<EOT;
96#if defined($name)
97 OUTPUT_MACRO_NAME_VALUE($name);
98#endif /* $name */
99
100EOT
Andres Amaya Garcia88121a92018-10-16 22:00:13 +0100101 }
102}
103
Tom Cosgroveef83b832022-07-25 11:42:38 +0100104close(CONFIG_FILE);
105
Andres Amaya Garcia109f8b62018-10-23 19:53:14 +0100106# Read the full format file into a string
Andres Amaya Garcia88121a92018-10-16 22:00:13 +0100107local $/;
Tom Cosgroveef83b832022-07-25 11:42:38 +0100108open(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 +0100109my $query_config_format = <FORMAT_FILE>;
110close(FORMAT_FILE);
111
112# Replace the body of the query_config() function with the code we just wrote
113$query_config_format =~ s/CHECK_CONFIG/$config_check/g;
Jerry Yu84e63a72021-12-06 13:40:37 +0800114$query_config_format =~ s/LIST_CONFIG/$list_config/g;
Andres Amaya Garcia88121a92018-10-16 22:00:13 +0100115
116# Rewrite the query_config.c file
Tom Cosgroveef83b832022-07-25 11:42:38 +0100117open(QUERY_CONFIG_FILE, ">", $query_config_file) or die "Opening destination file '$query_config_file': $!";
Andres Amaya Garcia88121a92018-10-16 22:00:13 +0100118print QUERY_CONFIG_FILE $query_config_format;
119close(QUERY_CONFIG_FILE);