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