generate_query_config.pl 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102
  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/mbedtls_config.h. The idea is that the mbedtls_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 mbedtls_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, or
  17. # generate_query_config.pl config_file template_file output_file
  18. #
  19. # Copyright The Mbed TLS Contributors
  20. # SPDX-License-Identifier: Apache-2.0
  21. #
  22. # Licensed under the Apache License, Version 2.0 (the "License"); you may
  23. # not use this file except in compliance with the License.
  24. # You may obtain a copy of the License at
  25. #
  26. # http://www.apache.org/licenses/LICENSE-2.0
  27. #
  28. # Unless required by applicable law or agreed to in writing, software
  29. # distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
  30. # WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  31. # See the License for the specific language governing permissions and
  32. # limitations under the License.
  33. use strict;
  34. my ($config_file, $query_config_format_file, $query_config_file);
  35. if( @ARGV ) {
  36. die "Invalid number of arguments - usage: $0 [CONFIG_FILE TEMPLATE_FILE OUTPUT_FILE]" if scalar @ARGV != 3;
  37. ($config_file, $query_config_format_file, $query_config_file) = @ARGV;
  38. -f $config_file or die "No such file: $config_file";
  39. -f $query_config_format_file or die "No such file: $query_config_format_file";
  40. } else {
  41. $config_file = "./include/mbedtls/mbedtls_config.h";
  42. $query_config_format_file = "./scripts/data_files/query_config.fmt";
  43. $query_config_file = "./programs/test/query_config.c";
  44. unless( -f $config_file && -f $query_config_format_file ) {
  45. chdir '..' or die;
  46. -f $config_file && -f $query_config_format_file
  47. or die "No arguments supplied, must be run from project root or a first-level subdirectory\n";
  48. }
  49. }
  50. # Excluded macros from the generated query_config.c. For example, macros that
  51. # have commas or function-like macros cannot be transformed into strings easily
  52. # using the preprocessor, so they should be excluded or the preprocessor will
  53. # throw errors.
  54. my @excluded = qw(
  55. MBEDTLS_SSL_CIPHERSUITES
  56. );
  57. my $excluded_re = join '|', @excluded;
  58. open(CONFIG_FILE, "$config_file") or die "Opening config file '$config_file': $!";
  59. # This variable will contain the string to replace in the CHECK_CONFIG of the
  60. # format file
  61. my $config_check = "";
  62. while (my $line = <CONFIG_FILE>) {
  63. if ($line =~ /^(\/\/)?\s*#\s*define\s+(MBEDTLS_\w+).*/) {
  64. my $name = $2;
  65. # Skip over the macro if it is in the ecluded list
  66. next if $name =~ /$excluded_re/;
  67. $config_check .= "#if defined($name)\n";
  68. $config_check .= " if( strcmp( \"$name\", config ) == 0 )\n";
  69. $config_check .= " {\n";
  70. $config_check .= " MACRO_EXPANSION_TO_STR( $name );\n";
  71. $config_check .= " return( 0 );\n";
  72. $config_check .= " }\n";
  73. $config_check .= "#endif /* $name */\n";
  74. $config_check .= "\n";
  75. }
  76. }
  77. # Read the full format file into a string
  78. local $/;
  79. open(FORMAT_FILE, "$query_config_format_file") or die "Opening query config format file '$query_config_format_file': $!";
  80. my $query_config_format = <FORMAT_FILE>;
  81. close(FORMAT_FILE);
  82. # Replace the body of the query_config() function with the code we just wrote
  83. $query_config_format =~ s/CHECK_CONFIG/$config_check/g;
  84. # Rewrite the query_config.c file
  85. open(QUERY_CONFIG_FILE, ">$query_config_file") or die "Opening destination file '$query_config_file': $!";
  86. print QUERY_CONFIG_FILE $query_config_format;
  87. close(QUERY_CONFIG_FILE);