blob: f6ab627fccea30eaffcc3f078157fdcd0bbc1af8 [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
Harry Ramseyc94cb702024-10-31 14:28:53 +000024my $mbedtls_programs_dir = "programs";
25my $tfpsacrypto_programs_dir = "tf-psa-crypto/programs";
26
Darryl Green588e8cb2018-07-20 13:27:58 +010027my $mbedtls_header_dir = 'include/mbedtls';
Ronald Cron3d817ad2024-06-14 08:43:28 +020028my $drivers_builtin_header_dir = 'tf-psa-crypto/drivers/builtin/include/mbedtls';
Ronald Cronc7e9e362024-06-10 09:41:49 +020029my $psa_header_dir = 'tf-psa-crypto/include/psa';
Ronald Cronc29afb62024-07-01 14:50:54 +020030my $tls_source_dir = 'library';
31my $crypto_core_source_dir = 'tf-psa-crypto/core';
32my $crypto_source_dir = 'tf-psa-crypto/drivers/builtin/src';
David Horstmann08953f72024-11-06 16:24:49 +000033my $tls_test_source_dir = 'tests/src';
34my $tls_test_header_dir = 'tests/include/test';
David Horstmann5b93d972024-10-31 15:36:05 +000035my $test_source_dir = 'framework/tests/src';
36my $test_header_dir = 'framework/tests/include/test';
37my $test_drivers_header_dir = 'framework/tests/include/test/drivers';
38my $test_drivers_source_dir = 'framework/tests/src/drivers';
Gilles Peskine13fac982020-02-12 20:34:22 +010039
40my @thirdparty_header_dirs = qw(
Ronald Cronaef8cf32024-07-01 18:33:24 +020041 tf-psa-crypto/drivers/everest/include/everest
Gilles Peskine13fac982020-02-12 20:34:22 +010042);
43my @thirdparty_source_dirs = qw(
Ronald Cronaef8cf32024-07-01 18:33:24 +020044 tf-psa-crypto/drivers/everest/library
45 tf-psa-crypto/drivers/everest/library/kremlib
46 tf-psa-crypto/drivers/everest/library/legacy
Gilles Peskine13fac982020-02-12 20:34:22 +010047);
Gilles Peskineb41f88f2020-02-12 20:45:18 +010048
Gilles Peskine7156d8c2020-02-19 20:08:44 +010049# Directories to add to the include path.
50# Order matters in case there are files with the same name in more than
51# one directory: the compiler will use the first match.
52my @include_directories = qw(
53 include
Ronald Cronc7e9e362024-06-10 09:41:49 +020054 tf-psa-crypto/include
Ronald Cron3d817ad2024-06-14 08:43:28 +020055 tf-psa-crypto/drivers/builtin/include
Ronald Cronaef8cf32024-07-01 18:33:24 +020056 tf-psa-crypto/drivers/everest/include/
57 tf-psa-crypto/drivers/everest/include/everest
58 tf-psa-crypto/drivers/everest/include/everest/vs2013
59 tf-psa-crypto/drivers/everest/include/everest/kremlib
David Horstmanna2446872024-11-08 12:23:28 +000060 tests/include
David Horstmann5b93d972024-10-31 15:36:05 +000061 framework/tests/include
Gilles Peskine7156d8c2020-02-19 20:08:44 +010062);
63my $include_directories = join(';', map {"../../$_"} @include_directories);
64
Ronald Cronc29afb62024-07-01 14:50:54 +020065# Directories to add to the include path when building the libraries, but not
Gilles Peskine66c3dc42020-06-03 02:25:17 +020066# when building tests or applications.
67my @library_include_directories = qw(
68 library
Ronald Cronc29afb62024-07-01 14:50:54 +020069 tf-psa-crypto/core
70 tf-psa-crypto/drivers/builtin/src
Gilles Peskine66c3dc42020-06-03 02:25:17 +020071);
72my $library_include_directories =
73 join(';', map {"../../$_"} (@library_include_directories,
74 @include_directories));
75
Gilles Peskineb41f88f2020-02-12 20:45:18 +010076my @excluded_files = qw(
Ronald Cronaef8cf32024-07-01 18:33:24 +020077 tf-psa-crypto/drivers/everest/library/Hacl_Curve25519.c
Gilles Peskine13fac982020-02-12 20:34:22 +010078);
Gilles Peskineb41f88f2020-02-12 20:45:18 +010079my %excluded_files = ();
80foreach (@excluded_files) { $excluded_files{$_} = 1 }
Manuel Pégourié-Gonnard0aafa5c2014-05-08 11:33:30 +020081
Manuel Pégourié-Gonnard0aafa5c2014-05-08 11:33:30 +020082my $vsx_hdr_tpl = <<EOT;
Gilles Peskine6e245c62021-04-22 20:09:25 +020083 <ClInclude Include="..\\..\\{NAME}" />
Manuel Pégourié-Gonnard0aafa5c2014-05-08 11:33:30 +020084EOT
85my $vsx_src_tpl = <<EOT;
Gilles Peskine6e245c62021-04-22 20:09:25 +020086 <ClCompile Include="..\\..\\{NAME}" />
Manuel Pégourié-Gonnard0aafa5c2014-05-08 11:33:30 +020087EOT
Manuel Pégourié-Gonnard1b578782013-09-16 13:33:42 +020088
Manuel Pégourié-Gonnard0598faf2014-05-09 12:56:03 +020089my $vsx_sln_app_entry_tpl = <<EOT;
Gilles Peskine6e245c62021-04-22 20:09:25 +020090Project("{8BC9CEB8-8B4A-11D0-8D11-00A0C91BC942}") = "{APPNAME}", "{APPNAME}.vcxproj", "{GUID}"
91 ProjectSection(ProjectDependencies) = postProject
92 {46CF2D25-6A36-4189-B59C-E4815388E554} = {46CF2D25-6A36-4189-B59C-E4815388E554}
93 EndProjectSection
94EndProject
Manuel Pégourié-Gonnard0598faf2014-05-09 12:56:03 +020095EOT
96
97my $vsx_sln_conf_entry_tpl = <<EOT;
Gilles Peskine6e245c62021-04-22 20:09:25 +020098 {GUID}.Debug|Win32.ActiveCfg = Debug|Win32
99 {GUID}.Debug|Win32.Build.0 = Debug|Win32
100 {GUID}.Debug|x64.ActiveCfg = Debug|x64
101 {GUID}.Debug|x64.Build.0 = Debug|x64
102 {GUID}.Release|Win32.ActiveCfg = Release|Win32
103 {GUID}.Release|Win32.Build.0 = Release|Win32
104 {GUID}.Release|x64.ActiveCfg = Release|x64
105 {GUID}.Release|x64.Build.0 = Release|x64
Manuel Pégourié-Gonnard0598faf2014-05-09 12:56:03 +0200106EOT
107
Manuel Pégourié-Gonnard1b578782013-09-16 13:33:42 +0200108exit( main() );
109
Manuel Pégourié-Gonnard2d34fe32014-05-07 17:51:20 +0200110sub check_dirs {
Gilles Peskine13fac982020-02-12 20:34:22 +0100111 foreach my $d (@thirdparty_header_dirs, @thirdparty_source_dirs) {
112 if (not (-d $d)) { return 0; }
113 }
Manuel Pégourié-Gonnard51f14be2015-05-14 13:04:03 +0200114 return -d $vsx_dir
Darryl Green588e8cb2018-07-20 13:27:58 +0100115 && -d $mbedtls_header_dir
Ronald Cron3d817ad2024-06-14 08:43:28 +0200116 && -d $drivers_builtin_header_dir
Darryl Green588e8cb2018-07-20 13:27:58 +0100117 && -d $psa_header_dir
Ronald Cronc29afb62024-07-01 14:50:54 +0200118 && -d $tls_source_dir
119 && -d $crypto_core_source_dir
120 && -d $crypto_source_dir
Ronald Cron8dc0af22020-06-05 16:00:22 +0200121 && -d $test_source_dir
David Horstmann08953f72024-11-06 16:24:49 +0000122 && -d $tls_test_source_dir
Steven Cooreman16141ed2021-04-08 10:58:33 +0200123 && -d $test_drivers_source_dir
Ronald Cron8dc0af22020-06-05 16:00:22 +0200124 && -d $test_header_dir
David Horstmann08953f72024-11-06 16:24:49 +0000125 && -d $tls_test_header_dir
Steven Cooremana70d5882020-07-16 20:26:18 +0200126 && -d $test_drivers_header_dir
Harry Ramseyc94cb702024-10-31 14:28:53 +0000127 && -d $mbedtls_programs_dir
128 && -d $tfpsacrypto_programs_dir;
Manuel Pégourié-Gonnard2d34fe32014-05-07 17:51:20 +0200129}
130
Manuel Pégourié-Gonnard1b578782013-09-16 13:33:42 +0200131sub slurp_file {
132 my ($filename) = @_;
133
134 local $/ = undef;
Gilles Peskine6e245c62021-04-22 20:09:25 +0200135 open my $fh, '<:crlf', $filename or die "Could not read $filename\n";
Manuel Pégourié-Gonnard1b578782013-09-16 13:33:42 +0200136 my $content = <$fh>;
137 close $fh;
138
139 return $content;
140}
141
Manuel Pégourié-Gonnard411f73e2014-05-09 13:00:18 +0200142sub content_to_file {
143 my ($content, $filename) = @_;
144
Gilles Peskine6e245c62021-04-22 20:09:25 +0200145 open my $fh, '>:crlf', $filename or die "Could not write to $filename\n";
Manuel Pégourié-Gonnard411f73e2014-05-09 13:00:18 +0200146 print $fh $content;
147 close $fh;
148}
149
Manuel Pégourié-Gonnard41e8b622014-05-09 12:27:49 +0200150sub gen_app_guid {
151 my ($path) = @_;
152
Manuel Pégourié-Gonnard813e5852015-01-26 15:42:27 +0000153 my $guid = md5_hex( "mbedTLS:$path" );
Manuel Pégourié-Gonnard41e8b622014-05-09 12:27:49 +0200154 $guid =~ s/(.{8})(.{4})(.{4})(.{4})(.{12})/\U{$1-$2-$3-$4-$5}/;
155
156 return $guid;
157}
158
Manuel Pégourié-Gonnard1b578782013-09-16 13:33:42 +0200159sub gen_app {
160 my ($path, $template, $dir, $ext) = @_;
161
Manuel Pégourié-Gonnard41e8b622014-05-09 12:27:49 +0200162 my $guid = gen_app_guid( $path );
Manuel Pégourié-Gonnard1b578782013-09-16 13:33:42 +0200163 $path =~ s!/!\\!g;
164 (my $appname = $path) =~ s/.*\\//;
Gilles Peskinea1023e22023-11-03 19:16:56 +0100165 my $is_test_app = ($path =~ m/^test\\/);
Manuel Pégourié-Gonnard1b578782013-09-16 13:33:42 +0200166
Gilles Peskine0f3f9c32020-04-01 13:34:50 +0200167 my $srcs = "<ClCompile Include=\"..\\..\\programs\\$path.c\" \/>";
Andres Amaya Garciac84a65d2018-10-30 21:46:21 +0000168 if( $appname eq "ssl_client2" or $appname eq "ssl_server2" or
169 $appname eq "query_compile_time_config" ) {
Gilles Peskine6e245c62021-04-22 20:09:25 +0200170 $srcs .= "\n <ClCompile Include=\"..\\..\\programs\\test\\query_config.c\" \/>";
Andres Amaya Garciac84a65d2018-10-30 21:46:21 +0000171 }
Gilles Peskinea3ed34f2021-01-05 21:11:16 +0100172 if( $appname eq "ssl_client2" or $appname eq "ssl_server2" ) {
Gilles Peskine6e245c62021-04-22 20:09:25 +0200173 $srcs .= "\n <ClCompile Include=\"..\\..\\programs\\ssl\\ssl_test_lib.c\" \/>";
Gilles Peskinea3ed34f2021-01-05 21:11:16 +0100174 }
Andres Amaya Garciac84a65d2018-10-30 21:46:21 +0000175
Manuel Pégourié-Gonnard1b578782013-09-16 13:33:42 +0200176 my $content = $template;
Andres Amaya Garciac84a65d2018-10-30 21:46:21 +0000177 $content =~ s/<SOURCES>/$srcs/g;
Manuel Pégourié-Gonnard1b578782013-09-16 13:33:42 +0200178 $content =~ s/<APPNAME>/$appname/g;
Manuel Pégourié-Gonnard41e8b622014-05-09 12:27:49 +0200179 $content =~ s/<GUID>/$guid/g;
Gilles Peskinea1023e22023-11-03 19:16:56 +0100180 $content =~ s/INCLUDE_DIRECTORIES\n/($is_test_app ?
181 $library_include_directories :
182 $include_directories)/ge;
Manuel Pégourié-Gonnard1b578782013-09-16 13:33:42 +0200183
Manuel Pégourié-Gonnard411f73e2014-05-09 13:00:18 +0200184 content_to_file( $content, "$dir/$appname.$ext" );
Manuel Pégourié-Gonnard1b578782013-09-16 13:33:42 +0200185}
186
187sub get_app_list {
Gilles Peskineb0fa9d22021-04-22 20:07:44 +0200188 my $makefile_contents = slurp_file('programs/Makefile');
Harry Ramseyc94cb702024-10-31 14:28:53 +0000189 $makefile_contents =~ s/\$\(TF-PSA-CRYPTO_PROGRAM_PATH\)/..\/tf-psa-crypto\/programs/g;
Gilles Peskineb0fa9d22021-04-22 20:07:44 +0200190 $makefile_contents =~ /\n\s*APPS\s*=[\\\s]*(.*?)(?<!\\)[\#\n]/s
191 or die "Cannot find APPS = ... in programs/Makefile\n";
192 return split /(?:\s|\\)+/, $1;
Manuel Pégourié-Gonnard1b578782013-09-16 13:33:42 +0200193}
194
Manuel Pégourié-Gonnard2d34fe32014-05-07 17:51:20 +0200195sub gen_app_files {
Manuel Pégourié-Gonnard0aafa5c2014-05-08 11:33:30 +0200196 my @app_list = @_;
197
Manuel Pégourié-Gonnard2d34fe32014-05-07 17:51:20 +0200198 my $vsx_tpl = slurp_file( $vsx_app_tpl_file );
Manuel Pégourié-Gonnard1b578782013-09-16 13:33:42 +0200199
Manuel Pégourié-Gonnard0aafa5c2014-05-08 11:33:30 +0200200 for my $app ( @app_list ) {
Manuel Pégourié-Gonnard1b578782013-09-16 13:33:42 +0200201 gen_app( $app, $vsx_tpl, $vsx_dir, $vsx_ext );
202 }
Manuel Pégourié-Gonnard2d34fe32014-05-07 17:51:20 +0200203}
204
Manuel Pégourié-Gonnard0aafa5c2014-05-08 11:33:30 +0200205sub gen_entry_list {
Manuel Pégourié-Gonnardcd8f8442014-05-08 12:53:58 +0200206 my ($tpl, @names) = @_;
Manuel Pégourié-Gonnard0aafa5c2014-05-08 11:33:30 +0200207
208 my $entries;
Manuel Pégourié-Gonnardcd8f8442014-05-08 12:53:58 +0200209 for my $name (@names) {
210 (my $entry = $tpl) =~ s/{NAME}/$name/g;
Manuel Pégourié-Gonnard0aafa5c2014-05-08 11:33:30 +0200211 $entries .= $entry;
212 }
213
214 return $entries;
215}
216
217sub gen_main_file {
Gilles Peskineb41f88f2020-02-12 20:45:18 +0100218 my ($headers, $sources,
219 $hdr_tpl, $src_tpl,
220 $main_tpl, $main_out) = @_;
Manuel Pégourié-Gonnard0aafa5c2014-05-08 11:33:30 +0200221
Gilles Peskineb41f88f2020-02-12 20:45:18 +0100222 my $header_entries = gen_entry_list( $hdr_tpl, @$headers );
Manuel Pégourié-Gonnard0aafa5c2014-05-08 11:33:30 +0200223 my $source_entries = gen_entry_list( $src_tpl, @$sources );
224
225 my $out = slurp_file( $main_tpl );
Gilles Peskine6e245c62021-04-22 20:09:25 +0200226 $out =~ s/SOURCE_ENTRIES\n/$source_entries/m;
227 $out =~ s/HEADER_ENTRIES\n/$header_entries/m;
228 $out =~ s/INCLUDE_DIRECTORIES\n/$library_include_directories/g;
Manuel Pégourié-Gonnard0aafa5c2014-05-08 11:33:30 +0200229
Manuel Pégourié-Gonnard411f73e2014-05-09 13:00:18 +0200230 content_to_file( $out, $main_out );
Manuel Pégourié-Gonnard0aafa5c2014-05-08 11:33:30 +0200231}
232
Manuel Pégourié-Gonnard0598faf2014-05-09 12:56:03 +0200233sub gen_vsx_solution {
234 my (@app_names) = @_;
235
236 my ($app_entries, $conf_entries);
237 for my $path (@app_names) {
238 my $guid = gen_app_guid( $path );
239 (my $appname = $path) =~ s!.*/!!;
240
241 my $app_entry = $vsx_sln_app_entry_tpl;
242 $app_entry =~ s/{APPNAME}/$appname/g;
243 $app_entry =~ s/{GUID}/$guid/g;
244
245 $app_entries .= $app_entry;
246
247 my $conf_entry = $vsx_sln_conf_entry_tpl;
248 $conf_entry =~ s/{GUID}/$guid/g;
249
250 $conf_entries .= $conf_entry;
251 }
252
253 my $out = slurp_file( $vsx_sln_tpl_file );
Gilles Peskine6e245c62021-04-22 20:09:25 +0200254 $out =~ s/APP_ENTRIES\n/$app_entries/m;
255 $out =~ s/CONF_ENTRIES\n/$conf_entries/m;
Manuel Pégourié-Gonnard0598faf2014-05-09 12:56:03 +0200256
Manuel Pégourié-Gonnard411f73e2014-05-09 13:00:18 +0200257 content_to_file( $out, $vsx_sln_file );
Manuel Pégourié-Gonnard0598faf2014-05-09 12:56:03 +0200258}
259
Andres Amaya Garcia3c5f9492018-01-11 19:51:27 +0000260sub del_vsx_files {
261 unlink glob "'$vsx_dir/*.$vsx_ext'";
262 unlink $vsx_main_file;
263 unlink $vsx_sln_file;
264}
265
Manuel Pégourié-Gonnard2d34fe32014-05-07 17:51:20 +0200266sub main {
267 if( ! check_dirs() ) {
268 chdir '..' or die;
Dave Rodgman0f459d72023-01-10 15:09:19 +0000269 check_dirs or die "Must be run from Mbed TLS root or scripts dir\n";
Manuel Pégourié-Gonnard2d34fe32014-05-07 17:51:20 +0200270 }
271
Andres Amaya Garcia3c5f9492018-01-11 19:51:27 +0000272 # Remove old files to ensure that, for example, project files from deleted
273 # apps are not kept
274 del_vsx_files();
275
Manuel Pégourié-Gonnard0aafa5c2014-05-08 11:33:30 +0200276 my @app_list = get_app_list();
Gilles Peskineb41f88f2020-02-12 20:45:18 +0100277 my @header_dirs = (
278 $mbedtls_header_dir,
Ronald Cron3d817ad2024-06-14 08:43:28 +0200279 $drivers_builtin_header_dir,
Gilles Peskineb41f88f2020-02-12 20:45:18 +0100280 $psa_header_dir,
Ronald Cron8dc0af22020-06-05 16:00:22 +0200281 $test_header_dir,
David Horstmann08953f72024-11-06 16:24:49 +0000282 $tls_test_header_dir,
Steven Cooremana70d5882020-07-16 20:26:18 +0200283 $test_drivers_header_dir,
Ronald Cronc29afb62024-07-01 14:50:54 +0200284 $tls_source_dir,
285 $crypto_core_source_dir,
286 $crypto_source_dir,
Gilles Peskineb41f88f2020-02-12 20:45:18 +0100287 @thirdparty_header_dirs,
288 );
289 my @headers = (map { <$_/*.h> } @header_dirs);
290 my @source_dirs = (
Ronald Cronc29afb62024-07-01 14:50:54 +0200291 $tls_source_dir,
292 $crypto_core_source_dir,
293 $crypto_source_dir,
Ronald Cron8dc0af22020-06-05 16:00:22 +0200294 $test_source_dir,
David Horstmann08953f72024-11-06 16:24:49 +0000295 $tls_test_source_dir,
Steven Cooreman16141ed2021-04-08 10:58:33 +0200296 $test_drivers_source_dir,
Gilles Peskineb41f88f2020-02-12 20:45:18 +0100297 @thirdparty_source_dirs,
298 );
299 my @sources = (map { <$_/*.c> } @source_dirs);
Manuel Pégourié-Gonnard0aafa5c2014-05-08 11:33:30 +0200300
Gilles Peskineb41f88f2020-02-12 20:45:18 +0100301 @headers = grep { ! $excluded_files{$_} } @headers;
302 @sources = grep { ! $excluded_files{$_} } @sources;
303 map { s!/!\\!g } @headers;
304 map { s!/!\\!g } @sources;
Christoph M. Wintersteiger48d26c22018-12-06 18:59:19 +0000305
Manuel Pégourié-Gonnard0aafa5c2014-05-08 11:33:30 +0200306 gen_app_files( @app_list );
Manuel Pégourié-Gonnard0aafa5c2014-05-08 11:33:30 +0200307
Gilles Peskineb41f88f2020-02-12 20:45:18 +0100308 gen_main_file( \@headers, \@sources,
309 $vsx_hdr_tpl, $vsx_src_tpl,
310 $vsx_main_tpl_file, $vsx_main_file );
Manuel Pégourié-Gonnard1b578782013-09-16 13:33:42 +0200311
Manuel Pégourié-Gonnard0598faf2014-05-09 12:56:03 +0200312 gen_vsx_solution( @app_list );
Manuel Pégourié-Gonnardcd8f8442014-05-08 12:53:58 +0200313
Manuel Pégourié-Gonnard1b578782013-09-16 13:33:42 +0200314 return 0;
315}