fbrosson | 533407a | 2018-04-04 21:44:29 +0000 | [diff] [blame] | 1 | #!/usr/bin/env perl |
Paul Bakker | 0f90d7d | 2014-04-30 11:49:44 +0200 | [diff] [blame] | 2 | # |
Bence Szépkúti | 1e14827 | 2020-08-07 13:07:28 +0200 | [diff] [blame] | 3 | # Copyright The Mbed TLS Contributors |
Dave Rodgman | 16799db | 2023-11-02 19:47:20 +0000 | [diff] [blame] | 4 | # SPDX-License-Identifier: Apache-2.0 OR GPL-2.0-or-later |
Paul Bakker | 0f90d7d | 2014-04-30 11:49:44 +0200 | [diff] [blame] | 5 | |
| 6 | use strict; |
| 7 | |
Manuel Pégourié-Gonnard | d66f900 | 2014-05-09 13:40:14 +0200 | [diff] [blame] | 8 | my ($include_dir, $data_dir, $feature_file); |
| 9 | |
| 10 | if( @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é-Gonnard | 7f80997 | 2015-03-09 17:05:11 +0000 | [diff] [blame] | 17 | $include_dir = 'include/mbedtls'; |
Manuel Pégourié-Gonnard | d66f900 | 2014-05-09 13:40:14 +0200 | [diff] [blame] | 18 | $data_dir = 'scripts/data_files'; |
| 19 | $feature_file = 'library/version_features.c'; |
| 20 | |
| 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 Bakker | 0f90d7d | 2014-04-30 11:49:44 +0200 | [diff] [blame] | 28 | my $feature_format_file = $data_dir.'/version_features.fmt'; |
| 29 | |
Gilles Peskine | e820c0a | 2023-08-03 17:45:20 +0200 | [diff] [blame] | 30 | my @sections = ( "System support", "Mbed TLS modules", |
| 31 | "Mbed TLS feature support" ); |
Paul Bakker | 0f90d7d | 2014-04-30 11:49:44 +0200 | [diff] [blame] | 32 | |
| 33 | my $line_separator = $/; |
| 34 | undef $/; |
| 35 | |
Gilles Peskine | a4d3273 | 2021-04-23 09:44:59 +0200 | [diff] [blame] | 36 | open(FORMAT_FILE, '<:crlf', "$feature_format_file") or die "Opening feature format file '$feature_format_file': $!"; |
Paul Bakker | 0f90d7d | 2014-04-30 11:49:44 +0200 | [diff] [blame] | 37 | my $feature_format = <FORMAT_FILE>; |
| 38 | close(FORMAT_FILE); |
| 39 | |
| 40 | $/ = $line_separator; |
| 41 | |
Bence Szépkúti | bb0cfeb | 2021-05-28 09:42:25 +0200 | [diff] [blame] | 42 | open(CONFIG_H, '<:crlf', "$include_dir/mbedtls_config.h") || die("Failure when opening mbedtls_config.h: $!"); |
Paul Bakker | 0f90d7d | 2014-04-30 11:49:44 +0200 | [diff] [blame] | 43 | |
| 44 | my $feature_defines = ""; |
| 45 | my $in_section = 0; |
| 46 | |
| 47 | while (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 Rodgman | 90dfc21 | 2023-06-14 17:06:53 +0100 | [diff] [blame] | 57 | # 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 Rodgman | b28d1c3 | 2023-06-14 20:05:01 +0100 | [diff] [blame] | 63 | $feature_defines .= " \"${define}\", //no-check-names\n"; |
Dave Rodgman | 90dfc21 | 2023-06-14 17:06:53 +0100 | [diff] [blame] | 64 | $feature_defines .= "#endif /* MBEDTLS_${define} */\n"; |
Paul Bakker | 0f90d7d | 2014-04-30 11:49:44 +0200 | [diff] [blame] | 65 | } |
| 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 | |
| 77 | open(ERROR_FILE, ">$feature_file") or die "Opening destination file '$feature_file': $!"; |
| 78 | print ERROR_FILE $feature_format; |
| 79 | close(ERROR_FILE); |