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 | 468a76f | 2020-05-26 00:33:31 +0200 | [diff] [blame^] | 3 | # Copyright (C) 2014-2015, Arm Limited, All Rights Reserved |
| 4 | # |
| 5 | # This file is part of Mbed TLS (https://tls.mbed.org) |
Paul Bakker | 0f90d7d | 2014-04-30 11:49:44 +0200 | [diff] [blame] | 6 | |
| 7 | use strict; |
| 8 | |
Manuel Pégourié-Gonnard | d66f900 | 2014-05-09 13:40:14 +0200 | [diff] [blame] | 9 | my ($include_dir, $data_dir, $feature_file); |
| 10 | |
| 11 | if( @ARGV ) { |
| 12 | die "Invalid number of arguments" if scalar @ARGV != 3; |
| 13 | ($include_dir, $data_dir, $feature_file) = @ARGV; |
| 14 | |
| 15 | -d $include_dir or die "No such directory: $include_dir\n"; |
| 16 | -d $data_dir or die "No such directory: $data_dir\n"; |
| 17 | } else { |
Manuel Pégourié-Gonnard | 7f80997 | 2015-03-09 17:05:11 +0000 | [diff] [blame] | 18 | $include_dir = 'include/mbedtls'; |
Manuel Pégourié-Gonnard | d66f900 | 2014-05-09 13:40:14 +0200 | [diff] [blame] | 19 | $data_dir = 'scripts/data_files'; |
| 20 | $feature_file = 'library/version_features.c'; |
| 21 | |
| 22 | unless( -d $include_dir && -d $data_dir ) { |
| 23 | chdir '..' or die; |
| 24 | -d $include_dir && -d $data_dir |
| 25 | or die "Without arguments, must be run from root or scripts\n" |
| 26 | } |
| 27 | } |
| 28 | |
Paul Bakker | 0f90d7d | 2014-04-30 11:49:44 +0200 | [diff] [blame] | 29 | my $feature_format_file = $data_dir.'/version_features.fmt'; |
| 30 | |
Manuel Pégourié-Gonnard | b4fe3cb | 2015-01-22 16:11:05 +0000 | [diff] [blame] | 31 | my @sections = ( "System support", "mbed TLS modules", |
| 32 | "mbed TLS feature support" ); |
Paul Bakker | 0f90d7d | 2014-04-30 11:49:44 +0200 | [diff] [blame] | 33 | |
| 34 | my $line_separator = $/; |
| 35 | undef $/; |
| 36 | |
| 37 | open(FORMAT_FILE, "$feature_format_file") or die "Opening feature format file '$feature_format_file': $!"; |
| 38 | my $feature_format = <FORMAT_FILE>; |
| 39 | close(FORMAT_FILE); |
| 40 | |
| 41 | $/ = $line_separator; |
| 42 | |
| 43 | open(CONFIG_H, "$include_dir/config.h") || die("Failure when opening config.h: $!"); |
| 44 | |
| 45 | my $feature_defines = ""; |
| 46 | my $in_section = 0; |
| 47 | |
| 48 | while (my $line = <CONFIG_H>) |
| 49 | { |
| 50 | next if ($in_section && $line !~ /#define/ && $line !~ /SECTION/); |
| 51 | next if (!$in_section && $line !~ /SECTION/); |
| 52 | |
| 53 | if ($in_section) { |
| 54 | if ($line =~ /SECTION/) { |
| 55 | $in_section = 0; |
| 56 | next; |
| 57 | } |
| 58 | |
| 59 | my ($define) = $line =~ /#define (\w+)/; |
| 60 | $feature_defines .= "#if defined(${define})\n"; |
| 61 | $feature_defines .= " \"${define}\",\n"; |
| 62 | $feature_defines .= "#endif /* ${define} */\n"; |
| 63 | } |
| 64 | |
| 65 | if (!$in_section) { |
| 66 | my ($section_name) = $line =~ /SECTION: ([\w ]+)/; |
| 67 | my $found_section = grep $_ eq $section_name, @sections; |
| 68 | |
| 69 | $in_section = 1 if ($found_section); |
| 70 | } |
| 71 | }; |
| 72 | |
| 73 | $feature_format =~ s/FEATURE_DEFINES\n/$feature_defines/g; |
| 74 | |
| 75 | open(ERROR_FILE, ">$feature_file") or die "Opening destination file '$feature_file': $!"; |
| 76 | print ERROR_FILE $feature_format; |
| 77 | close(ERROR_FILE); |