blob: ea7782108ce359a29044129ad0eab7bece4b1734 [file] [log] [blame]
Manuel Pégourié-Gonnardab3d8622014-07-12 03:19:18 +02001#!/usr/bin/perl
SimonB60f2cf92016-04-03 14:16:08 +01002#
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#
Janos Follath53de7842016-06-08 15:29:18 +010021# MBEDTLS_TEST_WO_ENTROPY
SimonB60f2cf92016-04-03 14:16:08 +010022# 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é-Gonnardab3d8622014-07-12 03:19:18 +020039
40use warnings;
41use strict;
42
SimonB60f2cf92016-04-03 14:16:08 +010043my $config_file = "include/mbedtls/config.h";
Manuel Pégourié-Gonnardab3d8622014-07-12 03:19:18 +020044my $usage = <<EOU;
SimonB60f2cf92016-04-03 14:16:08 +010045$0 [-f <file> | --file <file>] [-o | --force]
46 [set <symbol> <value> | unset <symbol> | full | realfull]
Manuel Pégourié-Gonnardab3d8622014-07-12 03:19:18 +020047
SimonB60f2cf92016-04-03 14:16:08 +010048Commands
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
61Options
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
70EOU
71
Manuel Pégourié-Gonnardab3d8622014-07-12 03:19:18 +020072my @excluded = qw(
Janos Follath53de7842016-06-08 15:29:18 +010073MBEDTLS_TEST_WO_ENTROPY
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020074MBEDTLS_DEPRECATED_REMOVED
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020075MBEDTLS_HAVE_SSE2
76MBEDTLS_PLATFORM_NO_STD_FUNCTIONS
77MBEDTLS_ECP_DP_M221_ENABLED
78MBEDTLS_ECP_DP_M383_ENABLED
79MBEDTLS_ECP_DP_M511_ENABLED
80MBEDTLS_NO_DEFAULT_ENTROPY_SOURCES
81MBEDTLS_NO_PLATFORM_ENTROPY
82MBEDTLS_REMOVE_ARC4_CIPHERSUITES
83MBEDTLS_SSL_HW_RECORD_ACCEL
84MBEDTLS_X509_ALLOW_EXTENSIONS_NON_V3
85MBEDTLS_X509_ALLOW_UNSUPPORTED_CRITICAL_EXTENSION
86MBEDTLS_ZLIB_SUPPORT
87MBEDTLS_PKCS11_C
Manuel Pégourié-Gonnardab3d8622014-07-12 03:19:18 +020088_ALT\s*$
89);
90
Manuel Pégourié-Gonnardb7527152015-06-03 09:59:06 +010091# Things that should be enabled in "full" even if they match @excluded
92my @non_excluded = qw(
93PLATFORM_[A-Z0-9]+_ALT
94);
95
SimonB60f2cf92016-04-03 14:16:08 +010096# Process the command line arguments
Manuel Pégourié-Gonnardab3d8622014-07-12 03:19:18 +020097
SimonB60f2cf92016-04-03 14:16:08 +010098my $force_option = 0;
Manuel Pégourié-Gonnardab3d8622014-07-12 03:19:18 +020099
SimonB60f2cf92016-04-03 14:16:08 +0100100my ($arg, $name, $value, $action);
101
SimonB73883c12016-04-04 13:49:10 +0100102while ($arg = shift) {
SimonB60f2cf92016-04-03 14:16:08 +0100103
104 # Check if the argument is an option
SimonB73883c12016-04-04 13:49:10 +0100105 if ($arg eq "-f" || $arg eq "--file") {
SimonB60f2cf92016-04-03 14:16:08 +0100106 $config_file = shift;
107
108 -f $config_file or die "No such file: $config_file\n";
109
110 }
SimonB73883c12016-04-04 13:49:10 +0100111 elsif ($arg eq "-o" || $arg eq "--force") {
SimonB60f2cf92016-04-03 14:16:08 +0100112 $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é-Gonnardab3d8622014-07-12 03:19:18 +0200139 }
140}
141
SimonB60f2cf92016-04-03 14:16:08 +0100142# Check the config file is present
143if (! -f $config_file) {
Manuel Pégourié-Gonnardab3d8622014-07-12 03:19:18 +0200144
SimonB60f2cf92016-04-03 14:16:08 +0100145 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é-Gonnardab3d8622014-07-12 03:19:18 +0200151}
SimonB60f2cf92016-04-03 14:16:08 +0100152
153
154# Now read the file and process the contents
Manuel Pégourié-Gonnardab3d8622014-07-12 03:19:18 +0200155
156open my $config_read, '<', $config_file or die "read $config_file: $!\n";
157my @config_lines = <$config_read>;
158close $config_read;
159
Manuel Pégourié-Gonnard1989caf2016-01-04 12:57:32 +0100160my ($exclude_re, $no_exclude_re);
161if ($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é-Gonnardab3d8622014-07-12 03:19:18 +0200168
169open my $config_write, '>', $config_file or die "write $config_file: $!\n";
170
171my $done;
172for my $line (@config_lines) {
Manuel Pégourié-Gonnard1989caf2016-01-04 12:57:32 +0100173 if ($action eq "full" || $action eq "realfull") {
Manuel Pégourié-Gonnardab3d8622014-07-12 03:19:18 +0200174 if ($line =~ /name SECTION: Module configuration options/) {
175 $done = 1;
176 }
177
Manuel Pégourié-Gonnardb7527152015-06-03 09:59:06 +0100178 if (!$done && $line =~ m!^//\s?#define! &&
179 ( $line !~ /$exclude_re/ || $line =~ /$no_exclude_re/ ) ) {
Manuel Pégourié-Gonnardea0920f2015-03-24 09:50:15 +0100180 $line =~ s!^//\s?!!;
181 }
Manuel Pégourié-Gonnard7ee5ddd2015-06-03 10:33:55 +0100182 if (!$done && $line =~ m!^\s?#define! &&
183 ! ( $line !~ /$exclude_re/ || $line =~ /$no_exclude_re/ ) ) {
Manuel Pégourié-Gonnardea0920f2015-03-24 09:50:15 +0100184 $line =~ s!^!//!;
Manuel Pégourié-Gonnardab3d8622014-07-12 03:19:18 +0200185 }
186 } elsif ($action eq "unset") {
Manuel Pégourié-Gonnard7f9049b2015-06-23 17:42:51 +0200187 if (!$done && $line =~ /^\s*#define\s*$name\b/) {
Manuel Pégourié-Gonnardab3d8622014-07-12 03:19:18 +0200188 $line = '//' . $line;
189 $done = 1;
190 }
191 } elsif (!$done && $action eq "set") {
Manuel Pégourié-Gonnard7f9049b2015-06-23 17:42:51 +0200192 if ($line =~ m!^(?://)?\s*#define\s*$name\b!) {
Manuel Pégourié-Gonnardab3d8622014-07-12 03:19:18 +0200193 $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
SimonB60f2cf92016-04-03 14:16:08 +0100203# Did the set command work?
204if ($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é-Gonnardab3d8622014-07-12 03:19:18 +0200215close $config_write;
216
SimonB60f2cf92016-04-03 14:16:08 +0100217if ($action eq "full" && !$done) {
218 die "Configuration section was not found in $config_file\n";
219
220}
221
222if ($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é-Gonnardab3d8622014-07-12 03:19:18 +0200225
226__END__