config.pl 7.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252
  1. #!/usr/bin/perl
  2. #
  3. # This file is part of mbed TLS (https://tls.mbed.org)
  4. #
  5. # Copyright (c) 2014-2016, ARM Limited, All Rights Reserved
  6. #
  7. # Purpose
  8. #
  9. # Comments and uncomments #define lines in the given header file and optionally
  10. # sets their value or can get the value. This is to provide scripting control of
  11. # what preprocessor symbols, and therefore what build time configuration flags
  12. # are set in the 'config.h' file.
  13. #
  14. # Usage: config.pl [-f <file> | --file <file>] [-o | --force]
  15. # [set <symbol> <value> | unset <symbol> | get <symbol> |
  16. # full | realfull]
  17. #
  18. # Full usage description provided below.
  19. #
  20. # Things that shouldn't be enabled with "full".
  21. #
  22. # MBEDTLS_TEST_NULL_ENTROPY
  23. # MBEDTLS_DEPRECATED_REMOVED
  24. # MBEDTLS_HAVE_SSE2
  25. # MBEDTLS_PLATFORM_NO_STD_FUNCTIONS
  26. # MBEDTLS_ECP_DP_M221_ENABLED
  27. # MBEDTLS_ECP_DP_M383_ENABLED
  28. # MBEDTLS_ECP_DP_M511_ENABLED
  29. # MBEDTLS_NO_DEFAULT_ENTROPY_SOURCES
  30. # MBEDTLS_NO_PLATFORM_ENTROPY
  31. # MBEDTLS_REMOVE_ARC4_CIPHERSUITES
  32. # MBEDTLS_SSL_HW_RECORD_ACCEL
  33. # MBEDTLS_X509_ALLOW_EXTENSIONS_NON_V3
  34. # MBEDTLS_X509_ALLOW_UNSUPPORTED_CRITICAL_EXTENSION
  35. # - this could be enabled if the respective tests were adapted
  36. # MBEDTLS_ZLIB_SUPPORT
  37. # MBEDTLS_PKCS11_C
  38. # and any symbol beginning _ALT
  39. #
  40. use warnings;
  41. use strict;
  42. my $config_file = "include/mbedtls/config.h";
  43. my $usage = <<EOU;
  44. $0 [-f <file> | --file <file>] [-o | --force]
  45. [set <symbol> <value> | unset <symbol> | get <symbol> |
  46. full | realfull]
  47. Commands
  48. set <symbol> [<value>] - Uncomments or adds a #define for the <symbol> to
  49. the configuration file, and optionally making it
  50. of <value>.
  51. If the symbol isn't present in the file an error
  52. is returned.
  53. unset <symbol> - Comments out the #define for the given symbol if
  54. present in the configuration file.
  55. get <symbol> - Finds the #define for the given symbol, returning
  56. an exitcode of 0 if the symbol is found, and -1 if
  57. not. The value of the symbol is output if one is
  58. specified in the configuration file.
  59. full - Uncomments all #define's in the configuration file
  60. excluding some reserved symbols, until the
  61. 'Module configuration options' section
  62. realfull - Uncomments all #define's with no exclusions
  63. Options
  64. -f | --file <filename> - The file or file path for the configuration file
  65. to edit. When omitted, the following default is
  66. used:
  67. $config_file
  68. -o | --force - If the symbol isn't present in the configuration
  69. file when setting its value, a #define is
  70. appended to the end of the file.
  71. EOU
  72. my @excluded = qw(
  73. MBEDTLS_TEST_NULL_ENTROPY
  74. MBEDTLS_DEPRECATED_REMOVED
  75. MBEDTLS_HAVE_SSE2
  76. MBEDTLS_PLATFORM_NO_STD_FUNCTIONS
  77. MBEDTLS_ECP_DP_M221_ENABLED
  78. MBEDTLS_ECP_DP_M383_ENABLED
  79. MBEDTLS_ECP_DP_M511_ENABLED
  80. MBEDTLS_NO_DEFAULT_ENTROPY_SOURCES
  81. MBEDTLS_NO_PLATFORM_ENTROPY
  82. MBEDTLS_REMOVE_ARC4_CIPHERSUITES
  83. MBEDTLS_SSL_HW_RECORD_ACCEL
  84. MBEDTLS_X509_ALLOW_EXTENSIONS_NON_V3
  85. MBEDTLS_X509_ALLOW_UNSUPPORTED_CRITICAL_EXTENSION
  86. MBEDTLS_ZLIB_SUPPORT
  87. MBEDTLS_PKCS11_C
  88. _ALT\s*$
  89. );
  90. # Things that should be enabled in "full" even if they match @excluded
  91. my @non_excluded = qw(
  92. PLATFORM_[A-Z0-9]+_ALT
  93. );
  94. # Process the command line arguments
  95. my $force_option = 0;
  96. my ($arg, $name, $value, $action);
  97. while ($arg = shift) {
  98. # Check if the argument is an option
  99. if ($arg eq "-f" || $arg eq "--file") {
  100. $config_file = shift;
  101. -f $config_file or die "No such file: $config_file\n";
  102. }
  103. elsif ($arg eq "-o" || $arg eq "--force") {
  104. $force_option = 1;
  105. }
  106. else
  107. {
  108. # ...else assume it's a command
  109. $action = $arg;
  110. if ($action eq "full" || $action eq "realfull") {
  111. # No additional parameters
  112. die $usage if @ARGV;
  113. }
  114. elsif ($action eq "unset" || $action eq "get") {
  115. die $usage unless @ARGV;
  116. $name = shift;
  117. }
  118. elsif ($action eq "set") {
  119. die $usage unless @ARGV;
  120. $name = shift;
  121. $value = shift if @ARGV;
  122. }
  123. else {
  124. die "Command '$action' not recognised.\n\n".$usage;
  125. }
  126. }
  127. }
  128. # If no command was specified, exit...
  129. if ( not defined($action) ){ die $usage; }
  130. # Check the config file is present
  131. if (! -f $config_file) {
  132. chdir '..' or die;
  133. # Confirm this is the project root directory and try again
  134. if ( !(-d 'scripts' && -d 'include' && -d 'library' && -f $config_file) ) {
  135. die "If no file specified, must be run from the project root or scripts directory.\n";
  136. }
  137. }
  138. # Now read the file and process the contents
  139. open my $config_read, '<', $config_file or die "read $config_file: $!\n";
  140. my @config_lines = <$config_read>;
  141. close $config_read;
  142. my ($exclude_re, $no_exclude_re);
  143. if ($action eq "realfull") {
  144. $exclude_re = qr/^$/;
  145. $no_exclude_re = qr/./;
  146. } else {
  147. $exclude_re = join '|', @excluded;
  148. $no_exclude_re = join '|', @non_excluded;
  149. }
  150. open my $config_write, '>', $config_file or die "write $config_file: $!\n";
  151. my $done;
  152. for my $line (@config_lines) {
  153. if ($action eq "full" || $action eq "realfull") {
  154. if ($line =~ /name SECTION: Module configuration options/) {
  155. $done = 1;
  156. }
  157. if (!$done && $line =~ m!^//\s?#define! &&
  158. ( $line !~ /$exclude_re/ || $line =~ /$no_exclude_re/ ) ) {
  159. $line =~ s!^//\s?!!;
  160. }
  161. if (!$done && $line =~ m!^\s?#define! &&
  162. ! ( $line !~ /$exclude_re/ || $line =~ /$no_exclude_re/ ) ) {
  163. $line =~ s!^!//!;
  164. }
  165. } elsif ($action eq "unset") {
  166. if (!$done && $line =~ /^\s*#define\s*$name\b/) {
  167. $line = '//' . $line;
  168. $done = 1;
  169. }
  170. } elsif (!$done && $action eq "set") {
  171. if ($line =~ m!^(?://)?\s*#define\s*$name\b!) {
  172. $line = "#define $name";
  173. $line .= " $value" if defined $value && $value ne "";
  174. $line .= "\n";
  175. $done = 1;
  176. }
  177. } elsif (!$done && $action eq "get") {
  178. if ($line =~ /^\s*#define\s*$name\s*(.*)\s*\b/) {
  179. $value = $1;
  180. $done = 1;
  181. }
  182. }
  183. print $config_write $line;
  184. }
  185. # Did the set command work?
  186. if ($action eq "set"&& $force_option && !$done) {
  187. # If the force option was set, append the symbol to the end of the file
  188. my $line = "#define $name";
  189. $line .= " $value" if defined $value && $value ne "";
  190. $line .= "\n";
  191. $done = 1;
  192. print $config_write $line;
  193. }
  194. close $config_write;
  195. if ($action eq "get") {
  196. if($done) {
  197. if ($value ne '') {
  198. print $value;
  199. }
  200. exit 0;
  201. } else {
  202. # If the symbol was not found, return an error
  203. exit -1;
  204. }
  205. }
  206. if ($action eq "full" && !$done) {
  207. die "Configuration section was not found in $config_file\n";
  208. }
  209. if ($action ne "full" && $action ne "unset" && !$done) {
  210. die "A #define for the symbol $name was not found in $config_file\n";
  211. }
  212. __END__