generate_features.pl 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116
  1. #!/usr/bin/env perl
  2. #
  3. # Copyright The Mbed TLS Contributors
  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:
  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.
  23. #
  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. # **********
  44. use strict;
  45. my ($include_dir, $data_dir, $feature_file);
  46. if( @ARGV ) {
  47. die "Invalid number of arguments" if scalar @ARGV != 3;
  48. ($include_dir, $data_dir, $feature_file) = @ARGV;
  49. -d $include_dir or die "No such directory: $include_dir\n";
  50. -d $data_dir or die "No such directory: $data_dir\n";
  51. } else {
  52. $include_dir = 'include/mbedtls';
  53. $data_dir = 'scripts/data_files';
  54. $feature_file = 'library/version_features.c';
  55. unless( -d $include_dir && -d $data_dir ) {
  56. chdir '..' or die;
  57. -d $include_dir && -d $data_dir
  58. or die "Without arguments, must be run from root or scripts\n"
  59. }
  60. }
  61. my $feature_format_file = $data_dir.'/version_features.fmt';
  62. my @sections = ( "System support", "mbed TLS modules",
  63. "mbed TLS feature support" );
  64. my $line_separator = $/;
  65. undef $/;
  66. open(FORMAT_FILE, "$feature_format_file") or die "Opening feature format file '$feature_format_file': $!";
  67. my $feature_format = <FORMAT_FILE>;
  68. close(FORMAT_FILE);
  69. $/ = $line_separator;
  70. open(CONFIG_H, "$include_dir/config.h") || die("Failure when opening config.h: $!");
  71. my $feature_defines = "";
  72. my $in_section = 0;
  73. while (my $line = <CONFIG_H>)
  74. {
  75. next if ($in_section && $line !~ /#define/ && $line !~ /SECTION/);
  76. next if (!$in_section && $line !~ /SECTION/);
  77. if ($in_section) {
  78. if ($line =~ /SECTION/) {
  79. $in_section = 0;
  80. next;
  81. }
  82. my ($define) = $line =~ /#define (\w+)/;
  83. $feature_defines .= "#if defined(${define})\n";
  84. $feature_defines .= " \"${define}\",\n";
  85. $feature_defines .= "#endif /* ${define} */\n";
  86. }
  87. if (!$in_section) {
  88. my ($section_name) = $line =~ /SECTION: ([\w ]+)/;
  89. my $found_section = grep $_ eq $section_name, @sections;
  90. $in_section = 1 if ($found_section);
  91. }
  92. };
  93. $feature_format =~ s/FEATURE_DEFINES\n/$feature_defines/g;
  94. open(ERROR_FILE, ">$feature_file") or die "Opening destination file '$feature_file': $!";
  95. print ERROR_FILE $feature_format;
  96. close(ERROR_FILE);