blob: fd8821165da7787788fb1cf866cab842ac4f1a65 [file] [log] [blame]
Manuel Pégourié-Gonnardab3d8622014-07-12 03:19:18 +02001#!/usr/bin/perl
Azim Khan23399662017-12-21 09:28:39 +00002#
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é-Gonnardab3d8622014-07-12 03:19:18 +020040
41use warnings;
42use strict;
43
Azim Khan23399662017-12-21 09:28:39 +000044my $config_file = "include/polarssl/config.h";
Manuel Pégourié-Gonnardab3d8622014-07-12 03:19:18 +020045my $usage = <<EOU;
Azim Khan23399662017-12-21 09:28:39 +000046$0 [-f <file> | --file <file>] [-o | --force]
47 [set <symbol> <value> | unset <symbol> | get <symbol> |
48 full | realfull | baremetal]
Manuel Pégourié-Gonnardab3d8622014-07-12 03:19:18 +020049
Azim Khan23399662017-12-21 09:28:39 +000050Commands
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
68Options
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
77EOU
78
Manuel Pégourié-Gonnardab3d8622014-07-12 03:19:18 +020079my @excluded = qw(
Manuel Pégourié-Gonnard4d9e36a2015-09-02 10:10:32 +020080POLARSSL_ERROR_STRERROR_BC
81POLARSSL_MEMORY_C
82POLARSSL_PBKDF2_C
Manuel Pégourié-Gonnardab3d8622014-07-12 03:19:18 +020083POLARSSL_HAVE_INT8
84POLARSSL_HAVE_INT16
85POLARSSL_HAVE_SSE2
86POLARSSL_PLATFORM_NO_STD_FUNCTIONS
87POLARSSL_ECP_DP_M221_ENABLED
88POLARSSL_ECP_DP_M383_ENABLED
89POLARSSL_ECP_DP_M511_ENABLED
90POLARSSL_NO_DEFAULT_ENTROPY_SOURCES
91POLARSSL_NO_PLATFORM_ENTROPY
92POLARSSL_SSL_HW_RECORD_ACCEL
Manuel Pégourié-Gonnard86b29082014-11-06 02:28:34 +010093POLARSSL_SSL_DISABLE_RENEGOTIATION
Manuel Pégourié-Gonnardab3d8622014-07-12 03:19:18 +020094POLARSSL_X509_ALLOW_EXTENSIONS_NON_V3
95POLARSSL_X509_ALLOW_UNSUPPORTED_CRITICAL_EXTENSION
96POLARSSL_ZLIB_SUPPORT
97POLARSSL_PKCS11_C
98_ALT\s*$
99);
100
Azim Khan23399662017-12-21 09:28:39 +0000101# Things that should be disabled in "baremetal"
102my @excluded_baremetal = qw(
103POLARSSL_NET_C
104POLARSSL_TIMING_C
105POLARSSL_FS_IO
106POLARSSL_ENTROPY_NV_SEED
107POLARSSL_HAVE_TIME
108POLARSSL_HAVE_TIME_DATE
109POLARSSL_DEPRECATED_WARNING
110POLARSSL_HAVEGE_C
111POLARSSL_THREADING_C
112POLARSSL_THREADING_PTHREAD
113POLARSSL_MEMORY_BACKTRACE
114POLARSSL_MEMORY_BUFFER_ALLOC_C
115POLARSSL_PLATFORM_TIME_ALT
116POLARSSL_PLATFORM_FPRINTF_ALT
117);
118
Manuel Pégourié-Gonnardb7527152015-06-03 09:59:06 +0100119# Things that should be enabled in "full" even if they match @excluded
120my @non_excluded = qw(
121PLATFORM_[A-Z0-9]+_ALT
122);
123
Azim Khan23399662017-12-21 09:28:39 +0000124# Things that should be enabled in "baremetal"
125my @non_excluded_baremetal = qw(
126POLARSSL_NO_PLATFORM_ENTROPY
127);
Manuel Pégourié-Gonnardab3d8622014-07-12 03:19:18 +0200128
Azim Khan23399662017-12-21 09:28:39 +0000129# Process the command line arguments
Manuel Pégourié-Gonnardab3d8622014-07-12 03:19:18 +0200130
Azim Khan23399662017-12-21 09:28:39 +0000131my $force_option = 0;
132
133my ($arg, $name, $value, $action);
134
135while ($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é-Gonnardab3d8622014-07-12 03:19:18 +0200172 }
173}
174
Azim Khan23399662017-12-21 09:28:39 +0000175# If no command was specified, exit...
176if ( not defined($action) ){ die $usage; }
Manuel Pégourié-Gonnardab3d8622014-07-12 03:19:18 +0200177
Azim Khan23399662017-12-21 09:28:39 +0000178# Check the config file is present
179if (! -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é-Gonnardab3d8622014-07-12 03:19:18 +0200187}
Azim Khan23399662017-12-21 09:28:39 +0000188
189
190# Now read the file and process the contents
Manuel Pégourié-Gonnardab3d8622014-07-12 03:19:18 +0200191
192open my $config_read, '<', $config_file or die "read $config_file: $!\n";
193my @config_lines = <$config_read>;
194close $config_read;
195
Azim Khan23399662017-12-21 09:28:39 +0000196# Add required baremetal symbols to the list that is included.
197if ( $action eq "baremetal" ) {
198 @non_excluded = ( @non_excluded, @non_excluded_baremetal );
199}
Manuel Pégourié-Gonnardab3d8622014-07-12 03:19:18 +0200200
Azim Khan23399662017-12-21 09:28:39 +0000201my ($exclude_re, $no_exclude_re, $exclude_baremetal_re);
202if ($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}
209if ( $action eq "baremetal" ) {
210 $exclude_baremetal_re = join '|', @excluded_baremetal;
211}
212
213my $config_write = undef;
214if ($action ne "get") {
215 open $config_write, '>', $config_file or die "write $config_file: $!\n";
216}
Manuel Pégourié-Gonnardab3d8622014-07-12 03:19:18 +0200217
218my $done;
219for my $line (@config_lines) {
Azim Khan23399662017-12-21 09:28:39 +0000220 if ($action eq "full" || $action eq "realfull" || $action eq "baremetal" ) {
Manuel Pégourié-Gonnardab3d8622014-07-12 03:19:18 +0200221 if ($line =~ /name SECTION: Module configuration options/) {
222 $done = 1;
223 }
224
Manuel Pégourié-Gonnardb7527152015-06-03 09:59:06 +0100225 if (!$done && $line =~ m!^//\s?#define! &&
Azim Khan23399662017-12-21 09:28:39 +0000226 ( $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é-Gonnardab3d8622014-07-12 03:19:18 +0200234 }
235 } elsif ($action eq "unset") {
Azim Khan23399662017-12-21 09:28:39 +0000236 if (!$done && $line =~ /^\s*#define\s*$name\b/) {
Manuel Pégourié-Gonnardab3d8622014-07-12 03:19:18 +0200237 $line = '//' . $line;
238 $done = 1;
239 }
240 } elsif (!$done && $action eq "set") {
Azim Khan23399662017-12-21 09:28:39 +0000241 if ($line =~ m!^(?://)?\s*#define\s*$name\b!) {
Manuel Pégourié-Gonnardab3d8622014-07-12 03:19:18 +0200242 $line = "#define $name";
243 $line .= " $value" if defined $value && $value ne "";
244 $line .= "\n";
245 $done = 1;
246 }
Azim Khan23399662017-12-21 09:28:39 +0000247 } elsif (!$done && $action eq "get") {
248 if ($line =~ /^\s*#define\s*$name(?:\s+(.*?))\s*(?:$|\/\*|\/\/)/) {
249 $value = $1;
250 $done = 1;
251 }
Manuel Pégourié-Gonnardab3d8622014-07-12 03:19:18 +0200252 }
253
Azim Khan23399662017-12-21 09:28:39 +0000254 if (defined $config_write) {
255 print $config_write $line or die "write $config_file: $!\n";
256 }
Manuel Pégourié-Gonnardab3d8622014-07-12 03:19:18 +0200257}
258
Azim Khan23399662017-12-21 09:28:39 +0000259# Did the set command work?
260if ($action eq "set" && $force_option && !$done) {
Manuel Pégourié-Gonnardab3d8622014-07-12 03:19:18 +0200261
Azim Khan23399662017-12-21 09:28:39 +0000262 # 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
271if (defined $config_write) {
272 close $config_write or die "close $config_file: $!\n";
273}
274
275if ($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
287if ($action eq "full" && !$done) {
288 die "Configuration section was not found in $config_file\n";
289
290}
291
292if ($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é-Gonnardab3d8622014-07-12 03:19:18 +0200295
296__END__