blob: e60bb88fbd7822023b3eca42e01b9f6be554e03a [file] [log] [blame]
fbrosson3a745712018-04-04 22:26:56 +00001#!/usr/bin/env perl
Paul Bakker0f90d7d2014-04-30 11:49:44 +02002#
Bence Szépkútib7246ad2020-05-26 00:33:31 +02003# Copyright (C) 2014-2015, Arm Limited, All Rights Reserved
Bence Szépkúti09b4f192020-05-26 01:54:15 +02004# 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.
Bence Szépkútib7246ad2020-05-26 00:33:31 +020017#
18# This file is part of Mbed TLS (https://tls.mbed.org)
Paul Bakker0f90d7d2014-04-30 11:49:44 +020019
20use strict;
21
Manuel Pégourié-Gonnardd66f9002014-05-09 13:40:14 +020022my ($include_dir, $data_dir, $feature_file);
23
24if( @ARGV ) {
25 die "Invalid number of arguments" if scalar @ARGV != 3;
26 ($include_dir, $data_dir, $feature_file) = @ARGV;
27
28 -d $include_dir or die "No such directory: $include_dir\n";
29 -d $data_dir or die "No such directory: $data_dir\n";
30} else {
Manuel Pégourié-Gonnard7f809972015-03-09 17:05:11 +000031 $include_dir = 'include/mbedtls';
Manuel Pégourié-Gonnardd66f9002014-05-09 13:40:14 +020032 $data_dir = 'scripts/data_files';
33 $feature_file = 'library/version_features.c';
34
35 unless( -d $include_dir && -d $data_dir ) {
36 chdir '..' or die;
37 -d $include_dir && -d $data_dir
38 or die "Without arguments, must be run from root or scripts\n"
39 }
40}
41
Paul Bakker0f90d7d2014-04-30 11:49:44 +020042my $feature_format_file = $data_dir.'/version_features.fmt';
43
Manuel Pégourié-Gonnardb4fe3cb2015-01-22 16:11:05 +000044my @sections = ( "System support", "mbed TLS modules",
45 "mbed TLS feature support" );
Paul Bakker0f90d7d2014-04-30 11:49:44 +020046
47my $line_separator = $/;
48undef $/;
49
50open(FORMAT_FILE, "$feature_format_file") or die "Opening feature format file '$feature_format_file': $!";
51my $feature_format = <FORMAT_FILE>;
52close(FORMAT_FILE);
53
54$/ = $line_separator;
55
56open(CONFIG_H, "$include_dir/config.h") || die("Failure when opening config.h: $!");
57
58my $feature_defines = "";
59my $in_section = 0;
60
61while (my $line = <CONFIG_H>)
62{
63 next if ($in_section && $line !~ /#define/ && $line !~ /SECTION/);
64 next if (!$in_section && $line !~ /SECTION/);
65
66 if ($in_section) {
67 if ($line =~ /SECTION/) {
68 $in_section = 0;
69 next;
70 }
71
72 my ($define) = $line =~ /#define (\w+)/;
73 $feature_defines .= "#if defined(${define})\n";
74 $feature_defines .= " \"${define}\",\n";
75 $feature_defines .= "#endif /* ${define} */\n";
76 }
77
78 if (!$in_section) {
79 my ($section_name) = $line =~ /SECTION: ([\w ]+)/;
80 my $found_section = grep $_ eq $section_name, @sections;
81
82 $in_section = 1 if ($found_section);
83 }
84};
85
86$feature_format =~ s/FEATURE_DEFINES\n/$feature_defines/g;
87
88open(ERROR_FILE, ">$feature_file") or die "Opening destination file '$feature_file': $!";
89print ERROR_FILE $feature_format;
90close(ERROR_FILE);