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 |
Bence Szépkúti | c7da1fe | 2020-05-26 01:54:15 +0200 | [diff] [blame] | 4 | # SPDX-License-Identifier: Apache-2.0 |
| 5 | # |
| 6 | # Licensed under the Apache License, Version 2.0 (the "License"); you may |
| 7 | # not use this file except in compliance with the License. |
| 8 | # You may obtain a copy of the License at |
| 9 | # |
| 10 | # http://www.apache.org/licenses/LICENSE-2.0 |
| 11 | # |
| 12 | # Unless required by applicable law or agreed to in writing, software |
| 13 | # distributed under the License is distributed on an "AS IS" BASIS, WITHOUT |
| 14 | # WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 15 | # See the License for the specific language governing permissions and |
| 16 | # limitations under the License. |
Paul Bakker | 0f90d7d | 2014-04-30 11:49:44 +0200 | [diff] [blame] | 17 | |
| 18 | use strict; |
| 19 | |
Manuel Pégourié-Gonnard | d66f900 | 2014-05-09 13:40:14 +0200 | [diff] [blame] | 20 | my ($include_dir, $data_dir, $feature_file); |
| 21 | |
| 22 | if( @ARGV ) { |
| 23 | die "Invalid number of arguments" if scalar @ARGV != 3; |
| 24 | ($include_dir, $data_dir, $feature_file) = @ARGV; |
| 25 | |
| 26 | -d $include_dir or die "No such directory: $include_dir\n"; |
| 27 | -d $data_dir or die "No such directory: $data_dir\n"; |
| 28 | } else { |
Manuel Pégourié-Gonnard | 7f80997 | 2015-03-09 17:05:11 +0000 | [diff] [blame] | 29 | $include_dir = 'include/mbedtls'; |
Manuel Pégourié-Gonnard | d66f900 | 2014-05-09 13:40:14 +0200 | [diff] [blame] | 30 | $data_dir = 'scripts/data_files'; |
| 31 | $feature_file = 'library/version_features.c'; |
| 32 | |
| 33 | unless( -d $include_dir && -d $data_dir ) { |
| 34 | chdir '..' or die; |
| 35 | -d $include_dir && -d $data_dir |
| 36 | or die "Without arguments, must be run from root or scripts\n" |
| 37 | } |
| 38 | } |
| 39 | |
Paul Bakker | 0f90d7d | 2014-04-30 11:49:44 +0200 | [diff] [blame] | 40 | my $feature_format_file = $data_dir.'/version_features.fmt'; |
| 41 | |
Manuel Pégourié-Gonnard | b4fe3cb | 2015-01-22 16:11:05 +0000 | [diff] [blame] | 42 | my @sections = ( "System support", "mbed TLS modules", |
| 43 | "mbed TLS feature support" ); |
Paul Bakker | 0f90d7d | 2014-04-30 11:49:44 +0200 | [diff] [blame] | 44 | |
| 45 | my $line_separator = $/; |
| 46 | undef $/; |
| 47 | |
| 48 | open(FORMAT_FILE, "$feature_format_file") or die "Opening feature format file '$feature_format_file': $!"; |
| 49 | my $feature_format = <FORMAT_FILE>; |
| 50 | close(FORMAT_FILE); |
| 51 | |
| 52 | $/ = $line_separator; |
| 53 | |
| 54 | open(CONFIG_H, "$include_dir/config.h") || die("Failure when opening config.h: $!"); |
| 55 | |
| 56 | my $feature_defines = ""; |
| 57 | my $in_section = 0; |
| 58 | |
| 59 | while (my $line = <CONFIG_H>) |
| 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 | $feature_defines .= "#if defined(${define})\n"; |
| 72 | $feature_defines .= " \"${define}\",\n"; |
| 73 | $feature_defines .= "#endif /* ${define} */\n"; |
| 74 | } |
| 75 | |
| 76 | if (!$in_section) { |
| 77 | my ($section_name) = $line =~ /SECTION: ([\w ]+)/; |
| 78 | my $found_section = grep $_ eq $section_name, @sections; |
| 79 | |
| 80 | $in_section = 1 if ($found_section); |
| 81 | } |
| 82 | }; |
| 83 | |
| 84 | $feature_format =~ s/FEATURE_DEFINES\n/$feature_defines/g; |
| 85 | |
| 86 | open(ERROR_FILE, ">$feature_file") or die "Opening destination file '$feature_file': $!"; |
| 87 | print ERROR_FILE $feature_format; |
| 88 | close(ERROR_FILE); |