blob: c904dafcb1b530965b87b1a291dca2a0a2545a56 [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
20#
21# This file is part of Mbed TLS (https://tls.mbed.org)
Andres Amaya Garciaf1a5b262018-10-16 22:00:13 +010022
23use strict;
24
25my $config_file = "./include/mbedtls/config.h";
26
27my $query_config_format_file = "./scripts/data_files/query_config.fmt";
28my $query_config_file = "./programs/ssl/query_config.c";
29
Andres Amaya Garcia4eb040a2019-01-03 20:16:43 +000030# Excluded macros from the generated query_config.c. For example, macros that
31# have commas or function-like macros cannot be transformed into strings easily
32# using the preprocessor, so they should be excluded or the preprocessor will
33# throw errors.
34my @excluded = qw(
35MBEDTLS_SSL_CIPHERSUITES
Andres Amaya Garciafe52d2a2019-01-08 20:02:48 +000036MBEDTLS_PARAM_FAILED
Andres Amaya Garcia4eb040a2019-01-03 20:16:43 +000037);
38my $excluded_re = join '|', @excluded;
39
Andres Amaya Garciaf1a5b262018-10-16 22:00:13 +010040open(CONFIG_FILE, "$config_file") or die "Opening config file '$config_file': $!";
41
42# This variable will contain the string to replace in the CHECK_CONFIG of the
43# format file
44my $config_check = "";
45
46while (my $line = <CONFIG_FILE>) {
47 if ($line =~ /^(\/\/)?\s*#\s*define\s+(MBEDTLS_\w+).*/) {
48 my $name = $2;
49
50 # Skip over the macro that prevents multiple inclusion
51 next if "MBEDTLS_CONFIG_H" eq $name;
52
Andres Amaya Garcia4eb040a2019-01-03 20:16:43 +000053 # Skip over the macro if it is in the ecluded list
54 next if $name =~ /$excluded_re/;
55
Andres Amaya Garciaf1a5b262018-10-16 22:00:13 +010056 $config_check .= "#if defined($name)\n";
57 $config_check .= " if( strcmp( \"$name\", config ) == 0 )\n";
58 $config_check .= " {\n";
Andres Amaya Garciac500ad82018-12-05 10:47:31 +000059 $config_check .= " MACRO_EXPANSION_TO_STR( $name );\n";
Andres Amaya Garciaf1a5b262018-10-16 22:00:13 +010060 $config_check .= " return( 0 );\n";
61 $config_check .= " }\n";
62 $config_check .= "#endif /* $name */\n";
63 $config_check .= "\n";
64 }
65}
66
Andres Amaya Garciae8b11dd2018-10-23 19:53:14 +010067# Read the full format file into a string
Andres Amaya Garciaf1a5b262018-10-16 22:00:13 +010068local $/;
69open(FORMAT_FILE, "$query_config_format_file") or die "Opening query config format file '$query_config_format_file': $!";
70my $query_config_format = <FORMAT_FILE>;
71close(FORMAT_FILE);
72
73# Replace the body of the query_config() function with the code we just wrote
74$query_config_format =~ s/CHECK_CONFIG/$config_check/g;
75
76# Rewrite the query_config.c file
77open(QUERY_CONFIG_FILE, ">$query_config_file") or die "Opening destination file '$query_config_file': $!";
78print QUERY_CONFIG_FILE $query_config_format;
79close(QUERY_CONFIG_FILE);