generate_query_config.pl 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118
  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 OR GPL-2.0-or-later
  20. #
  21. # This file is provided under the Apache License 2.0, or the
  22. # GNU General Public License v2.0 or later.
  23. #
  24. # **********
  25. # Apache License 2.0:
  26. #
  27. # Licensed under the Apache License, Version 2.0 (the "License"); you may
  28. # not use this file except in compliance with the License.
  29. # You may obtain a copy of the License at
  30. #
  31. # http://www.apache.org/licenses/LICENSE-2.0
  32. #
  33. # Unless required by applicable law or agreed to in writing, software
  34. # distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
  35. # WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  36. # See the License for the specific language governing permissions and
  37. # limitations under the License.
  38. #
  39. # **********
  40. #
  41. # **********
  42. # GNU General Public License v2.0 or later:
  43. #
  44. # This program is free software; you can redistribute it and/or modify
  45. # it under the terms of the GNU General Public License as published by
  46. # the Free Software Foundation; either version 2 of the License, or
  47. # (at your option) any later version.
  48. #
  49. # This program is distributed in the hope that it will be useful,
  50. # but WITHOUT ANY WARRANTY; without even the implied warranty of
  51. # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  52. # GNU General Public License for more details.
  53. #
  54. # You should have received a copy of the GNU General Public License along
  55. # with this program; if not, write to the Free Software Foundation, Inc.,
  56. # 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
  57. #
  58. # **********
  59. use strict;
  60. my $config_file = "./include/mbedtls/config.h";
  61. my $query_config_format_file = "./scripts/data_files/query_config.fmt";
  62. my $query_config_file = "./programs/ssl/query_config.c";
  63. # Excluded macros from the generated query_config.c. For example, macros that
  64. # have commas or function-like macros cannot be transformed into strings easily
  65. # using the preprocessor, so they should be excluded or the preprocessor will
  66. # throw errors.
  67. my @excluded = qw(
  68. MBEDTLS_SSL_CIPHERSUITES
  69. MBEDTLS_PARAM_FAILED
  70. );
  71. my $excluded_re = join '|', @excluded;
  72. open(CONFIG_FILE, "$config_file") or die "Opening config file '$config_file': $!";
  73. # This variable will contain the string to replace in the CHECK_CONFIG of the
  74. # format file
  75. my $config_check = "";
  76. while (my $line = <CONFIG_FILE>) {
  77. if ($line =~ /^(\/\/)?\s*#\s*define\s+(MBEDTLS_\w+).*/) {
  78. my $name = $2;
  79. # Skip over the macro that prevents multiple inclusion
  80. next if "MBEDTLS_CONFIG_H" eq $name;
  81. # Skip over the macro if it is in the ecluded list
  82. next if $name =~ /$excluded_re/;
  83. $config_check .= "#if defined($name)\n";
  84. $config_check .= " if( strcmp( \"$name\", config ) == 0 )\n";
  85. $config_check .= " {\n";
  86. $config_check .= " MACRO_EXPANSION_TO_STR( $name );\n";
  87. $config_check .= " return( 0 );\n";
  88. $config_check .= " }\n";
  89. $config_check .= "#endif /* $name */\n";
  90. $config_check .= "\n";
  91. }
  92. }
  93. # Read the full format file into a string
  94. local $/;
  95. open(FORMAT_FILE, "$query_config_format_file") or die "Opening query config format file '$query_config_format_file': $!";
  96. my $query_config_format = <FORMAT_FILE>;
  97. close(FORMAT_FILE);
  98. # Replace the body of the query_config() function with the code we just wrote
  99. $query_config_format =~ s/CHECK_CONFIG/$config_check/g;
  100. # Rewrite the query_config.c file
  101. open(QUERY_CONFIG_FILE, ">$query_config_file") or die "Opening destination file '$query_config_file': $!";
  102. print QUERY_CONFIG_FILE $query_config_format;
  103. close(QUERY_CONFIG_FILE);