Manuel Pégourié-Gonnard | ab3d862 | 2014-07-12 03:19:18 +0200 | [diff] [blame] | 1 | #!/usr/bin/perl |
Azim Khan | 2339966 | 2017-12-21 09:28:39 +0000 | [diff] [blame] | 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 | # POLARSSL_TEST_NULL_ENTROPY |
| 23 | # POLARSSL_DEPRECATED_REMOVED |
| 24 | # POLARSSL_HAVE_SSE2 |
| 25 | # POLARSSL_PLATFORM_NO_STD_FUNCTIONS |
| 26 | # POLARSSL_ECP_DP_M221_ENABLED |
| 27 | # POLARSSL_ECP_DP_M383_ENABLED |
| 28 | # POLARSSL_ECP_DP_M511_ENABLED |
| 29 | # POLARSSL_NO_DEFAULT_ENTROPY_SOURCES |
| 30 | # POLARSSL_NO_PLATFORM_ENTROPY |
| 31 | # POLARSSL_REMOVE_ARC4_CIPHERSUITES |
| 32 | # POLARSSL_SSL_HW_RECORD_ACCEL |
| 33 | # POLARSSL_X509_ALLOW_EXTENSIONS_NON_V3 |
| 34 | # POLARSSL_X509_ALLOW_UNSUPPORTED_CRITICAL_EXTENSION |
| 35 | # - this could be enabled if the respective tests were adapted |
| 36 | # POLARSSL_ZLIB_SUPPORT |
| 37 | # POLARSSL_PKCS11_C |
| 38 | # and any symbol beginning _ALT |
| 39 | # |
Manuel Pégourié-Gonnard | ab3d862 | 2014-07-12 03:19:18 +0200 | [diff] [blame] | 40 | |
| 41 | use warnings; |
| 42 | use strict; |
| 43 | |
Azim Khan | 2339966 | 2017-12-21 09:28:39 +0000 | [diff] [blame] | 44 | my $config_file = "include/polarssl/config.h"; |
Manuel Pégourié-Gonnard | ab3d862 | 2014-07-12 03:19:18 +0200 | [diff] [blame] | 45 | my $usage = <<EOU; |
Azim Khan | 2339966 | 2017-12-21 09:28:39 +0000 | [diff] [blame] | 46 | $0 [-f <file> | --file <file>] [-o | --force] |
| 47 | [set <symbol> <value> | unset <symbol> | get <symbol> | |
| 48 | full | realfull | baremetal] |
Manuel Pégourié-Gonnard | ab3d862 | 2014-07-12 03:19:18 +0200 | [diff] [blame] | 49 | |
Azim Khan | 2339966 | 2017-12-21 09:28:39 +0000 | [diff] [blame] | 50 | Commands |
| 51 | set <symbol> [<value>] - Uncomments or adds a #define for the <symbol> to |
| 52 | the configuration file, and optionally making it |
| 53 | of <value>. |
| 54 | If the symbol isn't present in the file an error |
| 55 | is returned. |
| 56 | unset <symbol> - Comments out the #define for the given symbol if |
| 57 | present in the configuration file. |
| 58 | get <symbol> - Finds the #define for the given symbol, returning |
| 59 | an exitcode of 0 if the symbol is found, and 1 if |
| 60 | not. The value of the symbol is output if one is |
| 61 | specified in the configuration file. |
| 62 | full - Uncomments all #define's in the configuration file |
| 63 | excluding some reserved symbols, until the |
| 64 | 'Module configuration options' section |
| 65 | realfull - Uncomments all #define's with no exclusions |
| 66 | baremetal - Sets full configuration suitable for baremetal build. |
| 67 | |
| 68 | Options |
| 69 | -f | --file <filename> - The file or file path for the configuration file |
| 70 | to edit. When omitted, the following default is |
| 71 | used: |
| 72 | $config_file |
| 73 | -o | --force - If the symbol isn't present in the configuration |
| 74 | file when setting its value, a #define is |
| 75 | appended to the end of the file. |
| 76 | |
| 77 | EOU |
| 78 | |
Manuel Pégourié-Gonnard | ab3d862 | 2014-07-12 03:19:18 +0200 | [diff] [blame] | 79 | my @excluded = qw( |
Manuel Pégourié-Gonnard | 4d9e36a | 2015-09-02 10:10:32 +0200 | [diff] [blame] | 80 | POLARSSL_ERROR_STRERROR_BC |
| 81 | POLARSSL_MEMORY_C |
| 82 | POLARSSL_PBKDF2_C |
Manuel Pégourié-Gonnard | ab3d862 | 2014-07-12 03:19:18 +0200 | [diff] [blame] | 83 | POLARSSL_HAVE_INT8 |
| 84 | POLARSSL_HAVE_INT16 |
| 85 | POLARSSL_HAVE_SSE2 |
| 86 | POLARSSL_PLATFORM_NO_STD_FUNCTIONS |
| 87 | POLARSSL_ECP_DP_M221_ENABLED |
| 88 | POLARSSL_ECP_DP_M383_ENABLED |
| 89 | POLARSSL_ECP_DP_M511_ENABLED |
| 90 | POLARSSL_NO_DEFAULT_ENTROPY_SOURCES |
| 91 | POLARSSL_NO_PLATFORM_ENTROPY |
| 92 | POLARSSL_SSL_HW_RECORD_ACCEL |
Manuel Pégourié-Gonnard | 86b2908 | 2014-11-06 02:28:34 +0100 | [diff] [blame] | 93 | POLARSSL_SSL_DISABLE_RENEGOTIATION |
Manuel Pégourié-Gonnard | ab3d862 | 2014-07-12 03:19:18 +0200 | [diff] [blame] | 94 | POLARSSL_X509_ALLOW_EXTENSIONS_NON_V3 |
| 95 | POLARSSL_X509_ALLOW_UNSUPPORTED_CRITICAL_EXTENSION |
| 96 | POLARSSL_ZLIB_SUPPORT |
| 97 | POLARSSL_PKCS11_C |
| 98 | _ALT\s*$ |
| 99 | ); |
| 100 | |
Azim Khan | 2339966 | 2017-12-21 09:28:39 +0000 | [diff] [blame] | 101 | # Things that should be disabled in "baremetal" |
| 102 | my @excluded_baremetal = qw( |
| 103 | POLARSSL_NET_C |
| 104 | POLARSSL_TIMING_C |
| 105 | POLARSSL_FS_IO |
| 106 | POLARSSL_ENTROPY_NV_SEED |
| 107 | POLARSSL_HAVE_TIME |
| 108 | POLARSSL_HAVE_TIME_DATE |
| 109 | POLARSSL_DEPRECATED_WARNING |
| 110 | POLARSSL_HAVEGE_C |
| 111 | POLARSSL_THREADING_C |
| 112 | POLARSSL_THREADING_PTHREAD |
| 113 | POLARSSL_MEMORY_BACKTRACE |
| 114 | POLARSSL_MEMORY_BUFFER_ALLOC_C |
| 115 | POLARSSL_PLATFORM_TIME_ALT |
| 116 | POLARSSL_PLATFORM_FPRINTF_ALT |
| 117 | ); |
| 118 | |
Manuel Pégourié-Gonnard | b752715 | 2015-06-03 09:59:06 +0100 | [diff] [blame] | 119 | # Things that should be enabled in "full" even if they match @excluded |
| 120 | my @non_excluded = qw( |
| 121 | PLATFORM_[A-Z0-9]+_ALT |
| 122 | ); |
| 123 | |
Azim Khan | 2339966 | 2017-12-21 09:28:39 +0000 | [diff] [blame] | 124 | # Things that should be enabled in "baremetal" |
| 125 | my @non_excluded_baremetal = qw( |
| 126 | POLARSSL_NO_PLATFORM_ENTROPY |
| 127 | ); |
Manuel Pégourié-Gonnard | ab3d862 | 2014-07-12 03:19:18 +0200 | [diff] [blame] | 128 | |
Azim Khan | 2339966 | 2017-12-21 09:28:39 +0000 | [diff] [blame] | 129 | # Process the command line arguments |
Manuel Pégourié-Gonnard | ab3d862 | 2014-07-12 03:19:18 +0200 | [diff] [blame] | 130 | |
Azim Khan | 2339966 | 2017-12-21 09:28:39 +0000 | [diff] [blame] | 131 | my $force_option = 0; |
| 132 | |
| 133 | my ($arg, $name, $value, $action); |
| 134 | |
| 135 | while ($arg = shift) { |
| 136 | |
| 137 | # Check if the argument is an option |
| 138 | if ($arg eq "-f" || $arg eq "--file") { |
| 139 | $config_file = shift; |
| 140 | |
| 141 | -f $config_file or die "No such file: $config_file\n"; |
| 142 | |
| 143 | } |
| 144 | elsif ($arg eq "-o" || $arg eq "--force") { |
| 145 | $force_option = 1; |
| 146 | |
| 147 | } |
| 148 | else |
| 149 | { |
| 150 | # ...else assume it's a command |
| 151 | $action = $arg; |
| 152 | |
| 153 | if ($action eq "full" || $action eq "realfull" || $action eq "baremetal" ) { |
| 154 | # No additional parameters |
| 155 | die $usage if @ARGV; |
| 156 | |
| 157 | } |
| 158 | elsif ($action eq "unset" || $action eq "get") { |
| 159 | die $usage unless @ARGV; |
| 160 | $name = shift; |
| 161 | |
| 162 | } |
| 163 | elsif ($action eq "set") { |
| 164 | die $usage unless @ARGV; |
| 165 | $name = shift; |
| 166 | $value = shift if @ARGV; |
| 167 | |
| 168 | } |
| 169 | else { |
| 170 | die "Command '$action' not recognised.\n\n".$usage; |
| 171 | } |
Manuel Pégourié-Gonnard | ab3d862 | 2014-07-12 03:19:18 +0200 | [diff] [blame] | 172 | } |
| 173 | } |
| 174 | |
Azim Khan | 2339966 | 2017-12-21 09:28:39 +0000 | [diff] [blame] | 175 | # If no command was specified, exit... |
| 176 | if ( not defined($action) ){ die $usage; } |
Manuel Pégourié-Gonnard | ab3d862 | 2014-07-12 03:19:18 +0200 | [diff] [blame] | 177 | |
Azim Khan | 2339966 | 2017-12-21 09:28:39 +0000 | [diff] [blame] | 178 | # Check the config file is present |
| 179 | if (! -f $config_file) { |
| 180 | |
| 181 | chdir '..' or die; |
| 182 | |
| 183 | # Confirm this is the project root directory and try again |
| 184 | if ( !(-d 'scripts' && -d 'include' && -d 'library' && -f $config_file) ) { |
| 185 | die "If no file specified, must be run from the project root or scripts directory.\n"; |
| 186 | } |
Manuel Pégourié-Gonnard | ab3d862 | 2014-07-12 03:19:18 +0200 | [diff] [blame] | 187 | } |
Azim Khan | 2339966 | 2017-12-21 09:28:39 +0000 | [diff] [blame] | 188 | |
| 189 | |
| 190 | # Now read the file and process the contents |
Manuel Pégourié-Gonnard | ab3d862 | 2014-07-12 03:19:18 +0200 | [diff] [blame] | 191 | |
| 192 | open my $config_read, '<', $config_file or die "read $config_file: $!\n"; |
| 193 | my @config_lines = <$config_read>; |
| 194 | close $config_read; |
| 195 | |
Azim Khan | 2339966 | 2017-12-21 09:28:39 +0000 | [diff] [blame] | 196 | # Add required baremetal symbols to the list that is included. |
| 197 | if ( $action eq "baremetal" ) { |
| 198 | @non_excluded = ( @non_excluded, @non_excluded_baremetal ); |
| 199 | } |
Manuel Pégourié-Gonnard | ab3d862 | 2014-07-12 03:19:18 +0200 | [diff] [blame] | 200 | |
Azim Khan | 2339966 | 2017-12-21 09:28:39 +0000 | [diff] [blame] | 201 | my ($exclude_re, $no_exclude_re, $exclude_baremetal_re); |
| 202 | if ($action eq "realfull") { |
| 203 | $exclude_re = qr/^$/; |
| 204 | $no_exclude_re = qr/./; |
| 205 | } else { |
| 206 | $exclude_re = join '|', @excluded; |
| 207 | $no_exclude_re = join '|', @non_excluded; |
| 208 | } |
| 209 | if ( $action eq "baremetal" ) { |
| 210 | $exclude_baremetal_re = join '|', @excluded_baremetal; |
| 211 | } |
| 212 | |
| 213 | my $config_write = undef; |
| 214 | if ($action ne "get") { |
| 215 | open $config_write, '>', $config_file or die "write $config_file: $!\n"; |
| 216 | } |
Manuel Pégourié-Gonnard | ab3d862 | 2014-07-12 03:19:18 +0200 | [diff] [blame] | 217 | |
| 218 | my $done; |
| 219 | for my $line (@config_lines) { |
Azim Khan | 2339966 | 2017-12-21 09:28:39 +0000 | [diff] [blame] | 220 | if ($action eq "full" || $action eq "realfull" || $action eq "baremetal" ) { |
Manuel Pégourié-Gonnard | ab3d862 | 2014-07-12 03:19:18 +0200 | [diff] [blame] | 221 | if ($line =~ /name SECTION: Module configuration options/) { |
| 222 | $done = 1; |
| 223 | } |
| 224 | |
Manuel Pégourié-Gonnard | b752715 | 2015-06-03 09:59:06 +0100 | [diff] [blame] | 225 | if (!$done && $line =~ m!^//\s?#define! && |
Azim Khan | 2339966 | 2017-12-21 09:28:39 +0000 | [diff] [blame] | 226 | ( $line !~ /$exclude_re/ || $line =~ /$no_exclude_re/ ) && |
| 227 | ( $action ne "baremetal" || ( $line !~ /$exclude_baremetal_re/ ) ) ) { |
| 228 | $line =~ s!^//\s?!!; |
| 229 | } |
| 230 | if (!$done && $line =~ m!^\s?#define! && |
| 231 | ! ( ( $line !~ /$exclude_re/ || $line =~ /$no_exclude_re/ ) && |
| 232 | ( $action ne "baremetal" || ( $line !~ /$exclude_baremetal_re/ ) ) ) ) { |
| 233 | $line =~ s!^!//!; |
Manuel Pégourié-Gonnard | ab3d862 | 2014-07-12 03:19:18 +0200 | [diff] [blame] | 234 | } |
| 235 | } elsif ($action eq "unset") { |
Azim Khan | 2339966 | 2017-12-21 09:28:39 +0000 | [diff] [blame] | 236 | if (!$done && $line =~ /^\s*#define\s*$name\b/) { |
Manuel Pégourié-Gonnard | ab3d862 | 2014-07-12 03:19:18 +0200 | [diff] [blame] | 237 | $line = '//' . $line; |
| 238 | $done = 1; |
| 239 | } |
| 240 | } elsif (!$done && $action eq "set") { |
Azim Khan | 2339966 | 2017-12-21 09:28:39 +0000 | [diff] [blame] | 241 | if ($line =~ m!^(?://)?\s*#define\s*$name\b!) { |
Manuel Pégourié-Gonnard | ab3d862 | 2014-07-12 03:19:18 +0200 | [diff] [blame] | 242 | $line = "#define $name"; |
| 243 | $line .= " $value" if defined $value && $value ne ""; |
| 244 | $line .= "\n"; |
| 245 | $done = 1; |
| 246 | } |
Azim Khan | 2339966 | 2017-12-21 09:28:39 +0000 | [diff] [blame] | 247 | } elsif (!$done && $action eq "get") { |
| 248 | if ($line =~ /^\s*#define\s*$name(?:\s+(.*?))\s*(?:$|\/\*|\/\/)/) { |
| 249 | $value = $1; |
| 250 | $done = 1; |
| 251 | } |
Manuel Pégourié-Gonnard | ab3d862 | 2014-07-12 03:19:18 +0200 | [diff] [blame] | 252 | } |
| 253 | |
Azim Khan | 2339966 | 2017-12-21 09:28:39 +0000 | [diff] [blame] | 254 | if (defined $config_write) { |
| 255 | print $config_write $line or die "write $config_file: $!\n"; |
| 256 | } |
Manuel Pégourié-Gonnard | ab3d862 | 2014-07-12 03:19:18 +0200 | [diff] [blame] | 257 | } |
| 258 | |
Azim Khan | 2339966 | 2017-12-21 09:28:39 +0000 | [diff] [blame] | 259 | # Did the set command work? |
| 260 | if ($action eq "set" && $force_option && !$done) { |
Manuel Pégourié-Gonnard | ab3d862 | 2014-07-12 03:19:18 +0200 | [diff] [blame] | 261 | |
Azim Khan | 2339966 | 2017-12-21 09:28:39 +0000 | [diff] [blame] | 262 | # If the force option was set, append the symbol to the end of the file |
| 263 | my $line = "#define $name"; |
| 264 | $line .= " $value" if defined $value && $value ne ""; |
| 265 | $line .= "\n"; |
| 266 | $done = 1; |
| 267 | |
| 268 | print $config_write $line or die "write $config_file: $!\n"; |
| 269 | } |
| 270 | |
| 271 | if (defined $config_write) { |
| 272 | close $config_write or die "close $config_file: $!\n"; |
| 273 | } |
| 274 | |
| 275 | if ($action eq "get") { |
| 276 | if ($done) { |
| 277 | if ($value ne '') { |
| 278 | print "$value\n"; |
| 279 | } |
| 280 | exit 0; |
| 281 | } else { |
| 282 | # If the symbol was not found, return an error |
| 283 | exit 1; |
| 284 | } |
| 285 | } |
| 286 | |
| 287 | if ($action eq "full" && !$done) { |
| 288 | die "Configuration section was not found in $config_file\n"; |
| 289 | |
| 290 | } |
| 291 | |
| 292 | if ($action ne "full" && $action ne "unset" && !$done) { |
| 293 | die "A #define for the symbol $name was not found in $config_file\n"; |
| 294 | } |
Manuel Pégourié-Gonnard | ab3d862 | 2014-07-12 03:19:18 +0200 | [diff] [blame] | 295 | |
| 296 | __END__ |