generate_query_config.pl 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131
  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 mbedtls_config_file template_file output_file [psa_crypto_config_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 ($mbedtls_config_file, $query_config_format_file, $query_config_file, $psa_crypto_config_file);
  35. my $default_mbedtls_config_file = "./include/mbedtls/mbedtls_config.h";
  36. my $default_query_config_format_file = "./scripts/data_files/query_config.fmt";
  37. my $default_query_config_file = "./programs/test/query_config.c";
  38. my $default_psa_crypto_config_file = "./include/psa/crypto_config.h";
  39. if( @ARGV ) {
  40. die "Invalid number of arguments - usage: $0 [CONFIG_FILE TEMPLATE_FILE OUTPUT_FILE]" if scalar @ARGV != 3;
  41. ($mbedtls_config_file, $query_config_format_file, $query_config_file) = @ARGV;
  42. -f $mbedtls_config_file or die "No such file: $mbedtls_config_file";
  43. -f $query_config_format_file or die "No such file: $query_config_format_file";
  44. if (defined($psa_crypto_config_file) && length($psa_crypto_config_file)) {
  45. -f $psa_crypto_config_file or die "No such file: $psa_crypto_config_file";
  46. } else {
  47. $psa_crypto_config_file = (-f $default_psa_crypto_config_file) ? $default_psa_crypto_config_file : undef;
  48. }
  49. } else {
  50. $mbedtls_config_file = $default_mbedtls_config_file;
  51. $query_config_format_file = $default_query_config_format_file;
  52. $query_config_file = $default_query_config_file;
  53. $psa_crypto_config_file = $default_psa_crypto_config_file;
  54. unless(-f $mbedtls_config_file && -f $query_config_format_file && -f $psa_crypto_config_file) {
  55. chdir '..' or die;
  56. -f $mbedtls_config_file && -f $query_config_format_file && -f $psa_crypto_config_file
  57. or die "No arguments supplied, must be run from project root or a first-level subdirectory\n";
  58. }
  59. }
  60. # Excluded macros from the generated query_config.c. For example, macros that
  61. # have commas or function-like macros cannot be transformed into strings easily
  62. # using the preprocessor, so they should be excluded or the preprocessor will
  63. # throw errors.
  64. my @excluded = qw(
  65. MBEDTLS_SSL_CIPHERSUITES
  66. );
  67. my $excluded_re = join '|', @excluded;
  68. # This variable will contain the string to replace in the CHECK_CONFIG of the
  69. # format file
  70. my $config_check = "";
  71. my $list_config = "";
  72. for my $config_file ($mbedtls_config_file, $psa_crypto_config_file) {
  73. next unless defined($config_file); # we might not have been given a PSA crypto config file
  74. open(CONFIG_FILE, "<", $config_file) or die "Opening config file '$config_file': $!";
  75. while (my $line = <CONFIG_FILE>) {
  76. if ($line =~ /^(\/\/)?\s*#\s*define\s+(MBEDTLS_\w+|PSA_WANT_\w+).*/) {
  77. my $name = $2;
  78. # Skip over the macro if it is in the excluded list
  79. next if $name =~ /$excluded_re/;
  80. $config_check .= <<EOT;
  81. #if defined($name)
  82. if( strcmp( "$name", config ) == 0 )
  83. {
  84. MACRO_EXPANSION_TO_STR( $name );
  85. return( 0 );
  86. }
  87. #endif /* $name */
  88. EOT
  89. $list_config .= <<EOT;
  90. #if defined($name)
  91. OUTPUT_MACRO_NAME_VALUE($name);
  92. #endif /* $name */
  93. EOT
  94. }
  95. }
  96. close(CONFIG_FILE);
  97. }
  98. # Read the full format file into a string
  99. local $/;
  100. open(FORMAT_FILE, "<", $query_config_format_file) or die "Opening query config format file '$query_config_format_file': $!";
  101. my $query_config_format = <FORMAT_FILE>;
  102. close(FORMAT_FILE);
  103. # Replace the body of the query_config() function with the code we just wrote
  104. $query_config_format =~ s/CHECK_CONFIG/$config_check/g;
  105. $query_config_format =~ s/LIST_CONFIG/$list_config/g;
  106. # Rewrite the query_config.c file
  107. open(QUERY_CONFIG_FILE, ">", $query_config_file) or die "Opening destination file '$query_config_file': $!";
  108. print QUERY_CONFIG_FILE $query_config_format;
  109. close(QUERY_CONFIG_FILE);