generate_features.pl 2.0 KB

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