generate_query_config.pl 3.4 KB

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