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 | a2947ac | 2020-08-19 16:37:36 +0200 | [diff] [blame^] | 3 | # Copyright The Mbed TLS Contributors |
Bence Szépkúti | f744bd7 | 2020-06-05 13:02:18 +0200 | [diff] [blame] | 4 | # SPDX-License-Identifier: Apache-2.0 OR GPL-2.0-or-later |
| 5 | # |
| 6 | # This file is provided under the Apache License 2.0, or the |
| 7 | # GNU General Public License v2.0 or later. |
| 8 | # |
| 9 | # ********** |
| 10 | # Apache License 2.0: |
Bence Szépkúti | 51b41d5 | 2020-05-26 01:54:15 +0200 | [diff] [blame] | 11 | # |
| 12 | # Licensed under the Apache License, Version 2.0 (the "License"); you may |
| 13 | # not use this file except in compliance with the License. |
| 14 | # You may obtain a copy of the License at |
| 15 | # |
| 16 | # http://www.apache.org/licenses/LICENSE-2.0 |
| 17 | # |
| 18 | # Unless required by applicable law or agreed to in writing, software |
| 19 | # distributed under the License is distributed on an "AS IS" BASIS, WITHOUT |
| 20 | # WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 21 | # See the License for the specific language governing permissions and |
| 22 | # limitations under the License. |
Bence Szépkúti | 468a76f | 2020-05-26 00:33:31 +0200 | [diff] [blame] | 23 | # |
Bence Szépkúti | f744bd7 | 2020-06-05 13:02:18 +0200 | [diff] [blame] | 24 | # ********** |
| 25 | # |
| 26 | # ********** |
| 27 | # GNU General Public License v2.0 or later: |
| 28 | # |
| 29 | # This program is free software; you can redistribute it and/or modify |
| 30 | # it under the terms of the GNU General Public License as published by |
| 31 | # the Free Software Foundation; either version 2 of the License, or |
| 32 | # (at your option) any later version. |
| 33 | # |
| 34 | # This program is distributed in the hope that it will be useful, |
| 35 | # but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 36 | # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 37 | # GNU General Public License for more details. |
| 38 | # |
| 39 | # You should have received a copy of the GNU General Public License along |
| 40 | # with this program; if not, write to the Free Software Foundation, Inc., |
| 41 | # 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. |
| 42 | # |
| 43 | # ********** |
Paul Bakker | 0f90d7d | 2014-04-30 11:49:44 +0200 | [diff] [blame] | 44 | |
| 45 | use strict; |
| 46 | |
Manuel Pégourié-Gonnard | d66f900 | 2014-05-09 13:40:14 +0200 | [diff] [blame] | 47 | my ($include_dir, $data_dir, $feature_file); |
| 48 | |
| 49 | if( @ARGV ) { |
| 50 | die "Invalid number of arguments" if scalar @ARGV != 3; |
| 51 | ($include_dir, $data_dir, $feature_file) = @ARGV; |
| 52 | |
| 53 | -d $include_dir or die "No such directory: $include_dir\n"; |
| 54 | -d $data_dir or die "No such directory: $data_dir\n"; |
| 55 | } else { |
Manuel Pégourié-Gonnard | 7f80997 | 2015-03-09 17:05:11 +0000 | [diff] [blame] | 56 | $include_dir = 'include/mbedtls'; |
Manuel Pégourié-Gonnard | d66f900 | 2014-05-09 13:40:14 +0200 | [diff] [blame] | 57 | $data_dir = 'scripts/data_files'; |
| 58 | $feature_file = 'library/version_features.c'; |
| 59 | |
| 60 | unless( -d $include_dir && -d $data_dir ) { |
| 61 | chdir '..' or die; |
| 62 | -d $include_dir && -d $data_dir |
| 63 | or die "Without arguments, must be run from root or scripts\n" |
| 64 | } |
| 65 | } |
| 66 | |
Paul Bakker | 0f90d7d | 2014-04-30 11:49:44 +0200 | [diff] [blame] | 67 | my $feature_format_file = $data_dir.'/version_features.fmt'; |
| 68 | |
Manuel Pégourié-Gonnard | b4fe3cb | 2015-01-22 16:11:05 +0000 | [diff] [blame] | 69 | my @sections = ( "System support", "mbed TLS modules", |
| 70 | "mbed TLS feature support" ); |
Paul Bakker | 0f90d7d | 2014-04-30 11:49:44 +0200 | [diff] [blame] | 71 | |
| 72 | my $line_separator = $/; |
| 73 | undef $/; |
| 74 | |
| 75 | open(FORMAT_FILE, "$feature_format_file") or die "Opening feature format file '$feature_format_file': $!"; |
| 76 | my $feature_format = <FORMAT_FILE>; |
| 77 | close(FORMAT_FILE); |
| 78 | |
| 79 | $/ = $line_separator; |
| 80 | |
| 81 | open(CONFIG_H, "$include_dir/config.h") || die("Failure when opening config.h: $!"); |
| 82 | |
| 83 | my $feature_defines = ""; |
| 84 | my $in_section = 0; |
| 85 | |
| 86 | while (my $line = <CONFIG_H>) |
| 87 | { |
| 88 | next if ($in_section && $line !~ /#define/ && $line !~ /SECTION/); |
| 89 | next if (!$in_section && $line !~ /SECTION/); |
| 90 | |
| 91 | if ($in_section) { |
| 92 | if ($line =~ /SECTION/) { |
| 93 | $in_section = 0; |
| 94 | next; |
| 95 | } |
| 96 | |
| 97 | my ($define) = $line =~ /#define (\w+)/; |
| 98 | $feature_defines .= "#if defined(${define})\n"; |
| 99 | $feature_defines .= " \"${define}\",\n"; |
| 100 | $feature_defines .= "#endif /* ${define} */\n"; |
| 101 | } |
| 102 | |
| 103 | if (!$in_section) { |
| 104 | my ($section_name) = $line =~ /SECTION: ([\w ]+)/; |
| 105 | my $found_section = grep $_ eq $section_name, @sections; |
| 106 | |
| 107 | $in_section = 1 if ($found_section); |
| 108 | } |
| 109 | }; |
| 110 | |
| 111 | $feature_format =~ s/FEATURE_DEFINES\n/$feature_defines/g; |
| 112 | |
| 113 | open(ERROR_FILE, ">$feature_file") or die "Opening destination file '$feature_file': $!"; |
| 114 | print ERROR_FILE $feature_format; |
| 115 | close(ERROR_FILE); |