1a8e1175bSopenharmony_ci#!/usr/bin/env perl
2a8e1175bSopenharmony_ci#
3a8e1175bSopenharmony_ci# Copyright The Mbed TLS Contributors
4a8e1175bSopenharmony_ci# SPDX-License-Identifier: Apache-2.0 OR GPL-2.0-or-later
5a8e1175bSopenharmony_ci
6a8e1175bSopenharmony_ciuse strict;
7a8e1175bSopenharmony_ci
8a8e1175bSopenharmony_cimy ($include_dir, $data_dir, $feature_file);
9a8e1175bSopenharmony_ci
10a8e1175bSopenharmony_ciif( @ARGV ) {
11a8e1175bSopenharmony_ci    die "Invalid number of arguments" if scalar @ARGV != 3;
12a8e1175bSopenharmony_ci    ($include_dir, $data_dir, $feature_file) = @ARGV;
13a8e1175bSopenharmony_ci
14a8e1175bSopenharmony_ci    -d $include_dir or die "No such directory: $include_dir\n";
15a8e1175bSopenharmony_ci    -d $data_dir or die "No such directory: $data_dir\n";
16a8e1175bSopenharmony_ci} else {
17a8e1175bSopenharmony_ci    $include_dir = 'include/mbedtls';
18a8e1175bSopenharmony_ci    $data_dir = 'scripts/data_files';
19a8e1175bSopenharmony_ci    $feature_file = 'library/version_features.c';
20a8e1175bSopenharmony_ci
21a8e1175bSopenharmony_ci    unless( -d $include_dir && -d $data_dir ) {
22a8e1175bSopenharmony_ci        chdir '..' or die;
23a8e1175bSopenharmony_ci        -d $include_dir && -d $data_dir
24a8e1175bSopenharmony_ci            or die "Without arguments, must be run from root or scripts\n"
25a8e1175bSopenharmony_ci    }
26a8e1175bSopenharmony_ci}
27a8e1175bSopenharmony_ci
28a8e1175bSopenharmony_cimy $feature_format_file = $data_dir.'/version_features.fmt';
29a8e1175bSopenharmony_ci
30a8e1175bSopenharmony_cimy @sections = ( "System support", "Mbed TLS modules",
31a8e1175bSopenharmony_ci                 "Mbed TLS feature support" );
32a8e1175bSopenharmony_ci
33a8e1175bSopenharmony_cimy $line_separator = $/;
34a8e1175bSopenharmony_ciundef $/;
35a8e1175bSopenharmony_ci
36a8e1175bSopenharmony_ciopen(FORMAT_FILE, '<:crlf', "$feature_format_file") or die "Opening feature format file '$feature_format_file': $!";
37a8e1175bSopenharmony_cimy $feature_format = <FORMAT_FILE>;
38a8e1175bSopenharmony_ciclose(FORMAT_FILE);
39a8e1175bSopenharmony_ci
40a8e1175bSopenharmony_ci$/ = $line_separator;
41a8e1175bSopenharmony_ci
42a8e1175bSopenharmony_ciopen(CONFIG_H, '<:crlf', "$include_dir/mbedtls_config.h") || die("Failure when opening mbedtls_config.h: $!");
43a8e1175bSopenharmony_ci
44a8e1175bSopenharmony_cimy $feature_defines = "";
45a8e1175bSopenharmony_cimy $in_section = 0;
46a8e1175bSopenharmony_ci
47a8e1175bSopenharmony_ciwhile (my $line = <CONFIG_H>)
48a8e1175bSopenharmony_ci{
49a8e1175bSopenharmony_ci    next if ($in_section && $line !~ /#define/ && $line !~ /SECTION/);
50a8e1175bSopenharmony_ci    next if (!$in_section && $line !~ /SECTION/);
51a8e1175bSopenharmony_ci
52a8e1175bSopenharmony_ci    if ($in_section) {
53a8e1175bSopenharmony_ci        if ($line =~ /SECTION/) {
54a8e1175bSopenharmony_ci            $in_section = 0;
55a8e1175bSopenharmony_ci            next;
56a8e1175bSopenharmony_ci        }
57a8e1175bSopenharmony_ci        # Strip leading MBEDTLS_ to save binary size
58a8e1175bSopenharmony_ci        my ($mbedtls_prefix, $define) = $line =~ /#define (MBEDTLS_)?(\w+)/;
59a8e1175bSopenharmony_ci        if (!$mbedtls_prefix) {
60a8e1175bSopenharmony_ci            die "Feature does not start with 'MBEDTLS_': $line\n";
61a8e1175bSopenharmony_ci        }
62a8e1175bSopenharmony_ci        $feature_defines .= "#if defined(MBEDTLS_${define})\n";
63a8e1175bSopenharmony_ci        $feature_defines .= "    \"${define}\", //no-check-names\n";
64a8e1175bSopenharmony_ci        $feature_defines .= "#endif /* MBEDTLS_${define} */\n";
65a8e1175bSopenharmony_ci    }
66a8e1175bSopenharmony_ci
67a8e1175bSopenharmony_ci    if (!$in_section) {
68a8e1175bSopenharmony_ci        my ($section_name) = $line =~ /SECTION: ([\w ]+)/;
69a8e1175bSopenharmony_ci        my $found_section = grep $_ eq $section_name, @sections;
70a8e1175bSopenharmony_ci
71a8e1175bSopenharmony_ci        $in_section = 1 if ($found_section);
72a8e1175bSopenharmony_ci    }
73a8e1175bSopenharmony_ci};
74a8e1175bSopenharmony_ci
75a8e1175bSopenharmony_ci$feature_format =~ s/FEATURE_DEFINES\n/$feature_defines/g;
76a8e1175bSopenharmony_ci
77a8e1175bSopenharmony_ciopen(ERROR_FILE, ">$feature_file") or die "Opening destination file '$feature_file': $!";
78a8e1175bSopenharmony_ciprint ERROR_FILE $feature_format;
79a8e1175bSopenharmony_ciclose(ERROR_FILE);
80