blob: 3c049915e11dbd074ef281f6109632164050121b [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úti700ee442020-05-26 00:33:31 +02003# Copyright (C) 2014-2015, Arm Limited, All Rights Reserved
4#
5# This file is part of Mbed TLS (https://tls.mbed.org)
Paul Bakker0f90d7d2014-04-30 11:49:44 +02006
7use strict;
8
Manuel Pégourié-Gonnardd66f9002014-05-09 13:40:14 +02009my ($include_dir, $data_dir, $feature_file);
10
11if( @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é-Gonnard7f809972015-03-09 17:05:11 +000018 $include_dir = 'include/mbedtls';
Manuel Pégourié-Gonnardd66f9002014-05-09 13:40:14 +020019 $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 Bakker0f90d7d2014-04-30 11:49:44 +020029my $feature_format_file = $data_dir.'/version_features.fmt';
30
Manuel Pégourié-Gonnardb4fe3cb2015-01-22 16:11:05 +000031my @sections = ( "System support", "mbed TLS modules",
32 "mbed TLS feature support" );
Paul Bakker0f90d7d2014-04-30 11:49:44 +020033
34my $line_separator = $/;
35undef $/;
36
37open(FORMAT_FILE, "$feature_format_file") or die "Opening feature format file '$feature_format_file': $!";
38my $feature_format = <FORMAT_FILE>;
39close(FORMAT_FILE);
40
41$/ = $line_separator;
42
43open(CONFIG_H, "$include_dir/config.h") || die("Failure when opening config.h: $!");
44
45my $feature_defines = "";
46my $in_section = 0;
47
48while (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
75open(ERROR_FILE, ">$feature_file") or die "Opening destination file '$feature_file': $!";
76print ERROR_FILE $feature_format;
77close(ERROR_FILE);