blob: af3076be0ea1925279831c0ddd3a5438bcd428d6 [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#
Andrzej Kurekb4b1ae12019-04-05 04:16:12 -040017# Usage: ./scripts/generate_query_config.pl include_crypto
Andres Amaya Garcia88121a92018-10-16 22:00:13 +010018
19use strict;
20
Andrzej Kurekef907602019-04-05 03:33:18 -040021my $include_crypto = 1;
Andres Amaya Garcia88121a92018-10-16 22:00:13 +010022my $config_file = "./include/mbedtls/config.h";
Andrzej Kurekef907602019-04-05 03:33:18 -040023my $crypto_config_file = "./crypto/include/mbedtls/config.h";
Andres Amaya Garcia88121a92018-10-16 22:00:13 +010024
25my $query_config_format_file = "./scripts/data_files/query_config.fmt";
26my $query_config_file = "./programs/ssl/query_config.c";
27
Andres Amaya Garciaef672f02019-01-03 20:16:43 +000028# Excluded macros from the generated query_config.c. For example, macros that
29# have commas or function-like macros cannot be transformed into strings easily
30# using the preprocessor, so they should be excluded or the preprocessor will
31# throw errors.
32my @excluded = qw(
33MBEDTLS_SSL_CIPHERSUITES
Andres Amaya Garcia8645f732019-01-08 20:02:48 +000034MBEDTLS_PARAM_FAILED
Andres Amaya Garciaef672f02019-01-03 20:16:43 +000035);
36my $excluded_re = join '|', @excluded;
37
Andres Amaya Garcia88121a92018-10-16 22:00:13 +010038
39# This variable will contain the string to replace in the CHECK_CONFIG of the
40# format file
41my $config_check = "";
Andrzej Kurekef907602019-04-05 03:33:18 -040042my %defines_seen;
43my @files = ($config_file);
Andres Amaya Garcia88121a92018-10-16 22:00:13 +010044
Andres Amaya Garcia88121a92018-10-16 22:00:13 +010045
Andrzej Kurekef907602019-04-05 03:33:18 -040046if( @ARGV ) {
47 die "Invalid number of arguments" if scalar @ARGV != 1;
48 ($include_crypto) = @ARGV;
49}
Andres Amaya Garcia88121a92018-10-16 22:00:13 +010050
Andrzej Kurekef907602019-04-05 03:33:18 -040051if( $include_crypto ) {
52 push(@files, $crypto_config_file);
53}
Andres Amaya Garciaef672f02019-01-03 20:16:43 +000054
Andrzej Kurekef907602019-04-05 03:33:18 -040055foreach my $file (@files) {
56 open(FILE, "$file") or die "Opening config file failed: '$file': $!";
57 while (my $line = <FILE>) {
58 if ($line =~ /^(\/\/)?\s*#\s*define\s+(MBEDTLS_\w+).*/) {
59 my $name = $2;
60
61 # Skip over the macro that prevents multiple inclusion
62 next if "MBEDTLS_CONFIG_H" eq $name;
63
64 # Skip over the macro if it is in the excluded list
65 next if $name =~ /$excluded_re/;
66
67 # Skip if this define is already added
68 if( $defines_seen{$name}++ ) {
69 print "Skipping $name, already added. \n";
70 next;
71 }
72
73 $config_check .= "#if defined($name)\n";
74 $config_check .= " if( strcmp( \"$name\", config ) == 0 )\n";
75 $config_check .= " {\n";
76 $config_check .= " MACRO_EXPANSION_TO_STR( $name );\n";
77 $config_check .= " return( 0 );\n";
78 $config_check .= " }\n";
79 $config_check .= "#endif /* $name */\n";
80 $config_check .= "\n";
81 }
Andres Amaya Garcia88121a92018-10-16 22:00:13 +010082 }
Andrzej Kurekef907602019-04-05 03:33:18 -040083 close(FILE);
Andres Amaya Garcia88121a92018-10-16 22:00:13 +010084}
85
Andres Amaya Garcia109f8b62018-10-23 19:53:14 +010086# Read the full format file into a string
Andres Amaya Garcia88121a92018-10-16 22:00:13 +010087local $/;
88open(FORMAT_FILE, "$query_config_format_file") or die "Opening query config format file '$query_config_format_file': $!";
89my $query_config_format = <FORMAT_FILE>;
90close(FORMAT_FILE);
91
92# Replace the body of the query_config() function with the code we just wrote
93$query_config_format =~ s/CHECK_CONFIG/$config_check/g;
94
95# Rewrite the query_config.c file
96open(QUERY_CONFIG_FILE, ">$query_config_file") or die "Opening destination file '$query_config_file': $!";
97print QUERY_CONFIG_FILE $query_config_format;
98close(QUERY_CONFIG_FILE);