generate_query_config.pl 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475
  1. #! /usr/bin/env perl
  2. # Generate query_config.c
  3. #
  4. # The file query_config.c contains a C function that can be used to check if
  5. # a configuration macro is defined and to retrieve its expansion in string
  6. # form (if any). This facilitates querying the compile time configuration of
  7. # the library, for example, for testing.
  8. #
  9. # The query_config.c is generated from the current configuration at
  10. # include/mbedtls/config.h. The idea is that the config.h contains ALL the
  11. # compile time configurations available in Mbed TLS (commented or uncommented).
  12. # This script extracts the configuration macros from the config.h and this
  13. # information is used to automatically generate the body of the query_config()
  14. # function by using the template in scripts/data_files/query_config.fmt.
  15. #
  16. # Usage: ./scripts/generate_query_config.pl without arguments
  17. use strict;
  18. my $config_file = "./include/mbedtls/config.h";
  19. my $query_config_format_file = "./scripts/data_files/query_config.fmt";
  20. my $query_config_file = "./programs/ssl/query_config.c";
  21. # Excluded macros from the generated query_config.c. For example, macros that
  22. # have commas or function-like macros cannot be transformed into strings easily
  23. # using the preprocessor, so they should be excluded or the preprocessor will
  24. # throw errors.
  25. my @excluded = qw(
  26. MBEDTLS_SSL_CIPHERSUITES
  27. MBEDTLS_PARAM_FAILED
  28. );
  29. my $excluded_re = join '|', @excluded;
  30. open(CONFIG_FILE, "$config_file") or die "Opening config file '$config_file': $!";
  31. # This variable will contain the string to replace in the CHECK_CONFIG of the
  32. # format file
  33. my $config_check = "";
  34. while (my $line = <CONFIG_FILE>) {
  35. if ($line =~ /^(\/\/)?\s*#\s*define\s+(MBEDTLS_\w+).*/) {
  36. my $name = $2;
  37. # Skip over the macro that prevents multiple inclusion
  38. next if "MBEDTLS_CONFIG_H" eq $name;
  39. # Skip over the macro if it is in the ecluded list
  40. next if $name =~ /$excluded_re/;
  41. $config_check .= "#if defined($name)\n";
  42. $config_check .= " if( strcmp( \"$name\", config ) == 0 )\n";
  43. $config_check .= " {\n";
  44. $config_check .= " MACRO_EXPANSION_TO_STR( $name );\n";
  45. $config_check .= " return( 0 );\n";
  46. $config_check .= " }\n";
  47. $config_check .= "#endif /* $name */\n";
  48. $config_check .= "\n";
  49. }
  50. }
  51. # Read the full format file into a string
  52. local $/;
  53. open(FORMAT_FILE, "$query_config_format_file") or die "Opening query config format file '$query_config_format_file': $!";
  54. my $query_config_format = <FORMAT_FILE>;
  55. close(FORMAT_FILE);
  56. # Replace the body of the query_config() function with the code we just wrote
  57. $query_config_format =~ s/CHECK_CONFIG/$config_check/g;
  58. # Rewrite the query_config.c file
  59. open(QUERY_CONFIG_FILE, ">$query_config_file") or die "Opening destination file '$query_config_file': $!";
  60. print QUERY_CONFIG_FILE $query_config_format;
  61. close(QUERY_CONFIG_FILE);