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 | # |
| 3 | |
| 4 | use strict; |
| 5 | |
Andrzej Kurek | 79369cd | 2019-04-05 04:07:40 -0400 | [diff] [blame] | 6 | my ($include_dir, $data_dir, $feature_file, $include_crypto); |
| 7 | my $crypto_include_dir = "crypto/include/mbedtls"; |
Manuel Pégourié-Gonnard | d66f900 | 2014-05-09 13:40:14 +0200 | [diff] [blame] | 8 | |
| 9 | if( @ARGV ) { |
Andrzej Kurek | 79369cd | 2019-04-05 04:07:40 -0400 | [diff] [blame] | 10 | die "Invalid number of arguments" if scalar @ARGV != 4; |
| 11 | ($include_dir, $data_dir, $feature_file, $include_crypto) = @ARGV; |
Manuel Pégourié-Gonnard | d66f900 | 2014-05-09 13:40:14 +0200 | [diff] [blame] | 12 | |
| 13 | -d $include_dir or die "No such directory: $include_dir\n"; |
| 14 | -d $data_dir or die "No such directory: $data_dir\n"; |
Andrzej Kurek | 79369cd | 2019-04-05 04:07:40 -0400 | [diff] [blame] | 15 | if( $include_crypto ) { |
| 16 | -d $crypto_include_dir or die "Crypto submodule not present\n"; |
| 17 | } |
Manuel Pégourié-Gonnard | d66f900 | 2014-05-09 13:40:14 +0200 | [diff] [blame] | 18 | } else { |
Manuel Pégourié-Gonnard | 7f80997 | 2015-03-09 17:05:11 +0000 | [diff] [blame] | 19 | $include_dir = 'include/mbedtls'; |
Manuel Pégourié-Gonnard | d66f900 | 2014-05-09 13:40:14 +0200 | [diff] [blame] | 20 | $data_dir = 'scripts/data_files'; |
| 21 | $feature_file = 'library/version_features.c'; |
Andrzej Kurek | 79369cd | 2019-04-05 04:07:40 -0400 | [diff] [blame] | 22 | $include_crypto = 1; |
| 23 | -d $crypto_include_dir or die "Crypto submodule not present\n"; |
Manuel Pégourié-Gonnard | d66f900 | 2014-05-09 13:40:14 +0200 | [diff] [blame] | 24 | |
| 25 | unless( -d $include_dir && -d $data_dir ) { |
| 26 | chdir '..' or die; |
| 27 | -d $include_dir && -d $data_dir |
| 28 | or die "Without arguments, must be run from root or scripts\n" |
| 29 | } |
| 30 | } |
| 31 | |
Paul Bakker | 0f90d7d | 2014-04-30 11:49:44 +0200 | [diff] [blame] | 32 | my $feature_format_file = $data_dir.'/version_features.fmt'; |
| 33 | |
Manuel Pégourié-Gonnard | b4fe3cb | 2015-01-22 16:11:05 +0000 | [diff] [blame] | 34 | my @sections = ( "System support", "mbed TLS modules", |
| 35 | "mbed TLS feature support" ); |
Paul Bakker | 0f90d7d | 2014-04-30 11:49:44 +0200 | [diff] [blame] | 36 | |
| 37 | my $line_separator = $/; |
| 38 | undef $/; |
| 39 | |
| 40 | open(FORMAT_FILE, "$feature_format_file") or die "Opening feature format file '$feature_format_file': $!"; |
| 41 | my $feature_format = <FORMAT_FILE>; |
| 42 | close(FORMAT_FILE); |
| 43 | |
| 44 | $/ = $line_separator; |
Andrzej Kurek | 79369cd | 2019-04-05 04:07:40 -0400 | [diff] [blame] | 45 | my %defines_seen; |
| 46 | my @files = ("$include_dir/config.h"); |
Paul Bakker | 0f90d7d | 2014-04-30 11:49:44 +0200 | [diff] [blame] | 47 | |
Andrzej Kurek | 79369cd | 2019-04-05 04:07:40 -0400 | [diff] [blame] | 48 | if( $include_crypto ) { |
| 49 | push(@files, "$crypto_include_dir/config.h"); |
| 50 | } |
Paul Bakker | 0f90d7d | 2014-04-30 11:49:44 +0200 | [diff] [blame] | 51 | |
| 52 | my $feature_defines = ""; |
Paul Bakker | 0f90d7d | 2014-04-30 11:49:44 +0200 | [diff] [blame] | 53 | |
Andrzej Kurek | 79369cd | 2019-04-05 04:07:40 -0400 | [diff] [blame] | 54 | foreach my $file (@files) { |
| 55 | open(FILE, "$file") or die "Opening config file failed: '$file': $!"; |
Paul Bakker | 0f90d7d | 2014-04-30 11:49:44 +0200 | [diff] [blame] | 56 | |
Andrzej Kurek | 79369cd | 2019-04-05 04:07:40 -0400 | [diff] [blame] | 57 | my $in_section = 0; |
| 58 | |
| 59 | while (my $line = <FILE>) |
| 60 | { |
| 61 | next if ($in_section && $line !~ /#define/ && $line !~ /SECTION/); |
| 62 | next if (!$in_section && $line !~ /SECTION/); |
| 63 | |
| 64 | if ($in_section) { |
| 65 | if ($line =~ /SECTION/) { |
| 66 | $in_section = 0; |
| 67 | next; |
| 68 | } |
| 69 | |
| 70 | my ($define) = $line =~ /#define (\w+)/; |
| 71 | |
| 72 | # Skip if this define is already added |
| 73 | if( $defines_seen{$define}++ ) { |
| 74 | print "Skipping $define, already added. \n"; |
| 75 | next; |
| 76 | } |
| 77 | |
| 78 | $feature_defines .= "#if defined(${define})\n"; |
| 79 | $feature_defines .= " \"${define}\",\n"; |
| 80 | $feature_defines .= "#endif /* ${define} */\n"; |
Paul Bakker | 0f90d7d | 2014-04-30 11:49:44 +0200 | [diff] [blame] | 81 | } |
| 82 | |
Andrzej Kurek | 79369cd | 2019-04-05 04:07:40 -0400 | [diff] [blame] | 83 | if (!$in_section) { |
| 84 | my ($section_name) = $line =~ /SECTION: ([\w ]+)/; |
| 85 | my $found_section = grep $_ eq $section_name, @sections; |
Paul Bakker | 0f90d7d | 2014-04-30 11:49:44 +0200 | [diff] [blame] | 86 | |
Andrzej Kurek | 79369cd | 2019-04-05 04:07:40 -0400 | [diff] [blame] | 87 | $in_section = 1 if ($found_section); |
| 88 | } |
| 89 | }; |
| 90 | close(FILE); |
| 91 | } |
Paul Bakker | 0f90d7d | 2014-04-30 11:49:44 +0200 | [diff] [blame] | 92 | $feature_format =~ s/FEATURE_DEFINES\n/$feature_defines/g; |
| 93 | |
| 94 | open(ERROR_FILE, ">$feature_file") or die "Opening destination file '$feature_file': $!"; |
| 95 | print ERROR_FILE $feature_format; |
| 96 | close(ERROR_FILE); |