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 | 7ff7965 | 2023-11-03 12:04:52 +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 | f08ca83 | 2023-09-12 19:21:54 +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 | |
| 36 | open(FORMAT_FILE, "$feature_format_file") or die "Opening feature format file '$feature_format_file': $!"; |
| 37 | my $feature_format = <FORMAT_FILE>; |
| 38 | close(FORMAT_FILE); |
| 39 | |
| 40 | $/ = $line_separator; |
| 41 | |
| 42 | open(CONFIG_H, "$include_dir/config.h") || die("Failure when opening config.h: $!"); |
| 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 | } |
| 57 | |
| 58 | my ($define) = $line =~ /#define (\w+)/; |
| 59 | $feature_defines .= "#if defined(${define})\n"; |
| 60 | $feature_defines .= " \"${define}\",\n"; |
| 61 | $feature_defines .= "#endif /* ${define} */\n"; |
| 62 | } |
| 63 | |
| 64 | if (!$in_section) { |
| 65 | my ($section_name) = $line =~ /SECTION: ([\w ]+)/; |
| 66 | my $found_section = grep $_ eq $section_name, @sections; |
| 67 | |
| 68 | $in_section = 1 if ($found_section); |
| 69 | } |
| 70 | }; |
| 71 | |
| 72 | $feature_format =~ s/FEATURE_DEFINES\n/$feature_defines/g; |
| 73 | |
| 74 | open(ERROR_FILE, ">$feature_file") or die "Opening destination file '$feature_file': $!"; |
| 75 | print ERROR_FILE $feature_format; |
| 76 | close(ERROR_FILE); |