blob: 9f28cddffbeecb43cbfe3fe274c5f165653c28a0 [file] [log] [blame]
fbrosson533407a2018-04-04 21:44:29 +00001#!/usr/bin/env perl
Manuel Pégourié-Gonnard1b578782013-09-16 13:33:42 +02002
Dave Rodgman0f459d72023-01-10 15:09:19 +00003# Generate main file, individual apps and solution files for
Bence Szépkúti0719d7c2024-03-12 16:45:55 +01004# MS Visual Studio 2017
Manuel Pégourié-Gonnard2d34fe32014-05-07 17:51:20 +02005#
Dave Rodgman0f459d72023-01-10 15:09:19 +00006# Must be run from Mbed TLS root or scripts directory.
Manuel Pégourié-Gonnard2d34fe32014-05-07 17:51:20 +02007# Takes no argument.
Bence Szépkúti700ee442020-05-26 00:33:31 +02008#
Bence Szépkúti1e148272020-08-07 13:07:28 +02009# Copyright The Mbed TLS Contributors
Dave Rodgman16799db2023-11-02 19:47:20 +000010# SPDX-License-Identifier: Apache-2.0 OR GPL-2.0-or-later
Manuel Pégourié-Gonnard684e9dc2013-09-20 15:11:44 +020011
Manuel Pégourié-Gonnard1b578782013-09-16 13:33:42 +020012use warnings;
13use strict;
Manuel Pégourié-Gonnard41e8b622014-05-09 12:27:49 +020014use Digest::MD5 'md5_hex';
Manuel Pégourié-Gonnard1b578782013-09-16 13:33:42 +020015
Bence Szépkútifac11222024-03-12 16:38:23 +010016my $vsx_dir = "visualc/VS2017";
Manuel Pégourié-Gonnard1b578782013-09-16 13:33:42 +020017my $vsx_ext = "vcxproj";
Bence Szépkútifac11222024-03-12 16:38:23 +010018my $vsx_app_tpl_file = "scripts/data_files/vs2017-app-template.$vsx_ext";
19my $vsx_main_tpl_file = "scripts/data_files/vs2017-main-template.$vsx_ext";
Manuel Pégourié-Gonnard813e5852015-01-26 15:42:27 +000020my $vsx_main_file = "$vsx_dir/mbedTLS.$vsx_ext";
Bence Szépkútifac11222024-03-12 16:38:23 +010021my $vsx_sln_tpl_file = "scripts/data_files/vs2017-sln-template.sln";
Manuel Pégourié-Gonnard813e5852015-01-26 15:42:27 +000022my $vsx_sln_file = "$vsx_dir/mbedTLS.sln";
Manuel Pégourié-Gonnard2d34fe32014-05-07 17:51:20 +020023
24my $programs_dir = 'programs';
Harry Ramsey03f49572025-02-17 12:04:23 +000025my $framework_programs_dir = 'framework/tests/programs';
Darryl Green588e8cb2018-07-20 13:27:58 +010026my $mbedtls_header_dir = 'include/mbedtls';
27my $psa_header_dir = 'include/psa';
Manuel Pégourié-Gonnard0aafa5c2014-05-08 11:33:30 +020028my $source_dir = 'library';
David Horstmann2ab59f02024-11-06 16:24:49 +000029my $tls_test_source_dir = 'tests/src';
30my $tls_test_header_dir = 'tests/include/test';
David Horstmanndcf42a02024-11-08 14:40:12 +000031my $test_source_dir = 'framework/tests/src';
32my $test_header_dir = 'framework/tests/include/test';
33my $test_drivers_header_dir = 'framework/tests/include/test/drivers';
34my $test_drivers_source_dir = 'framework/tests/src/drivers';
Gilles Peskine13fac982020-02-12 20:34:22 +010035
36my @thirdparty_header_dirs = qw(
37 3rdparty/everest/include/everest
38);
39my @thirdparty_source_dirs = qw(
40 3rdparty/everest/library
41 3rdparty/everest/library/kremlib
42 3rdparty/everest/library/legacy
43);
Gilles Peskineb41f88f2020-02-12 20:45:18 +010044
Gilles Peskine7156d8c2020-02-19 20:08:44 +010045# Directories to add to the include path.
46# Order matters in case there are files with the same name in more than
47# one directory: the compiler will use the first match.
48my @include_directories = qw(
49 include
50 3rdparty/everest/include/
51 3rdparty/everest/include/everest
Dave Rodgman378ecdd2023-01-10 15:08:30 +000052 3rdparty/everest/include/everest/vs2013
Gilles Peskine7156d8c2020-02-19 20:08:44 +010053 3rdparty/everest/include/everest/kremlib
David Horstmann0f33bfa2024-11-08 12:23:28 +000054 tests/include
David Horstmanndcf42a02024-11-08 14:40:12 +000055 framework/tests/include
Harry Ramsey03f49572025-02-17 12:04:23 +000056 framework/tests/programs
Gilles Peskine7156d8c2020-02-19 20:08:44 +010057);
58my $include_directories = join(';', map {"../../$_"} @include_directories);
59
Gilles Peskine66c3dc42020-06-03 02:25:17 +020060# Directories to add to the include path when building the library, but not
61# when building tests or applications.
62my @library_include_directories = qw(
63 library
64);
65my $library_include_directories =
66 join(';', map {"../../$_"} (@library_include_directories,
67 @include_directories));
68
Gilles Peskineb41f88f2020-02-12 20:45:18 +010069my @excluded_files = qw(
Gilles Peskine13fac982020-02-12 20:34:22 +010070 3rdparty/everest/library/Hacl_Curve25519.c
71);
Gilles Peskineb41f88f2020-02-12 20:45:18 +010072my %excluded_files = ();
73foreach (@excluded_files) { $excluded_files{$_} = 1 }
Manuel Pégourié-Gonnard0aafa5c2014-05-08 11:33:30 +020074
Manuel Pégourié-Gonnard0aafa5c2014-05-08 11:33:30 +020075my $vsx_hdr_tpl = <<EOT;
Gilles Peskine6e245c62021-04-22 20:09:25 +020076 <ClInclude Include="..\\..\\{NAME}" />
Manuel Pégourié-Gonnard0aafa5c2014-05-08 11:33:30 +020077EOT
78my $vsx_src_tpl = <<EOT;
Gilles Peskine6e245c62021-04-22 20:09:25 +020079 <ClCompile Include="..\\..\\{NAME}" />
Manuel Pégourié-Gonnard0aafa5c2014-05-08 11:33:30 +020080EOT
Manuel Pégourié-Gonnard1b578782013-09-16 13:33:42 +020081
Manuel Pégourié-Gonnard0598faf2014-05-09 12:56:03 +020082my $vsx_sln_app_entry_tpl = <<EOT;
Gilles Peskine6e245c62021-04-22 20:09:25 +020083Project("{8BC9CEB8-8B4A-11D0-8D11-00A0C91BC942}") = "{APPNAME}", "{APPNAME}.vcxproj", "{GUID}"
84 ProjectSection(ProjectDependencies) = postProject
85 {46CF2D25-6A36-4189-B59C-E4815388E554} = {46CF2D25-6A36-4189-B59C-E4815388E554}
86 EndProjectSection
87EndProject
Manuel Pégourié-Gonnard0598faf2014-05-09 12:56:03 +020088EOT
89
90my $vsx_sln_conf_entry_tpl = <<EOT;
Gilles Peskine6e245c62021-04-22 20:09:25 +020091 {GUID}.Debug|Win32.ActiveCfg = Debug|Win32
92 {GUID}.Debug|Win32.Build.0 = Debug|Win32
93 {GUID}.Debug|x64.ActiveCfg = Debug|x64
94 {GUID}.Debug|x64.Build.0 = Debug|x64
95 {GUID}.Release|Win32.ActiveCfg = Release|Win32
96 {GUID}.Release|Win32.Build.0 = Release|Win32
97 {GUID}.Release|x64.ActiveCfg = Release|x64
98 {GUID}.Release|x64.Build.0 = Release|x64
Manuel Pégourié-Gonnard0598faf2014-05-09 12:56:03 +020099EOT
100
Manuel Pégourié-Gonnard1b578782013-09-16 13:33:42 +0200101exit( main() );
102
Manuel Pégourié-Gonnard2d34fe32014-05-07 17:51:20 +0200103sub check_dirs {
Gilles Peskine13fac982020-02-12 20:34:22 +0100104 foreach my $d (@thirdparty_header_dirs, @thirdparty_source_dirs) {
105 if (not (-d $d)) { return 0; }
106 }
Manuel Pégourié-Gonnard51f14be2015-05-14 13:04:03 +0200107 return -d $vsx_dir
Darryl Green588e8cb2018-07-20 13:27:58 +0100108 && -d $mbedtls_header_dir
109 && -d $psa_header_dir
Manuel Pégourié-Gonnard0aafa5c2014-05-08 11:33:30 +0200110 && -d $source_dir
Ronald Cron8dc0af22020-06-05 16:00:22 +0200111 && -d $test_source_dir
David Horstmann2ab59f02024-11-06 16:24:49 +0000112 && -d $tls_test_source_dir
Steven Cooreman16141ed2021-04-08 10:58:33 +0200113 && -d $test_drivers_source_dir
Ronald Cron8dc0af22020-06-05 16:00:22 +0200114 && -d $test_header_dir
David Horstmann2ab59f02024-11-06 16:24:49 +0000115 && -d $tls_test_header_dir
Steven Cooremana70d5882020-07-16 20:26:18 +0200116 && -d $test_drivers_header_dir
Harry Ramsey03f49572025-02-17 12:04:23 +0000117 && -d $programs_dir
118 && -d $framework_programs_dir;
Manuel Pégourié-Gonnard2d34fe32014-05-07 17:51:20 +0200119}
120
Manuel Pégourié-Gonnard1b578782013-09-16 13:33:42 +0200121sub slurp_file {
122 my ($filename) = @_;
123
124 local $/ = undef;
Gilles Peskine6e245c62021-04-22 20:09:25 +0200125 open my $fh, '<:crlf', $filename or die "Could not read $filename\n";
Manuel Pégourié-Gonnard1b578782013-09-16 13:33:42 +0200126 my $content = <$fh>;
127 close $fh;
128
129 return $content;
130}
131
Manuel Pégourié-Gonnard411f73e2014-05-09 13:00:18 +0200132sub content_to_file {
133 my ($content, $filename) = @_;
134
Gilles Peskine6e245c62021-04-22 20:09:25 +0200135 open my $fh, '>:crlf', $filename or die "Could not write to $filename\n";
Manuel Pégourié-Gonnard411f73e2014-05-09 13:00:18 +0200136 print $fh $content;
137 close $fh;
138}
139
Manuel Pégourié-Gonnard41e8b622014-05-09 12:27:49 +0200140sub gen_app_guid {
141 my ($path) = @_;
142
Manuel Pégourié-Gonnard813e5852015-01-26 15:42:27 +0000143 my $guid = md5_hex( "mbedTLS:$path" );
Manuel Pégourié-Gonnard41e8b622014-05-09 12:27:49 +0200144 $guid =~ s/(.{8})(.{4})(.{4})(.{4})(.{12})/\U{$1-$2-$3-$4-$5}/;
145
146 return $guid;
147}
148
Manuel Pégourié-Gonnard1b578782013-09-16 13:33:42 +0200149sub gen_app {
150 my ($path, $template, $dir, $ext) = @_;
151
Manuel Pégourié-Gonnard41e8b622014-05-09 12:27:49 +0200152 my $guid = gen_app_guid( $path );
Manuel Pégourié-Gonnard1b578782013-09-16 13:33:42 +0200153 $path =~ s!/!\\!g;
154 (my $appname = $path) =~ s/.*\\//;
Gilles Peskinea1023e22023-11-03 19:16:56 +0100155 my $is_test_app = ($path =~ m/^test\\/);
Manuel Pégourié-Gonnard1b578782013-09-16 13:33:42 +0200156
Harry Ramsey03f49572025-02-17 12:04:23 +0000157 my $srcs;
158 if( $appname eq "metatest" or $appname eq "query_compile_time_config" or
159 $appname eq "query_included_headers" or $appname eq "zeroize" ) {
160 $srcs = "<ClCompile Include=\"..\\..\\framework\\tests\\programs\\$appname.c\" \/>";
161 } else {
162 $srcs = "<ClCompile Include=\"..\\..\\programs\\$path.c\" \/>";
163 }
164
Andres Amaya Garciac84a65d2018-10-30 21:46:21 +0000165 if( $appname eq "ssl_client2" or $appname eq "ssl_server2" or
166 $appname eq "query_compile_time_config" ) {
Gilles Peskine6e245c62021-04-22 20:09:25 +0200167 $srcs .= "\n <ClCompile Include=\"..\\..\\programs\\test\\query_config.c\" \/>";
Andres Amaya Garciac84a65d2018-10-30 21:46:21 +0000168 }
Gilles Peskinea3ed34f2021-01-05 21:11:16 +0100169 if( $appname eq "ssl_client2" or $appname eq "ssl_server2" ) {
Gilles Peskine6e245c62021-04-22 20:09:25 +0200170 $srcs .= "\n <ClCompile Include=\"..\\..\\programs\\ssl\\ssl_test_lib.c\" \/>";
Gilles Peskinea3ed34f2021-01-05 21:11:16 +0100171 }
Andres Amaya Garciac84a65d2018-10-30 21:46:21 +0000172
Manuel Pégourié-Gonnard1b578782013-09-16 13:33:42 +0200173 my $content = $template;
Andres Amaya Garciac84a65d2018-10-30 21:46:21 +0000174 $content =~ s/<SOURCES>/$srcs/g;
Manuel Pégourié-Gonnard1b578782013-09-16 13:33:42 +0200175 $content =~ s/<APPNAME>/$appname/g;
Manuel Pégourié-Gonnard41e8b622014-05-09 12:27:49 +0200176 $content =~ s/<GUID>/$guid/g;
Gilles Peskinea1023e22023-11-03 19:16:56 +0100177 $content =~ s/INCLUDE_DIRECTORIES\n/($is_test_app ?
178 $library_include_directories :
179 $include_directories)/ge;
Manuel Pégourié-Gonnard1b578782013-09-16 13:33:42 +0200180
Manuel Pégourié-Gonnard411f73e2014-05-09 13:00:18 +0200181 content_to_file( $content, "$dir/$appname.$ext" );
Manuel Pégourié-Gonnard1b578782013-09-16 13:33:42 +0200182}
183
184sub get_app_list {
Gilles Peskineb0fa9d22021-04-22 20:07:44 +0200185 my $makefile_contents = slurp_file('programs/Makefile');
186 $makefile_contents =~ /\n\s*APPS\s*=[\\\s]*(.*?)(?<!\\)[\#\n]/s
187 or die "Cannot find APPS = ... in programs/Makefile\n";
188 return split /(?:\s|\\)+/, $1;
Manuel Pégourié-Gonnard1b578782013-09-16 13:33:42 +0200189}
190
Manuel Pégourié-Gonnard2d34fe32014-05-07 17:51:20 +0200191sub gen_app_files {
Manuel Pégourié-Gonnard0aafa5c2014-05-08 11:33:30 +0200192 my @app_list = @_;
193
Manuel Pégourié-Gonnard2d34fe32014-05-07 17:51:20 +0200194 my $vsx_tpl = slurp_file( $vsx_app_tpl_file );
Manuel Pégourié-Gonnard1b578782013-09-16 13:33:42 +0200195
Manuel Pégourié-Gonnard0aafa5c2014-05-08 11:33:30 +0200196 for my $app ( @app_list ) {
Manuel Pégourié-Gonnard1b578782013-09-16 13:33:42 +0200197 gen_app( $app, $vsx_tpl, $vsx_dir, $vsx_ext );
198 }
Manuel Pégourié-Gonnard2d34fe32014-05-07 17:51:20 +0200199}
200
Manuel Pégourié-Gonnard0aafa5c2014-05-08 11:33:30 +0200201sub gen_entry_list {
Manuel Pégourié-Gonnardcd8f8442014-05-08 12:53:58 +0200202 my ($tpl, @names) = @_;
Manuel Pégourié-Gonnard0aafa5c2014-05-08 11:33:30 +0200203
204 my $entries;
Manuel Pégourié-Gonnardcd8f8442014-05-08 12:53:58 +0200205 for my $name (@names) {
206 (my $entry = $tpl) =~ s/{NAME}/$name/g;
Manuel Pégourié-Gonnard0aafa5c2014-05-08 11:33:30 +0200207 $entries .= $entry;
208 }
209
210 return $entries;
211}
212
213sub gen_main_file {
Gilles Peskineb41f88f2020-02-12 20:45:18 +0100214 my ($headers, $sources,
215 $hdr_tpl, $src_tpl,
216 $main_tpl, $main_out) = @_;
Manuel Pégourié-Gonnard0aafa5c2014-05-08 11:33:30 +0200217
Gilles Peskineb41f88f2020-02-12 20:45:18 +0100218 my $header_entries = gen_entry_list( $hdr_tpl, @$headers );
Manuel Pégourié-Gonnard0aafa5c2014-05-08 11:33:30 +0200219 my $source_entries = gen_entry_list( $src_tpl, @$sources );
220
221 my $out = slurp_file( $main_tpl );
Gilles Peskine6e245c62021-04-22 20:09:25 +0200222 $out =~ s/SOURCE_ENTRIES\n/$source_entries/m;
223 $out =~ s/HEADER_ENTRIES\n/$header_entries/m;
224 $out =~ s/INCLUDE_DIRECTORIES\n/$library_include_directories/g;
Manuel Pégourié-Gonnard0aafa5c2014-05-08 11:33:30 +0200225
Manuel Pégourié-Gonnard411f73e2014-05-09 13:00:18 +0200226 content_to_file( $out, $main_out );
Manuel Pégourié-Gonnard0aafa5c2014-05-08 11:33:30 +0200227}
228
Manuel Pégourié-Gonnard0598faf2014-05-09 12:56:03 +0200229sub gen_vsx_solution {
230 my (@app_names) = @_;
231
232 my ($app_entries, $conf_entries);
233 for my $path (@app_names) {
234 my $guid = gen_app_guid( $path );
235 (my $appname = $path) =~ s!.*/!!;
236
237 my $app_entry = $vsx_sln_app_entry_tpl;
238 $app_entry =~ s/{APPNAME}/$appname/g;
239 $app_entry =~ s/{GUID}/$guid/g;
240
241 $app_entries .= $app_entry;
242
243 my $conf_entry = $vsx_sln_conf_entry_tpl;
244 $conf_entry =~ s/{GUID}/$guid/g;
245
246 $conf_entries .= $conf_entry;
247 }
248
249 my $out = slurp_file( $vsx_sln_tpl_file );
Gilles Peskine6e245c62021-04-22 20:09:25 +0200250 $out =~ s/APP_ENTRIES\n/$app_entries/m;
251 $out =~ s/CONF_ENTRIES\n/$conf_entries/m;
Manuel Pégourié-Gonnard0598faf2014-05-09 12:56:03 +0200252
Manuel Pégourié-Gonnard411f73e2014-05-09 13:00:18 +0200253 content_to_file( $out, $vsx_sln_file );
Manuel Pégourié-Gonnard0598faf2014-05-09 12:56:03 +0200254}
255
Andres Amaya Garcia3c5f9492018-01-11 19:51:27 +0000256sub del_vsx_files {
257 unlink glob "'$vsx_dir/*.$vsx_ext'";
258 unlink $vsx_main_file;
259 unlink $vsx_sln_file;
260}
261
Manuel Pégourié-Gonnard2d34fe32014-05-07 17:51:20 +0200262sub main {
263 if( ! check_dirs() ) {
264 chdir '..' or die;
Dave Rodgman0f459d72023-01-10 15:09:19 +0000265 check_dirs or die "Must be run from Mbed TLS root or scripts dir\n";
Manuel Pégourié-Gonnard2d34fe32014-05-07 17:51:20 +0200266 }
267
Andres Amaya Garcia3c5f9492018-01-11 19:51:27 +0000268 # Remove old files to ensure that, for example, project files from deleted
269 # apps are not kept
270 del_vsx_files();
271
Manuel Pégourié-Gonnard0aafa5c2014-05-08 11:33:30 +0200272 my @app_list = get_app_list();
Gilles Peskineb41f88f2020-02-12 20:45:18 +0100273 my @header_dirs = (
274 $mbedtls_header_dir,
275 $psa_header_dir,
Ronald Cron8dc0af22020-06-05 16:00:22 +0200276 $test_header_dir,
David Horstmann2ab59f02024-11-06 16:24:49 +0000277 $tls_test_header_dir,
Steven Cooremana70d5882020-07-16 20:26:18 +0200278 $test_drivers_header_dir,
Gilles Peskineb41f88f2020-02-12 20:45:18 +0100279 $source_dir,
Harry Ramsey03f49572025-02-17 12:04:23 +0000280 $framework_programs_dir,
Gilles Peskineb41f88f2020-02-12 20:45:18 +0100281 @thirdparty_header_dirs,
282 );
283 my @headers = (map { <$_/*.h> } @header_dirs);
284 my @source_dirs = (
285 $source_dir,
Ronald Cron8dc0af22020-06-05 16:00:22 +0200286 $test_source_dir,
David Horstmann2ab59f02024-11-06 16:24:49 +0000287 $tls_test_source_dir,
Steven Cooreman16141ed2021-04-08 10:58:33 +0200288 $test_drivers_source_dir,
Gilles Peskineb41f88f2020-02-12 20:45:18 +0100289 @thirdparty_source_dirs,
290 );
291 my @sources = (map { <$_/*.c> } @source_dirs);
Manuel Pégourié-Gonnard0aafa5c2014-05-08 11:33:30 +0200292
Gilles Peskineb41f88f2020-02-12 20:45:18 +0100293 @headers = grep { ! $excluded_files{$_} } @headers;
294 @sources = grep { ! $excluded_files{$_} } @sources;
295 map { s!/!\\!g } @headers;
296 map { s!/!\\!g } @sources;
Christoph M. Wintersteiger48d26c22018-12-06 18:59:19 +0000297
Manuel Pégourié-Gonnard0aafa5c2014-05-08 11:33:30 +0200298 gen_app_files( @app_list );
Manuel Pégourié-Gonnard0aafa5c2014-05-08 11:33:30 +0200299
Gilles Peskineb41f88f2020-02-12 20:45:18 +0100300 gen_main_file( \@headers, \@sources,
301 $vsx_hdr_tpl, $vsx_src_tpl,
302 $vsx_main_tpl_file, $vsx_main_file );
Manuel Pégourié-Gonnard1b578782013-09-16 13:33:42 +0200303
Manuel Pégourié-Gonnard0598faf2014-05-09 12:56:03 +0200304 gen_vsx_solution( @app_list );
Manuel Pégourié-Gonnardcd8f8442014-05-08 12:53:58 +0200305
Manuel Pégourié-Gonnard1b578782013-09-16 13:33:42 +0200306 return 0;
307}