blob: 5e50ca6a4ef3b60a732608aa2efde26fe1c36812 [file] [log] [blame]
fbrosson533407a2018-04-04 21:44:29 +00001#!/usr/bin/env perl
Paul Bakker0f90d7d2014-04-30 11:49:44 +02002#
Bence Szépkúti1e148272020-08-07 13:07:28 +02003# Copyright The Mbed TLS Contributors
Dave Rodgman16799db2023-11-02 19:47:20 +00004# SPDX-License-Identifier: Apache-2.0 OR GPL-2.0-or-later
Paul Bakker0f90d7d2014-04-30 11:49:44 +02005
6use strict;
7
Manuel Pégourié-Gonnardd66f9002014-05-09 13:40:14 +02008my ($include_dir, $data_dir, $feature_file);
9
10if( @ARGV ) {
11 die "Invalid number of arguments" if scalar @ARGV != 3;
12 ($include_dir, $data_dir, $feature_file) = @ARGV;
13
14 -d $include_dir or die "No such directory: $include_dir\n";
15 -d $data_dir or die "No such directory: $data_dir\n";
16} else {
Manuel Pégourié-Gonnard7f809972015-03-09 17:05:11 +000017 $include_dir = 'include/mbedtls';
Manuel Pégourié-Gonnardd66f9002014-05-09 13:40:14 +020018 $data_dir = 'scripts/data_files';
Harry Ramsey468c0ae2024-09-27 14:38:53 +010019 $feature_file = 'library/version_features.c';
Manuel Pégourié-Gonnardd66f9002014-05-09 13:40:14 +020020
21 unless( -d $include_dir && -d $data_dir ) {
22 chdir '..' or die;
23 -d $include_dir && -d $data_dir
24 or die "Without arguments, must be run from root or scripts\n"
25 }
26}
27
Paul Bakker0f90d7d2014-04-30 11:49:44 +020028my $feature_format_file = $data_dir.'/version_features.fmt';
29
Minos Galanakis9d51a542024-12-13 12:24:46 +000030my @sections = ( "Platform abstraction layer", "General configuration options",
31 "TLS feature selection", "X.509 feature selection" );
Paul Bakker0f90d7d2014-04-30 11:49:44 +020032
33my $line_separator = $/;
34undef $/;
35
Gilles Peskinea4d32732021-04-23 09:44:59 +020036open(FORMAT_FILE, '<:crlf', "$feature_format_file") or die "Opening feature format file '$feature_format_file': $!";
Paul Bakker0f90d7d2014-04-30 11:49:44 +020037my $feature_format = <FORMAT_FILE>;
38close(FORMAT_FILE);
39
40$/ = $line_separator;
41
Bence Szépkútibb0cfeb2021-05-28 09:42:25 +020042open(CONFIG_H, '<:crlf', "$include_dir/mbedtls_config.h") || die("Failure when opening mbedtls_config.h: $!");
Paul Bakker0f90d7d2014-04-30 11:49:44 +020043
44my $feature_defines = "";
45my $in_section = 0;
46
47while (my $line = <CONFIG_H>)
48{
49 next if ($in_section && $line !~ /#define/ && $line !~ /SECTION/);
50 next if (!$in_section && $line !~ /SECTION/);
51
52 if ($in_section) {
53 if ($line =~ /SECTION/) {
54 $in_section = 0;
55 next;
56 }
Dave Rodgman90dfc212023-06-14 17:06:53 +010057 # Strip leading MBEDTLS_ to save binary size
58 my ($mbedtls_prefix, $define) = $line =~ /#define (MBEDTLS_)?(\w+)/;
59 if (!$mbedtls_prefix) {
60 die "Feature does not start with 'MBEDTLS_': $line\n";
61 }
62 $feature_defines .= "#if defined(MBEDTLS_${define})\n";
Dave Rodgmanb28d1c32023-06-14 20:05:01 +010063 $feature_defines .= " \"${define}\", //no-check-names\n";
Dave Rodgman90dfc212023-06-14 17:06:53 +010064 $feature_defines .= "#endif /* MBEDTLS_${define} */\n";
Paul Bakker0f90d7d2014-04-30 11:49:44 +020065 }
66
67 if (!$in_section) {
68 my ($section_name) = $line =~ /SECTION: ([\w ]+)/;
69 my $found_section = grep $_ eq $section_name, @sections;
70
71 $in_section = 1 if ($found_section);
72 }
73};
74
75$feature_format =~ s/FEATURE_DEFINES\n/$feature_defines/g;
76
77open(ERROR_FILE, ">$feature_file") or die "Opening destination file '$feature_file': $!";
78print ERROR_FILE $feature_format;
79close(ERROR_FILE);