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