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