blob: 78bf3ac531acf7919954871db04576ae1bfd0b37 [file] [log] [blame]
fbrosson533407a2018-04-04 21:44:29 +00001#!/usr/bin/env perl
Paul Bakker0f90d7d2014-04-30 11:49:44 +02002#
Bence Szépkúti1e148272020-08-07 13:07:28 +02003# Copyright The Mbed TLS Contributors
Dave Rodgman7ff79652023-11-03 12:04:52 +00004# SPDX-License-Identifier: Apache-2.0 OR GPL-2.0-or-later
Paul Bakker0f90d7d2014-04-30 11:49:44 +02005
6use strict;
7
Manuel Pégourié-Gonnardd66f9002014-05-09 13:40:14 +02008my ($include_dir, $data_dir, $feature_file);
9
10if( @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é-Gonnard7f809972015-03-09 17:05:11 +000017 $include_dir = 'include/mbedtls';
Manuel Pégourié-Gonnardd66f9002014-05-09 13:40:14 +020018 $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 Bakker0f90d7d2014-04-30 11:49:44 +020028my $feature_format_file = $data_dir.'/version_features.fmt';
29
Gilles Peskinef08ca832023-09-12 19:21:54 +020030my @sections = ( "System support", "Mbed TLS modules",
31 "Mbed TLS feature support" );
Paul Bakker0f90d7d2014-04-30 11:49:44 +020032
33my $line_separator = $/;
34undef $/;
35
36open(FORMAT_FILE, "$feature_format_file") or die "Opening feature format file '$feature_format_file': $!";
37my $feature_format = <FORMAT_FILE>;
38close(FORMAT_FILE);
39
40$/ = $line_separator;
41
42open(CONFIG_H, "$include_dir/config.h") || die("Failure when opening config.h: $!");
43
44my $feature_defines = "";
45my $in_section = 0;
46
47while (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
74open(ERROR_FILE, ">$feature_file") or die "Opening destination file '$feature_file': $!";
75print ERROR_FILE $feature_format;
76close(ERROR_FILE);