generate_features.pl 2.6 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788
  1. #!/usr/bin/env perl
  2. #
  3. # Copyright The Mbed TLS Contributors
  4. # 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.
  17. use strict;
  18. my ($include_dir, $data_dir, $feature_file);
  19. if( @ARGV ) {
  20. die "Invalid number of arguments" if scalar @ARGV != 3;
  21. ($include_dir, $data_dir, $feature_file) = @ARGV;
  22. -d $include_dir or die "No such directory: $include_dir\n";
  23. -d $data_dir or die "No such directory: $data_dir\n";
  24. } else {
  25. $include_dir = 'include/mbedtls';
  26. $data_dir = 'scripts/data_files';
  27. $feature_file = 'library/version_features.c';
  28. unless( -d $include_dir && -d $data_dir ) {
  29. chdir '..' or die;
  30. -d $include_dir && -d $data_dir
  31. or die "Without arguments, must be run from root or scripts\n"
  32. }
  33. }
  34. my $feature_format_file = $data_dir.'/version_features.fmt';
  35. my @sections = ( "System support", "mbed TLS modules",
  36. "mbed TLS feature support" );
  37. my $line_separator = $/;
  38. undef $/;
  39. open(FORMAT_FILE, "$feature_format_file") or die "Opening feature format file '$feature_format_file': $!";
  40. my $feature_format = <FORMAT_FILE>;
  41. close(FORMAT_FILE);
  42. $/ = $line_separator;
  43. open(CONFIG_H, "$include_dir/config.h") || die("Failure when opening config.h: $!");
  44. my $feature_defines = "";
  45. my $in_section = 0;
  46. while (my $line = <CONFIG_H>)
  47. {
  48. next if ($in_section && $line !~ /#define/ && $line !~ /SECTION/);
  49. next if (!$in_section && $line !~ /SECTION/);
  50. if ($in_section) {
  51. if ($line =~ /SECTION/) {
  52. $in_section = 0;
  53. next;
  54. }
  55. my ($define) = $line =~ /#define (\w+)/;
  56. $feature_defines .= "#if defined(${define})\n";
  57. $feature_defines .= " \"${define}\",\n";
  58. $feature_defines .= "#endif /* ${define} */\n";
  59. }
  60. if (!$in_section) {
  61. my ($section_name) = $line =~ /SECTION: ([\w ]+)/;
  62. my $found_section = grep $_ eq $section_name, @sections;
  63. $in_section = 1 if ($found_section);
  64. }
  65. };
  66. $feature_format =~ s/FEATURE_DEFINES\n/$feature_defines/g;
  67. open(ERROR_FILE, ">$feature_file") or die "Opening destination file '$feature_file': $!";
  68. print ERROR_FILE $feature_format;
  69. close(ERROR_FILE);