blob: a6dcfe7d7f3c0b571626ede89caa1814576693cd [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#
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é-Gonnardab3d8622014-07-12 03:19:18 +020038
39use warnings;
40use strict;
41
SimonB60f2cf92016-04-03 14:16:08 +010042my $config_file = "include/mbedtls/config.h";
Manuel Pégourié-Gonnardab3d8622014-07-12 03:19:18 +020043my $usage = <<EOU;
SimonB60f2cf92016-04-03 14:16:08 +010044$0 [-f <file> | --file <file>] [-o | --force]
45 [set <symbol> <value> | unset <symbol> | full | realfull]
Manuel Pégourié-Gonnardab3d8622014-07-12 03:19:18 +020046
SimonB60f2cf92016-04-03 14:16:08 +010047Commands
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
60Options
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
69EOU
70
Manuel Pégourié-Gonnardab3d8622014-07-12 03:19:18 +020071my @excluded = qw(
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020072MBEDTLS_DEPRECATED_REMOVED
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020073MBEDTLS_HAVE_SSE2
74MBEDTLS_PLATFORM_NO_STD_FUNCTIONS
75MBEDTLS_ECP_DP_M221_ENABLED
76MBEDTLS_ECP_DP_M383_ENABLED
77MBEDTLS_ECP_DP_M511_ENABLED
78MBEDTLS_NO_DEFAULT_ENTROPY_SOURCES
79MBEDTLS_NO_PLATFORM_ENTROPY
80MBEDTLS_REMOVE_ARC4_CIPHERSUITES
81MBEDTLS_SSL_HW_RECORD_ACCEL
82MBEDTLS_X509_ALLOW_EXTENSIONS_NON_V3
83MBEDTLS_X509_ALLOW_UNSUPPORTED_CRITICAL_EXTENSION
84MBEDTLS_ZLIB_SUPPORT
85MBEDTLS_PKCS11_C
Manuel Pégourié-Gonnardab3d8622014-07-12 03:19:18 +020086_ALT\s*$
87);
88
Manuel Pégourié-Gonnardb7527152015-06-03 09:59:06 +010089# Things that should be enabled in "full" even if they match @excluded
90my @non_excluded = qw(
91PLATFORM_[A-Z0-9]+_ALT
92);
93
SimonB60f2cf92016-04-03 14:16:08 +010094# Process the command line arguments
Manuel Pégourié-Gonnardab3d8622014-07-12 03:19:18 +020095
SimonB60f2cf92016-04-03 14:16:08 +010096my $force_option = 0;
Manuel Pégourié-Gonnardab3d8622014-07-12 03:19:18 +020097
SimonB60f2cf92016-04-03 14:16:08 +010098my ($arg, $name, $value, $action);
99
SimonB73883c12016-04-04 13:49:10 +0100100while ($arg = shift) {
SimonB60f2cf92016-04-03 14:16:08 +0100101
102 # Check if the argument is an option
SimonB73883c12016-04-04 13:49:10 +0100103 if ($arg eq "-f" || $arg eq "--file") {
SimonB60f2cf92016-04-03 14:16:08 +0100104 $config_file = shift;
105
106 -f $config_file or die "No such file: $config_file\n";
107
108 }
SimonB73883c12016-04-04 13:49:10 +0100109 elsif ($arg eq "-o" || $arg eq "--force") {
SimonB60f2cf92016-04-03 14:16:08 +0100110 $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é-Gonnardab3d8622014-07-12 03:19:18 +0200137 }
138}
139
SimonB60f2cf92016-04-03 14:16:08 +0100140# Check the config file is present
141if (! -f $config_file) {
Manuel Pégourié-Gonnardab3d8622014-07-12 03:19:18 +0200142
SimonB60f2cf92016-04-03 14:16:08 +0100143 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é-Gonnardab3d8622014-07-12 03:19:18 +0200149}
SimonB60f2cf92016-04-03 14:16:08 +0100150
151
152# Now read the file and process the contents
Manuel Pégourié-Gonnardab3d8622014-07-12 03:19:18 +0200153
154open my $config_read, '<', $config_file or die "read $config_file: $!\n";
155my @config_lines = <$config_read>;
156close $config_read;
157
Manuel Pégourié-Gonnard1989caf2016-01-04 12:57:32 +0100158my ($exclude_re, $no_exclude_re);
159if ($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é-Gonnardab3d8622014-07-12 03:19:18 +0200166
167open my $config_write, '>', $config_file or die "write $config_file: $!\n";
168
169my $done;
170for my $line (@config_lines) {
Manuel Pégourié-Gonnard1989caf2016-01-04 12:57:32 +0100171 if ($action eq "full" || $action eq "realfull") {
Manuel Pégourié-Gonnardab3d8622014-07-12 03:19:18 +0200172 if ($line =~ /name SECTION: Module configuration options/) {
173 $done = 1;
174 }
175
Manuel Pégourié-Gonnardb7527152015-06-03 09:59:06 +0100176 if (!$done && $line =~ m!^//\s?#define! &&
177 ( $line !~ /$exclude_re/ || $line =~ /$no_exclude_re/ ) ) {
Manuel Pégourié-Gonnardea0920f2015-03-24 09:50:15 +0100178 $line =~ s!^//\s?!!;
179 }
Manuel Pégourié-Gonnard7ee5ddd2015-06-03 10:33:55 +0100180 if (!$done && $line =~ m!^\s?#define! &&
181 ! ( $line !~ /$exclude_re/ || $line =~ /$no_exclude_re/ ) ) {
Manuel Pégourié-Gonnardea0920f2015-03-24 09:50:15 +0100182 $line =~ s!^!//!;
Manuel Pégourié-Gonnardab3d8622014-07-12 03:19:18 +0200183 }
184 } elsif ($action eq "unset") {
Manuel Pégourié-Gonnard7f9049b2015-06-23 17:42:51 +0200185 if (!$done && $line =~ /^\s*#define\s*$name\b/) {
Manuel Pégourié-Gonnardab3d8622014-07-12 03:19:18 +0200186 $line = '//' . $line;
187 $done = 1;
188 }
189 } elsif (!$done && $action eq "set") {
Manuel Pégourié-Gonnard7f9049b2015-06-23 17:42:51 +0200190 if ($line =~ m!^(?://)?\s*#define\s*$name\b!) {
Manuel Pégourié-Gonnardab3d8622014-07-12 03:19:18 +0200191 $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
SimonB60f2cf92016-04-03 14:16:08 +0100201# Did the set command work?
202if ($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é-Gonnardab3d8622014-07-12 03:19:18 +0200213close $config_write;
214
SimonB60f2cf92016-04-03 14:16:08 +0100215if ($action eq "full" && !$done) {
216 die "Configuration section was not found in $config_file\n";
217
218}
219
220if ($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é-Gonnardab3d8622014-07-12 03:19:18 +0200223
224__END__